-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.cc
291 lines (259 loc) · 9.06 KB
/
Device.cc
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Tilt-wizard
* Copyright (C) 2018 John D. Strunk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Docs: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee417929(v%3dvs.85)
#include "Device.h"
#include "TWError.h"
BOOL
devEnumCb(LPCDIDEVICEINSTANCE lpddi, LPVOID voidDev)
{
Device::DescriptionList *devices =
reinterpret_cast<Device::DescriptionList*>(voidDev);
const unsigned G_LEN = 100;
wchar_t guid[G_LEN];
if (0 == StringFromGUID2(lpddi->guidInstance, guid, G_LEN))
throw TWError("Unable to convert GUID to string");
std::wstring wgstring(guid);
Device::DeviceDescription desc;
desc.deviceName = lpddi->tszInstanceName;
desc.guidString.assign(wgstring.begin(), wgstring.end());
memcpy(&desc.guid, &lpddi->guidInstance, sizeof(desc.guid));
devices->push_back(desc);
return DIENUM_CONTINUE;
}
Device::DescriptionList
Device::enumerateDevices()
{
// Initialize module handle instance
HINSTANCE hInst = GetModuleHandle(0);
if (!hInst) throw TWError("Failed to get module handle");
// Initialize DirectInput
LPDIRECTINPUT8 di8Interface = 0;
HRESULT res = DirectInput8Create(hInst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (LPVOID*)&di8Interface,
0);
if (FAILED(res)) throw TWError("Failed to get interface to DirectInput", res);
DescriptionList devices;
res = di8Interface->EnumDevices(DI8DEVCLASS_GAMECTRL, devEnumCb,
&devices, DIEDFL_ATTACHEDONLY);
if (FAILED(res)) throw TWError("Error enumerating devices", res);
return devices;
}
Device::Device(std::string guidString, CalibrationMode mode)
{
IID guid;
std::wstring wGuid;
wGuid.assign(guidString.begin(), guidString.end());
HRESULT res = IIDFromString(wGuid.c_str(), &guid);
if (FAILED(res)) throw TWError("Error parsing device GUID", res);
_init(&guid, mode);
}
Device::Device(IID *guid, CalibrationMode mode)
{
_init(guid, mode);
}
void
Device::_init(IID *guid, CalibrationMode mode)
{
if (!guid) throw TWError("GUID should not be null");
HINSTANCE hInst = GetModuleHandle(0);
if (!hInst) throw TWError("Unable to get module handle");
LPDIRECTINPUT8 di8Int = 0;
HRESULT res = DirectInput8Create(hInst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (LPVOID*)&di8Int,
0);
if (FAILED(res)) throw TWError("Failed to get interface to DirectInput", res);
res = di8Int->CreateDevice(*guid, &_dev, 0);
if (FAILED(res)) throw TWError("Unable to get handle for device", res);
di8Int->Release();
// Configure the device
res = _dev->SetCooperativeLevel(0, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(res)) throw TWError("Failed setting device cooperation level", res);
res = _dev->SetDataFormat(&c_dfDIJoystick);
if (FAILED(res)) throw TWError("Failed setting data format", res);
// Set to retrieve RAW data from device
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
if (mode == RAW) {
dipdw.dwData = DIPROPCALIBRATIONMODE_RAW;
} else {
dipdw.dwData = DIPROPCALIBRATIONMODE_COOKED;
}
res = _dev->SetProperty(DIPROP_CALIBRATIONMODE, &dipdw.diph);
if (FAILED(res)) throw TWError("Failed setting calibration mode", res);
// Open the device so we can read
res = _dev->Acquire();
if (FAILED(res)) throw TWError("Failed acquiring device", res);
}
Device::~Device()
{
_dev->Unacquire();
_dev->Release(); // Needed?
}
std::string
Device::name() const
{
DIDEVICEINSTANCE di;
di.dwSize = sizeof(di);
HRESULT res = _dev->GetDeviceInfo(&di);
if (FAILED(res)) throw TWError("Failed getting device info", res);
std::string name(di.tszInstanceName);
return name;
}
void
Device::poll()
{
HRESULT res = _dev->Poll();
if (FAILED(res)) throw TWError("Failed polling device", res);
res = _dev->GetDeviceState(sizeof(_state), &_state);
if (FAILED(res)) throw TWError("Failed getting current device position", res);
}
void
Device::calibration(WORD axisOffset, LONG *min, LONG *center, LONG *max) const
{
DIPROPCAL dical;
dical.diph.dwSize = sizeof(dical);
dical.diph.dwHeaderSize = sizeof(dical.diph);
dical.diph.dwObj = axisOffset;
dical.diph.dwHow = DIPH_BYOFFSET;
HRESULT res = _dev->GetProperty(DIPROP_CALIBRATION, &dical.diph);
if (FAILED(res)) throw TWError("Unable to get calibration", res);
*min = dical.lMin;
*center = dical.lCenter;
*max = dical.lMax;
}
void
Device::calibration(WORD axisOffset, LONG min, LONG center, LONG max)
{
DIPROPCAL dical;
dical.diph.dwSize = sizeof(dical);
dical.diph.dwHeaderSize = sizeof(dical.diph);
dical.diph.dwObj = axisOffset;
dical.diph.dwHow = DIPH_BYOFFSET;
dical.lMin = min;
dical.lCenter = center;
dical.lMax = max;
HRESULT res = _dev->SetProperty(DIPROP_CALIBRATION, &dical.diph);
if (FAILED(res)) throw TWError("Unable to set calibration", res);
/*
LPDIRECTINPUTJOYCONFIG idijc = 0;
res = di8Dev->QueryInterface(IID_IDirectInputJoyConfig,(LPVOID*)&idijc);
if (FAILED(res)) fatalError(L"Unable to get JoyConfig", res);
res = idijc->Acquire();
if (FAILED(res)) fatalError(L"JoyConfig acquire", res);
res = idijc->SendNotify();
if (FAILED(res)) fatalError(L"JoyConfig SendNotify", res);
res = idijc->Release();
if (FAILED(res)) fatalError(L"JoyConfig Release", res);
*/
}
LONG
Device::position(WORD axisOffset) const
{
switch (axisOffset) {
case DIJOFS_X:
return _state.lX;
case DIJOFS_Y:
return _state.lY;
default:
throw TWError("Unsupported axis");
}
// not reached
return 0;
}
double
Device::deadzone(WORD axisOffset) const
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = axisOffset;
dipdw.diph.dwHow = DIPH_BYOFFSET;
HRESULT res = _dev->GetProperty(DIPROP_DEADZONE, &dipdw.diph);
if (FAILED(res)) throw TWError("Unable to get deadzone", res);
return dipdw.dwData/100.0;
}
void
Device::deadzone(WORD axisOffset, double pct)
{
if (0.0 > pct || pct > 100.0)
throw TWError("Deadzone must be between 0 and 100");
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = axisOffset;
dipdw.diph.dwHow = DIPH_BYOFFSET;
dipdw.dwData = (DWORD)(pct*100.0);
HRESULT res = _dev->SetProperty(DIPROP_DEADZONE, &dipdw.diph);
if (FAILED(res)) throw TWError("Unable to set deadzone", res);
}
void
Device::range(WORD axisOffset, LONG min, LONG max)
{
if (min > max) throw TWError("min must be <= max");
DIPROPRANGE r;
r.diph.dwSize = sizeof(r);
r.diph.dwHeaderSize = sizeof(r.diph);
r.diph.dwObj = axisOffset;
r.diph.dwHow = DIPH_BYOFFSET;
r.lMin = min;
r.lMax = max;
HRESULT res = _dev->SetProperty(DIPROP_RANGE, &r.diph);
if (FAILED(res)) throw TWError("Unable to set axis range", res);
}
void
Device::range(WORD axisOffset, LONG *min, LONG *max) const
{
DIPROPRANGE r;
r.diph.dwSize = sizeof(r);
r.diph.dwHeaderSize = sizeof(r.diph);
r.diph.dwObj = axisOffset;
r.diph.dwHow = DIPH_BYOFFSET;
HRESULT res = _dev->GetProperty(DIPROP_RANGE, &r.diph);
if (FAILED(res)) throw TWError("Unable to get axis range", res);
*min = r.lMin;
*max = r.lMax;
}
double
Device::saturation(WORD axisOffset) const
{
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = axisOffset;
dipdw.diph.dwHow = DIPH_BYOFFSET;
HRESULT res = _dev->GetProperty(DIPROP_SATURATION, &dipdw.diph);
if (FAILED(res)) throw TWError("Unable to get axis saturation", res);
return dipdw.dwData/100.0;
}
void
Device::saturation(WORD axisOffset, double pct)
{
if (0.0 > pct || pct > 100.0)
throw TWError("Saturation must be between 0 and 100");
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwObj = axisOffset;
dipdw.diph.dwHow = DIPH_BYOFFSET;
dipdw.dwData = (DWORD)(pct*100.0);
HRESULT res = _dev->SetProperty(DIPROP_SATURATION, &dipdw.diph);
if (FAILED(res)) throw TWError("Unable to set axis saturation", res);
}