evdev: query position ranges for MT, too
[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                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
404                               &absinfo);
405                         device->abs.min_x = absinfo.minimum;
406                         device->abs.max_x = absinfo.maximum;
407                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
408                               &absinfo);
409                         device->abs.min_y = absinfo.minimum;
410                         device->abs.max_y = absinfo.maximum;
411                         device->is_mt = 1;
412                         device->mt.slot = 0;
413                         device->caps |= EVDEV_TOUCH;
414                 }
415         }
416         if (TEST_BIT(ev_bits, EV_REL)) {
417                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
418                       rel_bits);
419                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
420                         device->caps |= EVDEV_MOTION_REL;
421         }
422         if (TEST_BIT(ev_bits, EV_KEY)) {
423                 has_key = 1;
424                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
425                       key_bits);
426                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
427                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
428                     has_abs)
429                         device->dispatch = evdev_touchpad_create(device);
430                 for (i = KEY_ESC; i < KEY_MAX; i++) {
431                         if (i >= BTN_MISC && i < KEY_OK)
432                                 continue;
433                         if (TEST_BIT(key_bits, i)) {
434                                 device->caps |= EVDEV_KEYBOARD;
435                                 break;
436                         }
437                 }
438                 for (i = BTN_MISC; i < KEY_OK; i++) {
439                         if (TEST_BIT(key_bits, i)) {
440                                 device->caps |= EVDEV_BUTTON;
441                                 break;
442                         }
443                 }
444         }
445         if (TEST_BIT(ev_bits, EV_LED)) {
446                 device->caps |= EVDEV_KEYBOARD;
447         }
448
449         /* This rule tries to catch accelerometer devices and opt out. We may
450          * want to adjust the protocol later adding a proper event for dealing
451          * with accelerometers and implement here accordingly */
452         if (has_abs && !has_key && !device->is_mt)
453                 return -1;
454
455         if ((device->caps &
456              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
457                 weston_seat_init_pointer(&device->master->base);
458         if ((device->caps & EVDEV_KEYBOARD))
459                 weston_seat_init_keyboard(&device->master->base, NULL);
460         if ((device->caps & EVDEV_TOUCH))
461                 weston_seat_init_touch(&device->master->base);
462
463         return 0;
464 }
465
466 static struct evdev_input_device *
467 evdev_input_device_create(struct evdev_seat *master,
468                           struct wl_display *display, const char *path)
469 {
470         struct evdev_input_device *device;
471         struct weston_compositor *ec;
472
473         device = malloc(sizeof *device);
474         if (device == NULL)
475                 return NULL;
476
477         ec = master->base.compositor;
478         device->output =
479                 container_of(ec->output_list.next, struct weston_output, link);
480
481         device->master = master;
482         device->is_mt = 0;
483         device->mtdev = NULL;
484         device->devnode = strdup(path);
485         device->mt.slot = -1;
486         device->rel.dx = 0;
487         device->rel.dy = 0;
488         device->dispatch = NULL;
489
490         /* Use non-blocking mode so that we can loop on read on
491          * evdev_input_device_data() until all events on the fd are
492          * read.  mtdev_get() also expects this. */
493         device->fd = weston_launcher_open(ec, path, O_RDWR | O_NONBLOCK);
494         if (device->fd < 0)
495                 goto err0;
496
497         if (evdev_configure_device(device) == -1)
498                 goto err1;
499
500         /* If the dispatch was not set up use the fallback. */
501         if (device->dispatch == NULL)
502                 device->dispatch = fallback_dispatch_create();
503         if (device->dispatch == NULL)
504                 goto err1;
505
506
507         if (device->is_mt) {
508                 device->mtdev = mtdev_new_open(device->fd);
509                 if (!device->mtdev)
510                         weston_log("mtdev failed to open for %s\n", path);
511         }
512
513         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
514                                               WL_EVENT_READABLE,
515                                               evdev_input_device_data, device);
516         if (device->source == NULL)
517                 goto err2;
518
519         wl_list_insert(master->devices_list.prev, &device->link);
520
521         return device;
522
523 err2:
524         device->dispatch->interface->destroy(device->dispatch);
525 err1:
526         close(device->fd);
527 err0:
528         free(device->devnode);
529         free(device);
530         return NULL;
531 }
532
533 static const char default_seat[] = "seat0";
534
535 static void
536 device_added(struct udev_device *udev_device, struct evdev_seat *master)
537 {
538         struct weston_compositor *c;
539         const char *devnode;
540         const char *device_seat;
541
542         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
543         if (!device_seat)
544                 device_seat = default_seat;
545
546         if (strcmp(device_seat, master->seat_id))
547                 return;
548
549         c = master->base.compositor;
550         devnode = udev_device_get_devnode(udev_device);
551         evdev_input_device_create(master, c->wl_display, devnode);
552 }
553
554 static void
555 device_removed(struct evdev_input_device *device)
556 {
557         struct evdev_dispatch *dispatch;
558
559         dispatch = device->dispatch;
560         if (dispatch)
561                 dispatch->interface->destroy(dispatch);
562
563         wl_event_source_remove(device->source);
564         wl_list_remove(&device->link);
565         if (device->mtdev)
566                 mtdev_close_delete(device->mtdev);
567         close(device->fd);
568         free(device->devnode);
569         free(device);
570 }
571
572 static void
573 evdev_notify_keyboard_focus(struct evdev_seat *seat)
574 {
575         struct evdev_input_device *device;
576         struct wl_array keys;
577         unsigned int i, set;
578         char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
579         uint32_t *k;
580         int ret;
581
582         memset(all_keys, 0, sizeof all_keys);
583         wl_list_for_each(device, &seat->devices_list, link) {
584                 memset(evdev_keys, 0, sizeof evdev_keys);
585                 ret = ioctl(device->fd,
586                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
587                 if (ret < 0) {
588                         weston_log("failed to get keys for device %s\n",
589                                 device->devnode);
590                         continue;
591                 }
592                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
593                         all_keys[i] |= evdev_keys[i];
594         }
595
596         wl_array_init(&keys);
597         for (i = 0; i < KEY_CNT; i++) {
598                 set = all_keys[i >> 3] & (1 << (i & 7));
599                 if (set) {
600                         k = wl_array_add(&keys, sizeof *k);
601                         *k = i;
602                 }
603         }
604
605         notify_keyboard_focus_in(&seat->base.seat, &keys,
606                                  STATE_UPDATE_AUTOMATIC);
607
608         wl_array_release(&keys);
609 }
610
611 void
612 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
613 {
614         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
615         struct udev_enumerate *e;
616         struct udev_list_entry *entry;
617         struct udev_device *device;
618         const char *path, *sysname;
619
620         e = udev_enumerate_new(udev);
621         udev_enumerate_add_match_subsystem(e, "input");
622         udev_enumerate_scan_devices(e);
623         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
624                 path = udev_list_entry_get_name(entry);
625                 device = udev_device_new_from_syspath(udev, path);
626
627                 sysname = udev_device_get_sysname(device);
628                 if (strncmp("event", sysname, 5) != 0) {
629                         udev_device_unref(device);
630                         continue;
631                 }
632
633                 device_added(device, seat);
634
635                 udev_device_unref(device);
636         }
637         udev_enumerate_unref(e);
638
639         evdev_notify_keyboard_focus(seat);
640
641         if (wl_list_empty(&seat->devices_list)) {
642                 weston_log(
643                         "warning: no input devices on entering Weston. "
644                         "Possible causes:\n"
645                         "\t- no permissions to read /dev/input/event*\n"
646                         "\t- seats misconfigured "
647                         "(Weston backend option 'seat', "
648                         "udev device property ID_SEAT)\n");
649         }
650 }
651
652 static int
653 evdev_udev_handler(int fd, uint32_t mask, void *data)
654 {
655         struct evdev_seat *master = data;
656         struct udev_device *udev_device;
657         struct evdev_input_device *device, *next;
658         const char *action;
659         const char *devnode;
660
661         udev_device = udev_monitor_receive_device(master->udev_monitor);
662         if (!udev_device)
663                 return 1;
664
665         action = udev_device_get_action(udev_device);
666         if (action) {
667                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
668                         return 0;
669
670                 if (!strcmp(action, "add")) {
671                         device_added(udev_device, master);
672                 }
673                 else if (!strcmp(action, "remove")) {
674                         devnode = udev_device_get_devnode(udev_device);
675                         wl_list_for_each_safe(device, next,
676                                               &master->devices_list, link)
677                                 if (!strcmp(device->devnode, devnode)) {
678                                         device_removed(device);
679                                         break;
680                                 }
681                 }
682         }
683         udev_device_unref(udev_device);
684
685         return 0;
686 }
687
688 int
689 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
690 {
691         struct evdev_seat *master = (struct evdev_seat *) seat_base;
692         struct wl_event_loop *loop;
693         struct weston_compositor *c = master->base.compositor;
694         int fd;
695
696         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
697         if (!master->udev_monitor) {
698                 weston_log("udev: failed to create the udev monitor\n");
699                 return 0;
700         }
701
702         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
703                         "input", NULL);
704
705         if (udev_monitor_enable_receiving(master->udev_monitor)) {
706                 weston_log("udev: failed to bind the udev monitor\n");
707                 udev_monitor_unref(master->udev_monitor);
708                 return 0;
709         }
710
711         loop = wl_display_get_event_loop(c->wl_display);
712         fd = udev_monitor_get_fd(master->udev_monitor);
713         master->udev_monitor_source =
714                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
715                                      evdev_udev_handler, master);
716         if (!master->udev_monitor_source) {
717                 udev_monitor_unref(master->udev_monitor);
718                 return 0;
719         }
720
721         return 1;
722 }
723
724 void
725 evdev_disable_udev_monitor(struct weston_seat *seat_base)
726 {
727         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
728
729         if (!seat->udev_monitor)
730                 return;
731
732         udev_monitor_unref(seat->udev_monitor);
733         seat->udev_monitor = NULL;
734         wl_event_source_remove(seat->udev_monitor_source);
735         seat->udev_monitor_source = NULL;
736 }
737
738 void
739 evdev_input_create(struct weston_compositor *c, struct udev *udev,
740                    const char *seat_id)
741 {
742         struct evdev_seat *seat;
743
744         seat = malloc(sizeof *seat);
745         if (seat == NULL)
746                 return;
747
748         memset(seat, 0, sizeof *seat);
749         weston_seat_init(&seat->base, c);
750         seat->base.led_update = evdev_led_update;
751
752         wl_list_init(&seat->devices_list);
753         seat->seat_id = strdup(seat_id);
754         if (!evdev_enable_udev_monitor(udev, &seat->base)) {
755                 free(seat->seat_id);
756                 free(seat);
757                 return;
758         }
759
760         evdev_add_devices(udev, &seat->base);
761
762         c->seat = &seat->base;
763 }
764
765 void
766 evdev_remove_devices(struct weston_seat *seat_base)
767 {
768         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
769         struct evdev_input_device *device, *next;
770
771         wl_list_for_each_safe(device, next, &seat->devices_list, link)
772                 device_removed(device);
773
774         notify_keyboard_focus_out(&seat->base.seat);
775 }
776
777 void
778 evdev_input_destroy(struct weston_seat *seat_base)
779 {
780         struct evdev_seat *seat = (struct evdev_seat *) seat_base;
781
782         evdev_remove_devices(seat_base);
783         evdev_disable_udev_monitor(&seat->base);
784
785         weston_seat_release(seat_base);
786         free(seat->seat_id);
787         free(seat);
788 }