evdev: use a separate structure to denote accumulated motion events
[platform/upstream/libinput.git] / compositor / 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <linux/input.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "compositor.h"
31
32 struct evdev_input {
33         struct wlsc_input_device base;
34         struct wl_list devices_list;
35         struct udev_monitor *udev_monitor;
36         char *seat_id;
37 };
38
39 struct evdev_input_device {
40         struct evdev_input *master;
41         struct wl_list link;
42         struct wl_event_source *source;
43         struct wlsc_output *output;
44         char *devnode;
45         int tool;
46         int fd;
47         struct {
48                 int min_x, max_x, min_y, max_y;
49                 int old_x, old_y, reset_x, reset_y;
50         } abs;
51         int is_touchpad;
52 };
53
54 /* event type flags */
55 #define EVDEV_ABSOLUTE_MOTION   (1 << 0)
56 #define EVDEV_RELATIVE_MOTION   (1 << 1)
57
58 struct evdev_motion_accumulator {
59         int x, y;
60         int dx, dy;
61         int type; /* event type flags */
62 };
63
64 static inline void
65 evdev_process_key(struct evdev_input_device *device,
66                         struct input_event *e, int time)
67 {
68         if (e->value == 2)
69                 return;
70
71         switch (e->code) {
72         case BTN_TOOL_PEN:
73         case BTN_TOOL_RUBBER:
74         case BTN_TOOL_BRUSH:
75         case BTN_TOOL_PENCIL:
76         case BTN_TOOL_AIRBRUSH:
77         case BTN_TOOL_FINGER:
78         case BTN_TOOL_MOUSE:
79         case BTN_TOOL_LENS:
80                 device->tool = e->value ? e->code : 0;
81                 if (device->is_touchpad)
82                 {
83                         device->abs.reset_x = 1;
84                         device->abs.reset_y = 1;
85                 }
86                 break;
87
88         case BTN_TOUCH:
89                 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
90                  * BTN_LEFT */
91                 e->code = BTN_LEFT;
92                 /* Intentional fallthrough! */
93         case BTN_LEFT:
94         case BTN_RIGHT:
95         case BTN_MIDDLE:
96         case BTN_SIDE:
97         case BTN_EXTRA:
98         case BTN_FORWARD:
99         case BTN_BACK:
100         case BTN_TASK:
101                 notify_button(&device->master->base.input_device,
102                               time, e->code, e->value);
103                 break;
104
105         default:
106                 notify_key(&device->master->base.input_device,
107                            time, e->code, e->value);
108                 break;
109         }
110 }
111
112 static inline void
113 evdev_process_absolute_motion(struct evdev_input_device *device,
114                 struct input_event *e, struct evdev_motion_accumulator *accum)
115 {
116         const int screen_width = device->output->current->width;
117         const int screen_height = device->output->current->height;
118
119         switch (e->code) {
120         case ABS_X:
121                 accum->x = (e->value - device->abs.min_x) * screen_width /
122                         (device->abs.max_x - device->abs.min_x) +
123                         device->output->x;
124                 accum->type = EVDEV_ABSOLUTE_MOTION;
125                 break;
126         case ABS_Y:
127                 accum->y = (e->value - device->abs.min_y) * screen_height /
128                         (device->abs.max_y - device->abs.min_y) +
129                         device->output->y;
130                 accum->type = EVDEV_ABSOLUTE_MOTION;
131                 break;
132         }
133 }
134
135 static inline void
136 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
137                 struct input_event *e, struct evdev_motion_accumulator *accum)
138 {
139         /* FIXME: Make this configurable somehow. */
140         const int touchpad_speed = 700;
141
142         switch (e->code) {
143         case ABS_X:
144                 e->value -= device->abs.min_x;
145                 if (device->abs.reset_x)
146                         device->abs.reset_x = 0;
147                 else {
148                         accum->dx = (e->value - device->abs.old_x) *
149                                 touchpad_speed /
150                                 (device->abs.max_x - device->abs.min_x);
151                 }
152                 device->abs.old_x = e->value;
153                 accum->type = EVDEV_RELATIVE_MOTION;
154                 break;
155         case ABS_Y:
156                 e->value -= device->abs.min_y;
157                 if (device->abs.reset_y)
158                         device->abs.reset_y = 0;
159                 else {
160                         accum->dy = (e->value - device->abs.old_y) *
161                                 touchpad_speed /
162                                 /* maybe use x size here to have the same scale? */
163                                 (device->abs.max_y - device->abs.min_y);
164                 }
165                 device->abs.old_y = e->value;
166                 accum->type = EVDEV_RELATIVE_MOTION;
167                 break;
168         }
169 }
170
171 static inline void
172 evdev_process_relative_motion(struct input_event *e,
173                               struct evdev_motion_accumulator *accum)
174 {
175         switch (e->code) {
176         case REL_X:
177                 accum->dx += e->value;
178                 accum->type = EVDEV_RELATIVE_MOTION;
179                 break;
180         case REL_Y:
181                 accum->dy += e->value;
182                 accum->type = EVDEV_RELATIVE_MOTION;
183                 break;
184         }
185 }
186
187 static int
188 is_motion_event(struct input_event *e)
189 {
190         switch (e->type) {
191         case EV_REL:
192                 switch (e->code) {
193                 case REL_X:
194                 case REL_Y:
195                         return 1;
196                 }
197         case EV_ABS:
198                 switch (e->code) {
199                 case ABS_X:
200                 case ABS_Y:
201                         return 1;
202                 }
203         }
204
205         return 0;
206 }
207
208 static void
209 evdev_flush_motion(struct wl_input_device *device, uint32_t time,
210                    struct evdev_motion_accumulator accum)
211 {
212         if (accum.type == EVDEV_RELATIVE_MOTION)
213                 notify_motion(device, time, accum.x + accum.dx,
214                               accum.y + accum.dy);
215         if (accum.type == EVDEV_ABSOLUTE_MOTION)
216                 notify_motion(device, time, accum.x, accum.y);
217 }
218
219 static int
220 evdev_input_device_data(int fd, uint32_t mask, void *data)
221 {
222         struct wlsc_compositor *ec;
223         struct evdev_input_device *device = data;
224         struct input_event ev[8], *e, *end;
225         int len;
226         struct evdev_motion_accumulator accumulator;
227         uint32_t time;
228
229         ec = (struct wlsc_compositor *)
230                 device->master->base.input_device.compositor;
231         if (!ec->focus)
232                 return 1;
233
234         accumulator.dx = 0;
235         accumulator.dy = 0;
236         accumulator.type = 0;
237         accumulator.x = device->master->base.input_device.x;
238         accumulator.y = device->master->base.input_device.y;
239
240         len = read(fd, &ev, sizeof ev);
241         if (len < 0 || len % sizeof e[0] != 0) {
242                 /* FIXME: call device_removed when errno is ENODEV. */;
243                 return 1;
244         }
245
246         e = ev;
247         end = (void *) ev + len;
248         for (e = ev; e < end; e++) {
249                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
250
251                 /* we try to minimize the amount of notifications to be
252                  * forwarded to the compositor, so we accumulate motion
253                  * events and send as a bunch */
254                 if (!is_motion_event(e)) {
255                         evdev_flush_motion(&device->master->base.input_device,
256                                            time, accumulator);
257                         accumulator.dx = 0;
258                         accumulator.dy = 0;
259                         accumulator.type = 0;
260                 }
261
262                 switch (e->type) {
263                 case EV_REL:
264                         evdev_process_relative_motion(e, &accumulator);
265                         break;
266                 case EV_ABS:
267                         if (device->is_touchpad)
268                                 evdev_process_absolute_motion_touchpad(device,
269                                         e, &accumulator);
270                         else
271                                 evdev_process_absolute_motion(device, e,
272                                         &accumulator);
273                         break;
274                 case EV_KEY:
275                         evdev_process_key(device, e, time);
276                         break;
277                 }
278         }
279
280         evdev_flush_motion(&device->master->base.input_device, time,
281                            accumulator);
282
283         return 1;
284 }
285
286
287 /* copied from udev/extras/input_id/input_id.c */
288 /* we must use this kernel-compatible implementation */
289 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
290 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
291 #define OFF(x)  ((x)%BITS_PER_LONG)
292 #define BIT(x)  (1UL<<OFF(x))
293 #define LONG(x) ((x)/BITS_PER_LONG)
294 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
295 /* end copied */
296
297 static int
298 evdev_configure_device(struct evdev_input_device *device)
299 {
300         struct input_absinfo absinfo;
301         unsigned long ev_bits[NBITS(EV_MAX)];
302         unsigned long abs_bits[NBITS(ABS_MAX)];
303         unsigned long key_bits[NBITS(KEY_MAX)];
304         int has_key, has_abs;
305
306         has_key = 0;
307         has_abs = 0;
308
309         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
310         if (TEST_BIT(ev_bits, EV_ABS)) {
311                 has_abs = 1;
312
313                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
314                       abs_bits);
315                 if (TEST_BIT(abs_bits, ABS_X)) {
316                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
317                         device->abs.min_x = absinfo.minimum;
318                         device->abs.max_x = absinfo.maximum;
319                 }
320                 if (TEST_BIT(abs_bits, ABS_Y)) {
321                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
322                         device->abs.min_y = absinfo.minimum;
323                         device->abs.max_y = absinfo.maximum;
324                 }
325         }
326         if (TEST_BIT(ev_bits, EV_KEY)) {
327                 has_key = 1;
328                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
329                       key_bits);
330                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
331                              !TEST_BIT(key_bits, BTN_TOOL_PEN))
332                         device->is_touchpad = 1;
333         }
334
335         /* This rule tries to catch accelerometer devices and opt out. We may
336          * want to adjust the protocol later adding a proper event for dealing
337          * with accelerometers and implement here accordingly */
338         if (has_abs && !has_key)
339                 return -1;
340
341         return 0;
342 }
343
344 static struct evdev_input_device *
345 evdev_input_device_create(struct evdev_input *master,
346                           struct wl_display *display, const char *path)
347 {
348         struct evdev_input_device *device;
349         struct wl_event_loop *loop;
350         struct wlsc_compositor *ec;
351
352         device = malloc(sizeof *device);
353         if (device == NULL)
354                 return NULL;
355
356         ec = (struct wlsc_compositor *) master->base.input_device.compositor;
357         device->output =
358                 container_of(ec->output_list.next, struct wlsc_output, link);
359
360         device->tool = 1;
361         device->master = master;
362         device->is_touchpad = 0;
363         device->devnode = strdup(path);
364
365         device->fd = open(path, O_RDONLY);
366         if (device->fd < 0)
367                 goto err0;
368
369         if (evdev_configure_device(device) == -1)
370                 goto err1;
371
372         loop = wl_display_get_event_loop(display);
373         device->source = wl_event_loop_add_fd(loop, device->fd,
374                                               WL_EVENT_READABLE,
375                                               evdev_input_device_data, device);
376         if (device->source == NULL)
377                 goto err1;
378
379         wl_list_insert(master->devices_list.prev, &device->link);
380
381         return device;
382
383 err1:
384         close(device->fd);
385 err0:
386         free(device->devnode);
387         free(device);
388         return NULL;
389 }
390
391 static const char default_seat[] = "seat0";
392
393 static void
394 device_added(struct udev_device *udev_device, struct evdev_input *master)
395 {
396         struct wlsc_compositor *c;
397         const char *devnode;
398         const char *device_seat;
399
400         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
401         if (!device_seat)
402                 device_seat = default_seat;
403
404         if (strcmp(device_seat, master->seat_id))
405                 return;
406
407         c = (struct wlsc_compositor *) master->base.input_device.compositor;
408         devnode = udev_device_get_devnode(udev_device);
409         if (evdev_input_device_create(master, c->wl_display, devnode))
410                 fprintf(stderr, "evdev input device: added: %s\n", devnode);
411 }
412
413 static void
414 device_removed(struct udev_device *udev_device, struct evdev_input *master)
415 {
416         const char *devnode = udev_device_get_devnode(udev_device);
417         struct evdev_input_device *device, *next;
418
419         wl_list_for_each_safe(device, next, &master->devices_list, link) {
420                 if (!strcmp(device->devnode, devnode)) {
421                         wl_event_source_remove(device->source);
422                         wl_list_remove(&device->link);
423                         close(device->fd);
424                         free(device->devnode);
425                         free(device);
426                         break;
427                 }
428         }
429         fprintf(stderr, "evdev input device: removed: %s\n", devnode);
430 }
431
432 static int
433 evdev_udev_handler(int fd, uint32_t mask, void *data)
434 {
435         struct evdev_input *master = data;
436         struct udev_device *udev_device;
437         const char *action;
438
439         udev_device = udev_monitor_receive_device(master->udev_monitor);
440         if (!udev_device)
441                 return 1;
442
443         action = udev_device_get_action(udev_device);
444         if (action) {
445                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
446                         return 0;
447
448                 if (!strcmp(action, "add")) {
449                         device_added(udev_device, master);
450                 }
451                 else if (!strcmp(action, "remove"))
452                         device_removed(udev_device, master);
453         }
454         udev_device_unref(udev_device);
455
456         return 0;
457 }
458
459 static int
460 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
461 {
462         struct wl_event_loop *loop;
463         struct wlsc_compositor *c =
464             (struct wlsc_compositor *) master->base.input_device.compositor;
465
466         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
467         if (!master->udev_monitor)
468                 return 0;
469
470         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
471                         "input", NULL);
472
473         if (udev_monitor_enable_receiving(master->udev_monitor)) {
474                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
475                 return 0;
476         }
477
478         loop = wl_display_get_event_loop(c->wl_display);
479         wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
480                              WL_EVENT_READABLE, evdev_udev_handler, master);
481
482         return 1;
483 }
484
485 void
486 evdev_input_add_devices(struct wlsc_compositor *c,
487                         struct udev *udev, const char *seat)
488 {
489         struct evdev_input *input;
490         struct udev_enumerate *e;
491         struct udev_list_entry *entry;
492         struct udev_device *device;
493         const char *path;
494
495         input = malloc(sizeof *input);
496         if (input == NULL)
497                 return;
498
499         memset(input, 0, sizeof *input);
500         wlsc_input_device_init(&input->base, c);
501
502         wl_list_init(&input->devices_list);
503         input->seat_id = strdup(seat);
504         if (!evdev_config_udev_monitor(udev, input)) {
505                 free(input->seat_id);
506                 free(input);
507                 return;
508         }
509
510         e = udev_enumerate_new(udev);
511         udev_enumerate_add_match_subsystem(e, "input");
512         udev_enumerate_scan_devices(e);
513         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
514                 path = udev_list_entry_get_name(entry);
515                 device = udev_device_new_from_syspath(udev, path);
516
517                 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
518                         continue;
519
520                 device_added(device, input);
521
522                 udev_device_unref(device);
523         }
524         udev_enumerate_unref(e);
525
526         c->input_device = &input->base.input_device;
527 }