-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffb_example.cpp
More file actions
189 lines (175 loc) · 7.21 KB
/
Copy pathffb_example.cpp
File metadata and controls
189 lines (175 loc) · 7.21 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
#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("ffb_example",
"Play a DirectInput force feedback effect");
options.add_options()("e,effect",
"Effect to play: sine, square, sawtooth-down, "
"sawtooth-up, triangle, constant, ramp, spring, "
"damper, friction, inertia",
cxxopts::value<std::string>()->default_value("sine"))(
"n,num_updates",
"Number of updates to send before stopping. Use 0 to send a single "
"initial update and then wait for Ctrl+C.",
cxxopts::value<std::uint64_t>()->default_value("1000"))(
"m,mutate_forces",
"If true, mutate the force parameters on each update. If false, keep "
"the initial force values and only resend them.",
cxxopts::value<bool>()->default_value("false"))(
"s,strength_percentage",
"Strength percentage to apply to the effect, from 0 to 100. Defaults to "
"25.",
cxxopts::value<int>()->default_value("25"))("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);
std::string effectName = result["effect"].as<std::string>();
std::uint64_t numUpdates = result["num_updates"].as<std::uint64_t>();
bool mutateForces = result["mutate_forces"].as<bool>();
int strengthPercentage = result["strength_percentage"].as<int>();
GUID effectGuid = di_common::kSineEffectGuid;
if (!di_common::ResolveEffectGuid(effectName, effectGuid)) {
std::cerr << "Unknown effect '" << effectName
<< "'. Expected one of: sine, square, sawtooth-down, "
"sawtooth-up, triangle, constant, ramp, spring, damper, "
"friction, inertia"
<< std::endl;
return 2;
}
std::cout << "Using effect: " << effectName << std::endl;
if (numUpdates == 0) {
std::cout << "Sending one initial update and waiting for Ctrl+C."
<< std::endl;
} else {
std::cout << "Sending " << numUpdates << " updates." << std::endl;
}
if (mutateForces) {
std::cout << "Force values will mutate on each update." << std::endl;
} else {
std::cout << "Force values will remain fixed across updates."
<< std::endl;
}
const int clampedStrength =
di_common::ClampStrengthPercentage(strengthPercentage);
std::cout << "Strength percentage: " << clampedStrength << "%" << std::endl;
std::string normalizedEffectName =
di_common::NormalizeEffectName(effectName);
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;
} else 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) {
DIEFFECT effect;
void* typeSpecificParams = nullptr;
DWORD typeSpecificParamSize = 0;
if (!di_common::BuildEffectParameters(
effectName, effect, typeSpecificParams, typeSpecificParamSize, 0,
strengthPercentage)) {
std::cerr << "Unsupported effect type: " << effectName << std::endl;
return 2;
}
IDirectInputEffect* effectInterface = nullptr;
hr = device->CreateEffect(effectGuid, &effect, &effectInterface, nullptr);
if (!di_common::CheckDInputResult(hr, "CreateEffect")) {
return 1;
} else {
hr = effectInterface->Start(1, 0);
if (!di_common::CheckDInputResult(hr, "Start")) {
return 1;
}
const auto start_time = std::chrono::high_resolution_clock::now();
std::uint64_t updateCount = 0;
if (numUpdates == 0) {
// Special case; run until interrupted.
if (!di_common::BuildEffectParameters(
effectName, effect, typeSpecificParams, typeSpecificParamSize,
0, strengthPercentage)) {
std::cerr << "Unsupported effect type: " << effectName << std::endl;
return 2;
}
hr = effectInterface->SetParameters(&effect, DIEP_TYPESPECIFICPARAMS);
if (!di_common::CheckDInputResult(hr, "SetParameters")) {
return 1;
}
++updateCount;
while (!g_shouldStop) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
} else {
while (!g_shouldStop && updateCount < numUpdates) {
const std::uint64_t iterationIndex =
mutateForces ? updateCount : 0;
if (!di_common::BuildEffectParameters(
effectName, effect, typeSpecificParams,
typeSpecificParamSize, iterationIndex,
strengthPercentage)) {
std::cerr << "Unsupported effect type: " << effectName
<< std::endl;
return 2;
}
hr = effectInterface->SetParameters(&effect,
DIEP_TYPESPECIFICPARAMS);
if (!di_common::CheckDInputResult(hr, "SetParameters")) {
return 1;
}
++updateCount;
}
}
const auto kEndTime = std::chrono::high_resolution_clock::now();
const auto kDuration =
std::chrono::duration_cast<std::chrono::milliseconds>(kEndTime -
start_time);
const float kAverage =
updateCount > 0
? static_cast<float>(kDuration.count()) / updateCount
: 0.0f;
std::cout << "Total run time for " << std::dec << updateCount
<< " updates: " << kDuration.count()
<< " ms, average: " << kAverage << " ms" << std::endl;
if (g_shouldStop) {
std::cout << "Interrupted by Ctrl+C; stopping cleanly." << std::endl;
}
effectInterface->Stop();
effectInterface->Release();
}
device->Release();
}
directInput->Release();
return 0;
} catch (const cxxopts::exceptions::exception& ex) {
std::cerr << ex.what() << std::endl;
std::cerr << options.help() << std::endl;
return 2;
}
}