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