aboutsummaryrefslogtreecommitdiff
path: root/frontend/linux/in_evdev.c
blob: 23d2e2a377e9ea649f1eeebba473c6bc7fa4ea88 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
 * (C) Gražvydas "notaz" Ignotas, 2008-2010
 *
 * This work is licensed under the terms of any of these licenses
 * (at your option):
 *  - GNU GPL, version 2 or later.
 *  - GNU LGPL, version 2.1 or later.
 * See the COPYING file in the top-level directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/input.h>
#include <errno.h>

#include "../common/input.h"
#include "in_evdev.h"

#define MAX_ABS_DEVS 8

typedef struct {
	int fd;
	int *kbits;
	int abs_min_x; /* abs->digital mapping */
	int abs_max_x;
	int abs_min_y;
	int abs_max_y;
	int abs_lzone;
	int abs_lastx;
	int abs_lasty;
	int kc_first;
	int kc_last;
	unsigned int abs_count;
	int abs_mult[MAX_ABS_DEVS]; /* 16.16 multiplier to IN_ABS_RANGE */
	int abs_adj[MAX_ABS_DEVS];  /* adjust for centering */
	unsigned int abs_to_digital:1;
} in_evdev_t;

#ifndef KEY_CNT
#define KEY_CNT (KEY_MAX + 1)
#endif

#define KEYBITS_BIT(x) (keybits[(x)/sizeof(keybits[0])/8] & \
	(1 << ((x) & (sizeof(keybits[0])*8-1))))

#define KEYBITS_BIT_SET(x) (keybits[(x)/sizeof(keybits[0])/8] |= \
	(1 << ((x) & (sizeof(keybits[0])*8-1))))

#define KEYBITS_BIT_CLEAR(x) (keybits[(x)/sizeof(keybits[0])/8] &= \
	~(1 << ((x) & (sizeof(keybits[0])*8-1))))

int in_evdev_allow_abs_only;

#define IN_EVDEV_PREFIX "evdev:"

