SDL_xinput.c 4.8 KB

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