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.
26 #include <linux/input.h>
28 /* we must use this kernel-compatible implementation */
29 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
30 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
31 #define OFF(x) ((x)%BITS_PER_LONG)
32 #define BIT(x) (1UL<<OFF(x))
33 #define LONG(x) ((x)/BITS_PER_LONG)
34 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
37 * Read a capabilities/name file and return bitmask.
38 * @param path File to open
39 * @param bitmask: Output array; must have max_size elements
41 static void get_cap_mask (const char *path, unsigned long *bitmask, size_t max_size)
51 perror("opening caps file");
54 if (fgets(text, sizeof(text), f) == NULL) {
60 memset (bitmask, 0, max_size);
62 while ((word = strrchr(text, ' ')) != NULL) {
63 val = strtoul (word+1, NULL, 16);
68 val = strtoul (text, NULL, 16);
73 static void test_pointers (const unsigned long* bitmask_abs, const unsigned long* bitmask_key, const unsigned long* bitmask_rel)
78 if (test_bit (ABS_PRESSURE, bitmask_abs))
81 if (test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
82 if (test_bit (BTN_STYLUS, bitmask_key))
83 puts("ID_INPUT_TABLET=1");
84 else if (test_bit (BTN_TOUCH, bitmask_key))
86 else if (test_bit (BTN_TRIGGER, bitmask_key) ||
87 test_bit (BTN_A, bitmask_key) ||
88 test_bit (BTN_1, bitmask_key))
89 puts("ID_INPUT_JOYSTICK=1");
90 else if (test_bit (BTN_MOUSE, bitmask_key))
91 /* This path is taken by VMware's USB mouse, which has
92 * absolute axes, but no touch/pressure button. */
96 if (test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel))
100 puts("ID_INPUT_MOUSE=1");
102 puts("ID_INPUT_TOUCHPAD=1");
105 /* key like devices */
106 static void test_key (const unsigned long* bitmask_key)
112 /* do we have any KEY_* capability? */
114 for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i)
115 acc |= bitmask_key[i];
117 puts("ID_INPUT_KEY=1");
119 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
120 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
122 if ((bitmask_key[0] & mask) == mask)
123 puts("ID_INPUT_KEYBOARD=1");
126 int main (int argc, char** argv)
128 char capfile[PATH_MAX];
129 unsigned long bitmask_abs[NBITS(ABS_MAX)];
130 unsigned long bitmask_key[NBITS(KEY_MAX)];
131 unsigned long bitmask_rel[NBITS(REL_MAX)];
134 fprintf(stderr, "Usage: %s <device path (without /sys)>\n", argv[0]);
138 /* Use this as a flag that input devices were detected, so that this
139 * program doesn't need to be called more than once per device */
142 snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/abs", argv[1]);
143 get_cap_mask (capfile, bitmask_abs, sizeof (bitmask_abs));
144 snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/rel", argv[1]);
145 get_cap_mask (capfile, bitmask_rel, sizeof (bitmask_rel));
146 snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/key", argv[1]);
147 get_cap_mask (capfile, bitmask_key, sizeof (bitmask_key));
149 test_pointers(bitmask_abs, bitmask_key, bitmask_rel);
151 test_key(bitmask_key);