2 * input_id - input device classification
4 * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
5 * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with keymap; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 #include <linux/limits.h>
28 #include <linux/input.h>
31 #include "libudev-private.h"
33 /* we must use this kernel-compatible implementation */
34 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
35 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
36 #define OFF(x) ((x)%BITS_PER_LONG)
37 #define BIT(x) (1UL<<OFF(x))
38 #define LONG(x) ((x)/BITS_PER_LONG)
39 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
43 static void log_fn(struct udev *udev, int priority,
44 const char *file, int line, const char *fn,
45 const char *format, va_list args)
48 fprintf(stderr, "%s: ", fn);
49 vfprintf(stderr, format, args);
51 vsyslog(priority, format, args);
56 * Read a capability attribute and return bitmask.
57 * @param dev udev_device
58 * @param attr sysfs attribute name (e. g. "capabilities/key")
59 * @param bitmask: Output array which has a sizeof of bitmask_size
61 static void get_cap_mask (struct udev_device *dev, const char* attr,
62 unsigned long *bitmask, size_t bitmask_size)
69 snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(dev, attr));
71 info(udev_device_get_udev(dev), "%s raw kernel attribute: %s\n", attr, text);
73 memset (bitmask, 0, bitmask_size);
75 while ((word = strrchr(text, ' ')) != NULL) {
76 val = strtoul (word+1, NULL, 16);
77 if (i < bitmask_size/sizeof(unsigned long))
80 info(udev_device_get_udev(dev), "Ignoring %s block %lX which is larger than maximum size\n", attr, val);
84 val = strtoul (text, NULL, 16);
85 if (i < bitmask_size/sizeof(unsigned long))
88 info(udev_device_get_udev(dev), "Ignoring %s block %lX which is larger than maximum size\n", attr, val);
91 /* printf pattern with the right unsigned long number of hex chars */
92 snprintf(text, sizeof(text), " bit %%4u: %%0%zilX\n", 2*sizeof(unsigned long));
93 info(udev_device_get_udev(dev), "%s decoded bit map:\n", attr);
94 val = bitmask_size/sizeof (unsigned long);
95 /* skip over leading zeros */
96 while (bitmask[val-1] == 0 && val > 0)
98 for (i = 0; i < val; ++i)
99 info(udev_device_get_udev(dev), text, i * BITS_PER_LONG, bitmask[i]);
103 /* pointer devices */
104 static void test_pointers (const unsigned long* bitmask_ev,
105 const unsigned long* bitmask_abs,
106 const unsigned long* bitmask_key,
107 const unsigned long* bitmask_rel)
112 if (!test_bit (EV_KEY, bitmask_ev))
115 if (test_bit (EV_ABS, bitmask_ev) &&
116 test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
117 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
118 puts("ID_INPUT_TABLET=1");
119 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
121 else if (test_bit (BTN_TRIGGER, bitmask_key) ||
122 test_bit (BTN_A, bitmask_key) ||
123 test_bit (BTN_1, bitmask_key))
124 puts("ID_INPUT_JOYSTICK=1");
125 else if (test_bit (BTN_MOUSE, bitmask_key))
126 /* This path is taken by VMware's USB mouse, which has
127 * absolute axes, but no touch/pressure button. */
129 else if (test_bit (BTN_TOUCH, bitmask_key))
130 puts("ID_INPUT_TOUCHSCREEN=1");
133 if (test_bit (EV_REL, bitmask_ev) &&
134 test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) &&
135 test_bit (BTN_MOUSE, bitmask_key))
139 puts("ID_INPUT_MOUSE=1");
141 puts("ID_INPUT_TOUCHPAD=1");
144 /* key like devices */
145 static void test_key (struct udev *udev,
146 const unsigned long* bitmask_ev,
147 const unsigned long* bitmask_key)
153 /* do we have any KEY_* capability? */
154 if (!test_bit (EV_KEY, bitmask_ev)) {
155 info(udev, "test_key: no EV_KEY capability\n");
159 /* only consider KEY_* here, not BTN_* */
161 for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
162 found |= bitmask_key[i];
163 info(udev, "test_key: checking bit block %lu for any keys; found=%i\n", i*BITS_PER_LONG, found > 0);
165 /* If there are no keys in the lower block, check the higher block */
167 for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
168 if (test_bit (i, bitmask_key)) {
169 info(udev, "test_key: Found key %x in high block\n", i);
177 puts("ID_INPUT_KEY=1");
179 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
180 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
182 if ((bitmask_key[0] & mask) == mask)
183 puts("ID_INPUT_KEYBOARD=1");
188 printf("Usage: input_id [options] <device path>\n"
189 " --debug debug to stderr\n"
190 " --help print this help text\n\n");
193 int main (int argc, char** argv)
196 struct udev_device *dev;
198 static const struct option options[] = {
199 { "debug", no_argument, NULL, 'd' },
200 { "help", no_argument, NULL, 'h' },
204 char devpath[PATH_MAX];
205 unsigned long bitmask_ev[NBITS(EV_MAX)];
206 unsigned long bitmask_abs[NBITS(ABS_MAX)];
207 unsigned long bitmask_key[NBITS(KEY_MAX)];
208 unsigned long bitmask_rel[NBITS(REL_MAX)];
214 udev_log_init("input_id");
215 udev_set_log_fn(udev, log_fn);
217 /* CLI argument parsing */
221 option = getopt_long(argc, argv, "dxh", options, NULL);
228 if (udev_get_log_priority(udev) < LOG_INFO)
229 udev_set_log_priority(udev, LOG_INFO);
239 if (argv[optind] == NULL) {
245 snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]);
246 dev = udev_device_new_from_syspath(udev, devpath);
248 fprintf(stderr, "unable to access '%s'\n", devpath);
252 /* walk up the parental chain until we find the real input device; the
253 * argument is very likely a subdevice of this, like eventN */
254 while (dev != NULL && udev_device_get_sysattr_value(dev, "capabilities/ev") == NULL)
255 dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
257 /* not an "input" class device */
261 /* Use this as a flag that input devices were detected, so that this
262 * program doesn't need to be called more than once per device */
265 get_cap_mask (dev, "capabilities/ev", bitmask_ev, sizeof (bitmask_ev));
266 get_cap_mask (dev, "capabilities/abs", bitmask_abs, sizeof (bitmask_abs));
267 get_cap_mask (dev, "capabilities/rel", bitmask_rel, sizeof (bitmask_rel));
268 get_cap_mask (dev, "capabilities/key", bitmask_key, sizeof (bitmask_key));
270 test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
272 test_key(udev, bitmask_ev, bitmask_key);