blob: 5cc94284a766b55e5ab1a969596e36859f0c6529 [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -05004 * Copyright 2017-2018 Collabora, Ltd.
5 * Copyright 2017-2018 General Electric Company
Jonas Ådahle0de3c22014-03-12 22:08:42 +01006 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * 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:
Jonas Ådahle0de3c22014-03-12 22:08:42 +010014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * 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.
Jonas Ådahle0de3c22014-03-12 22:08:42 +010027 */
28
29#include "config.h"
30
31#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030032#include <stdint.h>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010033#include <stdlib.h>
34#include <string.h>
35#include <linux/input.h>
36#include <unistd.h>
37#include <fcntl.h>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010038#include <assert.h>
39#include <libinput.h>
40
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020041#include <libweston/libweston.h>
Marius Vlad63ef0782019-07-16 23:34:14 +030042#include "backend.h"
Marius Vlad9eb20642019-07-10 18:23:43 +030043#include "libweston-internal.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010044#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070045#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020046#include "shared/timespec-util.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010047
Jonas Ådahle0de3c22014-03-12 22:08:42 +010048void
49evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
50{
51 enum libinput_led leds = 0;
52
53 if (weston_leds & LED_NUM_LOCK)
54 leds |= LIBINPUT_LED_NUM_LOCK;
55 if (weston_leds & LED_CAPS_LOCK)
56 leds |= LIBINPUT_LED_CAPS_LOCK;
57 if (weston_leds & LED_SCROLL_LOCK)
58 leds |= LIBINPUT_LED_SCROLL_LOCK;
59
60 libinput_device_led_update(device->device, leds);
61}
62
63static void
Marius Vlade825fe32020-12-08 13:08:24 +020064ensure_pointer_capability(struct libinput_device *libinput_device)
65{
66 struct evdev_device *device = libinput_device_get_user_data(libinput_device);
67 struct weston_seat *seat = device->seat;
68
69 if (!libinput_device_has_capability(libinput_device, LIBINPUT_DEVICE_CAP_POINTER))
70 return;
71
72 if (!(device->seat_caps & EVDEV_SEAT_POINTER)) {
73 weston_seat_init_pointer(seat);
74 device->seat_caps |= EVDEV_SEAT_POINTER;
75 }
76}
77
78static void
Jonas Ådahle0de3c22014-03-12 22:08:42 +010079handle_keyboard_key(struct libinput_device *libinput_device,
80 struct libinput_event_keyboard *keyboard_event)
81{
82 struct evdev_device *device =
83 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080084 int key_state =
85 libinput_event_keyboard_get_key_state(keyboard_event);
86 int seat_key_count =
87 libinput_event_keyboard_get_seat_key_count(keyboard_event);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020088 struct timespec time;
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080089
90 /* Ignore key events that are not seat wide state changes. */
91 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
92 seat_key_count != 1) ||
93 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
94 seat_key_count != 0))
95 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010096
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020097 timespec_from_usec(&time,
98 libinput_event_keyboard_get_time_usec(keyboard_event));
99
100 notify_key(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100101 libinput_event_keyboard_get_key(keyboard_event),
Chris Michael7e7f7932016-02-22 08:47:24 -0500102 key_state, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100103}
104
Peter Hutterer87743e92016-01-18 16:38:22 +1000105static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100106handle_pointer_motion(struct libinput_device *libinput_device,
107 struct libinput_event_pointer *pointer_event)
108{
109 struct evdev_device *device =
110 libinput_device_get_user_data(libinput_device);
Jonas Ådahld2510102014-10-05 21:39:14 +0200111 struct weston_pointer_motion_event event = { 0 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200112 struct timespec time;
Jonas Ådahl3845b322014-10-21 23:51:29 +0200113 double dx_unaccel, dy_unaccel;
114
Marius Vlade825fe32020-12-08 13:08:24 +0200115 ensure_pointer_capability(libinput_device);
116
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200117 timespec_from_usec(&time,
118 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahl3845b322014-10-21 23:51:29 +0200119 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
120 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100121
Jonas Ådahld2510102014-10-05 21:39:14 +0200122 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +0200123 .mask = WESTON_POINTER_MOTION_REL |
124 WESTON_POINTER_MOTION_REL_UNACCEL,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200125 .time = time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200126 .dx = libinput_event_pointer_get_dx(pointer_event),
127 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200128 .dx_unaccel = dx_unaccel,
129 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200130 };
131
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200132 notify_motion(device->seat, &time, &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000133
134 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100135}
136
Peter Hutterer87743e92016-01-18 16:38:22 +1000137static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100138handle_pointer_motion_absolute(
139 struct libinput_device *libinput_device,
140 struct libinput_event_pointer *pointer_event)
141{
142 struct evdev_device *device =
143 libinput_device_get_user_data(libinput_device);
144 struct weston_output *output = device->output;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200145 struct timespec time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200146 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100147 uint32_t width, height;
148
Marius Vlade825fe32020-12-08 13:08:24 +0200149 ensure_pointer_capability(libinput_device);
150
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100151 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000152 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100153
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200154 timespec_from_usec(&time,
155 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100156 width = device->output->current_mode->width;
157 height = device->output->current_mode->height;
158
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200159 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
160 width);
161 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
162 height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100163
164 weston_output_transform_coordinate(device->output, x, y, &x, &y);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200165 notify_motion_absolute(device->seat, &time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000166
167 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100168}
169
Peter Hutterer87743e92016-01-18 16:38:22 +1000170static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100171handle_pointer_button(struct libinput_device *libinput_device,
172 struct libinput_event_pointer *pointer_event)
173{
174 struct evdev_device *device =
175 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800176 int button_state =
177 libinput_event_pointer_get_button_state(pointer_event);
178 int seat_button_count =
179 libinput_event_pointer_get_seat_button_count(pointer_event);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200180 struct timespec time;
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800181
Marius Vlade825fe32020-12-08 13:08:24 +0200182 ensure_pointer_capability(libinput_device);
183
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800184 /* Ignore button events that are not seat wide state changes. */
185 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
186 seat_button_count != 1) ||
187 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
188 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000189 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100190
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200191 timespec_from_usec(&time,
192 libinput_event_pointer_get_time_usec(pointer_event));
193
194 notify_button(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100195 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500196 button_state);
197
Peter Hutterer87743e92016-01-18 16:38:22 +1000198 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100199}
200
Peter Hutterer5dddd412015-01-15 13:14:43 +1000201static double
202normalize_scroll(struct libinput_event_pointer *pointer_event,
203 enum libinput_pointer_axis axis)
204{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000205 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000206 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000207
208 source = libinput_event_pointer_get_axis_source(pointer_event);
209 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
210 the value is the angle of the click in degrees. To keep
211 backwards-compat with existing clients, we just send multiples of
212 the click count.
213 */
214 switch (source) {
215 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
216 value = 10 * libinput_event_pointer_get_axis_value_discrete(
217 pointer_event,
218 axis);
219 break;
220 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
221 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
222 value = libinput_event_pointer_get_axis_value(pointer_event,
223 axis);
224 break;
Daniel Stone4933ca52017-03-14 17:24:04 +0000225 default:
226 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000227 }
228
229 return value;
230}
231
Peter Hutterer87743e92016-01-18 16:38:22 +1000232static int32_t
233get_axis_discrete(struct libinput_event_pointer *pointer_event,
234 enum libinput_pointer_axis axis)
235{
236 enum libinput_pointer_axis_source source;
237
238 source = libinput_event_pointer_get_axis_source(pointer_event);
239
240 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
241 return 0;
242
243 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
244 axis);
245}
246
247static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100248handle_pointer_axis(struct libinput_device *libinput_device,
249 struct libinput_event_pointer *pointer_event)
250{
Peter Hutterer87743e92016-01-18 16:38:22 +1000251 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100252 struct evdev_device *device =
253 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000254 double vert, horiz;
255 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000256 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000257 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000258 enum libinput_pointer_axis_source source;
259 uint32_t wl_axis_source;
260 bool has_vert, has_horiz;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200261 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100262
Marius Vlade825fe32020-12-08 13:08:24 +0200263 ensure_pointer_capability(libinput_device);
264
Peter Hutterer87743e92016-01-18 16:38:22 +1000265 has_vert = libinput_event_pointer_has_axis(pointer_event,
266 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
267 has_horiz = libinput_event_pointer_has_axis(pointer_event,
268 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
269
270 if (!has_vert && !has_horiz)
271 return false;
272
273 source = libinput_event_pointer_get_axis_source(pointer_event);
274 switch (source) {
275 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
276 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
277 break;
278 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
279 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
280 break;
281 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
282 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
283 break;
284 default:
285 if (warned < 5) {
286 weston_log("Unknown scroll source %d.\n", source);
287 warned++;
288 }
289 return false;
290 }
291
292 notify_axis_source(device->seat, wl_axis_source);
293
Alexandros Frantzis80321942017-11-16 18:20:56 +0200294 timespec_from_usec(&time,
295 libinput_event_pointer_get_time_usec(pointer_event));
296
Peter Hutterer87743e92016-01-18 16:38:22 +1000297 if (has_vert) {
298 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
299 vert_discrete = get_axis_discrete(pointer_event, axis);
300 vert = normalize_scroll(pointer_event, axis);
301
Peter Hutterer89b6a492016-01-18 15:58:17 +1000302 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200303 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000304 weston_event.discrete = vert_discrete;
305 weston_event.has_discrete = (vert_discrete != 0);
306
Alexandros Frantzis80321942017-11-16 18:20:56 +0200307 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000308 }
309
Peter Hutterer87743e92016-01-18 16:38:22 +1000310 if (has_horiz) {
311 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
312 horiz_discrete = get_axis_discrete(pointer_event, axis);
313 horiz = normalize_scroll(pointer_event, axis);
314
Peter Hutterer89b6a492016-01-18 15:58:17 +1000315 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200316 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000317 weston_event.discrete = horiz_discrete;
318 weston_event.has_discrete = (horiz_discrete != 0);
319
Alexandros Frantzis80321942017-11-16 18:20:56 +0200320 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000321 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000322
323 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100324}
325
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500326static struct weston_output *
327touch_get_output(struct weston_touch_device *device)
328{
329 struct evdev_device *evdev_device = device->backend_data;
330
331 return evdev_device->output;
332}
333
334static const char *
335touch_get_calibration_head_name(struct weston_touch_device *device)
336{
337 struct evdev_device *evdev_device = device->backend_data;
338 struct weston_output *output = evdev_device->output;
339 struct weston_head *head;
340
341 if (!output)
342 return NULL;
343
344 assert(output->enabled);
345 if (evdev_device->output_name)
346 return evdev_device->output_name;
347
348 /* No specific head was configured, so the association was made by
349 * the default rule. Just grab whatever head's name.
350 */
351 wl_list_for_each(head, &output->head_list, output_link)
352 return head->name;
353
354 assert(0);
355 return NULL;
356}
357
358static void
359touch_get_calibration(struct weston_touch_device *device,
360 struct weston_touch_device_matrix *cal)
361{
362 struct evdev_device *evdev_device = device->backend_data;
363
364 libinput_device_config_calibration_get_matrix(evdev_device->device,
365 cal->m);
366}
367
368static void
369do_set_calibration(struct evdev_device *evdev_device,
370 const struct weston_touch_device_matrix *cal)
371{
372 enum libinput_config_status status;
373
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300374 weston_log("input device %s: applying calibration:\n",
375 libinput_device_get_sysname(evdev_device->device));
376 weston_log_continue(STAMP_SPACE " %f %f %f\n",
377 cal->m[0], cal->m[1], cal->m[2]);
378 weston_log_continue(STAMP_SPACE " %f %f %f\n",
379 cal->m[3], cal->m[4], cal->m[5]);
380
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500381 status = libinput_device_config_calibration_set_matrix(evdev_device->device,
382 cal->m);
383 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300384 weston_log("Error: Failed to apply calibration.\n");
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500385}
386
387static void
388touch_set_calibration(struct weston_touch_device *device,
389 const struct weston_touch_device_matrix *cal)
390{
391 struct evdev_device *evdev_device = device->backend_data;
392
393 /* Stop output hotplug from reloading the WL_CALIBRATION values.
394 * libinput will maintain the latest calibration for us.
395 */
396 evdev_device->override_wl_calibration = true;
397
398 do_set_calibration(evdev_device, cal);
399}
400
401static const struct weston_touch_device_ops touch_calibration_ops = {
402 .get_output = touch_get_output,
403 .get_calibration_head_name = touch_get_calibration_head_name,
404 .get_calibration = touch_get_calibration,
405 .set_calibration = touch_set_calibration
406};
407
408static struct weston_touch_device *
409create_touch_device(struct evdev_device *device)
410{
411 const struct weston_touch_device_ops *ops = NULL;
412 struct weston_touch_device *touch_device;
413 struct udev_device *udev_device;
414
415 if (libinput_device_config_calibration_has_matrix(device->device))
416 ops = &touch_calibration_ops;
417
418 udev_device = libinput_device_get_udev_device(device->device);
419 if (!udev_device)
420 return NULL;
421
422 touch_device = weston_touch_create_touch_device(device->seat->touch_state,
423 udev_device_get_syspath(udev_device),
424 device, ops);
425
426 udev_device_unref(udev_device);
427
428 if (!touch_device)
429 return NULL;
430
431 weston_log("Touchscreen - %s - %s\n",
432 libinput_device_get_name(device->device),
433 touch_device->syspath);
434
435 return touch_device;
436}
437
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100438static void
439handle_touch_with_coords(struct libinput_device *libinput_device,
440 struct libinput_event_touch *touch_event,
441 int touch_type)
442{
443 struct evdev_device *device =
444 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200445 double x;
446 double y;
Pekka Paalanenf4062532018-02-26 16:18:29 +0200447 struct weston_point2d_device_normalized norm;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100448 uint32_t width, height;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200449 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100450 int32_t slot;
451
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300452 if (!device->output)
453 return;
454
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200455 timespec_from_usec(&time,
456 libinput_event_touch_get_time_usec(touch_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100457 slot = libinput_event_touch_get_seat_slot(touch_event);
458
459 width = device->output->current_mode->width;
460 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200461 x = libinput_event_touch_get_x_transformed(touch_event, width);
462 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100463
464 weston_output_transform_coordinate(device->output,
465 x, y, &x, &y);
466
Pekka Paalanenf4062532018-02-26 16:18:29 +0200467 if (weston_touch_device_can_calibrate(device->touch_device)) {
468 norm.x = libinput_event_touch_get_x_transformed(touch_event, 1);
469 norm.y = libinput_event_touch_get_y_transformed(touch_event, 1);
470 notify_touch_normalized(device->touch_device, &time, slot,
471 x, y, &norm, touch_type);
472 } else {
473 notify_touch(device->touch_device, &time, slot, x, y, touch_type);
474 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100475}
476
477static void
478handle_touch_down(struct libinput_device *device,
479 struct libinput_event_touch *touch_event)
480{
481 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
482}
483
484static void
485handle_touch_motion(struct libinput_device *device,
486 struct libinput_event_touch *touch_event)
487{
488 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
489}
490
491static void
492handle_touch_up(struct libinput_device *libinput_device,
493 struct libinput_event_touch *touch_event)
494{
495 struct evdev_device *device =
496 libinput_device_get_user_data(libinput_device);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200497 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100498 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
499
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200500 timespec_from_usec(&time,
501 libinput_event_touch_get_time_usec(touch_event));
502
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200503 notify_touch(device->touch_device, &time, slot, 0, 0, WL_TOUCH_UP);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100504}
505
Jonas Ådahl1679f232014-04-12 09:39:51 +0200506static void
507handle_touch_frame(struct libinput_device *libinput_device,
508 struct libinput_event_touch *touch_event)
509{
510 struct evdev_device *device =
511 libinput_device_get_user_data(libinput_device);
Jonas Ådahl1679f232014-04-12 09:39:51 +0200512
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200513 notify_touch_frame(device->touch_device);
Jonas Ådahl1679f232014-04-12 09:39:51 +0200514}
515
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100516int
517evdev_device_process_event(struct libinput_event *event)
518{
519 struct libinput_device *libinput_device =
520 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000521 struct evdev_device *device =
522 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100523 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000524 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100525
Marius Vladeb34f822021-03-30 23:09:05 +0300526 if (!device)
527 return 0;
528
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100529 switch (libinput_event_get_type(event)) {
530 case LIBINPUT_EVENT_KEYBOARD_KEY:
531 handle_keyboard_key(libinput_device,
532 libinput_event_get_keyboard_event(event));
533 break;
534 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000535 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100536 libinput_event_get_pointer_event(event));
537 break;
538 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000539 need_frame = handle_pointer_motion_absolute(
540 libinput_device,
541 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100542 break;
543 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000544 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100545 libinput_event_get_pointer_event(event));
546 break;
547 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000548 need_frame = handle_pointer_axis(
549 libinput_device,
550 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100551 break;
552 case LIBINPUT_EVENT_TOUCH_DOWN:
553 handle_touch_down(libinput_device,
554 libinput_event_get_touch_event(event));
555 break;
556 case LIBINPUT_EVENT_TOUCH_MOTION:
557 handle_touch_motion(libinput_device,
558 libinput_event_get_touch_event(event));
559 break;
560 case LIBINPUT_EVENT_TOUCH_UP:
561 handle_touch_up(libinput_device,
562 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700563 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200564 case LIBINPUT_EVENT_TOUCH_FRAME:
565 handle_touch_frame(libinput_device,
566 libinput_event_get_touch_event(event));
567 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100568 default:
569 handled = 0;
570 weston_log("unknown libinput event %d\n",
571 libinput_event_get_type(event));
572 }
573
Peter Hutterer87743e92016-01-18 16:38:22 +1000574 if (need_frame)
575 notify_pointer_frame(device->seat);
576
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100577 return handled;
578}
579
580static void
581notify_output_destroy(struct wl_listener *listener, void *data)
582{
583 struct evdev_device *device =
584 container_of(listener,
585 struct evdev_device, output_destroy_listener);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100586
Pekka Paalanen1bbf58f2018-03-20 14:45:36 +0200587 evdev_device_set_output(device, NULL);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100588}
589
Peter Hutterer3fbba492014-09-09 13:02:25 +1000590/**
591 * The WL_CALIBRATION property requires a pixel-specific matrix to be
592 * applied after scaling device coordinates to screen coordinates. libinput
593 * can't do that, so we need to convert the calibration to the normalized
594 * format libinput expects.
595 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300596void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000597evdev_device_set_calibration(struct evdev_device *device)
598{
599 struct udev *udev;
600 struct udev_device *udev_device = NULL;
601 const char *sysname = libinput_device_get_sysname(device->device);
602 const char *calibration_values;
603 uint32_t width, height;
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300604 struct weston_touch_device_matrix calibration;
Peter Hutterer3fbba492014-09-09 13:02:25 +1000605
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300606 if (!libinput_device_config_calibration_has_matrix(device->device))
Peter Hutterer3fbba492014-09-09 13:02:25 +1000607 return;
608
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300609 /* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
610 * override it with WL_CALIBRATION. It also means we don't need an
611 * output to load a calibration. */
612 if (libinput_device_config_calibration_get_default_matrix(
613 device->device,
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300614 calibration.m) != 0)
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300615 return;
616
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500617 /* touch_set_calibration() has updated the values, do not load old
618 * values from WL_CALIBRATION.
619 */
620 if (device->override_wl_calibration)
621 return;
622
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300623 if (!device->output) {
624 weston_log("input device %s has no enabled output associated "
625 "(%s named), skipping calibration for now.\n",
626 sysname, device->output_name ?: "none");
627 return;
628 }
629
Peter Hutterer3fbba492014-09-09 13:02:25 +1000630 width = device->output->width;
631 height = device->output->height;
632 if (width == 0 || height == 0)
633 return;
634
Peter Hutterer3fbba492014-09-09 13:02:25 +1000635 udev = udev_new();
636 if (!udev)
637 return;
638
639 udev_device = udev_device_new_from_subsystem_sysname(udev,
640 "input",
641 sysname);
642 if (!udev_device)
643 goto out;
644
645 calibration_values =
646 udev_device_get_property_value(udev_device,
647 "WL_CALIBRATION");
648
Pekka Paalanened51b622018-03-21 14:30:04 +0200649 if (calibration_values) {
650 weston_log("Warning: input device %s has WL_CALIBRATION property set. "
651 "Support for it will be removed in the future. "
652 "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
653 sysname);
654 }
655
Peter Hutterer3fbba492014-09-09 13:02:25 +1000656 if (!calibration_values || sscanf(calibration_values,
657 "%f %f %f %f %f %f",
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300658 &calibration.m[0],
659 &calibration.m[1],
660 &calibration.m[2],
661 &calibration.m[3],
662 &calibration.m[4],
663 &calibration.m[5]) != 6)
Peter Hutterer3fbba492014-09-09 13:02:25 +1000664 goto out;
665
Peter Hutterer3fbba492014-09-09 13:02:25 +1000666 /* normalize to a format libinput can use. There is a chance of
667 this being wrong if the width/height don't match the device
668 width/height but I'm not sure how to fix that */
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300669 calibration.m[2] /= width;
670 calibration.m[5] /= height;
Peter Hutterer3fbba492014-09-09 13:02:25 +1000671
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300672 do_set_calibration(device, &calibration);
673
674 weston_log_continue(STAMP_SPACE " raw translation %f %f for output %s\n",
675 calibration.m[2] * width,
676 calibration.m[5] * height,
677 device->output->name);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000678
679out:
680 if (udev_device)
681 udev_device_unref(udev_device);
682 udev_unref(udev);
683}
684
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100685void
686evdev_device_set_output(struct evdev_device *device,
687 struct weston_output *output)
688{
Pekka Paalanen018e6ad2017-04-18 12:22:12 +0300689 if (device->output == output)
690 return;
691
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700692 if (device->output_destroy_listener.notify) {
693 wl_list_remove(&device->output_destroy_listener.link);
694 device->output_destroy_listener.notify = NULL;
695 }
696
Pekka Paalanen98d50cc2017-04-20 11:38:06 +0300697 if (!output) {
698 weston_log("output for input device %s removed\n",
699 libinput_device_get_sysname(device->device));
700
701 device->output = NULL;
702 return;
703 }
704
Pekka Paalanen6632b6d2017-04-18 12:22:12 +0300705 weston_log("associating input device %s with output %s "
706 "(%s by udev)\n",
707 libinput_device_get_sysname(device->device),
708 output->name,
709 device->output_name ?: "none");
710
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100711 device->output = output;
712 device->output_destroy_listener.notify = notify_output_destroy;
713 wl_signal_add(&output->destroy_signal,
714 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000715 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100716}
717
718struct evdev_device *
719evdev_device_create(struct libinput_device *libinput_device,
720 struct weston_seat *seat)
721{
722 struct evdev_device *device;
723
724 device = zalloc(sizeof *device);
725 if (device == NULL)
726 return NULL;
727
728 device->seat = seat;
729 wl_list_init(&device->link);
730 device->device = libinput_device;
731
732 if (libinput_device_has_capability(libinput_device,
733 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
Marius Vladeb34f822021-03-30 23:09:05 +0300734 if (weston_seat_init_keyboard(seat, NULL) < 0) {
735 free(device);
736 return NULL;
737 }
738
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100739 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
740 }
Marius Vlade825fe32020-12-08 13:08:24 +0200741
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100742 if (libinput_device_has_capability(libinput_device,
743 LIBINPUT_DEVICE_CAP_TOUCH)) {
Marius Vladeb34f822021-03-30 23:09:05 +0300744 if (weston_seat_init_touch(seat) < 0) {
745 /* maybe we're a keyboard + touch device thus we need
746 * to release the keyboard in case we couldn't make use
747 * of the touch */
748 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
749 weston_seat_release_keyboard(seat);
750
751 free(device);
752 return NULL;
753 }
754
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100755 device->seat_caps |= EVDEV_SEAT_TOUCH;
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500756 device->touch_device = create_touch_device(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100757 }
758
759 libinput_device_set_user_data(libinput_device, device);
760 libinput_device_ref(libinput_device);
761
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100762 return device;
763}
764
765void
766evdev_device_destroy(struct evdev_device *device)
767{
768 if (device->seat_caps & EVDEV_SEAT_POINTER)
769 weston_seat_release_pointer(device->seat);
770 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
771 weston_seat_release_keyboard(device->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500772 if (device->seat_caps & EVDEV_SEAT_TOUCH) {
773 weston_touch_device_destroy(device->touch_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100774 weston_seat_release_touch(device->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500775 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100776
777 if (device->output)
778 wl_list_remove(&device->output_destroy_listener.link);
779 wl_list_remove(&device->link);
780 libinput_device_unref(device->device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100781 free(device->output_name);
782 free(device);
783}
784
785void
786evdev_notify_keyboard_focus(struct weston_seat *seat,
787 struct wl_list *evdev_devices)
788{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100789 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100790
Derek Foremand621df22014-11-19 11:04:12 -0600791 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100792 return;
793
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100794 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100795 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100796 wl_array_release(&keys);
797}