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