libinput: move calibration printing into do_set_calibration()
[platform/upstream/weston.git] / libweston / libinput-device.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  * Copyright 2017-2018 Collabora, Ltd.
5  * Copyright 2017-2018 General Electric Company
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28
29 #include "config.h"
30
31 #include <errno.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <linux/input.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <assert.h>
39 #include <libinput.h>
40
41 #include "compositor.h"
42 #include "libinput-device.h"
43 #include "shared/helpers.h"
44 #include "shared/timespec-util.h"
45
46 void
47 evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
48 {
49         enum libinput_led leds = 0;
50
51         if (weston_leds & LED_NUM_LOCK)
52                 leds |= LIBINPUT_LED_NUM_LOCK;
53         if (weston_leds & LED_CAPS_LOCK)
54                 leds |= LIBINPUT_LED_CAPS_LOCK;
55         if (weston_leds & LED_SCROLL_LOCK)
56                 leds |= LIBINPUT_LED_SCROLL_LOCK;
57
58         libinput_device_led_update(device->device, leds);
59 }
60
61 static void
62 handle_keyboard_key(struct libinput_device *libinput_device,
63                     struct libinput_event_keyboard *keyboard_event)
64 {
65         struct evdev_device *device =
66                 libinput_device_get_user_data(libinput_device);
67         int key_state =
68                 libinput_event_keyboard_get_key_state(keyboard_event);
69         int seat_key_count =
70                 libinput_event_keyboard_get_seat_key_count(keyboard_event);
71         struct timespec time;
72
73         /* Ignore key events that are not seat wide state changes. */
74         if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
75              seat_key_count != 1) ||
76             (key_state == LIBINPUT_KEY_STATE_RELEASED &&
77              seat_key_count != 0))
78                 return;
79
80         timespec_from_usec(&time,
81                            libinput_event_keyboard_get_time_usec(keyboard_event));
82
83         notify_key(device->seat, &time,
84                    libinput_event_keyboard_get_key(keyboard_event),
85                    key_state, STATE_UPDATE_AUTOMATIC);
86 }
87
88 static bool
89 handle_pointer_motion(struct libinput_device *libinput_device,
90                       struct libinput_event_pointer *pointer_event)
91 {
92         struct evdev_device *device =
93                 libinput_device_get_user_data(libinput_device);
94         struct weston_pointer_motion_event event = { 0 };
95         struct timespec time;
96         double dx_unaccel, dy_unaccel;
97
98         timespec_from_usec(&time,
99                            libinput_event_pointer_get_time_usec(pointer_event));
100         dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
101         dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
102
103         event = (struct weston_pointer_motion_event) {
104                 .mask = WESTON_POINTER_MOTION_REL |
105                         WESTON_POINTER_MOTION_REL_UNACCEL,
106                 .time = time,
107                 .dx = libinput_event_pointer_get_dx(pointer_event),
108                 .dy = libinput_event_pointer_get_dy(pointer_event),
109                 .dx_unaccel = dx_unaccel,
110                 .dy_unaccel = dy_unaccel,
111         };
112
113         notify_motion(device->seat, &time, &event);
114
115         return true;
116 }
117
118 static bool
119 handle_pointer_motion_absolute(
120         struct libinput_device *libinput_device,
121         struct libinput_event_pointer *pointer_event)
122 {
123         struct evdev_device *device =
124                 libinput_device_get_user_data(libinput_device);
125         struct weston_output *output = device->output;
126         struct timespec time;
127         double x, y;
128         uint32_t width, height;
129
130         if (!output)
131                 return false;
132
133         timespec_from_usec(&time,
134                            libinput_event_pointer_get_time_usec(pointer_event));
135         width = device->output->current_mode->width;
136         height = device->output->current_mode->height;
137
138         x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
139                                                               width);
140         y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
141                                                               height);
142
143         weston_output_transform_coordinate(device->output, x, y, &x, &y);
144         notify_motion_absolute(device->seat, &time, x, y);
145
146         return true;
147 }
148
149 static bool
150 handle_pointer_button(struct libinput_device *libinput_device,
151                       struct libinput_event_pointer *pointer_event)
152 {
153         struct evdev_device *device =
154                 libinput_device_get_user_data(libinput_device);
155         int button_state =
156                 libinput_event_pointer_get_button_state(pointer_event);
157         int seat_button_count =
158                 libinput_event_pointer_get_seat_button_count(pointer_event);
159         struct timespec time;
160
161         /* Ignore button events that are not seat wide state changes. */
162         if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
163              seat_button_count != 1) ||
164             (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
165              seat_button_count != 0))
166                 return false;
167
168         timespec_from_usec(&time,
169                            libinput_event_pointer_get_time_usec(pointer_event));
170
171         notify_button(device->seat, &time,
172                       libinput_event_pointer_get_button(pointer_event),
173                       button_state);
174
175         return true;
176 }
177
178 static double
179 normalize_scroll(struct libinput_event_pointer *pointer_event,
180                  enum libinput_pointer_axis axis)
181 {
182         enum libinput_pointer_axis_source source;
183         double value = 0.0;
184
185         source = libinput_event_pointer_get_axis_source(pointer_event);
186         /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
187            the value is the angle of the click in degrees. To keep
188            backwards-compat with existing clients, we just send multiples of
189            the click count.
190          */
191         switch (source) {
192         case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
193                 value = 10 * libinput_event_pointer_get_axis_value_discrete(
194                                                                    pointer_event,
195                                                                    axis);
196                 break;
197         case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
198         case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
199                 value = libinput_event_pointer_get_axis_value(pointer_event,
200                                                               axis);
201                 break;
202         default:
203                 assert(!"unhandled event source in normalize_scroll");
204         }
205
206         return value;
207 }
208
209 static int32_t
210 get_axis_discrete(struct libinput_event_pointer *pointer_event,
211                   enum libinput_pointer_axis axis)
212 {
213         enum libinput_pointer_axis_source source;
214
215         source = libinput_event_pointer_get_axis_source(pointer_event);
216
217         if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
218                 return 0;
219
220         return libinput_event_pointer_get_axis_value_discrete(pointer_event,
221                                                               axis);
222 }
223
224 static bool
225 handle_pointer_axis(struct libinput_device *libinput_device,
226                     struct libinput_event_pointer *pointer_event)
227 {
228         static int warned;
229         struct evdev_device *device =
230                 libinput_device_get_user_data(libinput_device);
231         double vert, horiz;
232         int32_t vert_discrete, horiz_discrete;
233         enum libinput_pointer_axis axis;
234         struct weston_pointer_axis_event weston_event;
235         enum libinput_pointer_axis_source source;
236         uint32_t wl_axis_source;
237         bool has_vert, has_horiz;
238         struct timespec time;
239
240         has_vert = libinput_event_pointer_has_axis(pointer_event,
241                                    LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
242         has_horiz = libinput_event_pointer_has_axis(pointer_event,
243                                    LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
244
245         if (!has_vert && !has_horiz)
246                 return false;
247
248         source = libinput_event_pointer_get_axis_source(pointer_event);
249         switch (source) {
250         case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
251                 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
252                 break;
253         case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
254                 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
255                 break;
256         case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
257                 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
258                 break;
259         default:
260                 if (warned < 5) {
261                         weston_log("Unknown scroll source %d.\n", source);
262                         warned++;
263                 }
264                 return false;
265         }
266
267         notify_axis_source(device->seat, wl_axis_source);
268
269         timespec_from_usec(&time,
270                            libinput_event_pointer_get_time_usec(pointer_event));
271
272         if (has_vert) {
273                 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
274                 vert_discrete = get_axis_discrete(pointer_event, axis);
275                 vert = normalize_scroll(pointer_event, axis);
276
277                 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
278                 weston_event.value = vert;
279                 weston_event.discrete = vert_discrete;
280                 weston_event.has_discrete = (vert_discrete != 0);
281
282                 notify_axis(device->seat, &time, &weston_event);
283         }
284
285         if (has_horiz) {
286                 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
287                 horiz_discrete = get_axis_discrete(pointer_event, axis);
288                 horiz = normalize_scroll(pointer_event, axis);
289
290                 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
291                 weston_event.value = horiz;
292                 weston_event.discrete = horiz_discrete;
293                 weston_event.has_discrete = (horiz_discrete != 0);
294
295                 notify_axis(device->seat, &time, &weston_event);
296         }
297
298         return true;
299 }
300
301 static struct weston_output *
302 touch_get_output(struct weston_touch_device *device)
303 {
304         struct evdev_device *evdev_device = device->backend_data;
305
306         return evdev_device->output;
307 }
308
309 static const char *
310 touch_get_calibration_head_name(struct weston_touch_device *device)
311 {
312         struct evdev_device *evdev_device = device->backend_data;
313         struct weston_output *output = evdev_device->output;
314         struct weston_head *head;
315
316         if (!output)
317                 return NULL;
318
319         assert(output->enabled);
320         if (evdev_device->output_name)
321                 return evdev_device->output_name;
322
323         /* No specific head was configured, so the association was made by
324          * the default rule. Just grab whatever head's name.
325          */
326         wl_list_for_each(head, &output->head_list, output_link)
327                 return head->name;
328
329         assert(0);
330         return NULL;
331 }
332
333 static void
334 touch_get_calibration(struct weston_touch_device *device,
335                       struct weston_touch_device_matrix *cal)
336 {
337         struct evdev_device *evdev_device = device->backend_data;
338
339         libinput_device_config_calibration_get_matrix(evdev_device->device,
340                                                       cal->m);
341 }
342
343 static void
344 do_set_calibration(struct evdev_device *evdev_device,
345                    const struct weston_touch_device_matrix *cal)
346 {
347         enum libinput_config_status status;
348
349         weston_log("input device %s: applying calibration:\n",
350                    libinput_device_get_sysname(evdev_device->device));
351         weston_log_continue(STAMP_SPACE "  %f %f %f\n",
352                             cal->m[0], cal->m[1], cal->m[2]);
353         weston_log_continue(STAMP_SPACE "  %f %f %f\n",
354                             cal->m[3], cal->m[4], cal->m[5]);
355
356         status = libinput_device_config_calibration_set_matrix(evdev_device->device,
357                                                                cal->m);
358         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
359                 weston_log("Error: Failed to apply calibration.\n");
360 }
361
362 static void
363 touch_set_calibration(struct weston_touch_device *device,
364                       const struct weston_touch_device_matrix *cal)
365 {
366         struct evdev_device *evdev_device = device->backend_data;
367
368         /* Stop output hotplug from reloading the WL_CALIBRATION values.
369          * libinput will maintain the latest calibration for us.
370          */
371         evdev_device->override_wl_calibration = true;
372
373         do_set_calibration(evdev_device, cal);
374 }
375
376 static const struct weston_touch_device_ops touch_calibration_ops = {
377         .get_output = touch_get_output,
378         .get_calibration_head_name = touch_get_calibration_head_name,
379         .get_calibration = touch_get_calibration,
380         .set_calibration = touch_set_calibration
381 };
382
383 static struct weston_touch_device *
384 create_touch_device(struct evdev_device *device)
385 {
386         const struct weston_touch_device_ops *ops = NULL;
387         struct weston_touch_device *touch_device;
388         struct udev_device *udev_device;
389
390         if (libinput_device_config_calibration_has_matrix(device->device))
391                 ops = &touch_calibration_ops;
392
393         udev_device = libinput_device_get_udev_device(device->device);
394         if (!udev_device)
395                 return NULL;
396
397         touch_device = weston_touch_create_touch_device(device->seat->touch_state,
398                                         udev_device_get_syspath(udev_device),
399                                         device, ops);
400
401         udev_device_unref(udev_device);
402
403         if (!touch_device)
404                 return NULL;
405
406         weston_log("Touchscreen - %s - %s\n",
407                    libinput_device_get_name(device->device),
408                    touch_device->syspath);
409
410         return touch_device;
411 }
412
413 static void
414 handle_touch_with_coords(struct libinput_device *libinput_device,
415                          struct libinput_event_touch *touch_event,
416                          int touch_type)
417 {
418         struct evdev_device *device =
419                 libinput_device_get_user_data(libinput_device);
420         double x;
421         double y;
422         uint32_t width, height;
423         struct timespec time;
424         int32_t slot;
425
426         if (!device->output)
427                 return;
428
429         timespec_from_usec(&time,
430                            libinput_event_touch_get_time_usec(touch_event));
431         slot = libinput_event_touch_get_seat_slot(touch_event);
432
433         width = device->output->current_mode->width;
434         height = device->output->current_mode->height;
435         x =  libinput_event_touch_get_x_transformed(touch_event, width);
436         y =  libinput_event_touch_get_y_transformed(touch_event, height);
437
438         weston_output_transform_coordinate(device->output,
439                                            x, y, &x, &y);
440
441         notify_touch(device->seat, &time, slot, x, y, touch_type);
442 }
443
444 static void
445 handle_touch_down(struct libinput_device *device,
446                   struct libinput_event_touch *touch_event)
447 {
448         handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
449 }
450
451 static void
452 handle_touch_motion(struct libinput_device *device,
453                     struct libinput_event_touch *touch_event)
454 {
455         handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
456 }
457
458 static void
459 handle_touch_up(struct libinput_device *libinput_device,
460                 struct libinput_event_touch *touch_event)
461 {
462         struct evdev_device *device =
463                 libinput_device_get_user_data(libinput_device);
464         struct timespec time;
465         int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
466
467         timespec_from_usec(&time,
468                            libinput_event_touch_get_time_usec(touch_event));
469
470         notify_touch(device->seat, &time, slot, 0, 0, WL_TOUCH_UP);
471 }
472
473 static void
474 handle_touch_frame(struct libinput_device *libinput_device,
475                    struct libinput_event_touch *touch_event)
476 {
477         struct evdev_device *device =
478                 libinput_device_get_user_data(libinput_device);
479         struct weston_seat *seat = device->seat;
480
481         notify_touch_frame(seat);
482 }
483
484 int
485 evdev_device_process_event(struct libinput_event *event)
486 {
487         struct libinput_device *libinput_device =
488                 libinput_event_get_device(event);
489         struct evdev_device *device =
490                 libinput_device_get_user_data(libinput_device);
491         int handled = 1;
492         bool need_frame = false;
493
494         switch (libinput_event_get_type(event)) {
495         case LIBINPUT_EVENT_KEYBOARD_KEY:
496                 handle_keyboard_key(libinput_device,
497                                     libinput_event_get_keyboard_event(event));
498                 break;
499         case LIBINPUT_EVENT_POINTER_MOTION:
500                 need_frame = handle_pointer_motion(libinput_device,
501                                       libinput_event_get_pointer_event(event));
502                 break;
503         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
504                 need_frame = handle_pointer_motion_absolute(
505                                 libinput_device,
506                                 libinput_event_get_pointer_event(event));
507                 break;
508         case LIBINPUT_EVENT_POINTER_BUTTON:
509                 need_frame = handle_pointer_button(libinput_device,
510                                       libinput_event_get_pointer_event(event));
511                 break;
512         case LIBINPUT_EVENT_POINTER_AXIS:
513                 need_frame = handle_pointer_axis(
514                                  libinput_device,
515                                  libinput_event_get_pointer_event(event));
516                 break;
517         case LIBINPUT_EVENT_TOUCH_DOWN:
518                 handle_touch_down(libinput_device,
519                                   libinput_event_get_touch_event(event));
520                 break;
521         case LIBINPUT_EVENT_TOUCH_MOTION:
522                 handle_touch_motion(libinput_device,
523                                     libinput_event_get_touch_event(event));
524                 break;
525         case LIBINPUT_EVENT_TOUCH_UP:
526                 handle_touch_up(libinput_device,
527                                 libinput_event_get_touch_event(event));
528                 break;
529         case LIBINPUT_EVENT_TOUCH_FRAME:
530                 handle_touch_frame(libinput_device,
531                                    libinput_event_get_touch_event(event));
532                 break;
533         default:
534                 handled = 0;
535                 weston_log("unknown libinput event %d\n",
536                            libinput_event_get_type(event));
537         }
538
539         if (need_frame)
540                 notify_pointer_frame(device->seat);
541
542         return handled;
543 }
544
545 static void
546 notify_output_destroy(struct wl_listener *listener, void *data)
547 {
548         struct evdev_device *device =
549                 container_of(listener,
550                              struct evdev_device, output_destroy_listener);
551
552         evdev_device_set_output(device, NULL);
553 }
554
555 /**
556  * The WL_CALIBRATION property requires a pixel-specific matrix to be
557  * applied after scaling device coordinates to screen coordinates. libinput
558  * can't do that, so we need to convert the calibration to the normalized
559  * format libinput expects.
560  */
561 void
562 evdev_device_set_calibration(struct evdev_device *device)
563 {
564         struct udev *udev;
565         struct udev_device *udev_device = NULL;
566         const char *sysname = libinput_device_get_sysname(device->device);
567         const char *calibration_values;
568         uint32_t width, height;
569         struct weston_touch_device_matrix calibration;
570
571         if (!libinput_device_config_calibration_has_matrix(device->device))
572                 return;
573
574         /* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
575          * override it with WL_CALIBRATION. It also means we don't need an
576          * output to load a calibration. */
577         if (libinput_device_config_calibration_get_default_matrix(
578                                                           device->device,
579                                                           calibration.m) != 0)
580                 return;
581
582         /* touch_set_calibration() has updated the values, do not load old
583          * values from WL_CALIBRATION.
584          */
585         if (device->override_wl_calibration)
586                 return;
587
588         if (!device->output) {
589                 weston_log("input device %s has no enabled output associated "
590                            "(%s named), skipping calibration for now.\n",
591                            sysname, device->output_name ?: "none");
592                 return;
593         }
594
595         width = device->output->width;
596         height = device->output->height;
597         if (width == 0 || height == 0)
598                 return;
599
600         udev = udev_new();
601         if (!udev)
602                 return;
603
604         udev_device = udev_device_new_from_subsystem_sysname(udev,
605                                                              "input",
606                                                              sysname);
607         if (!udev_device)
608                 goto out;
609
610         calibration_values =
611                 udev_device_get_property_value(udev_device,
612                                                "WL_CALIBRATION");
613
614         if (calibration_values) {
615                 weston_log("Warning: input device %s has WL_CALIBRATION property set. "
616                            "Support for it will be removed in the future. "
617                            "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
618                            sysname);
619         }
620
621         if (!calibration_values || sscanf(calibration_values,
622                                           "%f %f %f %f %f %f",
623                                           &calibration.m[0],
624                                           &calibration.m[1],
625                                           &calibration.m[2],
626                                           &calibration.m[3],
627                                           &calibration.m[4],
628                                           &calibration.m[5]) != 6)
629                 goto out;
630
631         /* normalize to a format libinput can use. There is a chance of
632            this being wrong if the width/height don't match the device
633            width/height but I'm not sure how to fix that */
634         calibration.m[2] /= width;
635         calibration.m[5] /= height;
636
637         do_set_calibration(device, &calibration);
638
639         weston_log_continue(STAMP_SPACE "  raw translation %f %f for output %s\n",
640                    calibration.m[2] * width,
641                    calibration.m[5] * height,
642                    device->output->name);
643
644 out:
645         if (udev_device)
646                 udev_device_unref(udev_device);
647         udev_unref(udev);
648 }
649
650 void
651 evdev_device_set_output(struct evdev_device *device,
652                         struct weston_output *output)
653 {
654         if (device->output == output)
655                 return;
656
657         if (device->output_destroy_listener.notify) {
658                 wl_list_remove(&device->output_destroy_listener.link);
659                 device->output_destroy_listener.notify = NULL;
660         }
661
662         if (!output) {
663                 weston_log("output for input device %s removed\n",
664                            libinput_device_get_sysname(device->device));
665
666                 device->output = NULL;
667                 return;
668         }
669
670         weston_log("associating input device %s with output %s "
671                    "(%s by udev)\n",
672                    libinput_device_get_sysname(device->device),
673                    output->name,
674                    device->output_name ?: "none");
675
676         device->output = output;
677         device->output_destroy_listener.notify = notify_output_destroy;
678         wl_signal_add(&output->destroy_signal,
679                       &device->output_destroy_listener);
680         evdev_device_set_calibration(device);
681 }
682
683 struct evdev_device *
684 evdev_device_create(struct libinput_device *libinput_device,
685                     struct weston_seat *seat)
686 {
687         struct evdev_device *device;
688
689         device = zalloc(sizeof *device);
690         if (device == NULL)
691                 return NULL;
692
693         device->seat = seat;
694         wl_list_init(&device->link);
695         device->device = libinput_device;
696
697         if (libinput_device_has_capability(libinput_device,
698                                            LIBINPUT_DEVICE_CAP_KEYBOARD)) {
699                 weston_seat_init_keyboard(seat, NULL);
700                 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
701         }
702         if (libinput_device_has_capability(libinput_device,
703                                            LIBINPUT_DEVICE_CAP_POINTER)) {
704                 weston_seat_init_pointer(seat);
705                 device->seat_caps |= EVDEV_SEAT_POINTER;
706         }
707         if (libinput_device_has_capability(libinput_device,
708                                            LIBINPUT_DEVICE_CAP_TOUCH)) {
709                 weston_seat_init_touch(seat);
710                 device->seat_caps |= EVDEV_SEAT_TOUCH;
711                 device->touch_device = create_touch_device(device);
712         }
713
714         libinput_device_set_user_data(libinput_device, device);
715         libinput_device_ref(libinput_device);
716
717         return device;
718 }
719
720 void
721 evdev_device_destroy(struct evdev_device *device)
722 {
723         if (device->seat_caps & EVDEV_SEAT_POINTER)
724                 weston_seat_release_pointer(device->seat);
725         if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
726                 weston_seat_release_keyboard(device->seat);
727         if (device->seat_caps & EVDEV_SEAT_TOUCH) {
728                 weston_touch_device_destroy(device->touch_device);
729                 weston_seat_release_touch(device->seat);
730         }
731
732         if (device->output)
733                 wl_list_remove(&device->output_destroy_listener.link);
734         wl_list_remove(&device->link);
735         libinput_device_unref(device->device);
736         free(device->output_name);
737         free(device);
738 }
739
740 void
741 evdev_notify_keyboard_focus(struct weston_seat *seat,
742                             struct wl_list *evdev_devices)
743 {
744         struct wl_array keys;
745
746         if (seat->keyboard_device_count == 0)
747                 return;
748
749         wl_array_init(&keys);
750         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
751         wl_array_release(&keys);
752 }