1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | Copyright (C) 2020 Collabora Ltd. |
5 | |
6 | This software is provided 'as-is', without any express or implied |
7 | warranty. In no event will the authors be held liable for any damages |
8 | arising from the use of this software. |
9 | |
10 | Permission is granted to anyone to use this software for any purpose, |
11 | including commercial applications, and to alter it and redistribute it |
12 | freely, subject to the following restrictions: |
13 | |
14 | 1. The origin of this software must not be misrepresented; you must not |
15 | claim that you wrote the original software. If you use this software |
16 | in a product, an acknowledgment in the product documentation would be |
17 | appreciated but is not required. |
18 | 2. Altered source versions must be plainly marked as such, and must not be |
19 | misrepresented as being the original software. |
20 | 3. This notice may not be removed or altered from any source distribution. |
21 | */ |
22 | #include "SDL_internal.h" |
23 | |
24 | #include "SDL_evdev_capabilities.h" |
25 | |
26 | #ifdef HAVE_LINUX_INPUT_H |
27 | |
28 | // missing defines in older Linux kernel headers |
29 | #ifndef BTN_TRIGGER_HAPPY |
30 | #define BTN_TRIGGER_HAPPY 0x2c0 |
31 | #endif |
32 | #ifndef BTN_DPAD_UP |
33 | #define BTN_DPAD_UP 0x220 |
34 | #endif |
35 | #ifndef KEY_ALS_TOGGLE |
36 | #define KEY_ALS_TOGGLE 0x230 |
37 | #endif |
38 | |
39 | extern int |
40 | SDL_EVDEV_GuessDeviceClass(const unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)], |
41 | const unsigned long bitmask_ev[NBITS(EV_MAX)], |
42 | const unsigned long bitmask_abs[NBITS(ABS_MAX)], |
43 | const unsigned long bitmask_key[NBITS(KEY_MAX)], |
44 | const unsigned long bitmask_rel[NBITS(REL_MAX)]) |
45 | { |
46 | struct range |
47 | { |
48 | unsigned start; |
49 | unsigned end; |
50 | }; |
51 | |
52 | // key code ranges above BTN_MISC (start is inclusive, stop is exclusive) |
53 | static const struct range high_key_blocks[] = { |
54 | { KEY_OK, BTN_DPAD_UP }, |
55 | { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY } |
56 | }; |
57 | |
58 | int devclass = 0; |
59 | unsigned long keyboard_mask; |
60 | |
61 | // If the kernel specifically says it's an accelerometer, believe it |
62 | if (test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props)) { |
63 | return SDL_UDEV_DEVICE_ACCELEROMETER; |
64 | } |
65 | |
66 | // We treat pointing sticks as indistinguishable from mice |
67 | if (test_bit(INPUT_PROP_POINTING_STICK, bitmask_props)) { |
68 | return SDL_UDEV_DEVICE_MOUSE; |
69 | } |
70 | |
71 | // We treat buttonpads as equivalent to touchpads |
72 | if (test_bit(INPUT_PROP_TOPBUTTONPAD, bitmask_props) || |
73 | test_bit(INPUT_PROP_BUTTONPAD, bitmask_props) || |
74 | test_bit(INPUT_PROP_SEMI_MT, bitmask_props)) { |
75 | return SDL_UDEV_DEVICE_TOUCHPAD; |
76 | } |
77 | |
78 | // X, Y, Z axes but no buttons probably means an accelerometer |
79 | if (test_bit(EV_ABS, bitmask_ev) && |
80 | test_bit(ABS_X, bitmask_abs) && |
81 | test_bit(ABS_Y, bitmask_abs) && |
82 | test_bit(ABS_Z, bitmask_abs) && |
83 | !test_bit(EV_KEY, bitmask_ev)) { |
84 | return SDL_UDEV_DEVICE_ACCELEROMETER; |
85 | } |
86 | |
87 | /* RX, RY, RZ axes but no buttons probably means a gyro or |
88 | * accelerometer (we don't distinguish) */ |
89 | if (test_bit(EV_ABS, bitmask_ev) && |
90 | test_bit(ABS_RX, bitmask_abs) && |
91 | test_bit(ABS_RY, bitmask_abs) && |
92 | test_bit(ABS_RZ, bitmask_abs) && |
93 | !test_bit(EV_KEY, bitmask_ev)) { |
94 | return SDL_UDEV_DEVICE_ACCELEROMETER; |
95 | } |
96 | |
97 | if (test_bit(EV_ABS, bitmask_ev) && |
98 | test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) { |
99 | if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) { |
100 | ; // ID_INPUT_TABLET |
101 | } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) { |
102 | devclass |= SDL_UDEV_DEVICE_TOUCHPAD; // ID_INPUT_TOUCHPAD |
103 | } else if (test_bit(BTN_MOUSE, bitmask_key)) { |
104 | devclass |= SDL_UDEV_DEVICE_MOUSE; // ID_INPUT_MOUSE |
105 | } else if (test_bit(BTN_TOUCH, bitmask_key)) { |
106 | /* TODO: better determining between touchscreen and multitouch touchpad, |
107 | see https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-input_id.c */ |
108 | devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN; // ID_INPUT_TOUCHSCREEN |
109 | } |
110 | |
111 | if (test_bit(BTN_TRIGGER, bitmask_key) || |
112 | test_bit(BTN_A, bitmask_key) || |
113 | test_bit(BTN_1, bitmask_key) || |
114 | test_bit(ABS_RX, bitmask_abs) || |
115 | test_bit(ABS_RY, bitmask_abs) || |
116 | test_bit(ABS_RZ, bitmask_abs) || |
117 | test_bit(ABS_THROTTLE, bitmask_abs) || |
118 | test_bit(ABS_RUDDER, bitmask_abs) || |
119 | test_bit(ABS_WHEEL, bitmask_abs) || |
120 | test_bit(ABS_GAS, bitmask_abs) || |
121 | test_bit(ABS_BRAKE, bitmask_abs)) { |
122 | devclass |= SDL_UDEV_DEVICE_JOYSTICK; // ID_INPUT_JOYSTICK |
123 | } |
124 | } |
125 | |
126 | if (test_bit(EV_REL, bitmask_ev) && |
127 | test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) && |
128 | test_bit(BTN_MOUSE, bitmask_key)) { |
129 | devclass |= SDL_UDEV_DEVICE_MOUSE; // ID_INPUT_MOUSE |
130 | } |
131 | |
132 | if (test_bit(EV_KEY, bitmask_ev)) { |
133 | unsigned i; |
134 | unsigned long found = 0; |
135 | |
136 | for (i = 0; i < BTN_MISC / BITS_PER_LONG; ++i) { |
137 | found |= bitmask_key[i]; |
138 | } |
139 | // If there are no keys in the lower block, check the higher blocks |
140 | if (!found) { |
141 | unsigned block; |
142 | for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { |
143 | for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { |
144 | if (test_bit(i, bitmask_key)) { |
145 | found = 1; |
146 | break; |
147 | } |
148 | } |
149 | } |
150 | } |
151 | |
152 | if (found > 0) { |
153 | devclass |= SDL_UDEV_DEVICE_HAS_KEYS; // ID_INPUT_KEY |
154 | } |
155 | } |
156 | |
157 | /* the first 32 bits are ESC, numbers, and Q to D, so if we have all of |
158 | * those, consider it to be a fully-featured keyboard; |
159 | * do not test KEY_RESERVED, though */ |
160 | keyboard_mask = 0xFFFFFFFE; |
161 | if ((bitmask_key[0] & keyboard_mask) == keyboard_mask) { |
162 | devclass |= SDL_UDEV_DEVICE_KEYBOARD; // ID_INPUT_KEYBOARD |
163 | } |
164 | |
165 | return devclass; |
166 | } |
167 | |
168 | #endif // HAVE_LINUX_INPUT_H |
169 | |