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