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>
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)
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
;
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;
54 fd
= open(devpath
, O_RDONLY
|O_CLOEXEC
);
58 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
59 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
62 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
65 snprintf(width
, sizeof(width
), "%d", abs_size_mm(&xabsinfo
));
66 snprintf(height
, sizeof(height
), "%d", abs_size_mm(&yabsinfo
));
68 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
69 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
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
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
,
88 v
= udev_device_get_sysattr_value(pdev
, attr
);
92 snprintf(text
, sizeof(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 snprintf(text
, sizeof(text
), " bit %%4u: %%0%zilX\n", 2 * sizeof(unsigned long));
115 log_debug("%s decoded bit map:", attr
);
116 val
= bitmask_size
/ sizeof (unsigned long);
117 /* skip over leading zeros */
118 while (bitmask
[val
-1] == 0 && val
> 0)
120 for (i
= 0; i
< val
; ++i
) {
121 DISABLE_WARNING_FORMAT_NONLITERAL
;
122 log_debug(text
, i
* BITS_PER_LONG
, bitmask
[i
]);
128 /* pointer devices */
129 static void 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
,
138 if (!test_bit (EV_KEY
, bitmask_ev
)) {
139 if (test_bit (EV_ABS
, bitmask_ev
) &&
140 test_bit (ABS_X
, bitmask_abs
) &&
141 test_bit (ABS_Y
, bitmask_abs
) &&
142 test_bit (ABS_Z
, bitmask_abs
))
143 udev_builtin_add_property(dev
, test
, "ID_INPUT_ACCELEROMETER", "1");
147 if (test_bit (EV_ABS
, bitmask_ev
) &&
148 test_bit (ABS_X
, bitmask_abs
) && test_bit (ABS_Y
, bitmask_abs
)) {
149 if (test_bit (BTN_STYLUS
, bitmask_key
) || test_bit (BTN_TOOL_PEN
, bitmask_key
))
150 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
151 else if (test_bit (BTN_TOOL_FINGER
, bitmask_key
) && !test_bit (BTN_TOOL_PEN
, bitmask_key
))
153 else if (test_bit (BTN_MOUSE
, bitmask_key
))
154 /* This path is taken by VMware's USB mouse, which has
155 * absolute axes, but no touch/pressure button. */
157 else if (test_bit (BTN_TOUCH
, bitmask_key
))
158 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
159 /* joysticks don't necessarily have to have buttons; e. g.
160 * rudders/pedals are joystick-like, but buttonless; they have
161 * other fancy axes */
162 else if (test_bit (BTN_TRIGGER
, bitmask_key
) ||
163 test_bit (BTN_A
, bitmask_key
) ||
164 test_bit (BTN_1
, bitmask_key
) ||
165 test_bit (ABS_RX
, bitmask_abs
) ||
166 test_bit (ABS_RY
, bitmask_abs
) ||
167 test_bit (ABS_RZ
, bitmask_abs
) ||
168 test_bit (ABS_THROTTLE
, bitmask_abs
) ||
169 test_bit (ABS_RUDDER
, bitmask_abs
) ||
170 test_bit (ABS_WHEEL
, bitmask_abs
) ||
171 test_bit (ABS_GAS
, bitmask_abs
) ||
172 test_bit (ABS_BRAKE
, bitmask_abs
))
173 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
176 if (test_bit (EV_REL
, bitmask_ev
) &&
177 test_bit (REL_X
, bitmask_rel
) && test_bit (REL_Y
, bitmask_rel
) &&
178 test_bit (BTN_MOUSE
, bitmask_key
))
182 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
184 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
187 /* key like devices */
188 static void test_key (struct udev_device
*dev
,
189 const unsigned long* bitmask_ev
,
190 const unsigned long* bitmask_key
,
196 /* do we have any KEY_* capability? */
197 if (!test_bit (EV_KEY
, bitmask_ev
)) {
198 log_debug("test_key: no EV_KEY capability");
202 /* only consider KEY_* here, not BTN_* */
204 for (i
= 0; i
< BTN_MISC
/BITS_PER_LONG
; ++i
) {
205 found
|= bitmask_key
[i
];
206 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i
*BITS_PER_LONG
, found
> 0);
208 /* If there are no keys in the lower block, check the higher block */
210 for (i
= KEY_OK
; i
< BTN_TRIGGER_HAPPY
; ++i
) {
211 if (test_bit (i
, bitmask_key
)) {
212 log_debug("test_key: Found key %x in high block", i
);
220 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
222 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
223 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
225 if ((bitmask_key
[0] & mask
) == mask
)
226 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
229 static int builtin_input_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
230 struct udev_device
*pdev
;
231 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
232 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
233 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
234 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
235 const char *sysname
, *devnode
;
237 /* walk up the parental chain until we find the real input device; the
238 * argument is very likely a subdevice of this, like eventN */
240 while (pdev
!= NULL
&& udev_device_get_sysattr_value(pdev
, "capabilities/ev") == NULL
)
241 pdev
= udev_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
);
244 /* Use this as a flag that input devices were detected, so that this
245 * program doesn't need to be called more than once per device */
246 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
247 get_cap_mask(dev
, pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
248 get_cap_mask(dev
, pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
249 get_cap_mask(dev
, pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
250 get_cap_mask(dev
, pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
251 test_pointers(dev
, bitmask_ev
, bitmask_abs
, bitmask_key
, bitmask_rel
, test
);
252 test_key(dev
, bitmask_ev
, bitmask_key
, test
);
255 devnode
= udev_device_get_devnode(dev
);
256 sysname
= udev_device_get_sysname(dev
);
257 if (devnode
&& sysname
&& startswith(sysname
, "event"))
258 extract_info(dev
, devnode
, test
);
263 const struct udev_builtin udev_builtin_input_id
= {
265 .cmd
= builtin_input_id
,
266 .help
= "Input device properties",