joystick.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* joystick.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. //author: Andreas Haas <hondres, [email protected]>
  30. #ifndef JOYSTICK_H
  31. #define JOYSTICK_H
  32. #include "os_windows.h"
  33. #define DIRECTINPUT_VERSION 0x0800
  34. #include <dinput.h>
  35. #include <xinput.h> // on unix the file is called "xinput.h", on windows I'm sure it won't mind
  36. #ifndef SAFE_RELEASE // when Windows Media Device M? is not present
  37. #define SAFE_RELEASE(x) \
  38. if(x != NULL) \
  39. { \
  40. x->Release(); \
  41. x = NULL; \
  42. }
  43. #endif
  44. #ifndef XUSER_MAX_COUNT
  45. #define XUSER_MAX_COUNT 4
  46. #endif
  47. class joystick_windows
  48. {
  49. public:
  50. joystick_windows();
  51. joystick_windows(InputDefault* _input, HWND* hwnd);
  52. ~joystick_windows();
  53. void probe_joysticks();
  54. unsigned int process_joysticks(unsigned int p_last_id);
  55. private:
  56. enum {
  57. JOYSTICKS_MAX = 16,
  58. JOY_AXIS_COUNT = 6,
  59. MIN_JOY_AXIS = 10,
  60. MAX_JOY_AXIS = 32768,
  61. MAX_JOY_BUTTONS = 128,
  62. KEY_EVENT_BUFFER_SIZE = 512,
  63. MAX_TRIGGER = 255
  64. };
  65. struct dinput_gamepad {
  66. int id;
  67. bool attached;
  68. bool confirmed;
  69. bool last_buttons[MAX_JOY_BUTTONS];
  70. DWORD last_pad;
  71. LPDIRECTINPUTDEVICE8 di_joy;
  72. List<DWORD> joy_axis;
  73. GUID guid;
  74. dinput_gamepad() {
  75. id = -1;
  76. last_pad = -1;
  77. attached = false;
  78. confirmed = false;
  79. for (int i = 0; i < MAX_JOY_BUTTONS; i++)
  80. last_buttons[i] = false;
  81. }
  82. };
  83. struct xinput_gamepad {
  84. int id;
  85. bool attached;
  86. DWORD last_packet;
  87. XINPUT_STATE state;
  88. xinput_gamepad() {
  89. attached = false;
  90. last_packet = 0;
  91. }
  92. };
  93. typedef DWORD (WINAPI *XInputGetState_t) (DWORD dwUserIndex, XINPUT_STATE* pState);
  94. HWND* hWnd;
  95. HANDLE xinput_dll;
  96. LPDIRECTINPUT8 dinput;
  97. InputDefault* input;
  98. int id_to_change;
  99. int joystick_count;
  100. bool attached_joysticks[JOYSTICKS_MAX];
  101. dinput_gamepad d_joysticks[JOYSTICKS_MAX];
  102. xinput_gamepad x_joysticks[XUSER_MAX_COUNT];
  103. static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* p_instance, void* p_context);
  104. static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE* instance, void* context);
  105. void setup_joystick_object(const DIDEVICEOBJECTINSTANCE* ob, int p_joy_id);
  106. void close_joystick(int id = -1);
  107. void load_xinput();
  108. void unload_xinput();
  109. int check_free_joy_slot() const;
  110. unsigned int post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad);
  111. bool have_device(const GUID &p_guid);
  112. bool is_xinput_device(const GUID* p_guid);
  113. bool setup_dinput_joystick(const DIDEVICEINSTANCE* instance);
  114. InputDefault::JoyAxis axis_correct(int p_val, bool p_xinput = false, bool p_trigger = false, bool p_negate = false) const;
  115. XInputGetState_t xinput_get_state;
  116. };
  117. #endif