joypad_windows.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #pragma once
  31. #include "os_windows.h"
  32. #define DIRECTINPUT_VERSION 0x0800
  33. #include <dinput.h>
  34. #include <xinput.h>
  35. #include <mmsystem.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. typedef MMRESULT(WINAPI *joyGetDevCaps_t)(UINT uJoyID, LPJOYCAPSW pjc, UINT cbjc);
  96. HWND *hWnd = nullptr;
  97. HANDLE xinput_dll;
  98. HANDLE winmm_dll;
  99. LPDIRECTINPUT8 dinput;
  100. Input *input = nullptr;
  101. int id_to_change;
  102. int slider_count;
  103. int x_joypad_probe_count; // XInput equivalent to dinput_gamepad.confirmed.
  104. int d_joypad_count;
  105. bool attached_joypads[JOYPADS_MAX];
  106. dinput_gamepad d_joypads[JOYPADS_MAX];
  107. xinput_gamepad x_joypads[XUSER_MAX_COUNT];
  108. static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context);
  109. static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context);
  110. void setup_d_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id);
  111. void close_d_joypad(int id = -1);
  112. void load_xinput();
  113. void unload_xinput();
  114. void unload_winmm();
  115. void post_hat(int p_device, DWORD p_dpad);
  116. bool is_d_joypad_known(const GUID &p_guid);
  117. bool is_xinput_joypad(const GUID *p_guid);
  118. bool setup_dinput_joypad(const DIDEVICEINSTANCE *instance);
  119. void probe_xinput_joypad(const String &name = ""); // Handles connect, disconnect & re-connect for XInput joypads.
  120. void joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  121. void joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp);
  122. float axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const;
  123. XInputGetState_t xinput_get_state;
  124. XInputSetState_t xinput_set_state;
  125. joyGetDevCaps_t winmm_get_joycaps; // Only for reading info on XInput joypads.
  126. };