Skip to content

Latest commit

 

History

History
1823 lines (1590 loc) · 66.1 KB

SDL_rawinputjoystick.c

File metadata and controls

1823 lines (1590 loc) · 66.1 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
/*
Simple DirectMedia Layer
Copyright (C) 2019 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
RAWINPUT Joystick API for better handling XInput-capable devices on Windows.
XInput is limited to 4 devices.
Windows.Gaming.Input does not get inputs from XBox One controllers when not in the foreground.
DirectInput does not get inputs from XBox One controllers when not in the foreground, nor rumble or accurate triggers.
RawInput does not get rumble or accurate triggers.
So, combine them as best we can!
*/
#include "../../SDL_internal.h"
#if SDL_JOYSTICK_RAWINPUT
#include "SDL_assert.h"
#include "SDL_endian.h"
Nov 24, 2020
Nov 24, 2020
38
#include "SDL_events.h"
39
#include "SDL_hints.h"
Nov 24, 2020
Nov 24, 2020
40
41
#include "SDL_timer.h"
#include "../usb_ids.h"
42
43
#include "../SDL_sysjoystick.h"
#include "../../core/windows/SDL_windows.h"
Nov 24, 2020
Nov 24, 2020
44
#include "../../core/windows/SDL_hid.h"
45
46
#include "../hidapi/SDL_hidapijoystick_c.h"
Nov 24, 2020
Nov 24, 2020
47
#define SDL_JOYSTICK_RAWINPUT_XINPUT
Nov 26, 2020
Nov 26, 2020
48
49
#ifdef SDL_WINDOWS10_SDK
#define SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
Nov 24, 2020
Nov 24, 2020
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
#include "../../core/windows/SDL_xinput.h"
#endif
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
#include "../../core/windows/SDL_windows.h"
typedef struct WindowsGamingInputGamepadState WindowsGamingInputGamepadState;
#define GamepadButtons_GUIDE 0x40000000
#define COBJMACROS
#include "windows.gaming.input.h"
#endif
#if defined(SDL_JOYSTICK_RAWINPUT_XINPUT) || defined(SDL_JOYSTICK_RAWINPUT_GAMING_INPUT)
#define SDL_JOYSTICK_RAWINPUT_MATCHING
#endif
/* #define DEBUG_RAWINPUT */
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
#ifndef RIDEV_EXINPUTSINK
#define RIDEV_EXINPUTSINK 0x00001000
#define RIDEV_DEVNOTIFY 0x00002000
#endif
#ifndef WM_INPUT_DEVICE_CHANGE
#define WM_INPUT_DEVICE_CHANGE 0x00FE
#endif
#ifndef WM_INPUT
#define WM_INPUT 0x00FF
#endif
#ifndef GIDC_ARRIVAL
#define GIDC_ARRIVAL 1
#define GIDC_REMOVAL 2
#endif
/* external variables referenced. */
extern HWND SDL_HelperWindow;
static SDL_bool SDL_RAWINPUT_inited = SDL_FALSE;
static int SDL_RAWINPUT_numjoysticks = 0;
static SDL_bool SDL_RAWINPUT_need_pump = SDL_TRUE;
static void RAWINPUT_JoystickDetect(void);
static void RAWINPUT_PumpMessages(void);
Nov 24, 2020
Nov 24, 2020
96
static void RAWINPUT_JoystickClose(SDL_Joystick *joystick);
97
98
99
typedef struct _SDL_RAWINPUT_Device
{
Apr 13, 2020
Apr 13, 2020
100
SDL_atomic_t refcount;
101
102
103
104
105
char *name;
Uint16 vendor_id;
Uint16 product_id;
Uint16 version;
SDL_JoystickGUID guid;
Nov 24, 2020
Nov 24, 2020
106
107
SDL_bool is_xinput;
PHIDP_PREPARSED_DATA preparsed_data;
108
109
110
111
112
113
114
115
116
117
HANDLE hDevice;
SDL_Joystick *joystick;
SDL_JoystickID joystick_id;
struct _SDL_RAWINPUT_Device *next;
} SDL_RAWINPUT_Device;
struct joystick_hwdata
{
Nov 24, 2020
Nov 24, 2020
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
SDL_bool is_xinput;
PHIDP_PREPARSED_DATA preparsed_data;
ULONG max_data_length;
HIDP_DATA *data;
USHORT *button_indices;
USHORT *axis_indices;
USHORT *hat_indices;
SDL_bool guide_hack;
SDL_bool trigger_hack;
USHORT trigger_hack_index;
#ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
Uint32 match_state; /* Low 16 bits for button states, high 16 for 4 4bit axes */
Uint32 last_state_packet;
#endif
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
SDL_bool xinput_enabled;
SDL_bool xinput_correlated;
Uint8 xinput_correlation_id;
Uint8 xinput_correlation_count;
Uint8 xinput_uncorrelate_count;
Uint8 xinput_slot;
#endif
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
SDL_bool wgi_correlated;
Uint8 wgi_correlation_id;
Uint8 wgi_correlation_count;
Uint8 wgi_uncorrelate_count;
WindowsGamingInputGamepadState *wgi_slot;
#endif
151
152
SDL_RAWINPUT_Device *device;
};
Nov 24, 2020
Nov 24, 2020
153
typedef struct joystick_hwdata RAWINPUT_DeviceContext;
154
155
156
157
SDL_RAWINPUT_Device *SDL_RAWINPUT_devices;
static const Uint16 subscribed_devices[] = {
Nov 24, 2020
Nov 24, 2020
158
USB_USAGE_GENERIC_GAMEPAD,
159
/* Don't need Joystick for any devices we're handling here (XInput-capable)
Nov 24, 2020
Nov 24, 2020
160
161
USB_USAGE_GENERIC_JOYSTICK,
USB_USAGE_GENERIC_MULTIAXISCONTROLLER,
Nov 24, 2020
Nov 24, 2020
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
static struct {
Uint32 last_state_packet;
SDL_Joystick *joystick;
SDL_Joystick *last_joystick;
} guide_button_candidate;
typedef struct WindowsMatchState {
SHORT match_axes[4];
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
WORD xinput_buttons;
#endif
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
Uint32 wgi_buttons;
#endif
SDL_bool any_data;
} WindowsMatchState;
Nov 24, 2020
Nov 24, 2020
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
static void RAWINPUT_FillMatchState(WindowsMatchState *state, Uint32 match_state)
{
int ii;
state->any_data = SDL_FALSE;
/* SHORT state->match_axes[4] = {
(match_state & 0x000F0000) >> 4,
(match_state & 0x00F00000) >> 8,
(match_state & 0x0F000000) >> 12,
(match_state & 0xF0000000) >> 16,
}; */
for (ii = 0; ii < 4; ii++) {
state->match_axes[ii] = (match_state & (0x000F0000 << (ii * 4))) >> (4 + ii * 4);
if ((Uint32)(state->match_axes[ii] + 0x1000) > 0x2000) { /* match_state bit is not 0xF, 0x1, or 0x2 */
state->any_data = SDL_TRUE;
}
Nov 24, 2020
Nov 24, 2020
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
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
/* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
#define XInputAxesMatch(gamepad) (\
(Uint32)(gamepad.sThumbLX - state->match_axes[0] + 0x1000) <= 0x2fff && \
(Uint32)(~gamepad.sThumbLY - state->match_axes[1] + 0x1000) <= 0x2fff && \
(Uint32)(gamepad.sThumbRX - state->match_axes[2] + 0x1000) <= 0x2fff && \
(Uint32)(~gamepad.sThumbRY - state->match_axes[3] + 0x1000) <= 0x2fff)
/* Explicit
#define XInputAxesMatch(gamepad) (\
SDL_abs((Sint8)((gamepad.sThumbLX & 0xF000) >> 8) - ((match_state & 0x000F0000) >> 12)) <= 0x10 && \
SDL_abs((Sint8)((~gamepad.sThumbLY & 0xF000) >> 8) - ((match_state & 0x00F00000) >> 16)) <= 0x10 && \
SDL_abs((Sint8)((gamepad.sThumbRX & 0xF000) >> 8) - ((match_state & 0x0F000000) >> 20)) <= 0x10 && \
SDL_abs((Sint8)((~gamepad.sThumbRY & 0xF000) >> 8) - ((match_state & 0xF0000000) >> 24)) <= 0x10) */
state->xinput_buttons =
/* Bitwise map .RLDUWVQTS.KYXBA -> YXBA..WVQTKSRLDU */
match_state << 12 | (match_state & 0x0780) >> 1 | (match_state & 0x0010) << 1 | (match_state & 0x0040) >> 2 | (match_state & 0x7800) >> 11;
/* Explicit
((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? XINPUT_GAMEPAD_A : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? XINPUT_GAMEPAD_B : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? XINPUT_GAMEPAD_X : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? XINPUT_GAMEPAD_Y : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? XINPUT_GAMEPAD_BACK : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? XINPUT_GAMEPAD_START : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? XINPUT_GAMEPAD_RIGHT_THUMB: 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? XINPUT_GAMEPAD_DPAD_UP : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? XINPUT_GAMEPAD_DPAD_DOWN : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? XINPUT_GAMEPAD_DPAD_LEFT : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? XINPUT_GAMEPAD_DPAD_RIGHT : 0);
*/
if (state->xinput_buttons)
state->any_data = SDL_TRUE;
#endif
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
/* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
#define WindowsGamingInputAxesMatch(gamepad) (\
(Uint16)(((Sint16)(gamepad.LeftThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[0] + 0x1000) <= 0x2fff && \
(Uint16)((~(Sint16)(gamepad.LeftThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[1] + 0x1000) <= 0x2fff && \
(Uint16)(((Sint16)(gamepad.RightThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[2] + 0x1000) <= 0x2fff && \
(Uint16)((~(Sint16)(gamepad.RightThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[3] + 0x1000) <= 0x2fff)
state->wgi_buttons =
/* Bitwise map .RLD UWVQ TS.K YXBA -> ..QT WVRL DUYX BAKS */
/* RStick/LStick (QT) RShould/LShould (WV) DPad R/L/D/U YXBA bac(K) (S)tart */
(match_state & 0x0180) << 5 | (match_state & 0x0600) << 1 | (match_state & 0x7800) >> 5 | (match_state & 0x000F) << 2 | (match_state & 0x0010) >> 3 | (match_state & 0x0040) >> 6;
/* Explicit
((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? GamepadButtons_A : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? GamepadButtons_B : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? GamepadButtons_X : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? GamepadButtons_Y : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? GamepadButtons_View : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? GamepadButtons_Menu : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? GamepadButtons_LeftThumbstick : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? GamepadButtons_RightThumbstick: 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? GamepadButtons_LeftShoulder: 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? GamepadButtons_RightShoulder: 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? GamepadButtons_DPadUp : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? GamepadButtons_DPadDown : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? GamepadButtons_DPadLeft : 0) |
((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? GamepadButtons_DPadRight : 0); */
if (state->wgi_buttons)
state->any_data = SDL_TRUE;
#endif
}
#endif /* SDL_JOYSTICK_RAWINPUT_MATCHING */
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
static struct {
XINPUT_STATE_EX state;
SDL_bool connected; /* Currently has an active XInput device */
SDL_bool used; /* Is currently mapped to an SDL device */
Uint8 correlation_id;
} xinput_state[XUSER_MAX_COUNT];
static SDL_bool xinput_device_change = SDL_TRUE;
static SDL_bool xinput_state_dirty = SDL_TRUE;
static void
RAWINPUT_UpdateXInput()
{
DWORD user_index;
if (xinput_device_change) {
for (user_index = 0; user_index < XUSER_MAX_COUNT; user_index++) {
XINPUT_CAPABILITIES capabilities;
xinput_state[user_index].connected = (XINPUTGETCAPABILITIES(user_index, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS) ? SDL_TRUE : SDL_FALSE;
}
xinput_device_change = SDL_FALSE;
xinput_state_dirty = SDL_TRUE;
}
if (xinput_state_dirty) {
xinput_state_dirty = SDL_FALSE;
for (user_index = 0; user_index < SDL_arraysize(xinput_state); ++user_index) {
if (xinput_state[user_index].connected) {
if (XINPUTGETSTATE(user_index, &xinput_state[user_index].state) != ERROR_SUCCESS) {
xinput_state[user_index].connected = SDL_FALSE;
}
}
}
Nov 24, 2020
Nov 24, 2020
309
}
Nov 24, 2020
Nov 24, 2020
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
static void
RAWINPUT_MarkXInputSlotUsed(Uint8 xinput_slot)
{
if (xinput_slot != XUSER_INDEX_ANY) {
xinput_state[xinput_slot].used = SDL_TRUE;
}
}
static void
RAWINPUT_MarkXInputSlotFree(Uint8 xinput_slot)
{
if (xinput_slot != XUSER_INDEX_ANY) {
xinput_state[xinput_slot].used = SDL_FALSE;
}
}
static SDL_bool
RAWINPUT_MissingXInputSlot()
{
int ii;
for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
if (xinput_state[ii].connected && !xinput_state[ii].used) {
return SDL_TRUE;
}
Nov 24, 2020
Nov 24, 2020
335
336
return SDL_FALSE;
}
Nov 24, 2020
Nov 24, 2020
338
339
340
341
342
343
344
345
346
347
348
static SDL_bool
RAWINPUT_XInputSlotMatches(const WindowsMatchState *state, Uint8 slot_idx)
{
if (xinput_state[slot_idx].connected) {
WORD xinput_buttons = xinput_state[slot_idx].state.Gamepad.wButtons;
if ((xinput_buttons & ~XINPUT_GAMEPAD_GUIDE) == state->xinput_buttons && XInputAxesMatch(xinput_state[slot_idx].state.Gamepad)) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
Nov 24, 2020
Nov 24, 2020
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
static SDL_bool
RAWINPUT_GuessXInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, Uint8 *slot_idx)
{
int user_index;
int match_count;
*slot_idx = 0;
match_count = 0;
for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
if (!xinput_state[user_index].used && RAWINPUT_XInputSlotMatches(state, user_index)) {
++match_count;
*slot_idx = (Uint8)user_index;
/* Incrementing correlation_id for any match, as negative evidence for others being correlated */
*correlation_id = ++xinput_state[user_index].correlation_id;
}
}
/* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
data. */
if (match_count == 1 && state->any_data) {
return SDL_TRUE;
}
return SDL_FALSE;
}
#endif /* SDL_JOYSTICK_RAWINPUT_XINPUT */
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
typedef struct WindowsGamingInputGamepadState {
__x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
RAWINPUT_DeviceContext *correlated_context;
SDL_bool used; /* Is currently mapped to an SDL device */
SDL_bool connected; /* Just used during update to track disconnected */
Uint8 correlation_id;
struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
} WindowsGamingInputGamepadState;
static struct {
WindowsGamingInputGamepadState **per_gamepad;
int per_gamepad_count;
SDL_bool initialized;
SDL_bool dirty;
SDL_bool need_device_list_update;
int ref_count;
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
} wgi_state;
static void
RAWINPUT_MarkWindowsGamingInputSlotUsed(WindowsGamingInputGamepadState *wgi_slot, RAWINPUT_DeviceContext *ctx)
{
wgi_slot->used = SDL_TRUE;
wgi_slot->correlated_context = ctx;
}
static void
RAWINPUT_MarkWindowsGamingInputSlotFree(WindowsGamingInputGamepadState *wgi_slot)
{
wgi_slot->used = SDL_FALSE;
wgi_slot->correlated_context = NULL;
}
static SDL_bool
RAWINPUT_MissingWindowsGamingInputSlot()
{
int ii;
for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
if (!wgi_state.per_gamepad[ii]->used) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static void
RAWINPUT_UpdateWindowsGamingInput()
{
int ii;
if (!wgi_state.gamepad_statics)
return;
if (!wgi_state.dirty)
return;
wgi_state.dirty = SDL_FALSE;
if (wgi_state.need_device_list_update) {
wgi_state.need_device_list_update = SDL_FALSE;
for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
wgi_state.per_gamepad[ii]->connected = SDL_FALSE;
}
HRESULT hr;
__FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(wgi_state.gamepad_statics, &gamepads);
if (SUCCEEDED(hr)) {
unsigned int num_gamepads;
hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
if (SUCCEEDED(hr)) {
unsigned int i;
for (i = 0; i < num_gamepads; ++i) {
__x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
if (SUCCEEDED(hr)) {
SDL_bool found = SDL_FALSE;
int jj;
for (jj = 0; jj < wgi_state.per_gamepad_count ; jj++) {
if (wgi_state.per_gamepad[jj]->gamepad == gamepad) {
found = SDL_TRUE;
wgi_state.per_gamepad[jj]->connected = SDL_TRUE;
break;
}
}
if (!found) {
/* New device, add it */
wgi_state.per_gamepad_count++;
wgi_state.per_gamepad = SDL_realloc(wgi_state.per_gamepad, sizeof(wgi_state.per_gamepad[0]) * wgi_state.per_gamepad_count);
if (!wgi_state.per_gamepad) {
SDL_OutOfMemory();
return;
}
WindowsGamingInputGamepadState *gamepad_state = SDL_calloc(1, sizeof(*gamepad_state));
if (!gamepad_state) {
SDL_OutOfMemory();
return;
}
wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1] = gamepad_state;
gamepad_state->gamepad = gamepad;
gamepad_state->connected = SDL_TRUE;
} else {
/* Already tracked */
__x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
}
}
}
for (ii = wgi_state.per_gamepad_count - 1; ii >= 0; ii--) {
WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
if (!gamepad_state->connected) {
/* Device missing, must be disconnected */
if (gamepad_state->correlated_context) {
gamepad_state->correlated_context->wgi_correlated = SDL_FALSE;
gamepad_state->correlated_context->wgi_slot = NULL;
}
__x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad_state->gamepad);
SDL_free(gamepad_state);
wgi_state.per_gamepad[ii] = wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1];
--wgi_state.per_gamepad_count;
}
}
Nov 24, 2020
Nov 24, 2020
505
506
507
508
509
510
511
512
__FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
}
} /* need_device_list_update */
for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
HRESULT hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(wgi_state.per_gamepad[ii]->gamepad, &wgi_state.per_gamepad[ii]->state);
if (!SUCCEEDED(hr)) {
wgi_state.per_gamepad[ii]->connected = SDL_FALSE; /* Not used by anything, currently */
Nov 24, 2020
Nov 24, 2020
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
}
static void
RAWINPUT_InitWindowsGamingInput(RAWINPUT_DeviceContext *ctx)
{
wgi_state.need_device_list_update = SDL_TRUE;
wgi_state.ref_count++;
if (!wgi_state.initialized) {
/* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
if (FAILED(WIN_CoInitialize())) {
return;
}
wgi_state.initialized = SDL_TRUE;
wgi_state.dirty = SDL_TRUE;
static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
HRESULT hr;
HMODULE hModule = LoadLibraryA("combase.dll");
if (hModule != NULL) {
typedef HRESULT (WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
typedef HRESULT (WINAPI *WindowsDeleteString_t)(HSTRING string);
typedef HRESULT (WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
WindowsDeleteString_t WindowsDeleteStringFunc = (WindowsDeleteString_t)GetProcAddress(hModule, "WindowsDeleteString");
RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
if (WindowsCreateStringFunc && WindowsDeleteStringFunc && RoGetActivationFactoryFunc) {
LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
HSTRING hNamespaceString;
hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
if (SUCCEEDED(hr)) {
RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &wgi_state.gamepad_statics);
WindowsDeleteStringFunc(hNamespaceString);
}
}
FreeLibrary(hModule);
}
Nov 24, 2020
Nov 24, 2020
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
static SDL_bool
RAWINPUT_WindowsGamingInputSlotMatches(const WindowsMatchState *state, WindowsGamingInputGamepadState *slot)
{
Uint32 wgi_buttons = slot->state.Buttons;
if ((wgi_buttons & 0x3FFF) == state->wgi_buttons && WindowsGamingInputAxesMatch(slot->state)) {
return SDL_TRUE;
}
return SDL_FALSE;
}
static SDL_bool
RAWINPUT_GuessWindowsGamingInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, WindowsGamingInputGamepadState **slot)
{
int match_count;
match_count = 0;
for (int user_index = 0; user_index < wgi_state.per_gamepad_count; ++user_index) {
WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[user_index];
if (RAWINPUT_WindowsGamingInputSlotMatches(state, gamepad_state)) {
++match_count;
*slot = gamepad_state;
/* Incrementing correlation_id for any match, as negative evidence for others being correlated */
*correlation_id = ++gamepad_state->correlation_id;
}
}
/* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
data. */
if (match_count == 1 && state->any_data) {
return SDL_TRUE;
}
return SDL_FALSE;
}
static void
RAWINPUT_QuitWindowsGamingInput(RAWINPUT_DeviceContext *ctx)
{
wgi_state.need_device_list_update = SDL_TRUE;
--wgi_state.ref_count;
if (!wgi_state.ref_count && wgi_state.initialized) {
for (int ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
__x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(wgi_state.per_gamepad[ii]->gamepad);
}
if (wgi_state.per_gamepad) {
SDL_free(wgi_state.per_gamepad);
wgi_state.per_gamepad = NULL;
}
wgi_state.per_gamepad_count = 0;
if (wgi_state.gamepad_statics) {
__x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(wgi_state.gamepad_statics);
wgi_state.gamepad_statics = NULL;
}
WIN_CoUninitialize();
wgi_state.initialized = SDL_FALSE;
}
}
#endif /* SDL_JOYSTICK_RAWINPUT_GAMING_INPUT */
614
615
616
617
618
619
620
621
static int
RAWINPUT_JoystickInit(void)
{
int ii;
RAWINPUTDEVICE rid[SDL_arraysize(subscribed_devices)];
SDL_assert(!SDL_RAWINPUT_inited);
SDL_assert(SDL_HelperWindow);
Nov 24, 2020
Nov 24, 2020
622
if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_RAWINPUT, SDL_TRUE)) {
Nov 24, 2020
Nov 24, 2020
624
}
Nov 24, 2020
Nov 24, 2020
626
if (WIN_LoadHIDDLL() < 0) {
627
628
629
630
return -1;
}
for (ii = 0; ii < SDL_arraysize(subscribed_devices); ii++) {
Nov 24, 2020
Nov 24, 2020
631
rid[ii].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
632
633
634
635
636
637
638
rid[ii].usUsage = subscribed_devices[ii];
rid[ii].dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK; /* Receive messages when in background, including device add/remove */
rid[ii].hwndTarget = SDL_HelperWindow;
}
if (!RegisterRawInputDevices(rid, SDL_arraysize(rid), sizeof(RAWINPUTDEVICE))) {
SDL_SetError("Couldn't initialize RAWINPUT");
Nov 24, 2020
Nov 24, 2020
639
WIN_UnloadHIDDLL();
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
return -1;
}
SDL_RAWINPUT_inited = SDL_TRUE;
RAWINPUT_JoystickDetect();
RAWINPUT_PumpMessages();
return 0;
}
static int
RAWINPUT_JoystickGetCount(void)
{
return SDL_RAWINPUT_numjoysticks;
}
Apr 13, 2020
Apr 13, 2020
656
657
658
659
660
661
662
663
664
665
666
static SDL_RAWINPUT_Device *
RAWINPUT_AcquireDevice(SDL_RAWINPUT_Device *device)
{
SDL_AtomicIncRef(&device->refcount);
return device;
}
static void
RAWINPUT_ReleaseDevice(SDL_RAWINPUT_Device *device)
{
if (SDL_AtomicDecRef(&device->refcount)) {
Nov 24, 2020
Nov 24, 2020
667
668
669
if (device->preparsed_data) {
SDL_HidD_FreePreparsedData(device->preparsed_data);
}
Apr 13, 2020
Apr 13, 2020
670
671
672
673
674
SDL_free(device->name);
SDL_free(device);
}
}
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
static SDL_RAWINPUT_Device *
RAWINPUT_DeviceFromHandle(HANDLE hDevice)
{
SDL_RAWINPUT_Device *curr;
for (curr = SDL_RAWINPUT_devices; curr; curr = curr->next) {
if (curr->hDevice == hDevice)
return curr;
}
return NULL;
}
static void
RAWINPUT_AddDevice(HANDLE hDevice)
{
#define CHECK(exp) { if(!(exp)) goto err; }
SDL_RAWINPUT_Device *device = NULL;
Nov 24, 2020
Nov 24, 2020
692
SDL_RAWINPUT_Device *curr, *last;
693
694
RID_DEVICE_INFO rdi;
UINT rdi_size = sizeof(rdi);
Nov 24, 2020
Nov 24, 2020
695
char dev_name[MAX_PATH];
696
UINT name_size = SDL_arraysize(dev_name);
Nov 24, 2020
Nov 24, 2020
697
HANDLE hFile = INVALID_HANDLE_VALUE;
Nov 24, 2020
Nov 24, 2020
699
700
701
702
/* Make sure we're not trying to add the same device twice */
if (RAWINPUT_DeviceFromHandle(hDevice)) {
return;
}
703
704
705
706
707
708
709
710
711
/* Figure out what kind of device it is */
CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICEINFO, &rdi, &rdi_size) != (UINT)-1);
CHECK(rdi.dwType == RIM_TYPEHID);
/* Get the device "name" (HID Path) */
CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICENAME, dev_name, &name_size) != (UINT)-1);
/* Only take XInput-capable devices */
CHECK(SDL_strstr(dev_name, "IG_") != NULL);
Nov 24, 2020
Nov 24, 2020
712
713
714
715
#ifdef SDL_JOYSTICK_HIDAPI
/* Don't take devices handled by HIDAPI */
CHECK(!HIDAPI_IsDevicePresent((Uint16)rdi.hid.dwVendorId, (Uint16)rdi.hid.dwProductId, (Uint16)rdi.hid.dwVersionNumber, ""));
#endif
Nov 24, 2020
Nov 24, 2020
717
CHECK(device = (SDL_RAWINPUT_Device *)SDL_calloc(1, sizeof(SDL_RAWINPUT_Device)));
718
719
720
721
device->hDevice = hDevice;
device->vendor_id = (Uint16)rdi.hid.dwVendorId;
device->product_id = (Uint16)rdi.hid.dwProductId;
device->version = (Uint16)rdi.hid.dwVersionNumber;
Nov 24, 2020
Nov 24, 2020
722
device->is_xinput = SDL_TRUE;
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
{
const Uint16 vendor = device->vendor_id;
const Uint16 product = device->product_id;
const Uint16 version = device->version;
Uint16 *guid16 = (Uint16 *)device->guid.data;
*guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(vendor);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(product);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(version);
*guid16++ = 0;
/* Note that this is a RAWINPUT device for special handling elsewhere */
device->guid.data[14] = 'r';
device->guid.data[15] = 0;
}
Nov 24, 2020
Nov 24, 2020
744
745
hFile = CreateFileA(dev_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
CHECK(hFile != INVALID_HANDLE_VALUE);
Nov 24, 2020
Nov 24, 2020
747
{
Apr 7, 2020
Apr 7, 2020
748
749
char *manufacturer_string = NULL;
char *product_string = NULL;
Nov 24, 2020
Nov 24, 2020
750
751
752
753
754
755
756
WCHAR string[128];
if (SDL_HidD_GetManufacturerString(hFile, string, sizeof(string))) {
manufacturer_string = WIN_StringToUTF8(string);
}
if (SDL_HidD_GetProductString(hFile, string, sizeof(string))) {
product_string = WIN_StringToUTF8(string);
Apr 7, 2020
Apr 7, 2020
757
758
759
760
761
762
763
764
765
766
}
device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
if (manufacturer_string) {
SDL_free(manufacturer_string);
}
if (product_string) {
SDL_free(product_string);
}
Nov 24, 2020
Nov 24, 2020
769
770
771
772
773
774
775
CHECK(SDL_HidD_GetPreparsedData(hFile, &device->preparsed_data));
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
device->joystick_id = SDL_GetNextJoystickInstanceID();
776
777
778
779
780
#ifdef DEBUG_RAWINPUT
SDL_Log("Adding RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle 0x%.8x\n", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
#endif
/* Add it to the list */
Apr 13, 2020
Apr 13, 2020
781
RAWINPUT_AcquireDevice(device);
782
783
784
785
786
787
788
789
790
791
for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
continue;
}
if (last) {
last->next = device;
} else {
SDL_RAWINPUT_devices = device;
}
++SDL_RAWINPUT_numjoysticks;
Nov 24, 2020
Nov 24, 2020
792
793
SDL_PrivateJoystickAdded(device->joystick_id);
794
795
796
797
return;
err:
Nov 24, 2020
Nov 24, 2020
798
799
800
if (hFile != INVALID_HANDLE_VALUE) {
CloseHandle(hFile);
}
801
802
803
804
805
if (device) {
if (device->name)
SDL_free(device->name);
SDL_free(device);
}
Nov 24, 2020
Nov 24, 2020
806
#undef CHECK
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
}
static void
RAWINPUT_DelDevice(SDL_RAWINPUT_Device *device, SDL_bool send_event)
{
SDL_RAWINPUT_Device *curr, *last;
for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
if (curr == device) {
if (last) {
last->next = curr->next;
} else {
SDL_RAWINPUT_devices = curr->next;
}
--SDL_RAWINPUT_numjoysticks;
Nov 24, 2020
Nov 24, 2020
822
SDL_PrivateJoystickRemoved(device->joystick_id);
823
824
825
826
#ifdef DEBUG_RAWINPUT
SDL_Log("Removing RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle 0x%.8x\n", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
#endif
Apr 13, 2020
Apr 13, 2020
827
RAWINPUT_ReleaseDevice(device);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
return;
}
}
}
static void
RAWINPUT_PumpMessages(void)
{
if (SDL_RAWINPUT_need_pump) {
MSG msg;
while (PeekMessage(&msg, SDL_HelperWindow, WM_INPUT, WM_INPUT, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
SDL_RAWINPUT_need_pump = SDL_FALSE;
}
}
static void
RAWINPUT_UpdateDeviceList(void)
{
MSG msg;
/* In theory, want only WM_INPUT_DEVICE_CHANGE messages here, but PeekMessage returns nothing unless you also ask
for WM_INPUT */
while (PeekMessage(&msg, SDL_HelperWindow, WM_INPUT_DEVICE_CHANGE, WM_INPUT, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Nov 24, 2020
Nov 24, 2020
858
859
static void
RAWINPUT_PostUpdate(void)
Nov 24, 2020
Nov 24, 2020
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
SDL_bool unmapped_guide_pressed = SDL_FALSE;
#ifdef SDL_JOYSTICK_RAWINPUT_GAMING_INPUT
if (!wgi_state.dirty) {
int ii;
for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
if (!gamepad_state->used && (gamepad_state->state.Buttons & GamepadButtons_GUIDE)) {
unmapped_guide_pressed = SDL_TRUE;
break;
}
}
}
wgi_state.dirty = SDL_TRUE;
#endif
Nov 24, 2020
Nov 24, 2020
879
880
881
882
883
884
885
886
#ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
if (!xinput_state_dirty) {
int ii;
for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
if (xinput_state[ii].connected && !xinput_state[ii].used && (xinput_state[ii].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)) {
unmapped_guide_pressed = SDL_TRUE;
break;
}
Nov 24, 2020
Nov 24, 2020
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
xinput_state_dirty = SDL_TRUE;
#endif
if (unmapped_guide_pressed) {
if (guide_button_candidate.joystick && !guide_button_candidate.last_joystick) {
SDL_Joystick *joystick = guide_button_candidate.joystick;
RAWINPUT_DeviceContext *ctx = joystick->hwdata;
if (ctx->guide_hack) {
int guide_button = joystick->nbuttons - 1;
SDL_PrivateJoystickButton(guide_button_candidate.joystick, guide_button, SDL_PRESSED);
}
guide_button_candidate.last_joystick = guide_button_candidate.joystick;
}
} else if (guide_button_candidate.last_joystick) {
SDL_Joystick *joystick = guide_button_candidate.last_joystick;
RAWINPUT_DeviceContext *ctx = joystick->hwdata;
if (ctx->guide_hack) {
int guide_button = joystick->nbuttons - 1;
SDL_PrivateJoystickButton(joystick, guide_button, SDL_RELEASED);
}
guide_button_candidate.last_joystick = NULL;
}
guide_button_candidate.joystick = NULL;
#endif /* SDL_JOYSTICK_RAWINPUT_MATCHING */
}
SDL_bool
RAWINPUT_IsEnabled()
{
return SDL_RAWINPUT_inited;
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
}
SDL_bool
RAWINPUT_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version)
{
SDL_RAWINPUT_Device *device;
/* Make sure the device list is completely up to date when we check for device presence */
RAWINPUT_UpdateDeviceList();
device = SDL_RAWINPUT_devices;
while (device) {
if (device->vendor_id == vendor_id && device->product_id == product_id) {
return SDL_TRUE;
}
device = device->next;
}
return SDL_FALSE;
}
static void
RAWINPUT_JoystickDetect(void)
{
/* Just ensure the window's add/remove messages have been pumped */
RAWINPUT_UpdateDeviceList();
Nov 24, 2020
Nov 24, 2020
948
949
RAWINPUT_PostUpdate();
950
951
952
953
SDL_RAWINPUT_need_pump = SDL_TRUE;
}
static SDL_RAWINPUT_Device *
Nov 24, 2020
Nov 24, 2020
954
RAWINPUT_GetDeviceByIndex(int device_index)
955
956
957
{
SDL_RAWINPUT_Device *device = SDL_RAWINPUT_devices;
while (device) {
Nov 24, 2020
Nov 24, 2020
958
959
if (device_index == 0) {
break;
Nov 24, 2020
Nov 24, 2020
961
--device_index;
962
963
device = device->next;
}
Nov 24, 2020
Nov 24, 2020
964
return device;
965
966
967
968
969
}
static const char *
RAWINPUT_JoystickGetDeviceName(int device_index)
{
Nov 24, 2020
Nov 24, 2020
970
return RAWINPUT_GetDeviceByIndex(device_index)->name;
971
972
973
974
975
}
static int
RAWINPUT_JoystickGetDevicePlayerIndex(int device_index)
{
Nov 24, 2020
Nov 24, 2020
976
return -1;
977
978
979
980
981
982
983
984
985
986
987
}
static void
RAWINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
}
static SDL_JoystickGUID
RAWINPUT_JoystickGetDeviceGUID(int device_index)
{
Nov 24, 2020
Nov 24, 2020
988
return RAWINPUT_GetDeviceByIndex(device_index)->guid;
989
990
991
992
993
}
static SDL_JoystickID
RAWINPUT_JoystickGetDeviceInstanceID(int device_index)
{
Nov 24, 2020
Nov 24, 2020
994
return RAWINPUT_GetDeviceByIndex(device_index)->joystick_id;
Nov 24, 2020
Nov 24, 2020
998
RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
Nov 24, 2020
Nov 24, 2020
1000
SDL_RAWINPUT_Device *device = RAWINPUT_GetDeviceByIndex(device_index);