chiark / gitweb /
accelerometer: add orientation property
[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                 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");
118                 return;
119         }
120
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))
126                         is_touchpad = 1;
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. */
134                         is_mouse = 1;
135                 else if (test_bit (BTN_TOUCH, bitmask_key))
136                         puts("ID_INPUT_TOUCHSCREEN=1");
137         }
138
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))
142                 is_mouse = 1;
143
144         if (is_mouse)
145                 puts("ID_INPUT_MOUSE=1");
146         if (is_touchpad)
147                 puts("ID_INPUT_TOUCHPAD=1");
148 }
149
150 /* key like devices */
151 static void test_key (struct udev *udev,
152                       const unsigned long* bitmask_ev, 
153                       const unsigned long* bitmask_key)
154 {
155         unsigned i;
156         unsigned long found;
157         unsigned long mask;
158
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");
162                 return;
163         }
164
165         /* only consider KEY_* here, not BTN_* */
166         found = 0;
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);
170         }
171         /* If there are no keys in the lower block, check the higher block */
172         if (!found) {
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);
176                                 found = 1;
177                                 break;
178                         }
179                 }
180         }
181
182         if (found > 0)
183                 puts("ID_INPUT_KEY=1");
184
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 */
187         mask = 0xFFFFFFFE;
188         if ((bitmask_key[0] & mask) == mask)
189                 puts("ID_INPUT_KEYBOARD=1");
190 }
191
192 static void help(void)
193 {
194         printf("Usage: input_id [options] <device path>\n"
195                "  --debug         debug to stderr\n"
196                "  --help          print this help text\n\n");
197 }
198
199 int main (int argc, char** argv)
200 {
201         struct udev *udev;
202         struct udev_device *dev;
203
204         static const struct option options[] = {
205                 { "debug", no_argument, NULL, 'd' },
206                 { "help", no_argument, NULL, 'h' },
207                 {}
208         };
209
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)];
215
216         udev = udev_new();
217         if (udev == NULL)
218                 return 1;
219
220         udev_log_init("input_id");
221         udev_set_log_fn(udev, log_fn);
222
223         /* CLI argument parsing */
224         while (1) {
225                 int option;
226
227                 option = getopt_long(argc, argv, "dxh", options, NULL);
228                 if (option == -1)
229                         break;
230
231                 switch (option) {
232                 case 'd':
233                         debug = 1;
234                         if (udev_get_log_priority(udev) < LOG_INFO)
235                                 udev_set_log_priority(udev, LOG_INFO);
236                         break;
237                 case 'h':
238                         help();
239                         exit(0);
240                 default:
241                         exit(1);
242                 }
243         }
244
245         if (argv[optind] == NULL) {
246                 help();
247                 exit(1);
248         }
249
250         /* get the device */
251         snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[optind]);
252         dev = udev_device_new_from_syspath(udev, devpath);
253         if (dev == NULL) {
254                 fprintf(stderr, "unable to access '%s'\n", devpath);
255                 return 1;
256         }
257
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);
262
263         /* not an "input" class device */
264         if (dev == NULL)
265                 return 0;
266
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 */
269         puts("ID_INPUT=1");
270
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));
275
276         test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
277
278         test_key(udev, bitmask_ev, bitmask_key);
279
280         return 0;
281 }