src/core/windows/SDL_xinput.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 May 2015 06:27:46 -0700
changeset 9619 b94b6d0bff0f
parent 8973 47fb6a7e7f68
child 9865 c1941443f59a
permissions -rw-r--r--
Updated the copyright year to 2015
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 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_assert.h"
    24 #include "SDL_xinput.h"
    25 
    26 
    27 #ifdef HAVE_XINPUT_H
    28 
    29 XInputGetState_t SDL_XInputGetState = NULL;
    30 XInputSetState_t SDL_XInputSetState = NULL;
    31 XInputGetCapabilities_t SDL_XInputGetCapabilities = 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 
    59     /* XInput 1.4 ships with Windows 8 and 8.1: */
    60     SDL_XInputVersion = (1 << 16) | 4;
    61 
    62     return 0;
    63 }
    64 
    65 void
    66 WIN_UnloadXInputDLL(void)
    67 {
    68 }
    69 
    70 #else /* !__WINRT__ */
    71 
    72 int
    73 WIN_LoadXInputDLL(void)
    74 {
    75     DWORD version = 0;
    76 
    77     if (s_pXInputDLL) {
    78         SDL_assert(s_XInputDLLRefCount > 0);
    79         s_XInputDLLRefCount++;
    80         return 0;  /* already loaded */
    81     }
    82 
    83     version = (1 << 16) | 4;
    84     s_pXInputDLL = LoadLibrary(L"XInput1_4.dll");  /* 1.4 Ships with Windows 8. */
    85     if (!s_pXInputDLL) {
    86         version = (1 << 16) | 3;
    87         s_pXInputDLL = LoadLibrary(L"XInput1_3.dll");  /* 1.3 Ships with Vista and Win7, can be installed as a redistributable component. */
    88     }
    89     if (!s_pXInputDLL) {
    90         s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
    91     }
    92     if (!s_pXInputDLL) {
    93         return -1;
    94     }
    95 
    96     SDL_assert(s_XInputDLLRefCount == 0);
    97     SDL_XInputVersion = version;
    98     s_XInputDLLRefCount = 1;
    99 
   100     /* 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... */
   101     SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
   102     SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
   103     SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
   104     if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
   105         WIN_UnloadXInputDLL();
   106         return -1;
   107     }
   108 
   109     return 0;
   110 }
   111 
   112 void
   113 WIN_UnloadXInputDLL(void)
   114 {
   115     if (s_pXInputDLL) {
   116         SDL_assert(s_XInputDLLRefCount > 0);
   117         if (--s_XInputDLLRefCount == 0) {
   118             FreeLibrary(s_pXInputDLL);
   119             s_pXInputDLL = NULL;
   120         }
   121     } else {
   122         SDL_assert(s_XInputDLLRefCount == 0);
   123     }
   124 }
   125 
   126 #endif /* __WINRT__ */
   127 #endif /* HAVE_XINPUT_H */
   128 
   129 /* vi: set ts=4 sw=4 expandtab: */