joypad_windows.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifndef SAFE_RELEASE // when Windows Media Device M? is not present
  37. #define SAFE_RELEASE(x) \
  38. if (x != nullptr) { \
  39. x->Release(); \
  40. x = nullptr; \
  41. }
  42. #endif
  43. #ifndef XUSER_MAX_COUNT
  44. #define XUSER_MAX_COUNT 4
  45. #endif
  46. class JoypadWindows {
  47. public:
  48. JoypadWindows();
  49. JoypadWindows(HWND *hwnd);
  50. ~JoypadWindows();
  51. void probe_joypads();
  52. void process_joypads();
  53. private:
  54. enum {
  55. JOYPADS_MAX = 16,
  56. JOY_AXIS_COUNT = 6,
  57. MIN_JOY_AXIS = 10,
  58. MAX_JOY_AXIS = 32768,
  59. MAX_JOY_BUTTONS = 128,
  60. KEY_EVENT_BUFFER_SIZE = 512,
  61. MAX_TRIGGER = 255
  62. };
  63. struct dinput_gamepad {
  64. int id;
  65. bool attached;
  66. bool confirmed;
  67. bool last_buttons[MAX_JOY_BUTTONS];
  68. DWORD last_pad;
  69. LPDIRECTINPUTDEVICE8 di_joy;
  70. LocalVector<LONG> joy_axis;
  71. GUID guid;
  72. dinput_gamepad() {
  73. id = -1;
  74. last_pad = -1;
  75. attached = false;
  76. confirmed = false;
  77. di_joy = nullptr;
  78. guid = {};
  79. for (int i = 0; i < MAX_JOY_BUTTONS; i++) {
  80. last_buttons[i] = false;
  81. }
  82. }
  83. };
  84. struct xinput_gamepad {
  85. int id = 0;
  86. bool attached = false;
  87. bool vibrating = false;
  88. DWORD last_packet = 0;
  89. XINPUT_STATE state;
  90. uint64_t ff_timestamp = 0;
  91. uint64_t ff_end_timestamp = 0;
  92. };
  93. typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE *pState);
  94. typedef DWORD(WINAPI *XInputSetState_t)(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration);
  95. HWND *hWnd = nullptr;
  96. HANDLE xinput_dll;
  97. LPDIRECTINPUT8 dinput;
  98. Input *input = nullptr;
  99. int id_to_change;
  100. int slider_count;
  101. int joypad_count;
  102. bool attached_joypads[JOYPADS_MAX];
  103. dinput_gamepad d_joypads[JOYPADS_MAX];
  104. xinput_gamepad x_joypads[XUSER_MAX_COUNT];
  105. static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context);
  106. static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context);
  107. void setup_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id);
  108. void close_joypad(int id = -1);
  109. void load_xinput();
  110. void unload_xinput();
  111. void post_hat(int p_device, DWORD p_dpad);
  112. bool have_device(const GUID &p_guid);
  113. bool is_xinput_device(const GUID *p_guid);
  114. bool setup_dinput_joypad(const DIDEVICEINSTANCE *instance);
  115. void joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  116. void joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp);
  117. float axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const;
  118. XInputGetState_t xinput_get_state;
  119. XInputSetState_t xinput_set_state;
  120. };
  121. #endif // JOYPAD_WINDOWS_H