26002b42075822eb3c2cf01df4118ac834080f41
[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_seat {
36         struct weston_seat 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_seat *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                 wl_fixed_t 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.seat,
124                               time, e->code, e->value);
125                 break;
126
127         default:
128                 notify_key(&device->master->base.seat,
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         int dx, dy;
200
201         switch (e->code) {
202         case ABS_X:
203                 e->value -= device->abs.min_x;
204                 if (device->abs.reset_x)
205                         device->abs.reset_x = 0;
206                 else {
207                         dx =
208                                 (e->value - device->abs.old_x) *
209                                 touchpad_speed /
210                                 (device->abs.max_x - device->abs.min_x);
211                         device->rel.dx = wl_fixed_from_int(dx);
212                 }
213                 device->abs.old_x = e->value;
214                 device->type |= EVDEV_RELATIVE_MOTION;
215                 break;
216         case ABS_Y:
217                 e->value -= device->abs.min_y;
218                 if (device->abs.reset_y)
219                         device->abs.reset_y = 0;
220                 else {
221                         dy =
222                                 (e->value - device->abs.old_y) *
223                                 touchpad_speed /
224                                 /* maybe use x size here to have the same scale? */
225                                 (device->abs.max_y - device->abs.min_y);
226                         device->rel.dy = wl_fixed_from_int(dy);
227                 }
228                 device->abs.old_y = e->value;
229                 device->type |= EVDEV_RELATIVE_MOTION;
230                 break;
231         }
232 }
233
234 static inline void
235 evdev_process_relative(struct evdev_input_device *device,
236                               struct input_event *e, uint32_t time)
237 {
238         switch (e->code) {
239         case REL_X:
240                 device->rel.dx += wl_fixed_from_int(e->value);
241                 device->type |= EVDEV_RELATIVE_MOTION;
242                 break;
243         case REL_Y:
244                 device->rel.dy += wl_fixed_from_int(e->value);
245                 device->type |= EVDEV_RELATIVE_MOTION;
246                 break;
247         case REL_WHEEL:
248                 notify_axis(&device->master->base.seat,
249                               time,
250                               WL_POINTER_AXIS_VERTICAL_SCROLL, e->value);
251                 break;
252         case REL_HWHEEL:
253                 notify_axis(&device->master->base.seat,
254                               time,
255                               WL_POINTER_AXIS_HORIZONTAL_SCROLL, e->value);
256                 break;
257         }
258 }
259
260 static inline void
261 evdev_process_absolute(struct evdev_input_device *device,
262                        struct input_event *e)
263 {
264         if (device->is_touchpad) {
265                 evdev_process_absolute_motion_touchpad(device, e);
266         } else if (device->is_mt) {
267                 evdev_process_touch(device, e);
268         } else {
269                 evdev_process_absolute_motion(device, e);
270         }
271 }
272
273 static int
274 is_motion_event(struct input_event *e)
275 {
276         switch (e->type) {
277         case EV_REL:
278                 switch (e->code) {
279                 case REL_X:
280                 case REL_Y:
281                         return 1;
282                 }
283         case EV_ABS:
284                 switch (e->code) {
285                 case ABS_X:
286                 case ABS_Y:
287                 case ABS_MT_POSITION_X:
288                 case ABS_MT_POSITION_Y:
289                         return 1;
290                 }
291         }
292
293         return 0;
294 }
295
296 static void
297 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
298 {
299         struct weston_seat *master = &device->master->base;
300
301         if (!device->type)
302                 return;
303
304         if (device->type & EVDEV_RELATIVE_MOTION) {
305                 notify_motion(&master->seat, time,
306                               master->seat.pointer->x + device->rel.dx,
307                               master->seat.pointer->y + device->rel.dy);
308                 device->type &= ~EVDEV_RELATIVE_MOTION;
309                 device->rel.dx = 0;
310                 device->rel.dy = 0;
311         }
312         if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
313                 notify_touch(&master->seat, time,
314                              device->mt.slot,
315                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
316                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
317                              WL_TOUCH_DOWN);
318                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
319                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
320         }
321         if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
322                 notify_touch(&master->seat, time,
323                              device->mt.slot,
324                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
325                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
326                              WL_TOUCH_MOTION);
327                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
328                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
329         }
330         if (device->type & EVDEV_ABSOLUTE_MT_UP) {
331                 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
332                              WL_TOUCH_UP);
333                 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
334         }
335         if (device->type & EVDEV_ABSOLUTE_MOTION) {
336                 notify_motion(&master->seat, time,
337                               wl_fixed_from_int(device->abs.x),
338                               wl_fixed_from_int(device->abs.y));
339                 device->type &= ~EVDEV_ABSOLUTE_MOTION;
340         }
341 }
342
343 static void
344 evdev_process_events(struct evdev_input_device *device,
345                      struct input_event *ev, int count)
346 {
347         struct input_event *e, *end;
348         uint32_t time = 0;
349
350         device->type = 0;
351
352         e = ev;
353         end = e + count;
354         for (e = ev; e < end; e++) {
355                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
356
357                 /* we try to minimize the amount of notifications to be
358                  * forwarded to the compositor, so we accumulate motion
359                  * events and send as a bunch */
360                 if (!is_motion_event(e))
361                         evdev_flush_motion(device, time);
362                 switch (e->type) {
363                 case EV_REL:
364                         evdev_process_relative(device, e, time);
365                         break;
366                 case EV_ABS:
367                         evdev_process_absolute(device, e);
368                         break;
369                 case EV_KEY:
370                         evdev_process_key(device, e, time);
371                         break;
372                 }
373         }
374
375         evdev_flush_motion(device, time);
376 }
377
378 static int
379 evdev_input_device_data(int fd, uint32_t mask, void *data)
380 {
381         struct weston_compositor *ec;
382         struct evdev_input_device *device = data;
383         struct input_event ev[32];
384         int len;
385
386         ec = device->master->base.compositor;
387         if (!ec->focus)
388                 return 1;
389
390         /* If the compositor is repainting, this function is called only once
391          * per frame and we have to process all the events available on the
392          * fd, otherwise there will be input lag. */
393         do {
394                 if (device->mtdev)
395                         len = mtdev_get(device->mtdev, fd, ev,
396                                         ARRAY_LENGTH(ev)) *
397                                 sizeof (struct input_event);
398                 else
399                         len = read(fd, &ev, sizeof ev);
400
401                 if (len < 0 || len % sizeof ev[0] != 0) {
402                         /* FIXME: call device_removed when errno is ENODEV. */
403                         return 1;
404                 }
405
406                 evdev_process_events(device, ev, len / sizeof ev[0]);
407
408         } while (len > 0);
409
410         return 1;
411 }
412
413 /* copied from udev/extras/input_id/input_id.c */
414 /* we must use this kernel-compatible implementation */
415 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
416 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
417 #define OFF(x)  ((x)%BITS_PER_LONG)
418 #define BIT(x)  (1UL<<OFF(x))
419 #define LONG(x) ((x)/BITS_PER_LONG)
420 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
421 /* end copied */
422
423 static int
424 evdev_configure_device(struct evdev_input_device *device)
425 {
426         struct input_absinfo absinfo;
427         unsigned long ev_bits[NBITS(EV_MAX)];
428         unsigned long abs_bits[NBITS(ABS_MAX)];
429         unsigned long key_bits[NBITS(KEY_MAX)];
430         int has_key, has_abs;
431
432         has_key = 0;
433         has_abs = 0;
434
435         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
436         if (TEST_BIT(ev_bits, EV_ABS)) {
437                 has_abs = 1;
438
439                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
440                       abs_bits);
441                 if (TEST_BIT(abs_bits, ABS_X)) {
442                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
443                         device->abs.min_x = absinfo.minimum;
444                         device->abs.max_x = absinfo.maximum;
445                 }
446                 if (TEST_BIT(abs_bits, ABS_Y)) {
447                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
448                         device->abs.min_y = absinfo.minimum;
449                         device->abs.max_y = absinfo.maximum;
450                 }
451                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
452                         device->is_mt = 1;
453                         device->mt.slot = 0;
454                 }
455         }
456         if (TEST_BIT(ev_bits, EV_KEY)) {
457                 has_key = 1;
458                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
459                       key_bits);
460                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
461                              !TEST_BIT(key_bits, BTN_TOOL_PEN))
462                         device->is_touchpad = 1;
463         }
464
465         /* This rule tries to catch accelerometer devices and opt out. We may
466          * want to adjust the protocol later adding a proper event for dealing
467          * with accelerometers and implement here accordingly */
468         if (has_abs && !has_key)
469                 return -1;
470
471         return 0;
472 }
473
474 static struct evdev_input_device *
475 evdev_input_device_create(struct evdev_seat *master,
476                           struct wl_display *display, const char *path)
477 {
478         struct evdev_input_device *device;
479         struct weston_compositor *ec;
480
481         device = malloc(sizeof *device);
482         if (device == NULL)
483                 return NULL;
484
485         ec = master->base.compositor;
486         device->output =
487                 container_of(ec->output_list.next, struct weston_output, link);
488
489         device->master = master;
490         device->is_touchpad = 0;
491         device->is_mt = 0;
492         device->mtdev = NULL;
493         device->devnode = strdup(path);
494         device->mt.slot = -1;
495         device->rel.dx = 0;
496         device->rel.dy = 0;
497
498         /* Use non-blocking mode so that we can loop on read on
499          * evdev_input_device_data() until all events on the fd are
500          * read.  mtdev_get() also expects this. */
501         device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
502         if (device->fd < 0)
503                 goto err0;
504
505         if (evdev_configure_device(device) == -1)
506                 goto err1;
507
508         if (device->is_mt) {
509                 device->mtdev = mtdev_new_open(device->fd);
510                 if (!device->mtdev)
511                         fprintf(stderr, "mtdev failed to open for %s\n", path);
512         }
513
514         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
515                                               WL_EVENT_READABLE,
516                                               evdev_input_device_data, device);
517         if (device->source == NULL)
518                 goto err1;
519
520         wl_list_insert(master->devices_list.prev, &device->link);
521
522         return device;
523
524 err1:
525         close(device->fd);
526 err0:
527         free(device->devnode);
528         free(device);
529         return NULL;
530 }
531
532 static const char default_seat[] = "seat0";
533
534 static void
535 device_added(struct udev_device *udev_device, struct evdev_seat *master)
536 {
537         struct weston_compositor *c;
538         const char *devnode;
539         const char *device_seat;
540
541         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
542         if (!device_seat)
543                 device_seat = default_seat;
544
545         if (strcmp(device_seat, master->seat_id))
546                 return;
547
548         c = master->base.compositor;
549         devnode = udev_device_get_devnode(udev_device);
550         evdev_input_device_create(master, c->wl_display, devnode);
551 }
552
553 static void
554 device_removed(struct evdev_input_device *device)
555 {
556         wl_event_source_remove(device->source);
557         wl_list_remove(&device->link);
558         if (device->mtdev)
559                 mtdev_close_delete(device->mtdev);
560         close(device->fd);
561         free(device->devnode);
562         free(device);
563 }
564
565 static void
566 evdev_notify_keyboard_focus(struct evdev_seat *seat)
567 {
568         struct evdev_input_device *device;
569         struct wl_array keys;
570         unsigned int i, set;
571         char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
572         uint32_t *k;
573         int ret;
574
575         memset(all_keys, 0, sizeof all_keys);
576         wl_list_for_each(device, &seat->devices_list, link) {
577                 memset(evdev_keys, 0, sizeof evdev_keys);
578                 ret = ioctl(device->fd,
579                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
580                 if (ret < 0) {
581                         fprintf(stderr, "failed to get keys for device %s\n",
582                                 device->devnode);
583                         continue;
584                 }
585                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
586                         all_keys[i] |= evdev_keys[i];
587         }
588
589         wl_array_init(&keys);
590         for (i = 0; i < KEY_CNT; i++) {
591                 set = all_keys[i >> 3] & (1 << (i & 7));
592                 if (set) {
593                         k = wl_array_add(&keys, sizeof *k);
594                         *k = i;
595                 }
596         }
597
598         notify_keyboard_focus(&seat->base.seat, &keys);
599
600         wl_array_release(&keys);
601 }
602
603 void
604 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
605 {
606         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
607         struct udev_enumerate *e;
608         struct udev_list_entry *entry;
609         struct udev_device *device;
610         const char *path, *sysname;
611
612         e = udev_enumerate_new(udev);
613         udev_enumerate_add_match_subsystem(e, "input");
614         udev_enumerate_scan_devices(e);
615         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
616                 path = udev_list_entry_get_name(entry);
617                 device = udev_device_new_from_syspath(udev, path);
618
619                 sysname = udev_device_get_sysname(device);
620                 if (strncmp("event", sysname, 5) != 0) {
621                         udev_device_unref(device);
622                         continue;
623                 }
624
625                 device_added(device, seat);
626
627                 udev_device_unref(device);
628         }
629         udev_enumerate_unref(e);
630
631         evdev_notify_keyboard_focus(seat);
632
633         if (wl_list_empty(&seat->devices_list)) {
634                 fprintf(stderr,
635                         "warning: no input devices on entering Weston. "
636                         "Possible causes:\n"
637                         "\t- no permissions to read /dev/input/event*\n"
638                         "\t- seats misconfigured "
639                         "(Weston backend option 'seat', "
640                         "udev device property ID_SEAT)\n");
641         }
642 }
643
644 static int
645 evdev_udev_handler(int fd, uint32_t mask, void *data)
646 {
647         struct evdev_seat *master = data;
648         struct udev_device *udev_device;
649         struct evdev_input_device *device, *next;
650         const char *action;
651         const char *devnode;
652
653         udev_device = udev_monitor_receive_device(master->udev_monitor);
654         if (!udev_device)
655                 return 1;
656
657         action = udev_device_get_action(udev_device);
658         if (action) {
659                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
660                         return 0;
661
662                 if (!strcmp(action, "add")) {
663                         device_added(udev_device, master);
664                 }
665                 else if (!strcmp(action, "remove")) {
666                         devnode = udev_device_get_devnode(udev_device);
667                         wl_list_for_each_safe(device, next,
668                                               &master->devices_list, link)
669                                 if (!strcmp(device->devnode, devnode)) {
670                                         device_removed(device);
671                                         break;
672                                 }
673                 }
674         }
675         udev_device_unref(udev_device);
676
677         return 0;
678 }
679
680 int
681 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
682 {
683         struct evdev_seat *master = (struct evdev_seat *) seat_base;
684         struct wl_event_loop *loop;
685         struct weston_compositor *c = master->base.compositor;
686         int fd;
687
688         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
689         if (!master->udev_monitor) {
690                 fprintf(stderr, "udev: failed to create the udev monitor\n");
691                 return 0;
692         }
693
694         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
695                         "input", NULL);
696
697         if (udev_monitor_enable_receiving(master->udev_monitor)) {
698                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
699                 udev_monitor_unref(master->udev_monitor);
700                 return 0;
701         }
702
703         loop = wl_display_get_event_loop(c->wl_display);
704         fd = udev_monitor_get_fd(master->udev_monitor);
705         master->udev_monitor_source =
706                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
707                                      evdev_udev_handler, master);
708         if (!master->udev_monitor_source) {
709                 udev_monitor_unref(master->udev_monitor);
710                 return 0;
711         }
712
713         return 1;
714 }
715
716 void
717 evdev_disable_udev_monitor(struct weston_seat *seat_base)
718 {
719         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
720
721         if (!seat->udev_monitor)
722                 return;
723
724         udev_monitor_unref(seat->udev_monitor);
725         seat->udev_monitor = NULL;
726         wl_event_source_remove(seat->udev_monitor_source);
727         seat->udev_monitor_source = NULL;
728 }
729
730 void
731 evdev_input_create(struct weston_compositor *c, struct udev *udev,
732                    const char *seat_id)
733 {
734         struct evdev_seat *seat;
735
736         seat = malloc(sizeof *seat);
737         if (seat == NULL)
738                 return;
739
740         memset(seat, 0, sizeof *seat);
741         weston_seat_init(&seat->base, c);
742
743         wl_list_init(&seat->devices_list);
744         seat->seat_id = strdup(seat_id);
745         if (!evdev_enable_udev_monitor(udev, &seat->base)) {
746                 free(seat->seat_id);
747                 free(seat);
748                 return;
749         }
750
751         evdev_add_devices(udev, &seat->base);
752
753         c->seat = &seat->base;
754 }
755
756 void
757 evdev_remove_devices(struct weston_seat *seat_base)
758 {
759         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
760         struct evdev_input_device *device, *next;
761
762         wl_list_for_each_safe(device, next, &seat->devices_list, link)
763                 device_removed(device);
764
765         notify_keyboard_focus(&seat->base.seat, NULL);
766 }
767
768 void
769 evdev_input_destroy(struct weston_seat *seat_base)
770 {
771         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
772
773         evdev_remove_devices(seat_base);
774         evdev_disable_udev_monitor(&seat->base);
775
776         wl_list_remove(&seat->base.link);
777         free(seat->seat_id);
778         free(seat);
779 }