Refactored Xbox One Bluetooth protocol and verified Xbox One S, Xbox Series X, and Xbox One Elite Series 2 controllers
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
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index c40210f..77ebbc4 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -103,12 +103,6 @@ static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
};
typedef enum {
- XBOX_ONE_WIRELESS_PROTOCOL_UNKNOWN,
- XBOX_ONE_WIRELESS_PROTOCOL_V1,
- XBOX_ONE_WIRELESS_PROTOCOL_V2
-} SDL_XboxOneWirelessProtocol;
-
-typedef enum {
XBOX_ONE_INIT_STATE_START_NEGOTIATING = 0,
XBOX_ONE_INIT_STATE_NEGOTIATING = 1,
XBOX_ONE_INIT_STATE_PREPARE_INPUT = 2,
@@ -119,7 +113,6 @@ typedef struct {
Uint16 vendor_id;
Uint16 product_id;
SDL_bool bluetooth;
- SDL_XboxOneWirelessProtocol wireless_protocol;
SDL_XboxOneInitState init_state;
int init_packet;
Uint32 start_time;
@@ -463,7 +456,7 @@ HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Jo
}
static void
-HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
Sint16 axis;
@@ -598,16 +591,14 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
}
static void
-HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
static void
-HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+HIDAPI_DriverXboxOneBluetooth_HandleButtons16(SDL_Joystick *joystick, 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);
@@ -624,79 +615,36 @@ HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(SDL_Joystick *joystick, hid_de
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);
+static void
+HIDAPI_DriverXboxOneBluetooth_HandleButtons17(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+{
+ 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);
}
- 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;
+ if (ctx->last_state[15] != data[15]) {
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[15] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
+ 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);
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[15] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
}
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
- axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
- if (axis == 32704) {
- axis = 32767;
+ if (ctx->last_state[16] != data[16]) {
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MISC1, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
- 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)
+HIDAPI_DriverXboxOneBluetooth_HandleButtonsElite(SDL_Joystick *joystick, 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);
@@ -710,66 +658,13 @@ HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(SDL_Joystick *joystick, hid_de
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);
- 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);
- }
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[15] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
}
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);
- }
-
/*
Paddle bits:
P3: 0x04 (A) P1: 0x01 (B)
@@ -824,6 +719,67 @@ HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(SDL_Joystick *joystick, hid_de
SDL_PrivateJoystickButton(joystick, nButton++, (data[paddle_index] & button4_bit) ? SDL_PRESSED : SDL_RELEASED);
}
}
+}
+
+static void
+HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+{
+ Sint16 axis;
+
+ if (size == 16) {
+ /* Xbox One S, with separate report for guide button */
+ HIDAPI_DriverXboxOneBluetooth_HandleButtons16(joystick, ctx, data, size);
+ } else if (size == 17) {
+ /* Xbox One Series X, with guide button in the last byte of the report */
+ HIDAPI_DriverXboxOneBluetooth_HandleButtons17(joystick, ctx, data, size);
+ } else if (size > 17) {
+ /* Xbox One Elite Series 2, with paddles */
+ HIDAPI_DriverXboxOneBluetooth_HandleButtonsElite(joystick, ctx, data, size);
+ }
+
+ 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);
@@ -850,14 +806,13 @@ HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(SDL_Joystick *joystick, hid_de
}
static void
-HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
- ctx->wireless_protocol = XBOX_ONE_WIRELESS_PROTOCOL_V1;
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
static void
-HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
{
Uint8 flags = data[1];
SDL_bool on_usb = (((flags & 0x0C) >> 2) == 0);
@@ -965,10 +920,8 @@ HIDAPI_DriverXboxOne_UpdateJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
if (ctx->bluetooth) {
switch (data[0]) {
case 0x01:
- if (size == 16) {
- HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV1(joystick, device->dev, ctx, data, size);
- } else if (size > 16) {
- HIDAPI_DriverXboxOneBluetooth_HandleStatePacketV2(joystick, device->dev, ctx, data, size);
+ if (size >= 16) {
+ HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(joystick, ctx, data, size);
} else {
#ifdef DEBUG_JOYSTICK
SDL_Log("Unknown Xbox One Bluetooth packet size: %d\n", size);
@@ -976,10 +929,10 @@ HIDAPI_DriverXboxOne_UpdateJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
}
break;
case 0x02:
- HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, device->dev, ctx, data, size);
+ HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, ctx, data, size);
break;
case 0x04:
- HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(joystick, device->dev, ctx, data, size);
+ HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(joystick, ctx, data, size);
break;
default:
#ifdef DEBUG_JOYSTICK
@@ -1031,7 +984,7 @@ HIDAPI_DriverXboxOne_UpdateJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
/* Unknown chatty controller information, sent by both sides */
break;
case 0x07:
- HIDAPI_DriverXboxOne_HandleModePacket(joystick, device->dev, ctx, data, size);
+ HIDAPI_DriverXboxOne_HandleModePacket(joystick, ctx, data, size);
break;
case 0x1E:
/* If the packet starts with this:
@@ -1059,7 +1012,7 @@ HIDAPI_DriverXboxOne_UpdateJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
#endif
break;
}
- HIDAPI_DriverXboxOne_HandleStatePacket(joystick, device->dev, ctx, data, size);
+ HIDAPI_DriverXboxOne_HandleStatePacket(joystick, ctx, data, size);
break;
default:
#ifdef DEBUG_JOYSTICK