blob: 3ce74b8d87a7e4a973d15ac298010f69f1340c7b [file] [log] [blame]
Jonas Ådahle0de3c22014-03-12 22:08:42 +01001/*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include "config.h"
25
26#include <errno.h>
27#include <stdlib.h>
28#include <string.h>
29#include <linux/input.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <mtdev.h>
33#include <assert.h>
34#include <libinput.h>
35
36#include "compositor.h"
37#include "libinput-device.h"
38
39#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
40
41void
42evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
43{
44 enum libinput_led leds = 0;
45
46 if (weston_leds & LED_NUM_LOCK)
47 leds |= LIBINPUT_LED_NUM_LOCK;
48 if (weston_leds & LED_CAPS_LOCK)
49 leds |= LIBINPUT_LED_CAPS_LOCK;
50 if (weston_leds & LED_SCROLL_LOCK)
51 leds |= LIBINPUT_LED_SCROLL_LOCK;
52
53 libinput_device_led_update(device->device, leds);
54}
55
56static void
57handle_keyboard_key(struct libinput_device *libinput_device,
58 struct libinput_event_keyboard *keyboard_event)
59{
60 struct evdev_device *device =
61 libinput_device_get_user_data(libinput_device);
62
63 notify_key(device->seat,
64 libinput_event_keyboard_get_time(keyboard_event),
65 libinput_event_keyboard_get_key(keyboard_event),
66 libinput_event_keyboard_get_key_state(keyboard_event),
67 STATE_UPDATE_AUTOMATIC);
68}
69
70static void
71handle_pointer_motion(struct libinput_device *libinput_device,
72 struct libinput_event_pointer *pointer_event)
73{
74 struct evdev_device *device =
75 libinput_device_get_user_data(libinput_device);
Jonas Ådahl26714b42014-06-02 23:15:48 +020076 wl_fixed_t dx, dy;
Jonas Ådahle0de3c22014-03-12 22:08:42 +010077
Jonas Ådahl26714b42014-06-02 23:15:48 +020078 dx = wl_fixed_from_double(libinput_event_pointer_get_dx(pointer_event));
79 dy = wl_fixed_from_double(libinput_event_pointer_get_dy(pointer_event));
Jonas Ådahle0de3c22014-03-12 22:08:42 +010080 notify_motion(device->seat,
81 libinput_event_pointer_get_time(pointer_event),
Jonas Ådahl26714b42014-06-02 23:15:48 +020082 dx,
83 dy);
Jonas Ådahle0de3c22014-03-12 22:08:42 +010084}
85
86static void
87handle_pointer_motion_absolute(
88 struct libinput_device *libinput_device,
89 struct libinput_event_pointer *pointer_event)
90{
91 struct evdev_device *device =
92 libinput_device_get_user_data(libinput_device);
93 struct weston_output *output = device->output;
94 uint32_t time;
95 wl_fixed_t x, y;
96 uint32_t width, height;
97
98 if (!output)
99 return;
100
101 time = libinput_event_pointer_get_time(pointer_event);
102 width = device->output->current_mode->width;
103 height = device->output->current_mode->height;
104
Jonas Ådahl26714b42014-06-02 23:15:48 +0200105 x = wl_fixed_from_double(
106 libinput_event_pointer_get_absolute_x_transformed(pointer_event,
107 width));
108 y = wl_fixed_from_double(
109 libinput_event_pointer_get_absolute_y_transformed(pointer_event,
110 height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100111
112 weston_output_transform_coordinate(device->output, x, y, &x, &y);
113 notify_motion_absolute(device->seat, time, x, y);
114}
115
116static void
117handle_pointer_button(struct libinput_device *libinput_device,
118 struct libinput_event_pointer *pointer_event)
119{
120 struct evdev_device *device =
121 libinput_device_get_user_data(libinput_device);
122
123 notify_button(device->seat,
124 libinput_event_pointer_get_time(pointer_event),
125 libinput_event_pointer_get_button(pointer_event),
126 libinput_event_pointer_get_button_state(pointer_event));
127}
128
Peter Hutterer5dddd412015-01-15 13:14:43 +1000129static double
130normalize_scroll(struct libinput_event_pointer *pointer_event,
131 enum libinput_pointer_axis axis)
132{
133 static int warned;
134 enum libinput_pointer_axis_source source;
135 double value;
136
137 source = libinput_event_pointer_get_axis_source(pointer_event);
138 /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
139 the value is the angle of the click in degrees. To keep
140 backwards-compat with existing clients, we just send multiples of
141 the click count.
142 */
143 switch (source) {
144 case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
145 value = 10 * libinput_event_pointer_get_axis_value_discrete(
146 pointer_event,
147 axis);
148 break;
149 case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
150 case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
151 value = libinput_event_pointer_get_axis_value(pointer_event,
152 axis);
153 break;
154 default:
155 value = 0;
156 if (warned < 5) {
157 weston_log("Unknown scroll source %d. Event discarded\n",
158 source);
159 warned++;
160 }
161 break;
162 }
163
164 return value;
165}
166
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100167static void
168handle_pointer_axis(struct libinput_device *libinput_device,
169 struct libinput_event_pointer *pointer_event)
170{
171 struct evdev_device *device =
172 libinput_device_get_user_data(libinput_device);
Jonas Ådahl26714b42014-06-02 23:15:48 +0200173 double value;
Peter Huttererc54f23d2015-01-13 11:55:37 +1000174 enum libinput_pointer_axis axis;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100175
Peter Huttererc54f23d2015-01-13 11:55:37 +1000176 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
177 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000178 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000179 notify_axis(device->seat,
180 libinput_event_pointer_get_time(pointer_event),
181 WL_POINTER_AXIS_VERTICAL_SCROLL,
182 wl_fixed_from_double(value));
183 }
184
185 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
186 if (libinput_event_pointer_has_axis(pointer_event, axis)) {
Peter Hutterer5dddd412015-01-15 13:14:43 +1000187 value = normalize_scroll(pointer_event, axis);
Peter Huttererc54f23d2015-01-13 11:55:37 +1000188 notify_axis(device->seat,
189 libinput_event_pointer_get_time(pointer_event),
190 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
191 wl_fixed_from_double(value));
192 }
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100193}
194
195static void
196handle_touch_with_coords(struct libinput_device *libinput_device,
197 struct libinput_event_touch *touch_event,
198 int touch_type)
199{
200 struct evdev_device *device =
201 libinput_device_get_user_data(libinput_device);
202 wl_fixed_t x;
203 wl_fixed_t y;
204 uint32_t width, height;
205 uint32_t time;
206 int32_t slot;
207
Ander Conselvan de Oliveiraf957dfb2014-04-24 15:11:14 +0300208 if (!device->output)
209 return;
210
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100211 time = libinput_event_touch_get_time(touch_event);
212 slot = libinput_event_touch_get_seat_slot(touch_event);
213
214 width = device->output->current_mode->width;
215 height = device->output->current_mode->height;
Jonas Ådahl26714b42014-06-02 23:15:48 +0200216 x = wl_fixed_from_double(
217 libinput_event_touch_get_x_transformed(touch_event, width));
218 y = wl_fixed_from_double(
219 libinput_event_touch_get_y_transformed(touch_event, height));
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100220
221 weston_output_transform_coordinate(device->output,
222 x, y, &x, &y);
223
224 notify_touch(device->seat, time, slot, x, y, touch_type);
225}
226
227static void
228handle_touch_down(struct libinput_device *device,
229 struct libinput_event_touch *touch_event)
230{
231 handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
232}
233
234static void
235handle_touch_motion(struct libinput_device *device,
236 struct libinput_event_touch *touch_event)
237{
238 handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
239}
240
241static void
242handle_touch_up(struct libinput_device *libinput_device,
243 struct libinput_event_touch *touch_event)
244{
245 struct evdev_device *device =
246 libinput_device_get_user_data(libinput_device);
247 uint32_t time = libinput_event_touch_get_time(touch_event);
248 int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
249
250 notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
251}
252
Jonas Ådahl1679f232014-04-12 09:39:51 +0200253static void
254handle_touch_frame(struct libinput_device *libinput_device,
255 struct libinput_event_touch *touch_event)
256{
257 struct evdev_device *device =
258 libinput_device_get_user_data(libinput_device);
259 struct weston_seat *seat = device->seat;
260
261 notify_touch_frame(seat);
262}
263
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100264int
265evdev_device_process_event(struct libinput_event *event)
266{
267 struct libinput_device *libinput_device =
268 libinput_event_get_device(event);
269 int handled = 1;
270
271 switch (libinput_event_get_type(event)) {
272 case LIBINPUT_EVENT_KEYBOARD_KEY:
273 handle_keyboard_key(libinput_device,
274 libinput_event_get_keyboard_event(event));
275 break;
276 case LIBINPUT_EVENT_POINTER_MOTION:
277 handle_pointer_motion(libinput_device,
278 libinput_event_get_pointer_event(event));
279 break;
280 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
281 handle_pointer_motion_absolute(
282 libinput_device,
283 libinput_event_get_pointer_event(event));
284 break;
285 case LIBINPUT_EVENT_POINTER_BUTTON:
286 handle_pointer_button(libinput_device,
287 libinput_event_get_pointer_event(event));
288 break;
289 case LIBINPUT_EVENT_POINTER_AXIS:
290 handle_pointer_axis(libinput_device,
291 libinput_event_get_pointer_event(event));
292 break;
293 case LIBINPUT_EVENT_TOUCH_DOWN:
294 handle_touch_down(libinput_device,
295 libinput_event_get_touch_event(event));
296 break;
297 case LIBINPUT_EVENT_TOUCH_MOTION:
298 handle_touch_motion(libinput_device,
299 libinput_event_get_touch_event(event));
300 break;
301 case LIBINPUT_EVENT_TOUCH_UP:
302 handle_touch_up(libinput_device,
303 libinput_event_get_touch_event(event));
U. Artie Eoffcd9e5452014-04-17 07:53:24 -0700304 break;
Jonas Ådahl1679f232014-04-12 09:39:51 +0200305 case LIBINPUT_EVENT_TOUCH_FRAME:
306 handle_touch_frame(libinput_device,
307 libinput_event_get_touch_event(event));
308 break;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100309 default:
310 handled = 0;
311 weston_log("unknown libinput event %d\n",
312 libinput_event_get_type(event));
313 }
314
315 return handled;
316}
317
318static void
319notify_output_destroy(struct wl_listener *listener, void *data)
320{
321 struct evdev_device *device =
322 container_of(listener,
323 struct evdev_device, output_destroy_listener);
324 struct weston_compositor *c = device->seat->compositor;
325 struct weston_output *output;
326
Ander Conselvan de Oliveiraa7caef92014-04-24 15:11:17 +0300327 if (!device->output_name && !wl_list_empty(&c->output_list)) {
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100328 output = container_of(c->output_list.next,
329 struct weston_output, link);
330 evdev_device_set_output(device, output);
331 } else {
332 device->output = NULL;
333 }
334}
335
Peter Hutterer3fbba492014-09-09 13:02:25 +1000336/**
337 * The WL_CALIBRATION property requires a pixel-specific matrix to be
338 * applied after scaling device coordinates to screen coordinates. libinput
339 * can't do that, so we need to convert the calibration to the normalized
340 * format libinput expects.
341 */
342static void
343evdev_device_set_calibration(struct evdev_device *device)
344{
345 struct udev *udev;
346 struct udev_device *udev_device = NULL;
347 const char *sysname = libinput_device_get_sysname(device->device);
348 const char *calibration_values;
349 uint32_t width, height;
350 float calibration[6];
351 enum libinput_config_status status;
352
353 if (!device->output)
354 return;
355
356 width = device->output->width;
357 height = device->output->height;
358 if (width == 0 || height == 0)
359 return;
360
361 /* If libinput has a pre-set calibration matrix, don't override it */
362 if (!libinput_device_config_calibration_has_matrix(device->device) ||
363 libinput_device_config_calibration_get_default_matrix(
364 device->device,
365 calibration) != 0)
366 return;
367
368 udev = udev_new();
369 if (!udev)
370 return;
371
372 udev_device = udev_device_new_from_subsystem_sysname(udev,
373 "input",
374 sysname);
375 if (!udev_device)
376 goto out;
377
378 calibration_values =
379 udev_device_get_property_value(udev_device,
380 "WL_CALIBRATION");
381
382 if (!calibration_values || sscanf(calibration_values,
383 "%f %f %f %f %f %f",
384 &calibration[0],
385 &calibration[1],
386 &calibration[2],
387 &calibration[3],
388 &calibration[4],
389 &calibration[5]) != 6)
390 goto out;
391
392 weston_log("Applying calibration: %f %f %f %f %f %f "
393 "(normalized %f %f)\n",
394 calibration[0],
395 calibration[1],
396 calibration[2],
397 calibration[3],
398 calibration[4],
399 calibration[5],
400 calibration[2] / width,
401 calibration[5] / height);
402
403 /* normalize to a format libinput can use. There is a chance of
404 this being wrong if the width/height don't match the device
405 width/height but I'm not sure how to fix that */
406 calibration[2] /= width;
407 calibration[5] /= height;
408
409 status = libinput_device_config_calibration_set_matrix(device->device,
410 calibration);
411 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
412 weston_log("Failed to apply calibration.\n");
413
414out:
415 if (udev_device)
416 udev_device_unref(udev_device);
417 udev_unref(udev);
418}
419
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100420void
421evdev_device_set_output(struct evdev_device *device,
422 struct weston_output *output)
423{
U. Artie Eoff161c6c52014-04-17 07:53:25 -0700424 if (device->output_destroy_listener.notify) {
425 wl_list_remove(&device->output_destroy_listener.link);
426 device->output_destroy_listener.notify = NULL;
427 }
428
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100429 device->output = output;
430 device->output_destroy_listener.notify = notify_output_destroy;
431 wl_signal_add(&output->destroy_signal,
432 &device->output_destroy_listener);
Peter Hutterer3fbba492014-09-09 13:02:25 +1000433 evdev_device_set_calibration(device);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100434}
435
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200436static void
437configure_device(struct evdev_device *device)
438{
439 struct weston_compositor *compositor = device->seat->compositor;
440 struct weston_config_section *s;
441 int enable_tap;
442 int enable_tap_default;
443
444 s = weston_config_get_section(compositor->config,
445 "libinput", NULL, NULL);
446
447 if (libinput_device_config_tap_get_finger_count(device->device) > 0) {
448 enable_tap_default =
449 libinput_device_config_tap_get_default_enabled(
450 device->device);
451 weston_config_section_get_bool(s, "enable_tap",
452 &enable_tap,
453 enable_tap_default);
454 libinput_device_config_tap_set_enabled(device->device,
455 enable_tap);
456 }
Peter Hutterer3fbba492014-09-09 13:02:25 +1000457
458 evdev_device_set_calibration(device);
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200459}
460
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100461struct evdev_device *
462evdev_device_create(struct libinput_device *libinput_device,
463 struct weston_seat *seat)
464{
465 struct evdev_device *device;
466
467 device = zalloc(sizeof *device);
468 if (device == NULL)
469 return NULL;
470
471 device->seat = seat;
472 wl_list_init(&device->link);
473 device->device = libinput_device;
474
475 if (libinput_device_has_capability(libinput_device,
476 LIBINPUT_DEVICE_CAP_KEYBOARD)) {
477 weston_seat_init_keyboard(seat, NULL);
478 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
479 }
480 if (libinput_device_has_capability(libinput_device,
481 LIBINPUT_DEVICE_CAP_POINTER)) {
482 weston_seat_init_pointer(seat);
483 device->seat_caps |= EVDEV_SEAT_POINTER;
484 }
485 if (libinput_device_has_capability(libinput_device,
486 LIBINPUT_DEVICE_CAP_TOUCH)) {
487 weston_seat_init_touch(seat);
488 device->seat_caps |= EVDEV_SEAT_TOUCH;
489 }
490
491 libinput_device_set_user_data(libinput_device, device);
492 libinput_device_ref(libinput_device);
493
Jonas Ådahl05e4a1f2014-07-22 22:49:41 +0200494 configure_device(device);
495
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100496 return device;
497}
498
499void
500evdev_device_destroy(struct evdev_device *device)
501{
502 if (device->seat_caps & EVDEV_SEAT_POINTER)
503 weston_seat_release_pointer(device->seat);
504 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
505 weston_seat_release_keyboard(device->seat);
506 if (device->seat_caps & EVDEV_SEAT_TOUCH)
507 weston_seat_release_touch(device->seat);
508
509 if (device->output)
510 wl_list_remove(&device->output_destroy_listener.link);
511 wl_list_remove(&device->link);
512 libinput_device_unref(device->device);
513 free(device->devnode);
514 free(device->output_name);
515 free(device);
516}
517
518void
519evdev_notify_keyboard_focus(struct weston_seat *seat,
520 struct wl_list *evdev_devices)
521{
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100522 struct wl_array keys;
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100523
Derek Foremand621df22014-11-19 11:04:12 -0600524 if (seat->keyboard_device_count == 0)
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100525 return;
526
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100527 wl_array_init(&keys);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100528 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Jonas Ådahle0de3c22014-03-12 22:08:42 +0100529 wl_array_release(&keys);
530}