blob: 2cbfb88dc075c7b975b76f77792d061f65d18823 [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
43#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
44
45void
46evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
47{
48 enum libinput_led leds = 0;
49
50 if (weston_leds & LED_NUM_LOCK)
51 leds |= LIBINPUT_LED_NUM_LOCK;
52 if (weston_leds & LED_CAPS_LOCK)
53 leds |= LIBINPUT_LED_CAPS_LOCK;
54 if (weston_leds & LED_SCROLL_LOCK)
55 leds |= LIBINPUT_LED_SCROLL_LOCK;
56
57 libinput_device_led_update(device->device, leds);
58}
59
60static void
61handle_keyboard_key(struct libinput_device *libinput_device,
62 struct libinput_event_keyboard *keyboard_event)
63{
64 struct evdev_device *device =
65 libinput_device_get_user_data(libinput_device);
Jonas Ådahl90d1ac82015-01-30 12:23:00 +080066 int key_state =
67 libinput_event_keyboard_get_key_state(keyboard_event);
68 int seat_key_count =
69 libinput_event_keyboard_get_seat_key_count(keyboard_event);
70
71 /* Ignore key events that are not seat wide state changes. */
72 if ((key_state == LIBINPUT_KEY_STATE_PRESSED &&
73 seat_key_count != 1) ||
74 (key_state == LIBINPUT_KEY_STATE_RELEASED &&
75 seat_key_count != 0))
76 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010077
78 notify_key(device->seat,
79 libinput_event_keyboard_get_time(keyboard_event),
80 libinput_event_keyboard_get_key(keyboard_event),
81 libinput_event_keyboard_get_key_state(keyboard_event),
82 STATE_UPDATE_AUTOMATIC);
83}
84
85static void
86handle_pointer_motion(struct libinput_device *libinput_device,
87 struct libinput_event_pointer *pointer_event)
88{
89 struct evdev_device *device =
90 libinput_device_get_user_data(libinput_device);
Jonas Ådahl26714b42014-06-02 23:15:48 +020091 wl_fixed_t dx, dy;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010092
Jonas Ådahl26714b42014-06-02 23:15:48 +020093 dx = wl_fixed_from_double(libinput_event_pointer_get_dx(pointer_event));
94 dy = wl_fixed_from_double(libinput_event_pointer_get_dy(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +010095 notify_motion(device->seat,
96 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahl26714b42014-06-02 23:15:48 +020097 dx,
98 dy);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010099}
100
101static void
102handle_pointer_motion_absolute(
103 struct libinput_device *libinput_device,
104 struct libinput_event_pointer *pointer_event)
105{
106 struct evdev_device *device =
107 libinput_device_get_user_data(libinput_device);
108 struct weston_output *output = device->output;
109 uint32_t time;
110 wl_fixed_t x, y;
111 uint32_t width, height;
112
113 if (!output)
114 return;
115
116 time = libinput_event_pointer_get_time(pointer_event);
117 width = device->output->current_mode->width;
118 height = device->output->current_mode->height;
119
Jonas Ådahl26714b42014-06-02 23:15:48 +0200120 x = wl_fixed_from_double(
121 libinput_event_pointer_get_absolute_x_transformed(pointer_event,
122 width));
123 y = wl_fixed_from_double(
124 libinput_event_pointer_get_absolute_y_transformed(pointer_event,
125 height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100126
127 weston_output_transform_coordinate(device->output, x, y, &x, &y);
128 notify_motion_absolute(device->seat, time, x, y);
129}
130
131static void
132handle_pointer_button(struct libinput_device *libinput_device,
133 struct libinput_event_pointer *pointer_event)
134{
135 struct evdev_device *device =
136 libinput_device_get_user_data(libinput_device);
Jonas Ådahle90b9e92015-01-30 12:22:59 +0800137 int button_state =
138 libinput_event_pointer_get_button_state(pointer_event);
139 int seat_button_count =
140 libinput_event_pointer_get_seat_button_count(pointer_event);
141
142 /* Ignore button events that are not seat wide state changes. */
143 if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
144 seat_button_count != 1) ||
145 (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
146 seat_button_count != 0))
147 return;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100148
149 notify_button(device->seat,
150 libinput_event_pointer_get_time(pointer_event),
151 libinput_event_pointer_get_button(pointer_event),
152 libinput_event_pointer_get_button_state(pointer_event));
153}
154
Peter Hutterer5dddd412015-01-15 13:14:43 +1000155static double
156normalize_scroll(struct libinput_event_pointer *pointer_event,
157 enum libinput_pointer_axis axis)
158{
159 static int warned;
160 enum libinput_pointer_axis_source source;
161 double value;
162
163 source = libinput_event_pointer_get_axis_source(pointer_event);
164 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
165 the value is the angle of the click in degrees. To keep
166 backwards-compat with existing clients, we just send multiples of
167 the click count.
168 */
169 switch (source) {
170 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
171 value = 10 * libinput_event_pointer_get_axis_value_discrete(
172 pointer_event,
173 axis);
174 break;
175 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
176 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
177 value = libinput_event_pointer_get_axis_value(pointer_event,
178 axis);
179 break;
180 default:
181 value = 0;
182 if (warned < 5) {
183 weston_log("Unknown scroll source %d. Event discarded\n",
184 source);
185 warned++;
186 }
187 break;
188 }
189
190 return value;
191}
192
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100193static void
194handle_pointer_axis(struct libinput_device *libinput_device,
195 struct libinput_event_pointer *pointer_event)
196{
197 struct evdev_device *device =
198 libinput_device_get_user_data(libinput_device);
Jonas Ådahl26714b42014-06-02 23:15:48 +0200199 double value;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000200 enum libinput_pointer_axis axis;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100201
Peter Huttererc54f23d2015-01-13 11:55:37 +1000202 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
203 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000204 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000205 notify_axis(device->seat,
206 libinput_event_pointer_get_time(pointer_event),
207 WL_POINTER_AXIS_VERTICAL_SCROLL,
208 wl_fixed_from_double(value));
209 }
210
211 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
212 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000213 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000214 notify_axis(device->seat,
215 libinput_event_pointer_get_time(pointer_event),
216 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
217 wl_fixed_from_double(value));
218 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100219}
220
221static void
222handle_touch_with_coords(struct libinput_device *libinput_device,
223 struct libinput_event_touch *touch_event,
224 int touch_type)
225{
226 struct evdev_device *device =
227 libinput_device_get_user_data(libinput_device);
228 wl_fixed_t x;
229 wl_fixed_t y;
230 uint32_t width, height;
231 uint32_t time;
232 int32_t slot;
233
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300234 if (!device->output)
235 return;
236
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100237 time = libinput_event_touch_get_time(touch_event);
238 slot = libinput_event_touch_get_seat_slot(touch_event);
239
240 width = device->output->current_mode->width;
241 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200242 x = wl_fixed_from_double(
243 libinput_event_touch_get_x_transformed(touch_event, width));
244 y = wl_fixed_from_double(
245 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100246
247 weston_output_transform_coordinate(device->output,
248 x, y, &x, &y);
249
250 notify_touch(device->seat, time, slot, x, y, touch_type);
251}
252
253static void
254handle_touch_down(struct libinput_device *device,
255 struct libinput_event_touch *touch_event)
256{
257 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
258}
259
260static void
261handle_touch_motion(struct libinput_device *device,
262 struct libinput_event_touch *touch_event)
263{
264 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
265}
266
267static void
268handle_touch_up(struct libinput_device *libinput_device,
269 struct libinput_event_touch *touch_event)
270{
271 struct evdev_device *device =
272 libinput_device_get_user_data(libinput_device);
273 uint32_t time = libinput_event_touch_get_time(touch_event);
274 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
275
276 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
277}
278
Jonas Ådahl1679f232014-04-12 09:39:51 +0200279static void
280handle_touch_frame(struct libinput_device *libinput_device,
281 struct libinput_event_touch *touch_event)
282{
283 struct evdev_device *device =
284 libinput_device_get_user_data(libinput_device);
285 struct weston_seat *seat = device->seat;
286
287 notify_touch_frame(seat);
288}
289
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100290int
291evdev_device_process_event(struct libinput_event *event)
292{
293 struct libinput_device *libinput_device =
294 libinput_event_get_device(event);
295 int handled = 1;
296
297 switch (libinput_event_get_type(event)) {
298 case LIBINPUT_EVENT_KEYBOARD_KEY:
299 handle_keyboard_key(libinput_device,
300 libinput_event_get_keyboard_event(event));
301 break;
302 case LIBINPUT_EVENT_POINTER_MOTION:
303 handle_pointer_motion(libinput_device,
304 libinput_event_get_pointer_event(event));
305 break;
306 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
307 handle_pointer_motion_absolute(
308 libinput_device,
309 libinput_event_get_pointer_event(event));
310 break;
311 case LIBINPUT_EVENT_POINTER_BUTTON:
312 handle_pointer_button(libinput_device,
313 libinput_event_get_pointer_event(event));
314 break;
315 case LIBINPUT_EVENT_POINTER_AXIS:
316 handle_pointer_axis(libinput_device,
317 libinput_event_get_pointer_event(event));
318 break;
319 case LIBINPUT_EVENT_TOUCH_DOWN:
320 handle_touch_down(libinput_device,
321 libinput_event_get_touch_event(event));
322 break;
323 case LIBINPUT_EVENT_TOUCH_MOTION:
324 handle_touch_motion(libinput_device,
325 libinput_event_get_touch_event(event));
326 break;
327 case LIBINPUT_EVENT_TOUCH_UP:
328 handle_touch_up(libinput_device,
329 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700330 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200331 case LIBINPUT_EVENT_TOUCH_FRAME:
332 handle_touch_frame(libinput_device,
333 libinput_event_get_touch_event(event));
334 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100335 default:
336 handled = 0;
337 weston_log("unknown libinput event %d\n",
338 libinput_event_get_type(event));
339 }
340
341 return handled;
342}
343
344static void
345notify_output_destroy(struct wl_listener *listener, void *data)
346{
347 struct evdev_device *device =
348 container_of(listener,
349 struct evdev_device, output_destroy_listener);
350 struct weston_compositor *c = device->seat->compositor;
351 struct weston_output *output;
352
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300353 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100354 output = container_of(c->output_list.next,
355 struct weston_output, link);
356 evdev_device_set_output(device, output);
357 } else {
358 device->output = NULL;
359 }
360}
361
Peter Hutterer3fbba492014-09-09 13:02:25 +1000362/**
363 * The WL_CALIBRATION property requires a pixel-specific matrix to be
364 * applied after scaling device coordinates to screen coordinates. libinput
365 * can't do that, so we need to convert the calibration to the normalized
366 * format libinput expects.
367 */
368static void
369evdev_device_set_calibration(struct evdev_device *device)
370{
371 struct udev *udev;
372 struct udev_device *udev_device = NULL;
373 const char *sysname = libinput_device_get_sysname(device->device);
374 const char *calibration_values;
375 uint32_t width, height;
376 float calibration[6];
377 enum libinput_config_status status;
378
379 if (!device->output)
380 return;
381
382 width = device->output->width;
383 height = device->output->height;
384 if (width == 0 || height == 0)
385 return;
386
387 /* If libinput has a pre-set calibration matrix, don't override it */
388 if (!libinput_device_config_calibration_has_matrix(device->device) ||
389 libinput_device_config_calibration_get_default_matrix(
390 device->device,
391 calibration) != 0)
392 return;
393
394 udev = udev_new();
395 if (!udev)
396 return;
397
398 udev_device = udev_device_new_from_subsystem_sysname(udev,
399 "input",
400 sysname);
401 if (!udev_device)
402 goto out;
403
404 calibration_values =
405 udev_device_get_property_value(udev_device,
406 "WL_CALIBRATION");
407
408 if (!calibration_values || sscanf(calibration_values,
409 "%f %f %f %f %f %f",
410 &calibration[0],
411 &calibration[1],
412 &calibration[2],
413 &calibration[3],
414 &calibration[4],
415 &calibration[5]) != 6)
416 goto out;
417
418 weston_log("Applying calibration: %f %f %f %f %f %f "
419 "(normalized %f %f)\n",
420 calibration[0],
421 calibration[1],
422 calibration[2],
423 calibration[3],
424 calibration[4],
425 calibration[5],
426 calibration[2] / width,
427 calibration[5] / height);
428
429 /* normalize to a format libinput can use. There is a chance of
430 this being wrong if the width/height don't match the device
431 width/height but I'm not sure how to fix that */
432 calibration[2] /= width;
433 calibration[5] /= height;
434
435 status = libinput_device_config_calibration_set_matrix(device->device,
436 calibration);
437 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
438 weston_log("Failed to apply calibration.\n");
439
440out:
441 if (udev_device)
442 udev_device_unref(udev_device);
443 udev_unref(udev);
444}
445
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100446void
447evdev_device_set_output(struct evdev_device *device,
448 struct weston_output *output)
449{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700450 if (device->output_destroy_listener.notify) {
451 wl_list_remove(&device->output_destroy_listener.link);
452 device->output_destroy_listener.notify = NULL;
453 }
454
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100455 device->output = output;
456 device->output_destroy_listener.notify = notify_output_destroy;
457 wl_signal_add(&output->destroy_signal,
458 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000459 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100460}
461
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200462static void
463configure_device(struct evdev_device *device)
464{
465 struct weston_compositor *compositor = device->seat->compositor;
466 struct weston_config_section *s;
467 int enable_tap;
468 int enable_tap_default;
469
470 s = weston_config_get_section(compositor->config,
471 "libinput", NULL, NULL);
472
473 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
474 enable_tap_default =
475 libinput_device_config_tap_get_default_enabled(
476 device->device);
477 weston_config_section_get_bool(s, "enable_tap",
478 &enable_tap,
479 enable_tap_default);
480 libinput_device_config_tap_set_enabled(device->device,
481 enable_tap);
482 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000483
484 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200485}
486
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100487struct evdev_device *
488evdev_device_create(struct libinput_device *libinput_device,
489 struct weston_seat *seat)
490{
491 struct evdev_device *device;
492
493 device = zalloc(sizeof *device);
494 if (device == NULL)
495 return NULL;
496
497 device->seat = seat;
498 wl_list_init(&device->link);
499 device->device = libinput_device;
500
501 if (libinput_device_has_capability(libinput_device,
502 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
503 weston_seat_init_keyboard(seat, NULL);
504 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
505 }
506 if (libinput_device_has_capability(libinput_device,
507 LIBINPUT_DEVICE_CAP_POINTER)) {
508 weston_seat_init_pointer(seat);
509 device->seat_caps |= EVDEV_SEAT_POINTER;
510 }
511 if (libinput_device_has_capability(libinput_device,
512 LIBINPUT_DEVICE_CAP_TOUCH)) {
513 weston_seat_init_touch(seat);
514 device->seat_caps |= EVDEV_SEAT_TOUCH;
515 }
516
517 libinput_device_set_user_data(libinput_device, device);
518 libinput_device_ref(libinput_device);
519
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200520 configure_device(device);
521
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100522 return device;
523}
524
525void
526evdev_device_destroy(struct evdev_device *device)
527{
528 if (device->seat_caps & EVDEV_SEAT_POINTER)
529 weston_seat_release_pointer(device->seat);
530 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
531 weston_seat_release_keyboard(device->seat);
532 if (device->seat_caps & EVDEV_SEAT_TOUCH)
533 weston_seat_release_touch(device->seat);
534
535 if (device->output)
536 wl_list_remove(&device->output_destroy_listener.link);
537 wl_list_remove(&device->link);
538 libinput_device_unref(device->device);
539 free(device->devnode);
540 free(device->output_name);
541 free(device);
542}
543
544void
545evdev_notify_keyboard_focus(struct weston_seat *seat,
546 struct wl_list *evdev_devices)
547{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100548 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100549
Derek Foremand621df22014-11-19 11:04:12 -0600550 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100551 return;
552
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100553 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100554 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100555 wl_array_release(&keys);
556}