SDL_gameinput.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifdef HAVE_GAMEINPUT_H
  20. #include "SDL_windows.h"
  21. #include "SDL_gameinput.h"
  22. static SDL_SharedObject *g_hGameInputDLL;
  23. static IGameInput *g_pGameInput;
  24. static int g_nGameInputRefCount;
  25. bool SDL_InitGameInput(IGameInput **ppGameInput)
  26. {
  27. if (g_nGameInputRefCount == 0) {
  28. g_hGameInputDLL = SDL_LoadObject("gameinput.dll");
  29. if (!g_hGameInputDLL) {
  30. return false;
  31. }
  32. typedef HRESULT (WINAPI *GameInputCreate_t)(IGameInput **gameInput);
  33. GameInputCreate_t GameInputCreateFunc = (GameInputCreate_t)SDL_LoadFunction(g_hGameInputDLL, "GameInputCreate");
  34. if (!GameInputCreateFunc) {
  35. SDL_UnloadObject(g_hGameInputDLL);
  36. return false;
  37. }
  38. IGameInput *pGameInput = NULL;
  39. HRESULT hr = GameInputCreateFunc(&pGameInput);
  40. if (FAILED(hr)) {
  41. SDL_UnloadObject(g_hGameInputDLL);
  42. return WIN_SetErrorFromHRESULT("GameInputCreate failed", hr);
  43. }
  44. #ifdef SDL_PLATFORM_WIN32
  45. #if GAMEINPUT_API_VERSION >= 1
  46. hr = pGameInput->QueryInterface(IID_IGameInput, (void **)&g_pGameInput);
  47. #else
  48. // We require GameInput v1.1 or newer
  49. hr = E_NOINTERFACE;
  50. #endif
  51. pGameInput->Release();
  52. if (FAILED(hr)) {
  53. SDL_UnloadObject(g_hGameInputDLL);
  54. return WIN_SetErrorFromHRESULT("GameInput QueryInterface failed", hr);
  55. }
  56. #else
  57. // Assume that the version we get is compatible with the current SDK
  58. g_pGameInput = pGameInput;
  59. #endif
  60. }
  61. ++g_nGameInputRefCount;
  62. if (ppGameInput) {
  63. *ppGameInput = g_pGameInput;
  64. }
  65. return true;
  66. }
  67. void SDL_QuitGameInput(void)
  68. {
  69. SDL_assert(g_nGameInputRefCount > 0);
  70. --g_nGameInputRefCount;
  71. if (g_nGameInputRefCount == 0) {
  72. if (g_pGameInput) {
  73. g_pGameInput->Release();
  74. g_pGameInput = NULL;
  75. }
  76. if (g_hGameInputDLL) {
  77. SDL_UnloadObject(g_hGameInputDLL);
  78. g_hGameInputDLL = NULL;
  79. }
  80. }
  81. }
  82. #endif // HAVE_GAMEINPUT_H