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