1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2021 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 | |
23 | #include "SDL_evdev_capabilities.h" |
24 | |
25 | #if HAVE_LIBUDEV_H || defined(SDL_JOYSTICK_LINUX) |
26 | |
27 | /* missing defines in older Linux kernel headers */ |
28 | #ifndef BTN_TRIGGER_HAPPY |
29 | #define BTN_TRIGGER_HAPPY 0x2c0 |
30 | #endif |
31 | #ifndef BTN_DPAD_UP |
32 | #define BTN_DPAD_UP 0x220 |
33 | #endif |
34 | #ifndef KEY_ALS_TOGGLE |
35 | #define KEY_ALS_TOGGLE 0x230 |
36 | #endif |
37 | |
38 | extern int |
39 | SDL_EVDEV_GuessDeviceClass(unsigned long bitmask_ev[NBITS(EV_MAX)], |
40 | unsigned long bitmask_abs[NBITS(ABS_MAX)], |
41 | unsigned long bitmask_key[NBITS(KEY_MAX)], |
42 | unsigned long bitmask_rel[NBITS(REL_MAX)]) |
43 | { |
44 | struct range { |
45 | unsigned start; |
46 | unsigned end; |
47 | }; |
48 | |
49 | /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/ |
50 | static const struct range high_key_blocks[] = { |
51 | { KEY_OK, BTN_DPAD_UP }, |
52 | { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY } |
53 | }; |
54 | |
55 | int devclass = 0; |
56 | unsigned long keyboard_mask; |
57 | |
58 | /* X, Y, Z axes but no buttons probably means an accelerometer */ |
59 | if (test_bit(EV_ABS, bitmask_ev) && |
60 | test_bit(ABS_X, bitmask_abs) && |
61 | test_bit(ABS_Y, bitmask_abs) && |
62 | test_bit(ABS_Z, bitmask_abs) && |
63 | !test_bit(EV_KEY, bitmask_ev)) { |
64 | return SDL_UDEV_DEVICE_ACCELEROMETER; |
65 | } |
66 | |
67 | /* RX, RY, RZ axes but no buttons also probably means an accelerometer */ |
68 | if (test_bit(EV_ABS, bitmask_ev) && |
69 | test_bit(ABS_RX, bitmask_abs) && |
70 | test_bit(ABS_RY, bitmask_abs) && |
71 | test_bit(ABS_RZ, bitmask_abs) && |
72 | !test_bit(EV_KEY, bitmask_ev)) { |
73 | return SDL_UDEV_DEVICE_ACCELEROMETER; |
74 | } |
75 | |
76 | if (test_bit(EV_ABS, bitmask_ev) && |
77 | test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) { |
78 | if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) { |
79 | ; /* ID_INPUT_TABLET */ |
80 | } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) { |
81 | ; /* ID_INPUT_TOUCHPAD */ |
82 | } else if (test_bit(BTN_MOUSE, bitmask_key)) { |
83 | devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */ |
84 | } else if (test_bit(BTN_TOUCH, bitmask_key)) { |
85 | /* TODO: better determining between touchscreen and multitouch touchpad, |
86 | see https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-input_id.c */ |
87 | devclass |= SDL_UDEV_DEVICE_TOUCHSCREEN; /* ID_INPUT_TOUCHSCREEN */ |
88 | } |
89 | |
90 | if (test_bit(BTN_TRIGGER, bitmask_key) || |
91 | test_bit(BTN_A, bitmask_key) || |
92 | test_bit(BTN_1, bitmask_key) || |
93 | test_bit(ABS_RX, bitmask_abs) || |
94 | test_bit(ABS_RY, bitmask_abs) || |
95 | test_bit(ABS_RZ, bitmask_abs) || |
96 | test_bit(ABS_THROTTLE, bitmask_abs) || |
97 | test_bit(ABS_RUDDER, bitmask_abs) || |
98 | test_bit(ABS_WHEEL, bitmask_abs) || |
99 | test_bit(ABS_GAS, bitmask_abs) || |
100 | test_bit(ABS_BRAKE, bitmask_abs)) { |
101 | devclass |= SDL_UDEV_DEVICE_JOYSTICK; /* ID_INPUT_JOYSTICK */ |
102 | } |
103 | } |
104 | |
105 | if (test_bit(EV_REL, bitmask_ev) && |
106 | test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) && |
107 | test_bit(BTN_MOUSE, bitmask_key)) { |
108 | devclass |= SDL_UDEV_DEVICE_MOUSE; /* ID_INPUT_MOUSE */ |
109 | } |
110 | |
111 | if (test_bit(EV_KEY, bitmask_ev)) { |
112 | unsigned i; |
113 | unsigned long found = 0; |
114 | |
115 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { |
116 | found |= bitmask_key[i]; |
117 | } |
118 | /* If there are no keys in the lower block, check the higher blocks */ |
119 | if (!found) { |
120 | unsigned block; |
121 | for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { |
122 | for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { |
123 | if (test_bit(i, bitmask_key)) { |
124 | found = 1; |
125 | break; |
126 | } |
127 | } |
128 | } |
129 | } |
130 | |
131 | if (found > 0) { |
132 | devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEY */ |
133 | } |
134 | } |
135 | |
136 | /* the first 32 bits are ESC, numbers, and Q to D; if we have any of |
137 | * those, consider it a keyboard device; do not test KEY_RESERVED, though */ |
138 | keyboard_mask = 0xFFFFFFFE; |
139 | if ((bitmask_key[0] & keyboard_mask) != 0) |
140 | devclass |= SDL_UDEV_DEVICE_KEYBOARD; /* ID_INPUT_KEYBOARD */ |
141 | |
142 | return devclass; |
143 | } |
144 | |
145 | #endif |
146 | |