evdev: Convert wl_fixed_t to int before using internally
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <linux/input.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <mtdev.h>
30
31 #include "compositor.h"
32 #include "evdev.h"
33 #include "launcher-util.h"
34
35 struct evdev_input {
36         struct weston_input_device base;
37         struct wl_list devices_list;
38         struct udev_monitor *udev_monitor;
39         struct wl_event_source *udev_monitor_source;
40         char *seat_id;
41 };
42
43 #define MAX_SLOTS 16
44
45 struct evdev_input_device {
46         struct evdev_input *master;
47         struct wl_list link;
48         struct wl_event_source *source;
49         struct weston_output *output;
50         char *devnode;
51         int fd;
52         struct {
53                 int min_x, max_x, min_y, max_y;
54                 int old_x, old_y, reset_x, reset_y;
55                 int32_t x, y;
56         } abs;
57
58         struct {
59                 int slot;
60                 int32_t x[MAX_SLOTS];
61                 int32_t y[MAX_SLOTS];
62         } mt;
63         struct mtdev *mtdev;
64
65         struct {
66                 int dx, dy;
67         } rel;
68
69         int type; /* event type flags */
70
71         int is_touchpad, is_mt;
72 };
73
74 /* event type flags */
75 #define EVDEV_ABSOLUTE_MOTION           (1 << 0)
76 #define EVDEV_ABSOLUTE_MT_DOWN          (1 << 1)
77 #define EVDEV_ABSOLUTE_MT_MOTION        (1 << 2)
78 #define EVDEV_ABSOLUTE_MT_UP            (1 << 3)
79 #define EVDEV_RELATIVE_MOTION           (1 << 4)
80
81 static inline void
82 evdev_process_key(struct evdev_input_device *device,
83                         struct input_event *e, int time)
84 {
85         if (e->value == 2)
86                 return;
87
88         switch (e->code) {
89         case BTN_TOOL_PEN:
90         case BTN_TOOL_RUBBER:
91         case BTN_TOOL_BRUSH:
92         case BTN_TOOL_PENCIL:
93         case BTN_TOOL_AIRBRUSH:
94         case BTN_TOOL_FINGER:
95         case BTN_TOOL_MOUSE:
96         case BTN_TOOL_LENS:
97                 if (device->is_touchpad)
98                 {
99                         device->abs.reset_x = 1;
100                         device->abs.reset_y = 1;
101                 }
102                 break;
103         case BTN_TOUCH:
104                 /* Multitouch touchscreen devices might not send individually
105                  * button events each time a new finger is down. So we don't
106                  * send notification for such devices and we solve the button
107                  * case emulating on compositor side. */
108                 if (device->is_mt)
109                         break;
110
111                 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
112                  * BTN_LEFT */
113                 e->code = BTN_LEFT;
114                 /* Intentional fallthrough! */
115         case BTN_LEFT:
116         case BTN_RIGHT:
117         case BTN_MIDDLE:
118         case BTN_SIDE:
119         case BTN_EXTRA:
120         case BTN_FORWARD:
121         case BTN_BACK:
122         case BTN_TASK:
123                 notify_button(&device->master->base.input_device,
124                               time, e->code, e->value);
125                 break;
126
127         default:
128                 notify_key(&device->master->base.input_device,
129                            time, e->code, e->value);
130                 break;
131         }
132 }
133
134 static void
135 evdev_process_touch(struct evdev_input_device *device,
136                     struct input_event *e)
137 {
138         const int screen_width = device->output->current->width;
139         const int screen_height = device->output->current->height;
140
141         switch (e->code) {
142         case ABS_MT_SLOT:
143                 device->mt.slot = e->value;
144                 break;
145         case ABS_MT_TRACKING_ID:
146                 if (e->value >= 0)
147                         device->type |= EVDEV_ABSOLUTE_MT_DOWN;
148                 else
149                         device->type |= EVDEV_ABSOLUTE_MT_UP;
150                 break;
151         case ABS_MT_POSITION_X:
152                 device->mt.x[device->mt.slot] =
153                         (e->value - device->abs.min_x) * screen_width /
154                         (device->abs.max_x - device->abs.min_x) +
155                         device->output->x;
156                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
157                 break;
158         case ABS_MT_POSITION_Y:
159                 device->mt.y[device->mt.slot] =
160                         (e->value - device->abs.min_y) * screen_height /
161                         (device->abs.max_y - device->abs.min_y) +
162                         device->output->y;
163                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
164                 break;
165         }
166 }
167
168 static inline void
169 evdev_process_absolute_motion(struct evdev_input_device *device,
170                               struct input_event *e)
171 {
172         const int screen_width = device->output->current->width;
173         const int screen_height = device->output->current->height;
174
175         switch (e->code) {
176         case ABS_X:
177                 device->abs.x =
178                         (e->value - device->abs.min_x) * screen_width /
179                         (device->abs.max_x - device->abs.min_x) +
180                         device->output->x;
181                 device->type |= EVDEV_ABSOLUTE_MOTION;
182                 break;
183         case ABS_Y:
184                 device->abs.y =
185                         (e->value - device->abs.min_y) * screen_height /
186                         (device->abs.max_y - device->abs.min_y) +
187                         device->output->y;
188                 device->type |= EVDEV_ABSOLUTE_MOTION;
189                 break;
190         }
191 }
192
193 static inline void
194 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
195                                        struct input_event *e)
196 {
197         /* FIXME: Make this configurable somehow. */
198         const int touchpad_speed = 700;
199
200         switch (e->code) {
201         case ABS_X:
202                 e->value -= device->abs.min_x;
203                 if (device->abs.reset_x)
204                         device->abs.reset_x = 0;
205                 else {
206                         device->rel.dx =
207                                 (e->value - device->abs.old_x) *
208                                 touchpad_speed /
209                                 (device->abs.max_x - device->abs.min_x);
210                 }
211                 device->abs.old_x = e->value;
212                 device->type |= EVDEV_RELATIVE_MOTION;
213                 break;
214         case ABS_Y:
215                 e->value -= device->abs.min_y;
216                 if (device->abs.reset_y)
217                         device->abs.reset_y = 0;
218                 else {
219                         device->rel.dy =
220                                 (e->value - device->abs.old_y) *
221                                 touchpad_speed /
222                                 /* maybe use x size here to have the same scale? */
223                                 (device->abs.max_y - device->abs.min_y);
224                 }
225                 device->abs.old_y = e->value;
226                 device->type |= EVDEV_RELATIVE_MOTION;
227                 break;
228         }
229 }
230
231 static inline void
232 evdev_process_relative(struct evdev_input_device *device,
233                               struct input_event *e, uint32_t time)
234 {
235         switch (e->code) {
236         case REL_X:
237                 device->rel.dx += e->value;
238                 device->type |= EVDEV_RELATIVE_MOTION;
239                 break;
240         case REL_Y:
241                 device->rel.dy += e->value;
242                 device->type |= EVDEV_RELATIVE_MOTION;
243                 break;
244         case REL_WHEEL:
245                 notify_axis(&device->master->base.input_device,
246                               time,
247                               WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
248                 break;
249         case REL_HWHEEL:
250                 notify_axis(&device->master->base.input_device,
251                               time,
252                               WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
253                 break;
254         }
255 }
256
257 static inline void
258 evdev_process_absolute(struct evdev_input_device *device,
259                        struct input_event *e)
260 {
261         if (device->is_touchpad) {
262                 evdev_process_absolute_motion_touchpad(device, e);
263         } else if (device->is_mt) {
264                 evdev_process_touch(device, e);
265         } else {
266                 evdev_process_absolute_motion(device, e);
267         }
268 }
269
270 static int
271 is_motion_event(struct input_event *e)
272 {
273         switch (e->type) {
274         case EV_REL:
275                 switch (e->code) {
276                 case REL_X:
277                 case REL_Y:
278                         return 1;
279                 }
280         case EV_ABS:
281                 switch (e->code) {
282                 case ABS_X:
283                 case ABS_Y:
284                 case ABS_MT_POSITION_X:
285                 case ABS_MT_POSITION_Y:
286                         return 1;
287                 }
288         }
289
290         return 0;
291 }
292
293 static void
294 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
295 {
296         struct wl_input_device *master = &device->master->base.input_device;
297
298         if (!device->type)
299                 return;
300
301         if (device->type & EVDEV_RELATIVE_MOTION) {
302                 notify_motion(master, time,
303                               wl_fixed_to_int(master->x) + device->rel.dx,
304                               wl_fixed_to_int(master->y) + device->rel.dy);
305                 device->type &= ~EVDEV_RELATIVE_MOTION;
306                 device->rel.dx = 0;
307                 device->rel.dy = 0;
308         }
309         if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
310                 notify_touch(master, time,
311                              device->mt.slot,
312                              device->mt.x[device->mt.slot],
313                              device->mt.y[device->mt.slot],
314                              WL_INPUT_DEVICE_TOUCH_DOWN);
315                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
316                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
317         }
318         if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
319                 notify_touch(master, time,
320                              device->mt.slot,
321                              device->mt.x[device->mt.slot],
322                              device->mt.y[device->mt.slot],
323                              WL_INPUT_DEVICE_TOUCH_MOTION);
324                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
325                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
326         }
327         if (device->type & EVDEV_ABSOLUTE_MT_UP) {
328                 notify_touch(master, time, device->mt.slot, 0, 0,
329                              WL_INPUT_DEVICE_TOUCH_UP);
330                 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
331         }
332         if (device->type & EVDEV_ABSOLUTE_MOTION) {
333                 notify_motion(master, time, device->abs.x, device->abs.y);
334                 device->type &= ~EVDEV_ABSOLUTE_MOTION;
335         }
336 }
337
338 static void
339 evdev_process_events(struct evdev_input_device *device,
340                      struct input_event *ev, int count)
341 {
342         struct input_event *e, *end;
343         uint32_t time = 0;
344
345         device->type = 0;
346
347         e = ev;
348         end = e + count;
349         for (e = ev; e < end; e++) {
350                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
351
352                 /* we try to minimize the amount of notifications to be
353                  * forwarded to the compositor, so we accumulate motion
354                  * events and send as a bunch */
355                 if (!is_motion_event(e))
356                         evdev_flush_motion(device, time);
357                 switch (e->type) {
358                 case EV_REL:
359                         evdev_process_relative(device, e, time);
360                         break;
361                 case EV_ABS:
362                         evdev_process_absolute(device, e);
363                         break;
364                 case EV_KEY:
365                         evdev_process_key(device, e, time);
366                         break;
367                 }
368         }
369
370         evdev_flush_motion(device, time);
371 }
372
373 static int
374 evdev_input_device_data(int fd, uint32_t mask, void *data)
375 {
376         struct weston_compositor *ec;
377         struct evdev_input_device *device = data;
378         struct input_event ev[32];
379         int len;
380
381         ec = device->master->base.compositor;
382         if (!ec->focus)
383                 return 1;
384
385         /* If the compositor is repainting, this function is called only once
386          * per frame and we have to process all the events available on the
387          * fd, otherwise there will be input lag. */
388         do {
389                 if (device->mtdev)
390                         len = mtdev_get(device->mtdev, fd, ev,
391                                         ARRAY_LENGTH(ev)) *
392                                 sizeof (struct input_event);
393                 else
394                         len = read(fd, &ev, sizeof ev);
395
396                 if (len < 0 || len % sizeof ev[0] != 0) {
397                         /* FIXME: call device_removed when errno is ENODEV. */
398                         return 1;
399                 }
400
401                 evdev_process_events(device, ev, len / sizeof ev[0]);
402
403         } while (len > 0);
404
405         return 1;
406 }
407
408 /* copied from udev/extras/input_id/input_id.c */
409 /* we must use this kernel-compatible implementation */
410 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
411 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
412 #define OFF(x)  ((x)%BITS_PER_LONG)
413 #define BIT(x)  (1UL<<OFF(x))
414 #define LONG(x) ((x)/BITS_PER_LONG)
415 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
416 /* end copied */
417
418 static int
419 evdev_configure_device(struct evdev_input_device *device)
420 {
421         struct input_absinfo absinfo;
422         unsigned long ev_bits[NBITS(EV_MAX)];
423         unsigned long abs_bits[NBITS(ABS_MAX)];
424         unsigned long key_bits[NBITS(KEY_MAX)];
425         int has_key, has_abs;
426
427         has_key = 0;
428         has_abs = 0;
429
430         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
431         if (TEST_BIT(ev_bits, EV_ABS)) {
432                 has_abs = 1;
433
434                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
435                       abs_bits);
436                 if (TEST_BIT(abs_bits, ABS_X)) {
437                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
438                         device->abs.min_x = absinfo.minimum;
439                         device->abs.max_x = absinfo.maximum;
440                 }
441                 if (TEST_BIT(abs_bits, ABS_Y)) {
442                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
443                         device->abs.min_y = absinfo.minimum;
444                         device->abs.max_y = absinfo.maximum;
445                 }
446                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
447                         device->is_mt = 1;
448                         device->mt.slot = 0;
449                 }
450         }
451         if (TEST_BIT(ev_bits, EV_KEY)) {
452                 has_key = 1;
453                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
454                       key_bits);
455                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
456                              !TEST_BIT(key_bits, BTN_TOOL_PEN))
457                         device->is_touchpad = 1;
458         }
459
460         /* This rule tries to catch accelerometer devices and opt out. We may
461          * want to adjust the protocol later adding a proper event for dealing
462          * with accelerometers and implement here accordingly */
463         if (has_abs && !has_key)
464                 return -1;
465
466         return 0;
467 }
468
469 static struct evdev_input_device *
470 evdev_input_device_create(struct evdev_input *master,
471                           struct wl_display *display, const char *path)
472 {
473         struct evdev_input_device *device;
474         struct weston_compositor *ec;
475
476         device = malloc(sizeof *device);
477         if (device == NULL)
478                 return NULL;
479
480         ec = master->base.compositor;
481         device->output =
482                 container_of(ec->output_list.next, struct weston_output, link);
483
484         device->master = master;
485         device->is_touchpad = 0;
486         device->is_mt = 0;
487         device->mtdev = NULL;
488         device->devnode = strdup(path);
489         device->mt.slot = -1;
490         device->rel.dx = 0;
491         device->rel.dy = 0;
492
493         /* Use non-blocking mode so that we can loop on read on
494          * evdev_input_device_data() until all events on the fd are
495          * read.  mtdev_get() also expects this. */
496         device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
497         if (device->fd < 0)
498                 goto err0;
499
500         if (evdev_configure_device(device) == -1)
501                 goto err1;
502
503         if (device->is_mt) {
504                 device->mtdev = mtdev_new_open(device->fd);
505                 if (!device->mtdev)
506                         fprintf(stderr, "mtdev failed to open for %s\n", path);
507         }
508
509         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
510                                               WL_EVENT_READABLE,
511                                               evdev_input_device_data, device);
512         if (device->source == NULL)
513                 goto err1;
514
515         wl_list_insert(master->devices_list.prev, &device->link);
516
517         return device;
518
519 err1:
520         close(device->fd);
521 err0:
522         free(device->devnode);
523         free(device);
524         return NULL;
525 }
526
527 static const char default_seat[] = "seat0";
528
529 static void
530 device_added(struct udev_device *udev_device, struct evdev_input *master)
531 {
532         struct weston_compositor *c;
533         const char *devnode;
534         const char *device_seat;
535
536         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
537         if (!device_seat)
538                 device_seat = default_seat;
539
540         if (strcmp(device_seat, master->seat_id))
541                 return;
542
543         c = master->base.compositor;
544         devnode = udev_device_get_devnode(udev_device);
545         evdev_input_device_create(master, c->wl_display, devnode);
546 }
547
548 static void
549 device_removed(struct evdev_input_device *device)
550 {
551         wl_event_source_remove(device->source);
552         wl_list_remove(&device->link);
553         if (device->mtdev)
554                 mtdev_close_delete(device->mtdev);
555         close(device->fd);
556         free(device->devnode);
557         free(device);
558 }
559
560 static void
561 evdev_notify_keyboard_focus(struct evdev_input *input)
562 {
563         struct evdev_input_device *device;
564         struct wl_array keys;
565         unsigned int i, set;
566         char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
567         uint32_t *k;
568         int ret;
569
570         memset(all_keys, 0, sizeof all_keys);
571         wl_list_for_each(device, &input->devices_list, link) {
572                 memset(evdev_keys, 0, sizeof evdev_keys);
573                 ret = ioctl(device->fd,
574                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
575                 if (ret < 0) {
576                         fprintf(stderr, "failed to get keys for device %s\n",
577                                 device->devnode);
578                         continue;
579                 }
580                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
581                         all_keys[i] |= evdev_keys[i];
582         }
583
584         wl_array_init(&keys);
585         for (i = 0; i < KEY_CNT; i++) {
586                 set = all_keys[i >> 3] & (1 << (i & 7));
587                 if (set) {
588                         k = wl_array_add(&keys, sizeof *k);
589                         *k = i;
590                 }
591         }
592
593         notify_keyboard_focus(&input->base.input_device, &keys);
594
595         wl_array_release(&keys);
596 }
597
598 void
599 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
600 {
601         struct evdev_input *input = (struct evdev_input *) input_base;
602         struct udev_enumerate *e;
603         struct udev_list_entry *entry;
604         struct udev_device *device;
605         const char *path, *sysname;
606
607         e = udev_enumerate_new(udev);
608         udev_enumerate_add_match_subsystem(e, "input");
609         udev_enumerate_scan_devices(e);
610         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
611                 path = udev_list_entry_get_name(entry);
612                 device = udev_device_new_from_syspath(udev, path);
613
614                 sysname = udev_device_get_sysname(device);
615                 if (strncmp("event", sysname, 5) != 0) {
616                         udev_device_unref(device);
617                         continue;
618                 }
619
620                 device_added(device, input);
621
622                 udev_device_unref(device);
623         }
624         udev_enumerate_unref(e);
625
626         evdev_notify_keyboard_focus(input);
627
628         if (wl_list_empty(&input->devices_list)) {
629                 fprintf(stderr,
630                         "warning: no input devices on entering Weston. "
631                         "Possible causes:\n"
632                         "\t- no permissions to read /dev/input/event*\n"
633                         "\t- seats misconfigured "
634                         "(Weston backend option 'seat', "
635                         "udev device property ID_SEAT)\n");
636         }
637 }
638
639 static int
640 evdev_udev_handler(int fd, uint32_t mask, void *data)
641 {
642         struct evdev_input *master = data;
643         struct udev_device *udev_device;
644         struct evdev_input_device *device, *next;
645         const char *action;
646         const char *devnode;
647
648         udev_device = udev_monitor_receive_device(master->udev_monitor);
649         if (!udev_device)
650                 return 1;
651
652         action = udev_device_get_action(udev_device);
653         if (action) {
654                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
655                         return 0;
656
657                 if (!strcmp(action, "add")) {
658                         device_added(udev_device, master);
659                 }
660                 else if (!strcmp(action, "remove")) {
661                         devnode = udev_device_get_devnode(udev_device);
662                         wl_list_for_each_safe(device, next,
663                                               &master->devices_list, link)
664                                 if (!strcmp(device->devnode, devnode)) {
665                                         device_removed(device);
666                                         break;
667                                 }
668                 }
669         }
670         udev_device_unref(udev_device);
671
672         return 0;
673 }
674
675 int
676 evdev_enable_udev_monitor(struct udev *udev, struct weston_input_device *input_base)
677 {
678         struct evdev_input *master = (struct evdev_input *) input_base;
679         struct wl_event_loop *loop;
680         struct weston_compositor *c = master->base.compositor;
681         int fd;
682
683         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
684         if (!master->udev_monitor) {
685                 fprintf(stderr, "udev: failed to create the udev monitor\n");
686                 return 0;
687         }
688
689         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
690                         "input", NULL);
691
692         if (udev_monitor_enable_receiving(master->udev_monitor)) {
693                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
694                 udev_monitor_unref(master->udev_monitor);
695                 return 0;
696         }
697
698         loop = wl_display_get_event_loop(c->wl_display);
699         fd = udev_monitor_get_fd(master->udev_monitor);
700         master->udev_monitor_source =
701                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
702                                      evdev_udev_handler, master);
703         if (!master->udev_monitor_source) {
704                 udev_monitor_unref(master->udev_monitor);
705                 return 0;
706         }
707
708         return 1;
709 }
710
711 void
712 evdev_disable_udev_monitor(struct weston_input_device *input_base)
713 {
714         struct evdev_input *input = (struct evdev_input *) input_base;
715
716         if (!input->udev_monitor)
717                 return;
718
719         udev_monitor_unref(input->udev_monitor);
720         input->udev_monitor = NULL;
721         wl_event_source_remove(input->udev_monitor_source);
722         input->udev_monitor_source = NULL;
723 }
724
725 void
726 evdev_input_create(struct weston_compositor *c, struct udev *udev,
727                    const char *seat)
728 {
729         struct evdev_input *input;
730
731         input = malloc(sizeof *input);
732         if (input == NULL)
733                 return;
734
735         memset(input, 0, sizeof *input);
736         weston_input_device_init(&input->base, c);
737
738         wl_list_init(&input->devices_list);
739         input->seat_id = strdup(seat);
740         if (!evdev_enable_udev_monitor(udev, &input->base)) {
741                 free(input->seat_id);
742                 free(input);
743                 return;
744         }
745
746         evdev_add_devices(udev, &input->base);
747
748         c->input_device = &input->base.input_device;
749 }
750
751 void
752 evdev_remove_devices(struct weston_input_device *input_base)
753 {
754         struct evdev_input *input = (struct evdev_input *) input_base;
755         struct evdev_input_device *device, *next;
756
757         wl_list_for_each_safe(device, next, &input->devices_list, link)
758                 device_removed(device);
759
760         notify_keyboard_focus(&input->base.input_device, NULL);
761 }
762
763 void
764 evdev_input_destroy(struct weston_input_device *input_base)
765 {
766         struct evdev_input *input = (struct evdev_input *) input_base;
767
768         evdev_remove_devices(input_base);
769         evdev_disable_udev_monitor(&input->base);
770
771         wl_list_remove(&input->base.link);
772         free(input->seat_id);
773         free(input);
774 }