From a807b14957038349ee503db491bd6f43a7d74e10 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 22 Jun 2023 14:33:07 -0700
Subject: [PATCH] Greatly improved Xbox One controller initialization sequence
---
src/joystick/hidapi/SDL_hidapi_xboxone.c | 919 +++++++++++++++--------
1 file changed, 602 insertions(+), 317 deletions(-)
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index 48c63b59bd5b..94323718b74d 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -35,7 +35,13 @@
/* Define this if you want to log all packets from the controller */
/*#define DEBUG_XBOX_PROTOCOL*/
-#define CONTROLLER_NEGOTIATION_TIMEOUT_MS 300
+#if defined(__WIN32__) || defined(__WINGDK__)
+#define XBOX_ONE_DRIVER_ACTIVE 1
+#else
+#define XBOX_ONE_DRIVER_ACTIVE 0
+#endif
+
+#define CONTROLLER_IDENTIFY_TIMEOUT_MS 100
#define CONTROLLER_PREPARE_INPUT_TIMEOUT_MS 50
/* Deadzone thresholds */
@@ -43,28 +49,28 @@
#define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
#define XINPUT_GAMEPAD_TRIGGER_THRESHOLD -25058 /* Uint8 30 scaled to Sint16 full range */
-/* Start controller */
-static const Uint8 xboxone_init0[] = {
- 0x05, 0x20, 0x03, 0x01, 0x00
+/* Power on */
+static const Uint8 xbox_init_power_on[] = {
+ 0x05, 0x20, 0x00, 0x01, 0x00
};
/* Enable LED */
-static const Uint8 xboxone_init1[] = {
+static const Uint8 xbox_init_enable_led[] = {
0x0A, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
};
+/* This controller passed security check */
+static const Uint8 xbox_init_security_passed[] = {
+ 0x06, 0x20, 0x00, 0x02, 0x01, 0x00
+};
/* Some PowerA controllers need to actually start the rumble motors */
-static const Uint8 xboxone_powera_rumble_init[] = {
+static const Uint8 xbox_init_powera_rumble[] = {
0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
0x1D, 0x1D, 0xFF, 0x00, 0x00
};
/* Setup rumble (not needed for Microsoft controllers, but it doesn't hurt) */
-static const Uint8 xboxone_init2[] = {
+static const Uint8 xbox_init_rumble[] = {
0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
0x00, 0x00, 0xFF, 0x00, 0xEB
};
-/* This controller passed security check */
-static const Uint8 security_passed_packet[] = {
- 0x06, 0x20, 0x00, 0x02, 0x01, 0x00
-};
/*
* This specifies the selection of init packets that a gamepad
@@ -76,28 +82,25 @@ typedef struct
{
Uint16 vendor_id;
Uint16 product_id;
- Uint16 exclude_vendor_id;
- Uint16 exclude_product_id;
const Uint8 *data;
int size;
- const Uint8 response[2];
} SDL_DriverXboxOne_InitPacket;
static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
- { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init0, sizeof(xboxone_init0), { 0x00, 0x00 } },
- { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init1, sizeof(xboxone_init1), { 0x00, 0x00 } },
- /* The PDP Rock Candy and Victrix Gambit controllers don't start sending input until they get this packet */
- { 0x0e6f, 0x0000, 0x0000, 0x0000, security_passed_packet, sizeof(security_passed_packet), { 0x00, 0x00 } },
- { 0x24c6, 0x541a, 0x0000, 0x0000, xboxone_powera_rumble_init, sizeof(xboxone_powera_rumble_init), { 0x00, 0x00 } },
- { 0x24c6, 0x542a, 0x0000, 0x0000, xboxone_powera_rumble_init, sizeof(xboxone_powera_rumble_init), { 0x00, 0x00 } },
- { 0x24c6, 0x543a, 0x0000, 0x0000, xboxone_powera_rumble_init, sizeof(xboxone_powera_rumble_init), { 0x00, 0x00 } },
- { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init2, sizeof(xboxone_init2), { 0x00, 0x00 } },
+ { 0x0000, 0x0000, xbox_init_power_on, sizeof(xbox_init_power_on) },
+ { 0x0000, 0x0000, xbox_init_enable_led, sizeof(xbox_init_enable_led) },
+ { 0x0000, 0x0000, xbox_init_security_passed, sizeof(xbox_init_security_passed) },
+ { 0x24c6, 0x541a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
+ { 0x24c6, 0x542a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
+ { 0x24c6, 0x543a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
+ { 0x0000, 0x0000, xbox_init_rumble, sizeof(xbox_init_rumble) },
};
typedef enum
{
- XBOX_ONE_INIT_STATE_START_NEGOTIATING,
- XBOX_ONE_INIT_STATE_NEGOTIATING,
+ XBOX_ONE_INIT_STATE_ANNOUNCED,
+ XBOX_ONE_INIT_STATE_IDENTIFYING,
+ XBOX_ONE_INIT_STATE_STARTUP,
XBOX_ONE_INIT_STATE_PREPARE_INPUT,
XBOX_ONE_INIT_STATE_COMPLETE,
} SDL_XboxOneInitState;
@@ -116,7 +119,6 @@ typedef struct
Uint16 product_id;
SDL_bool bluetooth;
SDL_XboxOneInitState init_state;
- int init_packet;
Uint64 start_time;
Uint8 sequence;
Uint64 send_time;
@@ -135,6 +137,8 @@ typedef struct
Uint64 rumble_time;
SDL_bool rumble_pending;
Uint8 last_state[USB_PACKET_LENGTH];
+ Uint8 *chunk_buffer;
+ Uint32 chunk_length;
} SDL_DriverXboxOne_Context;
static SDL_bool ControllerHasColorLED(Uint16 vendor_id, Uint16 product_id)
@@ -210,72 +214,86 @@ static void SetInitState(SDL_DriverXboxOne_Context *ctx, SDL_XboxOneInitState st
ctx->init_state = state;
}
-static void SendAckIfNeeded(SDL_HIDAPI_Device *device, const Uint8 *data, int size)
+static Uint8 GetNextPacketSequence(SDL_DriverXboxOne_Context *ctx)
{
-#if defined(__WIN32__) || defined(__WINGDK__)
- /* The Windows driver is taking care of acks */
-#else
- if ((data[1] & 0x30) == 0x30) {
- Uint8 ack_packet[] = { 0x01, 0x20, 0x00, 0x09, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
- ack_packet[2] = data[2];
- ack_packet[5] = data[0];
- ack_packet[7] = data[3];
-
- /* The initial ack needs 0x80 added to the response, for some reason */
- if (data[0] == 0x04 && data[1] == 0xF0) {
- ack_packet[11] = 0x80;
- }
+ ++ctx->sequence;
+ if (!ctx->sequence) {
+ ctx->sequence = 1;
+ }
+ return ctx->sequence;
+}
+static SDL_bool SendProtocolPacket(SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
+{
#ifdef DEBUG_XBOX_PROTOCOL
- HIDAPI_DumpPacket("Xbox One sending ACK packet: size = %d", ack_packet, sizeof(ack_packet));
+ HIDAPI_DumpPacket("Xbox One sending packet: size = %d", data, size);
#endif
- if (SDL_HIDAPI_LockRumble() != 0 ||
- SDL_HIDAPI_SendRumbleAndUnlock(device, ack_packet, sizeof(ack_packet)) != sizeof(ack_packet)) {
- SDL_SetError("Couldn't send ack packet");
- }
+
+ ctx->send_time = SDL_GetTicks();
+
+ if (SDL_HIDAPI_LockRumble() != 0) {
+ return SDL_FALSE;
+ }
+ if (SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size) != size) {
+ return SDL_FALSE;
}
-#endif /* defined(__WIN32__) || defined(__WINGDK__ */
+ return SDL_TRUE;
}
#if 0
-static SDL_bool SendSerialRequest(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_Context *ctx)
+static SDL_bool SendSerialRequest(SDL_DriverXboxOne_Context *ctx)
{
- Uint8 serial_packet[] = { 0x1E, 0x30, 0x07, 0x01, 0x04 };
+ Uint8 packet[] = { 0x1E, 0x20, 0x00, 0x01, 0x04 };
- ctx->send_time = SDL_GetTicks();
+ packet[2] = GetNextPacketSequence(ctx);
/* Request the serial number
- * Sending this should be done only after the negotiation is complete.
+ * Sending this should be done only after startup is complete.
* It will cancel the announce packet if sent before that, and will be
- * ignored if sent during the negotiation.
+ * ignored if sent during the startup sequence.
*/
- if (SDL_HIDAPI_LockRumble() != 0 ||
- SDL_HIDAPI_SendRumbleAndUnlock(device, serial_packet, sizeof(serial_packet)) != sizeof(serial_packet)) {
- SDL_SetError("Couldn't send serial packet");
+ if (!SendProtocolPacket(ctx, packet, sizeof(packet))) {
+ SDL_SetError("Couldn't send serial request packet");
return SDL_FALSE;
}
return SDL_TRUE;
}
#endif
-static SDL_bool ControllerNeedsNegotiation(SDL_DriverXboxOne_Context *ctx)
+static SDL_bool ControllerSendsAnnouncement(Uint16 vendor_id, Uint16 product_id)
{
- if (ctx->vendor_id == USB_VENDOR_PDP && ctx->product_id == 0x0246) {
+ if (vendor_id == USB_VENDOR_PDP && product_id == 0x0246) {
/* The PDP Rock Candy (PID 0x0246) doesn't send the announce packet on Linux for some reason */
- return SDL_TRUE;
+ return SDL_FALSE;
}
- return SDL_FALSE;
+ return SDL_TRUE;
}
-static SDL_bool SendControllerInit(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_Context *ctx)
+static SDL_bool SendIdentificationRequest(SDL_DriverXboxOne_Context *ctx)
+{
+ /* Request identification, sent in response to announce packet */
+ Uint8 packet[] = {
+ 0x04, 0x20, 0x00, 0x00
+ };
+
+ packet[2] = GetNextPacketSequence(ctx);
+
+ if (!SendProtocolPacket(ctx, packet, sizeof(packet))) {
+ SDL_SetError("Couldn't send identification request packet");
+ return SDL_FALSE;
+ }
+ return SDL_TRUE;
+}
+
+static SDL_bool SendControllerStartup(SDL_DriverXboxOne_Context *ctx)
{
Uint16 vendor_id = ctx->vendor_id;
Uint16 product_id = ctx->product_id;
Uint8 init_packet[USB_PACKET_LENGTH];
+ size_t i;
- for (; ctx->init_packet < SDL_arraysize(xboxone_init_packets); ++ctx->init_packet) {
- const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[ctx->init_packet];
+ for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
+ const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
continue;
@@ -285,48 +303,26 @@ static SDL_bool SendControllerInit(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_
continue;
}
- if (packet->exclude_vendor_id && (vendor_id == packet->exclude_vendor_id)) {
- continue;
- }
-
- if (packet->exclude_product_id && (product_id == packet->exclude_product_id)) {
- continue;
- }
-
SDL_memcpy(init_packet, packet->data, packet->size);
- if (init_packet[0] != 0x01) {
- init_packet[2] = ctx->sequence++;
- }
+ init_packet[2] = GetNextPacketSequence(ctx);
+
if (init_packet[0] == 0x0A) {
/* Get the initial brightness value */
int brightness = GetHomeLEDBrightness(SDL_GetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED));
init_packet[5] = (brightness > 0) ? 0x01 : 0x00;
init_packet[6] = (Uint8)brightness;
}
-#ifdef DEBUG_XBOX_PROTOCOL
- HIDAPI_DumpPacket("Xbox One sending INIT packet: size = %d", init_packet, packet->size);
-#endif
- ctx->send_time = SDL_GetTicks();
- if (SDL_HIDAPI_LockRumble() != 0 ||
- SDL_HIDAPI_SendRumbleAndUnlock(device, init_packet, packet->size) != packet->size) {
- SDL_SetError("Couldn't write Xbox One initialization packet");
+ if (!SendProtocolPacket(ctx, init_packet, packet->size)) {
+ SDL_SetError("Couldn't send initialization packet");
return SDL_FALSE;
}
- if (packet->response[0]) {
- return SDL_TRUE;
- }
-
/* Wait to process the rumble packet */
- if (packet->data == xboxone_powera_rumble_init) {
+ if (packet->data == xbox_init_powera_rumble) {
SDL_Delay(10);
}
}
-
- /* All done with the negotiation, prepare for input! */
- SetInitState(ctx, XBOX_ONE_INIT_STATE_PREPARE_INPUT);
-
return SDL_TRUE;
}
@@ -374,17 +370,17 @@ static SDL_bool HIDAPI_DriverXboxOne_InitDevice(SDL_HIDAPI_Device *device)
ctx->vendor_id = device->vendor_id;
ctx->product_id = device->product_id;
- ctx->bluetooth = SDL_IsJoystickBluetoothXboxOne(device->vendor_id, device->product_id);
ctx->start_time = SDL_GetTicks();
- ctx->sequence = 1;
+ ctx->sequence = 0;
ctx->has_color_led = ControllerHasColorLED(ctx->vendor_id, ctx->product_id);
ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
ctx->has_trigger_rumble = ControllerHasTriggerRumble(ctx->vendor_id, ctx->product_id);
ctx->has_share_button = ControllerHasShareButton(ctx->vendor_id, ctx->product_id);
/* Assume that the controller is correctly initialized when we start */
- if (ControllerNeedsNegotiation(ctx)) {
- ctx->init_state = XBOX_ONE_INIT_STATE_START_NEGOTIATING;
+ if (!ControllerSendsAnnouncement(device->vendor_id, device->product_id)) {
+ /* Jump into the startup sequence for this controller */
+ ctx->init_state = XBOX_ONE_INIT_STATE_STARTUP;
} else {
ctx->init_state = XBOX_ONE_INIT_STATE_COMPLETE;
}
@@ -432,7 +428,7 @@ static SDL_bool HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL
}
joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
- if (!ctx->bluetooth) {
+ if (!device->is_bluetooth) {
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
}
@@ -447,10 +443,8 @@ static void HIDAPI_DriverXboxOne_RumbleSent(void *userdata)
ctx->rumble_time = SDL_GetTicks();
}
-static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
+static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_DriverXboxOne_Context *ctx)
{
- SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
-
if (ctx->rumble_state == XBOX_ONE_RUMBLE_STATE_QUEUED) {
if (ctx->rumble_time) {
ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_BUSY;
@@ -458,7 +452,7 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
}
if (ctx->rumble_state == XBOX_ONE_RUMBLE_STATE_BUSY) {
- const int RUMBLE_BUSY_TIME_MS = ctx->bluetooth ? 50 : 10;
+ const int RUMBLE_BUSY_TIME_MS = ctx->device->is_bluetooth ? 50 : 10;
if (SDL_GetTicks() >= (ctx->rumble_time + RUMBLE_BUSY_TIME_MS)) {
ctx->rumble_time = 0;
ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_IDLE;
@@ -480,7 +474,7 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
return -1;
}
- if (ctx->bluetooth) {
+ if (ctx->device->is_bluetooth) {
Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
rumble_packet[2] = ctx->left_trigger_rumble;
@@ -488,7 +482,7 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
rumble_packet[4] = ctx->low_frequency_rumble;
rumble_packet[5] = ctx->high_frequency_rumble;
- if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
+ if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(ctx->device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
} else {
@@ -499,7 +493,7 @@ static int HIDAPI_DriverXboxOne_UpdateRumble(SDL_HIDAPI_Device *device)
rumble_packet[8] = ctx->low_frequency_rumble;
rumble_packet[9] = ctx->high_frequency_rumble;
- if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
+ if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(ctx->device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
}
@@ -518,7 +512,7 @@ static int HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Jo
ctx->high_frequency_rumble = (Uint8)(high_frequency_rumble / 655);
ctx->rumble_pending = SDL_TRUE;
- return HIDAPI_DriverXboxOne_UpdateRumble(device);
+ return HIDAPI_DriverXboxOne_UpdateRumble(ctx);
}
static int HIDAPI_DriverXboxOne_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
@@ -534,7 +528,7 @@ static int HIDAPI_DriverXboxOne_RumbleJoystickTriggers(SDL_HIDAPI_Device *device
ctx->right_trigger_rumble = (Uint8)(right_rumble / 655);
ctx->rumble_pending = SDL_TRUE;
- return HIDAPI_DriverXboxOne_UpdateRumble(device);
+ return HIDAPI_DriverXboxOne_UpdateRumble(ctx);
}
static Uint32 HIDAPI_DriverXboxOne_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
@@ -600,18 +594,18 @@ static void HIDAPI_DriverXboxOne_HandleUnmappedStatePacket(SDL_Joystick *joystic
SDL_bool paddles_mapped;
Uint64 timestamp = SDL_GetTicksNS();
- if (size == 21) {
+ if (size == 17) {
/* XBox One Elite Series 2 */
- paddle_index = 18;
+ paddle_index = 14;
button1_bit = 0x01;
button2_bit = 0x02;
button3_bit = 0x04;
button4_bit = 0x08;
- profile = data[19];
+ profile = data[15];
if (profile == 0) {
paddles_mapped = SDL_FALSE;
- } else if (SDL_memcmp(&data[4], &ctx->last_state[4], 14) == 0) {
+ } else if (SDL_memcmp(&data[0], &ctx->last_state[0], 14) == 0) {
/* We're using a profile, but paddles aren't mapped */
paddles_mapped = SDL_FALSE;
} else {
@@ -653,87 +647,87 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
Sint16 axis;
Uint64 timestamp = SDL_GetTicksNS();
- /* Some controllers have larger packets over NDIS, but the real size is in data[3] */
- size = SDL_min(4 + data[3], size);
-
/* Enable paddles on the Xbox Elite controller when connected over USB */
- if (ctx->has_paddles && !ctx->has_unmapped_state && size == 50) {
+ if (ctx->has_paddles && !ctx->has_unmapped_state && size == 46) {
Uint8 packet[] = { 0x4d, 0x00, 0x00, 0x02, 0x07, 0x00 };
+#ifdef DEBUG_JOYSTICK
+ SDL_Log("Enabling paddles on XBox Elite 2\n");
+#endif
SDL_HIDAPI_SendRumble(ctx->device, packet, sizeof(packet));
}
- if (ctx->last_state[4] != data[4]) {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
+ if (ctx->last_state[0] != data[0]) {
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, (data[0] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, (data[0] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_A, (data[0] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_B, (data[0] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_X, (data[0] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_Y, (data[0] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
- if (ctx->last_state[5] != data[5]) {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
+ if (ctx->last_state[1] != data[1]) {
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_UP, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_DOWN, (data[1] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_LEFT, (data[1] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, (data[1] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
if (ctx->vendor_id == USB_VENDOR_RAZER && ctx->product_id == USB_PRODUCT_RAZER_ATROX) {
/* The Razer Atrox has the right and left shoulder bits reversed */
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, (data[1] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, (data[1] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
} else {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, (data[1] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, (data[1] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
}
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, (data[1] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, (data[1] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
}
if (ctx->has_share_button) {
- /* Xbox Series X firmware version 5.0, report is 36 bytes, share button is in byte 18
- * Xbox Series X firmware version 5.1, report is 44 bytes, share button is in byte 18
- * Xbox Series X firmware version 5.5, report is 48 bytes, share button is in byte 22
- * Victrix Gambit Tournament Controller, report is 50 bytes, share button is in byte 32
- * ThrustMaster eSwap PRO Controller Xbox, report is 64 bytes, share button is in byte 46
+ /* Xbox Series X firmware version 5.0, report is 32 bytes, share button is in byte 14
+ * Xbox Series X firmware version 5.1, report is 40 bytes, share button is in byte 14
+ * Xbox Series X firmware version 5.5, report is 44 bytes, share button is in byte 18
+ * Victrix Gambit Tournament Controller, report is 46 bytes, share button is in byte 28
+ * ThrustMaster eSwap PRO Controller Xbox, report is 60 bytes, share button is in byte 42
*/
- if (size < 48) {
+ if (size < 44) {
+ if (ctx->last_state[14] != data[14]) {
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ }
+ } else if (size == 44) {
if (ctx->last_state[18] != data[18]) {
SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[18] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
- } else if (size == 48) {
- if (ctx->last_state[22] != data[22]) {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[22] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
- }
- } else if (size == 50) {
- if (ctx->last_state[32] != data[32]) {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[32] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ } else if (size == 46) {
+ if (ctx->last_state[28] != data[28]) {
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[28] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
- } else if (size == 64) {
- if (ctx->last_state[46] != data[46]) {
- SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[46] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
+ } else if (size == 60) {
+ if (ctx->last_state[42] != data[42]) {
+ SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, (data[42] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
}
}
}
- /* 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
+ /* Xbox One S report is 14 bytes
+ Xbox One Elite Series 1 report is 29 bytes, paddles in data[28], mode in data[28] & 0x10, both modes have mapped paddles by default
Paddle bits:
P3: 0x01 (A) P1: 0x02 (B)
P4: 0x04 (X) P2: 0x08 (Y)
- Xbox One Elite Series 2 4.x firmware report is 38 bytes, paddles in data[18], mode in data[19], mode 0 has no mapped paddles by default
+ Xbox One Elite Series 2 4.x firmware report is 34 bytes, paddles in data[14], mode in data[15], mode 0 has no mapped paddles by default
Paddle bits:
P3: 0x04 (A) P1: 0x01 (B)
P4: 0x08 (X) P2: 0x02 (Y)
- Xbox One Elite Series 2 5.x firmware report is 50 bytes, paddles in data[22], mode in data[23], mode 0 has no mapped paddles by default
+ Xbox One Elite Series 2 5.x firmware report is 46 bytes, paddles in data[18], mode in data[19], mode 0 has no mapped paddles by default
Paddle bits:
P3: 0x04 (A) P1: 0x01 (B)
P4: 0x08 (X) P2: 0x02 (Y)
- Xbox One Elite Series 2 5.17+ firmware report is 51 bytes, paddles in data[18], mode in data[24], mode 0 has no mapped paddles by default
+ Xbox One Elite Series 2 5.17+ firmware report is 47 bytes, paddles in data[14], mode in data[20], mode 0 has no mapped paddles by default
Paddle bits:
P3: 0x04 (A) P1: 0x01 (B)
P4: 0x08 (X) P2: 0x02 (Y)
*/
- if (ctx->has_paddles && !ctx->has_unmapped_state && (size == 33 || size == 38 || size == 50 || size == 51)) {
+ if (ctx->has_paddles && !ctx->has_unmapped_state && (size == 29 || size == 34 || size == 46 || size == 47)) {
int paddle_index;
int button1_bit;
int button2_bit;
@@ -741,42 +735,42 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
int button4_bit;
SDL_bool paddles_mapped;
- if (size == 33) {
+ if (size == 29) {
/* XBox One Elite Series 1 */
- paddle_index = 32;
+ paddle_index = 28;
button1_bit = 0x02;
button2_bit = 0x08;
button3_bit = 0x01;
button4_bit = 0x04;
- /* 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], 2) != 0);
+ /* The mapped controller state is at offset 0, the raw state is at offset 14, compare them to see if the paddles are mapped */
+ paddles_mapped = (SDL_memcmp(&data[0], &data[14], 2) != 0);
- } else if (size == 38) {
+ } else if (size == 34) {
/* XBox One Elite Series 2 */
- paddle_index = 18;
+ paddle_index = 14;
button1_bit = 0x01;
button2_bit = 0x02;
button3_bit = 0x04;
button4_bit = 0x08;
- paddles_mapped = (data[19] != 0);
+ paddles_mapped = (data[15] != 0);
- } else if (size == 50) {
+ } else if (size == 46) {
/* XBox One Elite Series 2 */
- paddle_index = 22;
+ paddle_index = 18;
button1_bit = 0x01;
button2_bit = 0x02;
button3_bit = 0x04;
button4_bit = 0x08;
- paddles_mapped = (data[23] != 0);
- } else /* if (size == 51) */ {
+ paddles_mapped = (data[19] != 0);
+ } else /* if (size == 47) */ {
/* XBox One Elite Series 2 */
- paddle_index = 18;
+ paddle_index = 14;
button1_bit = 0x01;
button2_bit = 0x02;
button3_bit = 0x04;
button4_bit = 0x08;
- paddles_mapped = (data[24] != 0);
+ paddles_mapped = (data[20] != 0);
}
#ifdef DEBUG_XBOX_PROTOCOL
SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s\n",
@@ -802,17 +796,17 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
}
}
- axis = ((int)SDL_SwapLE16(*(Sint16 *)(&data[6])) * 64) - 32768;
+ axis = ((int)SDL_SwapLE16(*(Sint16 *)(&data[2])) * 64) - 32768;
if (axis == 32704) {
axis = 32767;
}
- if (axis == -32768 && size == 30 && (data[22] & 0x80)) {
+ if (axis == -32768 && size == 26 && (data[18] & 0x80)) {
axis = 32767;
}
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
- axis = ((int)SDL_SwapLE16(*(Sint16 *)(&data[8])) * 64) - 32768;
- if (axis == -32768 && size == 30 && (data[22] & 0x40)) {
+ axis = ((int)SDL_SwapLE16(*(Sint16 *)(&data[4])) * 64) - 32768;
+ if (axis == -32768 && size == 26 && (data[18] & 0x40)) {
axis = 32767;
}
if (axis == 32704) {
@@ -820,13 +814,13 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
}
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
- axis = SDL_SwapLE16(*(Sint16 *)(&data[10]));
+ axis = SDL_SwapLE16(*(Sint16 *)(&data[6]));
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
- axis = SDL_SwapLE16(*(Sint16 *)(&data[12]));
+ axis = SDL_SwapLE16(*(Sint16 *)(&data[8]));
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~axis);
- axis = SDL_SwapLE16(*(Sint16 *)(&data[14]));
+ axis = SDL_SwapLE16(*(Sint16 *)(&data[10]));
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
- axis = SDL_SwapLE16(*(Sint16 *)(&data[16]));
+ axis = SDL_SwapLE16(*(Sint16 *)(&data[12]));
SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
@@ -835,7 +829,7 @@ static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_D
ctx->has_unmapped_state = SDL_FALSE;
}
-static void HIDAPI_DriverXboxOne_HandleStatusPacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
+static void HIDAPI_DriverXboxOne_HandleStatusPacket(SDL_DriverXboxOne_Contex
(Patch may be truncated, please check the link at the top of this post.)