joypad_linux.h 5.0 KB

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