blob: 7cc6a3571b57c6e5e17c1974224c71185609d34a [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;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100202
Peter Huttererc54f23d2015-01-13 11:55:37 +1000203 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
204 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000205 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000206 notify_axis(device->seat,
207 libinput_event_pointer_get_time(pointer_event),
208 WL_POINTER_AXIS_VERTICAL_SCROLL,
209 wl_fixed_from_double(value));
210 }
211
212 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
213 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000214 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000215 notify_axis(device->seat,
216 libinput_event_pointer_get_time(pointer_event),
217 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
218 wl_fixed_from_double(value));
219 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100220}
221
222static void
223handle_touch_with_coords(struct libinput_device *libinput_device,
224 struct libinput_event_touch *touch_event,
225 int touch_type)
226{
227 struct evdev_device *device =
228 libinput_device_get_user_data(libinput_device);
229 wl_fixed_t x;
230 wl_fixed_t y;
231 uint32_t width, height;
232 uint32_t time;
233 int32_t slot;
234
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300235 if (!device->output)
236 return;
237
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100238 time = libinput_event_touch_get_time(touch_event);
239 slot = libinput_event_touch_get_seat_slot(touch_event);
240
241 width = device->output->current_mode->width;
242 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200243 x = wl_fixed_from_double(
244 libinput_event_touch_get_x_transformed(touch_event, width));
245 y = wl_fixed_from_double(
246 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100247
248 weston_output_transform_coordinate(device->output,
249 x, y, &x, &y);
250
251 notify_touch(device->seat, time, slot, x, y, touch_type);
252}
253
254static void
255handle_touch_down(struct libinput_device *device,
256 struct libinput_event_touch *touch_event)
257{
258 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
259}
260
261static void
262handle_touch_motion(struct libinput_device *device,
263 struct libinput_event_touch *touch_event)
264{
265 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
266}
267
268static void
269handle_touch_up(struct libinput_device *libinput_device,
270 struct libinput_event_touch *touch_event)
271{
272 struct evdev_device *device =
273 libinput_device_get_user_data(libinput_device);
274 uint32_t time = libinput_event_touch_get_time(touch_event);
275 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
276
277 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
278}
279
Jonas Ådahl1679f232014-04-12 09:39:51 +0200280static void
281handle_touch_frame(struct libinput_device *libinput_device,
282 struct libinput_event_touch *touch_event)
283{
284 struct evdev_device *device =
285 libinput_device_get_user_data(libinput_device);
286 struct weston_seat *seat = device->seat;
287
288 notify_touch_frame(seat);
289}
290
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100291int
292evdev_device_process_event(struct libinput_event *event)
293{
294 struct libinput_device *libinput_device =
295 libinput_event_get_device(event);
296 int handled = 1;
297
298 switch (libinput_event_get_type(event)) {
299 case LIBINPUT_EVENT_KEYBOARD_KEY:
300 handle_keyboard_key(libinput_device,
301 libinput_event_get_keyboard_event(event));
302 break;
303 case LIBINPUT_EVENT_POINTER_MOTION:
304 handle_pointer_motion(libinput_device,
305 libinput_event_get_pointer_event(event));
306 break;
307 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
308 handle_pointer_motion_absolute(
309 libinput_device,
310 libinput_event_get_pointer_event(event));
311 break;
312 case LIBINPUT_EVENT_POINTER_BUTTON:
313 handle_pointer_button(libinput_device,
314 libinput_event_get_pointer_event(event));
315 break;
316 case LIBINPUT_EVENT_POINTER_AXIS:
317 handle_pointer_axis(libinput_device,
318 libinput_event_get_pointer_event(event));
319 break;
320 case LIBINPUT_EVENT_TOUCH_DOWN:
321 handle_touch_down(libinput_device,
322 libinput_event_get_touch_event(event));
323 break;
324 case LIBINPUT_EVENT_TOUCH_MOTION:
325 handle_touch_motion(libinput_device,
326 libinput_event_get_touch_event(event));
327 break;
328 case LIBINPUT_EVENT_TOUCH_UP:
329 handle_touch_up(libinput_device,
330 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700331 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200332 case LIBINPUT_EVENT_TOUCH_FRAME:
333 handle_touch_frame(libinput_device,
334 libinput_event_get_touch_event(event));
335 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100336 default:
337 handled = 0;
338 weston_log("unknown libinput event %d\n",
339 libinput_event_get_type(event));
340 }
341
342 return handled;
343}
344
345static void
346notify_output_destroy(struct wl_listener *listener, void *data)
347{
348 struct evdev_device *device =
349 container_of(listener,
350 struct evdev_device, output_destroy_listener);
351 struct weston_compositor *c = device->seat->compositor;
352 struct weston_output *output;
353
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300354 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100355 output = container_of(c->output_list.next,
356 struct weston_output, link);
357 evdev_device_set_output(device, output);
358 } else {
359 device->output = NULL;
360 }
361}
362
Peter Hutterer3fbba492014-09-09 13:02:25 +1000363/**
364 * The WL_CALIBRATION property requires a pixel-specific matrix to be
365 * applied after scaling device coordinates to screen coordinates. libinput
366 * can't do that, so we need to convert the calibration to the normalized
367 * format libinput expects.
368 */
369static void
370evdev_device_set_calibration(struct evdev_device *device)
371{
372 struct udev *udev;
373 struct udev_device *udev_device = NULL;
374 const char *sysname = libinput_device_get_sysname(device->device);
375 const char *calibration_values;
376 uint32_t width, height;
377 float calibration[6];
378 enum libinput_config_status status;
379
380 if (!device->output)
381 return;
382
383 width = device->output->width;
384 height = device->output->height;
385 if (width == 0 || height == 0)
386 return;
387
388 /* If libinput has a pre-set calibration matrix, don't override it */
389 if (!libinput_device_config_calibration_has_matrix(device->device) ||
390 libinput_device_config_calibration_get_default_matrix(
391 device->device,
392 calibration) != 0)
393 return;
394
395 udev = udev_new();
396 if (!udev)
397 return;
398
399 udev_device = udev_device_new_from_subsystem_sysname(udev,
400 "input",
401 sysname);
402 if (!udev_device)
403 goto out;
404
405 calibration_values =
406 udev_device_get_property_value(udev_device,
407 "WL_CALIBRATION");
408
409 if (!calibration_values || sscanf(calibration_values,
410 "%f %f %f %f %f %f",
411 &calibration[0],
412 &calibration[1],
413 &calibration[2],
414 &calibration[3],
415 &calibration[4],
416 &calibration[5]) != 6)
417 goto out;
418
419 weston_log("Applying calibration: %f %f %f %f %f %f "
420 "(normalized %f %f)\n",
421 calibration[0],
422 calibration[1],
423 calibration[2],
424 calibration[3],
425 calibration[4],
426 calibration[5],
427 calibration[2] / width,
428 calibration[5] / height);
429
430 /* normalize to a format libinput can use. There is a chance of
431 this being wrong if the width/height don't match the device
432 width/height but I'm not sure how to fix that */
433 calibration[2] /= width;
434 calibration[5] /= height;
435
436 status = libinput_device_config_calibration_set_matrix(device->device,
437 calibration);
438 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
439 weston_log("Failed to apply calibration.\n");
440
441out:
442 if (udev_device)
443 udev_device_unref(udev_device);
444 udev_unref(udev);
445}
446
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100447void
448evdev_device_set_output(struct evdev_device *device,
449 struct weston_output *output)
450{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700451 if (device->output_destroy_listener.notify) {
452 wl_list_remove(&device->output_destroy_listener.link);
453 device->output_destroy_listener.notify = NULL;
454 }
455
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100456 device->output = output;
457 device->output_destroy_listener.notify = notify_output_destroy;
458 wl_signal_add(&output->destroy_signal,
459 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000460 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100461}
462
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200463static void
464configure_device(struct evdev_device *device)
465{
466 struct weston_compositor *compositor = device->seat->compositor;
467 struct weston_config_section *s;
468 int enable_tap;
469 int enable_tap_default;
470
471 s = weston_config_get_section(compositor->config,
472 "libinput", NULL, NULL);
473
474 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
475 enable_tap_default =
476 libinput_device_config_tap_get_default_enabled(
477 device->device);
478 weston_config_section_get_bool(s, "enable_tap",
479 &enable_tap,
480 enable_tap_default);
481 libinput_device_config_tap_set_enabled(device->device,
482 enable_tap);
483 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000484
485 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200486}
487
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100488struct evdev_device *
489evdev_device_create(struct libinput_device *libinput_device,
490 struct weston_seat *seat)
491{
492 struct evdev_device *device;
493
494 device = zalloc(sizeof *device);
495 if (device == NULL)
496 return NULL;
497
498 device->seat = seat;
499 wl_list_init(&device->link);
500 device->device = libinput_device;
501
502 if (libinput_device_has_capability(libinput_device,
503 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
504 weston_seat_init_keyboard(seat, NULL);
505 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
506 }
507 if (libinput_device_has_capability(libinput_device,
508 LIBINPUT_DEVICE_CAP_POINTER)) {
509 weston_seat_init_pointer(seat);
510 device->seat_caps |= EVDEV_SEAT_POINTER;
511 }
512 if (libinput_device_has_capability(libinput_device,
513 LIBINPUT_DEVICE_CAP_TOUCH)) {
514 weston_seat_init_touch(seat);
515 device->seat_caps |= EVDEV_SEAT_TOUCH;
516 }
517
518 libinput_device_set_user_data(libinput_device, device);
519 libinput_device_ref(libinput_device);
520
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200521 configure_device(device);
522
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100523 return device;
524}
525
526void
527evdev_device_destroy(struct evdev_device *device)
528{
529 if (device->seat_caps & EVDEV_SEAT_POINTER)
530 weston_seat_release_pointer(device->seat);
531 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
532 weston_seat_release_keyboard(device->seat);
533 if (device->seat_caps & EVDEV_SEAT_TOUCH)
534 weston_seat_release_touch(device->seat);
535
536 if (device->output)
537 wl_list_remove(&device->output_destroy_listener.link);
538 wl_list_remove(&device->link);
539 libinput_device_unref(device->device);
540 free(device->devnode);
541 free(device->output_name);
542 free(device);
543}
544
545void
546evdev_notify_keyboard_focus(struct weston_seat *seat,
547 struct wl_list *evdev_devices)
548{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100549 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100550
Derek Foremand621df22014-11-19 11:04:12 -0600551 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100552 return;
553
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100554 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100555 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100556 wl_array_release(&keys);
557}