static const char * const in_evdev_keys[KEY_CNT] = {
	[0 ... KEY_MAX] = NULL,
	[KEY_RESERVED] = "Reserved",		[KEY_ESC] = "Esc",
	[KEY_1] = "1",				[KEY_2] = "2",
	[KEY_3] = "3",				[KEY_4] = "4",
	[KEY_5] = "5",				[KEY_6] = "6",
	[KEY_7] = "7",				[KEY_8] = "8",
	[KEY_9] = "9",				[KEY_0] = "0",
	[KEY_MINUS] = "Minus",			[KEY_EQUAL] = "Equal",
	[KEY_BACKSPACE] = "Backspace",		[KEY_TAB] = "Tab",
	[KEY_Q] = "Q",				[KEY_W] = "W",
	[KEY_E] = "E",				[KEY_R] = "R",
	[KEY_T] = "T",				[KEY_Y] = "Y",
	[KEY_U] = "U",				[KEY_I] = "I",
	[KEY_O] = "O",				[KEY_P] = "P",
	[KEY_LEFTBRACE] = "LeftBrace",		[KEY_RIGHTBRACE] = "RightBrace",
	[KEY_ENTER] = "Enter",			[KEY_LEFTCTRL] = "LeftControl",
	[KEY_A] = "A",				[KEY_S] = "S",
	[KEY_D] = "D",				[KEY_F] = "F",
	[KEY_G] = "G",				[KEY_H] = "H",
	[KEY_J] = "J",				[KEY_K] = "K",
	[KEY_L] = "L",				[KEY_SEMICOLON] = "Semicolon",
	[KEY_APOSTROPHE] = "Apostrophe",	[KEY_GRAVE] = "Grave",
	[KEY_LEFTSHIFT] = "LeftShift",		[KEY_BACKSLASH] = "BackSlash",
	[KEY_Z] = "Z",				[KEY_X] = "X",
	[KEY_C] = "C",				[KEY_V] = "V",
	[KEY_B] = "B",				[KEY_N] = "N",
	[KEY_M] = "M",				[KEY_COMMA] = "Comma",
	[KEY_DOT] = "Dot",			[KEY_SLASH] = "Slash",
	[KEY_RIGHTSHIFT] = "RightShift",	[KEY_KPASTERISK] = "KPAsterisk",
	[KEY_LEFTALT] = "LeftAlt",		[KEY_SPACE] = "Space",
	[KEY_CAPSLOCK] = "CapsLock",		[KEY_F1] = "F1",
	[KEY_F2] = "F2",			[KEY_F3] = "F3",
	[KEY_F4] = "F4",			[KEY_F5] = "F5",
	[KEY_F6] = "F6",			[KEY_F7] = "F7",
	[KEY_F8] = "F8",			[KEY_F9] = "F9",
	[KEY_F10] = "F10",			[KEY_NUMLOCK] = "NumLock",
	[KEY_SCROLLLOCK] = "ScrollLock",	[KEY_KP7] = "KP7",
	[KEY_KP8] = "KP8",			[KEY_KP9] = "KP9",
	[KEY_KPMINUS] = "KPMinus",		[KEY_KP4] = "KP4",
	[KEY_KP5] = "KP5",			[KEY_KP6] = "KP6",
	[KEY_KPPLUS] = "KPPlus",		[KEY_KP1] = "KP1",
	[KEY_KP2] = "KP2",			[KEY_KP3] = "KP3",
	[KEY_KP0] = "KP0",			[KEY_KPDOT] = "KPDot",
	[KEY_ZENKAKUHANKAKU] = "Zenkaku/Hankaku", [KEY_102ND] = "102nd",
	[KEY_F11] = "F11",			[KEY_F12] = "F12",
	[KEY_KPJPCOMMA] = "KPJpComma",		[KEY_KPENTER] = "KPEnter",
	[KEY_RIGHTCTRL] = "RightCtrl",		[KEY_KPSLASH] = "KPSlash",
	[KEY_SYSRQ] = "SysRq",			[KEY_RIGHTALT] = "RightAlt",
	[KEY_LINEFEED] = "LineFeed",		[KEY_HOME] = "Home",
	[KEY_UP] = "Up",			[KEY_PAGEUP] = "PageUp",
	[KEY_LEFT] = "Left",			[KEY_RIGHT] = "Right",
	[KEY_END] = "End",			[KEY_DOWN] = "Down",
	[KEY_PAGEDOWN] = "PageDown",		[KEY_INSERT] = "Insert",
	[KEY_DELETE] = "Delete",		[KEY_MACRO] = "Macro",
	[KEY_HELP] = "Help",			[KEY_MENU] = "Menu",
	[KEY_COFFEE] = "Coffee",		[KEY_DIRECTION] = "Direction",
	[BTN_0] = "Btn0",			[BTN_1] = "Btn1",
	[BTN_2] = "Btn2",			[BTN_3] = "Btn3",
	[BTN_4] = "Btn4",			[BTN_5] = "Btn5",
	[BTN_6] = "Btn6",			[BTN_7] = "Btn7",
	[BTN_8] = "Btn8",			[BTN_9] = "Btn9",
	[BTN_LEFT] = "LeftBtn",			[BTN_RIGHT] = "RightBtn",
	[BTN_MIDDLE] = "MiddleBtn",		[BTN_SIDE] = "SideBtn",
	[BTN_EXTRA] = "ExtraBtn",		[BTN_FORWARD] = "ForwardBtn",
	[BTN_BACK] = "BackBtn",			[BTN_TASK] = "TaskBtn",
	[BTN_TRIGGER] = "Trigger",		[BTN_THUMB] = "ThumbBtn",
	[BTN_THUMB2] = "ThumbBtn2",		[BTN_TOP] = "TopBtn",
	[BTN_TOP2] = "TopBtn2",			[BTN_PINKIE] = "PinkieBtn",
	[BTN_BASE] = "BaseBtn",			[BTN_BASE2] = "BaseBtn2",
	[BTN_BASE3] = "BaseBtn3",		[BTN_BASE4] = "BaseBtn4",
	[BTN_BASE5] = "BaseBtn5",		[BTN_BASE6] = "BaseBtn6",
	[BTN_DEAD] = "BtnDead",			[BTN_A] = "BtnA",
	[BTN_B] = "BtnB",			[BTN_C] = "BtnC",
	[BTN_X] = "BtnX",			[BTN_Y] = "BtnY",
	[BTN_Z] = "BtnZ",			[BTN_TL] = "BtnTL",
	[BTN_TR] = "BtnTR",			[BTN_TL2] = "BtnTL2",
	[BTN_TR2] = "BtnTR2",			[BTN_SELECT] = "BtnSelect",
	[BTN_START] = "BtnStart",		[BTN_MODE] = "BtnMode",
	[BTN_THUMBL] = "BtnThumbL",		[BTN_THUMBR] = "BtnThumbR",
	[BTN_TOUCH] = "Touch",			[BTN_STYLUS] = "Stylus",
	[BTN_STYLUS2] = "Stylus2",		[BTN_TOOL_DOUBLETAP] = "Tool Doubletap",
	[BTN_TOOL_TRIPLETAP] = "Tool Tripletap", [BTN_GEAR_DOWN] = "WheelBtn",
	[BTN_GEAR_UP] = "Gear up",		[KEY_OK] = "Ok",
};


