-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce_spikes.cpp
More file actions
191 lines (166 loc) · 6.5 KB
/
Copy pathforce_spikes.cpp
File metadata and controls
191 lines (166 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cxxopts.hpp>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "di_common/di_common.h"
namespace {
volatile std::sig_atomic_t g_shouldStop = 0;
void HandleSignal(int) { g_shouldStop = 1; }
} // namespace
int main(int argc, char** argv) {
cxxopts::Options options(
"force_spikes",
"Create a background sine effect with periodic constant-force spikes");
options.add_options()("b,background_strength",
"Background sine strength percentage, from 0 to 100.",
cxxopts::value<int>()->default_value("10"))(
"s,spike_duration_ms",
"Duration of each spike in milliseconds, from 1 to 10000.",
cxxopts::value<int>()->default_value("30"))(
"d,spike_delay_ms",
"Delay between spikes in milliseconds, greater than 1.",
cxxopts::value<int>()->default_value("500"))("h,help",
"Print help");
try {
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}
std::signal(SIGINT, HandleSignal);
int backgroundStrength = result["background_strength"].as<int>();
int spikeDurationMs = result["spike_duration_ms"].as<int>();
int spikeDelayMs = result["spike_delay_ms"].as<int>();
if (backgroundStrength < 0 || backgroundStrength > 100) {
std::cerr << "background_strength must be between 0 and 100." << std::endl;
return 2;
}
if (spikeDurationMs < 1 || spikeDurationMs > 10000) {
std::cerr << "spike_duration_ms must be between 1 and 10000." << std::endl;
return 2;
}
if (spikeDelayMs <= 1) {
std::cerr << "spike_delay_ms must be greater than 1." << std::endl;
return 2;
}
std::cout << "Background sine strength: " << backgroundStrength << "%"
<< std::endl;
std::cout << "Spike duration: " << spikeDurationMs << " ms" << std::endl;
std::cout << "Spike delay: " << spikeDelayMs << " ms" << std::endl;
IDirectInput8* directInput = nullptr;
std::vector<IDirectInputDevice8*> forceFeedbackDevices;
HRESULT hr =
DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&directInput, nullptr);
if (!di_common::CheckDInputResult(hr, "DirectInput8Create")) {
return 1;
}
if (!directInput) {
std::cerr << "directInput unexpectedly null" << std::endl;
return 1;
}
hr = directInput->EnumDevices(
DI8DEVCLASS_GAMECTRL, di_common::EnumDevicesCallback,
&forceFeedbackDevices, DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK);
if (!di_common::CheckDInputResult(hr, "EnumDevices")) {
directInput->Release();
return 1;
}
std::cout << "Found " << forceFeedbackDevices.size()
<< " force feedback devices." << std::endl;
for (IDirectInputDevice8* device : forceFeedbackDevices) {
std::cout << "Using device for force spike output." << std::endl;
DIEFFECT backgroundEffect;
void* backgroundParams = nullptr;
DWORD backgroundParamSize = 0;
GUID backgroundGuid = di_common::kSineEffectGuid;
if (!di_common::ResolveEffectGuid("sine", backgroundGuid)) {
std::cerr << "Failed to resolve sine effect GUID." << std::endl;
device->Release();
directInput->Release();
return 1;
}
if (!di_common::BuildEffectParameters("sine", backgroundEffect,
backgroundParams,
backgroundParamSize, 0,
backgroundStrength)) {
std::cerr << "Failed to build background sine effect." << std::endl;
device->Release();
directInput->Release();
return 1;
}
DIEFFECT spikeEffect;
void* spikeParams = nullptr;
DWORD spikeParamSize = 0;
GUID spikeGuid = di_common::kSineEffectGuid;
if (!di_common::ResolveEffectGuid("constant", spikeGuid)) {
std::cerr << "Failed to resolve constant-force effect GUID."
<< std::endl;
device->Release();
directInput->Release();
return 1;
}
if (!di_common::BuildEffectParameters("constant", spikeEffect, spikeParams,
spikeParamSize, 0, 100)) {
std::cerr << "Failed to build constant-force spike effect."
<< std::endl;
device->Release();
directInput->Release();
return 1;
}
IDirectInputEffect* backgroundEffectInterface = nullptr;
hr = device->CreateEffect(backgroundGuid, &backgroundEffect,
&backgroundEffectInterface, nullptr);
if (!di_common::CheckDInputResult(hr, "CreateEffect(background)")) {
device->Release();
directInput->Release();
return 1;
}
IDirectInputEffect* spikeEffectInterface = nullptr;
hr = device->CreateEffect(spikeGuid, &spikeEffect, &spikeEffectInterface,
nullptr);
if (!di_common::CheckDInputResult(hr, "CreateEffect(spike)")) {
backgroundEffectInterface->Release();
device->Release();
directInput->Release();
return 1;
}
if (!di_common::CheckDInputResult(backgroundEffectInterface->Start(1, 0),
"Start(background)")) {
spikeEffectInterface->Release();
backgroundEffectInterface->Release();
device->Release();
directInput->Release();
return 1;
}
while (!g_shouldStop) {
std::cout << "Starting spike" << std::endl;
hr = spikeEffectInterface->Start(1, 0);
if (!di_common::CheckDInputResult(hr, "Start(spike)")) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(spikeDurationMs));
hr = spikeEffectInterface->Stop();
if (!di_common::CheckDInputResult(hr, "Stop(spike)")) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(spikeDelayMs));
}
backgroundEffectInterface->Stop();
backgroundEffectInterface->Release();
spikeEffectInterface->Release();
device->Release();
break;
}
directInput->Release();
return 0;
} catch (const cxxopts::exceptions::exception& ex) {
std::cerr << ex.what() << std::endl;
std::cerr << options.help() << std::endl;
return 2;
}
}