]>
Commit | Line | Data |
---|---|---|
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 | ||
07630cea | 33 | #include "string-util.h" |
d7867b31 | 34 | #include "udev.h" |
24447733 | 35 | #include "util.h" |
d7867b31 KS |
36 | |
37 | /* we must use this kernel-compatible implementation */ | |
38 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) | |
39 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) | |
40 | #define OFF(x) ((x)%BITS_PER_LONG) | |
41 | #define BIT(x) (1UL<<OFF(x)) | |
42 | #define LONG(x) ((x)/BITS_PER_LONG) | |
43 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
44 | ||
24447733 DH |
45 | static inline int abs_size_mm(const struct input_absinfo *absinfo) { |
46 | /* Resolution is defined to be in units/mm for ABS_X/Y */ | |
47 | return (absinfo->maximum - absinfo->minimum) / absinfo->resolution; | |
48 | } | |
49 | ||
50 | static void extract_info(struct udev_device *dev, const char *devpath, bool test) { | |
51 | char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)]; | |
52 | struct input_absinfo xabsinfo = {}, yabsinfo = {}; | |
53 | _cleanup_close_ int fd = -1; | |
54 | ||
55 | fd = open(devpath, O_RDONLY|O_CLOEXEC); | |
56 | if (fd < 0) | |
57 | return; | |
58 | ||
59 | if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 || | |
60 | ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0) | |
61 | return; | |
62 | ||
63 | if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0) | |
64 | return; | |
65 | ||
66 | snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo)); | |
67 | snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo)); | |
68 | ||
69 | udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width); | |
70 | udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height); | |
71 | } | |
72 | ||
d7867b31 KS |
73 | /* |
74 | * Read a capability attribute and return bitmask. | |
75 | * @param dev udev_device | |
76 | * @param attr sysfs attribute name (e. g. "capabilities/key") | |
77 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
78 | */ | |
79 | static void get_cap_mask(struct udev_device *dev, | |
912541b0 KS |
80 | struct udev_device *pdev, const char* attr, |
81 | unsigned long *bitmask, size_t bitmask_size, | |
9ec6e95b | 82 | bool test) { |
975a9007 | 83 | const char *v; |
912541b0 KS |
84 | char text[4096]; |
85 | unsigned i; | |
86 | char* word; | |
87 | unsigned long val; | |
88 | ||
975a9007 DH |
89 | v = udev_device_get_sysattr_value(pdev, attr); |
90 | if (!v) | |
91 | v = ""; | |
92 | ||
93 | snprintf(text, sizeof(text), "%s", v); | |
9f6445e3 | 94 | log_debug("%s raw kernel attribute: %s", attr, text); |
912541b0 | 95 | |
29804cc1 | 96 | memzero(bitmask, bitmask_size); |
912541b0 KS |
97 | i = 0; |
98 | while ((word = strrchr(text, ' ')) != NULL) { | |
99 | val = strtoul (word+1, NULL, 16); | |
100 | if (i < bitmask_size/sizeof(unsigned long)) | |
101 | bitmask[i] = val; | |
102 | else | |
9f6445e3 | 103 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
104 | *word = '\0'; |
105 | ++i; | |
106 | } | |
107 | val = strtoul (text, NULL, 16); | |
108 | if (i < bitmask_size / sizeof(unsigned long)) | |
109 | bitmask[i] = val; | |
110 | else | |
9f6445e3 | 111 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
112 | |
113 | if (test) { | |
114 | /* printf pattern with the right unsigned long number of hex chars */ | |
1fa2f38f | 115 | snprintf(text, sizeof(text), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long)); |
9f6445e3 | 116 | log_debug("%s decoded bit map:", attr); |
912541b0 KS |
117 | val = bitmask_size / sizeof (unsigned long); |
118 | /* skip over leading zeros */ | |
119 | while (bitmask[val-1] == 0 && val > 0) | |
120 | --val; | |
bcfce235 LP |
121 | for (i = 0; i < val; ++i) { |
122 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
baa30fbc | 123 | log_debug(text, i * BITS_PER_LONG, bitmask[i]); |
bcfce235 LP |
124 | REENABLE_WARNING; |
125 | } | |
912541b0 | 126 | } |
d7867b31 KS |
127 | } |
128 | ||
129 | /* pointer devices */ | |
941a2aca | 130 | static bool test_pointers(struct udev_device *dev, |
b914418a PH |
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) { | |
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); | |
172 | has_mouse_button = test_bit(BTN_LEFT, bitmask_key); | |
173 | has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel); | |
fa5a113d | 174 | has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs); |
495968cf AP |
175 | |
176 | /* unset has_mt_coordinates if devices claims to have all abs axis */ | |
177 | if(has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs)) | |
178 | has_mt_coordinates = false; | |
fa5a113d | 179 | is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props); |
15264e5a AP |
180 | has_touch = test_bit(BTN_TOUCH, bitmask_key); |
181 | /* joysticks don't necessarily have buttons; e. g. | |
182 | * rudders/pedals are joystick-like, but buttonless; they have | |
183 | * other fancy axes */ | |
184 | has_joystick_axes_or_buttons = test_bit(BTN_TRIGGER, bitmask_key) || | |
185 | test_bit(BTN_A, bitmask_key) || | |
186 | test_bit(BTN_1, bitmask_key) || | |
187 | test_bit(ABS_RX, bitmask_abs) || | |
188 | test_bit(ABS_RY, bitmask_abs) || | |
189 | test_bit(ABS_RZ, bitmask_abs) || | |
190 | test_bit(ABS_THROTTLE, bitmask_abs) || | |
191 | test_bit(ABS_RUDDER, bitmask_abs) || | |
192 | test_bit(ABS_WHEEL, bitmask_abs) || | |
193 | test_bit(ABS_GAS, bitmask_abs) || | |
194 | test_bit(ABS_BRAKE, bitmask_abs); | |
195 | ||
196 | if (has_abs_coordinates) { | |
197 | if (stylus_or_pen) | |
198 | is_tablet = true; | |
fa5a113d | 199 | else if (finger_but_no_pen && !is_direct) |
15264e5a AP |
200 | is_touchpad = true; |
201 | else if (has_mouse_button) | |
912541b0 KS |
202 | /* This path is taken by VMware's USB mouse, which has |
203 | * absolute axes, but no touch/pressure button. */ | |
15264e5a AP |
204 | is_mouse = true; |
205 | else if (has_touch) | |
206 | is_touchscreen = true; | |
207 | else if (has_joystick_axes_or_buttons) | |
208 | is_joystick = true; | |
912541b0 | 209 | } |
fa5a113d AP |
210 | if (has_mt_coordinates && is_direct) |
211 | is_touchscreen = true; | |
912541b0 | 212 | |
15264e5a AP |
213 | if (has_rel_coordinates && has_mouse_button) |
214 | is_mouse = true; | |
912541b0 | 215 | |
15264e5a AP |
216 | if (is_pointing_stick) |
217 | udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1"); | |
218 | if (is_mouse) | |
912541b0 | 219 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); |
15264e5a | 220 | if (is_touchpad) |
912541b0 | 221 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); |
15264e5a AP |
222 | if (is_touchscreen) |
223 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); | |
224 | if (is_joystick) | |
225 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); | |
226 | if (is_tablet) | |
227 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); | |
228 | ||
229 | return is_tablet || is_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick; | |
d7867b31 KS |
230 | } |
231 | ||
232 | /* key like devices */ | |
941a2aca | 233 | static bool test_key(struct udev_device *dev, |
b914418a PH |
234 | const unsigned long* bitmask_ev, |
235 | const unsigned long* bitmask_key, | |
236 | bool test) { | |
912541b0 KS |
237 | unsigned i; |
238 | unsigned long found; | |
239 | unsigned long mask; | |
941a2aca | 240 | bool ret = false; |
912541b0 KS |
241 | |
242 | /* do we have any KEY_* capability? */ | |
b914418a | 243 | if (!test_bit(EV_KEY, bitmask_ev)) { |
9f6445e3 | 244 | log_debug("test_key: no EV_KEY capability"); |
941a2aca | 245 | return false; |
912541b0 KS |
246 | } |
247 | ||
248 | /* only consider KEY_* here, not BTN_* */ | |
249 | found = 0; | |
250 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
251 | found |= bitmask_key[i]; | |
9f6445e3 | 252 | log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); |
912541b0 KS |
253 | } |
254 | /* If there are no keys in the lower block, check the higher block */ | |
255 | if (!found) { | |
256 | for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) { | |
b914418a | 257 | if (test_bit(i, bitmask_key)) { |
9f6445e3 | 258 | log_debug("test_key: Found key %x in high block", i); |
912541b0 KS |
259 | found = 1; |
260 | break; | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
941a2aca | 265 | if (found > 0) { |
912541b0 | 266 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
941a2aca HG |
267 | ret = true; |
268 | } | |
912541b0 KS |
269 | |
270 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
271 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
272 | mask = 0xFFFFFFFE; | |
941a2aca | 273 | if ((bitmask_key[0] & mask) == mask) { |
912541b0 | 274 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); |
941a2aca HG |
275 | ret = true; |
276 | } | |
277 | ||
278 | return ret; | |
d7867b31 KS |
279 | } |
280 | ||
9ec6e95b | 281 | static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) { |
912541b0 KS |
282 | struct udev_device *pdev; |
283 | unsigned long bitmask_ev[NBITS(EV_MAX)]; | |
284 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
285 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
286 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
606df97b | 287 | unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)]; |
24447733 | 288 | const char *sysname, *devnode; |
37186823 HG |
289 | bool is_pointer; |
290 | bool is_key; | |
912541b0 | 291 | |
3b64e4d4 TG |
292 | assert(dev); |
293 | ||
912541b0 KS |
294 | /* walk up the parental chain until we find the real input device; the |
295 | * argument is very likely a subdevice of this, like eventN */ | |
296 | pdev = dev; | |
297 | while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL) | |
298 | pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL); | |
299 | ||
24447733 DH |
300 | if (pdev) { |
301 | /* Use this as a flag that input devices were detected, so that this | |
302 | * program doesn't need to be called more than once per device */ | |
303 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); | |
304 | get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); | |
305 | get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
306 | get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
307 | get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
606df97b | 308 | get_cap_mask(dev, pdev, "properties", bitmask_props, sizeof(bitmask_props), test); |
37186823 HG |
309 | is_pointer = test_pointers(dev, bitmask_ev, bitmask_abs, |
310 | bitmask_key, bitmask_rel, | |
311 | bitmask_props, test); | |
312 | is_key = test_key(dev, bitmask_ev, bitmask_key, test); | |
313 | /* Some evdev nodes have only a scrollwheel */ | |
314 | if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) && | |
315 | (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel))) | |
316 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); | |
24447733 DH |
317 | } |
318 | ||
319 | devnode = udev_device_get_devnode(dev); | |
320 | sysname = udev_device_get_sysname(dev); | |
321 | if (devnode && sysname && startswith(sysname, "event")) | |
322 | extract_info(dev, devnode, test); | |
323 | ||
912541b0 | 324 | return EXIT_SUCCESS; |
d7867b31 KS |
325 | } |
326 | ||
327 | const struct udev_builtin udev_builtin_input_id = { | |
912541b0 KS |
328 | .name = "input_id", |
329 | .cmd = builtin_input_id, | |
5ac0162c | 330 | .help = "Input device properties", |
d7867b31 | 331 | }; |