src/core/windows/SDL_xinput.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 30 Sep 2015 15:39:30 -0700
changeset 9884 26b595dea221
parent 9865 c1941443f59a
child 9998 f67cf37e9cd4
permissions -rw-r--r--
SDL - added new SDL_JoystickCurrentPowerLevel() API that returns the battery level of the selected joystick. Currently only implemented for XInput devices, other platforms are a TODO.

CR: Sam
slouken@8972
     1
/*
slouken@8972
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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@8972
    85
    version = (1 << 16) | 4;
slouken@8972
    86
    s_pXInputDLL = LoadLibrary(L"XInput1_4.dll");  /* 1.4 Ships with Windows 8. */
slouken@8972
    87
    if (!s_pXInputDLL) {
slouken@8972
    88
        version = (1 << 16) | 3;
slime73@9865
    89
        s_pXInputDLL = LoadLibrary(L"XInput1_3.dll");  /* 1.3 can be installed as a redistributable component. */
slouken@8972
    90
    }
slouken@8972
    91
    if (!s_pXInputDLL) {
slouken@8972
    92
        s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
slouken@8972
    93
    }
slouken@8972
    94
    if (!s_pXInputDLL) {
slime73@9865
    95
        /* "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.)  */
slime73@9865
    96
        s_pXInputDLL = LoadLibrary(L"XInput9_1_0.dll");
slime73@9865
    97
    }
slime73@9865
    98
    if (!s_pXInputDLL) {
slouken@8972
    99
        return -1;
slouken@8972
   100
    }
slouken@8972
   101
slouken@8972
   102
    SDL_assert(s_XInputDLLRefCount == 0);
slouken@8972
   103
    SDL_XInputVersion = version;
slouken@8972
   104
    s_XInputDLLRefCount = 1;
slouken@8972
   105
slouken@8972
   106
    /* 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... */
slouken@8972
   107
    SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
slime73@9865
   108
    if (!SDL_XInputGetState) {
slime73@9865
   109
        SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetState");
slime73@9865
   110
    }
slouken@8972
   111
    SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
slouken@8972
   112
    SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
slouken@9884
   113
    SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
slouken@8972
   114
    if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
slouken@8972
   115
        WIN_UnloadXInputDLL();
slouken@8972
   116
        return -1;
slouken@8972
   117
    }
slouken@8972
   118
slouken@8972
   119
    return 0;
slouken@8972
   120
}
slouken@8972
   121
slouken@8972
   122
void
slouken@8972
   123
WIN_UnloadXInputDLL(void)
slouken@8972
   124
{
slouken@8972
   125
    if (s_pXInputDLL) {
slouken@8972
   126
        SDL_assert(s_XInputDLLRefCount > 0);
slouken@8972
   127
        if (--s_XInputDLLRefCount == 0) {
slouken@8972
   128
            FreeLibrary(s_pXInputDLL);
slouken@8972
   129
            s_pXInputDLL = NULL;
slouken@8972
   130
        }
slouken@8972
   131
    } else {
slouken@8972
   132
        SDL_assert(s_XInputDLLRefCount == 0);
slouken@8972
   133
    }
slouken@8972
   134
}
slouken@8972
   135
dludwig@8973
   136
#endif /* __WINRT__ */
slouken@8972
   137
#endif /* HAVE_XINPUT_H */
slouken@8972
   138
slouken@8972
   139
/* vi: set ts=4 sw=4 expandtab: */