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