summaryrefslogtreecommitdiff
path: root/SDL-3.2.8/src/core/windows/SDL_xinput.c
diff options
context:
space:
mode:
Diffstat (limited to 'SDL-3.2.8/src/core/windows/SDL_xinput.c')
-rw-r--r--SDL-3.2.8/src/core/windows/SDL_xinput.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/SDL-3.2.8/src/core/windows/SDL_xinput.c b/SDL-3.2.8/src/core/windows/SDL_xinput.c
new file mode 100644
index 0000000..ba5e4c1
--- /dev/null
+++ b/SDL-3.2.8/src/core/windows/SDL_xinput.c
@@ -0,0 +1,140 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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// Set up for C function definitions, even when using C++
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30XInputGetState_t SDL_XInputGetState = NULL;
31XInputSetState_t SDL_XInputSetState = NULL;
32XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
33XInputGetCapabilitiesEx_t SDL_XInputGetCapabilitiesEx = NULL;
34XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
35DWORD SDL_XInputVersion = 0;
36
37static HMODULE s_pXInputDLL = NULL;
38static int s_XInputDLLRefCount = 0;
39
40#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
41
42bool WIN_LoadXInputDLL(void)
43{
44 /* Getting handles to system dlls (via LoadLibrary and its variants) is not
45 * supported on Xbox, thus, pointers to XInput's functions can't be
46 * retrieved via GetProcAddress.
47 *
48 * When on Xbox, 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 SDL_XInputGetState = (XInputGetState_t)XInputGetState;
53 SDL_XInputSetState = (XInputSetState_t)XInputSetState;
54 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
55 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
56
57 // XInput 1.4 ships with Windows 8 and 8.1:
58 SDL_XInputVersion = (1 << 16) | 4;
59
60 return true;
61}
62
63void WIN_UnloadXInputDLL(void)
64{
65}
66
67#else // !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
68
69bool WIN_LoadXInputDLL(void)
70{
71 DWORD version = 0;
72
73 if (s_pXInputDLL) {
74 SDL_assert(s_XInputDLLRefCount > 0);
75 s_XInputDLLRefCount++;
76 return true; // already loaded
77 }
78
79 /* NOTE: Don't load XinputUap.dll
80 * This is XInput emulation over Windows.Gaming.Input, and has all the
81 * limitations of that API (no devices at startup, no background input, etc.)
82 */
83 version = (1 << 16) | 4;
84 s_pXInputDLL = LoadLibrary(TEXT("XInput1_4.dll")); // 1.4 Ships with Windows 8.
85 if (!s_pXInputDLL) {
86 version = (1 << 16) | 3;
87 s_pXInputDLL = LoadLibrary(TEXT("XInput1_3.dll")); // 1.3 can be installed as a redistributable component.
88 }
89 if (!s_pXInputDLL) {
90 s_pXInputDLL = LoadLibrary(TEXT("bin\\XInput1_3.dll"));
91 }
92 if (!s_pXInputDLL) {
93 // "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.)
94 s_pXInputDLL = LoadLibrary(TEXT("XInput9_1_0.dll"));
95 }
96 if (!s_pXInputDLL) {
97 return false;
98 }
99
100 SDL_assert(s_XInputDLLRefCount == 0);
101 SDL_XInputVersion = version;
102 s_XInputDLLRefCount = 1;
103
104 // 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...
105 SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, (LPCSTR)100);
106 if (!SDL_XInputGetState) {
107 SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, "XInputGetState");
108 }
109 SDL_XInputSetState = (XInputSetState_t)GetProcAddress(s_pXInputDLL, "XInputSetState");
110 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress(s_pXInputDLL, "XInputGetCapabilities");
111 // 108 is the ordinal for _XInputGetCapabilitiesEx, which additionally returns VID/PID of the controller.
112 SDL_XInputGetCapabilitiesEx = (XInputGetCapabilitiesEx_t)GetProcAddress(s_pXInputDLL, (LPCSTR)108);
113 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress(s_pXInputDLL, "XInputGetBatteryInformation");
114 if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
115 WIN_UnloadXInputDLL();
116 return false;
117 }
118
119 return true;
120}
121
122void WIN_UnloadXInputDLL(void)
123{
124 if (s_pXInputDLL) {
125 SDL_assert(s_XInputDLLRefCount > 0);
126 if (--s_XInputDLLRefCount == 0) {
127 FreeLibrary(s_pXInputDLL);
128 s_pXInputDLL = NULL;
129 }
130 } else {
131 SDL_assert(s_XInputDLLRefCount == 0);
132 }
133}
134
135#endif
136
137// Ends C function definitions when using C++
138#ifdef __cplusplus
139}
140#endif