summaryrefslogtreecommitdiff
path: root/src/input.c
blob: 445834e1287c33fed186d80a99c56a11cd458fa4 (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
/*
 * kmscon - udev input hotplug and evdev handling
 *
 * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * The main object kmscon_input discovers and monitors input devices, and
 * adds/removes them accordingly from the devices linked list.
 *
 * The udev monitor keeps running even while the object is in INPUT_ASLEEP.
 * We do this because we'll either lose track of the devices, or otherwise
 * have to re-scan the devices at every wakeup.
 *
 * The kmscon_input_device objects hold the file descriptors for their device
 * nodes. All events go through the input-object callback; there is currently
 * no "routing" or any differentiation between them. When the input is put to
 * sleep, all fd's are closed. When woken up, they are opened. There should be
 * not spurious events delivered. The initial state depends on the
 * kmscon_input's state.
 */

#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <linux/input.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "eloop.h"
#include "input.h"
#include "input-private.h"
#include "log.h"

enum input_state {
	INPUT_ASLEEP,
	INPUT_AWAKE,
};

struct kmscon_input_device {
	size_t ref;
	struct kmscon_input_device *next;
	struct kmscon_input *input;

	int rfd;
	char *devnode;
	struct kmscon_fd *fd;

	struct xkb_state xkb_state;
};

struct kmscon_input {
	size_t ref;
	enum input_state state;
	struct kmscon_input_device *devices;

	struct kmscon_eloop *eloop;
	kmscon_input_cb cb;
	void *data;

	struct udev *udev;
	struct udev_monitor *monitor;
	struct kmscon_fd *monitor_fd;

	struct xkb_desc *xkb_desc;
};

static void remove_device(struct kmscon_input *input, const char *node);

static void notify_key(struct kmscon_input_device *device,
				uint16_t type, uint16_t code, int32_t value)
{
	struct kmscon_input_event ev;
	bool has_event;
	struct kmscon_input *input;

	if (type != EV_KEY)
		return;

	input = device->input;
	has_event = process_evdev_key(input->xkb_desc, &device->xkb_state,
							value, code, &ev);

	if (has_event)
		input->cb(input, &ev, input->data);
}

static void device_data_arrived(struct kmscon_fd *fd, int mask, void *data)
{
	int i;
	ssize_t len, n;
	struct kmscon_input_device *device = data;
	struct kmscon_input *input = device->input;
	struct input_event ev[16];

	len = sizeof(ev);
	while (len == sizeof(ev)) {
		len = read(device->rfd, &ev, sizeof(ev));
		if (len < 0) {
			if (errno == EWOULDBLOCK)
				break;

			log_warning("input: reading device %s failed %d\n",
						device->devnode, errno);
			remove_device(input, device->devnode);
		} else if (len == 0) {
			log_debug("input: EOF device %s\n", device->devnode);
			remove_device(input, device->devnode);
		} else if (len % sizeof(*ev)) {
			log_warning("input: read invalid input_event\n");
		} else {
			n = len / sizeof(*ev);
			for (i = 0; i < n; i++)
				notify_key(device, ev[i].type, ev[i].code,
								ev[i].value);
		}
	}
}

int kmscon_input_device_wake_up(struct kmscon_input_device *device)
{
	int ret;

	if (!device || !device->input || !device->input->eloop)
		return -EINVAL;

	if (device->fd)
		return 0;

	device->rfd = open(device->devnode, O_CLOEXEC | O_NONBLOCK, O_RDONLY);
	if (device->rfd < 0) {
		log_warning("input: cannot open input device %s: %d\n",
						device->devnode, errno);
		return -errno;
	}

	/* this rediscovers the xkb state if sth changed during sleep */
	reset_xkb_state(device->input->xkb_desc, &device->xkb_state,
								device->rfd);

	ret = kmscon_eloop_new_fd(device->input->eloop, &device->fd,
		device->rfd, KMSCON_READABLE, device_data_arrived, device);
	if (ret) {
		close(device->rfd);
		device->rfd = -1;
		return ret;
	}

	return 0;
}

void kmscon_input_device_sleep(struct kmscon_input_device *device)
{
	if (!device)
		return;

	if (device->rfd < 0)
		return;

	kmscon_eloop_rm_fd(device->fd);
	device->fd = NULL;
	close(device->rfd);
	device->rfd = -1;
}

static int kmscon_input_device_new(struct kmscon_input_device **out,
			struct kmscon_input *input, const char *devnode)
{
	struct kmscon_input_device *device;

	if (!out || !input)
		return -EINVAL;

	log_debug("input: new input device %s\n", devnode);

	device = malloc(sizeof(*device));
	if (!device)
		return -ENOMEM;

	memset(device, 0, sizeof(*device));
	device->ref = 1;

	device->devnode = strdup(devnode);
	if (!device->devnode) {
		free(device);
		return -ENOMEM;
	}

	device->input = input;
	device->rfd = -1;

	*out = device;
	return 0;
}

static void kmscon_input_device_ref(struct kmscon_input_device *device)
{
	if (!device)
		return;

	++device->ref;
}

static void kmscon_input_device_unref(struct kmscon_input_device *device)
{
	if (!device || !device->ref)
		return;

	if (--device->ref)
		return;

	kmscon_input_device_sleep(device);
	log_debug("input: destroying input device %s\n", device->devnode);
	free(device->devnode);
	free(device);
}

int kmscon_input_new(struct kmscon_input **out)
{
	int ret;
	struct kmscon_input *input;

	/* TODO: Make configurable */
	static const char *layout = "us,ru,il";
	static const char *variant = "intl,,biblical";
	static const char *options = "grp:menu_toggle";

	if (!out)
		return -EINVAL;

	input = malloc(sizeof(*input));
	if (!input)
		return -ENOMEM;

	log_debug("input: creating input object\n");

	memset(input, 0, sizeof(*input));
	input->ref = 1;
	input->state = INPUT_ASLEEP;

	ret = new_xkb_desc(layout, variant, options, &input->xkb_desc);
	if (ret) {
		log_warning("input: cannot create xkb description\n");
		goto err_free;
	}

	input->udev = udev_new();
	if (!input->udev) {
		log_warning("input: cannot create udev object\n");
		ret = -EFAULT;
		goto err_xkb;
	}

	input->monitor = udev_monitor_new_from_netlink(input->udev, "udev");
	if (!input->monitor) {
		log_warning("input: cannot create udev monitor\n");
		ret = -EFAULT;
		goto err_udev;
	}

	ret = udev_monitor_filter_add_match_subsystem_devtype(input->monitor,
								"input", NULL);
	if (ret) {
		log_warning("input: cannot add udev filter\n");
		ret = -EFAULT;
		goto err_monitor;
	}

	ret = udev_monitor_enable_receiving(input->monitor);
	if (ret) {
		log_warning("input: cannot start udev monitor\n");
		ret = -EFAULT;
		goto err_monitor;
	}

	*out = input;
	return 0;

err_monitor:
	udev_monitor_unref(input->monitor);
err_udev:
	udev_unref(input->udev);
err_xkb:
	free_xkb_desc(input->xkb_desc);
err_free:
	free(input);
	return ret;
}

void kmscon_input_ref(struct kmscon_input *input)
{
	if (!input)
		return;

	++input->ref;
}

void kmscon_input_unref(struct kmscon_input *input)
{
	if (!input || !input->ref)
		return;

	if (--input->ref)
		return;

	kmscon_input_disconnect_eloop(input);
	udev_monitor_unref(input->monitor);
	udev_unref(input->udev);
	free_xkb_desc(input->xkb_desc);
	free(input);
	log_debug("input: destroying input object\n");
}

static void add_device(struct kmscon_input *input,
					struct udev_device *udev_device)
{
	int ret;
	struct kmscon_input_device *device;
	const char *value, *node;

	if (!input || !udev_device)
		return;

	/*
	 * TODO: Here should go a proper filtering of input devices we're
	 * interested in. Currently, we add all kinds of devices. A simple
	 * blacklist should be the easiest way.
	 */

	node = udev_device_get_devnode(udev_device);
	if (!node)
		return;

	value = udev_device_get_property_value(udev_device,
							"ID_INPUT_KEYBOARD");
	if (!value || strcmp(value, "1")) {
		log_debug("input: ignoring non-keyboard device %s\n", node);
		return;
	}

	ret = kmscon_input_device_new(&device, input, node);
	if (ret) {
		log_warning("input: cannot create input device for %s\n",
									node);
		return;
	}

	if (input->state == INPUT_AWAKE) {
		ret = kmscon_input_device_wake_up(device);
		if (ret) {
			log_warning("input: cannot wake up new device %s\n",
									node);
			kmscon_input_device_unref(device);
			return;
		}
	}

	device->next = input->devices;
	input->devices = device;
	log_debug("input: added device %s\n", node);
}

static void remove_device(struct kmscon_input *input, const char *node)
{
	struct kmscon_input_device *iter, *prev;

	if (!input || !node || !input->devices)
		return;

	iter = input->devices;
	prev = NULL;

	while (iter) {
		if (!strcmp(iter->devnode, node)) {
			if (prev == NULL)
				input->devices = iter->next;
			else
				prev->next = iter->next;

			kmscon_input_device_unref(iter);
			log_debug("input: removed device %s\n", node);
			break;
		}

		prev = iter;
		iter = iter->next;
	}
}

static void remove_device_udev(struct kmscon_input *input,
					struct udev_device *udev_device)
{
	const char *node;

	if (!udev_device)
		return;

	node = udev_device_get_devnode(udev_device);
	if (!node)
		return;

	remove_device(input, node);
}

static void device_changed(struct kmscon_fd *fd, int mask, void *data)
{
	struct kmscon_input *input = data;
	struct udev_device *udev_device;
	const char *action;

	udev_device = udev_monitor_receive_device(input->monitor);
	if (!udev_device)
		return;

	action = udev_device_get_action(udev_device);
	if (!action) {
		log_warning("input: cannot get action field of new device\n");
		goto err_device;
	}

	if (!strcmp(action, "add"))
		add_device(input, udev_device);
	else if (!strcmp(action, "remove"))
		remove_device_udev(input, udev_device);

err_device:
	udev_device_unref(udev_device);
}

static void add_initial_devices(struct kmscon_input *input)
{
	int ret;
	struct udev_enumerate *e;
	struct udev_list_entry *first;
	struct udev_list_entry *item;
	struct udev_device *udev_device;
	const char *syspath;

	e = udev_enumerate_new(input->udev);
	if (!e) {
		log_warning("input: cannot create udev enumeration\n");
		return;
	}

	ret = udev_enumerate_add_match_subsystem(e, "input");
	if (ret) {
		log_warning("input: cannot add match to udev enumeration\n");
		goto err_enum;
	}

	ret = udev_enumerate_scan_devices(e);
	if (ret) {
		log_warning("input: cannot scan udev enumeration\n");
		goto err_enum;
	}

	first = udev_enumerate_get_list_entry(e);
	udev_list_entry_foreach(item, first) {
		syspath = udev_list_entry_get_name(item);
		if (!syspath)
			continue;

		udev_device = udev_device_new_from_syspath(input->udev, syspath);
		if (!udev_device) {
			log_warning("input: cannot create device "
							"from udev path\n");
			continue;
		}

		add_device(input, udev_device);
		udev_device_unref(udev_device);
	}

err_enum:
	udev_enumerate_unref(e);
}

int kmscon_input_connect_eloop(struct kmscon_input *input,
		struct kmscon_eloop *eloop, kmscon_input_cb cb, void *data)
{
	int ret;
	int fd;

	if (!input || !eloop || !cb)
		return -EINVAL;

	if (input->eloop)
		return -EALREADY;

	fd = udev_monitor_get_fd(input->monitor);
	ret = kmscon_eloop_new_fd(eloop, &input->monitor_fd, fd,
				KMSCON_READABLE, device_changed, input);
	if (ret)
		return ret;

	kmscon_eloop_ref(eloop);
	input->eloop = eloop;
	input->cb = cb;
	input->data = data;

	add_initial_devices(input);

	return 0;
}

void kmscon_input_disconnect_eloop(struct kmscon_input *input)
{
	struct kmscon_input_device *tmp;

	if (!input || !input->eloop)
		return;

	while (input->devices) {
		tmp = input->devices;
		input->devices = tmp->next;
		kmscon_input_device_unref(tmp);
	}

	kmscon_eloop_rm_fd(input->monitor_fd);
	input->monitor_fd = NULL;
	kmscon_eloop_unref(input->eloop);
	input->eloop = NULL;
	input->cb = NULL;
	input->data = NULL;
}

void kmscon_input_sleep(struct kmscon_input *input)
{
	struct kmscon_input_device *iter;

	if (!input)
		return;

	for (iter = input->devices; iter; iter = iter->next)
		kmscon_input_device_sleep(iter);

	input->state = INPUT_ASLEEP;
}

void kmscon_input_wake_up(struct kmscon_input *input)
{
	struct kmscon_input_device *iter, *prev, *tmp;
	int ret;

	if (!input)
		return;

	prev = NULL;
	iter = input->devices;

	while (iter) {
		ret = kmscon_input_device_wake_up(iter);
		if (ret) {
			if (!prev)
				input->devices = iter->next;
			else
				prev->next = iter->next;

			tmp = iter;
			iter = iter->next;

			log_warning("input: device %s does not wake up, "
					"removing device\n", tmp->devnode);
			kmscon_input_device_unref(tmp);
		} else {
			prev = iter;
			iter = iter->next;
		}
	}

	input->state = INPUT_AWAKE;
}

bool kmscon_input_is_asleep(struct kmscon_input *input)
{
	if (!input)
		return false;

	return input->state == INPUT_ASLEEP;
}