2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 * Copyright 2017-2018 Collabora, Ltd.
5 * Copyright 2017-2018 General Electric Company
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:
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.
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
35 #include <linux/input.h>
41 #include "compositor.h"
42 #include "libinput-device.h"
43 #include "shared/helpers.h"
44 #include "shared/timespec-util.h"
47 evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
49 enum libinput_led leds = 0;
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;
58 libinput_device_led_update(device->device, leds);
62 handle_keyboard_key(struct libinput_device *libinput_device,
63 struct libinput_event_keyboard *keyboard_event)
65 struct evdev_device *device =
66 libinput_device_get_user_data(libinput_device);
68 libinput_event_keyboard_get_key_state(keyboard_event);
70 libinput_event_keyboard_get_seat_key_count(keyboard_event);
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 &&
80 timespec_from_usec(&time,
81 libinput_event_keyboard_get_time_usec(keyboard_event));
83 notify_key(device->seat, &time,
84 libinput_event_keyboard_get_key(keyboard_event),
85 key_state, STATE_UPDATE_AUTOMATIC);
89 handle_pointer_motion(struct libinput_device *libinput_device,
90 struct libinput_event_pointer *pointer_event)
92 struct evdev_device *device =
93 libinput_device_get_user_data(libinput_device);
94 struct weston_pointer_motion_event event = { 0 };
96 double dx_unaccel, dy_unaccel;
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);
103 event = (struct weston_pointer_motion_event) {
104 .mask = WESTON_POINTER_MOTION_REL |
105 WESTON_POINTER_MOTION_REL_UNACCEL,
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,
113 notify_motion(device->seat, &time, &event);
119 handle_pointer_motion_absolute(
120 struct libinput_device *libinput_device,
121 struct libinput_event_pointer *pointer_event)
123 struct evdev_device *device =
124 libinput_device_get_user_data(libinput_device);
125 struct weston_output *output = device->output;
126 struct timespec time;
128 uint32_t width, height;
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;
138 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
140 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
143 weston_output_transform_coordinate(device->output, x, y, &x, &y);
144 notify_motion_absolute(device->seat, &time, x, y);
150 handle_pointer_button(struct libinput_device *libinput_device,
151 struct libinput_event_pointer *pointer_event)
153 struct evdev_device *device =
154 libinput_device_get_user_data(libinput_device);
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;
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))
168 timespec_from_usec(&time,
169 libinput_event_pointer_get_time_usec(pointer_event));
171 notify_button(device->seat, &time,
172 libinput_event_pointer_get_button(pointer_event),
179 normalize_scroll(struct libinput_event_pointer *pointer_event,
180 enum libinput_pointer_axis axis)
182 enum libinput_pointer_axis_source source;
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
192 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
193 value = 10 * libinput_event_pointer_get_axis_value_discrete(
197 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
198 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
199 value = libinput_event_pointer_get_axis_value(pointer_event,
203 assert(!"unhandled event source in normalize_scroll");
210 get_axis_discrete(struct libinput_event_pointer *pointer_event,
211 enum libinput_pointer_axis axis)
213 enum libinput_pointer_axis_source source;
215 source = libinput_event_pointer_get_axis_source(pointer_event);
217 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
220 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
225 handle_pointer_axis(struct libinput_device *libinput_device,
226 struct libinput_event_pointer *pointer_event)
229 struct evdev_device *device =
230 libinput_device_get_user_data(libinput_device);
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;
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);
245 if (!has_vert && !has_horiz)
248 source = libinput_event_pointer_get_axis_source(pointer_event);
250 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
251 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
253 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
254 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
256 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
257 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
261 weston_log("Unknown scroll source %d.\n", source);
267 notify_axis_source(device->seat, wl_axis_source);
269 timespec_from_usec(&time,
270 libinput_event_pointer_get_time_usec(pointer_event));
273 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
274 vert_discrete = get_axis_discrete(pointer_event, axis);
275 vert = normalize_scroll(pointer_event, axis);
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);
282 notify_axis(device->seat, &time, &weston_event);
286 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
287 horiz_discrete = get_axis_discrete(pointer_event, axis);
288 horiz = normalize_scroll(pointer_event, axis);
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);
295 notify_axis(device->seat, &time, &weston_event);
301 static struct weston_output *
302 touch_get_output(struct weston_touch_device *device)
304 struct evdev_device *evdev_device = device->backend_data;
306 return evdev_device->output;
310 touch_get_calibration_head_name(struct weston_touch_device *device)
312 struct evdev_device *evdev_device = device->backend_data;
313 struct weston_output *output = evdev_device->output;
314 struct weston_head *head;
319 assert(output->enabled);
320 if (evdev_device->output_name)
321 return evdev_device->output_name;
323 /* No specific head was configured, so the association was made by
324 * the default rule. Just grab whatever head's name.
326 wl_list_for_each(head, &output->head_list, output_link)
334 touch_get_calibration(struct weston_touch_device *device,
335 struct weston_touch_device_matrix *cal)
337 struct evdev_device *evdev_device = device->backend_data;
339 libinput_device_config_calibration_get_matrix(evdev_device->device,
344 do_set_calibration(struct evdev_device *evdev_device,
345 const struct weston_touch_device_matrix *cal)
347 enum libinput_config_status status;
349 status = libinput_device_config_calibration_set_matrix(evdev_device->device,
351 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
352 weston_log("Failed to apply calibration.\n");
356 touch_set_calibration(struct weston_touch_device *device,
357 const struct weston_touch_device_matrix *cal)
359 struct evdev_device *evdev_device = device->backend_data;
361 /* Stop output hotplug from reloading the WL_CALIBRATION values.
362 * libinput will maintain the latest calibration for us.
364 evdev_device->override_wl_calibration = true;
366 do_set_calibration(evdev_device, cal);
369 static const struct weston_touch_device_ops touch_calibration_ops = {
370 .get_output = touch_get_output,
371 .get_calibration_head_name = touch_get_calibration_head_name,
372 .get_calibration = touch_get_calibration,
373 .set_calibration = touch_set_calibration
376 static struct weston_touch_device *
377 create_touch_device(struct evdev_device *device)
379 const struct weston_touch_device_ops *ops = NULL;
380 struct weston_touch_device *touch_device;
381 struct udev_device *udev_device;
383 if (libinput_device_config_calibration_has_matrix(device->device))
384 ops = &touch_calibration_ops;
386 udev_device = libinput_device_get_udev_device(device->device);
390 touch_device = weston_touch_create_touch_device(device->seat->touch_state,
391 udev_device_get_syspath(udev_device),
394 udev_device_unref(udev_device);
399 weston_log("Touchscreen - %s - %s\n",
400 libinput_device_get_name(device->device),
401 touch_device->syspath);
407 handle_touch_with_coords(struct libinput_device *libinput_device,
408 struct libinput_event_touch *touch_event,
411 struct evdev_device *device =
412 libinput_device_get_user_data(libinput_device);
415 uint32_t width, height;
416 struct timespec time;
422 timespec_from_usec(&time,
423 libinput_event_touch_get_time_usec(touch_event));
424 slot = libinput_event_touch_get_seat_slot(touch_event);
426 width = device->output->current_mode->width;
427 height = device->output->current_mode->height;
428 x = libinput_event_touch_get_x_transformed(touch_event, width);
429 y = libinput_event_touch_get_y_transformed(touch_event, height);
431 weston_output_transform_coordinate(device->output,
434 notify_touch(device->seat, &time, slot, x, y, touch_type);
438 handle_touch_down(struct libinput_device *device,
439 struct libinput_event_touch *touch_event)
441 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
445 handle_touch_motion(struct libinput_device *device,
446 struct libinput_event_touch *touch_event)
448 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
452 handle_touch_up(struct libinput_device *libinput_device,
453 struct libinput_event_touch *touch_event)
455 struct evdev_device *device =
456 libinput_device_get_user_data(libinput_device);
457 struct timespec time;
458 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
460 timespec_from_usec(&time,
461 libinput_event_touch_get_time_usec(touch_event));
463 notify_touch(device->seat, &time, slot, 0, 0, WL_TOUCH_UP);
467 handle_touch_frame(struct libinput_device *libinput_device,
468 struct libinput_event_touch *touch_event)
470 struct evdev_device *device =
471 libinput_device_get_user_data(libinput_device);
472 struct weston_seat *seat = device->seat;
474 notify_touch_frame(seat);
478 evdev_device_process_event(struct libinput_event *event)
480 struct libinput_device *libinput_device =
481 libinput_event_get_device(event);
482 struct evdev_device *device =
483 libinput_device_get_user_data(libinput_device);
485 bool need_frame = false;
487 switch (libinput_event_get_type(event)) {
488 case LIBINPUT_EVENT_KEYBOARD_KEY:
489 handle_keyboard_key(libinput_device,
490 libinput_event_get_keyboard_event(event));
492 case LIBINPUT_EVENT_POINTER_MOTION:
493 need_frame = handle_pointer_motion(libinput_device,
494 libinput_event_get_pointer_event(event));
496 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
497 need_frame = handle_pointer_motion_absolute(
499 libinput_event_get_pointer_event(event));
501 case LIBINPUT_EVENT_POINTER_BUTTON:
502 need_frame = handle_pointer_button(libinput_device,
503 libinput_event_get_pointer_event(event));
505 case LIBINPUT_EVENT_POINTER_AXIS:
506 need_frame = handle_pointer_axis(
508 libinput_event_get_pointer_event(event));
510 case LIBINPUT_EVENT_TOUCH_DOWN:
511 handle_touch_down(libinput_device,
512 libinput_event_get_touch_event(event));
514 case LIBINPUT_EVENT_TOUCH_MOTION:
515 handle_touch_motion(libinput_device,
516 libinput_event_get_touch_event(event));
518 case LIBINPUT_EVENT_TOUCH_UP:
519 handle_touch_up(libinput_device,
520 libinput_event_get_touch_event(event));
522 case LIBINPUT_EVENT_TOUCH_FRAME:
523 handle_touch_frame(libinput_device,
524 libinput_event_get_touch_event(event));
528 weston_log("unknown libinput event %d\n",
529 libinput_event_get_type(event));
533 notify_pointer_frame(device->seat);
539 notify_output_destroy(struct wl_listener *listener, void *data)
541 struct evdev_device *device =
542 container_of(listener,
543 struct evdev_device, output_destroy_listener);
545 evdev_device_set_output(device, NULL);
549 * The WL_CALIBRATION property requires a pixel-specific matrix to be
550 * applied after scaling device coordinates to screen coordinates. libinput
551 * can't do that, so we need to convert the calibration to the normalized
552 * format libinput expects.
555 evdev_device_set_calibration(struct evdev_device *device)
558 struct udev_device *udev_device = NULL;
559 const char *sysname = libinput_device_get_sysname(device->device);
560 const char *calibration_values;
561 uint32_t width, height;
562 float calibration[6];
563 enum libinput_config_status status;
565 if (!libinput_device_config_calibration_has_matrix(device->device))
568 /* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
569 * override it with WL_CALIBRATION. It also means we don't need an
570 * output to load a calibration. */
571 if (libinput_device_config_calibration_get_default_matrix(
576 /* touch_set_calibration() has updated the values, do not load old
577 * values from WL_CALIBRATION.
579 if (device->override_wl_calibration)
582 if (!device->output) {
583 weston_log("input device %s has no enabled output associated "
584 "(%s named), skipping calibration for now.\n",
585 sysname, device->output_name ?: "none");
589 width = device->output->width;
590 height = device->output->height;
591 if (width == 0 || height == 0)
598 udev_device = udev_device_new_from_subsystem_sysname(udev,
605 udev_device_get_property_value(udev_device,
608 if (calibration_values) {
609 weston_log("Warning: input device %s has WL_CALIBRATION property set. "
610 "Support for it will be removed in the future. "
611 "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
615 if (!calibration_values || sscanf(calibration_values,
622 &calibration[5]) != 6)
625 weston_log("Applying calibration: %f %f %f %f %f %f "
626 "(normalized %f %f)\n",
633 calibration[2] / width,
634 calibration[5] / height);
636 /* normalize to a format libinput can use. There is a chance of
637 this being wrong if the width/height don't match the device
638 width/height but I'm not sure how to fix that */
639 calibration[2] /= width;
640 calibration[5] /= height;
642 status = libinput_device_config_calibration_set_matrix(device->device,
644 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
645 weston_log("Failed to apply calibration.\n");
649 udev_device_unref(udev_device);
654 evdev_device_set_output(struct evdev_device *device,
655 struct weston_output *output)
657 if (device->output == output)
660 if (device->output_destroy_listener.notify) {
661 wl_list_remove(&device->output_destroy_listener.link);
662 device->output_destroy_listener.notify = NULL;
666 weston_log("output for input device %s removed\n",
667 libinput_device_get_sysname(device->device));
669 device->output = NULL;
673 weston_log("associating input device %s with output %s "
675 libinput_device_get_sysname(device->device),
677 device->output_name ?: "none");
679 device->output = output;
680 device->output_destroy_listener.notify = notify_output_destroy;
681 wl_signal_add(&output->destroy_signal,
682 &device->output_destroy_listener);
683 evdev_device_set_calibration(device);
686 struct evdev_device *
687 evdev_device_create(struct libinput_device *libinput_device,
688 struct weston_seat *seat)
690 struct evdev_device *device;
692 device = zalloc(sizeof *device);
697 wl_list_init(&device->link);
698 device->device = libinput_device;
700 if (libinput_device_has_capability(libinput_device,
701 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
702 weston_seat_init_keyboard(seat, NULL);
703 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
705 if (libinput_device_has_capability(libinput_device,
706 LIBINPUT_DEVICE_CAP_POINTER)) {
707 weston_seat_init_pointer(seat);
708 device->seat_caps |= EVDEV_SEAT_POINTER;
710 if (libinput_device_has_capability(libinput_device,
711 LIBINPUT_DEVICE_CAP_TOUCH)) {
712 weston_seat_init_touch(seat);
713 device->seat_caps |= EVDEV_SEAT_TOUCH;
714 device->touch_device = create_touch_device(device);
717 libinput_device_set_user_data(libinput_device, device);
718 libinput_device_ref(libinput_device);
724 evdev_device_destroy(struct evdev_device *device)
726 if (device->seat_caps & EVDEV_SEAT_POINTER)
727 weston_seat_release_pointer(device->seat);
728 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
729 weston_seat_release_keyboard(device->seat);
730 if (device->seat_caps & EVDEV_SEAT_TOUCH) {
731 weston_touch_device_destroy(device->touch_device);
732 weston_seat_release_touch(device->seat);
736 wl_list_remove(&device->output_destroy_listener.link);
737 wl_list_remove(&device->link);
738 libinput_device_unref(device->device);
739 free(device->output_name);
744 evdev_notify_keyboard_focus(struct weston_seat *seat,
745 struct wl_list *evdev_devices)
747 struct wl_array keys;
749 if (seat->keyboard_device_count == 0)
752 wl_array_init(&keys);
753 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
754 wl_array_release(&keys);