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)
64 struct udev *udev = udev_device_get_udev(dev);
70 snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(dev, attr));
71 info(udev, "%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, "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, "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, "%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, 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)) {
113 if (test_bit (EV_ABS, bitmask_ev) &&
114 test_bit (ABS_X, bitmask_abs) &&
115 test_bit (ABS_Y, bitmask_abs) &&
116 test_bit (ABS_Z, bitmask_abs))
117 puts("ID_INPUT_ACCELEROMETER=1");
121 if (test_bit (EV_ABS, bitmask_ev) &&
122 test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
123 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
124 puts("ID_INPUT_TABLET=1");
125 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
127 else if (test_bit (BTN_TRIGGER, bitmask_key) ||
128 test_bit (BTN_A, bitmask_key) ||
129 test_bit (BTN_1, bitmask_key))
130 puts("ID_INPUT_JOYSTICK=1");
131 else if (test_bit (BTN_MOUSE, bitmask_key))
132 /* This path is taken by VMware's USB mouse, which has
133 * absolute axes, but no touch/pressure button. */
135 else if (test_bit (BTN_TOUCH, bitmask_key))
136 puts("ID_INPUT_TOUCHSCREEN=1");
139 if (test_bit (EV_REL, bitmask_ev) &&
140 test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) &&
141 test_bit (BTN_MOUSE, bitmask_key))
145 puts("ID_INPUT_MOUSE=1");
147 puts("ID_INPUT_TOUCHPAD=1");
150 /* key like devices */
151 static void test_key (struct udev *udev,
152 const unsigned long* bitmask_ev,
153 const unsigned long* bitmask_key)
159 /* do we have any KEY_* capability? */
160 if (!test_bit (EV_KEY, bitmask_ev)) {
161 info(udev, "test_key: no EV_KEY capability\n");
165 /* only consider KEY_* here, not BTN_* */
167 for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
168 found |= bitmask_key[i];
169 info(udev, "test_key: checking bit block %lu for any keys; found=%i\n", i*BITS_PER_LONG, found > 0);
171 /* If there are no keys in the lower block, check the higher block */
173 for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
174 if (test_bit (i, bitmask_key)) {
175 info(udev, "test_key: Found key %x in high block\n", i);
183 puts("ID_INPUT_KEY=1");
185 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
186 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
188 if ((bitmask_key[0] & mask) == mask)
189 puts("ID_INPUT_KEYBOARD=1");
192 static void help(void)
194 printf("Usage: input_id [options] <device path>\n"
195 " --debug debug to stderr\n"
196 " --help print this help text\n\n");
199 int main (int argc, char** argv)
202 struct udev_device *dev;
204 static const struct option options[] = {
205 { "debug", no_argument, NULL, 'd' },
206 { "help", no_argument, NULL, 'h' },
210 char devpath[PATH_MAX];
211 unsigned long bitmask_ev[NBITS(EV_MAX)];
212 unsigned long bitmask_abs[NBITS(ABS_MAX)];
213 unsigned long bitmask_key[NBITS(KEY_MAX)];
214 unsigned long bitmask_rel[NBITS(REL_MAX)];
220 udev_log_init("input_id");
221 udev_set_log_fn(udev, log_fn);
223 /* CLI argument parsing */
227 option = getopt_long(argc, argv, "dxh", options, NULL);
234 if (udev_get_log_priority(udev) < LOG_INFO)
235 udev_set_log_priority(udev, LOG_INFO);
245 if (argv[optind] == NULL) {
251 snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]);
252 dev = udev_device_new_from_syspath(udev, devpath);
254 fprintf(stderr, "unable to access '%s'\n", devpath);
258 /* walk up the parental chain until we find the real input device; the
259 * argument is very likely a subdevice of this, like eventN */
260 while (dev != NULL && udev_device_get_sysattr_value(dev, "capabilities/ev") == NULL)
261 dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
263 /* not an "input" class device */
267 /* Use this as a flag that input devices were detected, so that this
268 * program doesn't need to be called more than once per device */
271 get_cap_mask (dev, "capabilities/ev", bitmask_ev, sizeof (bitmask_ev));
272 get_cap_mask (dev, "capabilities/abs", bitmask_abs, sizeof (bitmask_abs));
273 get_cap_mask (dev, "capabilities/rel", bitmask_rel, sizeof (bitmask_rel));
274 get_cap_mask (dev, "capabilities/key", bitmask_key, sizeof (bitmask_key));
276 test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
278 test_key(udev, bitmask_ev, bitmask_key);