static void in_evdev_probe(void)
{
	long keybits[KEY_CNT / sizeof(long) / 8];
	long absbits[(ABS_MAX+1) / sizeof(long) / 8];
	int i;

	// the kernel might support and return less keys then we know about,
	// so make sure the buffers are clear.
	memset(keybits, 0, sizeof(keybits));
	memset(absbits, 0, sizeof(absbits));

	for (i = 0;; i++)
	{
		int support = 0, count = 0;
		int u, ret, fd, kc_first = KEY_MAX, kc_last = 0, have_abs = 0;
		in_evdev_t *dev;
		char name[64];

		snprintf(name, sizeof(name), "/dev/input/event%d", i);
		fd = open(name, O_RDONLY|O_NONBLOCK);
		if (fd == -1) {
			if (errno == EACCES)
				continue;	/* maybe we can access next one */
			break;
		}

		/* check supported events */
		ret = ioctl(fd, EVIOCGBIT(0, sizeof(support)), &support);
		if (ret == -1) {
			printf("in_evdev: ioctl failed on %s\n", name);
			goto skip;
		}

		if (support & (1 << EV_KEY)) {
			ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
			if (ret == -1) {
				printf("in_evdev: ioctl failed on %s\n", name);
				goto skip;
			}

			/* check for interesting keys */
			for (u = 0; u < KEY_CNT; u++) {
				if (KEYBITS_BIT(u)) {
					if (u < kc_first)
						kc_first = u;
					if (u > kc_last)
						kc_last = u;
					if (u != KEY_POWER && u != KEY_SLEEP && u != BTN_TOUCH)
						count++;
					if (u == BTN_TOUCH) /* we can't deal with ts currently */
						goto skip;
				}
			}
		}

		dev = calloc(1, sizeof(*dev));
		if (dev == NULL)
			goto skip;

		ret = ioctl(fd, EVIOCGKEY(sizeof(keybits)), keybits);
		if (ret == -1) {
			printf("Warning: EVIOCGKEY not supported, will have to track state\n");
			dev->kbits = calloc(KEY_CNT, sizeof(int));
			if (dev->kbits == NULL) {
				free(dev);
				goto skip;
			}
		}

		/* check for abs too */
		if (support & (1 << EV_ABS)) {
			struct input_absinfo ainfo;
			int dist;
			ret = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits);
			if (ret == -1)
				goto no_abs;
			if (absbits[0] & (1 << ABS_X)) {
				ret = ioctl(fd, EVIOCGABS(ABS_X), &ainfo);
				if (ret == -1)
					goto no_abs;
				dist = ainfo.maximum - ainfo.minimum;
				dev->abs_lzone = dist / 4;
				dev->abs_min_x = ainfo.minimum;
				dev->abs_max_x = ainfo.maximum;
			}
			if (absbits[0] & (1 << ABS_Y)) {
				ret = ioctl(fd, EVIOCGABS(ABS_Y), &ainfo);
				if (ret == -1)
					goto no_abs;
				dist = ainfo.maximum - ainfo.minimum;
				dev->abs_min_y = ainfo.minimum;
				dev->abs_max_y = ainfo.maximum;
			}
			for (u = 0; u < MAX_ABS_DEVS; u++) {
				ret = ioctl(fd, EVIOCGABS(u), &ainfo);
				if (ret == -1)
					break;
				dist = ainfo.maximum - ainfo.minimum;
				if (dist != 0)
					dev->abs_mult[u] = IN_ABS_RANGE * 2 * 65536 / dist;
				dev->abs_adj[u] = -(ainfo.maximum + ainfo.minimum + 1) / 2;
				have_abs = 1;
			}
			dev->abs_count = u;
		}

no_abs:
		if (count == 0 && !have_abs) {
			free(dev);
			goto skip;
		}

		dev->fd = fd;
		dev->kc_first = kc_first;
		dev->kc_last = kc_last;
		if (count > 0 || in_evdev_allow_abs_only)
			dev->abs_to_digital = 1;
		strcpy(name, IN_EVDEV_PREFIX);
		ioctl(fd, EVIOCGNAME(sizeof(name)-6), name+6);
		printf("in_evdev: found \"%s\" with %d events (type %08x)\n",
			name+6, count, support);
		in_register(name, fd, dev, KEY_CNT, in_evdev_keys, 0);
		continue;

skip:
		close(fd);
	}
}

