2 * uterm - Linux User-Space Terminal
4 * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <linux/input.h>
40 #include "shl_dlist.h"
44 #include "uterm_input.h"
45 #include "uterm_input_internal.h"
47 #define LOG_SUBSYSTEM "input"
49 /* How many longs are needed to hold \n bits. */
50 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
52 static void input_free_dev(struct uterm_input_dev *dev);
54 static void notify_key(struct uterm_input_dev *dev,
62 uxkb_dev_process(dev, value, code);
65 static void input_data_dev(struct ev_fd *fd, int mask, void *data)
67 struct uterm_input_dev *dev = data;
68 struct input_event ev[16];
72 if (mask & (EV_HUP | EV_ERR)) {
73 log_debug("EOF on %s", dev->node);
79 while (len == sizeof(ev)) {
80 len = read(dev->rfd, &ev, sizeof(ev));
82 if (errno == EWOULDBLOCK)
84 log_warn("reading from %s failed (%d): %m",
87 } else if (len == 0) {
88 log_debug("EOF on %s", dev->node);
90 } else if (len % sizeof(*ev)) {
91 log_warn("invalid input_event on %s", dev->node);
93 n = len / sizeof(*ev);
94 for (i = 0; i < n; i++)
95 notify_key(dev, ev[i].type, ev[i].code,
101 static int input_wake_up_dev(struct uterm_input_dev *dev)
108 dev->rfd = open(dev->node, O_CLOEXEC | O_NONBLOCK | O_RDWR);
110 log_warn("cannot open device %s (%d): %m", dev->node, errno);
114 uxkb_dev_wake_up(dev);
116 ret = ev_eloop_new_fd(dev->input->eloop, &dev->fd, dev->rfd,
117 EV_READABLE, input_data_dev, dev);
127 static void input_sleep_dev(struct uterm_input_dev *dev)
134 dev->repeating = false;
135 ev_timer_update(dev->repeat_timer, NULL);
136 ev_eloop_rm_fd(dev->fd);
142 static void input_new_dev(struct uterm_input *input,
144 unsigned int capabilities)
146 struct uterm_input_dev *dev;
149 dev = malloc(sizeof(*dev));
152 memset(dev, 0, sizeof(*dev));
155 dev->capabilities = capabilities;
157 dev->node = strdup(node);
162 dev->event.keysyms = malloc(sizeof(uint32_t) * dev->num_syms);
163 if (!dev->event.keysyms)
165 dev->event.codepoints = malloc(sizeof(uint32_t) * dev->num_syms);
166 if (!dev->event.codepoints)
168 dev->repeat_event.keysyms = malloc(sizeof(uint32_t) * dev->num_syms);
169 if (!dev->repeat_event.keysyms)
171 dev->repeat_event.codepoints = malloc(sizeof(uint32_t) * dev->num_syms);
172 if (!dev->repeat_event.codepoints)
175 ret = uxkb_dev_init(dev);
177 goto err_rcodepoints;
179 if (input->awake > 0) {
180 ret = input_wake_up_dev(dev);
185 log_debug("new device %s", node);
186 shl_dlist_link(&input->devices, &dev->list);
190 uxkb_dev_destroy(dev);
192 free(dev->repeat_event.codepoints);
194 free(dev->repeat_event.keysyms);
196 free(dev->event.codepoints);
198 free(dev->event.keysyms);
205 static void input_free_dev(struct uterm_input_dev *dev)
207 log_debug("free device %s", dev->node);
208 input_sleep_dev(dev);
209 shl_dlist_unlink(&dev->list);
210 uxkb_dev_destroy(dev);
211 free(dev->repeat_event.codepoints);
212 free(dev->repeat_event.keysyms);
213 free(dev->event.codepoints);
214 free(dev->event.keysyms);
220 int uterm_input_new(struct uterm_input **out,
221 struct ev_eloop *eloop,
226 unsigned int repeat_delay,
227 unsigned int repeat_rate)
229 struct uterm_input *input;
237 if (repeat_delay >= 1000)
241 if (repeat_rate >= 1000)
244 input = malloc(sizeof(*input));
247 memset(input, 0, sizeof(*input));
249 input->eloop = eloop;
250 input->repeat_delay = repeat_delay;
251 input->repeat_rate = repeat_rate;
252 shl_dlist_init(&input->devices);
254 ret = shl_hook_new(&input->hook);
258 ret = uxkb_desc_init(input, model, layout, variant, options);
262 log_debug("new object %p", input);
263 ev_eloop_ref(input->eloop);
268 shl_hook_free(input->hook);
275 void uterm_input_ref(struct uterm_input *input)
277 if (!input || !input->ref)
284 void uterm_input_unref(struct uterm_input *input)
286 struct uterm_input_dev *dev;
288 if (!input || !input->ref || --input->ref)
291 log_debug("free object %p", input);
293 while (input->devices.next != &input->devices) {
294 dev = shl_dlist_entry(input->devices.next,
295 struct uterm_input_dev,
300 uxkb_desc_destroy(input);
301 shl_hook_free(input->hook);
302 ev_eloop_unref(input->eloop);
307 * See if the device has anything useful to offer.
308 * We go over the possible capabilities and return a mask of enum
309 * uterm_input_device_capability's.
311 static unsigned int probe_device_capabilities(const char *node)
314 unsigned int capabilities = 0;
315 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
316 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
318 fd = open(node, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
322 /* Which types of input events the device supports. */
323 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
327 /* Device supports keys/buttons. */
328 if (input_bit_is_set(evbits, EV_KEY)) {
329 ret = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
334 * If the device support any of the normal keyboard keys, we
335 * take it. Even if the keys are not ordinary they can be
336 * mapped to anything by the keyboard backend.
338 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++) {
339 if (input_bit_is_set(keybits, i)) {
340 capabilities |= UTERM_DEVICE_HAS_KEYS;
346 if (input_bit_is_set(evbits, EV_LED))
347 capabilities |= UTERM_DEVICE_HAS_LEDS;
353 log_warn("cannot probe capabilities of device %s (%d): %m",
360 void uterm_input_add_dev(struct uterm_input *input, const char *node)
362 unsigned int capabilities;
367 capabilities = probe_device_capabilities(node);
368 if (!(capabilities & UTERM_DEVICE_HAS_KEYS)) {
369 log_debug("ignoring non-useful device %s", node);
373 input_new_dev(input, node, capabilities);
377 void uterm_input_remove_dev(struct uterm_input *input, const char *node)
379 struct shl_dlist *iter;
380 struct uterm_input_dev *dev;
385 shl_dlist_for_each(iter, &input->devices) {
386 dev = shl_dlist_entry(iter,
387 struct uterm_input_dev,
389 if (!strcmp(dev->node, node)) {
397 int uterm_input_register_cb(struct uterm_input *input,
404 return shl_hook_add_cast(input->hook, cb, data, false);
408 void uterm_input_unregister_cb(struct uterm_input *input,
415 shl_hook_rm_cast(input->hook, cb, data);
419 void uterm_input_sleep(struct uterm_input *input)
421 struct shl_dlist *iter;
422 struct uterm_input_dev *dev;
428 if (input->awake != 0)
431 log_debug("going to sleep");
433 shl_dlist_for_each(iter, &input->devices) {
434 dev = shl_dlist_entry(iter,
435 struct uterm_input_dev,
437 input_sleep_dev(dev);
442 void uterm_input_wake_up(struct uterm_input *input)
444 struct shl_dlist *iter, *tmp;
445 struct uterm_input_dev *dev;
452 if (input->awake != 1)
455 log_debug("wakeing up");
457 shl_dlist_for_each_safe(iter, tmp, &input->devices) {
458 dev = shl_dlist_entry(iter,
459 struct uterm_input_dev,
461 ret = input_wake_up_dev(dev);
468 bool uterm_input_is_awake(struct uterm_input *input)
473 return input->awake > 0;