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