chiark / gitweb /
input_id: Cover key devices which only have KEY_* > 255
[elogind.git] / extras / input_id / input_id.c
1 /*
2  * input_id - input device classification
3  *
4  * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
5  * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <linux/limits.h>
27 #include <linux/input.h>
28
29 #include "libudev.h"
30
31 /* we must use this kernel-compatible implementation */
32 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
33 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
34 #define OFF(x)  ((x)%BITS_PER_LONG)
35 #define BIT(x)  (1UL<<OFF(x))
36 #define LONG(x) ((x)/BITS_PER_LONG)
37 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
38
39 static int debug = 0;
40 #define DBG(format, args...) { if (debug) fprintf(stderr, format, ##args); }
41
42 /* 
43  * Read a capability attribute and return bitmask.
44  * @param dev udev_device
45  * @param attr sysfs attribute name (e. g. "capabilities/key")
46  * @param bitmask: Output array which has a sizeof of bitmask_size
47  */
48 static void get_cap_mask (struct udev_device *dev, const char* attr,
49                           unsigned long *bitmask, size_t bitmask_size)
50 {
51         char text[4096];
52         unsigned i;
53         char* word;
54         unsigned long val;
55
56         snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(dev, attr));
57
58         DBG("%s raw kernel attribute: %s\n", attr, text);
59
60         memset (bitmask, 0, bitmask_size);
61         i = 0;
62         while ((word = strrchr(text, ' ')) != NULL) {
63                 val = strtoul (word+1, NULL, 16);
64                 if (i < bitmask_size/sizeof(unsigned long))
65                         bitmask[i] = val;
66                 else
67                         DBG("Ignoring %s block %lX which is larger than maximum size\n", attr, val);
68                 *word = '\0';
69                 ++i;
70         }
71         val = strtoul (text, NULL, 16);
72         if (i < bitmask_size/sizeof(unsigned long))
73                 bitmask[i] = val;
74         else
75                 DBG("Ignoring %s block %lX which is larger than maximum size\n", attr, val);
76
77         if (debug) {
78                 /* printf pattern with the right unsigned long number of hex chars */
79                 snprintf(text, sizeof(text), "  bit %%4u: %%0%zilX\n", 2*sizeof(unsigned long));
80                 DBG("%s decoded bit map:\n", attr);
81                 val = bitmask_size/sizeof (unsigned long);
82                 /* skip over leading zeros */
83                 while (bitmask[val-1] == 0 && val > 0)
84                     --val;
85                 for (i = 0; i < val; ++i)
86                         DBG(text, i * BITS_PER_LONG, bitmask[i]);
87         }
88 }
89
90 /* pointer devices */
91 static void test_pointers (const unsigned long* bitmask_ev,
92                            const unsigned long* bitmask_abs, 
93                            const unsigned long* bitmask_key, 
94                            const unsigned long* bitmask_rel)
95 {
96         int is_mouse = 0;
97         int is_touchpad = 0;
98
99         if (!test_bit (EV_KEY, bitmask_ev))
100                 return;
101
102         if (test_bit (EV_ABS, bitmask_ev) &&
103             test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
104                 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
105                         puts("ID_INPUT_TABLET=1");
106                 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
107                         is_touchpad = 1;
108                 else if (test_bit (BTN_TRIGGER, bitmask_key) || 
109                          test_bit (BTN_A, bitmask_key) || 
110                          test_bit (BTN_1, bitmask_key))
111                         puts("ID_INPUT_JOYSTICK=1");
112                 else if (test_bit (BTN_MOUSE, bitmask_key))
113                         /* This path is taken by VMware's USB mouse, which has
114                          * absolute axes, but no touch/pressure button. */
115                         is_mouse = 1;
116                 else if (test_bit (BTN_TOUCH, bitmask_key))
117                         puts("ID_INPUT_TOUCHSCREEN=1");
118         }
119
120         if (test_bit (EV_REL, bitmask_ev) && 
121             test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) &&
122             test_bit (BTN_MOUSE, bitmask_key))
123                 is_mouse = 1;
124
125         if (is_mouse)
126                 puts("ID_INPUT_MOUSE=1");
127         if (is_touchpad)
128                 puts("ID_INPUT_TOUCHPAD=1");
129 }
130
131 /* key like devices */
132 static void test_key (const unsigned long* bitmask_ev, 
133                       const unsigned long* bitmask_key)
134 {
135         unsigned i;
136         unsigned long found;
137         unsigned long mask;
138
139         /* do we have any KEY_* capability? */
140         if (!test_bit (EV_KEY, bitmask_ev)) {
141                 DBG("test_key: no EV_KEY capability\n");
142                 return;
143         }
144
145         /* only consider KEY_* here, not BTN_* */
146         found = 0;
147         for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
148                 found |= bitmask_key[i];
149                 DBG("test_key: checking bit block %lu for any keys; found=%i\n", i*BITS_PER_LONG, found > 0);
150         }
151         /* If there are no keys in the lower block, check the higher block */
152         if (!found) {
153                 for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
154                         if (test_bit (i, bitmask_key)) {
155                                 DBG("test_key: Found key %x in high block\n", i);
156                                 found = 1;
157                                 break;
158                         }
159                 }
160         }
161
162         if (found > 0)
163                 puts("ID_INPUT_KEY=1");
164
165         /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
166          * those, consider it a full keyboard; do not test KEY_RESERVED, though */
167         mask = 0xFFFFFFFE;
168         if ((bitmask_key[0] & mask) == mask)
169                 puts("ID_INPUT_KEYBOARD=1");
170 }
171
172 int main (int argc, char** argv)
173 {
174         struct udev *udev;
175         struct udev_device *dev;
176
177         char devpath[PATH_MAX];
178         unsigned long bitmask_ev[NBITS(EV_MAX)];
179         unsigned long bitmask_abs[NBITS(ABS_MAX)];
180         unsigned long bitmask_key[NBITS(KEY_MAX)];
181         unsigned long bitmask_rel[NBITS(REL_MAX)];
182
183         if (argc != 2) {
184                 fprintf(stderr, "Usage: %s <device path (without /sys)>\n", argv[0]);
185                 exit(1);
186         }
187
188         if (getenv ("DEBUG"))
189                 debug = 1;
190
191         /* get the device */
192         udev = udev_new();
193         if (udev == NULL)
194                 return 1;
195
196         snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[1]);
197         dev = udev_device_new_from_syspath(udev, devpath);
198         if (dev == NULL) {
199                 fprintf(stderr, "unable to access '%s'\n", devpath);
200                 return 1;
201         }
202
203         /* walk up the parental chain until we find the real input device; the
204          * argument is very likely a subdevice of this, like eventN */
205         while (dev != NULL && udev_device_get_sysattr_value(dev, "capabilities/ev") == NULL)
206                 dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
207
208         /* not an "input" class device */
209         if (dev == NULL)
210                 return 0;
211
212         /* Use this as a flag that input devices were detected, so that this
213          * program doesn't need to be called more than once per device */
214         puts("ID_INPUT=1");
215
216         get_cap_mask (dev, "capabilities/ev", bitmask_ev, sizeof (bitmask_ev));
217         get_cap_mask (dev, "capabilities/abs", bitmask_abs, sizeof (bitmask_abs));
218         get_cap_mask (dev, "capabilities/rel", bitmask_rel, sizeof (bitmask_rel));
219         get_cap_mask (dev, "capabilities/key", bitmask_key, sizeof (bitmask_key));
220
221         test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
222
223         test_key(bitmask_ev, bitmask_key);
224
225         return 0;
226 }