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