SDL_xinput.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_assert.h"
  20. #include "SDL_xinput.h"
  21. #ifdef HAVE_XINPUT_H
  22. XInputGetState_t SDL_XInputGetState = NULL;
  23. XInputSetState_t SDL_XInputSetState = NULL;
  24. XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
  25. XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
  26. DWORD SDL_XInputVersion = 0;
  27. static HANDLE s_pXInputDLL = 0;
  28. static int s_XInputDLLRefCount = 0;
  29. #ifdef __WINRT__
  30. int
  31. WIN_LoadXInputDLL(void)
  32. {
  33. /* Getting handles to system dlls (via LoadLibrary and its variants) is not
  34. * supported on WinRT, thus, pointers to XInput's functions can't be
  35. * retrieved via GetProcAddress.
  36. *
  37. * When on WinRT, assume that XInput is already loaded, and directly map
  38. * its XInput.h-declared functions to the SDL_XInput* set of function
  39. * pointers.
  40. *
  41. * Side-note: XInputGetStateEx is not available for use in WinRT.
  42. * This seems to mean that support for the guide button is not available
  43. * in WinRT, unfortunately.
  44. */
  45. SDL_XInputGetState = (XInputGetState_t)XInputGetState;
  46. SDL_XInputSetState = (XInputSetState_t)XInputSetState;
  47. SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
  48. SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
  49. /* XInput 1.4 ships with Windows 8 and 8.1: */
  50. SDL_XInputVersion = (1 << 16) | 4;
  51. return 0;
  52. }
  53. void
  54. WIN_UnloadXInputDLL(void)
  55. {
  56. }
  57. #else /* !__WINRT__ */
  58. int
  59. WIN_LoadXInputDLL(void)
  60. {
  61. DWORD version = 0;
  62. if (s_pXInputDLL) {
  63. SDL_assert(s_XInputDLLRefCount > 0);
  64. s_XInputDLLRefCount++;
  65. return 0; /* already loaded */
  66. }
  67. version = (1 << 16) | 4;
  68. s_pXInputDLL = LoadLibrary(L"XInput1_4.dll"); /* 1.4 Ships with Windows 8. */
  69. if (!s_pXInputDLL) {
  70. version = (1 << 16) | 3;
  71. s_pXInputDLL = LoadLibrary(L"XInput1_3.dll"); /* 1.3 can be installed as a redistributable component. */
  72. }
  73. if (!s_pXInputDLL) {
  74. s_pXInputDLL = LoadLibrary(L"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(L"XInput9_1_0.dll");
  79. }
  80. if (!s_pXInputDLL) {
  81. return -1;
  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. *(void**)&SDL_XInputGetState = GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
  88. if (!SDL_XInputGetState) {
  89. *(void**)&SDL_XInputGetState = GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetState");
  90. }
  91. *(void**)&SDL_XInputSetState = GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
  92. *(void**)&SDL_XInputGetCapabilities = GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
  93. *(void**)&SDL_XInputGetBatteryInformation = GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
  94. if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
  95. WIN_UnloadXInputDLL();
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. void
  101. WIN_UnloadXInputDLL(void)
  102. {
  103. if (s_pXInputDLL) {
  104. SDL_assert(s_XInputDLLRefCount > 0);
  105. if (--s_XInputDLLRefCount == 0) {
  106. FreeLibrary(s_pXInputDLL);
  107. s_pXInputDLL = NULL;
  108. }
  109. } else {
  110. SDL_assert(s_XInputDLLRefCount == 0);
  111. }
  112. }
  113. #endif /* __WINRT__ */
  114. #endif /* HAVE_XINPUT_H */
  115. /* vi: set ts=4 sw=4 expandtab: */