compositor-drm: Work around page flip not setting tiling mode on BYT
[platform/upstream/weston.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <linux/input.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <mtdev.h>
32 #include <assert.h>
33
34 #include "compositor.h"
35 #include "evdev.h"
36
37 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
38
39 void
40 evdev_led_update(struct evdev_device *device, enum weston_led leds)
41 {
42         static const struct {
43                 enum weston_led weston;
44                 int evdev;
45         } map[] = {
46                 { LED_NUM_LOCK, LED_NUML },
47                 { LED_CAPS_LOCK, LED_CAPSL },
48                 { LED_SCROLL_LOCK, LED_SCROLLL },
49         };
50         struct input_event ev[ARRAY_LENGTH(map) + 1];
51         unsigned int i;
52
53         if (!(device->seat_caps & EVDEV_SEAT_KEYBOARD))
54                 return;
55
56         memset(ev, 0, sizeof(ev));
57         for (i = 0; i < ARRAY_LENGTH(map); i++) {
58                 ev[i].type = EV_LED;
59                 ev[i].code = map[i].evdev;
60                 ev[i].value = !!(leds & map[i].weston);
61         }
62         ev[i].type = EV_SYN;
63         ev[i].code = SYN_REPORT;
64
65         i = write(device->fd, ev, sizeof ev);
66         (void)i; /* no, we really don't care about the return value */
67 }
68
69 static void
70 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
71 {
72        if (!device->abs.apply_calibration) {
73                *x = device->abs.x;
74                *y = device->abs.y;
75                return;
76        } else {
77                *x = device->abs.x * device->abs.calibration[0] +
78                        device->abs.y * device->abs.calibration[1] +
79                        device->abs.calibration[2];
80
81                *y = device->abs.x * device->abs.calibration[3] +
82                        device->abs.y * device->abs.calibration[4] +
83                        device->abs.calibration[5];
84        }
85 }
86
87 static void
88 evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
89 {
90         struct weston_seat *master = device->seat;
91         wl_fixed_t x, y;
92         int32_t cx, cy;
93         int slot, seat_slot;
94
95         slot = device->mt.slot;
96         switch (device->pending_event) {
97         case EVDEV_NONE:
98                 return;
99         case EVDEV_RELATIVE_MOTION:
100                 notify_motion(master, time, device->rel.dx, device->rel.dy);
101                 device->rel.dx = 0;
102                 device->rel.dy = 0;
103                 break;
104         case EVDEV_ABSOLUTE_MT_DOWN:
105                 if (device->output == NULL)
106                         break;
107                 weston_output_transform_coordinate(device->output,
108                                                    wl_fixed_from_int(device->mt.slots[slot].x),
109                                                    wl_fixed_from_int(device->mt.slots[slot].y),
110                                                    &x, &y);
111                 seat_slot = ffs(~master->slot_map) - 1;
112                 device->mt.slots[slot].seat_slot = seat_slot;
113                 master->slot_map |= 1 << seat_slot;
114
115                 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
116                 break;
117         case EVDEV_ABSOLUTE_MT_MOTION:
118                 if (device->output == NULL)
119                         break;
120                 weston_output_transform_coordinate(device->output,
121                                                    wl_fixed_from_int(device->mt.slots[slot].x),
122                                                    wl_fixed_from_int(device->mt.slots[slot].y),
123                                                    &x, &y);
124                 seat_slot = device->mt.slots[slot].seat_slot;
125                 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_MOTION);
126                 break;
127         case EVDEV_ABSOLUTE_MT_UP:
128                 seat_slot = device->mt.slots[slot].seat_slot;
129                 master->slot_map &= ~(1 << seat_slot);
130                 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
131                 break;
132         case EVDEV_ABSOLUTE_TOUCH_DOWN:
133                 if (device->output == NULL)
134                         break;
135                 transform_absolute(device, &cx, &cy);
136                 weston_output_transform_coordinate(device->output,
137                                                    wl_fixed_from_int(cx),
138                                                    wl_fixed_from_int(cy),
139                                                    &x, &y);
140                 seat_slot = ffs(~master->slot_map) - 1;
141                 device->abs.seat_slot = seat_slot;
142                 master->slot_map |= 1 << seat_slot;
143                 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
144                 break;
145         case EVDEV_ABSOLUTE_MOTION:
146                 if (device->output == NULL)
147                         break;
148                 transform_absolute(device, &cx, &cy);
149                 weston_output_transform_coordinate(device->output,
150                                                    wl_fixed_from_int(cx),
151                                                    wl_fixed_from_int(cy),
152                                                    &x, &y);
153
154                 if (device->seat_caps & EVDEV_SEAT_TOUCH)
155                         notify_touch(master, time, device->abs.seat_slot,
156                                      x, y, WL_TOUCH_MOTION);
157                 else if (device->seat_caps & EVDEV_SEAT_POINTER)
158                         notify_motion_absolute(master, time, x, y);
159                 break;
160         case EVDEV_ABSOLUTE_TOUCH_UP:
161                 seat_slot = device->abs.seat_slot;
162                 master->slot_map &= ~(1 << seat_slot);
163                 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
164                 break;
165         default:
166                 assert(0 && "Unknown pending event type");
167         }
168
169         device->pending_event = EVDEV_NONE;
170 }
171
172 static void
173 evdev_process_touch_button(struct evdev_device *device, int time, int value)
174 {
175         if (device->pending_event != EVDEV_NONE &&
176             device->pending_event != EVDEV_ABSOLUTE_MOTION)
177                 evdev_flush_pending_event(device, time);
178
179         device->pending_event = (value ?
180                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
181                                  EVDEV_ABSOLUTE_TOUCH_UP);
182 }
183
184 static inline void
185 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
186 {
187         /* ignore kernel key repeat */
188         if (e->value == 2)
189                 return;
190
191         if (e->code == BTN_TOUCH) {
192                 if (!device->is_mt)
193                         evdev_process_touch_button(device, time, e->value);
194                 return;
195         }
196
197         evdev_flush_pending_event(device, time);
198
199         switch (e->code) {
200         case BTN_LEFT:
201         case BTN_RIGHT:
202         case BTN_MIDDLE:
203         case BTN_SIDE:
204         case BTN_EXTRA:
205         case BTN_FORWARD:
206         case BTN_BACK:
207         case BTN_TASK:
208                 notify_button(device->seat,
209                               time, e->code,
210                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
211                                          WL_POINTER_BUTTON_STATE_RELEASED);
212                 break;
213
214         default:
215                 notify_key(device->seat,
216                            time, e->code,
217                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
218                                       WL_KEYBOARD_KEY_STATE_RELEASED,
219                            STATE_UPDATE_AUTOMATIC);
220                 break;
221         }
222 }
223
224 static void
225 evdev_process_touch(struct evdev_device *device,
226                     struct input_event *e,
227                     uint32_t time)
228 {
229         int screen_width, screen_height;
230
231         if (device->output == NULL)
232                 return;
233
234         screen_width = device->output->current_mode->width;
235         screen_height = device->output->current_mode->height;
236
237         switch (e->code) {
238         case ABS_MT_SLOT:
239                 evdev_flush_pending_event(device, time);
240                 device->mt.slot = e->value;
241                 break;
242         case ABS_MT_TRACKING_ID:
243                 if (device->pending_event != EVDEV_NONE &&
244                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
245                         evdev_flush_pending_event(device, time);
246                 if (e->value >= 0)
247                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
248                 else
249                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
250                 break;
251         case ABS_MT_POSITION_X:
252                 device->mt.slots[device->mt.slot].x =
253                         (e->value - device->abs.min_x) * screen_width /
254                         (device->abs.max_x - device->abs.min_x);
255                 if (device->pending_event == EVDEV_NONE)
256                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
257                 break;
258         case ABS_MT_POSITION_Y:
259                 device->mt.slots[device->mt.slot].y =
260                         (e->value - device->abs.min_y) * screen_height /
261                         (device->abs.max_y - device->abs.min_y);
262                 if (device->pending_event == EVDEV_NONE)
263                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
264                 break;
265         }
266 }
267
268 static inline void
269 evdev_process_absolute_motion(struct evdev_device *device,
270                               struct input_event *e)
271 {
272         int screen_width, screen_height;
273
274         if (device->output == NULL)
275                 return;
276
277         screen_width = device->output->current_mode->width;
278         screen_height = device->output->current_mode->height;
279
280         switch (e->code) {
281         case ABS_X:
282                 device->abs.x =
283                         (e->value - device->abs.min_x) * screen_width /
284                         (device->abs.max_x - device->abs.min_x);
285                 if (device->pending_event == EVDEV_NONE)
286                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
287                 break;
288         case ABS_Y:
289                 device->abs.y =
290                         (e->value - device->abs.min_y) * screen_height /
291                         (device->abs.max_y - device->abs.min_y);
292                 if (device->pending_event == EVDEV_NONE)
293                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
294                 break;
295         }
296 }
297
298 static inline void
299 evdev_process_relative(struct evdev_device *device,
300                        struct input_event *e, uint32_t time)
301 {
302         switch (e->code) {
303         case REL_X:
304                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
305                         evdev_flush_pending_event(device, time);
306                 device->rel.dx += wl_fixed_from_int(e->value);
307                 device->pending_event = EVDEV_RELATIVE_MOTION;
308                 break;
309         case REL_Y:
310                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
311                         evdev_flush_pending_event(device, time);
312                 device->rel.dy += wl_fixed_from_int(e->value);
313                 device->pending_event = EVDEV_RELATIVE_MOTION;
314                 break;
315         case REL_WHEEL:
316                 evdev_flush_pending_event(device, time);
317                 switch (e->value) {
318                 case -1:
319                         /* Scroll down */
320                 case 1:
321                         /* Scroll up */
322                         notify_axis(device->seat,
323                                     time,
324                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
325                                     -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
326                         break;
327                 default:
328                         break;
329                 }
330                 break;
331         case REL_HWHEEL:
332                 evdev_flush_pending_event(device, time);
333                 switch (e->value) {
334                 case -1:
335                         /* Scroll left */
336                 case 1:
337                         /* Scroll right */
338                         notify_axis(device->seat,
339                                     time,
340                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
341                                     e->value * DEFAULT_AXIS_STEP_DISTANCE);
342                         break;
343                 default:
344                         break;
345
346                 }
347         }
348 }
349
350 static inline void
351 evdev_process_absolute(struct evdev_device *device,
352                        struct input_event *e,
353                        uint32_t time)
354 {
355         if (device->is_mt) {
356                 evdev_process_touch(device, e, time);
357         } else {
358                 evdev_process_absolute_motion(device, e);
359         }
360 }
361
362 static void
363 fallback_process(struct evdev_dispatch *dispatch,
364                  struct evdev_device *device,
365                  struct input_event *event,
366                  uint32_t time)
367 {
368         switch (event->type) {
369         case EV_REL:
370                 evdev_process_relative(device, event, time);
371                 break;
372         case EV_ABS:
373                 evdev_process_absolute(device, event, time);
374                 break;
375         case EV_KEY:
376                 evdev_process_key(device, event, time);
377                 break;
378         case EV_SYN:
379                 evdev_flush_pending_event(device, time);
380                 break;
381         }
382 }
383
384 static void
385 fallback_destroy(struct evdev_dispatch *dispatch)
386 {
387         free(dispatch);
388 }
389
390 struct evdev_dispatch_interface fallback_interface = {
391         fallback_process,
392         fallback_destroy
393 };
394
395 static struct evdev_dispatch *
396 fallback_dispatch_create(void)
397 {
398         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
399         if (dispatch == NULL)
400                 return NULL;
401
402         dispatch->interface = &fallback_interface;
403
404         return dispatch;
405 }
406
407 static void
408 evdev_process_events(struct evdev_device *device,
409                      struct input_event *ev, int count)
410 {
411         struct evdev_dispatch *dispatch = device->dispatch;
412         struct input_event *e, *end;
413         uint32_t time = 0;
414
415         e = ev;
416         end = e + count;
417         for (e = ev; e < end; e++) {
418                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
419
420                 dispatch->interface->process(dispatch, device, e, time);
421         }
422 }
423
424 static int
425 evdev_device_data(int fd, uint32_t mask, void *data)
426 {
427         struct weston_compositor *ec;
428         struct evdev_device *device = data;
429         struct input_event ev[32];
430         int len;
431
432         ec = device->seat->compositor;
433         if (!ec->session_active)
434                 return 1;
435
436         /* If the compositor is repainting, this function is called only once
437          * per frame and we have to process all the events available on the
438          * fd, otherwise there will be input lag. */
439         do {
440                 if (device->mtdev)
441                         len = mtdev_get(device->mtdev, fd, ev,
442                                         ARRAY_LENGTH(ev)) *
443                                 sizeof (struct input_event);
444                 else
445                         len = read(fd, &ev, sizeof ev);
446
447                 if (len < 0 || len % sizeof ev[0] != 0) {
448                         if (len < 0 && errno != EAGAIN && errno != EINTR) {
449                                 weston_log("device %s died\n",
450                                            device->devnode);
451                                 wl_event_source_remove(device->source);
452                                 device->source = NULL;
453                         }
454
455                         return 1;
456                 }
457
458                 evdev_process_events(device, ev, len / sizeof ev[0]);
459
460         } while (len > 0);
461
462         return 1;
463 }
464
465 static int
466 evdev_configure_device(struct evdev_device *device)
467 {
468         struct input_absinfo absinfo;
469         unsigned long ev_bits[NBITS(EV_MAX)];
470         unsigned long abs_bits[NBITS(ABS_MAX)];
471         unsigned long rel_bits[NBITS(REL_MAX)];
472         unsigned long key_bits[NBITS(KEY_MAX)];
473         int has_abs, has_rel, has_mt;
474         int has_button, has_keyboard, has_touch;
475         unsigned int i;
476
477         has_rel = 0;
478         has_abs = 0;
479         has_mt = 0;
480         has_button = 0;
481         has_keyboard = 0;
482         has_touch = 0;
483
484         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
485         if (TEST_BIT(ev_bits, EV_ABS)) {
486                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
487                       abs_bits);
488
489                 if (TEST_BIT(abs_bits, ABS_X)) {
490                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
491                         device->abs.min_x = absinfo.minimum;
492                         device->abs.max_x = absinfo.maximum;
493                         has_abs = 1;
494                 }
495                 if (TEST_BIT(abs_bits, ABS_Y)) {
496                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
497                         device->abs.min_y = absinfo.minimum;
498                         device->abs.max_y = absinfo.maximum;
499                         has_abs = 1;
500                 }
501                 /* We only handle the slotted Protocol B in weston.
502                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
503                    require mtdev for conversion. */
504                 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
505                     TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
506                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
507                               &absinfo);
508                         device->abs.min_x = absinfo.minimum;
509                         device->abs.max_x = absinfo.maximum;
510                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
511                               &absinfo);
512                         device->abs.min_y = absinfo.minimum;
513                         device->abs.max_y = absinfo.maximum;
514                         device->is_mt = 1;
515                         has_touch = 1;
516                         has_mt = 1;
517
518                         if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
519                                 device->mtdev = mtdev_new_open(device->fd);
520                                 if (!device->mtdev) {
521                                         weston_log("mtdev required but failed to open for %s\n",
522                                                    device->devnode);
523                                         return 0;
524                                 }
525                                 device->mt.slot = device->mtdev->caps.slot.value;
526                         } else {
527                                 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
528                                       &absinfo);
529                                 device->mt.slot = absinfo.value;
530                         }
531                 }
532         }
533         if (TEST_BIT(ev_bits, EV_REL)) {
534                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
535                       rel_bits);
536                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
537                         has_rel = 1;
538         }
539         if (TEST_BIT(ev_bits, EV_KEY)) {
540                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
541                       key_bits);
542                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
543                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
544                     (has_abs || has_mt)) {
545                         device->dispatch = evdev_touchpad_create(device);
546                         weston_log("input device %s, %s is a touchpad\n",
547                                    device->devname, device->devnode);
548                 }
549                 for (i = KEY_ESC; i < KEY_MAX; i++) {
550                         if (i >= BTN_MISC && i < KEY_OK)
551                                 continue;
552                         if (TEST_BIT(key_bits, i)) {
553                                 has_keyboard = 1;
554                                 break;
555                         }
556                 }
557                 if (TEST_BIT(key_bits, BTN_TOUCH))
558                         has_touch = 1;
559                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
560                         if (TEST_BIT(key_bits, i)) {
561                                 has_button = 1;
562                                 break;
563                         }
564                 }
565         }
566         if (TEST_BIT(ev_bits, EV_LED))
567                 has_keyboard = 1;
568
569         if ((has_abs || has_rel) && has_button) {
570                 weston_seat_init_pointer(device->seat);
571                 device->seat_caps |= EVDEV_SEAT_POINTER;
572                 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
573                            device->devname, device->devnode,
574                            has_abs ? " absolute-motion" : "",
575                            has_rel ? " relative-motion": "",
576                            has_button ? " button" : "");
577         }
578         if (has_keyboard) {
579                 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
580                         return -1;
581                 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
582                 weston_log("input device %s, %s is a keyboard\n",
583                            device->devname, device->devnode);
584         }
585         if (has_touch && !has_button) {
586                 weston_seat_init_touch(device->seat);
587                 device->seat_caps |= EVDEV_SEAT_TOUCH;
588                 weston_log("input device %s, %s is a touch device\n",
589                            device->devname, device->devnode);
590         }
591
592         return 0;
593 }
594
595 static void
596 notify_output_destroy(struct wl_listener *listener, void *data)
597 {
598         struct evdev_device *device =
599                 container_of(listener,
600                              struct evdev_device, output_destroy_listener);
601         struct weston_compositor *c = device->seat->compositor;
602         struct weston_output *output;
603
604         if (!device->output_name && !wl_list_empty(&c->output_list)) {
605                 output = container_of(c->output_list.next,
606                                       struct weston_output, link);
607                 evdev_device_set_output(device, output);
608         } else {
609                 device->output = NULL;
610         }
611 }
612
613 void
614 evdev_device_set_output(struct evdev_device *device,
615                         struct weston_output *output)
616 {
617         if (device->output_destroy_listener.notify) {
618                 wl_list_remove(&device->output_destroy_listener.link);
619                 device->output_destroy_listener.notify = NULL;
620         }
621
622         device->output = output;
623         device->output_destroy_listener.notify = notify_output_destroy;
624         wl_signal_add(&output->destroy_signal,
625                       &device->output_destroy_listener);
626 }
627
628 struct evdev_device *
629 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
630 {
631         struct evdev_device *device;
632         struct weston_compositor *ec;
633         char devname[256] = "unknown";
634
635         device = zalloc(sizeof *device);
636         if (device == NULL)
637                 return NULL;
638
639         ec = seat->compositor;
640         device->seat = seat;
641         device->seat_caps = 0;
642         device->is_mt = 0;
643         device->mtdev = NULL;
644         device->devnode = strdup(path);
645         device->mt.slot = -1;
646         device->rel.dx = 0;
647         device->rel.dy = 0;
648         device->dispatch = NULL;
649         device->fd = device_fd;
650         device->pending_event = EVDEV_NONE;
651         wl_list_init(&device->link);
652
653         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
654         devname[sizeof(devname) - 1] = '\0';
655         device->devname = strdup(devname);
656
657         if (evdev_configure_device(device) == -1)
658                 goto err;
659
660         if (device->seat_caps == 0) {
661                 evdev_device_destroy(device);
662                 return EVDEV_UNHANDLED_DEVICE;
663         }
664
665         /* If the dispatch was not set up use the fallback. */
666         if (device->dispatch == NULL)
667                 device->dispatch = fallback_dispatch_create();
668         if (device->dispatch == NULL)
669                 goto err;
670
671         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
672                                               WL_EVENT_READABLE,
673                                               evdev_device_data, device);
674         if (device->source == NULL)
675                 goto err;
676
677         return device;
678
679 err:
680         evdev_device_destroy(device);
681         return NULL;
682 }
683
684 void
685 evdev_device_destroy(struct evdev_device *device)
686 {
687         struct evdev_dispatch *dispatch;
688
689         if (device->seat_caps & EVDEV_SEAT_POINTER)
690                 weston_seat_release_pointer(device->seat);
691         if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
692                 weston_seat_release_keyboard(device->seat);
693         if (device->seat_caps & EVDEV_SEAT_TOUCH)
694                 weston_seat_release_touch(device->seat);
695
696         dispatch = device->dispatch;
697         if (dispatch)
698                 dispatch->interface->destroy(dispatch);
699
700         if (device->source)
701                 wl_event_source_remove(device->source);
702         if (device->output)
703                 wl_list_remove(&device->output_destroy_listener.link);
704         wl_list_remove(&device->link);
705         if (device->mtdev)
706                 mtdev_close_delete(device->mtdev);
707         close(device->fd);
708         free(device->devname);
709         free(device->devnode);
710         free(device->output_name);
711         free(device);
712 }
713
714 void
715 evdev_notify_keyboard_focus(struct weston_seat *seat,
716                             struct wl_list *evdev_devices)
717 {
718         struct evdev_device *device;
719         struct wl_array keys;
720         unsigned int i, set;
721         char evdev_keys[(KEY_CNT + 7) / 8];
722         char all_keys[(KEY_CNT + 7) / 8];
723         uint32_t *k;
724         int ret;
725
726         if (!seat->keyboard_device_count > 0)
727                 return;
728
729         memset(all_keys, 0, sizeof all_keys);
730         wl_list_for_each(device, evdev_devices, link) {
731                 memset(evdev_keys, 0, sizeof evdev_keys);
732                 ret = ioctl(device->fd,
733                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
734                 if (ret < 0) {
735                         weston_log("failed to get keys for device %s\n",
736                                 device->devnode);
737                         continue;
738                 }
739                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
740                         all_keys[i] |= evdev_keys[i];
741         }
742
743         wl_array_init(&keys);
744         for (i = 0; i < KEY_CNT; i++) {
745                 set = all_keys[i >> 3] & (1 << (i & 7));
746                 if (set) {
747                         k = wl_array_add(&keys, sizeof *k);
748                         *k = i;
749                 }
750         }
751
752         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
753
754         wl_array_release(&keys);
755 }