static void in_evdev_free(void *drv_data)
{
	in_evdev_t *dev = drv_data;
	if (dev == NULL)
		return;
	close(dev->fd);
	free(dev);
}

static const char * const *
in_evdev_get_key_names(int *count)
{
	*count = KEY_CNT;
	return in_evdev_keys;
}

static void or_binds(const int *binds, int key, int *result)
{
	int t;
	for (t = 0; t < IN_BINDTYPE_COUNT; t++)
		result[t] |= binds[IN_BIND_OFFS(key, t)];
}

/* ORs result with binds of pressed buttons
 * XXX: should measure performance hit of this func, might need to optimize */
static int in_evdev_update(void *drv_data, const int *binds, int *result)
{
	struct input_event ev[16];
	struct input_absinfo ainfo;
	int keybits_[KEY_CNT / sizeof(int)];
	int *keybits = keybits_;
	in_evdev_t *dev = drv_data;
	int rd, ret, u, lzone;

	if (dev->kbits == NULL) {
		ret = ioctl(dev->fd, EVIOCGKEY(sizeof(keybits_)), keybits_);
		if (ret == -1) {
			perror("in_evdev: ioctl failed");
			return -1;
		}
	}
	else {
		keybits = dev->kbits;
		while (1) {
			rd = read(dev->fd, ev, sizeof(ev));
			if (rd < (int)sizeof(ev[0])) {
				if (errno != EAGAIN)
					perror("in_evdev: read failed");
				break;
			}
			for (u = 0; u < rd / sizeof(ev[0]); u++) {
				if (ev[u].type != EV_KEY)
					continue;
				else if (ev[u].value == 1)
					KEYBITS_BIT_SET(ev[u].code);
				else if (ev[u].value == 0)
					KEYBITS_BIT_CLEAR(ev[u].code);
			}
		}
	}

	for (u = dev->kc_first; u <= dev->kc_last; u++) {
		if (KEYBITS_BIT(u))
			or_binds(binds, u, result);
	}

	/* map X and Y absolute to UDLR */
	lzone = dev->abs_lzone;
	if (dev->abs_to_digital && lzone != 0) {
		ret = ioctl(dev->fd, EVIOCGABS(ABS_X), &ainfo);
		if (ret != -1) {
			if (ainfo.value < dev->abs_min_x + lzone) or_binds(binds, KEY_LEFT, result);
			if (ainfo.value > dev->abs_max_x - lzone) or_binds(binds, KEY_RIGHT, result);
		}
		ret = ioctl(dev->fd, EVIOCGABS(ABS_Y), &ainfo);
		if (ret != -1) {
			if (ainfo.value < dev->abs_min_y + lzone) or_binds(binds, KEY_UP, result);
			if (ainfo.value > dev->abs_max_y - lzone) or_binds(binds, KEY_DOWN, result);
		}
	}

	return 0;
}

static int in_evdev_update_analog(void *drv_data, int axis_id, int *result)
{
	struct input_absinfo ainfo;
	in_evdev_t *dev = drv_data;
	int ret;

	if ((unsigned int)axis_id >= MAX_ABS_DEVS)
		return -1;

	ret = ioctl(dev->fd, EVIOCGABS(axis_id), &ainfo);
	if (ret != 0)
		return ret;

	*result = (ainfo.value + dev->abs_adj[axis_id]) * dev->abs_mult[axis_id];
	*result >>= 16;
	return 0;
}

