evdev: Release weston_seat with underlying evdev device
[profile/ivi/weston.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 <stdlib.h>
24 #include <string.h>
25 #include <linux/input.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <mtdev.h>
29
30 #include "compositor.h"
31 #include "evdev.h"
32 #include "evdev-private.h"
33 #include "launcher-util.h"
34 #include "log.h"
35
36 static void
37 evdev_led_update(struct weston_seat *seat_base, enum weston_led leds)
38 {
39         static const struct {
40                 enum weston_led weston;
41                 int evdev;
42         } map[] = {
43                 { LED_NUM_LOCK, LED_NUML },
44                 { LED_CAPS_LOCK, LED_CAPSL },
45                 { LED_SCROLL_LOCK, LED_SCROLLL },
46         };
47         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
48         struct evdev_input_device *device;
49         struct input_event ev[ARRAY_LENGTH(map)];
50         unsigned int i;
51
52         memset(ev, 0, sizeof(ev));
53         for (i = 0; i < ARRAY_LENGTH(map); i++) {
54                 ev[i].type = EV_LED;
55                 ev[i].code = map[i].evdev;
56                 ev[i].value = !!(leds & map[i].weston);
57         }
58
59         wl_list_for_each(device, &seat->devices_list, link) {
60                 if (device->caps & EVDEV_KEYBOARD)
61                         write(device->fd, ev, sizeof ev);
62         }
63 }
64
65 static inline void
66 evdev_process_key(struct evdev_input_device *device,
67                         struct input_event *e, int time)
68 {
69         if (e->value == 2)
70                 return;
71
72         switch (e->code) {
73         case BTN_LEFT:
74         case BTN_RIGHT:
75         case BTN_MIDDLE:
76         case BTN_SIDE:
77         case BTN_EXTRA:
78         case BTN_FORWARD:
79         case BTN_BACK:
80         case BTN_TASK:
81                 notify_button(&device->master->base.seat,
82                               time, e->code,
83                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
84                                          WL_POINTER_BUTTON_STATE_RELEASED);
85                 break;
86
87         default:
88                 notify_key(&device->master->base.seat,
89                            time, e->code,
90                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
91                                       WL_KEYBOARD_KEY_STATE_RELEASED,
92                            STATE_UPDATE_AUTOMATIC);
93                 break;
94         }
95 }
96
97 static void
98 evdev_process_touch(struct evdev_input_device *device,
99                     struct input_event *e)
100 {
101         const int screen_width = device->output->current->width;
102         const int screen_height = device->output->current->height;
103
104         switch (e->code) {
105         case ABS_MT_SLOT:
106                 device->mt.slot = e->value;
107                 break;
108         case ABS_MT_TRACKING_ID:
109                 if (e->value >= 0)
110                         device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
111                 else
112                         device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
113                 break;
114         case ABS_MT_POSITION_X:
115                 device->mt.x[device->mt.slot] =
116                         (e->value - device->abs.min_x) * screen_width /
117                         (device->abs.max_x - device->abs.min_x) +
118                         device->output->x;
119                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
120                 break;
121         case ABS_MT_POSITION_Y:
122                 device->mt.y[device->mt.slot] =
123                         (e->value - device->abs.min_y) * screen_height /
124                         (device->abs.max_y - device->abs.min_y) +
125                         device->output->y;
126                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
127                 break;
128         }
129 }
130
131 static inline void
132 evdev_process_absolute_motion(struct evdev_input_device *device,
133                               struct input_event *e)
134 {
135         const int screen_width = device->output->current->width;
136         const int screen_height = device->output->current->height;
137
138         switch (e->code) {
139         case ABS_X:
140                 device->abs.x =
141                         (e->value - device->abs.min_x) * screen_width /
142                         (device->abs.max_x - device->abs.min_x) +
143                         device->output->x;
144                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
145                 break;
146         case ABS_Y:
147                 device->abs.y =
148                         (e->value - device->abs.min_y) * screen_height /
149                         (device->abs.max_y - device->abs.min_y) +
150                         device->output->y;
151                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
152                 break;
153         }
154 }
155
156 static inline void
157 evdev_process_relative(struct evdev_input_device *device,
158                        struct input_event *e, uint32_t time)
159 {
160         switch (e->code) {
161         case REL_X:
162                 device->rel.dx += wl_fixed_from_int(e->value);
163                 device->pending_events |= EVDEV_RELATIVE_MOTION;
164                 break;
165         case REL_Y:
166                 device->rel.dy += wl_fixed_from_int(e->value);
167                 device->pending_events |= EVDEV_RELATIVE_MOTION;
168                 break;
169         case REL_WHEEL:
170                 notify_axis(&device->master->base.seat,
171                               time,
172                               WL_POINTER_AXIS_VERTICAL_SCROLL,
173                               wl_fixed_from_int(e->value));
174                 break;
175         case REL_HWHEEL:
176                 notify_axis(&device->master->base.seat,
177                               time,
178                               WL_POINTER_AXIS_HORIZONTAL_SCROLL,
179                               wl_fixed_from_int(e->value));
180                 break;
181         }
182 }
183
184 static inline void
185 evdev_process_absolute(struct evdev_input_device *device,
186                        struct input_event *e)
187 {
188         if (device->is_mt) {
189                 evdev_process_touch(device, e);
190         } else {
191                 evdev_process_absolute_motion(device, e);
192         }
193 }
194
195 static int
196 is_motion_event(struct input_event *e)
197 {
198         switch (e->type) {
199         case EV_REL:
200                 switch (e->code) {
201                 case REL_X:
202                 case REL_Y:
203                         return 1;
204                 }
205         case EV_ABS:
206                 switch (e->code) {
207                 case ABS_X:
208                 case ABS_Y:
209                 case ABS_MT_POSITION_X:
210                 case ABS_MT_POSITION_Y:
211                         return 1;
212                 }
213         }
214
215         return 0;
216 }
217
218 static void
219 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
220 {
221         struct weston_seat *master = &device->master->base;
222
223         if (!device->pending_events)
224                 return;
225
226         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
227                 notify_motion(&master->seat, time,
228                               master->seat.pointer->x + device->rel.dx,
229                               master->seat.pointer->y + device->rel.dy);
230                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
231                 device->rel.dx = 0;
232                 device->rel.dy = 0;
233         }
234         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
235                 notify_touch(&master->seat, time,
236                              device->mt.slot,
237                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
238                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
239                              WL_TOUCH_DOWN);
240                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
241                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
242         }
243         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
244                 notify_touch(&master->seat, time,
245                              device->mt.slot,
246                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
247                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
248                              WL_TOUCH_MOTION);
249                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
250                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
251         }
252         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
253                 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
254                              WL_TOUCH_UP);
255                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
256         }
257         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
258                 notify_motion(&master->seat, time,
259                               wl_fixed_from_int(device->abs.x),
260                               wl_fixed_from_int(device->abs.y));
261                 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
262         }
263 }
264
265 static void
266 fallback_process(struct evdev_dispatch *dispatch,
267                  struct evdev_input_device *device,
268                  struct input_event *event,
269                  uint32_t time)
270 {
271         switch (event->type) {
272         case EV_REL:
273                 evdev_process_relative(device, event, time);
274                 break;
275         case EV_ABS:
276                 evdev_process_absolute(device, event);
277                 break;
278         case EV_KEY:
279                 evdev_process_key(device, event, time);
280                 break;
281         }
282 }
283
284 static void
285 fallback_destroy(struct evdev_dispatch *dispatch)
286 {
287         free(dispatch);
288 }
289
290 struct evdev_dispatch_interface fallback_interface = {
291         fallback_process,
292         fallback_destroy
293 };
294
295 static struct evdev_dispatch *
296 fallback_dispatch_create(void)
297 {
298         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
299         if (dispatch == NULL)
300                 return NULL;
301
302         dispatch->interface = &fallback_interface;
303
304         return dispatch;
305 }
306
307 static void
308 evdev_process_events(struct evdev_input_device *device,
309                      struct input_event *ev, int count)
310 {
311         struct evdev_dispatch *dispatch = device->dispatch;
312         struct input_event *e, *end;
313         uint32_t time = 0;
314
315         device->pending_events = 0;
316
317         e = ev;
318         end = e + count;
319         for (e = ev; e < end; e++) {
320                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
321
322                 /* we try to minimize the amount of notifications to be
323                  * forwarded to the compositor, so we accumulate motion
324                  * events and send as a bunch */
325                 if (!is_motion_event(e))
326                         evdev_flush_motion(device, time);
327
328                 dispatch->interface->process(dispatch, device, e, time);
329         }
330
331         evdev_flush_motion(device, time);
332 }
333
334 static int
335 evdev_input_device_data(int fd, uint32_t mask, void *data)
336 {
337         struct weston_compositor *ec;
338         struct evdev_input_device *device = data;
339         struct input_event ev[32];
340         int len;
341
342         ec = device->master->base.compositor;
343         if (!ec->focus)
344                 return 1;
345
346         /* If the compositor is repainting, this function is called only once
347          * per frame and we have to process all the events available on the
348          * fd, otherwise there will be input lag. */
349         do {
350                 if (device->mtdev)
351                         len = mtdev_get(device->mtdev, fd, ev,
352                                         ARRAY_LENGTH(ev)) *
353                                 sizeof (struct input_event);
354                 else
355                         len = read(fd, &ev, sizeof ev);
356
357                 if (len < 0 || len % sizeof ev[0] != 0) {
358                         /* FIXME: call device_removed when errno is ENODEV. */
359                         return 1;
360                 }
361
362                 evdev_process_events(device, ev, len / sizeof ev[0]);
363
364         } while (len > 0);
365
366         return 1;
367 }
368
369 static int
370 evdev_configure_device(struct evdev_input_device *device)
371 {
372         struct input_absinfo absinfo;
373         unsigned long ev_bits[NBITS(EV_MAX)];
374         unsigned long abs_bits[NBITS(ABS_MAX)];
375         unsigned long rel_bits[NBITS(REL_MAX)];
376         unsigned long key_bits[NBITS(KEY_MAX)];
377         int has_key, has_abs;
378         unsigned int i;
379
380         has_key = 0;
381         has_abs = 0;
382         device->caps = 0;
383
384         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
385         if (TEST_BIT(ev_bits, EV_ABS)) {
386                 has_abs = 1;
387
388                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
389                       abs_bits);
390                 if (TEST_BIT(abs_bits, ABS_X)) {
391                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
392                         device->abs.min_x = absinfo.minimum;
393                         device->abs.max_x = absinfo.maximum;
394                         device->caps |= EVDEV_MOTION_ABS;
395                 }
396                 if (TEST_BIT(abs_bits, ABS_Y)) {
397                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
398                         device->abs.min_y = absinfo.minimum;
399                         device->abs.max_y = absinfo.maximum;
400                         device->caps |= EVDEV_MOTION_ABS;
401                 }
402                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
403                         device->is_mt = 1;
404                         device->mt.slot = 0;
405                         device->caps |= EVDEV_TOUCH;
406                 }
407         }
408         if (TEST_BIT(ev_bits, EV_REL)) {
409                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
410                       rel_bits);
411                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
412                         device->caps |= EVDEV_MOTION_REL;
413         }
414         if (TEST_BIT(ev_bits, EV_KEY)) {
415                 has_key = 1;
416                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
417                       key_bits);
418                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
419                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
420                     has_abs)
421                         device->dispatch = evdev_touchpad_create(device);
422                 for (i = KEY_ESC; i < KEY_MAX; i++) {
423                         if (i >= BTN_MISC && i < KEY_OK)
424                                 continue;
425                         if (TEST_BIT(key_bits, i)) {
426                                 device->caps |= EVDEV_KEYBOARD;
427                                 break;
428                         }
429                 }
430                 for (i = BTN_MISC; i < KEY_OK; i++) {
431                         if (TEST_BIT(key_bits, i)) {
432                                 device->caps |= EVDEV_BUTTON;
433                                 break;
434                         }
435                 }
436         }
437         if (TEST_BIT(ev_bits, EV_LED)) {
438                 device->caps |= EVDEV_KEYBOARD;
439         }
440
441         /* This rule tries to catch accelerometer devices and opt out. We may
442          * want to adjust the protocol later adding a proper event for dealing
443          * with accelerometers and implement here accordingly */
444         if (has_abs && !has_key && !device->is_mt)
445                 return -1;
446
447         if ((device->caps &
448              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
449                 weston_seat_init_pointer(&device->master->base);
450         if ((device->caps & EVDEV_KEYBOARD))
451                 weston_seat_init_keyboard(&device->master->base, NULL);
452         if ((device->caps & EVDEV_TOUCH))
453                 weston_seat_init_touch(&device->master->base);
454
455         return 0;
456 }
457
458 static struct evdev_input_device *
459 evdev_input_device_create(struct evdev_seat *master,
460                           struct wl_display *display, const char *path)
461 {
462         struct evdev_input_device *device;
463         struct weston_compositor *ec;
464
465         device = malloc(sizeof *device);
466         if (device == NULL)
467                 return NULL;
468
469         ec = master->base.compositor;
470         device->output =
471                 container_of(ec->output_list.next, struct weston_output, link);
472
473         device->master = master;
474         device->is_mt = 0;
475         device->mtdev = NULL;
476         device->devnode = strdup(path);
477         device->mt.slot = -1;
478         device->rel.dx = 0;
479         device->rel.dy = 0;
480         device->dispatch = NULL;
481
482         /* Use non-blocking mode so that we can loop on read on
483          * evdev_input_device_data() until all events on the fd are
484          * read.  mtdev_get() also expects this. */
485         device->fd = weston_launcher_open(ec, path, O_RDWR | O_NONBLOCK);
486         if (device->fd < 0)
487                 goto err0;
488
489         if (evdev_configure_device(device) == -1)
490                 goto err1;
491
492         /* If the dispatch was not set up use the fallback. */
493         if (device->dispatch == NULL)
494                 device->dispatch = fallback_dispatch_create();
495         if (device->dispatch == NULL)
496                 goto err1;
497
498
499         if (device->is_mt) {
500                 device->mtdev = mtdev_new_open(device->fd);
501                 if (!device->mtdev)
502                         weston_log("mtdev failed to open for %s\n", path);
503         }
504
505         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
506                                               WL_EVENT_READABLE,
507                                               evdev_input_device_data, device);
508         if (device->source == NULL)
509                 goto err2;
510
511         wl_list_insert(master->devices_list.prev, &device->link);
512
513         return device;
514
515 err2:
516         device->dispatch->interface->destroy(device->dispatch);
517 err1:
518         close(device->fd);
519 err0:
520         free(device->devnode);
521         free(device);
522         return NULL;
523 }
524
525 static const char default_seat[] = "seat0";
526
527 static void
528 device_added(struct udev_device *udev_device, struct evdev_seat *master)
529 {
530         struct weston_compositor *c;
531         const char *devnode;
532         const char *device_seat;
533
534         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
535         if (!device_seat)
536                 device_seat = default_seat;
537
538         if (strcmp(device_seat, master->seat_id))
539                 return;
540
541         c = master->base.compositor;
542         devnode = udev_device_get_devnode(udev_device);
543         evdev_input_device_create(master, c->wl_display, devnode);
544 }
545
546 static void
547 device_removed(struct evdev_input_device *device)
548 {
549         struct evdev_dispatch *dispatch;
550
551         dispatch = device->dispatch;
552         if (dispatch)
553                 dispatch->interface->destroy(dispatch);
554
555         wl_event_source_remove(device->source);
556         wl_list_remove(&device->link);
557         if (device->mtdev)
558                 mtdev_close_delete(device->mtdev);
559         close(device->fd);
560         free(device->devnode);
561         free(device);
562 }
563
564 static void
565 evdev_notify_keyboard_focus(struct evdev_seat *seat)
566 {
567         struct evdev_input_device *device;
568         struct wl_array keys;
569         unsigned int i, set;
570         char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
571         uint32_t *k;
572         int ret;
573
574         memset(all_keys, 0, sizeof all_keys);
575         wl_list_for_each(device, &seat->devices_list, link) {
576                 memset(evdev_keys, 0, sizeof evdev_keys);
577                 ret = ioctl(device->fd,
578                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
579                 if (ret < 0) {
580                         weston_log("failed to get keys for device %s\n",
581                                 device->devnode);
582                         continue;
583                 }
584                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
585                         all_keys[i] |= evdev_keys[i];
586         }
587
588         wl_array_init(&keys);
589         for (i = 0; i < KEY_CNT; i++) {
590                 set = all_keys[i >> 3] & (1 << (i & 7));
591                 if (set) {
592                         k = wl_array_add(&keys, sizeof *k);
593                         *k = i;
594                 }
595         }
596
597         notify_keyboard_focus_in(&seat->base.seat, &keys,
598                                  STATE_UPDATE_AUTOMATIC);
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                 weston_log(
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                 weston_log("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                 weston_log("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         seat->base.led_update = evdev_led_update;
743
744         wl_list_init(&seat->devices_list);
745         seat->seat_id = strdup(seat_id);
746         if (!evdev_enable_udev_monitor(udev, &seat->base)) {
747                 free(seat->seat_id);
748                 free(seat);
749                 return;
750         }
751
752         evdev_add_devices(udev, &seat->base);
753
754         c->seat = &seat->base;
755 }
756
757 void
758 evdev_remove_devices(struct weston_seat *seat_base)
759 {
760         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
761         struct evdev_input_device *device, *next;
762
763         wl_list_for_each_safe(device, next, &seat->devices_list, link)
764                 device_removed(device);
765
766         notify_keyboard_focus_out(&seat->base.seat);
767 }
768
769 void
770 evdev_input_destroy(struct weston_seat *seat_base)
771 {
772         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
773
774         evdev_remove_devices(seat_base);
775         evdev_disable_udev_monitor(&seat->base);
776
777         weston_seat_release(seat_base);
778         free(seat->seat_id);
779         free(seat);
780 }