summaryrefslogtreecommitdiff
path: root/rcm-server-evdev.c
blob: 3b7648227ac9933c2e9d82848b4195b56a042483 (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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <systemd/sd-bus.h>
#include <errno.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libevdev/libevdev.h>

#include "utils.h"
#include "shared.h"
#include "rcm-server-main.h"
#include "rcm-server-evdev.h"
#include "rcm-server-keymap.h"

struct rc_scancode {
	__u16 protocol;
	__u16 reserved[3];
	__u64 scancode;
};

#define INPUT_KEYMAP_BY_INDEX   (1 << 0)
struct rc_keymap_entry {
	__u8  flags;
	__u8  len;
	__u16 index;
	__u32 keycode;
	union {
		struct rc_scancode rc;
		__u8 raw[32];
	};
	char end[];
};
#define RKE_SIZE (sizeof(struct rc_scancode))

static const char *
evdev_guess_protocol(struct device *device, uint64_t scancode, uint32_t keycode)
{
	struct rc_keymap_entry rke;
	unsigned i;

	if (!device || device->evdev_fd < 0)
		return NULL;

	for (i = 0; ; i++) {
		memset(&rke, 0, sizeof(rke));
		rke.len = RKE_SIZE;
		rke.index = i;
		rke.flags = INPUT_KEYMAP_BY_INDEX;
		if (ioctl(device->evdev_fd, EVIOCGKEYCODE_V2, &rke))
			break;

		if (rke.rc.scancode != scancode)
			continue;

		if (rke.keycode != keycode)
			continue;

		if (rke.rc.protocol > ARRAY_SIZE(rc_protocols))
			return NULL;

		return rc_protocols[rke.rc.protocol];
	}

	return NULL;
}

int
evdev_get_mapping(struct device *device, unsigned index, unsigned *ret_protocol,
		  uint64_t *ret_scancode, struct linux_input_keycode **ret_lik)
{
	struct rc_keymap_entry rke;
	struct linux_input_keycode *lik;

	memset(&rke, 0, sizeof(rke));
	rke.len = RKE_SIZE;
	rke.index = index;
	rke.flags = INPUT_KEYMAP_BY_INDEX;

	if (ioctl(device->evdev_fd, EVIOCGKEYCODE_V2, &rke))
		return 0;

	lik = get_linux_keycode_by_value(rke.keycode);
	if (!lik) {
		printf("unknown keycode: %" PRIu32 "\n", rke.keycode);
		return -EINVAL;
	}

	*ret_protocol = rke.rc.protocol;
	*ret_scancode = rke.rc.scancode;
	*ret_lik = lik;
	return 1;
}

int
evdev_set_mapping(struct device *device, unsigned protocol, uint64_t scancode,
		  struct linux_input_keycode *lik)
{
	struct rc_keymap_entry rke;
	int r;

	memset(&rke, 0, sizeof(rke));
	rke.len = RKE_SIZE;
	rke.rc.protocol = protocol;
	rke.rc.scancode = scancode;

	if (lik)
		rke.keycode = lik->value;
	else
		rke.keycode = KEY_RESERVED;

	if (ioctl(device->evdev_fd, EVIOCSKEYCODE_V2, &rke)) {
		r = -errno;
		printf("EVIOCSKEYCODE_V2 failed (%s)\n", strerror(errno));
	}

	return r;
}

static int
evdev_clear_keymap(struct device *device)
{
	struct rc_keymap_entry rke;
	unsigned count = 0;
	int r = 0;

	printf("Clearing keymap...");
	while (true) {
		memset(&rke, 0, sizeof(rke));
		rke.len = RKE_SIZE;
		rke.index = 0;
		rke.flags = INPUT_KEYMAP_BY_INDEX;
		if (ioctl(device->evdev_fd, EVIOCGKEYCODE_V2, &rke)) {
			printf("done (%u entries removed)\n", count);
			break;
		}

		/*
		printf("  Clearing 0x%08llX (0x%02X) to 0x%02X\n",
		       rke.u.rc.scancode, rke.u.rc.protocol, rke.keycode);
		       */

		rke.keycode = KEY_RESERVED;
		if (ioctl(device->evdev_fd, EVIOCSKEYCODE_V2, &rke)) {
			r = -errno;
			printf("failed (%s)\n", strerror(errno));
			break;
		}
		count++;
	}

	return r;
}

static void
set_keycode_new(struct device *device, __u16 protocol, __u64 scancode, __u32 keycode)
{
	struct rc_keymap_entry rke;

	memset(&rke, 0, sizeof(rke));
	rke.len = RKE_SIZE;
	rke.rc.protocol = protocol;
	rke.rc.scancode = scancode;
	rke.keycode = keycode;

	printf("Setting keycode (new ioctl) 0x%08llX (0x%02X) to 0x%02X\n",
	       rke.rc.scancode, rke.rc.protocol, rke.keycode);

	if (ioctl(device->evdev_fd, EVIOCSKEYCODE_V2, &rke)) {
		printf("Unable to call SETKEYCODE2 ioctl\n");
		exit(EXIT_FAILURE);
	}
}

static int
evdev_set_keymap(struct device *device, struct keymap *keymap)
{
	unsigned i;

	printf("\tSetting up keymap: %s\n", keymap->name);

	for (i = 0; i < keymap->keycode_count; i++)
		set_keycode_new(device,
				keymap->keycodes[i].protocol,
				keymap->keycodes[i].scancode,
				keymap->keycodes[i].lik->value);

	printf("\tDone: %u entries added\n", i);
	return 0;
}

static int
evdev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
	struct device *device = userdata;
	struct input_event ev;
	static struct linux_input_keycode *keycode = NULL;
	static bool pressed = false;
	static uint32_t scancode;
	static bool scancode_recv = false;
	int r;

	if (fd != device->evdev_fd)
		fprintf(stderr, "evdev fd mismatch: %i != %i\n", device->evdev_fd, fd);

	if (revents & EPOLLHUP) {
		fprintf(stderr, "evdev connection closed!\n");
		close(fd);
		device->evdev_fd = -1;
		return 0;
	}

	if (!(revents & EPOLLIN)) {
		fprintf(stderr, "unexpected evdev event: %" PRIu32 "\n", revents);
		return 0;
	}

	do {
		char buf[100];

		r = libevdev_next_event(device->evdev_dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
		if (r != LIBEVDEV_READ_STATUS_SUCCESS)
			continue;

		snprintf(buf, sizeof(buf), "Event: %s %s %u (0x%08x)\n",
			 libevdev_event_type_get_name(ev.type),
			 libevdev_event_code_get_name(ev.type, ev.code),
			 ev.value, ev.value);

		sd_bus_emit_signal(device->mgr->bus,
				   device->path,
				   "org.gnome.RemoteControlManager.Device",
				   "Event",
				   "s", buf);

		printf(buf);

		switch (ev.type) {
		case EV_KEY:
			if (keycode)
				printf("Reading from evdev - multiple keycodes?\n");

			keycode = get_linux_keycode_by_value(ev.code);

			if (!keycode) {
				printf("evdev - unknown keycode (%u)\n", ev.code);
				break;
			}

			if (ev.value)
				pressed = true;
			break;

		case EV_MSC:
			if (ev.code == MSC_SCAN) {
				if (scancode_recv)
					printf("Reading from evdev - multiple scancodes?\n");
				scancode_recv = true;
				scancode = ev.value;
			}
			break;

		case EV_SYN:
			if (keycode || scancode_recv) {
				const char *protocol;

				/* FIXME: protocol reporting needs kernel support */
				if (scancode_recv)
					protocol = evdev_guess_protocol(device, scancode, keycode ? keycode->value : KEY_RESERVED);

				printf("evdev -");

				if (scancode_recv) {
					printf(" protocol %s (guessed)", protocol);
					printf(" scancode 0x%08x", scancode);
				}

				if (keycode) {
					printf(" keycode %s (%u)", keycode->name, keycode->value);
					printf(" %s", pressed ? "pressed" : "released");
				}

				printf("\n");

				if (scancode_recv)
					sd_bus_emit_signal(device->mgr->bus,
							   device->path,
							   "org.gnome.RemoteControlManager.Device",
							   "KeyPressed",
							   "sts", protocol ? protocol : "NEC", scancode,
							   keycode ? keycode->name : "KEY_RESERVED");

				else if (keycode && !pressed)
					sd_bus_emit_signal(device->mgr->bus,
							   device->path,
							   "org.gnome.RemoteControlManager.Device",
							   "KeyReleased",
							   "s", keycode->name);
			}

			scancode_recv = false;
			pressed = false;
			keycode = NULL;
			break;

		default:
			break;
		}

	} while (r == LIBEVDEV_READ_STATUS_SUCCESS || r == LIBEVDEV_READ_STATUS_SYNC);

	return 0;
}

int
evdev_setup(struct device *device, const char *path)
{
	int r;
	struct keymap *keymap;

	/* FIXME: Fixup error handling */
	if (!device)
		return -EINVAL;

	if (device->evdev_fd >= 0) {
		printf("Multiple evdev devices!?\n");
		return 0;
	}

	device->evdev_fd = open(path, O_RDONLY | O_NONBLOCK);
	r = libevdev_new_from_fd(device->evdev_fd, &device->evdev_dev);
	if (r < 0) {
		printf("Failed to open evdev device %s: %s\n", path, strerror(-r));
		close(device->evdev_fd);
		return r;
	}

	device->input_name = strdup(libevdev_get_name(device->evdev_dev));

	printf("Performing evdev setup for device %s (%s)\n",
	       device->path, libevdev_get_name(device->evdev_dev));
	r = evdev_clear_keymap(device);
	if (r < 0)
		return r;

	list_for_each_entry(keymap, &device->keymaps, list)
		r = evdev_set_keymap(device, keymap);

	if (sd_event_add_io(device->mgr->event, &device->evdev_ev,
			    device->evdev_fd, EPOLLIN, evdev_read, device) < 0) {
		printf("Failed to add event source for evdev device %s: %s\n",
		       path, strerror(errno));
		close(device->evdev_fd);
		device->evdev_fd = -1;
		return -errno;
	}

	return r;
}