static int in_evdev_set_blocking(in_evdev_t *dev, int y)
{
	long flags;
	int ret;

	flags = (long)fcntl(dev->fd, F_GETFL);
	if ((int)flags == -1) {
		perror("in_evdev: F_GETFL fcntl failed");
		return -1;
	}

	if (flags & O_NONBLOCK) {
		/* flush the event queue */
		struct input_event ev;
		do {
			ret = read(dev->fd, &ev, sizeof(ev));
		}
		while (ret == sizeof(ev));
	}

	if (y)
		flags &= ~O_NONBLOCK;
	else
		flags |=  O_NONBLOCK;
	ret = fcntl(dev->fd, F_SETFL, flags);
	if (ret == -1) {
		perror("in_evdev: F_SETFL fcntl failed");
		return -1;
	}

	return 0;
}

static int in_evdev_get_config(void *drv_data, int what, int *val)
{
	in_evdev_t *dev = drv_data;

	switch (what) {
	case IN_CFG_ABS_AXIS_COUNT:
		*val = dev->abs_count;
		break;
	default:
		return -1;
	}

	return 0;
}

static int in_evdev_set_config(void *drv_data, int what, int val)
{
	in_evdev_t *dev = drv_data;
	int tmp;

	switch (what) {
	case IN_CFG_BLOCKING:
		return in_evdev_set_blocking(dev, val);
	case IN_CFG_ABS_DEAD_ZONE:
		if (val < 1 || val > 99 || dev->abs_lzone == 0)
			return -1;
		/* XXX: based on X axis only, for now.. */
		tmp = (dev->abs_max_x - dev->abs_min_x) / 2;
		dev->abs_lzone = tmp - tmp * val / 100;
		if (dev->abs_lzone < 1)
			dev->abs_lzone = 1;
		else if (dev->abs_lzone >= tmp)
			dev->abs_lzone = tmp - 1;
		break;
	default:
		return -1;
	}

	return 0;
}

static int in_evdev_update_keycode(void *data, int *is_down)
{
	int ret_kc = -1, ret_down = 0;
	in_evdev_t *dev = data;
	struct input_event ev;
	int rd;

	/* do single event, the caller sometimes wants
	 * to do select() in blocking mode */
	rd = read(dev->fd, &ev, sizeof(ev));
	if (rd < (int) sizeof(ev)) {
		if (errno != EAGAIN) {
			perror("in_evdev: error reading");
			//sleep(1);
			ret_kc = -2;
		}
		goto out;
	}

	if (ev.type == EV_KEY) {
		if (ev.value < 0 || ev.value > 1)
			goto out;
		ret_kc = ev.code;
		ret_down = ev.value;
		goto out;
	}
	else if (ev.type == EV_ABS && dev->abs_to_digital)
	{
		int lzone = dev->abs_lzone, down = 0, *last;

		// map absolute to up/down/left/right
		if (lzone != 0 && ev.code == ABS_X) {
			if (ev.value < dev->abs_min_x + lzone)
				down = KEY_LEFT;
			else if (ev.value > dev->abs_max_x - lzone)
				down = KEY_RIGHT;
			last = &dev->abs_lastx;
		}
		else if (lzone != 0 && ev.code == ABS_Y) {
			if (ev.value < dev->abs_min_y + lzone)
				down = KEY_UP;
			else if (ev.value > dev->abs_max_y - lzone)
				down = KEY_DOWN;
			last = &dev->abs_lasty;
		}
		else
			goto out;

		if (down == *last)
			goto out;

		if (down == 0 || *last != 0) {
			/* key up or direction change, return up event for old key */
			ret_kc = *last;
			ret_down = 0;
			*last = 0;
			goto out;
		}
		ret_kc = *last = down;
		ret_down = 1;
		goto out;
	}

out:
	if (is_down != NULL)
		*is_down = ret_down;

	return ret_kc;
}

