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