joypad_linux.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************/
  2. /* joypad_linux.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. 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 Joypad {
  49. float curr_axis[MAX_ABS];
  50. int key_map[MAX_KEY];
  51. int abs_map[MAX_ABS];
  52. HatMask dpad = HatMask::CENTER;
  53. int fd = -1;
  54. String devpath;
  55. input_absinfo *abs_info[MAX_ABS] = {};
  56. bool force_feedback = false;
  57. int ff_effect_id = 0;
  58. uint64_t ff_effect_timestamp = 0;
  59. ~Joypad();
  60. void reset();
  61. };
  62. #ifdef UDEV_ENABLED
  63. bool use_udev = false;
  64. #endif
  65. SafeFlag exit_monitor;
  66. Mutex joy_mutex;
  67. Thread joy_thread;
  68. Input *input = nullptr;
  69. Joypad joypads[JOYPADS_MAX];
  70. Vector<String> attached_devices;
  71. static void joy_thread_func(void *p_user);
  72. int get_joy_from_path(String p_path) const;
  73. void setup_joypad_properties(int p_id);
  74. void close_joypad(int p_id = -1);
  75. #ifdef UDEV_ENABLED
  76. void enumerate_joypads(struct udev *p_udev);
  77. void monitor_joypads(struct udev *p_udev);
  78. #endif
  79. void monitor_joypads();
  80. void run_joypad_thread();
  81. void open_joypad(const char *p_path);
  82. void joypad_vibration_start(int p_id, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  83. void joypad_vibration_stop(int p_id, uint64_t p_timestamp);
  84. float axis_correct(const input_absinfo *p_abs, int p_value) const;
  85. };
  86. #endif // JOYDEV_ENABLED
  87. #endif // JOYPAD_LINUX_H