blob: 6941a5c4810e885847c705ccae2c5ac62a19b618 [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 Vlad9eb20642019-07-10 18:23:43 +030042#include "libweston-internal.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070044#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020045#include "shared/timespec-util.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010046
Jonas Ådahle0de3c22014-03-12 22:08:42 +010047void
48evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
49{
50 enum libinput_led leds = 0;
51
52 if (weston_leds & LED_NUM_LOCK)
53 leds |= LIBINPUT_LED_NUM_LOCK;
54 if (weston_leds & LED_CAPS_LOCK)
55 leds |= LIBINPUT_LED_CAPS_LOCK;
56 if (weston_leds & LED_SCROLL_LOCK)
57 leds |= LIBINPUT_LED_SCROLL_LOCK;
58
59 libinput_device_led_update(device->device, leds);
60}
61
62static void
63handle_keyboard_key(struct libinput_device *libinput_device,
64 struct libinput_event_keyboard *keyboard_event)
65{
66 struct evdev_device *device =
67 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080068 int key_state =
69 libinput_event_keyboard_get_key_state(keyboard_event);
70 int seat_key_count =
71 libinput_event_keyboard_get_seat_key_count(keyboard_event);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020072 struct timespec time;
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080073
74 /* Ignore key events that are not seat wide state changes. */
75 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
76 seat_key_count != 1) ||
77 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
78 seat_key_count != 0))
79 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010080
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020081 timespec_from_usec(&time,
82 libinput_event_keyboard_get_time_usec(keyboard_event));
83
84 notify_key(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +010085 libinput_event_keyboard_get_key(keyboard_event),
Chris Michael7e7f7932016-02-22 08:47:24 -050086 key_state, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010087}
88
Peter Hutterer87743e92016-01-18 16:38:22 +100089static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +010090handle_pointer_motion(struct libinput_device *libinput_device,
91 struct libinput_event_pointer *pointer_event)
92{
93 struct evdev_device *device =
94 libinput_device_get_user_data(libinput_device);
Jonas Ådahld2510102014-10-05 21:39:14 +020095 struct weston_pointer_motion_event event = { 0 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020096 struct timespec time;
Jonas Ådahl3845b322014-10-21 23:51:29 +020097 double dx_unaccel, dy_unaccel;
98
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020099 timespec_from_usec(&time,
100 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahl3845b322014-10-21 23:51:29 +0200101 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
102 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100103
Jonas Ådahld2510102014-10-05 21:39:14 +0200104 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +0200105 .mask = WESTON_POINTER_MOTION_REL |
106 WESTON_POINTER_MOTION_REL_UNACCEL,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200107 .time = time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200108 .dx = libinput_event_pointer_get_dx(pointer_event),
109 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200110 .dx_unaccel = dx_unaccel,
111 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200112 };
113
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200114 notify_motion(device->seat, &time, &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000115
116 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100117}
118
Peter Hutterer87743e92016-01-18 16:38:22 +1000119static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100120handle_pointer_motion_absolute(
121 struct libinput_device *libinput_device,
122 struct libinput_event_pointer *pointer_event)
123{
124 struct evdev_device *device =
125 libinput_device_get_user_data(libinput_device);
126 struct weston_output *output = device->output;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200127 struct timespec time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200128 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100129 uint32_t width, height;
130
131 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000132 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100133
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200134 timespec_from_usec(&time,
135 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100136 width = device->output->current_mode->width;
137 height = device->output->current_mode->height;
138
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200139 x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
140 width);
141 y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
142 height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100143
144 weston_output_transform_coordinate(device->output, x, y, &x, &y);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200145 notify_motion_absolute(device->seat, &time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000146
147 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100148}
149
Peter Hutterer87743e92016-01-18 16:38:22 +1000150static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100151handle_pointer_button(struct libinput_device *libinput_device,
152 struct libinput_event_pointer *pointer_event)
153{
154 struct evdev_device *device =
155 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800156 int button_state =
157 libinput_event_pointer_get_button_state(pointer_event);
158 int seat_button_count =
159 libinput_event_pointer_get_seat_button_count(pointer_event);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200160 struct timespec time;
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800161
162 /* Ignore button events that are not seat wide state changes. */
163 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
164 seat_button_count != 1) ||
165 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
166 seat_button_count != 0))
Peter Hutterer87743e92016-01-18 16:38:22 +1000167 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100168
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200169 timespec_from_usec(&time,
170 libinput_event_pointer_get_time_usec(pointer_event));
171
172 notify_button(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100173 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500174 button_state);
175
Peter Hutterer87743e92016-01-18 16:38:22 +1000176 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100177}
178
Peter Hutterer5dddd412015-01-15 13:14:43 +1000179static double
180normalize_scroll(struct libinput_event_pointer *pointer_event,
181 enum libinput_pointer_axis axis)
182{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000183 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000184 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000185
186 source = libinput_event_pointer_get_axis_source(pointer_event);
187 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
188 the value is the angle of the click in degrees. To keep
189 backwards-compat with existing clients, we just send multiples of
190 the click count.
191 */
192 switch (source) {
193 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
194 value = 10 * libinput_event_pointer_get_axis_value_discrete(
195 pointer_event,
196 axis);
197 break;
198 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
199 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
200 value = libinput_event_pointer_get_axis_value(pointer_event,
201 axis);
202 break;
Daniel Stone4933ca52017-03-14 17:24:04 +0000203 default:
204 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000205 }
206
207 return value;
208}
209
Peter Hutterer87743e92016-01-18 16:38:22 +1000210static int32_t
211get_axis_discrete(struct libinput_event_pointer *pointer_event,
212 enum libinput_pointer_axis axis)
213{
214 enum libinput_pointer_axis_source source;
215
216 source = libinput_event_pointer_get_axis_source(pointer_event);
217
218 if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
219 return 0;
220
221 return libinput_event_pointer_get_axis_value_discrete(pointer_event,
222 axis);
223}
224
225static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100226handle_pointer_axis(struct libinput_device *libinput_device,
227 struct libinput_event_pointer *pointer_event)
228{
Peter Hutterer87743e92016-01-18 16:38:22 +1000229 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100230 struct evdev_device *device =
231 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000232 double vert, horiz;
233 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000234 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000235 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000236 enum libinput_pointer_axis_source source;
237 uint32_t wl_axis_source;
238 bool has_vert, has_horiz;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200239 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100240
Peter Hutterer87743e92016-01-18 16:38:22 +1000241 has_vert = libinput_event_pointer_has_axis(pointer_event,
242 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
243 has_horiz = libinput_event_pointer_has_axis(pointer_event,
244 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
245
246 if (!has_vert && !has_horiz)
247 return false;
248
249 source = libinput_event_pointer_get_axis_source(pointer_event);
250 switch (source) {
251 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
252 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
253 break;
254 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
255 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
256 break;
257 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
258 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
259 break;
260 default:
261 if (warned < 5) {
262 weston_log("Unknown scroll source %d.\n", source);
263 warned++;
264 }
265 return false;
266 }
267
268 notify_axis_source(device->seat, wl_axis_source);
269
Alexandros Frantzis80321942017-11-16 18:20:56 +0200270 timespec_from_usec(&time,
271 libinput_event_pointer_get_time_usec(pointer_event));
272
Peter Hutterer87743e92016-01-18 16:38:22 +1000273 if (has_vert) {
274 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
275 vert_discrete = get_axis_discrete(pointer_event, axis);
276 vert = normalize_scroll(pointer_event, axis);
277
Peter Hutterer89b6a492016-01-18 15:58:17 +1000278 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200279 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000280 weston_event.discrete = vert_discrete;
281 weston_event.has_discrete = (vert_discrete != 0);
282
Alexandros Frantzis80321942017-11-16 18:20:56 +0200283 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000284 }
285
Peter Hutterer87743e92016-01-18 16:38:22 +1000286 if (has_horiz) {
287 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
288 horiz_discrete = get_axis_discrete(pointer_event, axis);
289 horiz = normalize_scroll(pointer_event, axis);
290
Peter Hutterer89b6a492016-01-18 15:58:17 +1000291 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200292 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000293 weston_event.discrete = horiz_discrete;
294 weston_event.has_discrete = (horiz_discrete != 0);
295
Alexandros Frantzis80321942017-11-16 18:20:56 +0200296 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000297 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000298
299 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100300}
301
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500302static struct weston_output *
303touch_get_output(struct weston_touch_device *device)
304{
305 struct evdev_device *evdev_device = device->backend_data;
306
307 return evdev_device->output;
308}
309
310static const char *
311touch_get_calibration_head_name(struct weston_touch_device *device)
312{
313 struct evdev_device *evdev_device = device->backend_data;
314 struct weston_output *output = evdev_device->output;
315 struct weston_head *head;
316
317 if (!output)
318 return NULL;
319
320 assert(output->enabled);
321 if (evdev_device->output_name)
322 return evdev_device->output_name;
323
324 /* No specific head was configured, so the association was made by
325 * the default rule. Just grab whatever head's name.
326 */
327 wl_list_for_each(head, &output->head_list, output_link)
328 return head->name;
329
330 assert(0);
331 return NULL;
332}
333
334static void
335touch_get_calibration(struct weston_touch_device *device,
336 struct weston_touch_device_matrix *cal)
337{
338 struct evdev_device *evdev_device = device->backend_data;
339
340 libinput_device_config_calibration_get_matrix(evdev_device->device,
341 cal->m);
342}
343
344static void
345do_set_calibration(struct evdev_device *evdev_device,
346 const struct weston_touch_device_matrix *cal)
347{
348 enum libinput_config_status status;
349
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300350 weston_log("input device %s: applying calibration:\n",
351 libinput_device_get_sysname(evdev_device->device));
352 weston_log_continue(STAMP_SPACE " %f %f %f\n",
353 cal->m[0], cal->m[1], cal->m[2]);
354 weston_log_continue(STAMP_SPACE " %f %f %f\n",
355 cal->m[3], cal->m[4], cal->m[5]);
356
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500357 status = libinput_device_config_calibration_set_matrix(evdev_device->device,
358 cal->m);
359 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300360 weston_log("Error: Failed to apply calibration.\n");
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500361}
362
363static void
364touch_set_calibration(struct weston_touch_device *device,
365 const struct weston_touch_device_matrix *cal)
366{
367 struct evdev_device *evdev_device = device->backend_data;
368
369 /* Stop output hotplug from reloading the WL_CALIBRATION values.
370 * libinput will maintain the latest calibration for us.
371 */
372 evdev_device->override_wl_calibration = true;
373
374 do_set_calibration(evdev_device, cal);
375}
376
377static const struct weston_touch_device_ops touch_calibration_ops = {
378 .get_output = touch_get_output,
379 .get_calibration_head_name = touch_get_calibration_head_name,
380 .get_calibration = touch_get_calibration,
381 .set_calibration = touch_set_calibration
382};
383
384static struct weston_touch_device *
385create_touch_device(struct evdev_device *device)
386{
387 const struct weston_touch_device_ops *ops = NULL;
388 struct weston_touch_device *touch_device;
389 struct udev_device *udev_device;
390
391 if (libinput_device_config_calibration_has_matrix(device->device))
392 ops = &touch_calibration_ops;
393
394 udev_device = libinput_device_get_udev_device(device->device);
395 if (!udev_device)
396 return NULL;
397
398 touch_device = weston_touch_create_touch_device(device->seat->touch_state,
399 udev_device_get_syspath(udev_device),
400 device, ops);
401
402 udev_device_unref(udev_device);
403
404 if (!touch_device)
405 return NULL;
406
407 weston_log("Touchscreen - %s - %s\n",
408 libinput_device_get_name(device->device),
409 touch_device->syspath);
410
411 return touch_device;
412}
413
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100414static void
415handle_touch_with_coords(struct libinput_device *libinput_device,
416 struct libinput_event_touch *touch_event,
417 int touch_type)
418{
419 struct evdev_device *device =
420 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200421 double x;
422 double y;
Pekka Paalanenf4062532018-02-26 16:18:29 +0200423 struct weston_point2d_device_normalized norm;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100424 uint32_t width, height;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200425 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100426 int32_t slot;
427
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300428 if (!device->output)
429 return;
430
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200431 timespec_from_usec(&time,
432 libinput_event_touch_get_time_usec(touch_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100433 slot = libinput_event_touch_get_seat_slot(touch_event);
434
435 width = device->output->current_mode->width;
436 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200437 x = libinput_event_touch_get_x_transformed(touch_event, width);
438 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100439
440 weston_output_transform_coordinate(device->output,
441 x, y, &x, &y);
442
Pekka Paalanenf4062532018-02-26 16:18:29 +0200443 if (weston_touch_device_can_calibrate(device->touch_device)) {
444 norm.x = libinput_event_touch_get_x_transformed(touch_event, 1);
445 norm.y = libinput_event_touch_get_y_transformed(touch_event, 1);
446 notify_touch_normalized(device->touch_device, &time, slot,
447 x, y, &norm, touch_type);
448 } else {
449 notify_touch(device->touch_device, &time, slot, x, y, touch_type);
450 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100451}
452
453static void
454handle_touch_down(struct libinput_device *device,
455 struct libinput_event_touch *touch_event)
456{
457 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
458}
459
460static void
461handle_touch_motion(struct libinput_device *device,
462 struct libinput_event_touch *touch_event)
463{
464 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
465}
466
467static void
468handle_touch_up(struct libinput_device *libinput_device,
469 struct libinput_event_touch *touch_event)
470{
471 struct evdev_device *device =
472 libinput_device_get_user_data(libinput_device);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200473 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100474 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
475
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200476 timespec_from_usec(&time,
477 libinput_event_touch_get_time_usec(touch_event));
478
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200479 notify_touch(device->touch_device, &time, slot, 0, 0, WL_TOUCH_UP);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100480}
481
Jonas Ådahl1679f232014-04-12 09:39:51 +0200482static void
483handle_touch_frame(struct libinput_device *libinput_device,
484 struct libinput_event_touch *touch_event)
485{
486 struct evdev_device *device =
487 libinput_device_get_user_data(libinput_device);
Jonas Ådahl1679f232014-04-12 09:39:51 +0200488
Pekka Paalanenbcbce332018-02-26 14:43:00 +0200489 notify_touch_frame(device->touch_device);
Jonas Ådahl1679f232014-04-12 09:39:51 +0200490}
491
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100492int
493evdev_device_process_event(struct libinput_event *event)
494{
495 struct libinput_device *libinput_device =
496 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000497 struct evdev_device *device =
498 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100499 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000500 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100501
502 switch (libinput_event_get_type(event)) {
503 case LIBINPUT_EVENT_KEYBOARD_KEY:
504 handle_keyboard_key(libinput_device,
505 libinput_event_get_keyboard_event(event));
506 break;
507 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000508 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100509 libinput_event_get_pointer_event(event));
510 break;
511 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000512 need_frame = handle_pointer_motion_absolute(
513 libinput_device,
514 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100515 break;
516 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000517 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100518 libinput_event_get_pointer_event(event));
519 break;
520 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000521 need_frame = handle_pointer_axis(
522 libinput_device,
523 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100524 break;
525 case LIBINPUT_EVENT_TOUCH_DOWN:
526 handle_touch_down(libinput_device,
527 libinput_event_get_touch_event(event));
528 break;
529 case LIBINPUT_EVENT_TOUCH_MOTION:
530 handle_touch_motion(libinput_device,
531 libinput_event_get_touch_event(event));
532 break;
533 case LIBINPUT_EVENT_TOUCH_UP:
534 handle_touch_up(libinput_device,
535 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700536 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200537 case LIBINPUT_EVENT_TOUCH_FRAME:
538 handle_touch_frame(libinput_device,
539 libinput_event_get_touch_event(event));
540 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100541 default:
542 handled = 0;
543 weston_log("unknown libinput event %d\n",
544 libinput_event_get_type(event));
545 }
546
Peter Hutterer87743e92016-01-18 16:38:22 +1000547 if (need_frame)
548 notify_pointer_frame(device->seat);
549
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100550 return handled;
551}
552
553static void
554notify_output_destroy(struct wl_listener *listener, void *data)
555{
556 struct evdev_device *device =
557 container_of(listener,
558 struct evdev_device, output_destroy_listener);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100559
Pekka Paalanen1bbf58f2018-03-20 14:45:36 +0200560 evdev_device_set_output(device, NULL);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100561}
562
Peter Hutterer3fbba492014-09-09 13:02:25 +1000563/**
564 * The WL_CALIBRATION property requires a pixel-specific matrix to be
565 * applied after scaling device coordinates to screen coordinates. libinput
566 * can't do that, so we need to convert the calibration to the normalized
567 * format libinput expects.
568 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300569void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000570evdev_device_set_calibration(struct evdev_device *device)
571{
572 struct udev *udev;
573 struct udev_device *udev_device = NULL;
574 const char *sysname = libinput_device_get_sysname(device->device);
575 const char *calibration_values;
576 uint32_t width, height;
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300577 struct weston_touch_device_matrix calibration;
Peter Hutterer3fbba492014-09-09 13:02:25 +1000578
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300579 if (!libinput_device_config_calibration_has_matrix(device->device))
Peter Hutterer3fbba492014-09-09 13:02:25 +1000580 return;
581
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300582 /* If LIBINPUT_CALIBRATION_MATRIX was set to non-identity, we will not
583 * override it with WL_CALIBRATION. It also means we don't need an
584 * output to load a calibration. */
585 if (libinput_device_config_calibration_get_default_matrix(
586 device->device,
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300587 calibration.m) != 0)
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300588 return;
589
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500590 /* touch_set_calibration() has updated the values, do not load old
591 * values from WL_CALIBRATION.
592 */
593 if (device->override_wl_calibration)
594 return;
595
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300596 if (!device->output) {
597 weston_log("input device %s has no enabled output associated "
598 "(%s named), skipping calibration for now.\n",
599 sysname, device->output_name ?: "none");
600 return;
601 }
602
Peter Hutterer3fbba492014-09-09 13:02:25 +1000603 width = device->output->width;
604 height = device->output->height;
605 if (width == 0 || height == 0)
606 return;
607
Peter Hutterer3fbba492014-09-09 13:02:25 +1000608 udev = udev_new();
609 if (!udev)
610 return;
611
612 udev_device = udev_device_new_from_subsystem_sysname(udev,
613 "input",
614 sysname);
615 if (!udev_device)
616 goto out;
617
618 calibration_values =
619 udev_device_get_property_value(udev_device,
620 "WL_CALIBRATION");
621
Pekka Paalanened51b622018-03-21 14:30:04 +0200622 if (calibration_values) {
623 weston_log("Warning: input device %s has WL_CALIBRATION property set. "
624 "Support for it will be removed in the future. "
625 "Please use LIBINPUT_CALIBRATION_MATRIX instead.\n",
626 sysname);
627 }
628
Peter Hutterer3fbba492014-09-09 13:02:25 +1000629 if (!calibration_values || sscanf(calibration_values,
630 "%f %f %f %f %f %f",
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300631 &calibration.m[0],
632 &calibration.m[1],
633 &calibration.m[2],
634 &calibration.m[3],
635 &calibration.m[4],
636 &calibration.m[5]) != 6)
Peter Hutterer3fbba492014-09-09 13:02:25 +1000637 goto out;
638
Peter Hutterer3fbba492014-09-09 13:02:25 +1000639 /* normalize to a format libinput can use. There is a chance of
640 this being wrong if the width/height don't match the device
641 width/height but I'm not sure how to fix that */
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300642 calibration.m[2] /= width;
643 calibration.m[5] /= height;
Peter Hutterer3fbba492014-09-09 13:02:25 +1000644
Pekka Paalanen09fbe142017-04-18 12:11:53 +0300645 do_set_calibration(device, &calibration);
646
647 weston_log_continue(STAMP_SPACE " raw translation %f %f for output %s\n",
648 calibration.m[2] * width,
649 calibration.m[5] * height,
650 device->output->name);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000651
652out:
653 if (udev_device)
654 udev_device_unref(udev_device);
655 udev_unref(udev);
656}
657
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100658void
659evdev_device_set_output(struct evdev_device *device,
660 struct weston_output *output)
661{
Pekka Paalanen018e6ad2017-04-18 12:22:12 +0300662 if (device->output == output)
663 return;
664
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700665 if (device->output_destroy_listener.notify) {
666 wl_list_remove(&device->output_destroy_listener.link);
667 device->output_destroy_listener.notify = NULL;
668 }
669
Pekka Paalanen98d50cc2017-04-20 11:38:06 +0300670 if (!output) {
671 weston_log("output for input device %s removed\n",
672 libinput_device_get_sysname(device->device));
673
674 device->output = NULL;
675 return;
676 }
677
Pekka Paalanen6632b6d2017-04-18 12:22:12 +0300678 weston_log("associating input device %s with output %s "
679 "(%s by udev)\n",
680 libinput_device_get_sysname(device->device),
681 output->name,
682 device->output_name ?: "none");
683
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100684 device->output = output;
685 device->output_destroy_listener.notify = notify_output_destroy;
686 wl_signal_add(&output->destroy_signal,
687 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000688 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100689}
690
691struct evdev_device *
692evdev_device_create(struct libinput_device *libinput_device,
693 struct weston_seat *seat)
694{
695 struct evdev_device *device;
696
697 device = zalloc(sizeof *device);
698 if (device == NULL)
699 return NULL;
700
701 device->seat = seat;
702 wl_list_init(&device->link);
703 device->device = libinput_device;
704
705 if (libinput_device_has_capability(libinput_device,
706 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
707 weston_seat_init_keyboard(seat, NULL);
708 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
709 }
710 if (libinput_device_has_capability(libinput_device,
711 LIBINPUT_DEVICE_CAP_POINTER)) {
712 weston_seat_init_pointer(seat);
713 device->seat_caps |= EVDEV_SEAT_POINTER;
714 }
715 if (libinput_device_has_capability(libinput_device,
716 LIBINPUT_DEVICE_CAP_TOUCH)) {
717 weston_seat_init_touch(seat);
718 device->seat_caps |= EVDEV_SEAT_TOUCH;
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500719 device->touch_device = create_touch_device(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100720 }
721
722 libinput_device_set_user_data(libinput_device, device);
723 libinput_device_ref(libinput_device);
724
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100725 return device;
726}
727
728void
729evdev_device_destroy(struct evdev_device *device)
730{
731 if (device->seat_caps & EVDEV_SEAT_POINTER)
732 weston_seat_release_pointer(device->seat);
733 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
734 weston_seat_release_keyboard(device->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500735 if (device->seat_caps & EVDEV_SEAT_TOUCH) {
736 weston_touch_device_destroy(device->touch_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100737 weston_seat_release_touch(device->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500738 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100739
740 if (device->output)
741 wl_list_remove(&device->output_destroy_listener.link);
742 wl_list_remove(&device->link);
743 libinput_device_unref(device->device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100744 free(device->output_name);
745 free(device);
746}
747
748void
749evdev_notify_keyboard_focus(struct weston_seat *seat,
750 struct wl_list *evdev_devices)
751{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100752 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100753
Derek Foremand621df22014-11-19 11:04:12 -0600754 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100755 return;
756
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100757 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100758 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100759 wl_array_release(&keys);
760}