blob: 63f50319041d91124b94108150e1fd3578100469 [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
41#include "compositor.h"
42#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070043#include "shared/helpers.h"
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020044#include "shared/timespec-util.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010045
Jonas Ådahle0de3c22014-03-12 22:08:42 +010046void
47evdev_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
61static void
62handle_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);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080067 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);
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020071 struct timespec time;
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080072
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;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010079
Alexandros Frantzis47e79c82017-11-16 18:20:57 +020080 timespec_from_usec(&time,
81 libinput_event_keyboard_get_time_usec(keyboard_event));
82
83 notify_key(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +010084 libinput_event_keyboard_get_key(keyboard_event),
Chris Michael7e7f7932016-02-22 08:47:24 -050085 key_state, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010086}
87
Peter Hutterer87743e92016-01-18 16:38:22 +100088static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +010089handle_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);
Jonas Ådahld2510102014-10-05 21:39:14 +020094 struct weston_pointer_motion_event event = { 0 };
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020095 struct timespec time;
Jonas Ådahl3845b322014-10-21 23:51:29 +020096 double dx_unaccel, dy_unaccel;
97
Alexandros Frantzis84b31f82017-11-24 18:01:46 +020098 timespec_from_usec(&time,
99 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahl3845b322014-10-21 23:51:29 +0200100 dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
101 dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100102
Jonas Ådahld2510102014-10-05 21:39:14 +0200103 event = (struct weston_pointer_motion_event) {
Jonas Ådahl3845b322014-10-21 23:51:29 +0200104 .mask = WESTON_POINTER_MOTION_REL |
105 WESTON_POINTER_MOTION_REL_UNACCEL,
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200106 .time = time,
Jonas Ådahld2510102014-10-05 21:39:14 +0200107 .dx = libinput_event_pointer_get_dx(pointer_event),
108 .dy = libinput_event_pointer_get_dy(pointer_event),
Jonas Ådahl3845b322014-10-21 23:51:29 +0200109 .dx_unaccel = dx_unaccel,
110 .dy_unaccel = dy_unaccel,
Jonas Ådahld2510102014-10-05 21:39:14 +0200111 };
112
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200113 notify_motion(device->seat, &time, &event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000114
115 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100116}
117
Peter Hutterer87743e92016-01-18 16:38:22 +1000118static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100119handle_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;
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200126 struct timespec time;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200127 double x, y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100128 uint32_t width, height;
129
130 if (!output)
Peter Hutterer87743e92016-01-18 16:38:22 +1000131 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100132
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200133 timespec_from_usec(&time,
134 libinput_event_pointer_get_time_usec(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100135 width = device->output->current_mode->width;
136 height = device->output->current_mode->height;
137
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200138 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);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100142
143 weston_output_transform_coordinate(device->output, x, y, &x, &y);
Alexandros Frantzis84b31f82017-11-24 18:01:46 +0200144 notify_motion_absolute(device->seat, &time, x, y);
Peter Hutterer87743e92016-01-18 16:38:22 +1000145
146 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100147}
148
Peter Hutterer87743e92016-01-18 16:38:22 +1000149static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100150handle_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);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800155 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);
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200159 struct timespec time;
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800160
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))
Peter Hutterer87743e92016-01-18 16:38:22 +1000166 return false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100167
Alexandros Frantzis215bedc2017-11-16 18:20:55 +0200168 timespec_from_usec(&time,
169 libinput_event_pointer_get_time_usec(pointer_event));
170
171 notify_button(device->seat, &time,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100172 libinput_event_pointer_get_button(pointer_event),
Christopher Michaele1719c72016-02-19 11:07:00 -0500173 button_state);
174
Peter Hutterer87743e92016-01-18 16:38:22 +1000175 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100176}
177
Peter Hutterer5dddd412015-01-15 13:14:43 +1000178static double
179normalize_scroll(struct libinput_event_pointer *pointer_event,
180 enum libinput_pointer_axis axis)
181{
Peter Hutterer5dddd412015-01-15 13:14:43 +1000182 enum libinput_pointer_axis_source source;
Peter Hutterer87743e92016-01-18 16:38:22 +1000183 double value = 0.0;
Peter Hutterer5dddd412015-01-15 13:14:43 +1000184
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;
Daniel Stone4933ca52017-03-14 17:24:04 +0000202 default:
203 assert(!"unhandled event source in normalize_scroll");
Peter Hutterer5dddd412015-01-15 13:14:43 +1000204 }
205
206 return value;
207}
208
Peter Hutterer87743e92016-01-18 16:38:22 +1000209static int32_t
210get_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
224static bool
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100225handle_pointer_axis(struct libinput_device *libinput_device,
226 struct libinput_event_pointer *pointer_event)
227{
Peter Hutterer87743e92016-01-18 16:38:22 +1000228 static int warned;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100229 struct evdev_device *device =
230 libinput_device_get_user_data(libinput_device);
Peter Hutterer87743e92016-01-18 16:38:22 +1000231 double vert, horiz;
232 int32_t vert_discrete, horiz_discrete;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000233 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000234 struct weston_pointer_axis_event weston_event;
Peter Hutterer87743e92016-01-18 16:38:22 +1000235 enum libinput_pointer_axis_source source;
236 uint32_t wl_axis_source;
237 bool has_vert, has_horiz;
Alexandros Frantzis80321942017-11-16 18:20:56 +0200238 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100239
Peter Hutterer87743e92016-01-18 16:38:22 +1000240 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
Alexandros Frantzis80321942017-11-16 18:20:56 +0200269 timespec_from_usec(&time,
270 libinput_event_pointer_get_time_usec(pointer_event));
271
Peter Hutterer87743e92016-01-18 16:38:22 +1000272 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
Peter Hutterer89b6a492016-01-18 15:58:17 +1000277 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200278 weston_event.value = vert;
Peter Hutterer87743e92016-01-18 16:38:22 +1000279 weston_event.discrete = vert_discrete;
280 weston_event.has_discrete = (vert_discrete != 0);
281
Alexandros Frantzis80321942017-11-16 18:20:56 +0200282 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000283 }
284
Peter Hutterer87743e92016-01-18 16:38:22 +1000285 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
Peter Hutterer89b6a492016-01-18 15:58:17 +1000290 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200291 weston_event.value = horiz;
Peter Hutterer87743e92016-01-18 16:38:22 +1000292 weston_event.discrete = horiz_discrete;
293 weston_event.has_discrete = (horiz_discrete != 0);
294
Alexandros Frantzis80321942017-11-16 18:20:56 +0200295 notify_axis(device->seat, &time, &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000296 }
Peter Hutterer87743e92016-01-18 16:38:22 +1000297
298 return true;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100299}
300
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500301static struct weston_output *
302touch_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
309static const char *
310touch_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
333static void
334touch_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
343static void
344do_set_calibration(struct evdev_device *evdev_device,
345 const struct weston_touch_device_matrix *cal)
346{
347 enum libinput_config_status status;
348
349 status = libinput_device_config_calibration_set_matrix(evdev_device->device,
350 cal->m);
351 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
352 weston_log("Failed to apply calibration.\n");
353}
354
355static void
356touch_set_calibration(struct weston_touch_device *device,
357 const struct weston_touch_device_matrix *cal)
358{
359 struct evdev_device *evdev_device = device->backend_data;
360
361 /* Stop output hotplug from reloading the WL_CALIBRATION values.
362 * libinput will maintain the latest calibration for us.
363 */
364 evdev_device->override_wl_calibration = true;
365
366 do_set_calibration(evdev_device, cal);
367}
368
369static 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
374};
375
376static struct weston_touch_device *
377create_touch_device(struct evdev_device *device)
378{
379 const struct weston_touch_device_ops *ops = NULL;
380 struct weston_touch_device *touch_device;
381 struct udev_device *udev_device;
382
383 if (libinput_device_config_calibration_has_matrix(device->device))
384 ops = &touch_calibration_ops;
385
386 udev_device = libinput_device_get_udev_device(device->device);
387 if (!udev_device)
388 return NULL;
389
390 touch_device = weston_touch_create_touch_device(device->seat->touch_state,
391 udev_device_get_syspath(udev_device),
392 device, ops);
393
394 udev_device_unref(udev_device);
395
396 if (!touch_device)
397 return NULL;
398
399 weston_log("Touchscreen - %s - %s\n",
400 libinput_device_get_name(device->device),
401 touch_device->syspath);
402
403 return touch_device;
404}
405
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100406static void
407handle_touch_with_coords(struct libinput_device *libinput_device,
408 struct libinput_event_touch *touch_event,
409 int touch_type)
410{
411 struct evdev_device *device =
412 libinput_device_get_user_data(libinput_device);
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200413 double x;
414 double y;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100415 uint32_t width, height;
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200416 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100417 int32_t slot;
418
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300419 if (!device->output)
420 return;
421
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200422 timespec_from_usec(&time,
423 libinput_event_touch_get_time_usec(touch_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100424 slot = libinput_event_touch_get_seat_slot(touch_event);
425
426 width = device->output->current_mode->width;
427 height = device->output->current_mode->height;
Giulio Camuffo90a6fc62016-03-22 17:44:54 +0200428 x = libinput_event_touch_get_x_transformed(touch_event, width);
429 y = libinput_event_touch_get_y_transformed(touch_event, height);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100430
431 weston_output_transform_coordinate(device->output,
432 x, y, &x, &y);
433
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200434 notify_touch(device->seat, &time, slot, x, y, touch_type);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100435}
436
437static void
438handle_touch_down(struct libinput_device *device,
439 struct libinput_event_touch *touch_event)
440{
441 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
442}
443
444static void
445handle_touch_motion(struct libinput_device *device,
446 struct libinput_event_touch *touch_event)
447{
448 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
449}
450
451static void
452handle_touch_up(struct libinput_device *libinput_device,
453 struct libinput_event_touch *touch_event)
454{
455 struct evdev_device *device =
456 libinput_device_get_user_data(libinput_device);
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200457 struct timespec time;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100458 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
459
Alexandros Frantzis9448deb2017-11-16 18:20:58 +0200460 timespec_from_usec(&time,
461 libinput_event_touch_get_time_usec(touch_event));
462
463 notify_touch(device->seat, &time, slot, 0, 0, WL_TOUCH_UP);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100464}
465
Jonas Ådahl1679f232014-04-12 09:39:51 +0200466static void
467handle_touch_frame(struct libinput_device *libinput_device,
468 struct libinput_event_touch *touch_event)
469{
470 struct evdev_device *device =
471 libinput_device_get_user_data(libinput_device);
472 struct weston_seat *seat = device->seat;
473
474 notify_touch_frame(seat);
475}
476
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100477int
478evdev_device_process_event(struct libinput_event *event)
479{
480 struct libinput_device *libinput_device =
481 libinput_event_get_device(event);
Peter Hutterer87743e92016-01-18 16:38:22 +1000482 struct evdev_device *device =
483 libinput_device_get_user_data(libinput_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100484 int handled = 1;
Peter Hutterer87743e92016-01-18 16:38:22 +1000485 bool need_frame = false;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100486
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));
491 break;
492 case LIBINPUT_EVENT_POINTER_MOTION:
Peter Hutterer87743e92016-01-18 16:38:22 +1000493 need_frame = handle_pointer_motion(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100494 libinput_event_get_pointer_event(event));
495 break;
496 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
Peter Hutterer87743e92016-01-18 16:38:22 +1000497 need_frame = handle_pointer_motion_absolute(
498 libinput_device,
499 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100500 break;
501 case LIBINPUT_EVENT_POINTER_BUTTON:
Peter Hutterer87743e92016-01-18 16:38:22 +1000502 need_frame = handle_pointer_button(libinput_device,
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100503 libinput_event_get_pointer_event(event));
504 break;
505 case LIBINPUT_EVENT_POINTER_AXIS:
Peter Hutterer87743e92016-01-18 16:38:22 +1000506 need_frame = handle_pointer_axis(
507 libinput_device,
508 libinput_event_get_pointer_event(event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100509 break;
510 case LIBINPUT_EVENT_TOUCH_DOWN:
511 handle_touch_down(libinput_device,
512 libinput_event_get_touch_event(event));
513 break;
514 case LIBINPUT_EVENT_TOUCH_MOTION:
515 handle_touch_motion(libinput_device,
516 libinput_event_get_touch_event(event));
517 break;
518 case LIBINPUT_EVENT_TOUCH_UP:
519 handle_touch_up(libinput_device,
520 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700521 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200522 case LIBINPUT_EVENT_TOUCH_FRAME:
523 handle_touch_frame(libinput_device,
524 libinput_event_get_touch_event(event));
525 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100526 default:
527 handled = 0;
528 weston_log("unknown libinput event %d\n",
529 libinput_event_get_type(event));
530 }
531
Peter Hutterer87743e92016-01-18 16:38:22 +1000532 if (need_frame)
533 notify_pointer_frame(device->seat);
534
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100535 return handled;
536}
537
538static void
539notify_output_destroy(struct wl_listener *listener, void *data)
540{
541 struct evdev_device *device =
542 container_of(listener,
543 struct evdev_device, output_destroy_listener);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100544
Pekka Paalanen1bbf58f2018-03-20 14:45:36 +0200545 evdev_device_set_output(device, NULL);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100546}
547
Peter Hutterer3fbba492014-09-09 13:02:25 +1000548/**
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.
553 */
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300554void
Peter Hutterer3fbba492014-09-09 13:02:25 +1000555evdev_device_set_calibration(struct evdev_device *device)
556{
557 struct udev *udev;
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;
564
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300565 if (!libinput_device_config_calibration_has_matrix(device->device))
Peter Hutterer3fbba492014-09-09 13:02:25 +1000566 return;
567
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300568 /* 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(
572 device->device,
573 calibration) != 0)
574 return;
575
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500576 /* touch_set_calibration() has updated the values, do not load old
577 * values from WL_CALIBRATION.
578 */
579 if (device->override_wl_calibration)
580 return;
581
Pekka Paalanendfc9d3b2017-04-18 12:14:32 +0300582 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");
586 return;
587 }
588
Peter Hutterer3fbba492014-09-09 13:02:25 +1000589 width = device->output->width;
590 height = device->output->height;
591 if (width == 0 || height == 0)
592 return;
593
Peter Hutterer3fbba492014-09-09 13:02:25 +1000594 udev = udev_new();
595 if (!udev)
596 return;
597
598 udev_device = udev_device_new_from_subsystem_sysname(udev,
599 "input",
600 sysname);
601 if (!udev_device)
602 goto out;
603
604 calibration_values =
605 udev_device_get_property_value(udev_device,
606 "WL_CALIBRATION");
607
Pekka Paalanened51b622018-03-21 14:30:04 +0200608 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",
612 sysname);
613 }
614
Peter Hutterer3fbba492014-09-09 13:02:25 +1000615 if (!calibration_values || sscanf(calibration_values,
616 "%f %f %f %f %f %f",
617 &calibration[0],
618 &calibration[1],
619 &calibration[2],
620 &calibration[3],
621 &calibration[4],
622 &calibration[5]) != 6)
623 goto out;
624
625 weston_log("Applying calibration: %f %f %f %f %f %f "
626 "(normalized %f %f)\n",
627 calibration[0],
628 calibration[1],
629 calibration[2],
630 calibration[3],
631 calibration[4],
632 calibration[5],
633 calibration[2] / width,
634 calibration[5] / height);
635
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;
641
642 status = libinput_device_config_calibration_set_matrix(device->device,
643 calibration);
644 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
645 weston_log("Failed to apply calibration.\n");
646
647out:
648 if (udev_device)
649 udev_device_unref(udev_device);
650 udev_unref(udev);
651}
652
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100653void
654evdev_device_set_output(struct evdev_device *device,
655 struct weston_output *output)
656{
Pekka Paalanen018e6ad2017-04-18 12:22:12 +0300657 if (device->output == output)
658 return;
659
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700660 if (device->output_destroy_listener.notify) {
661 wl_list_remove(&device->output_destroy_listener.link);
662 device->output_destroy_listener.notify = NULL;
663 }
664
Pekka Paalanen98d50cc2017-04-20 11:38:06 +0300665 if (!output) {
666 weston_log("output for input device %s removed\n",
667 libinput_device_get_sysname(device->device));
668
669 device->output = NULL;
670 return;
671 }
672
Pekka Paalanen6632b6d2017-04-18 12:22:12 +0300673 weston_log("associating input device %s with output %s "
674 "(%s by udev)\n",
675 libinput_device_get_sysname(device->device),
676 output->name,
677 device->output_name ?: "none");
678
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100679 device->output = output;
680 device->output_destroy_listener.notify = notify_output_destroy;
681 wl_signal_add(&output->destroy_signal,
682 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000683 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100684}
685
686struct evdev_device *
687evdev_device_create(struct libinput_device *libinput_device,
688 struct weston_seat *seat)
689{
690 struct evdev_device *device;
691
692 device = zalloc(sizeof *device);
693 if (device == NULL)
694 return NULL;
695
696 device->seat = seat;
697 wl_list_init(&device->link);
698 device->device = libinput_device;
699
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;
704 }
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;
709 }
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;
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500714 device->touch_device = create_touch_device(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100715 }
716
717 libinput_device_set_user_data(libinput_device, device);
718 libinput_device_ref(libinput_device);
719
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100720 return device;
721}
722
723void
724evdev_device_destroy(struct evdev_device *device)
725{
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);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500730 if (device->seat_caps & EVDEV_SEAT_TOUCH) {
731 weston_touch_device_destroy(device->touch_device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100732 weston_seat_release_touch(device->seat);
Louis-Francis Ratté-Boulianne6ef59c92017-11-28 20:42:47 -0500733 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100734
735 if (device->output)
736 wl_list_remove(&device->output_destroy_listener.link);
737 wl_list_remove(&device->link);
738 libinput_device_unref(device->device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100739 free(device->output_name);
740 free(device);
741}
742
743void
744evdev_notify_keyboard_focus(struct weston_seat *seat,
745 struct wl_list *evdev_devices)
746{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100747 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100748
Derek Foremand621df22014-11-19 11:04:12 -0600749 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100750 return;
751
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100752 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100753 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100754 wl_array_release(&keys);
755}