1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * expose input properties via udev
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>
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.
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.
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/>.
31 #include <linux/limits.h>
32 #include <linux/input.h>
36 #include "stdio-util.h"
37 #include "string-util.h"
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)
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
}
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
;
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;
70 fd
= open(devpath
, O_RDONLY
|O_CLOEXEC
);
74 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
75 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
78 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
81 xsprintf(width
, "%d", abs_size_mm(&xabsinfo
));
82 xsprintf(height
, "%d", abs_size_mm(&yabsinfo
));
84 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
85 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
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
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
,
104 v
= udev_device_get_sysattr_value(pdev
, attr
);
108 xsprintf(text
, "%s", v
);
109 log_debug("%s raw kernel attribute: %s", attr
, text
);
111 memzero(bitmask
, bitmask_size
);
113 while ((word
= strrchr(text
, ' ')) != NULL
) {
114 val
= strtoul (word
+1, NULL
, 16);
115 if (i
< bitmask_size
/sizeof(unsigned long))
118 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
122 val
= strtoul (text
, NULL
, 16);
123 if (i
< bitmask_size
/ sizeof(unsigned long))
126 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
129 /* printf pattern with the right unsigned long number of hex chars */
130 xsprintf(text
, " bit %%4u: %%0%zulX\n",
131 2 * sizeof(unsigned long));
132 log_debug("%s decoded bit map:", attr
);
133 val
= bitmask_size
/ sizeof (unsigned long);
134 /* skip over leading zeros */
135 while (bitmask
[val
-1] == 0 && val
> 0)
137 for (i
= 0; i
< val
; ++i
) {
138 DISABLE_WARNING_FORMAT_NONLITERAL
;
139 log_debug(text
, i
* BITS_PER_LONG
, bitmask
[i
]);
145 /* pointer devices */
146 static bool test_pointers(struct udev_device
*dev
,
147 const unsigned long* bitmask_ev
,
148 const unsigned long* bitmask_abs
,
149 const unsigned long* bitmask_key
,
150 const unsigned long* bitmask_rel
,
151 const unsigned long* bitmask_props
,
154 bool has_abs_coordinates
= false;
155 bool has_rel_coordinates
= false;
156 bool has_mt_coordinates
= false;
157 bool has_joystick_axes_or_buttons
= false;
158 bool is_direct
= false;
159 bool has_touch
= false;
160 bool has_3d_coordinates
= false;
161 bool has_keys
= false;
162 bool stylus_or_pen
= false;
163 bool finger_but_no_pen
= false;
164 bool has_mouse_button
= false;
165 bool is_mouse
= false;
166 bool is_touchpad
= false;
167 bool is_touchscreen
= false;
168 bool is_tablet
= false;
169 bool is_joystick
= false;
170 bool is_accelerometer
= false;
171 bool is_pointing_stick
= false;
173 has_keys
= test_bit(EV_KEY
, bitmask_ev
);
174 has_abs_coordinates
= test_bit(ABS_X
, bitmask_abs
) && test_bit(ABS_Y
, bitmask_abs
);
175 has_3d_coordinates
= has_abs_coordinates
&& test_bit(ABS_Z
, bitmask_abs
);
176 is_accelerometer
= test_bit(INPUT_PROP_ACCELEROMETER
, bitmask_props
);
178 if (!has_keys
&& has_3d_coordinates
)
179 is_accelerometer
= true;
181 if (is_accelerometer
) {
182 udev_builtin_add_property(dev
, test
, "ID_INPUT_ACCELEROMETER", "1");
186 is_pointing_stick
= test_bit(INPUT_PROP_POINTING_STICK
, bitmask_props
);
187 stylus_or_pen
= test_bit(BTN_STYLUS
, bitmask_key
) || test_bit(BTN_TOOL_PEN
, bitmask_key
);
188 finger_but_no_pen
= test_bit(BTN_TOOL_FINGER
, bitmask_key
) && !test_bit(BTN_TOOL_PEN
, bitmask_key
);
189 for (button
= BTN_MOUSE
; button
< BTN_JOYSTICK
&& !has_mouse_button
; button
++)
190 has_mouse_button
= test_bit(button
, bitmask_key
);
191 has_rel_coordinates
= test_bit(EV_REL
, bitmask_ev
) && test_bit(REL_X
, bitmask_rel
) && test_bit(REL_Y
, bitmask_rel
);
192 has_mt_coordinates
= test_bit(ABS_MT_POSITION_X
, bitmask_abs
) && test_bit(ABS_MT_POSITION_Y
, bitmask_abs
);
194 /* unset has_mt_coordinates if devices claims to have all abs axis */
195 if (has_mt_coordinates
&& test_bit(ABS_MT_SLOT
, bitmask_abs
) && test_bit(ABS_MT_SLOT
- 1, bitmask_abs
))
196 has_mt_coordinates
= false;
197 is_direct
= test_bit(INPUT_PROP_DIRECT
, bitmask_props
);
198 has_touch
= test_bit(BTN_TOUCH
, bitmask_key
);
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 for (button
= BTN_JOYSTICK
; button
< BTN_DIGI
&& !has_joystick_axes_or_buttons
; button
++)
203 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
204 for (button
= BTN_TRIGGER_HAPPY1
; button
<= BTN_TRIGGER_HAPPY40
&& !has_joystick_axes_or_buttons
; button
++)
205 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
206 for (button
= BTN_DPAD_UP
; button
<= BTN_DPAD_RIGHT
&& !has_joystick_axes_or_buttons
; button
++)
207 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
208 for (axis
= ABS_RX
; axis
< ABS_PRESSURE
&& !has_joystick_axes_or_buttons
; axis
++)
209 has_joystick_axes_or_buttons
= test_bit(axis
, bitmask_abs
);
211 if (has_abs_coordinates
) {
214 else if (finger_but_no_pen
&& !is_direct
)
216 else if (has_mouse_button
)
217 /* This path is taken by VMware's USB mouse, which has
218 * absolute axes, but no touch/pressure button. */
220 else if (has_touch
|| is_direct
)
221 is_touchscreen
= true;
222 else if (has_joystick_axes_or_buttons
)
224 } else if (has_joystick_axes_or_buttons
) {
228 if (has_mt_coordinates
) {
231 else if (finger_but_no_pen
&& !is_direct
)
233 else if (has_touch
|| is_direct
)
234 is_touchscreen
= true;
237 if (has_mouse_button
&&
238 (has_rel_coordinates
||
239 !has_abs_coordinates
)) /* mouse buttons and no axis */
242 if (is_pointing_stick
)
243 udev_builtin_add_property(dev
, test
, "ID_INPUT_POINTINGSTICK", "1");
245 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
247 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
249 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
251 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
253 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
255 return is_tablet
|| is_mouse
|| is_touchpad
|| is_touchscreen
|| is_joystick
|| is_pointing_stick
;
258 /* key like devices */
259 static bool test_key(struct udev_device
*dev
,
260 const unsigned long* bitmask_ev
,
261 const unsigned long* bitmask_key
,
268 /* do we have any KEY_* capability? */
269 if (!test_bit(EV_KEY
, bitmask_ev
)) {
270 log_debug("test_key: no EV_KEY capability");
274 /* only consider KEY_* here, not BTN_* */
276 for (i
= 0; i
< BTN_MISC
/BITS_PER_LONG
; ++i
) {
277 found
|= bitmask_key
[i
];
278 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i
*BITS_PER_LONG
, found
> 0);
280 /* If there are no keys in the lower block, check the higher blocks */
283 for (block
= 0; block
< (sizeof(high_key_blocks
) / sizeof(struct range
)); ++block
) {
284 for (i
= high_key_blocks
[block
].start
; i
< high_key_blocks
[block
].end
; ++i
) {
285 if (test_bit(i
, bitmask_key
)) {
286 log_debug("test_key: Found key %x in high block", i
);
295 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
299 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
300 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
302 if ((bitmask_key
[0] & mask
) == mask
) {
303 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
310 static int builtin_input_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
311 struct udev_device
*pdev
;
312 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
313 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
314 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
315 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
316 unsigned long bitmask_props
[NBITS(INPUT_PROP_MAX
)];
317 const char *sysname
, *devnode
;
323 /* walk up the parental chain until we find the real input device; the
324 * argument is very likely a subdevice of this, like eventN */
326 while (pdev
!= NULL
&& udev_device_get_sysattr_value(pdev
, "capabilities/ev") == NULL
)
327 pdev
= udev_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
);
330 /* Use this as a flag that input devices were detected, so that this
331 * program doesn't need to be called more than once per device */
332 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
333 get_cap_mask(dev
, pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
334 get_cap_mask(dev
, pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
335 get_cap_mask(dev
, pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
336 get_cap_mask(dev
, pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
337 get_cap_mask(dev
, pdev
, "properties", bitmask_props
, sizeof(bitmask_props
), test
);
338 is_pointer
= test_pointers(dev
, bitmask_ev
, bitmask_abs
,
339 bitmask_key
, bitmask_rel
,
340 bitmask_props
, test
);
341 is_key
= test_key(dev
, bitmask_ev
, bitmask_key
, test
);
342 /* Some evdev nodes have only a scrollwheel */
343 if (!is_pointer
&& !is_key
&& test_bit(EV_REL
, bitmask_ev
) &&
344 (test_bit(REL_WHEEL
, bitmask_rel
) || test_bit(REL_HWHEEL
, bitmask_rel
)))
345 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
346 if (test_bit(EV_SW
, bitmask_ev
))
347 udev_builtin_add_property(dev
, test
, "ID_INPUT_SWITCH", "1");
351 devnode
= udev_device_get_devnode(dev
);
352 sysname
= udev_device_get_sysname(dev
);
353 if (devnode
&& sysname
&& startswith(sysname
, "event"))
354 extract_info(dev
, devnode
, test
);
359 const struct udev_builtin udev_builtin_input_id
= {
361 .cmd
= builtin_input_id
,
362 .help
= "Input device properties",