/
SDL_hidapi_xboxone.c
806 lines (711 loc) · 30 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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
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.
*/
#include "../../SDL_internal.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "SDL_hints.h"
#include "SDL_events.h"
#include "SDL_timer.h"
#include "SDL_joystick.h"
#include "SDL_gamecontroller.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
32
#include "SDL_hidapi_rumble.h"
33
34
35
36
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
37
38
39
/* Define this if you want to log all packets from the controller */
/*#define DEBUG_XBOX_PROTOCOL*/
40
/* The amount of time to wait after hotplug to send controller init sequence */
41
#define CONTROLLER_INIT_DELAY_MS 1500 /* 475 for Xbox One S, 1275 for the PDP Battlefield 1 */
42
43
44
45
/* Connect controller */
static const Uint8 xboxone_init0[] = {
0x04, 0x20, 0x00, 0x00
46
};
47
/* Initial ack */
48
49
50
static const Uint8 xboxone_init1[] = {
0x01, 0x20, 0x01, 0x09, 0x00, 0x04, 0x20, 0x3a,
0x00, 0x00, 0x00, 0x80, 0x00
51
};
52
53
54
/* Start controller - extended? */
static const Uint8 xboxone_init2[] = {
0x05, 0x20, 0x00, 0x0F, 0x06, 0x00, 0x00, 0x00,
55
56
57
0x00, 0x00, 0x00, 0x55, 0x53, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
58
59
/* Start controller with input */
static const Uint8 xboxone_init3[] = {
60
61
0x05, 0x20, 0x03, 0x01, 0x00
};
62
63
64
/* Enable LED */
static const Uint8 xboxone_init4[] = {
0x0A, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
65
};
66
67
/* Start input reports? */
static const Uint8 xboxone_init5[] = {
68
69
0x06, 0x20, 0x00, 0x02, 0x01, 0x00
};
70
71
/* Start rumble? */
static const Uint8 xboxone_init6[] = {
72
0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
73
0x00, 0x00, 0xFF, 0x00, 0xEB
74
75
76
77
78
79
80
81
82
83
84
};
/*
* This specifies the selection of init packets that a gamepad
* will be sent on init *and* the order in which they will be
* sent. The correct sequence number will be added when the
* packet is going to be sent.
*/
typedef struct {
Uint16 vendor_id;
Uint16 product_id;
85
86
Uint16 exclude_vendor_id;
Uint16 exclude_product_id;
87
88
const Uint8 *data;
int size;
89
const Uint8 response[2];
90
91
} SDL_DriverXboxOne_InitPacket;
92
93
static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
94
95
96
97
98
{ 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init0, sizeof(xboxone_init0), { 0x04, 0xf0 } },
{ 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init1, sizeof(xboxone_init1), { 0x04, 0xb0 } },
{ 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init2, sizeof(xboxone_init2), { 0x00, 0x00 } },
{ 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init3, sizeof(xboxone_init3), { 0x00, 0x00 } },
{ 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init4, sizeof(xboxone_init4), { 0x00, 0x00 } },
99
100
/* These next packets are required for third party controllers (PowerA, PDP, HORI),
101
but aren't the correct protocol for Microsoft Xbox controllers.
102
103
104
*/
{ 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init5, sizeof(xboxone_init5), { 0x00, 0x00 } },
{ 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init6, sizeof(xboxone_init6), { 0x00, 0x00 } },
105
106
};
107
108
109
110
111
112
typedef enum {
XBOX_ONE_WIRELESS_PROTOCOL_UNKNOWN,
XBOX_ONE_WIRELESS_PROTOCOL_V1,
XBOX_ONE_WIRELESS_PROTOCOL_V2,
} SDL_XboxOneWirelessProtocol;
113
typedef struct {
114
115
Uint16 vendor_id;
Uint16 product_id;
116
SDL_bool bluetooth;
117
SDL_XboxOneWirelessProtocol wireless_protocol;
118
SDL_bool initialized;
119
Uint32 start_time;
120
121
Uint8 sequence;
Uint8 last_state[USB_PACKET_LENGTH];
122
SDL_bool has_paddles;
123
124
125
} SDL_DriverXboxOne_Context;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifdef DEBUG_XBOX_PROTOCOL
static void
DumpPacket(const char *prefix, Uint8 *data, int size)
{
int i;
char buffer[5*USB_PACKET_LENGTH];
SDL_snprintf(buffer, sizeof(buffer), prefix, size);
for (i = 0; i < size; ++i) {
if ((i % 8) == 0) {
SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), "\n%.2d: ", i);
}
SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), " 0x%.2x", data[i]);
}
SDL_strlcat(buffer, "\n", sizeof(buffer));
SDL_Log("%s", buffer);
}
#endif /* DEBUG_XBOX_PROTOCOL */
145
146
147
148
static SDL_bool
IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
{
/* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
149
if (vendor_id == USB_VENDOR_MICROSOFT) {
150
151
152
if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
153
154
return SDL_TRUE;
}
155
156
157
158
}
return SDL_FALSE;
}
159
160
161
162
static SDL_bool
ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
{
if (vendor_id == USB_VENDOR_MICROSOFT) {
163
164
if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2) {
165
166
167
168
169
170
return SDL_TRUE;
}
}
return SDL_FALSE;
}
171
/* Return true if this controller sends the 0x02 "waiting for init" packet */
172
static SDL_bool
173
ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
174
{
175
176
177
178
179
if (vendor_id == USB_VENDOR_HYPERKIN) {
/* The Hyperkin controllers always send 0x02 when waiting for init,
and the Hyperkin Duke plays an Xbox startup animation, so we want
to make sure we don't send the init sequence if it isn't needed.
*/
180
return SDL_TRUE;
181
182
183
}
if (vendor_id == USB_VENDOR_PDP) {
/* The PDP Rock Candy (PID 0x0246) doesn't send 0x02 on Linux for some reason */
184
return SDL_FALSE;
185
}
186
187
188
189
/* It doesn't hurt to reinit, especially if a driver has misconfigured the controller */
/*return SDL_TRUE;*/
return SDL_FALSE;
190
191
}
192
static SDL_bool
193
SendControllerInit(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_Context *ctx)
194
195
196
{
Uint16 vendor_id = ctx->vendor_id;
Uint16 product_id = ctx->product_id;
197
198
int i;
Uint8 init_packet[USB_PACKET_LENGTH];
199
200
201
for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
202
203
204
205
if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
continue;
}
206
207
208
209
if (packet->product_id && (product_id != packet->product_id)) {
continue;
}
210
211
212
213
if (packet->exclude_vendor_id && (vendor_id == packet->exclude_vendor_id)) {
continue;
}
214
215
216
217
if (packet->exclude_product_id && (product_id == packet->exclude_product_id)) {
continue;
}
218
219
220
221
222
223
224
225
226
SDL_memcpy(init_packet, packet->data, packet->size);
if (init_packet[0] != 0x01) {
init_packet[2] = ctx->sequence++;
}
if (hid_write(device->dev, init_packet, packet->size) != packet->size) {
SDL_SetError("Couldn't write Xbox One initialization packet");
return SDL_FALSE;
}
227
228
229
230
231
if (packet->response[0]) {
const Uint32 RESPONSE_TIMEOUT_MS = 50;
Uint32 start = SDL_GetTicks();
SDL_bool got_response = SDL_FALSE;
232
233
234
235
while (!got_response && !SDL_TICKS_PASSED(SDL_GetTicks(), start + RESPONSE_TIMEOUT_MS)) {
Uint8 data[USB_PACKET_LENGTH];
int size;
236
237
while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
238
#ifdef DEBUG_XBOX_PROTOCOL
239
DumpPacket("Xbox One INIT packet: size = %d", data, size);
240
#endif
241
242
if (size >= 2 && data[0] == packet->response[0] && data[1] == packet->response[1]) {
got_response = SDL_TRUE;
243
244
}
}
245
}
246
#ifdef DEBUG_XBOX_PROTOCOL
247
SDL_Log("Init sequence %d got response: %s\n", i, got_response ? "TRUE" : "FALSE");
248
#endif
249
250
251
252
253
}
}
return SDL_TRUE;
}
254
static SDL_bool
255
HIDAPI_DriverXboxOne_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
256
{
257
#ifdef __LINUX__
258
if (vendor_id == USB_VENDOR_POWERA && product_id == 0x541a) {
259
/* The PowerA Mini controller, model 1240245-01, blocks while writing feature reports */
260
261
return SDL_FALSE;
}
262
263
264
265
266
267
#endif
#ifdef __MACOSX__
/* Wired Xbox One controllers are handled by the 360Controller driver */
if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
return SDL_FALSE;
}
268
#endif
269
return (type == SDL_CONTROLLER_TYPE_XBOXONE);
270
271
272
273
274
}
static const char *
HIDAPI_DriverXboxOne_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
{
275
return NULL;
276
277
278
}
static SDL_bool
279
280
HIDAPI_DriverXboxOne_InitDevice(SDL_HIDAPI_Device *device)
{
281
return HIDAPI_JoystickConnected(device, NULL, SDL_FALSE);
282
283
}
284
285
286
287
288
289
290
291
292
293
294
static int
HIDAPI_DriverXboxOne_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
{
return -1;
}
static void
HIDAPI_DriverXboxOne_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
{
}
295
296
static SDL_bool
HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
297
298
299
300
301
302
303
304
305
{
SDL_DriverXboxOne_Context *ctx;
ctx = (SDL_DriverXboxOne_Context *)SDL_calloc(1, sizeof(*ctx));
if (!ctx) {
SDL_OutOfMemory();
return SDL_FALSE;
}
306
307
308
309
310
311
312
313
314
315
device->dev = hid_open_path(device->path, 0);
if (!device->dev) {
SDL_free(ctx);
SDL_SetError("Couldn't open %s", device->path);
return SDL_FALSE;
}
device->context = ctx;
ctx->vendor_id = device->vendor_id;
ctx->product_id = device->product_id;
316
317
ctx->bluetooth = IsBluetoothXboxOneController(device->vendor_id, device->product_id);
ctx->initialized = ctx->bluetooth ? SDL_TRUE : SDL_FALSE;
318
ctx->start_time = SDL_GetTicks();
319
ctx->sequence = 1;
320
ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
321
322
/* Initialize the joystick capabilities */
323
joystick->nbuttons = ctx->has_paddles ? SDL_CONTROLLER_BUTTON_MAX : (SDL_CONTROLLER_BUTTON_MAX + 4);
324
325
326
327
328
329
330
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
return SDL_TRUE;
}
static int
331
HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
332
{
333
334
335
336
SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
if (ctx->bluetooth) {
Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
337
338
339
rumble_packet[4] = (low_frequency_rumble >> 8);
rumble_packet[5] = (high_frequency_rumble >> 8);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
} else {
Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
/* Magnitude is 1..100 so scale the 16-bit input here */
rumble_packet[8] = low_frequency_rumble / 655;
rumble_packet[9] = high_frequency_rumble / 655;
if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
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
}
return 0;
}
static void
HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
Sint16 axis;
if (ctx->last_state[4] != data[4]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->last_state[5] != data[5]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
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
/* Xbox One S report is 18 bytes
Xbox One Elite Series 1 report is 33 bytes, paddles in data[32], mode in data[32] & 0x10, both modes have mapped paddles by default
Paddle bits:
UL: 0x01 (A) UR: 0x02 (B)
LL: 0x04 (X) LR: 0x08 (Y)
Xbox One Elite Series 2 report is 38 bytes, paddles in data[18], mode in data[19], mode 0 has no mapped paddles by default
Paddle bits:
UL: 0x04 (A) UR: 0x01 (B)
LL: 0x08 (X) LR: 0x02 (Y)
*/
if (ctx->has_paddles && (size == 33 || size == 38)) {
int paddle_index;
int button1_bit;
int button2_bit;
int button3_bit;
int button4_bit;
SDL_bool paddles_mapped;
if (size == 33) {
/* XBox One Elite Series 1 */
paddle_index = 32;
button1_bit = 0x01;
button2_bit = 0x02;
button3_bit = 0x04;
button4_bit = 0x08;
/* The mapped controller state is at offset 4, the raw state is at offset 18, compare them to see if the paddles are mapped */
paddles_mapped = (SDL_memcmp(&data[4], &data[18], 14) != 0);
412
} else /* if (size == 38) */ {
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* XBox One Elite Series 2 */
paddle_index = 18;
button1_bit = 0x04;
button2_bit = 0x01;
button3_bit = 0x08;
button4_bit = 0x02;
paddles_mapped = (data[19] != 0);
}
#ifdef DEBUG_XBOX_PROTOCOL
SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s\n",
(data[paddle_index] & button1_bit) ? 1 : 0,
(data[paddle_index] & button2_bit) ? 1 : 0,
(data[paddle_index] & button3_bit) ? 1 : 0,
(data[paddle_index] & button4_bit) ? 1 : 0,
paddles_mapped ? "TRUE" : "FALSE"
);
#endif
if (paddles_mapped) {
432
/* Respect that the paddles are being used for other controls and don't pass them on to the app */
433
data[paddle_index] = 0;
434
}
435
436
437
438
439
440
if (ctx->last_state[paddle_index] != data[paddle_index]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+0, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+1, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+2, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+3, (data[paddle_index] & button4_bit) ? SDL_PRESSED : SDL_RELEASED);
441
442
443
}
}
444
axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
445
446
447
if (axis == 32704) {
axis = 32767;
}
448
449
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
450
451
452
if (axis == 32704) {
axis = 32767;
}
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
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
axis = *(Sint16*)(&data[10]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
axis = *(Sint16*)(&data[12]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
axis = *(Sint16*)(&data[14]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
axis = *(Sint16*)(&data[16]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
}
static void
HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
if (data[1] == 0x30) {
/* The Xbox One S controller needs acks for mode reports */
const Uint8 seqnum = data[2];
const Uint8 ack[] = { 0x01, 0x20, seqnum, 0x09, 0x00, 0x07, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
hid_write(dev, ack, sizeof(ack));
}
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
479
static void
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
Sint16 axis;
if (ctx->last_state[14] != data[14]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->last_state[15] != data[15]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->last_state[13] != data[13]) {
SDL_bool dpad_up = SDL_FALSE;
SDL_bool dpad_down = SDL_FALSE;
SDL_bool dpad_left = SDL_FALSE;
SDL_bool dpad_right = SDL_FALSE;
switch (data[13]) {
case 1:
dpad_up = SDL_TRUE;
break;
case 2:
dpad_up = SDL_TRUE;
dpad_right = SDL_TRUE;
break;
case 3:
dpad_right = SDL_TRUE;
break;
case 4:
dpad_right = SDL_TRUE;
dpad_down = SDL_TRUE;
break;
case 5:
dpad_down = SDL_TRUE;
break;
case 6:
dpad_left = SDL_TRUE;
dpad_down = SDL_TRUE;
break;
case 7:
dpad_left = SDL_TRUE;
break;
case 8:
dpad_up = SDL_TRUE;
dpad_left = SDL_TRUE;
break;
default:
break;
}
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
}
axis = (int)*(Uint16*)(&data[1]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
axis = (int)*(Uint16*)(&data[3]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
axis = (int)*(Uint16*)(&data[5]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
axis = (int)*(Uint16*)(&data[7]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
}
static void
HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
{
Sint16 axis;
if (ctx->last_state[14] != data[14]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->last_state[15] != data[15]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[15] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
586
587
588
589
590
591
592
593
594
595
if (ctx->wireless_protocol == XBOX_ONE_WIRELESS_PROTOCOL_UNKNOWN)
{
if (data[15] & 0x10) {
ctx->wireless_protocol = XBOX_ONE_WIRELESS_PROTOCOL_V2;
}
}
if (ctx->wireless_protocol == XBOX_ONE_WIRELESS_PROTOCOL_V2)
{
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[15] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
}
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
}
if (ctx->last_state[16] != data[16]) {
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->last_state[13] != data[13]) {
SDL_bool dpad_up = SDL_FALSE;
SDL_bool dpad_down = SDL_FALSE;
SDL_bool dpad_left = SDL_FALSE;
SDL_bool dpad_right = SDL_FALSE;
switch (data[13]) {
case 1:
dpad_up = SDL_TRUE;
break;
case 2:
dpad_up = SDL_TRUE;
dpad_right = SDL_TRUE;
break;
case 3:
dpad_right = SDL_TRUE;
break;
case 4:
dpad_right = SDL_TRUE;
dpad_down = SDL_TRUE;
break;
case 5:
dpad_down = SDL_TRUE;
break;
case 6:
dpad_left = SDL_TRUE;
dpad_down = SDL_TRUE;
break;
case 7:
dpad_left = SDL_TRUE;
break;
case 8:
dpad_up = SDL_TRUE;
dpad_left = SDL_TRUE;
break;
default:
break;
}
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
}
axis = (int)*(Uint16*)(&data[1]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
axis = (int)*(Uint16*)(&data[3]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
axis = (int)*(Uint16*)(&data[5]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
axis = (int)*(Uint16*)(&data[7]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
}
static void
HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
673
ctx->wireless_protocol = XBOX_ONE_WIRELESS_PROTOCOL_V1;
674
675
676
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
677
static SDL_bool
678
HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
679
{
680
681
SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
SDL_Joystick *joystick = NULL;
682
683
684
Uint8 data[USB_PACKET_LENGTH];
int size;
685
686
687
688
689
690
691
if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
}
if (!joystick) {
return SDL_FALSE;
}
692
693
if (!ctx->initialized &&
!ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
694
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
695
if (!SendControllerInit(device, ctx)) {
696
HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
697
698
699
700
701
702
return SDL_FALSE;
}
ctx->initialized = SDL_TRUE;
}
}
703
while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
704
#ifdef DEBUG_XBOX_PROTOCOL
705
DumpPacket("Xbox One packet: size = %d", data, size);
706
#endif
707
708
709
if (ctx->bluetooth) {
switch (data[0]) {
case 0x01:
710
711
712
713
714
715
716
717
718
719
switch (size) {
case 16:
HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(joystick, device->dev, ctx, data, size);
break;
case 17:
HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(joystick, device->dev, ctx, data, size);
break;
default:
break;
}
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
break;
case 0x02:
HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, device->dev, ctx, data, size);
break;
default:
#ifdef DEBUG_JOYSTICK
SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
#endif
break;
}
} else {
switch (data[0]) {
case 0x02:
/* Controller is connected and waiting for initialization */
if (!ctx->initialized) {
735
#ifdef DEBUG_XBOX_PROTOCOL
736
SDL_Log("Delay after init: %ums\n", SDL_GetTicks() - ctx->start_time);
737
#endif
738
if (!SendControllerInit(device, ctx)) {
739
HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
740
741
742
return SDL_FALSE;
}
ctx->initialized = SDL_TRUE;
743
}
744
745
746
747
748
749
750
751
752
753
754
break;
case 0x03:
/* Controller heartbeat */
break;
case 0x20:
HIDAPI_DriverXboxOne_HandleStatePacket(joystick, device->dev, ctx, data, size);
break;
case 0x07:
HIDAPI_DriverXboxOne_HandleModePacket(joystick, device->dev, ctx, data, size);
break;
default:
755
#ifdef DEBUG_JOYSTICK
756
SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
757
#endif
758
759
break;
}
760
761
762
}
}
763
764
if (size < 0) {
/* Read error, device is disconnected */
765
HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
766
}
767
return (size >= 0);
768
769
}
770
static void
771
772
773
774
775
776
777
778
779
780
781
HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
hid_close(device->dev);
device->dev = NULL;
SDL_free(device->context);
device->context = NULL;
}
static void
HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)
782
783
784
{
}
785
786
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
{
787
SDL_HINT_JOYSTICK_HIDAPI_XBOX,
788
789
790
SDL_TRUE,
HIDAPI_DriverXboxOne_IsSupportedDevice,
HIDAPI_DriverXboxOne_GetDeviceName,
791
HIDAPI_DriverXboxOne_InitDevice,
792
793
HIDAPI_DriverXboxOne_GetDevicePlayerIndex,
HIDAPI_DriverXboxOne_SetDevicePlayerIndex,
794
795
796
797
HIDAPI_DriverXboxOne_UpdateDevice,
HIDAPI_DriverXboxOne_OpenJoystick,
HIDAPI_DriverXboxOne_RumbleJoystick,
HIDAPI_DriverXboxOne_CloseJoystick,
798
799
HIDAPI_DriverXboxOne_FreeDevice,
NULL
800
801
802
803
804
805
806
};
#endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */
#endif /* SDL_JOYSTICK_HIDAPI */
/* vi: set ts=4 sw=4 expandtab: */