chiark / gitweb /
input_id: check event mask
[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/input.h>
27
28 #include "libudev.h"
29
30 /* we must use this kernel-compatible implementation */
31 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
32 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
33 #define OFF(x)  ((x)%BITS_PER_LONG)
34 #define BIT(x)  (1UL<<OFF(x))
35 #define LONG(x) ((x)/BITS_PER_LONG)
36 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
37
38 /* 
39  * Read a capability attribute and return bitmask.
40  * @param dev udev_device
41  * @param attr sysfs attribute name (e. g. "capabilities/key")
42  * @param bitmask: Output array; must have max_size elements
43  */
44 static void get_cap_mask (struct udev_device *dev, const char* attr,
45                           unsigned long *bitmask, size_t max_size)
46 {
47         char text[4096];
48         int i;
49         char* word;
50         unsigned long val;
51
52         snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(dev, attr));
53
54         memset (bitmask, 0, max_size);
55         i = 0;
56         while ((word = strrchr(text, ' ')) != NULL) {
57                 val = strtoul (word+1, NULL, 16);
58                 bitmask[i] = val;
59                 *word = '\0';
60                 ++i;
61         }
62         val = strtoul (text, NULL, 16);
63         bitmask[i] = val;
64 }
65
66 /* pointer devices */
67 static void test_pointers (const unsigned long* bitmask_ev,
68                            const unsigned long* bitmask_abs, 
69                            const unsigned long* bitmask_key, 
70                            const unsigned long* bitmask_rel)
71 {
72         int is_mouse = 0;
73         int is_touchpad = 0;
74
75         if (test_bit (EV_ABS, bitmask_ev) && test_bit (EV_KEY, bitmask_ev) &&
76             test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
77                 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
78                         puts("ID_INPUT_TABLET=1");
79                 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
80                         is_touchpad = 1;
81                 else if (test_bit (BTN_TRIGGER, bitmask_key) || 
82                          test_bit (BTN_A, bitmask_key) || 
83                          test_bit (BTN_1, bitmask_key))
84                         puts("ID_INPUT_JOYSTICK=1");
85                 else if (test_bit (BTN_MOUSE, bitmask_key))
86                         /* This path is taken by VMware's USB mouse, which has
87                          * absolute axes, but no touch/pressure button. */
88                         is_mouse = 1;
89         }
90
91         if (test_bit (EV_REL, bitmask_ev) && 
92             test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel))
93                 is_mouse = 1;
94
95         if (is_mouse)
96                 puts("ID_INPUT_MOUSE=1");
97         if (is_touchpad)
98                 puts("ID_INPUT_TOUCHPAD=1");
99 }
100
101 /* key like devices */
102 static void test_key (const unsigned long* bitmask_ev, 
103                       const unsigned long* bitmask_key)
104 {
105         unsigned i;
106         unsigned long acc;
107         unsigned long mask;
108
109         /* do we have any KEY_* capability? */
110         if (!test_bit (EV_KEY, bitmask_ev))
111                 return;
112
113         acc = 0;
114         for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i)
115             acc |= bitmask_key[i];
116         if (acc > 0)
117                 puts("ID_INPUT_KEY=1");
118
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 */
121         mask = 0xFFFFFFFE;
122         if ((bitmask_key[0] & mask) == mask)
123                 puts("ID_INPUT_KEYBOARD=1");
124 }
125
126 int main (int argc, char** argv)
127 {
128         struct udev *udev;
129         struct udev_device *dev;
130
131         char devpath[PATH_MAX];
132         unsigned long bitmask_ev[NBITS(EV_MAX)];
133         unsigned long bitmask_abs[NBITS(ABS_MAX)];
134         unsigned long bitmask_key[NBITS(KEY_MAX)];
135         unsigned long bitmask_rel[NBITS(REL_MAX)];
136
137         if (argc != 2) {
138                 fprintf(stderr, "Usage: %s <device path (without /sys)>\n", argv[0]);
139                 exit(1);
140         }
141
142         /* get the device */
143         udev = udev_new();
144         if (udev == NULL)
145                 return 1;
146
147         snprintf(devpath, sizeof(devpath), "%s/%s", udev_get_sys_path(udev), argv[1]);
148         dev = udev_device_new_from_syspath(udev, devpath);
149         if (dev == NULL) {
150                 fprintf(stderr, "unable to access '%s'\n", devpath);
151                 return 1;
152         }
153
154         /* walk up the parental chain until we find the real input device; the
155          * argument is very likely a subdevice of this, like eventN */
156         while (dev != NULL && udev_device_get_sysattr_value(dev, "capabilities/ev") == NULL)
157                 dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
158
159         /* not an "input" class device */
160         if (dev == NULL)
161                 return 0;
162
163         /* Use this as a flag that input devices were detected, so that this
164          * program doesn't need to be called more than once per device */
165         puts("ID_INPUT=1");
166
167         get_cap_mask (dev, "capabilities/ev", bitmask_ev, sizeof (bitmask_ev));
168         get_cap_mask (dev, "capabilities/abs", bitmask_abs, sizeof (bitmask_abs));
169         get_cap_mask (dev, "capabilities/rel", bitmask_rel, sizeof (bitmask_rel));
170         get_cap_mask (dev, "capabilities/key", bitmask_key, sizeof (bitmask_key));
171
172         test_pointers(bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel);
173
174         test_key(bitmask_ev, bitmask_key);
175
176         return 0;
177 }