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