joypad_linux.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* joypad_linux.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_LINUX_H
  31. #define JOYPAD_LINUX_H
  32. #ifdef JOYDEV_ENABLED
  33. #include "core/input/input.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/thread.h"
  36. #include "core/templates/local_vector.h"
  37. struct input_absinfo;
  38. class JoypadLinux {
  39. public:
  40. JoypadLinux(Input *in);
  41. ~JoypadLinux();
  42. void process_joypads();
  43. private:
  44. enum {
  45. JOYPADS_MAX = 16,
  46. MAX_ABS = 63,
  47. MAX_KEY = 767, // Hack because <linux/input.h> can't be included here
  48. };
  49. struct JoypadEvent {
  50. uint16_t type;
  51. uint16_t code;
  52. int32_t value;
  53. };
  54. struct Joypad {
  55. float curr_axis[MAX_ABS];
  56. int key_map[MAX_KEY];
  57. int abs_map[MAX_ABS];
  58. BitField<HatMask> dpad;
  59. int fd = -1;
  60. String devpath;
  61. input_absinfo *abs_info[MAX_ABS] = {};
  62. bool force_feedback = false;
  63. int ff_effect_id = 0;
  64. uint64_t ff_effect_timestamp = 0;
  65. LocalVector<JoypadEvent> events;
  66. ~Joypad();
  67. void reset();
  68. };
  69. #ifdef UDEV_ENABLED
  70. bool use_udev = false;
  71. #endif
  72. Input *input = nullptr;
  73. SafeFlag monitor_joypads_exit;
  74. SafeFlag joypad_events_exit;
  75. Thread monitor_joypads_thread;
  76. Thread joypad_events_thread;
  77. Joypad joypads[JOYPADS_MAX];
  78. Mutex joypads_mutex[JOYPADS_MAX];
  79. Vector<String> attached_devices;
  80. // List of lowercase words that will prevent the controller from being recognized if its name matches.
  81. // This is done to prevent trackpads, graphics tablets and motherboard LED controllers from being
  82. // recognized as controllers (and taking up controller ID slots as a result).
  83. // Only whole words are matched within the controller name string. The match is case-insensitive.
  84. const Vector<String> banned_words = {
  85. "touchpad", // Matches e.g. "SynPS/2 Synaptics TouchPad", "Sony Interactive Entertainment DualSense Wireless Controller Touchpad"
  86. "trackpad",
  87. "clickpad",
  88. "keyboard", // Matches e.g. "PG-90215 Keyboard", "Usb Keyboard Usb Keyboard Consumer Control"
  89. "mouse", // Matches e.g. "Mouse passthrough"
  90. "pen", // Matches e.g. "Wacom One by Wacom S Pen"
  91. "finger", // Matches e.g. "Wacom HID 495F Finger"
  92. "led", // Matches e.g. "ASRock LED Controller"
  93. };
  94. static void monitor_joypads_thread_func(void *p_user);
  95. void monitor_joypads_thread_run();
  96. void open_joypad(const char *p_path);
  97. void setup_joypad_properties(Joypad &p_joypad);
  98. void close_joypads();
  99. void close_joypad(const char *p_devpath);
  100. void close_joypad(Joypad &p_joypad, int p_id);
  101. #ifdef UDEV_ENABLED
  102. void enumerate_joypads(struct udev *p_udev);
  103. void monitor_joypads(struct udev *p_udev);
  104. #endif
  105. void monitor_joypads();
  106. void joypad_vibration_start(Joypad &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  107. void joypad_vibration_stop(Joypad &p_joypad, uint64_t p_timestamp);
  108. static void joypad_events_thread_func(void *p_user);
  109. void joypad_events_thread_run();
  110. float axis_correct(const input_absinfo *p_abs, int p_value) const;
  111. };
  112. #endif // JOYDEV_ENABLED
  113. #endif // JOYPAD_LINUX_H