blob: 9860d6e60943cc76ca80ab37c180771e7ad7a54c [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Jonas Ådahle0de3c22014-03-12 22:08:42 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Jonas Ådahle0de3c22014-03-12 22:08:42 +010025 */
26
27#include "config.h"
28
29#include <errno.h>
30#include <stdlib.h>
31#include <string.h>
32#include <linux/input.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <mtdev.h>
36#include <assert.h>
37#include <libinput.h>
38
39#include "compositor.h"
40#include "libinput-device.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070041#include "shared/helpers.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010042
Jonas Ådahle0de3c22014-03-12 22:08:42 +010043void
44evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
45{
46 enum libinput_led leds = 0;
47
48 if (weston_leds & LED_NUM_LOCK)
49 leds |= LIBINPUT_LED_NUM_LOCK;
50 if (weston_leds & LED_CAPS_LOCK)
51 leds |= LIBINPUT_LED_CAPS_LOCK;
52 if (weston_leds & LED_SCROLL_LOCK)
53 leds |= LIBINPUT_LED_SCROLL_LOCK;
54
55 libinput_device_led_update(device->device, leds);
56}
57
58static void
59handle_keyboard_key(struct libinput_device *libinput_device,
60 struct libinput_event_keyboard *keyboard_event)
61{
62 struct evdev_device *device =
63 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080064 int key_state =
65 libinput_event_keyboard_get_key_state(keyboard_event);
66 int seat_key_count =
67 libinput_event_keyboard_get_seat_key_count(keyboard_event);
68
69 /* Ignore key events that are not seat wide state changes. */
70 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
71 seat_key_count != 1) ||
72 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
73 seat_key_count != 0))
74 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010075
76 notify_key(device->seat,
77 libinput_event_keyboard_get_time(keyboard_event),
78 libinput_event_keyboard_get_key(keyboard_event),
79 libinput_event_keyboard_get_key_state(keyboard_event),
80 STATE_UPDATE_AUTOMATIC);
81}
82
83static void
84handle_pointer_motion(struct libinput_device *libinput_device,
85 struct libinput_event_pointer *pointer_event)
86{
87 struct evdev_device *device =
88 libinput_device_get_user_data(libinput_device);
Jonas Ådahld2510102014-10-05 21:39:14 +020089 struct weston_pointer_motion_event event = { 0 };
Jonas Ådahle0de3c22014-03-12 22:08:42 +010090
Jonas Ådahld2510102014-10-05 21:39:14 +020091 event = (struct weston_pointer_motion_event) {
92 .mask = WESTON_POINTER_MOTION_REL,
93 .dx = libinput_event_pointer_get_dx(pointer_event),
94 .dy = libinput_event_pointer_get_dy(pointer_event),
95 };
96
Jonas Ådahle0de3c22014-03-12 22:08:42 +010097 notify_motion(device->seat,
98 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahld2510102014-10-05 21:39:14 +020099 &event);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100100}
101
102static void
103handle_pointer_motion_absolute(
104 struct libinput_device *libinput_device,
105 struct libinput_event_pointer *pointer_event)
106{
107 struct evdev_device *device =
108 libinput_device_get_user_data(libinput_device);
109 struct weston_output *output = device->output;
110 uint32_t time;
111 wl_fixed_t x, y;
112 uint32_t width, height;
113
114 if (!output)
115 return;
116
117 time = libinput_event_pointer_get_time(pointer_event);
118 width = device->output->current_mode->width;
119 height = device->output->current_mode->height;
120
Jonas Ådahl26714b42014-06-02 23:15:48 +0200121 x = wl_fixed_from_double(
122 libinput_event_pointer_get_absolute_x_transformed(pointer_event,
123 width));
124 y = wl_fixed_from_double(
125 libinput_event_pointer_get_absolute_y_transformed(pointer_event,
126 height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100127
128 weston_output_transform_coordinate(device->output, x, y, &x, &y);
129 notify_motion_absolute(device->seat, time, x, y);
130}
131
132static void
133handle_pointer_button(struct libinput_device *libinput_device,
134 struct libinput_event_pointer *pointer_event)
135{
136 struct evdev_device *device =
137 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800138 int button_state =
139 libinput_event_pointer_get_button_state(pointer_event);
140 int seat_button_count =
141 libinput_event_pointer_get_seat_button_count(pointer_event);
142
143 /* Ignore button events that are not seat wide state changes. */
144 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
145 seat_button_count != 1) ||
146 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
147 seat_button_count != 0))
148 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100149
150 notify_button(device->seat,
151 libinput_event_pointer_get_time(pointer_event),
152 libinput_event_pointer_get_button(pointer_event),
153 libinput_event_pointer_get_button_state(pointer_event));
154}
155
Peter Hutterer5dddd412015-01-15 13:14:43 +1000156static double
157normalize_scroll(struct libinput_event_pointer *pointer_event,
158 enum libinput_pointer_axis axis)
159{
160 static int warned;
161 enum libinput_pointer_axis_source source;
162 double value;
163
164 source = libinput_event_pointer_get_axis_source(pointer_event);
165 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
166 the value is the angle of the click in degrees. To keep
167 backwards-compat with existing clients, we just send multiples of
168 the click count.
169 */
170 switch (source) {
171 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
172 value = 10 * libinput_event_pointer_get_axis_value_discrete(
173 pointer_event,
174 axis);
175 break;
176 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
177 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
178 value = libinput_event_pointer_get_axis_value(pointer_event,
179 axis);
180 break;
181 default:
182 value = 0;
183 if (warned < 5) {
184 weston_log("Unknown scroll source %d. Event discarded\n",
185 source);
186 warned++;
187 }
188 break;
189 }
190
191 return value;
192}
193
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100194static void
195handle_pointer_axis(struct libinput_device *libinput_device,
196 struct libinput_event_pointer *pointer_event)
197{
198 struct evdev_device *device =
199 libinput_device_get_user_data(libinput_device);
Jonas Ådahl26714b42014-06-02 23:15:48 +0200200 double value;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000201 enum libinput_pointer_axis axis;
Peter Hutterer89b6a492016-01-18 15:58:17 +1000202 struct weston_pointer_axis_event weston_event;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100203
Peter Huttererc54f23d2015-01-13 11:55:37 +1000204 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
205 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000206 value = normalize_scroll(pointer_event, axis);
Peter Hutterer89b6a492016-01-18 15:58:17 +1000207 weston_event.value = wl_fixed_from_double(value);
208 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000209 notify_axis(device->seat,
210 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000211 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000212 }
213
214 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
215 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000216 value = normalize_scroll(pointer_event, axis);
Peter Hutterer89b6a492016-01-18 15:58:17 +1000217 weston_event.value = wl_fixed_from_double(value);
218 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000219 notify_axis(device->seat,
220 libinput_event_pointer_get_time(pointer_event),
Peter Hutterer89b6a492016-01-18 15:58:17 +1000221 &weston_event);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000222 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100223}
224
225static void
226handle_touch_with_coords(struct libinput_device *libinput_device,
227 struct libinput_event_touch *touch_event,
228 int touch_type)
229{
230 struct evdev_device *device =
231 libinput_device_get_user_data(libinput_device);
232 wl_fixed_t x;
233 wl_fixed_t y;
234 uint32_t width, height;
235 uint32_t time;
236 int32_t slot;
237
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300238 if (!device->output)
239 return;
240
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100241 time = libinput_event_touch_get_time(touch_event);
242 slot = libinput_event_touch_get_seat_slot(touch_event);
243
244 width = device->output->current_mode->width;
245 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200246 x = wl_fixed_from_double(
247 libinput_event_touch_get_x_transformed(touch_event, width));
248 y = wl_fixed_from_double(
249 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100250
251 weston_output_transform_coordinate(device->output,
252 x, y, &x, &y);
253
254 notify_touch(device->seat, time, slot, x, y, touch_type);
255}
256
257static void
258handle_touch_down(struct libinput_device *device,
259 struct libinput_event_touch *touch_event)
260{
261 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
262}
263
264static void
265handle_touch_motion(struct libinput_device *device,
266 struct libinput_event_touch *touch_event)
267{
268 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
269}
270
271static void
272handle_touch_up(struct libinput_device *libinput_device,
273 struct libinput_event_touch *touch_event)
274{
275 struct evdev_device *device =
276 libinput_device_get_user_data(libinput_device);
277 uint32_t time = libinput_event_touch_get_time(touch_event);
278 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
279
280 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
281}
282
Jonas Ådahl1679f232014-04-12 09:39:51 +0200283static void
284handle_touch_frame(struct libinput_device *libinput_device,
285 struct libinput_event_touch *touch_event)
286{
287 struct evdev_device *device =
288 libinput_device_get_user_data(libinput_device);
289 struct weston_seat *seat = device->seat;
290
291 notify_touch_frame(seat);
292}
293
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100294int
295evdev_device_process_event(struct libinput_event *event)
296{
297 struct libinput_device *libinput_device =
298 libinput_event_get_device(event);
299 int handled = 1;
300
301 switch (libinput_event_get_type(event)) {
302 case LIBINPUT_EVENT_KEYBOARD_KEY:
303 handle_keyboard_key(libinput_device,
304 libinput_event_get_keyboard_event(event));
305 break;
306 case LIBINPUT_EVENT_POINTER_MOTION:
307 handle_pointer_motion(libinput_device,
308 libinput_event_get_pointer_event(event));
309 break;
310 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
311 handle_pointer_motion_absolute(
312 libinput_device,
313 libinput_event_get_pointer_event(event));
314 break;
315 case LIBINPUT_EVENT_POINTER_BUTTON:
316 handle_pointer_button(libinput_device,
317 libinput_event_get_pointer_event(event));
318 break;
319 case LIBINPUT_EVENT_POINTER_AXIS:
320 handle_pointer_axis(libinput_device,
321 libinput_event_get_pointer_event(event));
322 break;
323 case LIBINPUT_EVENT_TOUCH_DOWN:
324 handle_touch_down(libinput_device,
325 libinput_event_get_touch_event(event));
326 break;
327 case LIBINPUT_EVENT_TOUCH_MOTION:
328 handle_touch_motion(libinput_device,
329 libinput_event_get_touch_event(event));
330 break;
331 case LIBINPUT_EVENT_TOUCH_UP:
332 handle_touch_up(libinput_device,
333 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700334 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200335 case LIBINPUT_EVENT_TOUCH_FRAME:
336 handle_touch_frame(libinput_device,
337 libinput_event_get_touch_event(event));
338 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100339 default:
340 handled = 0;
341 weston_log("unknown libinput event %d\n",
342 libinput_event_get_type(event));
343 }
344
345 return handled;
346}
347
348static void
349notify_output_destroy(struct wl_listener *listener, void *data)
350{
351 struct evdev_device *device =
352 container_of(listener,
353 struct evdev_device, output_destroy_listener);
354 struct weston_compositor *c = device->seat->compositor;
355 struct weston_output *output;
356
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300357 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100358 output = container_of(c->output_list.next,
359 struct weston_output, link);
360 evdev_device_set_output(device, output);
361 } else {
362 device->output = NULL;
363 }
364}
365
Peter Hutterer3fbba492014-09-09 13:02:25 +1000366/**
367 * The WL_CALIBRATION property requires a pixel-specific matrix to be
368 * applied after scaling device coordinates to screen coordinates. libinput
369 * can't do that, so we need to convert the calibration to the normalized
370 * format libinput expects.
371 */
372static void
373evdev_device_set_calibration(struct evdev_device *device)
374{
375 struct udev *udev;
376 struct udev_device *udev_device = NULL;
377 const char *sysname = libinput_device_get_sysname(device->device);
378 const char *calibration_values;
379 uint32_t width, height;
380 float calibration[6];
381 enum libinput_config_status status;
382
383 if (!device->output)
384 return;
385
386 width = device->output->width;
387 height = device->output->height;
388 if (width == 0 || height == 0)
389 return;
390
391 /* If libinput has a pre-set calibration matrix, don't override it */
392 if (!libinput_device_config_calibration_has_matrix(device->device) ||
393 libinput_device_config_calibration_get_default_matrix(
394 device->device,
395 calibration) != 0)
396 return;
397
398 udev = udev_new();
399 if (!udev)
400 return;
401
402 udev_device = udev_device_new_from_subsystem_sysname(udev,
403 "input",
404 sysname);
405 if (!udev_device)
406 goto out;
407
408 calibration_values =
409 udev_device_get_property_value(udev_device,
410 "WL_CALIBRATION");
411
412 if (!calibration_values || sscanf(calibration_values,
413 "%f %f %f %f %f %f",
414 &calibration[0],
415 &calibration[1],
416 &calibration[2],
417 &calibration[3],
418 &calibration[4],
419 &calibration[5]) != 6)
420 goto out;
421
422 weston_log("Applying calibration: %f %f %f %f %f %f "
423 "(normalized %f %f)\n",
424 calibration[0],
425 calibration[1],
426 calibration[2],
427 calibration[3],
428 calibration[4],
429 calibration[5],
430 calibration[2] / width,
431 calibration[5] / height);
432
433 /* normalize to a format libinput can use. There is a chance of
434 this being wrong if the width/height don't match the device
435 width/height but I'm not sure how to fix that */
436 calibration[2] /= width;
437 calibration[5] /= height;
438
439 status = libinput_device_config_calibration_set_matrix(device->device,
440 calibration);
441 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
442 weston_log("Failed to apply calibration.\n");
443
444out:
445 if (udev_device)
446 udev_device_unref(udev_device);
447 udev_unref(udev);
448}
449
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100450void
451evdev_device_set_output(struct evdev_device *device,
452 struct weston_output *output)
453{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700454 if (device->output_destroy_listener.notify) {
455 wl_list_remove(&device->output_destroy_listener.link);
456 device->output_destroy_listener.notify = NULL;
457 }
458
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100459 device->output = output;
460 device->output_destroy_listener.notify = notify_output_destroy;
461 wl_signal_add(&output->destroy_signal,
462 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000463 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100464}
465
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200466static void
467configure_device(struct evdev_device *device)
468{
469 struct weston_compositor *compositor = device->seat->compositor;
470 struct weston_config_section *s;
471 int enable_tap;
472 int enable_tap_default;
473
474 s = weston_config_get_section(compositor->config,
475 "libinput", NULL, NULL);
476
477 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
478 enable_tap_default =
479 libinput_device_config_tap_get_default_enabled(
480 device->device);
481 weston_config_section_get_bool(s, "enable_tap",
482 &enable_tap,
483 enable_tap_default);
484 libinput_device_config_tap_set_enabled(device->device,
485 enable_tap);
486 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000487
488 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200489}
490
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100491struct evdev_device *
492evdev_device_create(struct libinput_device *libinput_device,
493 struct weston_seat *seat)
494{
495 struct evdev_device *device;
496
497 device = zalloc(sizeof *device);
498 if (device == NULL)
499 return NULL;
500
501 device->seat = seat;
502 wl_list_init(&device->link);
503 device->device = libinput_device;
504
505 if (libinput_device_has_capability(libinput_device,
506 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
507 weston_seat_init_keyboard(seat, NULL);
508 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
509 }
510 if (libinput_device_has_capability(libinput_device,
511 LIBINPUT_DEVICE_CAP_POINTER)) {
512 weston_seat_init_pointer(seat);
513 device->seat_caps |= EVDEV_SEAT_POINTER;
514 }
515 if (libinput_device_has_capability(libinput_device,
516 LIBINPUT_DEVICE_CAP_TOUCH)) {
517 weston_seat_init_touch(seat);
518 device->seat_caps |= EVDEV_SEAT_TOUCH;
519 }
520
521 libinput_device_set_user_data(libinput_device, device);
522 libinput_device_ref(libinput_device);
523
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200524 configure_device(device);
525
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100526 return device;
527}
528
529void
530evdev_device_destroy(struct evdev_device *device)
531{
532 if (device->seat_caps & EVDEV_SEAT_POINTER)
533 weston_seat_release_pointer(device->seat);
534 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
535 weston_seat_release_keyboard(device->seat);
536 if (device->seat_caps & EVDEV_SEAT_TOUCH)
537 weston_seat_release_touch(device->seat);
538
539 if (device->output)
540 wl_list_remove(&device->output_destroy_listener.link);
541 wl_list_remove(&device->link);
542 libinput_device_unref(device->device);
543 free(device->devnode);
544 free(device->output_name);
545 free(device);
546}
547
548void
549evdev_notify_keyboard_focus(struct weston_seat *seat,
550 struct wl_list *evdev_devices)
551{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100552 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100553
Derek Foremand621df22014-11-19 11:04:12 -0600554 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100555 return;
556
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100557 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100558 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100559 wl_array_release(&keys);
560}