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 | |
31 | #ifndef JOYPAD_LINUX_H |
32 | #define JOYPAD_LINUX_H |
33 | |
34 | #ifdef JOYDEV_ENABLED |
35 | |
36 | #include "core/input/input.h" |
37 | #include "core/os/mutex.h" |
38 | #include "core/os/thread.h" |
39 | #include "core/templates/local_vector.h" |
40 | |
41 | struct input_absinfo; |
42 | |
43 | class JoypadLinux { |
44 | public: |
45 | JoypadLinux(Input *in); |
46 | ~JoypadLinux(); |
47 | void process_joypads(); |
48 | |
49 | private: |
50 | enum { |
51 | JOYPADS_MAX = 16, |
52 | MAX_ABS = 63, |
53 | MAX_KEY = 767, // Hack because <linux/input.h> can't be included here |
54 | }; |
55 | |
56 | struct JoypadEvent { |
57 | uint16_t type; |
58 | uint16_t code; |
59 | int32_t value; |
60 | }; |
61 | |
62 | struct Joypad { |
63 | float curr_axis[MAX_ABS]; |
64 | int key_map[MAX_KEY]; |
65 | int abs_map[MAX_ABS]; |
66 | BitField<HatMask> dpad; |
67 | int fd = -1; |
68 | |
69 | String devpath; |
70 | input_absinfo *abs_info[MAX_ABS] = {}; |
71 | |
72 | bool force_feedback = false; |
73 | int ff_effect_id = 0; |
74 | uint64_t ff_effect_timestamp = 0; |
75 | |
76 | LocalVector<JoypadEvent> events; |
77 | |
78 | ~Joypad(); |
79 | void reset(); |
80 | }; |
81 | |
82 | #ifdef UDEV_ENABLED |
83 | bool use_udev = false; |
84 | #endif |
85 | Input *input = nullptr; |
86 | |
87 | SafeFlag monitor_joypads_exit; |
88 | SafeFlag joypad_events_exit; |
89 | Thread monitor_joypads_thread; |
90 | Thread joypad_events_thread; |
91 | |
92 | Joypad joypads[JOYPADS_MAX]; |
93 | Mutex joypads_mutex[JOYPADS_MAX]; |
94 | |
95 | Vector<String> attached_devices; |
96 | |
97 | static void monitor_joypads_thread_func(void *p_user); |
98 | void monitor_joypads_thread_run(); |
99 | |
100 | void open_joypad(const char *p_path); |
101 | void setup_joypad_properties(Joypad &p_joypad); |
102 | |
103 | void close_joypads(); |
104 | void close_joypad(const char *p_devpath); |
105 | void close_joypad(Joypad &p_joypad, int p_id); |
106 | |
107 | #ifdef UDEV_ENABLED |
108 | void enumerate_joypads(struct udev *p_udev); |
109 | void monitor_joypads(struct udev *p_udev); |
110 | #endif |
111 | void monitor_joypads(); |
112 | |
113 | void joypad_vibration_start(Joypad &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp); |
114 | void joypad_vibration_stop(Joypad &p_joypad, uint64_t p_timestamp); |
115 | |
116 | static void joypad_events_thread_func(void *p_user); |
117 | void joypad_events_thread_run(); |
118 | |
119 | float axis_correct(const input_absinfo *p_abs, int p_value) const; |
120 | }; |
121 | |
122 | #endif // JOYDEV_ENABLED |
123 | |
124 | #endif // JOYPAD_LINUX_H |
125 | |