1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * expose input properties via udev
5 * Portions Copyright © 2004 David Zeuthen, <david@fubar.dk>
6 * Copyright © 2014 Carlos Garnacho <carlosg@gnome.org>
16 #include <linux/limits.h>
17 #include <linux/input.h>
21 #include "stdio-util.h"
22 #include "string-util.h"
26 /* we must use this kernel-compatible implementation */
27 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
28 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
29 #define OFF(x) ((x)%BITS_PER_LONG)
30 #define BIT(x) (1UL<<OFF(x))
31 #define LONG(x) ((x)/BITS_PER_LONG)
32 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
39 /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/
40 static const struct range high_key_blocks
[] = {
41 { KEY_OK
, BTN_DPAD_UP
},
42 { KEY_ALS_TOGGLE
, BTN_TRIGGER_HAPPY
}
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
;
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;
55 fd
= open(devpath
, O_RDONLY
|O_CLOEXEC
);
59 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
60 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
63 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
66 xsprintf(width
, "%d", abs_size_mm(&xabsinfo
));
67 xsprintf(height
, "%d", abs_size_mm(&yabsinfo
));
69 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
70 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
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
79 static void get_cap_mask(struct udev_device
*dev
,
80 struct udev_device
*pdev
, const char* attr
,
81 unsigned long *bitmask
, size_t bitmask_size
,
89 v
= udev_device_get_sysattr_value(pdev
, attr
);
92 xsprintf(text
, "%s", v
);
93 log_debug("%s raw kernel attribute: %s", attr
, text
);
95 memzero(bitmask
, bitmask_size
);
97 while ((word
= strrchr(text
, ' ')) != NULL
) {
98 val
= strtoul (word
+1, NULL
, 16);
99 if (i
< bitmask_size
/sizeof(unsigned long))
102 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
106 val
= strtoul (text
, NULL
, 16);
107 if (i
< bitmask_size
/ sizeof(unsigned long))
110 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
113 /* printf pattern with the right unsigned long number of hex chars */
114 xsprintf(text
, " bit %%4u: %%0%zulX\n",
115 2 * sizeof(unsigned long));
116 log_debug("%s decoded bit map:", attr
);
117 val
= bitmask_size
/ sizeof (unsigned long);
118 /* skip over leading zeros */
119 while (bitmask
[val
-1] == 0 && val
> 0)
121 for (i
= 0; i
< val
; ++i
) {
122 DISABLE_WARNING_FORMAT_NONLITERAL
;
123 log_debug(text
, i
* BITS_PER_LONG
, bitmask
[i
]);
129 /* pointer devices */
130 static bool test_pointers(struct udev_device
*dev
,
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
,
138 bool has_abs_coordinates
= false;
139 bool has_rel_coordinates
= false;
140 bool has_mt_coordinates
= false;
141 bool has_joystick_axes_or_buttons
= false;
142 bool is_direct
= false;
143 bool has_touch
= false;
144 bool has_3d_coordinates
= false;
145 bool has_keys
= false;
146 bool stylus_or_pen
= false;
147 bool finger_but_no_pen
= false;
148 bool has_mouse_button
= false;
149 bool is_mouse
= false;
150 bool is_touchpad
= false;
151 bool is_touchscreen
= false;
152 bool is_tablet
= false;
153 bool is_joystick
= false;
154 bool is_accelerometer
= false;
155 bool is_pointing_stick
= false;
157 has_keys
= test_bit(EV_KEY
, bitmask_ev
);
158 has_abs_coordinates
= test_bit(ABS_X
, bitmask_abs
) && test_bit(ABS_Y
, bitmask_abs
);
159 has_3d_coordinates
= has_abs_coordinates
&& test_bit(ABS_Z
, bitmask_abs
);
160 is_accelerometer
= test_bit(INPUT_PROP_ACCELEROMETER
, bitmask_props
);
162 if (!has_keys
&& has_3d_coordinates
)
163 is_accelerometer
= true;
165 if (is_accelerometer
) {
166 udev_builtin_add_property(dev
, test
, "ID_INPUT_ACCELEROMETER", "1");
170 is_pointing_stick
= test_bit(INPUT_PROP_POINTING_STICK
, bitmask_props
);
171 stylus_or_pen
= test_bit(BTN_STYLUS
, bitmask_key
) || test_bit(BTN_TOOL_PEN
, bitmask_key
);
172 finger_but_no_pen
= test_bit(BTN_TOOL_FINGER
, bitmask_key
) && !test_bit(BTN_TOOL_PEN
, bitmask_key
);
173 for (button
= BTN_MOUSE
; button
< BTN_JOYSTICK
&& !has_mouse_button
; button
++)
174 has_mouse_button
= test_bit(button
, bitmask_key
);
175 has_rel_coordinates
= test_bit(EV_REL
, bitmask_ev
) && test_bit(REL_X
, bitmask_rel
) && test_bit(REL_Y
, bitmask_rel
);
176 has_mt_coordinates
= test_bit(ABS_MT_POSITION_X
, bitmask_abs
) && test_bit(ABS_MT_POSITION_Y
, bitmask_abs
);
178 /* unset has_mt_coordinates if devices claims to have all abs axis */
179 if (has_mt_coordinates
&& test_bit(ABS_MT_SLOT
, bitmask_abs
) && test_bit(ABS_MT_SLOT
- 1, bitmask_abs
))
180 has_mt_coordinates
= false;
181 is_direct
= test_bit(INPUT_PROP_DIRECT
, bitmask_props
);
182 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. Others have buttons only but no axes.
188 * The BTN_JOYSTICK range starts after the mouse range, so a mouse
189 * with more than 16 buttons runs into the joystick range (e.g. Mad
190 * Catz Mad Catz M.M.O.TE). Skip those.
192 if (!test_bit(BTN_JOYSTICK
- 1, bitmask_key
)) {
193 for (button
= BTN_JOYSTICK
; button
< BTN_DIGI
&& !has_joystick_axes_or_buttons
; button
++)
194 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
195 for (button
= BTN_TRIGGER_HAPPY1
; button
<= BTN_TRIGGER_HAPPY40
&& !has_joystick_axes_or_buttons
; button
++)
196 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
197 for (button
= BTN_DPAD_UP
; button
<= BTN_DPAD_RIGHT
&& !has_joystick_axes_or_buttons
; button
++)
198 has_joystick_axes_or_buttons
= test_bit(button
, bitmask_key
);
200 for (axis
= ABS_RX
; axis
< ABS_PRESSURE
&& !has_joystick_axes_or_buttons
; axis
++)
201 has_joystick_axes_or_buttons
= test_bit(axis
, bitmask_abs
);
203 if (has_abs_coordinates
) {
206 else if (finger_but_no_pen
&& !is_direct
)
208 else if (has_mouse_button
)
209 /* This path is taken by VMware's USB mouse, which has
210 * absolute axes, but no touch/pressure button. */
212 else if (has_touch
|| is_direct
)
213 is_touchscreen
= true;
214 else if (has_joystick_axes_or_buttons
)
216 } else if (has_joystick_axes_or_buttons
) {
220 if (has_mt_coordinates
) {
223 else if (finger_but_no_pen
&& !is_direct
)
225 else if (has_touch
|| is_direct
)
226 is_touchscreen
= true;
229 if (!is_tablet
&& !is_touchpad
&& !is_joystick
&&
231 (has_rel_coordinates
||
232 !has_abs_coordinates
)) /* mouse buttons and no axis */
235 if (is_pointing_stick
)
236 udev_builtin_add_property(dev
, test
, "ID_INPUT_POINTINGSTICK", "1");
238 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
240 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
242 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
244 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
246 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
248 return is_tablet
|| is_mouse
|| is_touchpad
|| is_touchscreen
|| is_joystick
|| is_pointing_stick
;
251 /* key like devices */
252 static bool test_key(struct udev_device
*dev
,
253 const unsigned long* bitmask_ev
,
254 const unsigned long* bitmask_key
,
261 /* do we have any KEY_* capability? */
262 if (!test_bit(EV_KEY
, bitmask_ev
)) {
263 log_debug("test_key: no EV_KEY capability");
267 /* only consider KEY_* here, not BTN_* */
269 for (i
= 0; i
< BTN_MISC
/BITS_PER_LONG
; ++i
) {
270 found
|= bitmask_key
[i
];
271 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i
*BITS_PER_LONG
, found
> 0);
273 /* If there are no keys in the lower block, check the higher blocks */
276 for (block
= 0; block
< (sizeof(high_key_blocks
) / sizeof(struct range
)); ++block
) {
277 for (i
= high_key_blocks
[block
].start
; i
< high_key_blocks
[block
].end
; ++i
) {
278 if (test_bit(i
, bitmask_key
)) {
279 log_debug("test_key: Found key %x in high block", i
);
288 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
292 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
293 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
295 if (FLAGS_SET(bitmask_key
[0], mask
)) {
296 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
303 static int builtin_input_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
304 struct udev_device
*pdev
;
305 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
306 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
307 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
308 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
309 unsigned long bitmask_props
[NBITS(INPUT_PROP_MAX
)];
310 const char *sysname
, *devnode
;
316 /* walk up the parental chain until we find the real input device; the
317 * argument is very likely a subdevice of this, like eventN */
319 while (pdev
!= NULL
&& udev_device_get_sysattr_value(pdev
, "capabilities/ev") == NULL
)
320 pdev
= udev_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
);
323 /* Use this as a flag that input devices were detected, so that this
324 * program doesn't need to be called more than once per device */
325 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
326 get_cap_mask(dev
, pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
327 get_cap_mask(dev
, pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
328 get_cap_mask(dev
, pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
329 get_cap_mask(dev
, pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
330 get_cap_mask(dev
, pdev
, "properties", bitmask_props
, sizeof(bitmask_props
), test
);
331 is_pointer
= test_pointers(dev
, bitmask_ev
, bitmask_abs
,
332 bitmask_key
, bitmask_rel
,
333 bitmask_props
, test
);
334 is_key
= test_key(dev
, bitmask_ev
, bitmask_key
, test
);
335 /* Some evdev nodes have only a scrollwheel */
336 if (!is_pointer
&& !is_key
&& test_bit(EV_REL
, bitmask_ev
) &&
337 (test_bit(REL_WHEEL
, bitmask_rel
) || test_bit(REL_HWHEEL
, bitmask_rel
)))
338 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
339 if (test_bit(EV_SW
, bitmask_ev
))
340 udev_builtin_add_property(dev
, test
, "ID_INPUT_SWITCH", "1");
344 devnode
= udev_device_get_devnode(dev
);
345 sysname
= udev_device_get_sysname(dev
);
346 if (devnode
&& sysname
&& startswith(sysname
, "event"))
347 extract_info(dev
, devnode
, test
);
352 const struct udev_builtin udev_builtin_input_id
= {
354 .cmd
= builtin_input_id
,
355 .help
= "Input device properties",