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