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_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 XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
33 DWORD SDL_XInputVersion = 0;
34
35 static HANDLE s_pXInputDLL = 0;
36 static int s_XInputDLLRefCount = 0;
37
38
39 #ifdef __WINRT__
40
41 int
WIN_LoadXInputDLL(void)42 WIN_LoadXInputDLL(void)
43 {
44 /* Getting handles to system dlls (via LoadLibrary and its variants) is not
45 * supported on WinRT, thus, pointers to XInput's functions can't be
46 * retrieved via GetProcAddress.
47 *
48 * When on WinRT, assume that XInput is already loaded, and directly map
49 * its XInput.h-declared functions to the SDL_XInput* set of function
50 * pointers.
51 *
52 * Side-note: XInputGetStateEx is not available for use in WinRT.
53 * This seems to mean that support for the guide button is not available
54 * in WinRT, unfortunately.
55 */
56 SDL_XInputGetState = (XInputGetState_t)XInputGetState;
57 SDL_XInputSetState = (XInputSetState_t)XInputSetState;
58 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
59 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
60
61 /* XInput 1.4 ships with Windows 8 and 8.1: */
62 SDL_XInputVersion = (1 << 16) | 4;
63
64 return 0;
65 }
66
67 void
WIN_UnloadXInputDLL(void)68 WIN_UnloadXInputDLL(void)
69 {
70 }
71
72 #else /* !__WINRT__ */
73
74 int
WIN_LoadXInputDLL(void)75 WIN_LoadXInputDLL(void)
76 {
77 DWORD version = 0;
78
79 if (s_pXInputDLL) {
80 SDL_assert(s_XInputDLLRefCount > 0);
81 s_XInputDLLRefCount++;
82 return 0; /* already loaded */
83 }
84
85 /* NOTE: Don't load XinputUap.dll
86 * This is XInput emulation over Windows.Gaming.Input, and has all the
87 * limitations of that API (no devices at startup, no background input, etc.)
88 */
89 version = (1 << 16) | 4;
90 s_pXInputDLL = LoadLibrary(L"XInput1_4.dll"); /* 1.4 Ships with Windows 8. */
91 if (!s_pXInputDLL) {
92 version = (1 << 16) | 3;
93 s_pXInputDLL = LoadLibrary(L"XInput1_3.dll"); /* 1.3 can be installed as a redistributable component. */
94 }
95 if (!s_pXInputDLL) {
96 s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
97 }
98 if (!s_pXInputDLL) {
99 /* "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.) */
100 s_pXInputDLL = LoadLibrary(L"XInput9_1_0.dll");
101 }
102 if (!s_pXInputDLL) {
103 return -1;
104 }
105
106 SDL_assert(s_XInputDLLRefCount == 0);
107 SDL_XInputVersion = version;
108 s_XInputDLLRefCount = 1;
109
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... */
111 SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
112 if (!SDL_XInputGetState) {
113 SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetState");
114 }
115 SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
116 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
117 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
118 if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
119 WIN_UnloadXInputDLL();
120 return -1;
121 }
122
123 return 0;
124 }
125
126 void
WIN_UnloadXInputDLL(void)127 WIN_UnloadXInputDLL(void)
128 {
129 if (s_pXInputDLL) {
130 SDL_assert(s_XInputDLLRefCount > 0);
131 if (--s_XInputDLLRefCount == 0) {
132 FreeLibrary(s_pXInputDLL);
133 s_pXInputDLL = NULL;
134 }
135 } else {
136 SDL_assert(s_XInputDLLRefCount == 0);
137 }
138 }
139
140 #endif /* __WINRT__ */
141 #endif /* HAVE_XINPUT_H */
142
143 /* vi: set ts=4 sw=4 expandtab: */
144