sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_xinput.c (4860B)


      1 /*
      2   Simple DirectMedia Layer
      3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
      4 
      5   This software is provided 'as-is', without any express or implied
      6   warranty.  In no event will the authors be held liable for any damages
      7   arising from the use of this software.
      8 
      9   Permission is granted to anyone to use this software for any purpose,
     10   including commercial applications, and to alter it and redistribute it
     11   freely, subject to the following restrictions:
     12 
     13   1. The origin of this software must not be misrepresented; you must not
     14      claim that you wrote the original software. If you use this software
     15      in a product, an acknowledgment in the product documentation would be
     16      appreciated but is not required.
     17   2. Altered source versions must be plainly marked as such, and must not be
     18      misrepresented as being the original software.
     19   3. This notice may not be removed or altered from any source distribution.
     20 */
     21 #include "../../SDL_internal.h"
     22 
     23 #include "SDL_xinput.h"
     24 
     25 
     26 #ifdef HAVE_XINPUT_H
     27 
     28 XInputGetState_t SDL_XInputGetState = NULL;
     29 XInputSetState_t SDL_XInputSetState = NULL;
     30 XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
     31 XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
     32 DWORD SDL_XInputVersion = 0;
     33 
     34 static HANDLE s_pXInputDLL = 0;
     35 static int s_XInputDLLRefCount = 0;
     36 
     37 
     38 #ifdef __WINRT__
     39 
     40 int
     41 WIN_LoadXInputDLL(void)
     42 {
     43     /* Getting handles to system dlls (via LoadLibrary and its variants) is not
     44      * supported on WinRT, thus, pointers to XInput's functions can't be
     45      * retrieved via GetProcAddress.
     46      *
     47      * When on WinRT, assume that XInput is already loaded, and directly map
     48      * its XInput.h-declared functions to the SDL_XInput* set of function
     49      * pointers.
     50      *
     51      * Side-note: XInputGetStateEx is not available for use in WinRT.
     52      * This seems to mean that support for the guide button is not available
     53      * in WinRT, unfortunately.
     54      */
     55     SDL_XInputGetState = (XInputGetState_t)XInputGetState;
     56     SDL_XInputSetState = (XInputSetState_t)XInputSetState;
     57     SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
     58     SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
     59 
     60     /* XInput 1.4 ships with Windows 8 and 8.1: */
     61     SDL_XInputVersion = (1 << 16) | 4;
     62 
     63     return 0;
     64 }
     65 
     66 void
     67 WIN_UnloadXInputDLL(void)
     68 {
     69 }
     70 
     71 #else /* !__WINRT__ */
     72 
     73 int
     74 WIN_LoadXInputDLL(void)
     75 {
     76     DWORD version = 0;
     77 
     78     if (s_pXInputDLL) {
     79         SDL_assert(s_XInputDLLRefCount > 0);
     80         s_XInputDLLRefCount++;
     81         return 0;  /* already loaded */
     82     }
     83 
     84     /* NOTE: Don't load XinputUap.dll
     85      * This is XInput emulation over Windows.Gaming.Input, and has all the
     86      * limitations of that API (no devices at startup, no background input, etc.)
     87      */
     88     version = (1 << 16) | 4;
     89     s_pXInputDLL = LoadLibrary(L"XInput1_4.dll");  /* 1.4 Ships with Windows 8. */
     90     if (!s_pXInputDLL) {
     91         version = (1 << 16) | 3;
     92         s_pXInputDLL = LoadLibrary(L"XInput1_3.dll");  /* 1.3 can be installed as a redistributable component. */
     93     }
     94     if (!s_pXInputDLL) {
     95         s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
     96     }
     97     if (!s_pXInputDLL) {
     98         /* "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.)  */
     99         s_pXInputDLL = LoadLibrary(L"XInput9_1_0.dll");
    100     }
    101     if (!s_pXInputDLL) {
    102         return -1;
    103     }
    104 
    105     SDL_assert(s_XInputDLLRefCount == 0);
    106     SDL_XInputVersion = version;
    107     s_XInputDLLRefCount = 1;
    108 
    109     /* 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... */
    110     SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
    111     if (!SDL_XInputGetState) {
    112         SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetState");
    113     }
    114     SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
    115     SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
    116     SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
    117     if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
    118         WIN_UnloadXInputDLL();
    119         return -1;
    120     }
    121 
    122     return 0;
    123 }
    124 
    125 void
    126 WIN_UnloadXInputDLL(void)
    127 {
    128     if (s_pXInputDLL) {
    129         SDL_assert(s_XInputDLLRefCount > 0);
    130         if (--s_XInputDLLRefCount == 0) {
    131             FreeLibrary(s_pXInputDLL);
    132             s_pXInputDLL = NULL;
    133         }
    134     } else {
    135         SDL_assert(s_XInputDLLRefCount == 0);
    136     }
    137 }
    138 
    139 #endif /* __WINRT__ */
    140 #endif /* HAVE_XINPUT_H */
    141 
    142 /* vi: set ts=4 sw=4 expandtab: */