Atlas - SDL_xinput.c

Home / ext / SDL / src / core / windows Lines: 1 | Size: 4761 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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; 35 36static HMODULE s_pXInputDLL = NULL; 37static int s_XInputDLLRefCount = 0; 38 39#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES) 40 41bool WIN_LoadXInputDLL(void) 42{ 43 /* Getting handles to system dlls (via LoadLibrary and its variants) is not 44 * supported on Xbox, thus, pointers to XInput's functions can't be 45 * retrieved via GetProcAddress. 46 * 47 * When on Xbox, 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 SDL_XInputGetState = (XInputGetState_t)XInputGetState; 52 SDL_XInputSetState = (XInputSetState_t)XInputSetState; 53 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities; 54 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation; 55 56 return true; 57} 58 59void WIN_UnloadXInputDLL(void) 60{ 61} 62 63#else // !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) 64 65bool WIN_LoadXInputDLL(void) 66{ 67 if (s_pXInputDLL) { 68 SDL_assert(s_XInputDLLRefCount > 0); 69 s_XInputDLLRefCount++; 70 return true; // already loaded 71 } 72 73 /* NOTE: Don't load XinputUap.dll 74 * This is XInput emulation over Windows.Gaming.Input, and has all the 75 * limitations of that API (no devices at startup, no background input, etc.) 76 */ 77 s_pXInputDLL = LoadLibrary(TEXT("XInput1_4.dll")); // 1.4 Ships with Windows 8. 78 if (!s_pXInputDLL) { 79 s_pXInputDLL = LoadLibrary(TEXT("XInput1_3.dll")); // 1.3 can be installed as a redistributable component. 80 } 81 if (!s_pXInputDLL) { 82 // "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.) 83 s_pXInputDLL = LoadLibrary(TEXT("XInput9_1_0.dll")); 84 } 85 if (!s_pXInputDLL) { 86 return false; 87 } 88 89 SDL_assert(s_XInputDLLRefCount == 0); 90 s_XInputDLLRefCount = 1; 91 92 // 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... 93 SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, (LPCSTR)100); 94 if (!SDL_XInputGetState) { 95 SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, "XInputGetState"); 96 } 97 SDL_XInputSetState = (XInputSetState_t)GetProcAddress(s_pXInputDLL, "XInputSetState"); 98 SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress(s_pXInputDLL, "XInputGetCapabilities"); 99 // 108 is the ordinal for _XInputGetCapabilitiesEx, which additionally returns VID/PID of the controller. 100 SDL_XInputGetCapabilitiesEx = (XInputGetCapabilitiesEx_t)GetProcAddress(s_pXInputDLL, (LPCSTR)108); 101 SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress(s_pXInputDLL, "XInputGetBatteryInformation"); 102 if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) { 103 WIN_UnloadXInputDLL(); 104 return false; 105 } 106 107 return true; 108} 109 110void WIN_UnloadXInputDLL(void) 111{ 112 if (s_pXInputDLL) { 113 SDL_assert(s_XInputDLLRefCount > 0); 114 if (--s_XInputDLLRefCount == 0) { 115 FreeLibrary(s_pXInputDLL); 116 s_pXInputDLL = NULL; 117 } 118 } else { 119 SDL_assert(s_XInputDLLRefCount == 0); 120 } 121} 122 123#endif 124 125// Ends C function definitions when using C++ 126#ifdef __cplusplus 127} 128#endif 129
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.