Debian Codegraph @

src/udev/udev-builtin-input_id.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * expose input properties via udev
4 *
5 * Portions Copyright © 2004 David Zeuthen, <[email protected]>
6 * Copyright © 2014 Carlos Garnacho <[email protected]>
7 */
8
9 #include <fcntl.h>
10 #include <linux/input.h>
11
12 #include "device-private.h"
13 #include "device-util.h"
14 #include "fd-util.h"
15 #include "parse-util.h"
16 #include "stdio-util.h"
17 #include "string-util.h"
18 #include "udev-builtin.h"
19
20 /* we must use this kernel-compatible implementation */
21 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
22 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
23 #define OFF(x) ((x)%BITS_PER_LONG)
24 #define BIT(x) (1UL<<OFF(x))
25 #define LONG(x) ((x)/BITS_PER_LONG)
26 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
27
28 struct range {
29 unsigned start;
30 unsigned end;
31 };
32
33 /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive) */
34 static const struct range high_key_blocks[] = {
35 { KEY_OK, BTN_DPAD_UP },
36 { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY }
37 };
38
39 static int abs_size_mm(const struct input_absinfo *absinfo) {
40 /* Resolution is defined to be in units/mm for ABS_X/Y */
41 return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
42 }
43
44 static void extract_info(UdevEvent *event) {
45 char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
46 struct input_absinfo xabsinfo = {}, yabsinfo = {};
47 _cleanup_close_ int fd = -EBADF;
48
49 assert(event);
50 assert(event->dev);
51
52 fd = sd_device_open(event->dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
53 if (fd < 0)
54 return;
55
56 if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
57 ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
58 return;
59
60 if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
61 return;
62
63 xsprintf(width, "%d", abs_size_mm(&xabsinfo));
64 xsprintf(height, "%d", abs_size_mm(&yabsinfo));
65
66 udev_builtin_add_property(event, "ID_INPUT_WIDTH_MM", width);
67 udev_builtin_add_property(event, "ID_INPUT_HEIGHT_MM", height);
68 }
69
70 /*
71 * Read a capability attribute and return bitmask.
72 * @param dev sd_device
73 * @param attr sysfs attribute name (e. g. "capabilities/key")
74 * @param bitmask: Output array which has a sizeof of bitmask_size
75 */
76 static void get_cap_mask(
77 sd_device *pdev,
78 const char *attr,
79 unsigned long *bitmask,
80 size_t bitmask_size,
81 EventMode mode) {
82
83 const char *v;
84 char text[4096], *word;
85 unsigned i;
86 unsigned long val;
87 int r;
88
89 if (device_get_sysattr_safe_string(pdev, attr, &v) < 0)
90 v = "";
91
92 xsprintf(text, "%s", v);
93 log_device_debug(pdev, "%s raw kernel attribute: %s", attr, text);
94
95 memzero(bitmask, bitmask_size);
96 i = 0;
97 while ((word = strrchr(text, ' '))) {
98 r = safe_atolu_full(word+1, 16, &val);
99 if (r < 0)
100 log_device_debug_errno(pdev, r, "Ignoring %s block which failed to parse: %m", attr);
101 else if (i < bitmask_size / sizeof(unsigned long))
102 bitmask[i] = val;
103 else
104 log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val);
105 *word = '\0';
106 i++;
107 }
108 r = safe_atolu_full(text, 16, &val);
109 if (r < 0)
110 log_device_debug_errno(pdev, r, "Ignoring %s block which failed to parse: %m", attr);
111 else if (i < bitmask_size / sizeof(unsigned long))
112 bitmask[i] = val;
113 else
114 log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val);
115
116 if (mode == EVENT_UDEVADM_TEST_BUILTIN && DEBUG_LOGGING) {
117 log_device_debug(pdev, "%s decoded bit map:", attr);
118
119 val = bitmask_size / sizeof (unsigned long);
120 /* skip trailing zeros */
121 while (bitmask[val-1] == 0 && val > 0)
122 --val;
123
124 /* IN_SET() cannot be used in assert_cc(). */
125 assert_cc(sizeof(unsigned long) == 4 || sizeof(unsigned long) == 8);
126 for (unsigned long j = 0; j < val; j++)
127 log_device_debug(pdev,
128 sizeof(unsigned long) == 4 ? " bit %4lu: %08lX\n" : " bit %4lu: %016lX\n",
129 j * BITS_PER_LONG, bitmask[j]);
130 }
131 }
132
133 static struct input_id get_input_id(sd_device *dev) {
134 struct input_id id = {};
135
136 assert(dev);
137
138 (void) device_get_sysattr_u16_full(dev, "id/bustype", 16, &id.bustype);
139 (void) device_get_sysattr_u16_full(dev, "id/vendor", 16, &id.vendor);
140 (void) device_get_sysattr_u16_full(dev, "id/product", 16, &id.product);
141 (void) device_get_sysattr_u16_full(dev, "id/version", 16, &id.version);
142
143 return id;
144 }
145
146 /* pointer devices */
147 static bool test_pointers(
148 UdevEvent *event,
149 const struct input_id *id,
150 const unsigned long *bitmask_ev,
151 const unsigned long *bitmask_abs,
152 const unsigned long *bitmask_key,
153 const unsigned long *bitmask_rel,
154 const unsigned long *bitmask_props) {
155
156 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
157 size_t num_joystick_axes = 0, num_joystick_buttons = 0;
158 bool has_abs_coordinates = false,
159 has_rel_coordinates = false,
160 has_mt_coordinates = false,
161 has_pad_buttons = false,
162 is_direct = false,
163 has_touch = false,
164 has_3d_coordinates = false,
165 has_keys = false,
166 has_stylus = false,
167 has_pen = false,
168 finger_but_no_pen = false,
169 has_mouse_button = false,
170 is_mouse = false,
171 is_abs_mouse = false,
172 is_touchpad = false,
173 is_touchscreen = false,
174 is_tablet = false,
175 is_tablet_pad = false,
176 is_joystick = false,
177 is_accelerometer = false,
178 is_pointing_stick = false,
179 has_wheel = false;
180
181 has_keys = test_bit(EV_KEY, bitmask_ev);
182 has_abs_coordinates = test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs);
183 has_3d_coordinates = has_abs_coordinates && test_bit(ABS_Z, bitmask_abs);
184 is_accelerometer = test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props);
185
186 if (!has_keys && has_3d_coordinates)
187 is_accelerometer = true;
188
189 if (is_accelerometer) {
190 udev_builtin_add_property(event, "ID_INPUT_ACCELEROMETER", "1");
191 return true;
192 }
193
194 is_pointing_stick = test_bit(INPUT_PROP_POINTING_STICK, bitmask_props);
195 has_stylus = test_bit(BTN_STYLUS, bitmask_key);
196 has_pen = test_bit(BTN_TOOL_PEN, bitmask_key);
197 finger_but_no_pen = test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key);
198 for (int button = BTN_MOUSE; button < BTN_JOYSTICK && !has_mouse_button; button++)
199 has_mouse_button = test_bit(button, bitmask_key);
200 has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel);
201 has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs);
202
203 /* unset has_mt_coordinates if devices claims to have all abs axis */
204 if (has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs))
205 has_mt_coordinates = false;
206 is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props);
207 has_touch = test_bit(BTN_TOUCH, bitmask_key);
208 has_pad_buttons = test_bit(BTN_0, bitmask_key) && test_bit(BTN_1, bitmask_key) && !has_pen;
209 has_wheel = test_bit(EV_REL, bitmask_ev) && (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel));
210
211 /* joysticks don't necessarily have buttons; e. g.
212 * rudders/pedals are joystick-like, but buttonless; they have
213 * other fancy axes. Others have buttons only but no axes.
214 *
215 * The BTN_JOYSTICK range starts after the mouse range, so a mouse
216 * with more than 16 buttons runs into the joystick range (e.g. Mad
217 * Catz Mad Catz M.M.O.TE). Skip those.
218 */
219 if (!test_bit(BTN_JOYSTICK - 1, bitmask_key)) {
220 for (int button = BTN_JOYSTICK; button < BTN_DIGI; button++)
221 if (test_bit(button, bitmask_key))
222 num_joystick_buttons++;
223 for (int button = BTN_TRIGGER_HAPPY1; button <= BTN_TRIGGER_HAPPY40; button++)
224 if (test_bit(button, bitmask_key))
225 num_joystick_buttons++;
226 for (int button = BTN_DPAD_UP; button <= BTN_DPAD_RIGHT; button++)
227 if (test_bit(button, bitmask_key))
228 num_joystick_buttons++;
229 }
230 for (int axis = ABS_RX; axis < ABS_PRESSURE; axis++)
231 if (test_bit(axis, bitmask_abs))
232 num_joystick_axes++;
233
234 if (has_abs_coordinates) {
235 if (has_stylus || has_pen)
236 is_tablet = true;
237 else if (finger_but_no_pen && !is_direct)
238 is_touchpad = true;
239 else if (has_mouse_button)
240 /* This path is taken by VMware's USB mouse, which has
241 * absolute axes, but no touch/pressure button. */
242 is_abs_mouse = true;
243 else if (has_touch || is_direct)
244 is_touchscreen = true;
245 else if (num_joystick_buttons > 0 || num_joystick_axes > 0)
246 is_joystick = true;
247 } else if (num_joystick_buttons > 0 || num_joystick_axes > 0)
248 is_joystick = true;
249
250 if (has_mt_coordinates) {
251 if (has_stylus || has_pen)
252 is_tablet = true;
253 else if (finger_but_no_pen && !is_direct)
254 is_touchpad = true;
255 else if (has_touch || is_direct)
256 is_touchscreen = true;
257 }
258
259 if (is_tablet && has_pad_buttons)
260 is_tablet_pad = true;
261
262 if (has_pad_buttons && has_wheel && !has_rel_coordinates) {
263 is_tablet = true;
264 is_tablet_pad = true;
265 }
266
267 if (!is_tablet && !is_touchpad && !is_joystick &&
268 has_mouse_button &&
269 (has_rel_coordinates ||
270 !has_abs_coordinates)) /* mouse buttons and no axis */
271 is_mouse = true;
272
273 /* There is no such thing as an i2c mouse */
274 if (is_mouse && id->bustype == BUS_I2C)
275 is_pointing_stick = true;
276
277 /* Joystick un-detection. Some keyboards have random joystick buttons
278 * set. Avoid those being labeled as ID_INPUT_JOYSTICK with some heuristics.
279 * The well-known keys represent a (randomly picked) set of key groups.
280 * A joystick may have one of those but probably not several. And a joystick with less than 2 buttons
281 * or axes is not a joystick either.
282 * libinput uses similar heuristics, any changes here should be added to libinput too.
283 */
284 if (is_joystick) {
285 static const unsigned well_known_keyboard_keys[] = {
286 KEY_LEFTCTRL, KEY_CAPSLOCK, KEY_NUMLOCK, KEY_INSERT,
287 KEY_MUTE, KEY_CALC, KEY_FILE, KEY_MAIL, KEY_PLAYPAUSE,
288 KEY_BRIGHTNESSDOWN,
289 };
290 size_t num_well_known_keys = 0;
291
292 if (has_keys)
293 FOREACH_ELEMENT(key, well_known_keyboard_keys)
294 if (test_bit(*key, bitmask_key))
295 num_well_known_keys++;
296
297 if (num_well_known_keys >= 4 || num_joystick_buttons + num_joystick_axes < 2) {
298 log_device_debug(dev, "Input device has %zu joystick buttons and %zu axes but also %zu keyboard key sets, "
299 "assuming this is a keyboard, not a joystick.",
300 num_joystick_buttons, num_joystick_axes, num_well_known_keys);
301 is_joystick = false;
302 }
303
304 if (has_wheel && has_pad_buttons) {
305 log_device_debug(dev, "Input device has %zu joystick buttons as well as tablet pad buttons, "
306 "assuming this is a tablet pad, not a joystick.", num_joystick_buttons);
307
308 is_joystick = false;
309 }
310 }
311
312 if (is_pointing_stick)
313 udev_builtin_add_property(event, "ID_INPUT_POINTINGSTICK", "1");
314 if (is_mouse || is_abs_mouse)
315 udev_builtin_add_property(event, "ID_INPUT_MOUSE", "1");
316 if (is_touchpad)
317 udev_builtin_add_property(event, "ID_INPUT_TOUCHPAD", "1");
318 if (is_touchscreen)
319 udev_builtin_add_property(event, "ID_INPUT_TOUCHSCREEN", "1");
320 if (is_joystick)
321 udev_builtin_add_property(event, "ID_INPUT_JOYSTICK", "1");
322 if (is_tablet)
323 udev_builtin_add_property(event, "ID_INPUT_TABLET", "1");
324 if (is_tablet_pad)
325 udev_builtin_add_property(event, "ID_INPUT_TABLET_PAD", "1");
326
327 return is_tablet || is_mouse || is_abs_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick;
328 }
329
330 /* key like devices */
331 static bool test_key(
332 UdevEvent *event,
333 const unsigned long *bitmask_ev,
334 const unsigned long *bitmask_key) {
335
336 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
337 bool found = false;
338
339 /* do we have any KEY_* capability? */
340 if (!test_bit(EV_KEY, bitmask_ev)) {
341 log_device_debug(dev, "test_key: no EV_KEY capability");
342 return false;
343 }
344
345 /* only consider KEY_* here, not BTN_* */
346 for (size_t i = 0; i < BTN_MISC/BITS_PER_LONG && !found; i++) {
347 if (bitmask_key[i])
348 found = true;
349
350 log_device_debug(dev, "test_key: checking bit block %zu for any keys; found=%s",
351 i * BITS_PER_LONG, yes_no(found));
352 }
353 /* If there are no keys in the lower block, check the higher blocks */
354 for (size_t block = 0; block < ELEMENTSOF(high_key_blocks) && !found; block++)
355 for (unsigned i = high_key_blocks[block].start; i < high_key_blocks[block].end && !found; i++)
356 if (test_bit(i, bitmask_key)) {
357 log_device_debug(dev, "test_key: Found key %x in high block", i);
358 found = true;
359 }
360
361 if (found)
362 udev_builtin_add_property(event, "ID_INPUT_KEY", "1");
363
364 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
365 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
366 if (FLAGS_SET(bitmask_key[0], 0xFFFFFFFE)) {
367 udev_builtin_add_property(event, "ID_INPUT_KEYBOARD", "1");
368 return true;
369 }
370
371 return found;
372 }
373
374 static int builtin_input_id(UdevEvent *event, int argc, char *argv[]) {
375 sd_device *pdev, *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
376 unsigned long bitmask_ev[NBITS(EV_MAX)],
377 bitmask_abs[NBITS(ABS_MAX)],
378 bitmask_key[NBITS(KEY_MAX)],
379 bitmask_rel[NBITS(REL_MAX)],
380 bitmask_props[NBITS(INPUT_PROP_MAX)];
381 bool is_pointer, is_key;
382 int r;
383
384 /* walk up the parental chain until we find the real input device; the
385 * argument is very likely a subdevice of this, like eventN */
386 for (pdev = dev; pdev; ) {
387 if (sd_device_get_sysattr_value(pdev, "capabilities/ev", /* ret= */ NULL) >= 0)
388 break;
389
390 if (sd_device_get_parent_with_subsystem_devtype(pdev, "input", NULL, &pdev) >= 0)
391 continue;
392
393 pdev = NULL;
394 break;
395 }
396
397 if (pdev) {
398 struct input_id id = get_input_id(pdev);
399
400 /* Use this as a flag that input devices were detected, so that this
401 * program doesn't need to be called more than once per device */
402 udev_builtin_add_property(event, "ID_INPUT", "1");
403 get_cap_mask(pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), event->event_mode);
404 get_cap_mask(pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), event->event_mode);
405 get_cap_mask(pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), event->event_mode);
406 get_cap_mask(pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), event->event_mode);
407 get_cap_mask(pdev, "properties", bitmask_props, sizeof(bitmask_props), event->event_mode);
408 is_pointer = test_pointers(event, &id, bitmask_ev, bitmask_abs,
409 bitmask_key, bitmask_rel,
410 bitmask_props);
411 is_key = test_key(event, bitmask_ev, bitmask_key);
412 /* Some evdev nodes have only a scrollwheel */
413 if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) &&
414 (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel)))
415 udev_builtin_add_property(event, "ID_INPUT_KEY", "1");
416 if (test_bit(EV_SW, bitmask_ev))
417 udev_builtin_add_property(event, "ID_INPUT_SWITCH", "1");
418 }
419
420 r = device_sysname_startswith(dev, "event");
421 if (r < 0)
422 return r;
423 if (r > 0)
424 extract_info(event);
425
426 return 0;
427 }
428
429 const UdevBuiltin udev_builtin_input_id = {
430 .name = "input_id",
431 .cmd = builtin_input_id,
432 .help = "Input device properties",
433 };