evdev: rename evdev_seat to drm_seat
[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 "launcher-util.h"
33
34 static void
35 evdev_led_update(struct wl_list *evdev_devices, enum weston_led leds)
36 {
37         static const struct {
38                 enum weston_led weston;
39                 int evdev;
40         } map[] = {
41                 { LED_NUM_LOCK, LED_NUML },
42                 { LED_CAPS_LOCK, LED_CAPSL },
43                 { LED_SCROLL_LOCK, LED_SCROLLL },
44         };
45         struct evdev_input_device *device;
46         struct input_event ev[ARRAY_LENGTH(map)];
47         unsigned int i;
48
49         memset(ev, 0, sizeof(ev));
50         for (i = 0; i < ARRAY_LENGTH(map); i++) {
51                 ev[i].type = EV_LED;
52                 ev[i].code = map[i].evdev;
53                 ev[i].value = !!(leds & map[i].weston);
54         }
55
56         wl_list_for_each(device, evdev_devices, link) {
57                 if (device->caps & EVDEV_KEYBOARD)
58                         write(device->fd, ev, sizeof ev);
59         }
60 }
61
62 static void
63 drm_led_update(struct weston_seat *seat_base, enum weston_led leds)
64 {
65         struct drm_seat *seat = (struct drm_seat *) seat_base;
66
67         evdev_led_update(&seat->devices_list, leds);
68 }
69
70 static inline void
71 evdev_process_key(struct evdev_input_device *device,
72                         struct input_event *e, int time)
73 {
74         if (e->value == 2)
75                 return;
76
77         switch (e->code) {
78         case BTN_LEFT:
79         case BTN_RIGHT:
80         case BTN_MIDDLE:
81         case BTN_SIDE:
82         case BTN_EXTRA:
83         case BTN_FORWARD:
84         case BTN_BACK:
85         case BTN_TASK:
86                 notify_button(&device->seat->seat,
87                               time, e->code,
88                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
89                                          WL_POINTER_BUTTON_STATE_RELEASED);
90                 break;
91
92         default:
93                 notify_key(&device->seat->seat,
94                            time, e->code,
95                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
96                                       WL_KEYBOARD_KEY_STATE_RELEASED,
97                            STATE_UPDATE_AUTOMATIC);
98                 break;
99         }
100 }
101
102 static void
103 evdev_process_touch(struct evdev_input_device *device,
104                     struct input_event *e)
105 {
106         const int screen_width = device->output->current->width;
107         const int screen_height = device->output->current->height;
108
109         switch (e->code) {
110         case ABS_MT_SLOT:
111                 device->mt.slot = e->value;
112                 break;
113         case ABS_MT_TRACKING_ID:
114                 if (e->value >= 0)
115                         device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
116                 else
117                         device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
118                 break;
119         case ABS_MT_POSITION_X:
120                 device->mt.x[device->mt.slot] =
121                         (e->value - device->abs.min_x) * screen_width /
122                         (device->abs.max_x - device->abs.min_x) +
123                         device->output->x;
124                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
125                 break;
126         case ABS_MT_POSITION_Y:
127                 device->mt.y[device->mt.slot] =
128                         (e->value - device->abs.min_y) * screen_height /
129                         (device->abs.max_y - device->abs.min_y) +
130                         device->output->y;
131                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
132                 break;
133         }
134 }
135
136 static inline void
137 evdev_process_absolute_motion(struct evdev_input_device *device,
138                               struct input_event *e)
139 {
140         const int screen_width = device->output->current->width;
141         const int screen_height = device->output->current->height;
142
143         switch (e->code) {
144         case ABS_X:
145                 device->abs.x =
146                         (e->value - device->abs.min_x) * screen_width /
147                         (device->abs.max_x - device->abs.min_x) +
148                         device->output->x;
149                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
150                 break;
151         case ABS_Y:
152                 device->abs.y =
153                         (e->value - device->abs.min_y) * screen_height /
154                         (device->abs.max_y - device->abs.min_y) +
155                         device->output->y;
156                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
157                 break;
158         }
159 }
160
161 static inline void
162 evdev_process_relative(struct evdev_input_device *device,
163                        struct input_event *e, uint32_t time)
164 {
165         switch (e->code) {
166         case REL_X:
167                 device->rel.dx += wl_fixed_from_int(e->value);
168                 device->pending_events |= EVDEV_RELATIVE_MOTION;
169                 break;
170         case REL_Y:
171                 device->rel.dy += wl_fixed_from_int(e->value);
172                 device->pending_events |= EVDEV_RELATIVE_MOTION;
173                 break;
174         case REL_WHEEL:
175                 notify_axis(&device->seat->seat,
176                               time,
177                               WL_POINTER_AXIS_VERTICAL_SCROLL,
178                               wl_fixed_from_int(e->value));
179                 break;
180         case REL_HWHEEL:
181                 notify_axis(&device->seat->seat,
182                               time,
183                               WL_POINTER_AXIS_HORIZONTAL_SCROLL,
184                               wl_fixed_from_int(e->value));
185                 break;
186         }
187 }
188
189 static inline void
190 evdev_process_absolute(struct evdev_input_device *device,
191                        struct input_event *e)
192 {
193         if (device->is_mt) {
194                 evdev_process_touch(device, e);
195         } else {
196                 evdev_process_absolute_motion(device, e);
197         }
198 }
199
200 static int
201 is_motion_event(struct input_event *e)
202 {
203         switch (e->type) {
204         case EV_REL:
205                 switch (e->code) {
206                 case REL_X:
207                 case REL_Y:
208                         return 1;
209                 }
210         case EV_ABS:
211                 switch (e->code) {
212                 case ABS_X:
213                 case ABS_Y:
214                 case ABS_MT_POSITION_X:
215                 case ABS_MT_POSITION_Y:
216                         return 1;
217                 }
218         }
219
220         return 0;
221 }
222
223 static void
224 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
225 {
226         struct weston_seat *master = device->seat;
227
228         if (!device->pending_events)
229                 return;
230
231         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
232                 notify_motion(&master->seat, time,
233                               master->seat.pointer->x + device->rel.dx,
234                               master->seat.pointer->y + device->rel.dy);
235                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
236                 device->rel.dx = 0;
237                 device->rel.dy = 0;
238         }
239         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
240                 notify_touch(&master->seat, time,
241                              device->mt.slot,
242                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
243                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
244                              WL_TOUCH_DOWN);
245                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
246                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
247         }
248         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
249                 notify_touch(&master->seat, time,
250                              device->mt.slot,
251                              wl_fixed_from_int(device->mt.x[device->mt.slot]),
252                              wl_fixed_from_int(device->mt.y[device->mt.slot]),
253                              WL_TOUCH_MOTION);
254                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
255                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
256         }
257         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
258                 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
259                              WL_TOUCH_UP);
260                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
261         }
262         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
263                 notify_motion(&master->seat, time,
264                               wl_fixed_from_int(device->abs.x),
265                               wl_fixed_from_int(device->abs.y));
266                 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
267         }
268 }
269
270 static void
271 fallback_process(struct evdev_dispatch *dispatch,
272                  struct evdev_input_device *device,
273                  struct input_event *event,
274                  uint32_t time)
275 {
276         switch (event->type) {
277         case EV_REL:
278                 evdev_process_relative(device, event, time);
279                 break;
280         case EV_ABS:
281                 evdev_process_absolute(device, event);
282                 break;
283         case EV_KEY:
284                 evdev_process_key(device, event, time);
285                 break;
286         }
287 }
288
289 static void
290 fallback_destroy(struct evdev_dispatch *dispatch)
291 {
292         free(dispatch);
293 }
294
295 struct evdev_dispatch_interface fallback_interface = {
296         fallback_process,
297         fallback_destroy
298 };
299
300 static struct evdev_dispatch *
301 fallback_dispatch_create(void)
302 {
303         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
304         if (dispatch == NULL)
305                 return NULL;
306
307         dispatch->interface = &fallback_interface;
308
309         return dispatch;
310 }
311
312 static void
313 evdev_process_events(struct evdev_input_device *device,
314                      struct input_event *ev, int count)
315 {
316         struct evdev_dispatch *dispatch = device->dispatch;
317         struct input_event *e, *end;
318         uint32_t time = 0;
319
320         device->pending_events = 0;
321
322         e = ev;
323         end = e + count;
324         for (e = ev; e < end; e++) {
325                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
326
327                 /* we try to minimize the amount of notifications to be
328                  * forwarded to the compositor, so we accumulate motion
329                  * events and send as a bunch */
330                 if (!is_motion_event(e))
331                         evdev_flush_motion(device, time);
332
333                 dispatch->interface->process(dispatch, device, e, time);
334         }
335
336         evdev_flush_motion(device, time);
337 }
338
339 static int
340 evdev_input_device_data(int fd, uint32_t mask, void *data)
341 {
342         struct weston_compositor *ec;
343         struct evdev_input_device *device = data;
344         struct input_event ev[32];
345         int len;
346
347         ec = device->seat->compositor;
348         if (!ec->focus)
349                 return 1;
350
351         /* If the compositor is repainting, this function is called only once
352          * per frame and we have to process all the events available on the
353          * fd, otherwise there will be input lag. */
354         do {
355                 if (device->mtdev)
356                         len = mtdev_get(device->mtdev, fd, ev,
357                                         ARRAY_LENGTH(ev)) *
358                                 sizeof (struct input_event);
359                 else
360                         len = read(fd, &ev, sizeof ev);
361
362                 if (len < 0 || len % sizeof ev[0] != 0) {
363                         /* FIXME: call evdev_input_device_destroy when errno is ENODEV. */
364                         return 1;
365                 }
366
367                 evdev_process_events(device, ev, len / sizeof ev[0]);
368
369         } while (len > 0);
370
371         return 1;
372 }
373
374 static int
375 evdev_configure_device(struct evdev_input_device *device)
376 {
377         struct input_absinfo absinfo;
378         unsigned long ev_bits[NBITS(EV_MAX)];
379         unsigned long abs_bits[NBITS(ABS_MAX)];
380         unsigned long rel_bits[NBITS(REL_MAX)];
381         unsigned long key_bits[NBITS(KEY_MAX)];
382         int has_key, has_abs;
383         unsigned int i;
384
385         has_key = 0;
386         has_abs = 0;
387         device->caps = 0;
388
389         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
390         if (TEST_BIT(ev_bits, EV_ABS)) {
391                 has_abs = 1;
392
393                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
394                       abs_bits);
395                 if (TEST_BIT(abs_bits, ABS_X)) {
396                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
397                         device->abs.min_x = absinfo.minimum;
398                         device->abs.max_x = absinfo.maximum;
399                         device->caps |= EVDEV_MOTION_ABS;
400                 }
401                 if (TEST_BIT(abs_bits, ABS_Y)) {
402                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
403                         device->abs.min_y = absinfo.minimum;
404                         device->abs.max_y = absinfo.maximum;
405                         device->caps |= EVDEV_MOTION_ABS;
406                 }
407                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
408                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
409                               &absinfo);
410                         device->abs.min_x = absinfo.minimum;
411                         device->abs.max_x = absinfo.maximum;
412                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
413                               &absinfo);
414                         device->abs.min_y = absinfo.minimum;
415                         device->abs.max_y = absinfo.maximum;
416                         device->is_mt = 1;
417                         device->mt.slot = 0;
418                         device->caps |= EVDEV_TOUCH;
419                 }
420         }
421         if (TEST_BIT(ev_bits, EV_REL)) {
422                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
423                       rel_bits);
424                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
425                         device->caps |= EVDEV_MOTION_REL;
426         }
427         if (TEST_BIT(ev_bits, EV_KEY)) {
428                 has_key = 1;
429                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
430                       key_bits);
431                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
432                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
433                     has_abs)
434                         device->dispatch = evdev_touchpad_create(device);
435                 for (i = KEY_ESC; i < KEY_MAX; i++) {
436                         if (i >= BTN_MISC && i < KEY_OK)
437                                 continue;
438                         if (TEST_BIT(key_bits, i)) {
439                                 device->caps |= EVDEV_KEYBOARD;
440                                 break;
441                         }
442                 }
443                 for (i = BTN_MISC; i < KEY_OK; i++) {
444                         if (TEST_BIT(key_bits, i)) {
445                                 device->caps |= EVDEV_BUTTON;
446                                 break;
447                         }
448                 }
449         }
450         if (TEST_BIT(ev_bits, EV_LED)) {
451                 device->caps |= EVDEV_KEYBOARD;
452         }
453
454         /* This rule tries to catch accelerometer devices and opt out. We may
455          * want to adjust the protocol later adding a proper event for dealing
456          * with accelerometers and implement here accordingly */
457         if (has_abs && !has_key && !device->is_mt)
458                 return -1;
459
460         if ((device->caps &
461              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
462                 weston_seat_init_pointer(device->seat);
463         if ((device->caps & EVDEV_KEYBOARD))
464                 weston_seat_init_keyboard(device->seat, NULL);
465         if ((device->caps & EVDEV_TOUCH))
466                 weston_seat_init_touch(device->seat);
467
468         return 0;
469 }
470
471 static struct evdev_input_device *
472 evdev_input_device_create(struct weston_seat *seat,
473                           const char *path, int device_fd)
474 {
475         struct evdev_input_device *device;
476         struct weston_compositor *ec;
477
478         device = malloc(sizeof *device);
479         if (device == NULL)
480                 return NULL;
481         memset(device, 0, sizeof *device);
482
483         ec = seat->compositor;
484         device->output =
485                 container_of(ec->output_list.next, struct weston_output, link);
486
487         device->seat = seat;
488         device->is_mt = 0;
489         device->mtdev = NULL;
490         device->devnode = strdup(path);
491         device->mt.slot = -1;
492         device->rel.dx = 0;
493         device->rel.dy = 0;
494         device->dispatch = NULL;
495         device->fd = device_fd;
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         return device;
520
521 err2:
522         device->dispatch->interface->destroy(device->dispatch);
523 err1:
524         free(device->devnode);
525         free(device);
526         return NULL;
527 }
528
529 static void
530 evdev_input_device_destroy(struct evdev_input_device *device)
531 {
532         struct evdev_dispatch *dispatch;
533
534         dispatch = device->dispatch;
535         if (dispatch)
536                 dispatch->interface->destroy(dispatch);
537
538         wl_event_source_remove(device->source);
539         wl_list_remove(&device->link);
540         if (device->mtdev)
541                 mtdev_close_delete(device->mtdev);
542         close(device->fd);
543         free(device->devnode);
544         free(device);
545 }
546
547 static const char default_seat[] = "seat0";
548
549 static void
550 device_added(struct udev_device *udev_device, struct drm_seat *master)
551 {
552         struct weston_compositor *c;
553         struct evdev_input_device *device;
554         const char *devnode;
555         const char *device_seat;
556         int fd;
557
558         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
559         if (!device_seat)
560                 device_seat = default_seat;
561
562         if (strcmp(device_seat, master->seat_id))
563                 return;
564
565         c = master->base.compositor;
566         devnode = udev_device_get_devnode(udev_device);
567
568         /* Use non-blocking mode so that we can loop on read on
569          * evdev_input_device_data() until all events on the fd are
570          * read.  mtdev_get() also expects this. */
571         fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK);
572         if (fd < 0) {
573                 weston_log("opening input device '%s' failed.\n", devnode);
574                 return;
575         }
576
577         device = evdev_input_device_create(&master->base, devnode, fd);
578         if (!device) {
579                 close(fd);
580                 weston_log("not using input device '%s'.\n", devnode);
581                 return;
582         }
583
584         wl_list_insert(master->devices_list.prev, &device->link);
585 }
586
587 static void
588 evdev_notify_keyboard_focus(struct weston_seat *seat,
589                             struct wl_list *evdev_devices)
590 {
591         struct evdev_input_device *device;
592         struct wl_array keys;
593         unsigned int i, set;
594         char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
595         uint32_t *k;
596         int ret;
597
598         memset(all_keys, 0, sizeof all_keys);
599         wl_list_for_each(device, evdev_devices, link) {
600                 memset(evdev_keys, 0, sizeof evdev_keys);
601                 ret = ioctl(device->fd,
602                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
603                 if (ret < 0) {
604                         weston_log("failed to get keys for device %s\n",
605                                 device->devnode);
606                         continue;
607                 }
608                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
609                         all_keys[i] |= evdev_keys[i];
610         }
611
612         wl_array_init(&keys);
613         for (i = 0; i < KEY_CNT; i++) {
614                 set = all_keys[i >> 3] & (1 << (i & 7));
615                 if (set) {
616                         k = wl_array_add(&keys, sizeof *k);
617                         *k = i;
618                 }
619         }
620
621         notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
622
623         wl_array_release(&keys);
624 }
625
626 void
627 evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
628 {
629         struct drm_seat *seat = (struct drm_seat *) seat_base;
630         struct udev_enumerate *e;
631         struct udev_list_entry *entry;
632         struct udev_device *device;
633         const char *path, *sysname;
634
635         e = udev_enumerate_new(udev);
636         udev_enumerate_add_match_subsystem(e, "input");
637         udev_enumerate_scan_devices(e);
638         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
639                 path = udev_list_entry_get_name(entry);
640                 device = udev_device_new_from_syspath(udev, path);
641
642                 sysname = udev_device_get_sysname(device);
643                 if (strncmp("event", sysname, 5) != 0) {
644                         udev_device_unref(device);
645                         continue;
646                 }
647
648                 device_added(device, seat);
649
650                 udev_device_unref(device);
651         }
652         udev_enumerate_unref(e);
653
654         evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
655
656         if (wl_list_empty(&seat->devices_list)) {
657                 weston_log(
658                         "warning: no input devices on entering Weston. "
659                         "Possible causes:\n"
660                         "\t- no permissions to read /dev/input/event*\n"
661                         "\t- seats misconfigured "
662                         "(Weston backend option 'seat', "
663                         "udev device property ID_SEAT)\n");
664         }
665 }
666
667 static int
668 evdev_udev_handler(int fd, uint32_t mask, void *data)
669 {
670         struct drm_seat *master = data;
671         struct udev_device *udev_device;
672         struct evdev_input_device *device, *next;
673         const char *action;
674         const char *devnode;
675
676         udev_device = udev_monitor_receive_device(master->udev_monitor);
677         if (!udev_device)
678                 return 1;
679
680         action = udev_device_get_action(udev_device);
681         if (action) {
682                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
683                         return 0;
684
685                 if (!strcmp(action, "add")) {
686                         device_added(udev_device, master);
687                 }
688                 else if (!strcmp(action, "remove")) {
689                         devnode = udev_device_get_devnode(udev_device);
690                         wl_list_for_each_safe(device, next,
691                                               &master->devices_list, link)
692                                 if (!strcmp(device->devnode, devnode)) {
693                                         evdev_input_device_destroy(device);
694                                         break;
695                                 }
696                 }
697         }
698         udev_device_unref(udev_device);
699
700         return 0;
701 }
702
703 int
704 evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
705 {
706         struct drm_seat *master = (struct drm_seat *) seat_base;
707         struct wl_event_loop *loop;
708         struct weston_compositor *c = master->base.compositor;
709         int fd;
710
711         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
712         if (!master->udev_monitor) {
713                 weston_log("udev: failed to create the udev monitor\n");
714                 return 0;
715         }
716
717         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
718                         "input", NULL);
719
720         if (udev_monitor_enable_receiving(master->udev_monitor)) {
721                 weston_log("udev: failed to bind the udev monitor\n");
722                 udev_monitor_unref(master->udev_monitor);
723                 return 0;
724         }
725
726         loop = wl_display_get_event_loop(c->wl_display);
727         fd = udev_monitor_get_fd(master->udev_monitor);
728         master->udev_monitor_source =
729                 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
730                                      evdev_udev_handler, master);
731         if (!master->udev_monitor_source) {
732                 udev_monitor_unref(master->udev_monitor);
733                 return 0;
734         }
735
736         return 1;
737 }
738
739 void
740 evdev_disable_udev_monitor(struct weston_seat *seat_base)
741 {
742         struct drm_seat *seat = (struct drm_seat *) seat_base;
743
744         if (!seat->udev_monitor)
745                 return;
746
747         udev_monitor_unref(seat->udev_monitor);
748         seat->udev_monitor = NULL;
749         wl_event_source_remove(seat->udev_monitor_source);
750         seat->udev_monitor_source = NULL;
751 }
752
753 void
754 evdev_input_create(struct weston_compositor *c, struct udev *udev,
755                    const char *seat_id)
756 {
757         struct drm_seat *seat;
758
759         seat = malloc(sizeof *seat);
760         if (seat == NULL)
761                 return;
762
763         memset(seat, 0, sizeof *seat);
764         weston_seat_init(&seat->base, c);
765         seat->base.led_update = drm_led_update;
766
767         wl_list_init(&seat->devices_list);
768         seat->seat_id = strdup(seat_id);
769         if (!evdev_enable_udev_monitor(udev, &seat->base)) {
770                 free(seat->seat_id);
771                 free(seat);
772                 return;
773         }
774
775         evdev_add_devices(udev, &seat->base);
776
777         c->seat = &seat->base;
778 }
779
780 void
781 evdev_remove_devices(struct weston_seat *seat_base)
782 {
783         struct drm_seat *seat = (struct drm_seat *) seat_base;
784         struct evdev_input_device *device, *next;
785
786         wl_list_for_each_safe(device, next, &seat->devices_list, link)
787                 evdev_input_device_destroy(device);
788
789         notify_keyboard_focus_out(&seat->base.seat);
790 }
791
792 void
793 evdev_input_destroy(struct weston_seat *seat_base)
794 {
795         struct drm_seat *seat = (struct drm_seat *) seat_base;
796
797         evdev_remove_devices(seat_base);
798         evdev_disable_udev_monitor(&seat->base);
799
800         weston_seat_release(seat_base);
801         free(seat->seat_id);
802         free(seat);
803 }