SDL_xinput.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. DWORD SDL_XInputVersion = 0;
  26. static HANDLE s_pXInputDLL = 0;
  27. static int s_XInputDLLRefCount = 0;
  28. int
  29. WIN_LoadXInputDLL(void)
  30. {
  31. DWORD version = 0;
  32. if (s_pXInputDLL) {
  33. SDL_assert(s_XInputDLLRefCount > 0);
  34. s_XInputDLLRefCount++;
  35. return 0; /* already loaded */
  36. }
  37. version = (1 << 16) | 4;
  38. s_pXInputDLL = LoadLibrary(L"XInput1_4.dll"); /* 1.4 Ships with Windows 8. */
  39. if (!s_pXInputDLL) {
  40. version = (1 << 16) | 3;
  41. s_pXInputDLL = LoadLibrary(L"XInput1_3.dll"); /* 1.3 Ships with Vista and Win7, can be installed as a redistributable component. */
  42. }
  43. if (!s_pXInputDLL) {
  44. s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
  45. }
  46. if (!s_pXInputDLL) {
  47. return -1;
  48. }
  49. SDL_assert(s_XInputDLLRefCount == 0);
  50. SDL_XInputVersion = version;
  51. s_XInputDLLRefCount = 1;
  52. /* 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... */
  53. SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
  54. SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
  55. SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
  56. if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
  57. WIN_UnloadXInputDLL();
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. void
  63. WIN_UnloadXInputDLL(void)
  64. {
  65. if (s_pXInputDLL) {
  66. SDL_assert(s_XInputDLLRefCount > 0);
  67. if (--s_XInputDLLRefCount == 0) {
  68. FreeLibrary(s_pXInputDLL);
  69. s_pXInputDLL = NULL;
  70. }
  71. } else {
  72. SDL_assert(s_XInputDLLRefCount == 0);
  73. }
  74. }
  75. #endif /* HAVE_XINPUT_H */
  76. /* vi: set ts=4 sw=4 expandtab: */