SDL_xinput.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_xinput.h"
  20. // Set up for C function definitions, even when using C++
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. XInputGetState_t SDL_XInputGetState = NULL;
  25. XInputSetState_t SDL_XInputSetState = NULL;
  26. XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
  27. XInputGetCapabilitiesEx_t SDL_XInputGetCapabilitiesEx = NULL;
  28. XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
  29. DWORD SDL_XInputVersion = 0;
  30. static HMODULE s_pXInputDLL = NULL;
  31. static int s_XInputDLLRefCount = 0;
  32. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  33. bool WIN_LoadXInputDLL(void)
  34. {
  35. /* Getting handles to system dlls (via LoadLibrary and its variants) is not
  36. * supported on Xbox, thus, pointers to XInput's functions can't be
  37. * retrieved via GetProcAddress.
  38. *
  39. * When on Xbox, assume that XInput is already loaded, and directly map
  40. * its XInput.h-declared functions to the SDL_XInput* set of function
  41. * pointers.
  42. */
  43. SDL_XInputGetState = (XInputGetState_t)XInputGetState;
  44. SDL_XInputSetState = (XInputSetState_t)XInputSetState;
  45. SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
  46. SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
  47. // XInput 1.4 ships with Windows 8 and 8.1:
  48. SDL_XInputVersion = (1 << 16) | 4;
  49. return true;
  50. }
  51. void WIN_UnloadXInputDLL(void)
  52. {
  53. }
  54. #else // !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  55. bool WIN_LoadXInputDLL(void)
  56. {
  57. DWORD version = 0;
  58. if (s_pXInputDLL) {
  59. SDL_assert(s_XInputDLLRefCount > 0);
  60. s_XInputDLLRefCount++;
  61. return true; // already loaded
  62. }
  63. /* NOTE: Don't load XinputUap.dll
  64. * This is XInput emulation over Windows.Gaming.Input, and has all the
  65. * limitations of that API (no devices at startup, no background input, etc.)
  66. */
  67. version = (1 << 16) | 4;
  68. s_pXInputDLL = LoadLibrary(TEXT("XInput1_4.dll")); // 1.4 Ships with Windows 8.
  69. if (!s_pXInputDLL) {
  70. version = (1 << 16) | 3;
  71. s_pXInputDLL = LoadLibrary(TEXT("XInput1_3.dll")); // 1.3 can be installed as a redistributable component.
  72. }
  73. if (!s_pXInputDLL) {
  74. s_pXInputDLL = LoadLibrary(TEXT("bin\\XInput1_3.dll"));
  75. }
  76. if (!s_pXInputDLL) {
  77. // "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.)
  78. s_pXInputDLL = LoadLibrary(TEXT("XInput9_1_0.dll"));
  79. }
  80. if (!s_pXInputDLL) {
  81. return false;
  82. }
  83. SDL_assert(s_XInputDLLRefCount == 0);
  84. SDL_XInputVersion = version;
  85. s_XInputDLLRefCount = 1;
  86. // 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...
  87. SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, (LPCSTR)100);
  88. if (!SDL_XInputGetState) {
  89. SDL_XInputGetState = (XInputGetState_t)GetProcAddress(s_pXInputDLL, "XInputGetState");
  90. }
  91. SDL_XInputSetState = (XInputSetState_t)GetProcAddress(s_pXInputDLL, "XInputSetState");
  92. SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress(s_pXInputDLL, "XInputGetCapabilities");
  93. // 108 is the ordinal for _XInputGetCapabilitiesEx, which additionally returns VID/PID of the controller.
  94. SDL_XInputGetCapabilitiesEx = (XInputGetCapabilitiesEx_t)GetProcAddress(s_pXInputDLL, (LPCSTR)108);
  95. SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress(s_pXInputDLL, "XInputGetBatteryInformation");
  96. if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
  97. WIN_UnloadXInputDLL();
  98. return false;
  99. }
  100. return true;
  101. }
  102. void WIN_UnloadXInputDLL(void)
  103. {
  104. if (s_pXInputDLL) {
  105. SDL_assert(s_XInputDLLRefCount > 0);
  106. if (--s_XInputDLLRefCount == 0) {
  107. FreeLibrary(s_pXInputDLL);
  108. s_pXInputDLL = NULL;
  109. }
  110. } else {
  111. SDL_assert(s_XInputDLLRefCount == 0);
  112. }
  113. }
  114. #endif
  115. // Ends C function definitions when using C++
  116. #ifdef __cplusplus
  117. }
  118. #endif