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