joypad_linux.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. //author: Andreas Haas <hondres, liugam3@gmail.com>
  31. #ifndef JOYPAD_LINUX_H
  32. #define JOYPAD_LINUX_H
  33. #ifdef JOYDEV_ENABLED
  34. #include "core/os/mutex.h"
  35. #include "core/os/thread.h"
  36. #include "main/input_default.h"
  37. struct input_absinfo;
  38. class JoypadLinux {
  39. public:
  40. JoypadLinux(InputDefault *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. int dpad;
  59. int fd;
  60. String devpath;
  61. input_absinfo *abs_info[MAX_ABS];
  62. bool force_feedback;
  63. int ff_effect_id;
  64. uint64_t ff_effect_timestamp;
  65. LocalVector<JoypadEvent> events;
  66. Joypad();
  67. ~Joypad();
  68. void reset();
  69. };
  70. #ifdef UDEV_ENABLED
  71. bool use_udev;
  72. #endif
  73. InputDefault *input;
  74. SafeFlag monitor_joypads_exit;
  75. SafeFlag joypad_events_exit;
  76. Thread monitor_joypads_thread;
  77. Thread joypad_events_thread;
  78. Joypad joypads[JOYPADS_MAX];
  79. Mutex joypads_mutex[JOYPADS_MAX];
  80. Vector<String> attached_devices;
  81. static void monitor_joypads_thread_func(void *p_user);
  82. void monitor_joypads_thread_run();
  83. void open_joypad(const char *p_path);
  84. void setup_joypad_properties(Joypad &p_joypad);
  85. void close_joypads();
  86. void close_joypad(const char *p_devpath);
  87. void close_joypad(Joypad &p_joypad, int p_id);
  88. #ifdef UDEV_ENABLED
  89. void enumerate_joypads(struct udev *p_udev);
  90. void monitor_joypads(struct udev *p_udev);
  91. #endif
  92. void monitor_joypads();
  93. void joypad_vibration_start(Joypad &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp);
  94. void joypad_vibration_stop(Joypad &p_joypad, uint64_t p_timestamp);
  95. static void joypad_events_thread_func(void *p_user);
  96. void joypad_events_thread_run();
  97. float axis_correct(const input_absinfo *p_abs, int p_value) const;
  98. };
  99. #endif
  100. #endif // JOYPAD_LINUX_H