Use the HIDAPI driver for Xbox controllers on Windows, and determine the XInput mapping at runtime for extended functionality like rumble and guide button.
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
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360.c b/src/joystick/hidapi/SDL_hidapi_xbox360.c
index cbb43a2..fbd9f19 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c
@@ -34,23 +34,86 @@
#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
+#ifdef __WIN32__
+#include "../../core/windows/SDL_xinput.h"
+#endif
+
#define USB_PACKET_LENGTH 64
typedef struct {
Uint8 last_state[USB_PACKET_LENGTH];
Uint32 rumble_expiration;
+#ifdef __WIN32__
+ SDL_bool xinput_enabled;
+ Uint8 xinput_slot;
+#endif
} SDL_DriverXbox360_Context;
+#ifdef __WIN32__
+static Uint8 xinput_slots;
+
+static void
+HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
+{
+ if (xinput_slot != XUSER_INDEX_ANY) {
+ xinput_slots |= (0x01 << xinput_slot);
+ }
+}
+
+static void
+HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
+{
+ if (xinput_slot != XUSER_INDEX_ANY) {
+ xinput_slots &= ~(0x01 << xinput_slot);
+ }
+}
+
+static SDL_bool
+HIDAPI_DriverXbox360_MissingXInputSlot()
+{
+ return xinput_slots != 0x0F;
+}
+
+static Uint8
+HIDAPI_DriverXbox360_GuessXInputSlot(WORD wButtons)
+{
+ DWORD user_index;
+ int match_count;
+ Uint8 match_slot;
+
+ if (!XINPUTGETSTATE) {
+ return XUSER_INDEX_ANY;
+ }
+
+ match_count = 0;
+ for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
+ XINPUT_STATE_EX xinput_state;
+
+ if (XINPUTGETSTATE(user_index, &xinput_state) == ERROR_SUCCESS) {
+ if (xinput_state.Gamepad.wButtons == wButtons) {
+ ++match_count;
+ match_slot = (Uint8)user_index;
+ }
+ }
+ }
+ if (match_count == 1) {
+ return match_slot;
+ }
+ return XUSER_INDEX_ANY;
+}
+
+#endif /* __WIN32__ */
+
static SDL_bool
HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, Uint16 usage_page, Uint16 usage)
{
#if defined(__MACOSX__) || defined(__WIN32__)
- if (vendor_id == 0x045e && product_id == 0x028e && version == 1) {
- /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
- return SDL_FALSE;
- }
+ if (vendor_id == 0x045e && product_id == 0x028e && version == 1) {
+ /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
+ return SDL_FALSE;
+ }
return SDL_IsJoystickXbox360(vendor_id, product_id) || SDL_IsJoystickXboxOne(vendor_id, product_id);
#else
return SDL_IsJoystickXbox360(vendor_id, product_id);
@@ -68,9 +131,9 @@ static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
const Uint8 led_packet[] = { 0x01, 0x03, (2 + slot) };
if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
- return SDL_FALSE;
- }
- return SDL_TRUE;
+ return SDL_FALSE;
+ }
+ return SDL_TRUE;
}
static SDL_bool
@@ -83,10 +146,17 @@ HIDAPI_DriverXbox360_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor
SDL_OutOfMemory();
return SDL_FALSE;
}
+#ifdef __WIN32__
+ ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
+ if (ctx->xinput_enabled && WIN_LoadXInputDLL() < 0) {
+ ctx->xinput_enabled = SDL_FALSE;
+ }
+ ctx->xinput_slot = XUSER_INDEX_ANY;
+#endif
*context = ctx;
/* Set the controller LED */
- SetSlotLED(dev, (joystick->instance_id % 4));
+ SetSlotLED(dev, (joystick->instance_id % 4));
/* Initialize the joystick capabilities */
joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
@@ -100,6 +170,21 @@ static int
HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
+#ifdef __WIN32__
+ if (ctx->xinput_slot != XUSER_INDEX_ANY) {
+ XINPUT_VIBRATION XVibration;
+
+ if (!XINPUTSETSTATE) {
+ return SDL_Unsupported();
+ }
+
+ XVibration.wLeftMotorSpeed = low_frequency_rumble;
+ XVibration.wRightMotorSpeed = high_frequency_rumble;
+ if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) != ERROR_SUCCESS) {
+ return SDL_SetError("XInputSetState() failed");
+ }
+ }
+#else
#ifdef __MACOSX__
/* On Mac OS X the 360Controller driver uses this short report,
and we need to prefix it with a magic token so hidapi passes it through untouched
@@ -118,6 +203,7 @@ HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *conte
if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
+#endif /* __WIN32__ */
if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
@@ -204,16 +290,50 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
axis = (int)*(Uint16*)(&data[6]) - 0x8000;
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
- axis = (data[9] * 257) - 32768;
- if (data[9] < 0x80) {
- axis = -axis * 2 - 32769;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
- } else if (data[9] > 0x80) {
- axis = axis * 2 - 32767;
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
+ if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
+ WORD wButtons = 0;
+
+ if (data[10] & 0x01) {
+ wButtons |= XINPUT_GAMEPAD_A;
+ }
+ if (data[10] & 0x02) {
+ wButtons |= XINPUT_GAMEPAD_B;
+ }
+ if (data[10] & 0x04) {
+ wButtons |= XINPUT_GAMEPAD_X;
+ }
+ if (data[10] & 0x08) {
+ wButtons |= XINPUT_GAMEPAD_Y;
+ }
+ if (wButtons != 0) {
+ Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
+ if (xinput_slot != XUSER_INDEX_ANY) {
+ HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
+ ctx->xinput_slot = xinput_slot;
+ }
+ }
+ }
+
+ if (ctx->xinput_slot == XUSER_INDEX_ANY) {
+ axis = (data[9] * 257) - 32768;
+ if (data[9] < 0x80) {
+ axis = -axis * 2 - 32769;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
+ } else if (data[9] > 0x80) {
+ axis = axis * 2 - 32767;
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
+ } else {
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
+ }
} else {
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
- SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
+ XINPUT_STATE_EX xinput_state;
+
+ if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
+ SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
+ SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
+ }
}
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
@@ -225,9 +345,9 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
{
Sint16 axis;
#ifdef __MACOSX__
- const SDL_bool invert_y_axes = SDL_FALSE;
+ const SDL_bool invert_y_axes = SDL_FALSE;
#else
- const SDL_bool invert_y_axes = SDL_TRUE;
+ const SDL_bool invert_y_axes = SDL_TRUE;
#endif
if (ctx->last_state[2] != data[2]) {
@@ -258,16 +378,16 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
axis = *(Sint16*)(&data[6]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
axis = *(Sint16*)(&data[8]);
- if (invert_y_axes) {
- axis = ~axis;
- }
+ if (invert_y_axes) {
+ axis = ~axis;
+ }
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
axis = *(Sint16*)(&data[10]);
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
axis = *(Sint16*)(&data[12]);
- if (invert_y_axes) {
- axis = ~axis;
- }
+ if (invert_y_axes) {
+ axis = ~axis;
+ }
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
@@ -305,12 +425,20 @@ HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *conte
}
}
- return (size >= 0);
+ return (size >= 0);
}
static void
HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
{
+#ifdef __WIN32__
+ SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
+
+ if (ctx->xinput_enabled) {
+ HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
+ WIN_UnloadXInputDLL();
+ }
+#endif
SDL_free(context);
}
diff --git a/src/joystick/hidapi/SDL_hidapijoystick_c.h b/src/joystick/hidapi/SDL_hidapijoystick_c.h
index 838fcc7..cbfad31 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick_c.h
+++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h
@@ -32,8 +32,7 @@
#define SDL_JOYSTICK_HIDAPI_XBOXONE
#ifdef __WINDOWS__
-/* On Windows, Xbox controllers are handled by the XInput driver */
-//#undef SDL_JOYSTICK_HIDAPI_XBOX360
+/* On Windows, Xbox One controllers are handled by the Xbox 360 driver */
#undef SDL_JOYSTICK_HIDAPI_XBOXONE
#endif
diff --git a/src/joystick/windows/SDL_xinputjoystick.c b/src/joystick/windows/SDL_xinputjoystick.c
index 5a0f466..d81436f 100644
--- a/src/joystick/windows/SDL_xinputjoystick.c
+++ b/src/joystick/windows/SDL_xinputjoystick.c
@@ -259,7 +259,6 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
return;
}
-#if 0 /* There's no controller that's supported by both XInput and HIDAPI */
#ifdef SDL_JOYSTICK_HIDAPI
if (HIDAPI_IsDevicePresent(vendor, product, version)) {
/* The HIDAPI driver is taking care of this device */
@@ -267,7 +266,6 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
return;
}
#endif
-#endif
WINDOWS_AddJoystickDevice(pNewJoystick);
}