evdev: Don't add output offset in evdev_process_absolute_motion()
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <linux/input.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <mtdev.h>
31
32 #include "compositor.h"
33 #include "evdev.h"
34
35 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
36
37 void
38 evdev_led_update(struct evdev_device *device, enum weston_led leds)
39 {
40         static const struct {
41                 enum weston_led weston;
42                 int evdev;
43         } map[] = {
44                 { LED_NUM_LOCK, LED_NUML },
45                 { LED_CAPS_LOCK, LED_CAPSL },
46                 { LED_SCROLL_LOCK, LED_SCROLLL },
47         };
48         struct input_event ev[ARRAY_LENGTH(map)];
49         unsigned int i;
50
51         if (!device->caps & EVDEV_KEYBOARD)
52                 return;
53
54         memset(ev, 0, sizeof(ev));
55         for (i = 0; i < ARRAY_LENGTH(map); i++) {
56                 ev[i].type = EV_LED;
57                 ev[i].code = map[i].evdev;
58                 ev[i].value = !!(leds & map[i].weston);
59         }
60
61         i = write(device->fd, ev, sizeof ev);
62         (void)i; /* no, we really don't care about the return value */
63 }
64
65 static inline void
66 evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
67 {
68         if (e->value == 2)
69                 return;
70
71         switch (e->code) {
72         case BTN_LEFT:
73         case BTN_RIGHT:
74         case BTN_MIDDLE:
75         case BTN_SIDE:
76         case BTN_EXTRA:
77         case BTN_FORWARD:
78         case BTN_BACK:
79         case BTN_TASK:
80                 notify_button(device->seat,
81                               time, e->code,
82                               e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
83                                          WL_POINTER_BUTTON_STATE_RELEASED);
84                 break;
85
86         default:
87                 notify_key(device->seat,
88                            time, e->code,
89                            e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
90                                       WL_KEYBOARD_KEY_STATE_RELEASED,
91                            STATE_UPDATE_AUTOMATIC);
92                 break;
93         }
94 }
95
96 static void
97 evdev_process_touch(struct evdev_device *device, struct input_event *e)
98 {
99         const int screen_width = device->output->current->width;
100         const int screen_height = device->output->current->height;
101
102         switch (e->code) {
103         case ABS_MT_SLOT:
104                 device->mt.slot = e->value;
105                 break;
106         case ABS_MT_TRACKING_ID:
107                 if (e->value >= 0)
108                         device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
109                 else
110                         device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
111                 break;
112         case ABS_MT_POSITION_X:
113                 device->mt.x[device->mt.slot] =
114                         (e->value - device->abs.min_x) * screen_width /
115                         (device->abs.max_x - device->abs.min_x);
116                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
117                 break;
118         case ABS_MT_POSITION_Y:
119                 device->mt.y[device->mt.slot] =
120                         (e->value - device->abs.min_y) * screen_height /
121                         (device->abs.max_y - device->abs.min_y);
122                 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
123                 break;
124         }
125 }
126
127 static inline void
128 evdev_process_absolute_motion(struct evdev_device *device,
129                               struct input_event *e)
130 {
131         const int screen_width = device->output->current->width;
132         const int screen_height = device->output->current->height;
133
134         switch (e->code) {
135         case ABS_X:
136                 device->abs.x =
137                         (e->value - device->abs.min_x) * screen_width /
138                         (device->abs.max_x - device->abs.min_x);
139                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
140                 break;
141         case ABS_Y:
142                 device->abs.y =
143                         (e->value - device->abs.min_y) * screen_height /
144                         (device->abs.max_y - device->abs.min_y);
145                 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
146                 break;
147         }
148 }
149
150 static inline void
151 evdev_process_relative(struct evdev_device *device,
152                        struct input_event *e, uint32_t time)
153 {
154         switch (e->code) {
155         case REL_X:
156                 device->rel.dx += wl_fixed_from_int(e->value);
157                 device->pending_events |= EVDEV_RELATIVE_MOTION;
158                 break;
159         case REL_Y:
160                 device->rel.dy += wl_fixed_from_int(e->value);
161                 device->pending_events |= EVDEV_RELATIVE_MOTION;
162                 break;
163         case REL_WHEEL:
164                 switch (e->value) {
165                 case -1:
166                         /* Scroll down */
167                 case 1:
168                         /* Scroll up */
169                         notify_axis(device->seat,
170                                     time,
171                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
172                                     -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
173                         break;
174                 default:
175                         break;
176                 }
177                 break;
178         case REL_HWHEEL:
179                 switch (e->value) {
180                 case -1:
181                         /* Scroll left */
182                 case 1:
183                         /* Scroll right */
184                         notify_axis(device->seat,
185                                     time,
186                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
187                                     e->value * DEFAULT_AXIS_STEP_DISTANCE);
188                         break;
189                 default:
190                         break;
191
192                 }
193         }
194 }
195
196 static inline void
197 evdev_process_absolute(struct evdev_device *device, struct input_event *e)
198 {
199         if (device->is_mt) {
200                 evdev_process_touch(device, e);
201         } else {
202                 evdev_process_absolute_motion(device, e);
203         }
204 }
205
206 static int
207 is_motion_event(struct input_event *e)
208 {
209         switch (e->type) {
210         case EV_REL:
211                 switch (e->code) {
212                 case REL_X:
213                 case REL_Y:
214                         return 1;
215                 }
216                 break;
217         case EV_ABS:
218                 switch (e->code) {
219                 case ABS_X:
220                 case ABS_Y:
221                 case ABS_MT_POSITION_X:
222                 case ABS_MT_POSITION_Y:
223                         return 1;
224                 }
225         }
226
227         return 0;
228 }
229
230 static void
231 transform_absolute(struct evdev_device *device)
232 {
233         if (!device->abs.apply_calibration)
234                 return;
235
236         device->abs.x = device->abs.x * device->abs.calibration[0] +
237                         device->abs.y * device->abs.calibration[1] +
238                         device->abs.calibration[2];
239
240         device->abs.y = device->abs.x * device->abs.calibration[3] +
241                         device->abs.y * device->abs.calibration[4] +
242                         device->abs.calibration[5];
243 }
244
245 static void
246 evdev_flush_motion(struct evdev_device *device, uint32_t time)
247 {
248         struct weston_seat *master = device->seat;
249         wl_fixed_t x, y;
250         int slot;
251
252         if (!(device->pending_events & EVDEV_SYN))
253                 return;
254
255         slot = device->mt.slot;
256         device->pending_events &= ~EVDEV_SYN;
257         if (device->pending_events & EVDEV_RELATIVE_MOTION) {
258                 notify_motion(master, time, device->rel.dx, device->rel.dy);
259                 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
260                 device->rel.dx = 0;
261                 device->rel.dy = 0;
262         }
263         if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
264                 weston_output_transform_coordinate(device->output,
265                                                    device->mt.x[slot],
266                                                    device->mt.y[slot],
267                                                    &x, &y);
268                 notify_touch(master, time,
269                              device->mt.slot, x, y, WL_TOUCH_DOWN);
270                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
271                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
272         }
273         if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
274                 weston_output_transform_coordinate(device->output,
275                                                    device->mt.x[slot],
276                                                    device->mt.y[slot],
277                                                    &x, &y);
278                 notify_touch(master, time,
279                              device->mt.slot, x, y, WL_TOUCH_MOTION);
280                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
281                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
282         }
283         if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
284                 notify_touch(master, time, device->mt.slot, 0, 0,
285                              WL_TOUCH_UP);
286                 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
287         }
288         if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
289                 transform_absolute(device);
290                 weston_output_transform_coordinate(device->output,
291                                                    device->abs.x,
292                                                    device->abs.y, &x, &y);
293                 notify_motion_absolute(master, time, x, y);
294                 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
295         }
296 }
297
298 static void
299 fallback_process(struct evdev_dispatch *dispatch,
300                  struct evdev_device *device,
301                  struct input_event *event,
302                  uint32_t time)
303 {
304         switch (event->type) {
305         case EV_REL:
306                 evdev_process_relative(device, event, time);
307                 break;
308         case EV_ABS:
309                 evdev_process_absolute(device, event);
310                 break;
311         case EV_KEY:
312                 evdev_process_key(device, event, time);
313                 break;
314         case EV_SYN:
315                 device->pending_events |= EVDEV_SYN;
316                 break;
317         }
318 }
319
320 static void
321 fallback_destroy(struct evdev_dispatch *dispatch)
322 {
323         free(dispatch);
324 }
325
326 struct evdev_dispatch_interface fallback_interface = {
327         fallback_process,
328         fallback_destroy
329 };
330
331 static struct evdev_dispatch *
332 fallback_dispatch_create(void)
333 {
334         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
335         if (dispatch == NULL)
336                 return NULL;
337
338         dispatch->interface = &fallback_interface;
339
340         return dispatch;
341 }
342
343 static void
344 evdev_process_events(struct evdev_device *device,
345                      struct input_event *ev, int count)
346 {
347         struct evdev_dispatch *dispatch = device->dispatch;
348         struct input_event *e, *end;
349         uint32_t time = 0;
350
351         device->pending_events = 0;
352
353         e = ev;
354         end = e + count;
355         for (e = ev; e < end; e++) {
356                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
357
358                 /* we try to minimize the amount of notifications to be
359                  * forwarded to the compositor, so we accumulate motion
360                  * events and send as a bunch */
361                 if (!is_motion_event(e))
362                         evdev_flush_motion(device, time);
363
364                 dispatch->interface->process(dispatch, device, e, time);
365         }
366
367         evdev_flush_motion(device, time);
368 }
369
370 static int
371 evdev_device_data(int fd, uint32_t mask, void *data)
372 {
373         struct weston_compositor *ec;
374         struct evdev_device *device = data;
375         struct input_event ev[32];
376         int len;
377
378         ec = device->seat->compositor;
379         if (!ec->focus)
380                 return 1;
381
382         /* If the compositor is repainting, this function is called only once
383          * per frame and we have to process all the events available on the
384          * fd, otherwise there will be input lag. */
385         do {
386                 if (device->mtdev)
387                         len = mtdev_get(device->mtdev, fd, ev,
388                                         ARRAY_LENGTH(ev)) *
389                                 sizeof (struct input_event);
390                 else
391                         len = read(fd, &ev, sizeof ev);
392
393                 if (len < 0 || len % sizeof ev[0] != 0) {
394                         /* FIXME: call evdev_device_destroy when errno is ENODEV. */
395                         return 1;
396                 }
397
398                 evdev_process_events(device, ev, len / sizeof ev[0]);
399
400         } while (len > 0);
401
402         return 1;
403 }
404
405 static int
406 evdev_handle_device(struct evdev_device *device)
407 {
408         struct input_absinfo absinfo;
409         unsigned long ev_bits[NBITS(EV_MAX)];
410         unsigned long abs_bits[NBITS(ABS_MAX)];
411         unsigned long rel_bits[NBITS(REL_MAX)];
412         unsigned long key_bits[NBITS(KEY_MAX)];
413         int has_key, has_abs;
414         unsigned int i;
415
416         has_key = 0;
417         has_abs = 0;
418         device->caps = 0;
419
420         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
421         if (TEST_BIT(ev_bits, EV_ABS)) {
422                 has_abs = 1;
423
424                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
425                       abs_bits);
426                 if (TEST_BIT(abs_bits, ABS_X)) {
427                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
428                         device->abs.min_x = absinfo.minimum;
429                         device->abs.max_x = absinfo.maximum;
430                         device->caps |= EVDEV_MOTION_ABS;
431                 }
432                 if (TEST_BIT(abs_bits, ABS_Y)) {
433                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
434                         device->abs.min_y = absinfo.minimum;
435                         device->abs.max_y = absinfo.maximum;
436                         device->caps |= EVDEV_MOTION_ABS;
437                 }
438                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
439                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
440                               &absinfo);
441                         device->abs.min_x = absinfo.minimum;
442                         device->abs.max_x = absinfo.maximum;
443                         ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
444                               &absinfo);
445                         device->abs.min_y = absinfo.minimum;
446                         device->abs.max_y = absinfo.maximum;
447                         device->is_mt = 1;
448                         device->mt.slot = 0;
449                         device->caps |= EVDEV_TOUCH;
450                 }
451         }
452         if (TEST_BIT(ev_bits, EV_REL)) {
453                 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
454                       rel_bits);
455                 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
456                         device->caps |= EVDEV_MOTION_REL;
457         }
458         if (TEST_BIT(ev_bits, EV_KEY)) {
459                 has_key = 1;
460                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
461                       key_bits);
462                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
463                     !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
464                     has_abs)
465                         device->dispatch = evdev_touchpad_create(device);
466                 for (i = KEY_ESC; i < KEY_MAX; i++) {
467                         if (i >= BTN_MISC && i < KEY_OK)
468                                 continue;
469                         if (TEST_BIT(key_bits, i)) {
470                                 device->caps |= EVDEV_KEYBOARD;
471                                 break;
472                         }
473                 }
474                 for (i = BTN_MISC; i < KEY_OK; i++) {
475                         if (TEST_BIT(key_bits, i)) {
476                                 device->caps |= EVDEV_BUTTON;
477                                 break;
478                         }
479                 }
480         }
481         if (TEST_BIT(ev_bits, EV_LED)) {
482                 device->caps |= EVDEV_KEYBOARD;
483         }
484
485         /* This rule tries to catch accelerometer devices and opt out. We may
486          * want to adjust the protocol later adding a proper event for dealing
487          * with accelerometers and implement here accordingly */
488         if (has_abs && !has_key && !device->is_mt) {
489                 weston_log("input device %s, %s "
490                            "ignored: unsupported device type\n",
491                            device->devname, device->devnode);
492                 return 0;
493         }
494
495         return 1;
496 }
497
498 static int
499 evdev_configure_device(struct evdev_device *device)
500 {
501         if ((device->caps &
502              (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
503                 weston_seat_init_pointer(device->seat);
504                 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
505                            device->devname, device->devnode,
506                            device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
507                            device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
508                            device->caps & EVDEV_BUTTON ? " button" : "");
509         }
510         if ((device->caps & EVDEV_KEYBOARD)) {
511                 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
512                         return -1;
513                 weston_log("input device %s, %s is a keyboard\n",
514                            device->devname, device->devnode);
515         }
516         if ((device->caps & EVDEV_TOUCH)) {
517                 weston_seat_init_touch(device->seat);
518                 weston_log("input device %s, %s is a touch device\n",
519                            device->devname, device->devnode);
520         }
521
522         return 0;
523 }
524
525 struct evdev_device *
526 evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
527 {
528         struct evdev_device *device;
529         struct weston_compositor *ec;
530         char devname[256] = "unknown";
531
532         device = malloc(sizeof *device);
533         if (device == NULL)
534                 return NULL;
535         memset(device, 0, sizeof *device);
536
537         ec = seat->compositor;
538         device->output =
539                 container_of(ec->output_list.next, struct weston_output, link);
540
541         device->seat = seat;
542         device->is_mt = 0;
543         device->mtdev = NULL;
544         device->devnode = strdup(path);
545         device->mt.slot = -1;
546         device->rel.dx = 0;
547         device->rel.dy = 0;
548         device->dispatch = NULL;
549         device->fd = device_fd;
550
551         ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
552         device->devname = strdup(devname);
553
554         if (!evdev_handle_device(device)) {
555                 free(device->devnode);
556                 free(device->devname);
557                 free(device);
558                 return EVDEV_UNHANDLED_DEVICE;
559         }
560
561         if (evdev_configure_device(device) == -1)
562                 goto err1;
563
564         /* If the dispatch was not set up use the fallback. */
565         if (device->dispatch == NULL)
566                 device->dispatch = fallback_dispatch_create();
567         if (device->dispatch == NULL)
568                 goto err1;
569
570
571         if (device->is_mt) {
572                 device->mtdev = mtdev_new_open(device->fd);
573                 if (!device->mtdev)
574                         weston_log("mtdev failed to open for %s\n", path);
575         }
576
577         device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
578                                               WL_EVENT_READABLE,
579                                               evdev_device_data, device);
580         if (device->source == NULL)
581                 goto err2;
582
583         return device;
584
585 err2:
586         device->dispatch->interface->destroy(device->dispatch);
587 err1:
588         free(device->devname);
589         free(device->devnode);
590         free(device);
591         return NULL;
592 }
593
594 void
595 evdev_device_destroy(struct evdev_device *device)
596 {
597         struct evdev_dispatch *dispatch;
598
599         dispatch = device->dispatch;
600         if (dispatch)
601                 dispatch->interface->destroy(dispatch);
602
603         wl_event_source_remove(device->source);
604         wl_list_remove(&device->link);
605         if (device->mtdev)
606                 mtdev_close_delete(device->mtdev);
607         close(device->fd);
608         free(device->devname);
609         free(device->devnode);
610         free(device);
611 }
612
613 void
614 evdev_notify_keyboard_focus(struct weston_seat *seat,
615                             struct wl_list *evdev_devices)
616 {
617         struct evdev_device *device;
618         struct wl_array keys;
619         unsigned int i, set;
620         char evdev_keys[(KEY_CNT + 7) / 8];
621         char all_keys[(KEY_CNT + 7) / 8];
622         uint32_t *k;
623         int ret;
624
625         if (!seat->keyboard)
626                 return;
627
628         memset(all_keys, 0, sizeof all_keys);
629         wl_list_for_each(device, evdev_devices, link) {
630                 memset(evdev_keys, 0, sizeof evdev_keys);
631                 ret = ioctl(device->fd,
632                             EVIOCGKEY(sizeof evdev_keys), evdev_keys);
633                 if (ret < 0) {
634                         weston_log("failed to get keys for device %s\n",
635                                 device->devnode);
636                         continue;
637                 }
638                 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
639                         all_keys[i] |= evdev_keys[i];
640         }
641
642         wl_array_init(&keys);
643         for (i = 0; i < KEY_CNT; i++) {
644                 set = all_keys[i >> 3] & (1 << (i & 7));
645                 if (set) {
646                         k = wl_array_add(&keys, sizeof *k);
647                         *k = i;
648                 }
649         }
650
651         notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
652
653         wl_array_release(&keys);
654 }