2 * expose input properties via udev
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>
7 * Copyright (C) 2014 Carlos Garnacho <carlosg@gnome.org>
8 * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
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.
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.
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/>.
30 #include <linux/limits.h>
31 #include <linux/input.h>
34 #include "string-util.h"
38 /* we must use this kernel-compatible implementation */
39 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
40 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
41 #define OFF(x) ((x)%BITS_PER_LONG)
42 #define BIT(x) (1UL<<OFF(x))
43 #define LONG(x) ((x)/BITS_PER_LONG)
44 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
46 static inline int abs_size_mm(const struct input_absinfo
*absinfo
) {
47 /* Resolution is defined to be in units/mm for ABS_X/Y */
48 return (absinfo
->maximum
- absinfo
->minimum
) / absinfo
->resolution
;
51 static void extract_info(struct udev_device
*dev
, const char *devpath
, bool test
) {
52 char width
[DECIMAL_STR_MAX(int)], height
[DECIMAL_STR_MAX(int)];
53 struct input_absinfo xabsinfo
= {}, yabsinfo
= {};
54 _cleanup_close_
int fd
= -1;
56 fd
= open(devpath
, O_RDONLY
|O_CLOEXEC
);
60 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
61 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
64 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
67 snprintf(width
, sizeof(width
), "%d", abs_size_mm(&xabsinfo
));
68 snprintf(height
, sizeof(height
), "%d", abs_size_mm(&yabsinfo
));
70 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
71 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
75 * Read a capability attribute and return bitmask.
76 * @param dev udev_device
77 * @param attr sysfs attribute name (e. g. "capabilities/key")
78 * @param bitmask: Output array which has a sizeof of bitmask_size
80 static void get_cap_mask(struct udev_device
*dev
,
81 struct udev_device
*pdev
, const char* attr
,
82 unsigned long *bitmask
, size_t bitmask_size
,
90 v
= udev_device_get_sysattr_value(pdev
, attr
);
94 snprintf(text
, sizeof(text
), "%s", v
);
95 log_debug("%s raw kernel attribute: %s", attr
, text
);
97 memzero(bitmask
, bitmask_size
);
99 while ((word
= strrchr(text
, ' ')) != NULL
) {
100 val
= strtoul (word
+1, NULL
, 16);
101 if (i
< bitmask_size
/sizeof(unsigned long))
104 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
108 val
= strtoul (text
, NULL
, 16);
109 if (i
< bitmask_size
/ sizeof(unsigned long))
112 log_debug("ignoring %s block %lX which is larger than maximum size", attr
, val
);
115 /* printf pattern with the right unsigned long number of hex chars */
116 snprintf(text
, sizeof(text
), " bit %%4u: %%0%zulX\n", 2 * sizeof(unsigned long));
117 log_debug("%s decoded bit map:", attr
);
118 val
= bitmask_size
/ sizeof (unsigned long);
119 /* skip over leading zeros */
120 while (bitmask
[val
-1] == 0 && val
> 0)
122 for (i
= 0; i
< val
; ++i
) {
123 DISABLE_WARNING_FORMAT_NONLITERAL
;
124 log_debug(text
, i
* BITS_PER_LONG
, bitmask
[i
]);
130 /* pointer devices */
131 static bool test_pointers(struct udev_device
*dev
,
132 const unsigned long* bitmask_ev
,
133 const unsigned long* bitmask_abs
,
134 const unsigned long* bitmask_key
,
135 const unsigned long* bitmask_rel
,
136 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 has_mouse_button
= test_bit(BTN_LEFT
, bitmask_key
);
174 has_rel_coordinates
= test_bit(EV_REL
, bitmask_ev
) && test_bit(REL_X
, bitmask_rel
) && test_bit(REL_Y
, bitmask_rel
);
175 has_mt_coordinates
= test_bit(ABS_MT_POSITION_X
, bitmask_abs
) && test_bit(ABS_MT_POSITION_Y
, bitmask_abs
);
177 /* unset has_mt_coordinates if devices claims to have all abs axis */
178 if(has_mt_coordinates
&& test_bit(ABS_MT_SLOT
, bitmask_abs
) && test_bit(ABS_MT_SLOT
- 1, bitmask_abs
))
179 has_mt_coordinates
= false;
180 is_direct
= test_bit(INPUT_PROP_DIRECT
, bitmask_props
);
181 has_touch
= test_bit(BTN_TOUCH
, bitmask_key
);
182 /* joysticks don't necessarily have buttons; e. g.
183 * rudders/pedals are joystick-like, but buttonless; they have
184 * other fancy axes */
185 has_joystick_axes_or_buttons
= test_bit(BTN_TRIGGER
, bitmask_key
) ||
186 test_bit(BTN_A
, bitmask_key
) ||
187 test_bit(BTN_1
, bitmask_key
) ||
188 test_bit(ABS_RX
, bitmask_abs
) ||
189 test_bit(ABS_RY
, bitmask_abs
) ||
190 test_bit(ABS_RZ
, bitmask_abs
) ||
191 test_bit(ABS_THROTTLE
, bitmask_abs
) ||
192 test_bit(ABS_RUDDER
, bitmask_abs
) ||
193 test_bit(ABS_WHEEL
, bitmask_abs
) ||
194 test_bit(ABS_GAS
, bitmask_abs
) ||
195 test_bit(ABS_BRAKE
, bitmask_abs
);
197 if (has_abs_coordinates
) {
200 else if (finger_but_no_pen
&& !is_direct
)
202 else if (has_mouse_button
)
203 /* This path is taken by VMware's USB mouse, which has
204 * absolute axes, but no touch/pressure button. */
207 is_touchscreen
= true;
208 else if (has_joystick_axes_or_buttons
)
211 if (has_mt_coordinates
&& is_direct
)
212 is_touchscreen
= true;
214 if (has_rel_coordinates
&& has_mouse_button
)
217 if (is_pointing_stick
)
218 udev_builtin_add_property(dev
, test
, "ID_INPUT_POINTINGSTICK", "1");
220 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
222 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
224 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
226 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
228 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
230 return is_tablet
|| is_mouse
|| is_touchpad
|| is_touchscreen
|| is_joystick
|| is_pointing_stick
;
233 /* key like devices */
234 static bool test_key(struct udev_device
*dev
,
235 const unsigned long* bitmask_ev
,
236 const unsigned long* bitmask_key
,
243 /* do we have any KEY_* capability? */
244 if (!test_bit(EV_KEY
, bitmask_ev
)) {
245 log_debug("test_key: no EV_KEY capability");
249 /* only consider KEY_* here, not BTN_* */
251 for (i
= 0; i
< BTN_MISC
/BITS_PER_LONG
; ++i
) {
252 found
|= bitmask_key
[i
];
253 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i
*BITS_PER_LONG
, found
> 0);
255 /* If there are no keys in the lower block, check the higher block */
257 for (i
= KEY_OK
; i
< BTN_TRIGGER_HAPPY
; ++i
) {
258 if (test_bit(i
, bitmask_key
)) {
259 log_debug("test_key: Found key %x in high block", i
);
267 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
271 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
272 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
274 if ((bitmask_key
[0] & mask
) == mask
) {
275 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
282 static int builtin_input_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
283 struct udev_device
*pdev
;
284 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
285 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
286 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
287 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
288 unsigned long bitmask_props
[NBITS(INPUT_PROP_MAX
)];
289 const char *sysname
, *devnode
;
295 /* walk up the parental chain until we find the real input device; the
296 * argument is very likely a subdevice of this, like eventN */
298 while (pdev
!= NULL
&& udev_device_get_sysattr_value(pdev
, "capabilities/ev") == NULL
)
299 pdev
= udev_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
);
302 /* Use this as a flag that input devices were detected, so that this
303 * program doesn't need to be called more than once per device */
304 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
305 get_cap_mask(dev
, pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
306 get_cap_mask(dev
, pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
307 get_cap_mask(dev
, pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
308 get_cap_mask(dev
, pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
309 get_cap_mask(dev
, pdev
, "properties", bitmask_props
, sizeof(bitmask_props
), test
);
310 is_pointer
= test_pointers(dev
, bitmask_ev
, bitmask_abs
,
311 bitmask_key
, bitmask_rel
,
312 bitmask_props
, test
);
313 is_key
= test_key(dev
, bitmask_ev
, bitmask_key
, test
);
314 /* Some evdev nodes have only a scrollwheel */
315 if (!is_pointer
&& !is_key
&& test_bit(EV_REL
, bitmask_ev
) &&
316 (test_bit(REL_WHEEL
, bitmask_rel
) || test_bit(REL_HWHEEL
, bitmask_rel
)))
317 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
320 devnode
= udev_device_get_devnode(dev
);
321 sysname
= udev_device_get_sysname(dev
);
322 if (devnode
&& sysname
&& startswith(sysname
, "event"))
323 extract_info(dev
, devnode
, test
);
328 const struct udev_builtin udev_builtin_input_id
= {
330 .cmd
= builtin_input_id
,
331 .help
= "Input device properties",