]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-input_id.c
rules: move input_id rule to a separate file
[thirdparty/systemd.git] / src / udev / udev-builtin-input_id.c
CommitLineData
d7867b31 1/*
24447733 2 * expose input properties via udev
d7867b31
KS
3 *
4 * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
5 * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
1298001e 6 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
24447733 7 * Copyright (C) 2014 Carlos Garnacho <carlosg@gnome.org>
975a9007 8 * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
d7867b31
KS
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
07630cea
LP
24#include <errno.h>
25#include <stdarg.h>
d7867b31
KS
26#include <stdio.h>
27#include <stdlib.h>
d7867b31 28#include <string.h>
07630cea 29#include <unistd.h>
d7867b31
KS
30#include <linux/limits.h>
31#include <linux/input.h>
32
3ffd4af2 33#include "fd-util.h"
d054f0a4 34#include "stdio-util.h"
07630cea 35#include "string-util.h"
d7867b31 36#include "udev.h"
24447733 37#include "util.h"
d7867b31
KS
38
39/* we must use this kernel-compatible implementation */
40#define BITS_PER_LONG (sizeof(unsigned long) * 8)
41#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
42#define OFF(x) ((x)%BITS_PER_LONG)
43#define BIT(x) (1UL<<OFF(x))
44#define LONG(x) ((x)/BITS_PER_LONG)
45#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
46
f472d466
NL
47/* available as of kernel 3.11 */
48#ifndef BTN_DPAD_UP
49#define BTN_DPAD_UP 0x220
50#endif /* BTN_DPAD_UP */
51
52/* available as of kernel 3.13 */
53#ifndef KEY_ALS_TOGGLE
54#define KEY_ALS_TOGGLE 0x230
55#endif /* KEY_ALS_TOGGLE */
56
57struct range {
58 unsigned start;
59 unsigned end;
60};
61
62/* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/
63static const struct range high_key_blocks[] = {
64 { KEY_OK, BTN_DPAD_UP },
65 { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY }
66};
67
24447733
DH
68static inline int abs_size_mm(const struct input_absinfo *absinfo) {
69 /* Resolution is defined to be in units/mm for ABS_X/Y */
70 return (absinfo->maximum - absinfo->minimum) / absinfo->resolution;
71}
72
73static void extract_info(struct udev_device *dev, const char *devpath, bool test) {
74 char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)];
75 struct input_absinfo xabsinfo = {}, yabsinfo = {};
76 _cleanup_close_ int fd = -1;
77
78 fd = open(devpath, O_RDONLY|O_CLOEXEC);
79 if (fd < 0)
80 return;
81
82 if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
83 ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
84 return;
85
86 if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
87 return;
88
d054f0a4
DM
89 xsprintf(width, "%d", abs_size_mm(&xabsinfo));
90 xsprintf(height, "%d", abs_size_mm(&yabsinfo));
24447733
DH
91
92 udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
93 udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
94}
95
d7867b31
KS
96/*
97 * Read a capability attribute and return bitmask.
98 * @param dev udev_device
99 * @param attr sysfs attribute name (e. g. "capabilities/key")
100 * @param bitmask: Output array which has a sizeof of bitmask_size
101 */
102static void get_cap_mask(struct udev_device *dev,
912541b0
KS
103 struct udev_device *pdev, const char* attr,
104 unsigned long *bitmask, size_t bitmask_size,
9ec6e95b 105 bool test) {
975a9007 106 const char *v;
912541b0
KS
107 char text[4096];
108 unsigned i;
109 char* word;
110 unsigned long val;
111
975a9007
DH
112 v = udev_device_get_sysattr_value(pdev, attr);
113 if (!v)
114 v = "";
115
d054f0a4 116 xsprintf(text, "%s", v);
9f6445e3 117 log_debug("%s raw kernel attribute: %s", attr, text);
912541b0 118
29804cc1 119 memzero(bitmask, bitmask_size);
912541b0
KS
120 i = 0;
121 while ((word = strrchr(text, ' ')) != NULL) {
122 val = strtoul (word+1, NULL, 16);
123 if (i < bitmask_size/sizeof(unsigned long))
124 bitmask[i] = val;
125 else
9f6445e3 126 log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
912541b0
KS
127 *word = '\0';
128 ++i;
129 }
130 val = strtoul (text, NULL, 16);
131 if (i < bitmask_size / sizeof(unsigned long))
132 bitmask[i] = val;
133 else
9f6445e3 134 log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
912541b0
KS
135
136 if (test) {
137 /* printf pattern with the right unsigned long number of hex chars */
d054f0a4
DM
138 xsprintf(text, " bit %%4u: %%0%zulX\n",
139 2 * sizeof(unsigned long));
9f6445e3 140 log_debug("%s decoded bit map:", attr);
912541b0
KS
141 val = bitmask_size / sizeof (unsigned long);
142 /* skip over leading zeros */
143 while (bitmask[val-1] == 0 && val > 0)
144 --val;
bcfce235
LP
145 for (i = 0; i < val; ++i) {
146 DISABLE_WARNING_FORMAT_NONLITERAL;
baa30fbc 147 log_debug(text, i * BITS_PER_LONG, bitmask[i]);
bcfce235
LP
148 REENABLE_WARNING;
149 }
912541b0 150 }
d7867b31
KS
151}
152
153/* pointer devices */
941a2aca 154static bool test_pointers(struct udev_device *dev,
b914418a
PH
155 const unsigned long* bitmask_ev,
156 const unsigned long* bitmask_abs,
157 const unsigned long* bitmask_key,
158 const unsigned long* bitmask_rel,
159 const unsigned long* bitmask_props,
160 bool test) {
15264e5a
AP
161 bool has_abs_coordinates = false;
162 bool has_rel_coordinates = false;
fa5a113d 163 bool has_mt_coordinates = false;
15264e5a 164 bool has_joystick_axes_or_buttons = false;
fa5a113d 165 bool is_direct = false;
15264e5a
AP
166 bool has_touch = false;
167 bool has_3d_coordinates = false;
168 bool has_keys = false;
169 bool stylus_or_pen = false;
170 bool finger_but_no_pen = false;
171 bool has_mouse_button = false;
172 bool is_mouse = false;
173 bool is_touchpad = false;
174 bool is_touchscreen = false;
175 bool is_tablet = false;
176 bool is_joystick = false;
177 bool is_accelerometer = false;
178 bool is_pointing_stick= false;
179
180 has_keys = test_bit(EV_KEY, bitmask_ev);
181 has_abs_coordinates = test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs);
182 has_3d_coordinates = has_abs_coordinates && test_bit(ABS_Z, bitmask_abs);
183 is_accelerometer = test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props);
184
185 if (!has_keys && has_3d_coordinates)
186 is_accelerometer = true;
187
188 if (is_accelerometer) {
bd1acc9f 189 udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
941a2aca 190 return true;
bd1acc9f
HG
191 }
192
15264e5a
AP
193 is_pointing_stick = test_bit(INPUT_PROP_POINTING_STICK, bitmask_props);
194 stylus_or_pen = test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key);
195 finger_but_no_pen = test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key);
196 has_mouse_button = test_bit(BTN_LEFT, bitmask_key);
197 has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel);
fa5a113d 198 has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs);
495968cf
AP
199
200 /* unset has_mt_coordinates if devices claims to have all abs axis */
9ed794a3 201 if (has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs))
495968cf 202 has_mt_coordinates = false;
fa5a113d 203 is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props);
15264e5a
AP
204 has_touch = test_bit(BTN_TOUCH, bitmask_key);
205 /* joysticks don't necessarily have buttons; e. g.
206 * rudders/pedals are joystick-like, but buttonless; they have
2e856c63 207 * other fancy axes. Others have buttons only but no axes. */
15264e5a 208 has_joystick_axes_or_buttons = test_bit(BTN_TRIGGER, bitmask_key) ||
c874fed3 209 test_bit(BTN_TRIGGER_HAPPY, bitmask_key) ||
15264e5a
AP
210 test_bit(BTN_A, bitmask_key) ||
211 test_bit(BTN_1, bitmask_key) ||
212 test_bit(ABS_RX, bitmask_abs) ||
213 test_bit(ABS_RY, bitmask_abs) ||
214 test_bit(ABS_RZ, bitmask_abs) ||
215 test_bit(ABS_THROTTLE, bitmask_abs) ||
216 test_bit(ABS_RUDDER, bitmask_abs) ||
217 test_bit(ABS_WHEEL, bitmask_abs) ||
218 test_bit(ABS_GAS, bitmask_abs) ||
219 test_bit(ABS_BRAKE, bitmask_abs);
220
221 if (has_abs_coordinates) {
222 if (stylus_or_pen)
223 is_tablet = true;
fa5a113d 224 else if (finger_but_no_pen && !is_direct)
15264e5a
AP
225 is_touchpad = true;
226 else if (has_mouse_button)
912541b0
KS
227 /* This path is taken by VMware's USB mouse, which has
228 * absolute axes, but no touch/pressure button. */
15264e5a 229 is_mouse = true;
1a3439ef 230 else if (has_touch || is_direct)
15264e5a
AP
231 is_touchscreen = true;
232 else if (has_joystick_axes_or_buttons)
233 is_joystick = true;
2e856c63
PH
234 } else if (has_joystick_axes_or_buttons) {
235 is_joystick = true;
912541b0 236 }
2e856c63 237
d3a37494
AP
238 if (has_mt_coordinates) {
239 if (stylus_or_pen)
240 is_tablet = true;
241 else if (finger_but_no_pen && !is_direct)
242 is_touchpad = true;
243 else if (has_touch || is_direct)
244 is_touchscreen = true;
245 }
912541b0 246
15264e5a
AP
247 if (has_rel_coordinates && has_mouse_button)
248 is_mouse = true;
912541b0 249
15264e5a
AP
250 if (is_pointing_stick)
251 udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1");
252 if (is_mouse)
912541b0 253 udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
15264e5a 254 if (is_touchpad)
912541b0 255 udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
15264e5a
AP
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;
d7867b31
KS
264}
265
266/* key like devices */
941a2aca 267static bool test_key(struct udev_device *dev,
b914418a
PH
268 const unsigned long* bitmask_ev,
269 const unsigned long* bitmask_key,
270 bool test) {
912541b0
KS
271 unsigned i;
272 unsigned long found;
273 unsigned long mask;
941a2aca 274 bool ret = false;
912541b0
KS
275
276 /* do we have any KEY_* capability? */
b914418a 277 if (!test_bit(EV_KEY, bitmask_ev)) {
9f6445e3 278 log_debug("test_key: no EV_KEY capability");
941a2aca 279 return false;
912541b0
KS
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];
9f6445e3 286 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
912541b0 287 }
f472d466 288 /* If there are no keys in the lower block, check the higher blocks */
912541b0 289 if (!found) {
f472d466
NL
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 }
912541b0
KS
298 }
299 }
300 }
301
941a2aca 302 if (found > 0) {
912541b0 303 udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
941a2aca
HG
304 ret = true;
305 }
912541b0
KS
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;
941a2aca 310 if ((bitmask_key[0] & mask) == mask) {
912541b0 311 udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
941a2aca
HG
312 ret = true;
313 }
314
315 return ret;
d7867b31
KS
316}
317
9ec6e95b 318static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) {
912541b0
KS
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)];
606df97b 324 unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)];
24447733 325 const char *sysname, *devnode;
37186823
HG
326 bool is_pointer;
327 bool is_key;
912541b0 328
3b64e4d4
TG
329 assert(dev);
330
912541b0
KS
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
24447733
DH
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);
606df97b 345 get_cap_mask(dev, pdev, "properties", bitmask_props, sizeof(bitmask_props), test);
37186823
HG
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");
64083a60
PH
354 if (test_bit(EV_SW, bitmask_ev))
355 udev_builtin_add_property(dev, test, "ID_INPUT_SWITCH", "1");
356
24447733
DH
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
912541b0 364 return EXIT_SUCCESS;
d7867b31
KS
365}
366
367const struct udev_builtin udev_builtin_input_id = {
912541b0
KS
368 .name = "input_id",
369 .cmd = builtin_input_id,
5ac0162c 370 .help = "Input device properties",
d7867b31 371};