]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
d7867b31 | 2 | /* |
24447733 | 3 | * expose input properties via udev |
d7867b31 | 4 | * |
810adae9 | 5 | * Portions Copyright © 2004 David Zeuthen, <david@fubar.dk> |
810adae9 | 6 | * Copyright © 2014 Carlos Garnacho <carlosg@gnome.org> |
d7867b31 KS |
7 | */ |
8 | ||
07630cea | 9 | #include <errno.h> |
f5947a5e | 10 | #include <fcntl.h> |
07630cea | 11 | #include <stdarg.h> |
d7867b31 | 12 | #include <stdlib.h> |
07630cea | 13 | #include <unistd.h> |
d7867b31 | 14 | #include <linux/limits.h> |
d7867b31 | 15 | |
d9de38ca | 16 | #include "device-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
f5947a5e | 18 | #include "missing_input.h" |
d054f0a4 | 19 | #include "stdio-util.h" |
07630cea | 20 | #include "string-util.h" |
07a26e42 | 21 | #include "udev-builtin.h" |
24447733 | 22 | #include "util.h" |
d7867b31 KS |
23 | |
24 | /* we must use this kernel-compatible implementation */ | |
25 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) | |
26 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) | |
27 | #define OFF(x) ((x)%BITS_PER_LONG) | |
28 | #define BIT(x) (1UL<<OFF(x)) | |
29 | #define LONG(x) ((x)/BITS_PER_LONG) | |
30 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
31 | ||
f472d466 NL |
32 | struct range { |
33 | unsigned start; | |
34 | unsigned end; | |
35 | }; | |
36 | ||
37 | /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/ | |
38 | static const struct range high_key_blocks[] = { | |
39 | { KEY_OK, BTN_DPAD_UP }, | |
40 | { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY } | |
41 | }; | |
42 | ||
a1e92eee | 43 | static int abs_size_mm(const struct input_absinfo *absinfo) { |
24447733 DH |
44 | /* Resolution is defined to be in units/mm for ABS_X/Y */ |
45 | return (absinfo->maximum - absinfo->minimum) / absinfo->resolution; | |
46 | } | |
47 | ||
ff799927 | 48 | static void extract_info(sd_device *dev, const char *devpath, bool test) { |
24447733 DH |
49 | char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)]; |
50 | struct input_absinfo xabsinfo = {}, yabsinfo = {}; | |
51 | _cleanup_close_ int fd = -1; | |
52 | ||
53 | fd = open(devpath, O_RDONLY|O_CLOEXEC); | |
54 | if (fd < 0) | |
55 | return; | |
56 | ||
57 | if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 || | |
58 | ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0) | |
59 | return; | |
60 | ||
61 | if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0) | |
62 | return; | |
63 | ||
d054f0a4 DM |
64 | xsprintf(width, "%d", abs_size_mm(&xabsinfo)); |
65 | xsprintf(height, "%d", abs_size_mm(&yabsinfo)); | |
24447733 | 66 | |
ff799927 YW |
67 | udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width); |
68 | udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height); | |
24447733 DH |
69 | } |
70 | ||
d7867b31 KS |
71 | /* |
72 | * Read a capability attribute and return bitmask. | |
ff799927 | 73 | * @param dev sd_device |
d7867b31 KS |
74 | * @param attr sysfs attribute name (e. g. "capabilities/key") |
75 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
76 | */ | |
d9de38ca | 77 | static void get_cap_mask(sd_device *pdev, const char* attr, |
912541b0 | 78 | unsigned long *bitmask, size_t bitmask_size, |
9ec6e95b | 79 | bool test) { |
975a9007 | 80 | const char *v; |
912541b0 KS |
81 | char text[4096]; |
82 | unsigned i; | |
83 | char* word; | |
84 | unsigned long val; | |
85 | ||
ff799927 YW |
86 | if (sd_device_get_sysattr_value(pdev, attr, &v) < 0) |
87 | v = ""; | |
975a9007 | 88 | |
d054f0a4 | 89 | xsprintf(text, "%s", v); |
d9de38ca | 90 | log_device_debug(pdev, "%s raw kernel attribute: %s", attr, text); |
912541b0 | 91 | |
29804cc1 | 92 | memzero(bitmask, bitmask_size); |
912541b0 KS |
93 | i = 0; |
94 | while ((word = strrchr(text, ' ')) != NULL) { | |
d9de38ca YW |
95 | val = strtoul(word+1, NULL, 16); |
96 | if (i < bitmask_size / sizeof(unsigned long)) | |
912541b0 KS |
97 | bitmask[i] = val; |
98 | else | |
d9de38ca | 99 | log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
100 | *word = '\0'; |
101 | ++i; | |
102 | } | |
103 | val = strtoul (text, NULL, 16); | |
104 | if (i < bitmask_size / sizeof(unsigned long)) | |
105 | bitmask[i] = val; | |
106 | else | |
d9de38ca | 107 | log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
108 | |
109 | if (test) { | |
110 | /* printf pattern with the right unsigned long number of hex chars */ | |
d054f0a4 DM |
111 | xsprintf(text, " bit %%4u: %%0%zulX\n", |
112 | 2 * sizeof(unsigned long)); | |
d9de38ca | 113 | log_device_debug(pdev, "%s decoded bit map:", attr); |
912541b0 KS |
114 | val = bitmask_size / sizeof (unsigned long); |
115 | /* skip over leading zeros */ | |
116 | while (bitmask[val-1] == 0 && val > 0) | |
117 | --val; | |
bcfce235 LP |
118 | for (i = 0; i < val; ++i) { |
119 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
d9de38ca | 120 | log_device_debug(pdev, text, i * BITS_PER_LONG, bitmask[i]); |
bcfce235 LP |
121 | REENABLE_WARNING; |
122 | } | |
912541b0 | 123 | } |
d7867b31 KS |
124 | } |
125 | ||
126 | /* pointer devices */ | |
ff799927 | 127 | static bool test_pointers(sd_device *dev, |
b914418a PH |
128 | const unsigned long* bitmask_ev, |
129 | const unsigned long* bitmask_abs, | |
130 | const unsigned long* bitmask_key, | |
131 | const unsigned long* bitmask_rel, | |
132 | const unsigned long* bitmask_props, | |
133 | bool test) { | |
22811ad0 | 134 | int button, axis; |
15264e5a AP |
135 | bool has_abs_coordinates = false; |
136 | bool has_rel_coordinates = false; | |
fa5a113d | 137 | bool has_mt_coordinates = false; |
15264e5a | 138 | bool has_joystick_axes_or_buttons = false; |
fa5a113d | 139 | bool is_direct = false; |
15264e5a AP |
140 | bool has_touch = false; |
141 | bool has_3d_coordinates = false; | |
142 | bool has_keys = false; | |
143 | bool stylus_or_pen = false; | |
144 | bool finger_but_no_pen = false; | |
145 | bool has_mouse_button = false; | |
146 | bool is_mouse = false; | |
147 | bool is_touchpad = false; | |
148 | bool is_touchscreen = false; | |
149 | bool is_tablet = false; | |
150 | bool is_joystick = false; | |
151 | bool is_accelerometer = false; | |
152 | bool is_pointing_stick= false; | |
153 | ||
154 | has_keys = test_bit(EV_KEY, bitmask_ev); | |
155 | has_abs_coordinates = test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs); | |
156 | has_3d_coordinates = has_abs_coordinates && test_bit(ABS_Z, bitmask_abs); | |
157 | is_accelerometer = test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props); | |
158 | ||
159 | if (!has_keys && has_3d_coordinates) | |
160 | is_accelerometer = true; | |
161 | ||
162 | if (is_accelerometer) { | |
ff799927 | 163 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); |
941a2aca | 164 | return true; |
bd1acc9f HG |
165 | } |
166 | ||
15264e5a AP |
167 | is_pointing_stick = test_bit(INPUT_PROP_POINTING_STICK, bitmask_props); |
168 | stylus_or_pen = test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key); | |
169 | finger_but_no_pen = test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key); | |
22811ad0 MS |
170 | for (button = BTN_MOUSE; button < BTN_JOYSTICK && !has_mouse_button; button++) |
171 | has_mouse_button = test_bit(button, bitmask_key); | |
15264e5a | 172 | has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel); |
fa5a113d | 173 | has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs); |
495968cf AP |
174 | |
175 | /* unset has_mt_coordinates if devices claims to have all abs axis */ | |
9ed794a3 | 176 | if (has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs)) |
495968cf | 177 | has_mt_coordinates = false; |
fa5a113d | 178 | is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props); |
15264e5a | 179 | has_touch = test_bit(BTN_TOUCH, bitmask_key); |
0cd6767c | 180 | |
15264e5a AP |
181 | /* joysticks don't necessarily have buttons; e. g. |
182 | * rudders/pedals are joystick-like, but buttonless; they have | |
0cd6767c PH |
183 | * other fancy axes. Others have buttons only but no axes. |
184 | * | |
185 | * The BTN_JOYSTICK range starts after the mouse range, so a mouse | |
186 | * with more than 16 buttons runs into the joystick range (e.g. Mad | |
187 | * Catz Mad Catz M.M.O.TE). Skip those. | |
188 | */ | |
189 | if (!test_bit(BTN_JOYSTICK - 1, bitmask_key)) { | |
190 | for (button = BTN_JOYSTICK; button < BTN_DIGI && !has_joystick_axes_or_buttons; button++) | |
191 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
192 | for (button = BTN_TRIGGER_HAPPY1; button <= BTN_TRIGGER_HAPPY40 && !has_joystick_axes_or_buttons; button++) | |
193 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
194 | for (button = BTN_DPAD_UP; button <= BTN_DPAD_RIGHT && !has_joystick_axes_or_buttons; button++) | |
195 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
196 | } | |
22811ad0 MS |
197 | for (axis = ABS_RX; axis < ABS_PRESSURE && !has_joystick_axes_or_buttons; axis++) |
198 | has_joystick_axes_or_buttons = test_bit(axis, bitmask_abs); | |
15264e5a AP |
199 | |
200 | if (has_abs_coordinates) { | |
201 | if (stylus_or_pen) | |
202 | is_tablet = true; | |
fa5a113d | 203 | else if (finger_but_no_pen && !is_direct) |
15264e5a AP |
204 | is_touchpad = true; |
205 | else if (has_mouse_button) | |
912541b0 KS |
206 | /* This path is taken by VMware's USB mouse, which has |
207 | * absolute axes, but no touch/pressure button. */ | |
15264e5a | 208 | is_mouse = true; |
1a3439ef | 209 | else if (has_touch || is_direct) |
15264e5a AP |
210 | is_touchscreen = true; |
211 | else if (has_joystick_axes_or_buttons) | |
212 | is_joystick = true; | |
2e856c63 PH |
213 | } else if (has_joystick_axes_or_buttons) { |
214 | is_joystick = true; | |
912541b0 | 215 | } |
2e856c63 | 216 | |
d3a37494 AP |
217 | if (has_mt_coordinates) { |
218 | if (stylus_or_pen) | |
219 | is_tablet = true; | |
220 | else if (finger_but_no_pen && !is_direct) | |
221 | is_touchpad = true; | |
222 | else if (has_touch || is_direct) | |
223 | is_touchscreen = true; | |
224 | } | |
912541b0 | 225 | |
774ff9ba PH |
226 | if (!is_tablet && !is_touchpad && !is_joystick && |
227 | has_mouse_button && | |
0c21944e MS |
228 | (has_rel_coordinates || |
229 | !has_abs_coordinates)) /* mouse buttons and no axis */ | |
15264e5a | 230 | is_mouse = true; |
912541b0 | 231 | |
15264e5a | 232 | if (is_pointing_stick) |
ff799927 | 233 | udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1"); |
15264e5a | 234 | if (is_mouse) |
ff799927 | 235 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); |
15264e5a | 236 | if (is_touchpad) |
ff799927 | 237 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); |
15264e5a | 238 | if (is_touchscreen) |
ff799927 | 239 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); |
15264e5a | 240 | if (is_joystick) |
ff799927 | 241 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); |
15264e5a | 242 | if (is_tablet) |
ff799927 | 243 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); |
15264e5a AP |
244 | |
245 | return is_tablet || is_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick; | |
d7867b31 KS |
246 | } |
247 | ||
248 | /* key like devices */ | |
ff799927 | 249 | static bool test_key(sd_device *dev, |
b914418a PH |
250 | const unsigned long* bitmask_ev, |
251 | const unsigned long* bitmask_key, | |
252 | bool test) { | |
912541b0 KS |
253 | unsigned i; |
254 | unsigned long found; | |
255 | unsigned long mask; | |
941a2aca | 256 | bool ret = false; |
912541b0 KS |
257 | |
258 | /* do we have any KEY_* capability? */ | |
b914418a | 259 | if (!test_bit(EV_KEY, bitmask_ev)) { |
d9de38ca | 260 | log_device_debug(dev, "test_key: no EV_KEY capability"); |
941a2aca | 261 | return false; |
912541b0 KS |
262 | } |
263 | ||
264 | /* only consider KEY_* here, not BTN_* */ | |
265 | found = 0; | |
266 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
267 | found |= bitmask_key[i]; | |
d9de38ca | 268 | log_device_debug(dev, "test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); |
912541b0 | 269 | } |
f472d466 | 270 | /* If there are no keys in the lower block, check the higher blocks */ |
912541b0 | 271 | if (!found) { |
f472d466 NL |
272 | unsigned block; |
273 | for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { | |
274 | for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { | |
275 | if (test_bit(i, bitmask_key)) { | |
d9de38ca | 276 | log_device_debug(dev, "test_key: Found key %x in high block", i); |
f472d466 NL |
277 | found = 1; |
278 | break; | |
279 | } | |
912541b0 KS |
280 | } |
281 | } | |
282 | } | |
283 | ||
941a2aca | 284 | if (found > 0) { |
ff799927 | 285 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
941a2aca HG |
286 | ret = true; |
287 | } | |
912541b0 KS |
288 | |
289 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
290 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
291 | mask = 0xFFFFFFFE; | |
d94a24ca | 292 | if (FLAGS_SET(bitmask_key[0], mask)) { |
ff799927 | 293 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); |
941a2aca HG |
294 | ret = true; |
295 | } | |
296 | ||
297 | return ret; | |
d7867b31 KS |
298 | } |
299 | ||
3fc2e9a2 YW |
300 | static int builtin_input_id(sd_device *dev, int argc, char *argv[], bool test) { |
301 | sd_device *pdev; | |
912541b0 KS |
302 | unsigned long bitmask_ev[NBITS(EV_MAX)]; |
303 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
304 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
305 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
606df97b | 306 | unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)]; |
24447733 | 307 | const char *sysname, *devnode; |
37186823 HG |
308 | bool is_pointer; |
309 | bool is_key; | |
912541b0 | 310 | |
3b64e4d4 TG |
311 | assert(dev); |
312 | ||
912541b0 KS |
313 | /* walk up the parental chain until we find the real input device; the |
314 | * argument is very likely a subdevice of this, like eventN */ | |
ff799927 YW |
315 | for (pdev = dev; pdev; ) { |
316 | const char *s; | |
317 | ||
318 | if (sd_device_get_sysattr_value(pdev, "capabilities/ev", &s) >= 0) | |
319 | break; | |
320 | ||
321 | if (sd_device_get_parent_with_subsystem_devtype(pdev, "input", NULL, &pdev) >= 0) | |
322 | continue; | |
323 | ||
324 | pdev = NULL; | |
325 | break; | |
326 | } | |
912541b0 | 327 | |
24447733 DH |
328 | if (pdev) { |
329 | /* Use this as a flag that input devices were detected, so that this | |
330 | * program doesn't need to be called more than once per device */ | |
ff799927 | 331 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); |
d9de38ca YW |
332 | get_cap_mask(pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); |
333 | get_cap_mask(pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
334 | get_cap_mask(pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
335 | get_cap_mask(pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
336 | get_cap_mask(pdev, "properties", bitmask_props, sizeof(bitmask_props), test); | |
37186823 HG |
337 | is_pointer = test_pointers(dev, bitmask_ev, bitmask_abs, |
338 | bitmask_key, bitmask_rel, | |
339 | bitmask_props, test); | |
340 | is_key = test_key(dev, bitmask_ev, bitmask_key, test); | |
341 | /* Some evdev nodes have only a scrollwheel */ | |
342 | if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) && | |
343 | (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel))) | |
ff799927 | 344 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
64083a60 | 345 | if (test_bit(EV_SW, bitmask_ev)) |
ff799927 | 346 | udev_builtin_add_property(dev, test, "ID_INPUT_SWITCH", "1"); |
64083a60 | 347 | |
24447733 DH |
348 | } |
349 | ||
ff799927 YW |
350 | if (sd_device_get_devname(dev, &devnode) >= 0 && |
351 | sd_device_get_sysname(dev, &sysname) >= 0 && | |
352 | startswith(sysname, "event")) | |
24447733 DH |
353 | extract_info(dev, devnode, test); |
354 | ||
d354690e | 355 | return 0; |
d7867b31 KS |
356 | } |
357 | ||
25de7aa7 | 358 | const UdevBuiltin udev_builtin_input_id = { |
912541b0 KS |
359 | .name = "input_id", |
360 | .cmd = builtin_input_id, | |
5ac0162c | 361 | .help = "Input device properties", |
d7867b31 | 362 | }; |