2 * kmscon - udev input hotplug and evdev handling
4 * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * The main object kmscon_input discovers and monitors input devices, and
28 * adds/removes them accordingly from the devices linked list.
30 * The udev monitor keeps running even while the object is in INPUT_ASLEEP.
31 * We do this because we'll either lose track of the devices, or otherwise
32 * have to re-scan the devices at every wakeup.
34 * The kmscon_input_device objects hold the file descriptors for their device
35 * nodes. All events go through the input-object callback; there is currently
36 * no "routing" or any differentiation between them. When the input is put to
37 * sleep, all fd's are closed. When woken up, they are opened. There should be
38 * not spurious events delivered. The initial state depends on the
39 * kmscon_input's state.
45 #include <linux/input.h>
56 /* How many longs are needed to hold \n bits. */
57 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
64 /* See probe_device_features(). */
66 FEATURE_HAS_KEYS = 0x01,
69 struct kmscon_input_device {
71 struct kmscon_input_device *next;
72 struct kmscon_input *input;
74 unsigned int features;
80 struct kmscon_kbd *kbd;
85 enum input_state state;
86 struct kmscon_input_device *devices;
88 struct kmscon_eloop *eloop;
93 struct udev_monitor *monitor;
94 struct kmscon_fd *monitor_fd;
96 struct kmscon_kbd_desc *desc;
99 static void remove_device(struct kmscon_input *input, const char *node);
101 static void notify_key(struct kmscon_input_device *device,
102 uint16_t type, uint16_t code, int32_t value)
105 struct kmscon_input_event ev;
106 struct kmscon_input *input;
111 input = device->input;
112 ret = kmscon_kbd_process_key(device->kbd, value, code, &ev);
114 if (ret && ret != -ENOKEY)
117 if (ret != -ENOKEY && input->cb)
118 input->cb(input, &ev, input->data);
121 static void device_data_arrived(struct kmscon_fd *fd, int mask, void *data)
125 struct kmscon_input_device *device = data;
126 struct kmscon_input *input = device->input;
127 struct input_event ev[16];
130 while (len == sizeof(ev)) {
131 len = read(device->rfd, &ev, sizeof(ev));
133 if (errno == EWOULDBLOCK)
136 log_warn("input: reading device %s failed %d\n",
137 device->devnode, errno);
138 remove_device(input, device->devnode);
139 } else if (len == 0) {
140 log_debug("input: EOF device %s\n", device->devnode);
141 remove_device(input, device->devnode);
142 } else if (len % sizeof(*ev)) {
143 log_warn("input: read invalid input_event\n");
145 n = len / sizeof(*ev);
146 for (i = 0; i < n; i++)
147 notify_key(device, ev[i].type, ev[i].code,
153 int kmscon_input_device_wake_up(struct kmscon_input_device *device)
157 if (!device || !device->input || !device->input->eloop)
163 device->rfd = open(device->devnode, O_CLOEXEC | O_NONBLOCK | O_RDONLY);
164 if (device->rfd < 0) {
165 log_warn("input: cannot open input device %s: %d\n",
166 device->devnode, errno);
170 if (device->features & FEATURE_HAS_KEYS) {
171 /* rediscover the keyboard state if sth changed during sleep */
172 kmscon_kbd_reset(device->kbd, device->rfd);
174 ret = kmscon_eloop_new_fd(device->input->eloop, &device->fd,
175 device->rfd, KMSCON_READABLE,
176 device_data_arrived, device);
187 void kmscon_input_device_sleep(struct kmscon_input_device *device)
195 if (device->features & FEATURE_HAS_KEYS)
196 kmscon_eloop_rm_fd(device->fd);
203 static int kmscon_input_device_new(struct kmscon_input_device **out,
204 struct kmscon_input *input, const char *devnode,
205 unsigned int features)
208 struct kmscon_input_device *device;
213 log_debug("input: new input device %s\n", devnode);
215 device = malloc(sizeof(*device));
219 memset(device, 0, sizeof(*device));
222 device->devnode = strdup(devnode);
223 if (!device->devnode) {
228 ret = kmscon_kbd_new(&device->kbd, input->desc);
230 free(device->devnode);
235 device->input = input;
236 device->features = features;
243 /* static void kmscon_input_device_ref(struct kmscon_input_device *device) */
251 static void kmscon_input_device_unref(struct kmscon_input_device *device)
253 if (!device || !device->ref)
259 kmscon_input_device_sleep(device);
260 kmscon_kbd_unref(device->kbd);
261 log_debug("input: destroying input device %s\n", device->devnode);
262 free(device->devnode);
266 int kmscon_input_new(struct kmscon_input **out)
269 struct kmscon_input *input;
270 static const char *layout, *variant, *options;
275 input = malloc(sizeof(*input));
279 log_debug("input: creating input object\n");
281 memset(input, 0, sizeof(*input));
283 input->state = INPUT_ASLEEP;
285 /* TODO: Make properly configurable */
286 layout = getenv("KMSCON_XKB_LAYOUT") ?: "us";
287 variant = getenv("KMSCON_XKB_VARIANT") ?: "";
288 options = getenv("KMSCON_XKB_OPTIONS") ?: "";
290 ret = kmscon_kbd_desc_new(&input->desc, layout, variant, options);
292 log_warn("input: cannot create xkb description\n");
296 input->udev = udev_new();
298 log_warn("input: cannot create udev object\n");
303 input->monitor = udev_monitor_new_from_netlink(input->udev, "udev");
304 if (!input->monitor) {
305 log_warn("input: cannot create udev monitor\n");
310 ret = udev_monitor_filter_add_match_subsystem_devtype(input->monitor,
313 log_warn("input: cannot add udev filter\n");
318 ret = udev_monitor_enable_receiving(input->monitor);
320 log_warn("input: cannot start udev monitor\n");
329 udev_monitor_unref(input->monitor);
331 udev_unref(input->udev);
333 kmscon_kbd_desc_unref(input->desc);
339 void kmscon_input_ref(struct kmscon_input *input)
347 void kmscon_input_unref(struct kmscon_input *input)
349 if (!input || !input->ref)
355 kmscon_input_disconnect_eloop(input);
356 udev_monitor_unref(input->monitor);
357 udev_unref(input->udev);
358 kmscon_kbd_desc_unref(input->desc);
360 log_debug("input: destroying input object\n");
364 * See if the device has anything useful to offer.
365 * We go over the desired features and return a mask of enum device_feature's.
367 static unsigned int probe_device_features(const char *node)
370 unsigned int features = 0;
371 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
372 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
374 fd = open(node, O_NONBLOCK | O_CLOEXEC);
378 /* Which types of input events the device supports. */
380 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
384 /* Device supports keys/buttons. */
385 if (kmscon_evdev_bit_is_set(evbits, EV_KEY)) {
387 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
392 * If the device support any of the normal keyboard keys, we
393 * take it. Even if the keys are not ordinary they can be
394 * mapped to anything by the keyboard backend.
396 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++) {
397 if (kmscon_evdev_bit_is_set(keybits, i)) {
398 features |= FEATURE_HAS_KEYS;
409 log_warn("input: cannot probe features of device (%s): %m\n",
415 static void add_device(struct kmscon_input *input,
416 struct udev_device *udev_device)
419 struct kmscon_input_device *device;
421 unsigned int features;
423 if (!input || !udev_device)
426 node = udev_device_get_devnode(udev_device);
430 features = probe_device_features(node);
431 if (!(features & FEATURE_HAS_KEYS)) {
432 log_debug("input: ignoring non-useful device %s\n", node);
436 ret = kmscon_input_device_new(&device, input, node, features);
438 log_warn("input: cannot create input device for %s\n",
443 if (input->state == INPUT_AWAKE) {
444 ret = kmscon_input_device_wake_up(device);
446 log_warn("input: cannot wake up new device %s\n",
448 kmscon_input_device_unref(device);
453 device->next = input->devices;
454 input->devices = device;
455 log_debug("input: added device %s (features: %#x)\n", node, features);
458 static void remove_device(struct kmscon_input *input, const char *node)
460 struct kmscon_input_device *iter, *prev;
462 if (!input || !node || !input->devices)
465 iter = input->devices;
469 if (!strcmp(iter->devnode, node)) {
471 input->devices = iter->next;
473 prev->next = iter->next;
475 kmscon_input_device_unref(iter);
476 log_debug("input: removed device %s\n", node);
485 static void remove_device_udev(struct kmscon_input *input,
486 struct udev_device *udev_device)
493 node = udev_device_get_devnode(udev_device);
497 remove_device(input, node);
500 static void device_changed(struct kmscon_fd *fd, int mask, void *data)
502 struct kmscon_input *input = data;
503 struct udev_device *udev_device;
506 udev_device = udev_monitor_receive_device(input->monitor);
510 action = udev_device_get_action(udev_device);
512 log_warn("input: cannot get action field of new device\n");
516 if (!strcmp(action, "add"))
517 add_device(input, udev_device);
518 else if (!strcmp(action, "remove"))
519 remove_device_udev(input, udev_device);
522 udev_device_unref(udev_device);
525 static void add_initial_devices(struct kmscon_input *input)
528 struct udev_enumerate *e;
529 struct udev_list_entry *first;
530 struct udev_list_entry *item;
531 struct udev_device *udev_device;
534 e = udev_enumerate_new(input->udev);
536 log_warn("input: cannot create udev enumeration\n");
540 ret = udev_enumerate_add_match_subsystem(e, "input");
542 log_warn("input: cannot add match to udev enumeration\n");
546 ret = udev_enumerate_scan_devices(e);
548 log_warn("input: cannot scan udev enumeration\n");
552 first = udev_enumerate_get_list_entry(e);
553 udev_list_entry_foreach(item, first) {
554 syspath = udev_list_entry_get_name(item);
558 udev_device = udev_device_new_from_syspath(input->udev, syspath);
560 log_warn("input: cannot create device "
565 add_device(input, udev_device);
566 udev_device_unref(udev_device);
570 udev_enumerate_unref(e);
573 int kmscon_input_connect_eloop(struct kmscon_input *input,
574 struct kmscon_eloop *eloop, kmscon_input_cb cb, void *data)
579 if (!input || !eloop || !cb)
585 fd = udev_monitor_get_fd(input->monitor);
586 ret = kmscon_eloop_new_fd(eloop, &input->monitor_fd, fd,
587 KMSCON_READABLE, device_changed, input);
591 kmscon_eloop_ref(eloop);
592 input->eloop = eloop;
596 add_initial_devices(input);
601 void kmscon_input_disconnect_eloop(struct kmscon_input *input)
603 struct kmscon_input_device *tmp;
605 if (!input || !input->eloop)
608 while (input->devices) {
609 tmp = input->devices;
610 input->devices = tmp->next;
611 kmscon_input_device_unref(tmp);
614 kmscon_eloop_rm_fd(input->monitor_fd);
615 input->monitor_fd = NULL;
616 kmscon_eloop_unref(input->eloop);
622 void kmscon_input_sleep(struct kmscon_input *input)
624 struct kmscon_input_device *iter;
629 for (iter = input->devices; iter; iter = iter->next)
630 kmscon_input_device_sleep(iter);
632 input->state = INPUT_ASLEEP;
635 void kmscon_input_wake_up(struct kmscon_input *input)
637 struct kmscon_input_device *iter, *prev, *tmp;
644 iter = input->devices;
647 ret = kmscon_input_device_wake_up(iter);
650 input->devices = iter->next;
652 prev->next = iter->next;
657 log_warn("input: device %s does not wake up, "
658 "removing device\n", tmp->devnode);
659 kmscon_input_device_unref(tmp);
666 input->state = INPUT_AWAKE;
669 bool kmscon_input_is_asleep(struct kmscon_input *input)
674 return input->state == INPUT_ASLEEP;