src/joystick/windows/SDL_dxjoystick.c
changeset 6991 ff49588e3ddb
parent 6990 2514368c2aaf
child 6993 33f1e384ed56
     1.1 --- a/src/joystick/windows/SDL_dxjoystick.c	Sun Mar 10 13:05:47 2013 -0400
     1.2 +++ b/src/joystick/windows/SDL_dxjoystick.c	Sun Mar 10 13:28:39 2013 -0400
     1.3 @@ -75,6 +75,69 @@
     1.4  static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
     1.5  static SDL_bool s_bXInputEnabled = SDL_TRUE;
     1.6  
     1.7 +XInputGetState_t SDL_XInputGetState = NULL;
     1.8 +XInputSetState_t SDL_XInputSetState = NULL;
     1.9 +XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
    1.10 +DWORD SDL_XInputVersion = 0;
    1.11 +
    1.12 +static HANDLE s_pXInputDLL = 0;
    1.13 +static int s_XInputDLLRefCount = 0;
    1.14 +
    1.15 +int
    1.16 +WIN_LoadXInputDLL(void)
    1.17 +{
    1.18 +    DWORD version = 0;
    1.19 +
    1.20 +    if (s_pXInputDLL) {
    1.21 +        SDL_assert(s_XInputDLLRefCount > 0);
    1.22 +        s_XInputDLLRefCount++;
    1.23 +        return 0;  /* already loaded */
    1.24 +    }
    1.25 +
    1.26 +    version = (1 << 16) | 4;
    1.27 +    s_pXInputDLL = LoadLibrary( L"XInput1_4.dll" );  // 1.4 Ships with Windows 8.
    1.28 +    if (!s_pXInputDLL) {
    1.29 +        version = (1 << 16) | 3;
    1.30 +        s_pXInputDLL = LoadLibrary( L"XInput1_3.dll" );  // 1.3 Ships with Vista and Win7, can be installed as a restributable component.
    1.31 +    }
    1.32 +    if (!s_pXInputDLL) {
    1.33 +        s_pXInputDLL = LoadLibrary( L"bin\\XInput1_3.dll" );
    1.34 +    }
    1.35 +    if (!s_pXInputDLL) {
    1.36 +        return -1;
    1.37 +    }
    1.38 +
    1.39 +    SDL_assert(s_XInputDLLRefCount == 0);
    1.40 +    SDL_XInputVersion = version;
    1.41 +    s_XInputDLLRefCount = 1;
    1.42 +
    1.43 +    /* 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think... */
    1.44 +    SDL_XInputGetState = (XInputGetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, (LPCSTR)100 );
    1.45 +    SDL_XInputSetState = (XInputSetState_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputSetState" );
    1.46 +    SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetCapabilities" );
    1.47 +    if ( !SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities ) {
    1.48 +        WIN_UnloadXInputDLL();
    1.49 +        return -1;
    1.50 +    }
    1.51 +
    1.52 +    return 0;
    1.53 +}
    1.54 +
    1.55 +void
    1.56 +WIN_UnloadXInputDLL(void)
    1.57 +{
    1.58 +    if ( s_pXInputDLL ) {
    1.59 +        SDL_assert(s_XInputDLLRefCount > 0);
    1.60 +        if (--s_XInputDLLRefCount == 0) {
    1.61 +            FreeLibrary( s_pXInputDLL );
    1.62 +            s_pXInputDLL = NULL;
    1.63 +        }
    1.64 +    } else {
    1.65 +        SDL_assert(s_XInputDLLRefCount == 0);
    1.66 +    }
    1.67 +}
    1.68 +
    1.69 +
    1.70  extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
    1.71                                         LPDIRECTINPUT * ppDI,
    1.72                                         LPUNKNOWN punkOuter);