forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evdev.c
338 lines (287 loc) · 7.47 KB
/
evdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* ------------------------------------------------------------------------- *
* Copyright (C) 2012-2013 Jolla Ltd.
* Contact: Simo Piiroinen <[email protected]>
* License: GPLv2
* ------------------------------------------------------------------------- */
#include "evdev.h"
#include "mce-log.h"
#include <linux/input.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
/** Number of elements in array of fixed size */
#define numof(a) (sizeof(a)/sizeof*(a))
/* Autogenerated lookup tables */
#include "evdev.inc"
/** Number to string lookup with bounds checking
*/
static const char *lookup(const char * const *lut, size_t cnt, int val)
{
return ((size_t)val < cnt) ? lut[val] : 0;
}
/* Internal helper functions for looking up event codes by type */
static const char * syn_name(int ecode)
{
return lookup(lut_syn, numof(lut_syn), ecode) ?: "SYN_???";
}
static const char * key_name(int ecode)
{
return lookup(lut_key, numof(lut_key), ecode) ?: "KEY_???";
}
static const char * rel_name(int ecode)
{
return lookup(lut_rel, numof(lut_rel), ecode) ?: "REL_???";
}
static const char * abs_name(int ecode)
{
return lookup(lut_abs, numof(lut_abs), ecode) ?: "ABS_???";
}
static const char * msc_name(int ecode)
{
return lookup(lut_msc, numof(lut_msc), ecode) ?: "MSC_???";
}
static const char * led_name(int ecode)
{
return lookup(lut_led, numof(lut_led), ecode) ?: "LED_???";
}
static const char * rep_name(int ecode)
{
return lookup(lut_rep, numof(lut_rep), ecode) ?: "REP_???";
}
static const char * snd_name(int ecode)
{
return lookup(lut_snd, numof(lut_snd), ecode) ?: "SND_???";
}
static const char * sw_name(int ecode)
{
return lookup(lut_sw, numof(lut_sw), ecode) ?: "SW_???";
}
static const char * ff_name(int ecode)
{
return lookup(lut_ff, numof(lut_ff), ecode) ?: "FF_???";
}
static const char * pwr_name(int ecode)
{
(void)ecode; // not used
return "PWR_???";
//return lookup(lut_pwr, numof(lut_pwr), ecode) ?: "PWR_???";
}
/** How many "unsigned long" elements bitmap needs to cover bc bits */
#define BMAP_SIZE(bc) (((bc)+LONG_BIT-1)/LONG_BIT)
/** Test a bit in array of unsigned longs
*
* @param bmap array of longs
* @param bi bit index
*
* @return 1 if bit is set, 0 if not
*/
static int bit_is_set(unsigned long *bmap, size_t bi)
{
size_t i = bi / LONG_BIT;
unsigned long m = 1ul << (bi % LONG_BIT);
return (bmap[i] & m) != 0;
}
/** Helper for getting terminal width
*
* @return guestimate of number of characters per line
*/
static int get_terminal_width(void)
{
int res;
struct winsize ws;
const char *env;
if( (env = getenv("COLUMNS")) )
{
if( (res = strtol(env, 0, 10)) > 10 )
{
return res;
}
}
res = 80;
if( ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 ||
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) != -1 ||
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != -1 )
{
res = ws.ws_col;
}
return res;
}
/** Lookup human readable name for input event code
*
* @param etype input event type
* @param ecode input event code
*
* @return Name of input event code as string
*/
const char *evdev_get_event_code_name(int etype, int ecode)
{
typedef const char *(*lookup_fn)(int);
static const lookup_fn lut[] =
{
[EV_SYN] = syn_name,
[EV_KEY] = key_name,
[EV_REL] = rel_name,
[EV_ABS] = abs_name,
[EV_MSC] = msc_name,
[EV_SW] = sw_name,
[EV_LED] = led_name,
[EV_SND] = snd_name,
[EV_REP] = rep_name,
[EV_FF] = ff_name,
[EV_PWR] = pwr_name,
};
if( (size_t)etype < numof(lut) && lut[etype] )
{
return lut[etype](ecode);
}
return "???_???";
}
/** Lookup human readable name for input event type
*
* @param etype input event type
*
* @return Name of input event type as string
*/
const char * evdev_get_event_type_name(int etype)
{
return lookup(lut_ev, numof(lut_ev), etype) ?: "EV_???";
}
/** Open input device in read only mode
*
* @param path input device path to open
*
* @returns file descriptor on success, or
* -1 if the file can't be opened / it is not an input device
*/
int evdev_open_device(const char *path)
{
int fd;
if( (fd = open(path, O_RDONLY)) != -1 )
{
int vers = 0;
if( ioctl(fd, EVIOCGVERSION, &vers) == -1)
{
// assume this is not an input device node then
close(fd), fd = -1;
}
}
return fd;
}
/** Write input device information to stdout
*
* @param fd file descriptor
*
* @return 0 on success, or -1 in case of errors
*/
int evdev_identify_device(int fd)
{
int err = -1;
int vers;
int cols;
unsigned long bmap_type[BMAP_SIZE(EV_CNT)];
unsigned long bmap_code[BMAP_SIZE(KEY_CNT)];
unsigned long bmap_stat[BMAP_SIZE(KEY_CNT)];
char path[256];
int n;
if( fd < 0 )
{
goto cleanup;
}
snprintf(path, sizeof path, "/proc/self/fd/%d", fd);
if( (n = readlink(path, path, sizeof path - 1)) <= 0 )
{
strcpy(path, "unknown");
}
else
{
path[n] = 0;
}
// is it evdev file descriptor?
if( ioctl(fd, EVIOCGVERSION, &vers) == -1)
{
goto cleanup;
}
// device name
{
char name[256];;
if( ioctl(fd, EVIOCGNAME(sizeof(name)), name) == -1 )
{
mce_log(LL_WARN, "%s: EVIOCGNAME: %m", path);
strcpy(name, "Unknown");
}
printf("Name: \"%s\"\n", name);
}
// device id
{
unsigned short id[4];
if( ioctl(fd, EVIOCGID, id) == -1 )
{
mce_log(LL_WARN, "%s: EVIOCGID: %m", path);
memset(id, 0, sizeof id);
}
printf("ID: bus 0x%x, vendor, 0x%x, product 0x%x, version 0x%x\n",
id[ID_BUS], id[ID_VENDOR], id[ID_PRODUCT], id[ID_VERSION]);
}
// get bitmask of supported event types
memset(bmap_type, 0, sizeof(bmap_type));
if( ioctl(fd, EVIOCGBIT(0, EV_CNT), bmap_type) == -1 )
{
mce_log(LL_WARN, "%s: EVIOCGBIT(0): %m", path );
goto cleanup;
}
// guestimate how wide event code listing we can make
cols = get_terminal_width() - 10;
if( cols < 32 ) cols = 72;
// list supported event types and codes
for( int etype = 0; etype < EV_CNT; ++etype )
{
if( !bit_is_set(bmap_type, etype) )
{
continue;
}
printf("Type 0x%02x (%s)\n", etype, evdev_get_event_type_name(etype));
if( etype == EV_SYN || etype == EV_REP )
{
// EVIOCGBIT(0, n) returns event types supported, not
// what SYN_xxx codes are supported ... skip it
continue;
}
memset(bmap_code, 0, sizeof(bmap_code));
if( ioctl(fd, EVIOCGBIT(etype, KEY_CNT), bmap_code) == -1 )
{
mce_log(LL_WARN, "%s: EVIOCGBIT(%s): %m", path,
evdev_get_event_type_name(etype));
continue;
}
memset(bmap_stat, 0, sizeof(bmap_stat));
switch( etype )
{
case EV_KEY: ioctl(fd, EVIOCGKEY(KEY_CNT), bmap_stat); break;
case EV_LED: ioctl(fd, EVIOCGLED(LED_CNT), bmap_stat); break;
case EV_SND: ioctl(fd, EVIOCGSND(SND_CNT), bmap_stat); break;
case EV_SW: ioctl(fd, EVIOCGSW(SW_CNT), bmap_stat); break;
default: break;
}
int len = 0;
for( int ecode = 0; ecode < KEY_CNT; ++ecode )
{
if( bit_is_set(bmap_code, ecode) )
{
const char *tag = evdev_get_event_code_name(etype, ecode);
int set = bit_is_set(bmap_stat, ecode);
int add = strlen(tag) + 1 + set;
if( len == 0 ) printf("\t");
else if( len+add > cols ) printf("\n\t"), len = 0;
len += add, printf(" %s%s", tag, set ? "*" : "");
}
}
if( len ) printf("\n");
}
err = 0;
cleanup:
return err;
}