mirror of https://github.com/encounter/SDL.git
Use the HIDAPI driver for Xbox controllers on Windows, and determine the XInput mapping at runtime for extended functionality like rumble and guide button.
This commit is contained in:
parent
3f5ff751fe
commit
a2add1f683
|
@ -34,15 +34,78 @@
|
|||
|
||||
#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)
|
||||
{
|
||||
|
@ -83,6 +146,13 @@ 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 */
|
||||
|
@ -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,6 +290,31 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
|
|||
axis = (int)*(Uint16*)(&data[6]) - 0x8000;
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, 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;
|
||||
|
@ -215,6 +326,15 @@ HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev,
|
|||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
|
||||
}
|
||||
} else {
|
||||
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)));
|
||||
}
|
||||
|
@ -311,6 +431,14 @@ HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *conte
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -259,14 +259,12 @@ 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 */
|
||||
SDL_free(pNewJoystick);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
WINDOWS_AddJoystickDevice(pNewJoystick);
|
||||
|
|
Loading…
Reference in New Issue