joypad_windows.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**************************************************************************/
  2. /* joypad_windows.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef JOYPAD_WINDOWS_H
  31. #define JOYPAD_WINDOWS_H
  32. #include "os_windows.h"
  33. #define DIRECTINPUT_VERSION 0x0800
  34. #include <dinput.h>
  35. #include <xinput.h>
  36. #include <mmsystem.h>
  37. #ifndef SAFE_RELEASE // when Windows Media Device M? is not present
  38. #define SAFE_RELEASE(x) \
  39. if (x != nullptr) { \
  40. x->Release(); \
  41. x = nullptr; \
  42. }
  43. #endif
  44. #ifndef XUSER_MAX_COUNT
  45. #define XUSER_MAX_COUNT 4
  46. #endif
  47. class JoypadWindows {
  48. public:
  49. JoypadWindows();
  50. JoypadWindows(HWND *hwnd);
  51. ~JoypadWindows();
  52. void probe_joypads();
  53. void process_joypads();
  54. private:
  55. enum {
  56. JOYPADS_MAX = 16,
  57. JOY_AXIS_COUNT = 6,
  58. MIN_JOY_AXIS = 10,
  59. MAX_JOY_AXIS = 32768,
  60. MAX_JOY_BUTTONS = 128,
  61. KEY_EVENT_BUFFER_SIZE = 512,
  62. MAX_TRIGGER = 255
  63. };
  64. struct dinput_gamepad {
  65. int id;
  66. bool attached;
  67. bool confirmed;
  68. bool last_buttons[MAX_JOY_BUTTONS];
  69. DWORD last_pad;
  70. LPDIRECTINPUTDEVICE8 di_joy;
  71. LocalVector<LONG> joy_axis;
  72. GUID guid;
  73. dinput_gamepad() {
  74. id = -1;
  75. last_pad = -1;
  76. attached = false;
  77. confirmed = false;
  78. di_joy = nullptr;
  79. guid = {};
  80. for (int i = 0; i < MAX_JOY_BUTTONS; i++) {
  81. last_buttons[i] = false;
  82. }
  83. }
  84. };
  85. struct xinput_gamepad {
  86. int id = 0;
  87. bool attached = false;
  88. bool vibrating = false;
  89. DWORD last_packet = 0;
  90. XINPUT_STATE state;
  91. uint64_t ff_timestamp = 0;
  92. uint64_t ff_end_timestamp = 0;
  93. };
  94. typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE *pState);
  95. typedef DWORD(WINAPI *XInputSetState_t)(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration);
  96. typedef MMRESULT(WINAPI *joyGetDevCaps_t)(UINT uJoyID, LPJOYCAPSW pjc, UINT cbjc);
  97. HWND *hWnd = nullptr;
  98. HANDLE xinput_dll;
  99. HANDLE winmm_dll;
  100. LPDIRECTINPUT8 dinput;
  101. Input *input = nullptr;
  102. int id_to_change;
  103. int slider_count;
  104. int x_joypad_probe_count; // XInput equivalent to dinput_gamepad.confirmed.
  105. int d_joypad_count;
  106. bool attached_joypads[JOYPADS_MAX];
  107. dinput_gamepad d_joypads[JOYPADS_MAX];
  108. xinput_gamepad x_joypads[XUSER_MAX_COUNT];
  109. static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context);
  110. static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context);
  111. void setup_d_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id);
  112. void close_d_joypad(int id = -1);
  113. void load_xinput();
  114. void unload_xinput();
  115. void unload_winmm();
  116. void post_hat(int p_device, DWORD p_dpad);
  117. bool is_d_joypad_known(const GUID &p_guid);
  118. bool is_xinput_joypad(const GUID *p_guid);
  119. bool setup_dinput_joypad(const DIDEVICEINSTANCE *instance);
  120. void probe_xinput_joypad(const String &name = ""); // Handles connect, disconnect & re-connect for XInput joypads.
  121. void joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  122. void joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp);
  123. float axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const;
  124. XInputGetState_t xinput_get_state;
  125. XInputSetState_t xinput_set_state;
  126. joyGetDevCaps_t winmm_get_joycaps; // Only for reading info on XInput joypads.
  127. };
  128. #endif // JOYPAD_WINDOWS_H