static const struct {
	short key;
	short pbtn;
} key_pbtn_map[] =
{
	{ KEY_UP,	PBTN_UP },
	{ KEY_DOWN,	PBTN_DOWN },
	{ KEY_LEFT,	PBTN_LEFT },
	{ KEY_RIGHT,	PBTN_RIGHT },
	/* XXX: maybe better set this from it's plat code somehow */
	/* Pandora */
	{ KEY_END,	PBTN_MOK },
	{ KEY_PAGEDOWN,	PBTN_MBACK },
	{ KEY_HOME,	PBTN_MA2 },
	{ KEY_PAGEUP,	PBTN_MA3 },
	{ KEY_LEFTCTRL,   PBTN_MENU },
	{ KEY_RIGHTSHIFT, PBTN_L },
	{ KEY_RIGHTCTRL,  PBTN_R },
	/* Caanoo */
	{ BTN_THUMB2,	PBTN_MOK },
	{ BTN_THUMB,	PBTN_MBACK },
	{ BTN_TRIGGER,	PBTN_MA2 },
	{ BTN_TOP,	PBTN_MA3 },
	{ BTN_BASE,	PBTN_MENU },
	{ BTN_TOP2,	PBTN_L },
	{ BTN_PINKIE,	PBTN_R },
	/* "normal" keyboards */
	{ KEY_ENTER,	PBTN_MOK },
	{ KEY_ESC,	PBTN_MBACK },
	{ KEY_A,	PBTN_MA2 },
	{ KEY_S,	PBTN_MA3 },
	{ KEY_BACKSLASH,  PBTN_MENU },
	{ KEY_LEFTBRACE,  PBTN_L },
	{ KEY_RIGHTBRACE, PBTN_R },
};

#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))

static int in_evdev_menu_translate(void *drv_data, int keycode)
{
	in_evdev_t *dev = drv_data;
	int i;

	if (keycode < 0)
	{
		/* menu -> kc */
		keycode = -keycode;
		for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
			if (key_pbtn_map[i].pbtn == keycode) {
				int k = key_pbtn_map[i].key;
				/* should really check EVIOCGBIT, but this is enough for now */
				if (dev->kc_first <= k && k <= dev->kc_last)
					return k;
			}
	}
	else
	{
		for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
			if (key_pbtn_map[i].key == keycode)
				return key_pbtn_map[i].pbtn;
	}

	return 0;
}

static void in_evdev_get_def_binds(int *binds)
{
	int i;

	for (i = 0; ; i++) {
		if (in_evdev_defbinds[i].bit == 0 && in_evdev_defbinds[i].code == 0)
			break;
		binds[IN_BIND_OFFS(in_evdev_defbinds[i].code, in_evdev_defbinds[i].btype)] =
			1 << in_evdev_defbinds[i].bit;
	}
}

/* remove binds of missing keys, count remaining ones */
static int in_evdev_clean_binds(void *drv_data, int *binds, int *def_binds)
{
	int keybits[KEY_CNT / sizeof(int)];
	in_evdev_t *dev = drv_data;
	int i, t, ret, offs, count = 0;

	memset(keybits, 0, sizeof(keybits));
	ret = ioctl(dev->fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
	if (ret == -1) {
		perror("in_evdev: ioctl failed");
		// memset(keybits, 0xff, sizeof(keybits)); /* mark all as good */
	}

	if (dev->abs_to_digital && dev->abs_lzone != 0) {
		KEYBITS_BIT_SET(KEY_LEFT);
		KEYBITS_BIT_SET(KEY_RIGHT);
		KEYBITS_BIT_SET(KEY_UP);
		KEYBITS_BIT_SET(KEY_DOWN);
	}

	for (i = 0; i < KEY_CNT; i++) {
		for (t = 0; t < IN_BINDTYPE_COUNT; t++) {
			offs = IN_BIND_OFFS(i, t);
			if (!KEYBITS_BIT(i))
				binds[offs] = def_binds[offs] = 0;
			if (binds[offs])
				count++;
		}
	}

	return count;
}

static const in_drv_t in_evdev_drv = {
	.prefix         = IN_EVDEV_PREFIX,
	.probe          = in_evdev_probe,
	.free           = in_evdev_free,
	.get_key_names  = in_evdev_get_key_names,
	.get_def_binds  = in_evdev_get_def_binds,
	.clean_binds    = in_evdev_clean_binds,
	.get_config     = in_evdev_get_config,
	.set_config     = in_evdev_set_config,
	.update         = in_evdev_update,
	.update_analog  = in_evdev_update_analog,
	.update_keycode = in_evdev_update_keycode,
	.menu_translate = in_evdev_menu_translate,
};

void in_evdev_init(void)
{
	in_register_driver(&in_evdev_drv);
}