]>
Commit | Line | Data |
---|---|---|
663996b3 | 1 | /* |
e735f4d4 | 2 | * expose input properties via udev |
663996b3 MS |
3 | * |
4 | * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com> | |
5 | * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk> | |
6 | * Copyright (C) 2011 Kay Sievers <kay@vrfy.org> | |
e735f4d4 MP |
7 | * Copyright (C) 2014 Carlos Garnacho <carlosg@gnome.org> |
8 | * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com> | |
663996b3 MS |
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 | ||
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <stdarg.h> | |
27 | #include <unistd.h> | |
28 | #include <string.h> | |
29 | #include <errno.h> | |
30 | #include <linux/limits.h> | |
31 | #include <linux/input.h> | |
32 | ||
33 | #include "udev.h" | |
e735f4d4 | 34 | #include "util.h" |
663996b3 MS |
35 | |
36 | /* we must use this kernel-compatible implementation */ | |
37 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) | |
38 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) | |
39 | #define OFF(x) ((x)%BITS_PER_LONG) | |
40 | #define BIT(x) (1UL<<OFF(x)) | |
41 | #define LONG(x) ((x)/BITS_PER_LONG) | |
42 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
43 | ||
e735f4d4 MP |
44 | static inline int abs_size_mm(const struct input_absinfo *absinfo) { |
45 | /* Resolution is defined to be in units/mm for ABS_X/Y */ | |
46 | return (absinfo->maximum - absinfo->minimum) / absinfo->resolution; | |
47 | } | |
48 | ||
49 | static void extract_info(struct udev_device *dev, const char *devpath, bool test) { | |
50 | char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)]; | |
51 | struct input_absinfo xabsinfo = {}, yabsinfo = {}; | |
52 | _cleanup_close_ int fd = -1; | |
53 | ||
54 | fd = open(devpath, O_RDONLY|O_CLOEXEC); | |
55 | if (fd < 0) | |
56 | return; | |
57 | ||
58 | if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 || | |
59 | ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0) | |
60 | return; | |
61 | ||
62 | if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0) | |
63 | return; | |
64 | ||
65 | snprintf(width, sizeof(width), "%d", abs_size_mm(&xabsinfo)); | |
66 | snprintf(height, sizeof(height), "%d", abs_size_mm(&yabsinfo)); | |
67 | ||
68 | udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width); | |
69 | udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height); | |
70 | } | |
71 | ||
663996b3 MS |
72 | /* |
73 | * Read a capability attribute and return bitmask. | |
74 | * @param dev udev_device | |
75 | * @param attr sysfs attribute name (e. g. "capabilities/key") | |
76 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
77 | */ | |
78 | static void get_cap_mask(struct udev_device *dev, | |
79 | struct udev_device *pdev, const char* attr, | |
80 | unsigned long *bitmask, size_t bitmask_size, | |
5eef597e | 81 | bool test) { |
e735f4d4 | 82 | const char *v; |
663996b3 MS |
83 | char text[4096]; |
84 | unsigned i; | |
85 | char* word; | |
86 | unsigned long val; | |
87 | ||
e735f4d4 MP |
88 | v = udev_device_get_sysattr_value(pdev, attr); |
89 | if (!v) | |
90 | v = ""; | |
91 | ||
92 | snprintf(text, sizeof(text), "%s", v); | |
60f067b4 | 93 | log_debug("%s raw kernel attribute: %s", attr, text); |
663996b3 | 94 | |
60f067b4 | 95 | memzero(bitmask, bitmask_size); |
663996b3 MS |
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 | |
60f067b4 | 102 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
663996b3 MS |
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 | |
60f067b4 | 110 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
663996b3 MS |
111 | |
112 | if (test) { | |
113 | /* printf pattern with the right unsigned long number of hex chars */ | |
e735f4d4 | 114 | snprintf(text, sizeof(text), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long)); |
60f067b4 | 115 | log_debug("%s decoded bit map:", attr); |
663996b3 MS |
116 | val = bitmask_size / sizeof (unsigned long); |
117 | /* skip over leading zeros */ | |
118 | while (bitmask[val-1] == 0 && val > 0) | |
119 | --val; | |
60f067b4 JS |
120 | for (i = 0; i < val; ++i) { |
121 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
663996b3 | 122 | log_debug(text, i * BITS_PER_LONG, bitmask[i]); |
60f067b4 JS |
123 | REENABLE_WARNING; |
124 | } | |
663996b3 MS |
125 | } |
126 | } | |
663996b3 MS |
127 | |
128 | /* pointer devices */ | |
e3bff60a MP |
129 | static bool test_pointers(struct udev_device *dev, |
130 | const unsigned long* bitmask_ev, | |
131 | const unsigned long* bitmask_abs, | |
132 | const unsigned long* bitmask_key, | |
133 | const unsigned long* bitmask_rel, | |
134 | const unsigned long* bitmask_props, | |
135 | bool test) { | |
663996b3 MS |
136 | int is_mouse = 0; |
137 | int is_touchpad = 0; | |
e3bff60a | 138 | bool ret = false; |
663996b3 | 139 | |
e3bff60a MP |
140 | if (test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props)) { |
141 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); | |
142 | return true; | |
143 | } | |
144 | ||
145 | if (!test_bit(EV_KEY, bitmask_ev)) { | |
146 | if (test_bit(EV_ABS, bitmask_ev) && | |
147 | test_bit(ABS_X, bitmask_abs) && | |
148 | test_bit(ABS_Y, bitmask_abs) && | |
149 | test_bit(ABS_Z, bitmask_abs)) { | |
663996b3 | 150 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); |
e3bff60a MP |
151 | ret = true; |
152 | } | |
153 | return ret; | |
663996b3 MS |
154 | } |
155 | ||
e3bff60a MP |
156 | if (test_bit(EV_ABS, bitmask_ev) && |
157 | test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs)) { | |
158 | if (test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key)) { | |
663996b3 | 159 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); |
e3bff60a MP |
160 | ret = true; |
161 | } else if (test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key)) { | |
663996b3 | 162 | is_touchpad = 1; |
e3bff60a | 163 | } else if (test_bit(BTN_MOUSE, bitmask_key)) { |
663996b3 MS |
164 | /* This path is taken by VMware's USB mouse, which has |
165 | * absolute axes, but no touch/pressure button. */ | |
166 | is_mouse = 1; | |
e3bff60a | 167 | } else if (test_bit(BTN_TOUCH, bitmask_key)) { |
663996b3 | 168 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); |
e3bff60a | 169 | ret = true; |
60f067b4 JS |
170 | /* joysticks don't necessarily have to have buttons; e. g. |
171 | * rudders/pedals are joystick-like, but buttonless; they have | |
172 | * other fancy axes */ | |
e3bff60a MP |
173 | } else if (test_bit(BTN_TRIGGER, bitmask_key) || |
174 | test_bit(BTN_A, bitmask_key) || | |
175 | test_bit(BTN_1, bitmask_key) || | |
176 | test_bit(ABS_RX, bitmask_abs) || | |
177 | test_bit(ABS_RY, bitmask_abs) || | |
178 | test_bit(ABS_RZ, bitmask_abs) || | |
179 | test_bit(ABS_THROTTLE, bitmask_abs) || | |
180 | test_bit(ABS_RUDDER, bitmask_abs) || | |
181 | test_bit(ABS_WHEEL, bitmask_abs) || | |
182 | test_bit(ABS_GAS, bitmask_abs) || | |
183 | test_bit(ABS_BRAKE, bitmask_abs)) { | |
60f067b4 | 184 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); |
e3bff60a MP |
185 | ret = true; |
186 | } | |
187 | } | |
188 | ||
189 | if (test_bit(INPUT_PROP_POINTING_STICK, bitmask_props)) { | |
190 | udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1"); | |
191 | ret = true; | |
663996b3 MS |
192 | } |
193 | ||
e3bff60a MP |
194 | if (test_bit(EV_REL, bitmask_ev) && |
195 | test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel) && | |
196 | test_bit(BTN_MOUSE, bitmask_key)) | |
663996b3 MS |
197 | is_mouse = 1; |
198 | ||
e3bff60a | 199 | if (is_mouse) { |
663996b3 | 200 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); |
e3bff60a MP |
201 | ret = true; |
202 | } | |
203 | if (is_touchpad) { | |
663996b3 | 204 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); |
e3bff60a MP |
205 | ret = true; |
206 | } | |
207 | ||
208 | return ret; | |
663996b3 MS |
209 | } |
210 | ||
211 | /* key like devices */ | |
e3bff60a MP |
212 | static bool test_key(struct udev_device *dev, |
213 | const unsigned long* bitmask_ev, | |
214 | const unsigned long* bitmask_key, | |
215 | bool test) { | |
663996b3 MS |
216 | unsigned i; |
217 | unsigned long found; | |
218 | unsigned long mask; | |
e3bff60a | 219 | bool ret = false; |
663996b3 MS |
220 | |
221 | /* do we have any KEY_* capability? */ | |
e3bff60a | 222 | if (!test_bit(EV_KEY, bitmask_ev)) { |
60f067b4 | 223 | log_debug("test_key: no EV_KEY capability"); |
e3bff60a | 224 | return false; |
663996b3 MS |
225 | } |
226 | ||
227 | /* only consider KEY_* here, not BTN_* */ | |
228 | found = 0; | |
229 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
230 | found |= bitmask_key[i]; | |
60f067b4 | 231 | log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); |
663996b3 MS |
232 | } |
233 | /* If there are no keys in the lower block, check the higher block */ | |
234 | if (!found) { | |
235 | for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) { | |
e3bff60a | 236 | if (test_bit(i, bitmask_key)) { |
60f067b4 | 237 | log_debug("test_key: Found key %x in high block", i); |
663996b3 MS |
238 | found = 1; |
239 | break; | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
e3bff60a | 244 | if (found > 0) { |
663996b3 | 245 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
e3bff60a MP |
246 | ret = true; |
247 | } | |
663996b3 MS |
248 | |
249 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
250 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
251 | mask = 0xFFFFFFFE; | |
e3bff60a | 252 | if ((bitmask_key[0] & mask) == mask) { |
663996b3 | 253 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); |
e3bff60a MP |
254 | ret = true; |
255 | } | |
256 | ||
257 | return ret; | |
663996b3 MS |
258 | } |
259 | ||
5eef597e | 260 | static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) { |
663996b3 MS |
261 | struct udev_device *pdev; |
262 | unsigned long bitmask_ev[NBITS(EV_MAX)]; | |
263 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
264 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
265 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
e3bff60a | 266 | unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)]; |
e735f4d4 | 267 | const char *sysname, *devnode; |
e3bff60a MP |
268 | bool is_pointer; |
269 | bool is_key; | |
663996b3 MS |
270 | |
271 | /* walk up the parental chain until we find the real input device; the | |
272 | * argument is very likely a subdevice of this, like eventN */ | |
273 | pdev = dev; | |
274 | while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL) | |
275 | pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL); | |
276 | ||
e735f4d4 MP |
277 | if (pdev) { |
278 | /* Use this as a flag that input devices were detected, so that this | |
279 | * program doesn't need to be called more than once per device */ | |
280 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); | |
281 | get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); | |
282 | get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
283 | get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
284 | get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
e3bff60a MP |
285 | get_cap_mask(dev, pdev, "properties", bitmask_props, sizeof(bitmask_props), test); |
286 | is_pointer = test_pointers(dev, bitmask_ev, bitmask_abs, | |
287 | bitmask_key, bitmask_rel, | |
288 | bitmask_props, test); | |
289 | is_key = test_key(dev, bitmask_ev, bitmask_key, test); | |
290 | /* Some evdev nodes have only a scrollwheel */ | |
291 | if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) && | |
292 | (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel))) | |
293 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); | |
e735f4d4 MP |
294 | } |
295 | ||
296 | devnode = udev_device_get_devnode(dev); | |
297 | sysname = udev_device_get_sysname(dev); | |
298 | if (devnode && sysname && startswith(sysname, "event")) | |
299 | extract_info(dev, devnode, test); | |
300 | ||
663996b3 MS |
301 | return EXIT_SUCCESS; |
302 | } | |
303 | ||
304 | const struct udev_builtin udev_builtin_input_id = { | |
305 | .name = "input_id", | |
306 | .cmd = builtin_input_id, | |
e735f4d4 | 307 | .help = "Input device properties", |
663996b3 | 308 | }; |