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