Added experimental support for Windows.Gaming.Input
authorSam Lantinga <slouken@libsdl.org>
Wed, 19 Sep 2018 10:03:40 -0700
changeset 12196b504b29c3831
parent 12195 367b9a5e9622
child 12197 a72347375b36
Added experimental support for Windows.Gaming.Input
src/joystick/hidapi/SDL_hidapi_xbox360.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Tue Sep 18 11:49:42 2018 -0400
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Wed Sep 19 10:03:40 2018 -0700
     1.3 @@ -35,23 +35,41 @@
     1.4  #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
     1.5  
     1.6  #ifdef __WIN32__
     1.7 +#define SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
     1.8 +/* This requires the Windows 10 SDK to build */
     1.9 +/*#define SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT*/
    1.10 +#endif
    1.11 +
    1.12 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
    1.13  #include "../../core/windows/SDL_xinput.h"
    1.14  #endif
    1.15  
    1.16 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
    1.17 +#include "../../core/windows/SDL_windows.h"
    1.18 +#define COBJMACROS
    1.19 +#include "windows.gaming.input.h"
    1.20 +#endif
    1.21 +
    1.22  #define USB_PACKET_LENGTH   64
    1.23  
    1.24  
    1.25  typedef struct {
    1.26      Uint8 last_state[USB_PACKET_LENGTH];
    1.27      Uint32 rumble_expiration;
    1.28 -#ifdef __WIN32__
    1.29 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
    1.30      SDL_bool xinput_enabled;
    1.31      Uint8 xinput_slot;
    1.32  #endif
    1.33 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
    1.34 +    SDL_bool coinitialized;
    1.35 +    __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
    1.36 +    __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
    1.37 +    struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
    1.38 +#endif
    1.39  } SDL_DriverXbox360_Context;
    1.40  
    1.41  
    1.42 -#ifdef __WIN32__
    1.43 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
    1.44  static Uint8 xinput_slots;
    1.45  
    1.46  static void
    1.47 @@ -104,7 +122,128 @@
    1.48      return XUSER_INDEX_ANY;
    1.49  }
    1.50  
    1.51 -#endif /* __WIN32__ */
    1.52 +#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
    1.53 +
    1.54 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
    1.55 +
    1.56 +static void
    1.57 +HIDAPI_DriverXbox360_InitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
    1.58 +{
    1.59 +    /* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
    1.60 +    if (FAILED(WIN_CoInitialize())) {
    1.61 +        return;
    1.62 +    }
    1.63 +    ctx->coinitialized = SDL_TRUE;
    1.64 +
    1.65 +    {
    1.66 +        static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
    1.67 +        HRESULT hr;
    1.68 +        HMODULE hModule = LoadLibraryA("combase.dll");
    1.69 +        if (hModule != NULL) {
    1.70 +            typedef HRESULT(WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
    1.71 +            typedef HRESULT(WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
    1.72 +
    1.73 +            WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
    1.74 +            RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
    1.75 +            if (WindowsCreateStringFunc && RoGetActivationFactoryFunc) {
    1.76 +                LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
    1.77 +                HSTRING hNamespaceString;
    1.78 +
    1.79 +                hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
    1.80 +                if (SUCCEEDED(hr)) {
    1.81 +                    hr = RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &ctx->gamepad_statics);
    1.82 +                    if (SUCCEEDED(hr)) {
    1.83 +                    }
    1.84 +                }
    1.85 +            }
    1.86 +            FreeLibrary(hModule);
    1.87 +        }
    1.88 +    }
    1.89 +}
    1.90 +
    1.91 +static Uint8
    1.92 +HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(__x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad)
    1.93 +{
    1.94 +    HRESULT hr;
    1.95 +    struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
    1.96 +    Uint8 buttons = 0;
    1.97 +
    1.98 +    hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(gamepad, &state);
    1.99 +    if (SUCCEEDED(hr)) {
   1.100 +        if (state.Buttons & GamepadButtons_A) {
   1.101 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
   1.102 +        }
   1.103 +        if (state.Buttons & GamepadButtons_B) {
   1.104 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
   1.105 +        }
   1.106 +        if (state.Buttons & GamepadButtons_X) {
   1.107 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
   1.108 +        }
   1.109 +        if (state.Buttons & GamepadButtons_Y) {
   1.110 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
   1.111 +        }
   1.112 +    }
   1.113 +    return buttons;
   1.114 +}
   1.115 +
   1.116 +static void
   1.117 +HIDAPI_DriverXbox360_GuessGamepad(SDL_DriverXbox360_Context *ctx, Uint8 buttons)
   1.118 +{
   1.119 +    HRESULT hr;
   1.120 +    __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
   1.121 +
   1.122 +    hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(ctx->gamepad_statics, &gamepads);
   1.123 +    if (SUCCEEDED(hr)) {
   1.124 +        unsigned int i, num_gamepads;
   1.125 +
   1.126 +        hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
   1.127 +        if (SUCCEEDED(hr)) {
   1.128 +            int match_count;
   1.129 +            unsigned int match_slot;
   1.130 +
   1.131 +            match_count = 0;
   1.132 +            for (i = 0; i < num_gamepads; ++i) {
   1.133 +                __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
   1.134 +
   1.135 +                hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
   1.136 +                if (SUCCEEDED(hr)) {
   1.137 +                    Uint8 gamepad_buttons = HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(gamepad);
   1.138 +                    if (buttons == gamepad_buttons) {
   1.139 +                        ++match_count;
   1.140 +                        match_slot = i;
   1.141 +                    }
   1.142 +                    __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
   1.143 +                }
   1.144 +            }
   1.145 +            if (match_count == 1) {
   1.146 +                hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, match_slot, &ctx->gamepad);
   1.147 +                if (SUCCEEDED(hr)) {
   1.148 +                }
   1.149 +            }
   1.150 +        }
   1.151 +        __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
   1.152 +    }
   1.153 +}
   1.154 +
   1.155 +static void
   1.156 +HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
   1.157 +{
   1.158 +    if (ctx->gamepad_statics) {
   1.159 +        __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(ctx->gamepad_statics);
   1.160 +        ctx->gamepad_statics = NULL;
   1.161 +    }
   1.162 +    if (ctx->gamepad) {
   1.163 +        __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(ctx->gamepad);
   1.164 +        ctx->gamepad = NULL;
   1.165 +    }
   1.166 +
   1.167 +    if (ctx->coinitialized) {
   1.168 +        WIN_CoUninitialize();
   1.169 +        ctx->coinitialized = SDL_FALSE;
   1.170 +    }
   1.171 +}
   1.172 +
   1.173 +#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
   1.174  
   1.175  static SDL_bool
   1.176  HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number)
   1.177 @@ -146,13 +285,16 @@
   1.178          SDL_OutOfMemory();
   1.179          return SDL_FALSE;
   1.180      }
   1.181 -#ifdef __WIN32__
   1.182 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
   1.183      ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
   1.184      if (ctx->xinput_enabled && WIN_LoadXInputDLL() < 0) {
   1.185          ctx->xinput_enabled = SDL_FALSE;
   1.186      }
   1.187      ctx->xinput_slot = XUSER_INDEX_ANY;
   1.188  #endif
   1.189 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
   1.190 +    HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
   1.191 +#endif
   1.192      *context = ctx;
   1.193  
   1.194      /* Set the controller LED */
   1.195 @@ -170,8 +312,25 @@
   1.196  HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
   1.197  {
   1.198      SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
   1.199 +
   1.200  #ifdef __WIN32__
   1.201 -    if (ctx->xinput_slot != XUSER_INDEX_ANY) {
   1.202 +    SDL_bool rumbled = SDL_FALSE;
   1.203 +
   1.204 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
   1.205 +    if (!rumbled && ctx->gamepad) {
   1.206 +        HRESULT hr;
   1.207 +
   1.208 +        ctx->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
   1.209 +        ctx->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
   1.210 +        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(ctx->gamepad, ctx->vibration);
   1.211 +        if (SUCCEEDED(hr)) {
   1.212 +            rumbled = SDL_TRUE;
   1.213 +        }
   1.214 +    }
   1.215 +#endif
   1.216 +
   1.217 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
   1.218 +    if (!rumbled && ctx->xinput_slot != XUSER_INDEX_ANY) {
   1.219          XINPUT_VIBRATION XVibration;
   1.220  
   1.221          if (!XINPUTSETSTATE) {
   1.222 @@ -180,11 +339,16 @@
   1.223  
   1.224          XVibration.wLeftMotorSpeed = low_frequency_rumble;
   1.225          XVibration.wRightMotorSpeed = high_frequency_rumble;
   1.226 -        if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) != ERROR_SUCCESS) {
   1.227 +        if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
   1.228 +            rumbled = SDL_TRUE;
   1.229 +        } else {
   1.230              return SDL_SetError("XInputSetState() failed");
   1.231          }
   1.232      }
   1.233 -#else
   1.234 +#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
   1.235 +
   1.236 +#else /* !__WIN32__ */
   1.237 +
   1.238  #ifdef __MACOSX__
   1.239      /* On Mac OS X the 360Controller driver uses this short report,
   1.240         and we need to prefix it with a magic token so hidapi passes it through untouched
   1.241 @@ -217,11 +381,14 @@
   1.242   /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
   1.243      however with this interface there is no rumble support, no guide button,
   1.244      and the left and right triggers are tied together as a single axis.
   1.245 +
   1.246 +    We use XInput and Windows.Gaming.Input to make up for these shortcomings.
   1.247    */
   1.248  static void
   1.249  HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
   1.250  {
   1.251      Sint16 axis;
   1.252 +    SDL_bool has_trigger_data = SDL_FALSE;
   1.253  
   1.254      if (ctx->last_state[10] != data[10]) {
   1.255          SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
   1.256 @@ -290,31 +457,82 @@
   1.257      axis = (int)*(Uint16*)(&data[6]) - 0x8000;
   1.258      SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
   1.259  
   1.260 -    if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
   1.261 -        WORD wButtons = 0;
   1.262 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
   1.263 +    if (ctx->gamepad_statics && !ctx->gamepad) {
   1.264 +        Uint8 buttons = 0;
   1.265  
   1.266          if (data[10] & 0x01) {
   1.267 -            wButtons |= XINPUT_GAMEPAD_A;
   1.268 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
   1.269          }
   1.270          if (data[10] & 0x02) {
   1.271 -            wButtons |= XINPUT_GAMEPAD_B;
   1.272 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
   1.273          }
   1.274          if (data[10] & 0x04) {
   1.275 -            wButtons |= XINPUT_GAMEPAD_X;
   1.276 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
   1.277          }
   1.278          if (data[10] & 0x08) {
   1.279 -            wButtons |= XINPUT_GAMEPAD_Y;
   1.280 +            buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
   1.281          }
   1.282 -        if (wButtons != 0) {
   1.283 -            Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
   1.284 -            if (xinput_slot != XUSER_INDEX_ANY) {
   1.285 -                HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
   1.286 -                ctx->xinput_slot = xinput_slot;
   1.287 +        if (buttons != 0) {
   1.288 +            HIDAPI_DriverXbox360_GuessGamepad(ctx, buttons);
   1.289 +        }
   1.290 +    }
   1.291 +
   1.292 +    if (ctx->gamepad) {
   1.293 +        HRESULT hr;
   1.294 +        struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
   1.295 +        
   1.296 +        hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(ctx->gamepad, &state);
   1.297 +        if (SUCCEEDED(hr)) {
   1.298 +            SDL_Log("Windows.Gaming.Input buttons: 0x%x\n", state.Buttons);
   1.299 +            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (state.Buttons & 0x40000000) ? SDL_PRESSED : SDL_RELEASED);
   1.300 +            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)(state.LeftTrigger * SDL_MAX_UINT16)) - 32768);
   1.301 +            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)(state.RightTrigger * SDL_MAX_UINT16)) - 32768);
   1.302 +            has_trigger_data = SDL_TRUE;
   1.303 +        }
   1.304 +    }
   1.305 +#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
   1.306 +
   1.307 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
   1.308 +    if (ctx->xinput_enabled) {
   1.309 +        if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
   1.310 +            WORD wButtons = 0;
   1.311 +
   1.312 +            if (data[10] & 0x01) {
   1.313 +                wButtons |= XINPUT_GAMEPAD_A;
   1.314 +            }
   1.315 +            if (data[10] & 0x02) {
   1.316 +                wButtons |= XINPUT_GAMEPAD_B;
   1.317 +            }
   1.318 +            if (data[10] & 0x04) {
   1.319 +                wButtons |= XINPUT_GAMEPAD_X;
   1.320 +            }
   1.321 +            if (data[10] & 0x08) {
   1.322 +                wButtons |= XINPUT_GAMEPAD_Y;
   1.323 +            }
   1.324 +            if (wButtons != 0) {
   1.325 +                Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
   1.326 +                if (xinput_slot != XUSER_INDEX_ANY) {
   1.327 +                    HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
   1.328 +                    ctx->xinput_slot = xinput_slot;
   1.329 +                }
   1.330 +            }
   1.331 +        }
   1.332 +
   1.333 +        if (!has_trigger_data && ctx->xinput_slot != XUSER_INDEX_ANY) {
   1.334 +            XINPUT_STATE_EX xinput_state;
   1.335 +
   1.336 +            if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
   1.337 +                SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
   1.338 +                SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
   1.339 +                SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
   1.340 +                has_trigger_data = SDL_TRUE;
   1.341              }
   1.342          }
   1.343      }
   1.344 +#endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
   1.345  
   1.346 -    if (ctx->xinput_slot == XUSER_INDEX_ANY) {
   1.347 +    if (!has_trigger_data) {
   1.348          axis = (data[9] * 257) - 32768;
   1.349          if (data[9] < 0x80) {
   1.350              axis = -axis * 2 - 32769;
   1.351 @@ -326,14 +544,6 @@
   1.352              SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
   1.353              SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
   1.354          }
   1.355 -    } else {
   1.356 -        XINPUT_STATE_EX xinput_state;
   1.357 -
   1.358 -        if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
   1.359 -            SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
   1.360 -            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
   1.361 -            SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
   1.362 -        }
   1.363      }
   1.364  
   1.365      SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
   1.366 @@ -542,14 +752,17 @@
   1.367  static void
   1.368  HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
   1.369  {
   1.370 -#ifdef __WIN32__
   1.371      SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
   1.372  
   1.373 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
   1.374      if (ctx->xinput_enabled) {
   1.375          HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
   1.376          WIN_UnloadXInputDLL();
   1.377      }
   1.378  #endif
   1.379 +#ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
   1.380 +    HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
   1.381 +#endif
   1.382      SDL_free(context);
   1.383  }
   1.384