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