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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#ifdef _WIN32
#include "win32-joystick.hpp"
#include "compat/macros.hpp"
#include <cstddef>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <QWidget>
#include <QDebug>
#include <objbase.h>
namespace win32_joy_impl {
QMutex win32_joy_ctx::enum_state::mtx;
win32_joy_ctx::enum_state win32_joy_ctx::enumerator;
DIDEVICEOBJECTDATA win32_joy_ctx::joy::keystate_buffers[num_buffers] = {};
void win32_joy_ctx::poll(fn const& f)
{
//refresh(false);
QMutexLocker l(&enum_state::mtx);
auto& joys = enumerator.get_joys();
for (auto& j : joys)
j.second->poll(f);
}
bool win32_joy_ctx::poll_axis(const QString &guid, int* axes)
{
QMutexLocker l(&enum_state::mtx);
for (int k = 0; k < 10; k++)
{
if (k > 0)
enumerator.refresh();
const joys_t& joys = enumerator.get_joys();
auto iter = joys.find(guid);
if (iter == joys.end())
return false;
auto& j = iter->second;
auto& joy_handle = j->joy_handle;
DIJOYSTATE2 js = {};
if (!di_t::poll_device(joy_handle))
continue;
if (FAILED(joy_handle->GetDeviceState(sizeof(js), &js)))
{
//qDebug() << "joy get state failed" << guid;
continue;
}
const int values[] =
{
js.lX,
js.lY,
js.lZ,
js.lRx,
js.lRy,
js.lRz,
js.rglSlider[0],
js.rglSlider[1]
};
for (unsigned i = 0; i < std::size(values); i++)
axes[i] = values[i];
return true;
}
return false;
}
std::vector<win32_joy_ctx::joy_info> win32_joy_ctx::get_joy_info()
{
std::vector<joy_info> ret;
QMutexLocker l(&enum_state::mtx);
auto& joys = enumerator.get_joys();
ret.reserve(joys.size());
for (auto& j : joys)
ret.push_back(joy_info { j.second->name, j.first });
std::sort(ret.begin(), ret.end(), [&](const joy_info& fst, const joy_info& snd) { return fst.name < snd.name; });
return ret;
}
win32_joy_ctx::win32_joy_ctx()
{
refresh();
}
void win32_joy_ctx::refresh()
{
QMutexLocker l(&enum_state::mtx);
enumerator.refresh();
}
QString win32_joy_ctx::guid_to_string(const GUID& guid)
{
char buf[40] = {};
wchar_t szGuidW[40] = {};
StringFromGUID2(guid, szGuidW, sizeof(buf));
WideCharToMultiByte(0, 0, szGuidW, -1, buf, sizeof(buf), nullptr, nullptr);
buf[sizeof(buf)-1] = 0;
return QString(buf);
}
using fn = win32_joy_ctx::fn;
void win32_joy_ctx::joy::release()
{
if (joy_handle)
{
(void) joy_handle->Unacquire();
joy_handle->Release();
joy_handle = nullptr;
}
}
bool win32_joy_ctx::joy::poll(fn const& f)
{
HRESULT hr;
if (!di_t::poll_device(joy_handle))
{
eval_once(qDebug() << "joy poll failed" << guid << (void*)hr);
//(void)joy_handle->Unacquire();
//Sleep(0);
return false;
}
DWORD sz = num_buffers;
if (FAILED(hr = joy_handle->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), keystate_buffers, &sz, 0)))
{
eval_once(qDebug() << "joy GetDeviceData failed" << guid << (void*)hr);
return false;
}
for (unsigned k = 0; k < sz; k++)
{
const DIDEVICEOBJECTDATA& event = keystate_buffers[k];
bool is_pov = false;
int i = -1;
#define POV_HAT_OFFSET(k) \
(offsetof(DIJOYSTATE2, rgdwPOV) + (k) * sizeof(DWORD))
#define BUTTON_OFFSET(k) \
(offsetof(DIJOYSTATE2, rgbButtons) + (k) * sizeof(BYTE))
switch (event.dwOfs)
{
case POV_HAT_OFFSET(0): i = 0; is_pov = true; break;
case POV_HAT_OFFSET(1): i = 1; is_pov = true; break;
case POV_HAT_OFFSET(2): i = 2; is_pov = true; break;
case POV_HAT_OFFSET(3): i = 3; is_pov = true; break;
default:
if (event.dwOfs >= BUTTON_OFFSET(0) && event.dwOfs <= BUTTON_OFFSET(max_buttons - 1))
{
i = int(event.dwOfs - BUTTON_OFFSET(0));
i /= sizeof(DIJOYSTATE2().rgbButtons[0]);
i %= max_buttons; // defensive programming
}
break;
}
if (is_pov)
{
unsigned pos = event.dwData / value_per_pov_hat_direction;
i = max_buttons + i * pov_hat_directions;
for (unsigned j = 0; j < pov_hat_directions; j++)
{
const unsigned idx = i + j;
const bool new_value = pos == j;
if (last_state[idx] != new_value)
{
#ifdef WIN32_JOY_DEBUG
qDebug() << "DBG: pov" << idx << (pos == j);
#endif
last_state[idx] = new_value;
f(guid, idx, new_value);
}
}
}
else if ((unsigned)i < max_buttons)
{
const bool new_value = !!(event.dwData & 0x80);
if (last_state[i] != new_value)
{
#ifdef WIN32_JOY_DEBUG
qDebug() << "DBG: btn" << i << new_value;
#endif
last_state[i] = new_value;
f(guid, i, new_value);
}
}
}
return true;
}
win32_joy_ctx::enum_state::enum_state() = default;
win32_joy_ctx::enum_state::~enum_state()
{
QMutexLocker l(&mtx);
joys = std::unordered_map<QString, std::shared_ptr<joy>>();
}
void win32_joy_ctx::enum_state::refresh()
{
all.clear();
#ifdef __SANITIZE_ADDRESS__
return;
#endif
if (!di)
{
qDebug() << "dinput: can't create dinput";
return;
}
HRESULT hr;
if(FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
this,
DIEDFL_ATTACHEDONLY)))
{
eval_once(qDebug() << "dinput: failed enum joysticks" << (void*)hr);
return;
}
for (auto it = joys.begin(); it != joys.end(); )
{
if (std::find_if(all.cbegin(), all.cend(), [&](const QString& guid2) { return it->second->guid == guid2; }) == all.end())
it = joys.erase(it);
else
++it;
}
}
const win32_joy_ctx::joys_t& win32_joy_ctx::enum_state::get_joys() const { return joys; }
BOOL CALLBACK win32_joy_ctx::enum_state::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, void *pContext)
{
enum_state& state = *reinterpret_cast<enum_state*>(pContext);
const QString guid = guid_to_string(pdidInstance->guidInstance);
const QString name = QString(pdidInstance->tszInstanceName);
const bool exists = state.joys.find(guid) != state.joys.end();
state.all.push_back(guid);
if (exists)
goto end;
{
HRESULT hr;
LPDIRECTINPUTDEVICE8 h;
if (FAILED(hr = state.di->CreateDevice(pdidInstance->guidInstance, &h, nullptr)))
{
qDebug() << "dinput: failed joystick CreateDevice" << guid << (void*)hr;
goto end;
}
if (FAILED(hr = h->SetDataFormat(&c_dfDIJoystick2)))
{
qDebug() << "dinput: failed joystick SetDataFormat" << (void*)hr;
h->Release();
goto end;
}
// not a library-load-time member - need main() to run for some time first
static const QWidget fake_window;
if (FAILED(h->SetCooperativeLevel(reinterpret_cast<HWND>(fake_window.winId()), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
{
qDebug() << "coop";
h->Release();
goto end;
}
{
DIPROPDWORD dipdw = {};
dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
dipdw.diph.dwSize = sizeof(dipdw);
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.diph.dwObj = 0;
dipdw.dwData = num_buffers;
if (h->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph) != DI_OK)
{
qDebug() << "dinput: joystick DIPROP_BUFFERSIZE";
h->Release();
goto end;
}
}
if (FAILED(hr = h->EnumObjects(EnumObjectsCallback, h, DIDFT_ALL)))
{
qDebug() << "dinput: joystick EnumObjects";
h->Release();
goto end;
}
state.joys[guid] = std::make_shared<joy>(h, guid, name);
}
end:
return DIENUM_CONTINUE;
}
BOOL CALLBACK win32_joy_ctx::enum_state::EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *pdidoi, void *ctx)
{
if (pdidoi->dwType & DIDFT_AXIS)
{
DIPROPRANGE diprg = {};
diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_BYID;
diprg.diph.dwObj = pdidoi->dwType;
diprg.lMax = joy_axis_size;
diprg.lMin = -joy_axis_size;
HRESULT hr;
if (FAILED(hr = reinterpret_cast<LPDIRECTINPUTDEVICE8>(ctx)->SetProperty(DIPROP_RANGE, &diprg.diph)))
{
qDebug() << "dinput: failed joystick DIPROP_RANGE" << (void*)hr;
return DIENUM_STOP;
}
}
return DIENUM_CONTINUE;
}
win32_joy_ctx::joy::joy(LPDIRECTINPUTDEVICE8 handle, const QString& guid, const QString &name)
: joy_handle(handle), guid(guid), name(name)
{
//qDebug() << "make joy" << guid << name << joy_handle;
}
win32_joy_ctx::joy::~joy()
{
//qDebug() << "nix joy" << guid;
release();
}
} // ns win32_joy_impl
#endif
|