libweston: Use struct timespec for motion events
[platform/upstream/weston.git] / libweston / libinput-device.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * 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:
12  *
13  * 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.
25  */
26
27 #include "config.h"
28
29 #include <errno.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <linux/input.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <assert.h>
37 #include <libinput.h>
38
39 #include "compositor.h"
40 #include "libinput-device.h"
41 #include "shared/helpers.h"
42 #include "shared/timespec-util.h"
43
44 void
45 evdev_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
59 static void
60 handle_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);
65         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;
76
77         notify_key(device->seat,
78                    libinput_event_keyboard_get_time(keyboard_event),
79                    libinput_event_keyboard_get_key(keyboard_event),
80                    key_state, STATE_UPDATE_AUTOMATIC);
81 }
82
83 static bool
84 handle_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);
89         struct weston_pointer_motion_event event = { 0 };
90         struct timespec time;
91         double dx_unaccel, dy_unaccel;
92
93         timespec_from_usec(&time,
94                            libinput_event_pointer_get_time_usec(pointer_event));
95         dx_unaccel = libinput_event_pointer_get_dx_unaccelerated(pointer_event);
96         dy_unaccel = libinput_event_pointer_get_dy_unaccelerated(pointer_event);
97
98         event = (struct weston_pointer_motion_event) {
99                 .mask = WESTON_POINTER_MOTION_REL |
100                         WESTON_POINTER_MOTION_REL_UNACCEL,
101                 .time = time,
102                 .dx = libinput_event_pointer_get_dx(pointer_event),
103                 .dy = libinput_event_pointer_get_dy(pointer_event),
104                 .dx_unaccel = dx_unaccel,
105                 .dy_unaccel = dy_unaccel,
106         };
107
108         notify_motion(device->seat, &time, &event);
109
110         return true;
111 }
112
113 static bool
114 handle_pointer_motion_absolute(
115         struct libinput_device *libinput_device,
116         struct libinput_event_pointer *pointer_event)
117 {
118         struct evdev_device *device =
119                 libinput_device_get_user_data(libinput_device);
120         struct weston_output *output = device->output;
121         struct timespec time;
122         double x, y;
123         uint32_t width, height;
124
125         if (!output)
126                 return false;
127
128         timespec_from_usec(&time,
129                            libinput_event_pointer_get_time_usec(pointer_event));
130         width = device->output->current_mode->width;
131         height = device->output->current_mode->height;
132
133         x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
134                                                               width);
135         y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
136                                                               height);
137
138         weston_output_transform_coordinate(device->output, x, y, &x, &y);
139         notify_motion_absolute(device->seat, &time, x, y);
140
141         return true;
142 }
143
144 static bool
145 handle_pointer_button(struct libinput_device *libinput_device,
146                       struct libinput_event_pointer *pointer_event)
147 {
148         struct evdev_device *device =
149                 libinput_device_get_user_data(libinput_device);
150         int button_state =
151                 libinput_event_pointer_get_button_state(pointer_event);
152         int seat_button_count =
153                 libinput_event_pointer_get_seat_button_count(pointer_event);
154
155         /* Ignore button events that are not seat wide state changes. */
156         if ((button_state == LIBINPUT_BUTTON_STATE_PRESSED &&
157              seat_button_count != 1) ||
158             (button_state == LIBINPUT_BUTTON_STATE_RELEASED &&
159              seat_button_count != 0))
160                 return false;
161
162         notify_button(device->seat,
163                       libinput_event_pointer_get_time(pointer_event),
164                       libinput_event_pointer_get_button(pointer_event),
165                       button_state);
166
167         return true;
168 }
169
170 static double
171 normalize_scroll(struct libinput_event_pointer *pointer_event,
172                  enum libinput_pointer_axis axis)
173 {
174         enum libinput_pointer_axis_source source;
175         double value = 0.0;
176
177         source = libinput_event_pointer_get_axis_source(pointer_event);
178         /* libinput < 0.8 sent wheel click events with value 10. Since 0.8
179            the value is the angle of the click in degrees. To keep
180            backwards-compat with existing clients, we just send multiples of
181            the click count.
182          */
183         switch (source) {
184         case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
185                 value = 10 * libinput_event_pointer_get_axis_value_discrete(
186                                                                    pointer_event,
187                                                                    axis);
188                 break;
189         case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
190         case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
191                 value = libinput_event_pointer_get_axis_value(pointer_event,
192                                                               axis);
193                 break;
194         default:
195                 assert(!"unhandled event source in normalize_scroll");
196         }
197
198         return value;
199 }
200
201 static int32_t
202 get_axis_discrete(struct libinput_event_pointer *pointer_event,
203                   enum libinput_pointer_axis axis)
204 {
205         enum libinput_pointer_axis_source source;
206
207         source = libinput_event_pointer_get_axis_source(pointer_event);
208
209         if (source != LIBINPUT_POINTER_AXIS_SOURCE_WHEEL)
210                 return 0;
211
212         return libinput_event_pointer_get_axis_value_discrete(pointer_event,
213                                                               axis);
214 }
215
216 static bool
217 handle_pointer_axis(struct libinput_device *libinput_device,
218                     struct libinput_event_pointer *pointer_event)
219 {
220         static int warned;
221         struct evdev_device *device =
222                 libinput_device_get_user_data(libinput_device);
223         double vert, horiz;
224         int32_t vert_discrete, horiz_discrete;
225         enum libinput_pointer_axis axis;
226         struct weston_pointer_axis_event weston_event;
227         enum libinput_pointer_axis_source source;
228         uint32_t wl_axis_source;
229         bool has_vert, has_horiz;
230
231         has_vert = libinput_event_pointer_has_axis(pointer_event,
232                                    LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
233         has_horiz = libinput_event_pointer_has_axis(pointer_event,
234                                    LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
235
236         if (!has_vert && !has_horiz)
237                 return false;
238
239         source = libinput_event_pointer_get_axis_source(pointer_event);
240         switch (source) {
241         case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL:
242                 wl_axis_source = WL_POINTER_AXIS_SOURCE_WHEEL;
243                 break;
244         case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
245                 wl_axis_source = WL_POINTER_AXIS_SOURCE_FINGER;
246                 break;
247         case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
248                 wl_axis_source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
249                 break;
250         default:
251                 if (warned < 5) {
252                         weston_log("Unknown scroll source %d.\n", source);
253                         warned++;
254                 }
255                 return false;
256         }
257
258         notify_axis_source(device->seat, wl_axis_source);
259
260         if (has_vert) {
261                 axis = LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL;
262                 vert_discrete = get_axis_discrete(pointer_event, axis);
263                 vert = normalize_scroll(pointer_event, axis);
264
265                 weston_event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
266                 weston_event.value = vert;
267                 weston_event.discrete = vert_discrete;
268                 weston_event.has_discrete = (vert_discrete != 0);
269
270                 notify_axis(device->seat,
271                             libinput_event_pointer_get_time(pointer_event),
272                             &weston_event);
273         }
274
275         if (has_horiz) {
276                 axis = LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL;
277                 horiz_discrete = get_axis_discrete(pointer_event, axis);
278                 horiz = normalize_scroll(pointer_event, axis);
279
280                 weston_event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
281                 weston_event.value = horiz;
282                 weston_event.discrete = horiz_discrete;
283                 weston_event.has_discrete = (horiz_discrete != 0);
284
285                 notify_axis(device->seat,
286                             libinput_event_pointer_get_time(pointer_event),
287                             &weston_event);
288         }
289
290         return true;
291 }
292
293 static void
294 handle_touch_with_coords(struct libinput_device *libinput_device,
295                          struct libinput_event_touch *touch_event,
296                          int touch_type)
297 {
298         struct evdev_device *device =
299                 libinput_device_get_user_data(libinput_device);
300         double x;
301         double y;
302         uint32_t width, height;
303         uint32_t time;
304         int32_t slot;
305
306         if (!device->output)
307                 return;
308
309         time = libinput_event_touch_get_time(touch_event);
310         slot = libinput_event_touch_get_seat_slot(touch_event);
311
312         width = device->output->current_mode->width;
313         height = device->output->current_mode->height;
314         x =  libinput_event_touch_get_x_transformed(touch_event, width);
315         y =  libinput_event_touch_get_y_transformed(touch_event, height);
316
317         weston_output_transform_coordinate(device->output,
318                                            x, y, &x, &y);
319
320         notify_touch(device->seat, time, slot, x, y, touch_type);
321 }
322
323 static void
324 handle_touch_down(struct libinput_device *device,
325                   struct libinput_event_touch *touch_event)
326 {
327         handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
328 }
329
330 static void
331 handle_touch_motion(struct libinput_device *device,
332                     struct libinput_event_touch *touch_event)
333 {
334         handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
335 }
336
337 static void
338 handle_touch_up(struct libinput_device *libinput_device,
339                 struct libinput_event_touch *touch_event)
340 {
341         struct evdev_device *device =
342                 libinput_device_get_user_data(libinput_device);
343         uint32_t time = libinput_event_touch_get_time(touch_event);
344         int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
345
346         notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
347 }
348
349 static void
350 handle_touch_frame(struct libinput_device *libinput_device,
351                    struct libinput_event_touch *touch_event)
352 {
353         struct evdev_device *device =
354                 libinput_device_get_user_data(libinput_device);
355         struct weston_seat *seat = device->seat;
356
357         notify_touch_frame(seat);
358 }
359
360 int
361 evdev_device_process_event(struct libinput_event *event)
362 {
363         struct libinput_device *libinput_device =
364                 libinput_event_get_device(event);
365         struct evdev_device *device =
366                 libinput_device_get_user_data(libinput_device);
367         int handled = 1;
368         bool need_frame = false;
369
370         switch (libinput_event_get_type(event)) {
371         case LIBINPUT_EVENT_KEYBOARD_KEY:
372                 handle_keyboard_key(libinput_device,
373                                     libinput_event_get_keyboard_event(event));
374                 break;
375         case LIBINPUT_EVENT_POINTER_MOTION:
376                 need_frame = handle_pointer_motion(libinput_device,
377                                       libinput_event_get_pointer_event(event));
378                 break;
379         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
380                 need_frame = handle_pointer_motion_absolute(
381                                 libinput_device,
382                                 libinput_event_get_pointer_event(event));
383                 break;
384         case LIBINPUT_EVENT_POINTER_BUTTON:
385                 need_frame = handle_pointer_button(libinput_device,
386                                       libinput_event_get_pointer_event(event));
387                 break;
388         case LIBINPUT_EVENT_POINTER_AXIS:
389                 need_frame = handle_pointer_axis(
390                                  libinput_device,
391                                  libinput_event_get_pointer_event(event));
392                 break;
393         case LIBINPUT_EVENT_TOUCH_DOWN:
394                 handle_touch_down(libinput_device,
395                                   libinput_event_get_touch_event(event));
396                 break;
397         case LIBINPUT_EVENT_TOUCH_MOTION:
398                 handle_touch_motion(libinput_device,
399                                     libinput_event_get_touch_event(event));
400                 break;
401         case LIBINPUT_EVENT_TOUCH_UP:
402                 handle_touch_up(libinput_device,
403                                 libinput_event_get_touch_event(event));
404                 break;
405         case LIBINPUT_EVENT_TOUCH_FRAME:
406                 handle_touch_frame(libinput_device,
407                                    libinput_event_get_touch_event(event));
408                 break;
409         default:
410                 handled = 0;
411                 weston_log("unknown libinput event %d\n",
412                            libinput_event_get_type(event));
413         }
414
415         if (need_frame)
416                 notify_pointer_frame(device->seat);
417
418         return handled;
419 }
420
421 static void
422 notify_output_destroy(struct wl_listener *listener, void *data)
423 {
424         struct evdev_device *device =
425                 container_of(listener,
426                              struct evdev_device, output_destroy_listener);
427         struct weston_compositor *c = device->seat->compositor;
428         struct weston_output *output;
429
430         if (!device->output_name && !wl_list_empty(&c->output_list)) {
431                 output = container_of(c->output_list.next,
432                                       struct weston_output, link);
433                 evdev_device_set_output(device, output);
434         } else {
435                 device->output = NULL;
436         }
437 }
438
439 /**
440  * The WL_CALIBRATION property requires a pixel-specific matrix to be
441  * applied after scaling device coordinates to screen coordinates. libinput
442  * can't do that, so we need to convert the calibration to the normalized
443  * format libinput expects.
444  */
445 void
446 evdev_device_set_calibration(struct evdev_device *device)
447 {
448         struct udev *udev;
449         struct udev_device *udev_device = NULL;
450         const char *sysname = libinput_device_get_sysname(device->device);
451         const char *calibration_values;
452         uint32_t width, height;
453         float calibration[6];
454         enum libinput_config_status status;
455
456         if (!device->output)
457                 return;
458
459         width = device->output->width;
460         height = device->output->height;
461         if (width == 0 || height == 0)
462                 return;
463
464         /* If libinput has a pre-set calibration matrix, don't override it */
465         if (!libinput_device_config_calibration_has_matrix(device->device) ||
466             libinput_device_config_calibration_get_default_matrix(
467                                                           device->device,
468                                                           calibration) != 0)
469                 return;
470
471         udev = udev_new();
472         if (!udev)
473                 return;
474
475         udev_device = udev_device_new_from_subsystem_sysname(udev,
476                                                              "input",
477                                                              sysname);
478         if (!udev_device)
479                 goto out;
480
481         calibration_values =
482                 udev_device_get_property_value(udev_device,
483                                                "WL_CALIBRATION");
484
485         if (!calibration_values || sscanf(calibration_values,
486                                           "%f %f %f %f %f %f",
487                                           &calibration[0],
488                                           &calibration[1],
489                                           &calibration[2],
490                                           &calibration[3],
491                                           &calibration[4],
492                                           &calibration[5]) != 6)
493                 goto out;
494
495         weston_log("Applying calibration: %f %f %f %f %f %f "
496                    "(normalized %f %f)\n",
497                     calibration[0],
498                     calibration[1],
499                     calibration[2],
500                     calibration[3],
501                     calibration[4],
502                     calibration[5],
503                     calibration[2] / width,
504                     calibration[5] / height);
505
506         /* normalize to a format libinput can use. There is a chance of
507            this being wrong if the width/height don't match the device
508            width/height but I'm not sure how to fix that */
509         calibration[2] /= width;
510         calibration[5] /= height;
511
512         status = libinput_device_config_calibration_set_matrix(device->device,
513                                                                calibration);
514         if (status != LIBINPUT_CONFIG_STATUS_SUCCESS)
515                 weston_log("Failed to apply calibration.\n");
516
517 out:
518         if (udev_device)
519                 udev_device_unref(udev_device);
520         udev_unref(udev);
521 }
522
523 void
524 evdev_device_set_output(struct evdev_device *device,
525                         struct weston_output *output)
526 {
527         if (device->output_destroy_listener.notify) {
528                 wl_list_remove(&device->output_destroy_listener.link);
529                 device->output_destroy_listener.notify = NULL;
530         }
531
532         device->output = output;
533         device->output_destroy_listener.notify = notify_output_destroy;
534         wl_signal_add(&output->destroy_signal,
535                       &device->output_destroy_listener);
536         evdev_device_set_calibration(device);
537 }
538
539 struct evdev_device *
540 evdev_device_create(struct libinput_device *libinput_device,
541                     struct weston_seat *seat)
542 {
543         struct evdev_device *device;
544
545         device = zalloc(sizeof *device);
546         if (device == NULL)
547                 return NULL;
548
549         device->seat = seat;
550         wl_list_init(&device->link);
551         device->device = libinput_device;
552
553         if (libinput_device_has_capability(libinput_device,
554                                            LIBINPUT_DEVICE_CAP_KEYBOARD)) {
555                 weston_seat_init_keyboard(seat, NULL);
556                 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
557         }
558         if (libinput_device_has_capability(libinput_device,
559                                            LIBINPUT_DEVICE_CAP_POINTER)) {
560                 weston_seat_init_pointer(seat);
561                 device->seat_caps |= EVDEV_SEAT_POINTER;
562         }
563         if (libinput_device_has_capability(libinput_device,
564                                            LIBINPUT_DEVICE_CAP_TOUCH)) {
565                 weston_seat_init_touch(seat);
566                 device->seat_caps |= EVDEV_SEAT_TOUCH;
567         }
568
569         libinput_device_set_user_data(libinput_device, device);
570         libinput_device_ref(libinput_device);
571
572         return device;
573 }
574
575 void
576 evdev_device_destroy(struct evdev_device *device)
577 {
578         if (device->seat_caps & EVDEV_SEAT_POINTER)
579                 weston_seat_release_pointer(device->seat);
580         if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
581                 weston_seat_release_keyboard(device->seat);
582         if (device->seat_caps & EVDEV_SEAT_TOUCH)
583                 weston_seat_release_touch(device->seat);
584
585         if (device->output)
586                 wl_list_remove(&device->output_destroy_listener.link);
587         wl_list_remove(&device->link);
588         libinput_device_unref(device->device);
589         free(device->devnode);
590         free(device->output_name);
591         free(device);
592 }
593
594 void
595 evdev_notify_keyboard_focus(struct weston_seat *seat,
596                             struct wl_list *evdev_devices)
597 {
598         struct wl_array keys;
599
600         if (seat->keyboard_device_count == 0)
601                 return;
602
603         wl_array_init(&keys);
604         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
605         wl_array_release(&keys);
606 }