chiark / gitweb /
2e143a5628ccc508fc5baf8b6383e8f1e7f188b3
[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 /* 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)
35
36 /* 
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
40  */
41 static void get_cap_mask (const char *path, unsigned long *bitmask, size_t max_size)
42 {
43         FILE* f;
44         int i;
45         char text[4096];
46         char* word;
47         unsigned long val;
48
49         f = fopen(path, "r");
50         if (f == NULL) {
51                 perror("opening caps file");
52                 exit(1);
53         }
54         if (fgets(text, sizeof(text), f) == NULL) {
55                 perror("fgets");
56                 exit(1);
57         }
58         fclose(f);
59         
60         memset (bitmask, 0, max_size);
61         i = 0;
62         while ((word = strrchr(text, ' ')) != NULL) {
63                 val = strtoul (word+1, NULL, 16);
64                 bitmask[i] = val;
65                 *word = '\0';
66                 ++i;
67         }
68         val = strtoul (text, NULL, 16);
69         bitmask[i] = val;
70 }
71
72 /* pointer devices */
73 static void test_pointers (const unsigned long* bitmask_abs, const unsigned long* bitmask_key, const unsigned long* bitmask_rel)
74 {
75         int is_mouse = 0;
76         int is_touchpad = 0;
77
78         if (test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
79                 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
80                         puts("ID_INPUT_TABLET=1");
81                 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
82                         is_touchpad = 1;
83                 else if (test_bit (BTN_TRIGGER, bitmask_key) || 
84                          test_bit (BTN_A, bitmask_key) || 
85                          test_bit (BTN_1, bitmask_key))
86                         puts("ID_INPUT_JOYSTICK=1");
87                 else if (test_bit (BTN_MOUSE, bitmask_key))
88                         /* This path is taken by VMware's USB mouse, which has
89                          * absolute axes, but no touch/pressure button. */
90                         is_mouse = 1;
91         }
92
93         if (test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel))
94                 is_mouse = 1;
95
96         if (is_mouse)
97                 puts("ID_INPUT_MOUSE=1");
98         if (is_touchpad)
99                 puts("ID_INPUT_TOUCHPAD=1");
100 }
101
102 /* key like devices */
103 static void test_key (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         acc = 0;
111         for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i)
112             acc |= bitmask_key[i];
113         if (acc > 0)
114                 puts("ID_INPUT_KEY=1");
115
116         /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
117          * those, consider it a full keyboard; do not test KEY_RESERVED, though */
118         mask = 0xFFFFFFFE;
119         if ((bitmask_key[0] & mask) == mask)
120                 puts("ID_INPUT_KEYBOARD=1");
121 }
122
123 int main (int argc, char** argv)
124 {
125         char capfile[PATH_MAX];
126         unsigned long bitmask_abs[NBITS(ABS_MAX)];
127         unsigned long bitmask_key[NBITS(KEY_MAX)];
128         unsigned long bitmask_rel[NBITS(REL_MAX)];
129
130         if (argc != 2) {
131                 fprintf(stderr, "Usage: %s <device path (without /sys)>\n", argv[0]);
132                 exit(1);
133         }
134
135         /* Use this as a flag that input devices were detected, so that this
136          * program doesn't need to be called more than once per device */
137         puts("ID_INPUT=1");
138
139         snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/abs", argv[1]);
140         get_cap_mask (capfile, bitmask_abs, sizeof (bitmask_abs));
141         snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/rel", argv[1]);
142         get_cap_mask (capfile, bitmask_rel, sizeof (bitmask_rel));
143         snprintf(capfile, sizeof(capfile), "/sys/%s/device/capabilities/key", argv[1]);
144         get_cap_mask (capfile, bitmask_key, sizeof (bitmask_key));
145
146         test_pointers(bitmask_abs, bitmask_key, bitmask_rel);
147
148         test_key(bitmask_key);
149
150         return 0;
151 }