chiark / gitweb /
input_id: silent gcc warnings
[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 <getopt.h>
26 #include <limits.h>
27 #include <linux/limits.h>
28 #include <linux/input.h>
29
30 #include "libudev.h"
31 #include "libudev-private.h"
32
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)
40
41 static int debug = 0;
42
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)
46 {
47         if (debug) {
48                 fprintf(stderr, "%s: ", fn);
49                 vfprintf(stderr, format, args);
50         } else {
51                 vsyslog(priority, format, args);
52         }
53 }
54
55 /* 
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
60  */
61 static void get_cap_mask (struct udev_device *dev, const char* attr,
62                           unsigned long *bitmask, size_t bitmask_size)
63 {
64         struct udev *udev = udev_device_get_udev(dev);
65         char text[4096];
66         unsigned i;
67         char* word;
68         unsigned long val;
69
70         snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(dev, attr));
71         info(udev, "%s raw kernel attribute: %s\n", attr, text);
72
73         memset (bitmask, 0, bitmask_size);
74         i = 0;
75         while ((word = strrchr(text, ' ')) != NULL) {
76                 val = strtoul (word+1, NULL, 16);
77                 if (i < bitmask_size/sizeof(unsigned long))
78                         bitmask[i] = val;
79                 else
80                         info(udev, "ignoring %s block %lX which is larger than maximum size\n", attr, val);
81                 *word = '\0';
82                 ++i;
83         }
84         val = strtoul (text, NULL, 16);
85         if (i < bitmask_size / sizeof(unsigned long))
86                 bitmask[i] = val;
87         else
88                 info(udev, "ignoring %s block %lX which is larger than maximum size\n", attr, val);
89
90         if (debug) {
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)
97                         --val;
98                 for (i = 0; i < val; ++i)
99                         info(udev, text, i * BITS_PER_LONG, bitmask[i]);
100         }
101 }
102
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)
108 {
109         int is_mouse = 0;
110         int is_touchpad = 0;
111
112         if (!test_bit (EV_KEY, bitmask_ev))
113                 return;
114
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))
120                         is_touchpad = 1;
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. */
128                         is_mouse = 1;
129                 else if (test_bit (BTN_TOUCH, bitmask_key))
130                         puts("ID_INPUT_TOUCHSCREEN=1");
131         }
132
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))
136                 is_mouse = 1;
137
138         if (is_mouse)
139                 puts("ID_INPUT_MOUSE=1");
140         if (is_touchpad)
141                 puts("ID_INPUT_TOUCHPAD=1");
142 }
143
144 /* key like devices */
145 static void test_key (struct udev *udev,
146                       const unsigned long* bitmask_ev, 
147                       const unsigned long* bitmask_key)
148 {
149         unsigned i;
150         unsigned long found;
151         unsigned long mask;
152
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");
156                 return;
157         }
158
159         /* only consider KEY_* here, not BTN_* */
160         found = 0;
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);
164         }
165         /* If there are no keys in the lower block, check the higher block */
166         if (!found) {
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);
170                                 found = 1;
171                                 break;
172                         }
173                 }
174         }
175
176         if (found > 0)
177                 puts("ID_INPUT_KEY=1");
178
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 */
181         mask = 0xFFFFFFFE;
182         if ((bitmask_key[0] & mask) == mask)
183                 puts("ID_INPUT_KEYBOARD=1");
184 }
185
186 static void help(void)
187 {
188         printf("Usage: input_id [options] <device path>\n"
189                "  --debug         debug to stderr\n"
190                "  --help          print this help text\n\n");
191 }
192
193 int main (int argc, char** argv)
194 {
195         struct udev *udev;
196         struct udev_device *dev;
197
198         static const struct option options[] = {
199                 { "debug", no_argument, NULL, 'd' },
200                 { "help", no_argument, NULL, 'h' },
201                 {}
202         };
203
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)];
209
210         udev = udev_new();
211         if (udev == NULL)
212                 return 1;
213
214         udev_log_init("input_id");
215         udev_set_log_fn(udev, log_fn);
216
217         /* CLI argument parsing */
218         while (1) {
219                 int option;
220
221                 option = getopt_long(argc, argv, "dxh", options, NULL);
222                 if (option == -1)
223                         break;
224
225                 switch (option) {
226                 case 'd':
227                         debug = 1;
228                         if (udev_get_log_priority(udev) < LOG_INFO)
229                                 udev_set_log_priority(udev, LOG_INFO);
230                         break;
231                 case 'h':
232                         help();
233                         exit(0);
234                 default:
235                         exit(1);
236                 }
237         }
238
239         if (argv[optind] == NULL) {
240                 help();
241                 exit(1);
242         }
243
244         /* get the device */
245         snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]);
246         dev = udev_device_new_from_syspath(udev, devpath);
247         if (dev == NULL) {
248                 fprintf(stderr, "unable to access '%s'\n", devpath);
249                 return 1;
250         }
251
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);
256
257         /* not an "input" class device */
258         if (dev == NULL)
259                 return 0;
260
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 */
263         puts("ID_INPUT=1");
264
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));
269
270         test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
271
272         test_key(udev, bitmask_ev, bitmask_key);
273
274         return 0;
275 }