weston: Drop priviledges early, and seteuid when needed
[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 #define _GNU_SOURCE
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <linux/input.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include "compositor.h"
33 #include "evdev.h"
34
35 struct evdev_input {
36         struct weston_input_device base;
37         struct wl_list devices_list;
38         struct udev_monitor *udev_monitor;
39         char *seat_id;
40 };
41
42 #define MAX_SLOTS 16
43
44 struct evdev_input_device {
45         struct evdev_input *master;
46         struct wl_list link;
47         struct wl_event_source *source;
48         struct weston_output *output;
49         char *devnode;
50         int fd;
51         struct {
52                 int min_x, max_x, min_y, max_y;
53                 int old_x, old_y, reset_x, reset_y;
54                 int32_t x, y;
55         } abs;
56
57         struct {
58                 int slot;
59                 int32_t x[MAX_SLOTS];
60                 int32_t y[MAX_SLOTS];
61         } mt;
62
63         struct {
64                 int dx, dy;
65         } rel;
66
67         int type; /* event type flags */
68
69         int is_touchpad, is_mt;
70 };
71
72 /* event type flags */
73 #define EVDEV_ABSOLUTE_MOTION           (1 << 0)
74 #define EVDEV_ABSOLUTE_MT_DOWN          (1 << 1)
75 #define EVDEV_ABSOLUTE_MT_MOTION        (1 << 2)
76 #define EVDEV_ABSOLUTE_MT_UP            (1 << 3)
77 #define EVDEV_RELATIVE_MOTION           (1 << 4)
78
79 static inline void
80 evdev_process_key(struct evdev_input_device *device,
81                         struct input_event *e, int time)
82 {
83         if (e->value == 2)
84                 return;
85
86         switch (e->code) {
87         case BTN_TOOL_PEN:
88         case BTN_TOOL_RUBBER:
89         case BTN_TOOL_BRUSH:
90         case BTN_TOOL_PENCIL:
91         case BTN_TOOL_AIRBRUSH:
92         case BTN_TOOL_FINGER:
93         case BTN_TOOL_MOUSE:
94         case BTN_TOOL_LENS:
95                 if (device->is_touchpad)
96                 {
97                         device->abs.reset_x = 1;
98                         device->abs.reset_y = 1;
99                 }
100                 break;
101         case BTN_TOUCH:
102                 /* Multitouch touchscreen devices might not send individually
103                  * button events each time a new finger is down. So we don't
104                  * send notification for such devices and we solve the button
105                  * case emulating on compositor side. */
106                 if (device->is_mt)
107                         break;
108
109                 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
110                  * BTN_LEFT */
111                 e->code = BTN_LEFT;
112                 /* Intentional fallthrough! */
113         case BTN_LEFT:
114         case BTN_RIGHT:
115         case BTN_MIDDLE:
116         case BTN_SIDE:
117         case BTN_EXTRA:
118         case BTN_FORWARD:
119         case BTN_BACK:
120         case BTN_TASK:
121                 notify_button(&device->master->base.input_device,
122                               time, e->code, e->value);
123                 break;
124
125         default:
126                 notify_key(&device->master->base.input_device,
127                            time, e->code, e->value);
128                 break;
129         }
130 }
131
132 static void
133 evdev_process_touch(struct evdev_input_device *device,
134                     struct input_event *e)
135 {
136         const int screen_width = device->output->current->width;
137         const int screen_height = device->output->current->height;
138
139         switch (e->code) {
140         case ABS_MT_SLOT:
141                 device->mt.slot = e->value;
142                 break;
143         case ABS_MT_TRACKING_ID:
144                 if (e->value >= 0)
145                         device->type |= EVDEV_ABSOLUTE_MT_DOWN;
146                 else
147                         device->type |= EVDEV_ABSOLUTE_MT_UP;
148                 break;
149         case ABS_MT_POSITION_X:
150                 device->mt.x[device->mt.slot] =
151                         (e->value - device->abs.min_x) * screen_width /
152                         (device->abs.max_x - device->abs.min_x) +
153                         device->output->x;
154                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
155                 break;
156         case ABS_MT_POSITION_Y:
157                 device->mt.y[device->mt.slot] =
158                         (e->value - device->abs.min_y) * screen_height /
159                         (device->abs.max_y - device->abs.min_y) +
160                         device->output->y;
161                 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
162                 break;
163         }
164 }
165
166 static inline void
167 evdev_process_absolute_motion(struct evdev_input_device *device,
168                               struct input_event *e)
169 {
170         const int screen_width = device->output->current->width;
171         const int screen_height = device->output->current->height;
172
173         switch (e->code) {
174         case ABS_X:
175                 device->abs.x =
176                         (e->value - device->abs.min_x) * screen_width /
177                         (device->abs.max_x - device->abs.min_x) +
178                         device->output->x;
179                 device->type |= EVDEV_ABSOLUTE_MOTION;
180                 break;
181         case ABS_Y:
182                 device->abs.y =
183                         (e->value - device->abs.min_y) * screen_height /
184                         (device->abs.max_y - device->abs.min_y) +
185                         device->output->y;
186                 device->type |= EVDEV_ABSOLUTE_MOTION;
187                 break;
188         }
189 }
190
191 static inline void
192 evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
193                                        struct input_event *e)
194 {
195         /* FIXME: Make this configurable somehow. */
196         const int touchpad_speed = 700;
197
198         switch (e->code) {
199         case ABS_X:
200                 e->value -= device->abs.min_x;
201                 if (device->abs.reset_x)
202                         device->abs.reset_x = 0;
203                 else {
204                         device->rel.dx =
205                                 (e->value - device->abs.old_x) *
206                                 touchpad_speed /
207                                 (device->abs.max_x - device->abs.min_x);
208                 }
209                 device->abs.old_x = e->value;
210                 device->type |= EVDEV_RELATIVE_MOTION;
211                 break;
212         case ABS_Y:
213                 e->value -= device->abs.min_y;
214                 if (device->abs.reset_y)
215                         device->abs.reset_y = 0;
216                 else {
217                         device->rel.dy =
218                                 (e->value - device->abs.old_y) *
219                                 touchpad_speed /
220                                 /* maybe use x size here to have the same scale? */
221                                 (device->abs.max_y - device->abs.min_y);
222                 }
223                 device->abs.old_y = e->value;
224                 device->type |= EVDEV_RELATIVE_MOTION;
225                 break;
226         }
227 }
228
229 static inline void
230 evdev_process_relative_motion(struct evdev_input_device *device,
231                               struct input_event *e)
232 {
233         switch (e->code) {
234         case REL_X:
235                 device->rel.dx += e->value;
236                 device->type |= EVDEV_RELATIVE_MOTION;
237                 break;
238         case REL_Y:
239                 device->rel.dy += e->value;
240                 device->type |= EVDEV_RELATIVE_MOTION;
241                 break;
242         }
243 }
244
245 static inline void
246 evdev_process_absolute(struct evdev_input_device *device,
247                        struct input_event *e)
248 {
249         if (device->is_touchpad) {
250                 evdev_process_absolute_motion_touchpad(device, e);
251         } else if (device->is_mt) {
252                 evdev_process_touch(device, e);
253         } else {
254                 evdev_process_absolute_motion(device, e);
255         }
256 }
257
258 static int
259 is_motion_event(struct input_event *e)
260 {
261         switch (e->type) {
262         case EV_REL:
263                 switch (e->code) {
264                 case REL_X:
265                 case REL_Y:
266                         return 1;
267                 }
268         case EV_ABS:
269                 switch (e->code) {
270                 case ABS_X:
271                 case ABS_Y:
272                 case ABS_MT_POSITION_X:
273                 case ABS_MT_POSITION_Y:
274                         return 1;
275                 }
276         }
277
278         return 0;
279 }
280
281 static void
282 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
283 {
284         struct wl_input_device *master = &device->master->base.input_device;
285
286         if (!device->type)
287                 return;
288
289         if (device->type & EVDEV_RELATIVE_MOTION) {
290                 notify_motion(master, time,
291                               master->x + device->rel.dx,
292                               master->y + device->rel.dy);
293                 device->type &= ~EVDEV_RELATIVE_MOTION;
294                 device->rel.dx = 0;
295                 device->rel.dy = 0;
296         }
297         if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
298                 notify_touch(master, time,
299                              device->mt.slot,
300                              device->mt.x[device->mt.slot],
301                              device->mt.y[device->mt.slot],
302                              WL_INPUT_DEVICE_TOUCH_DOWN);
303                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
304                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
305         }
306         if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
307                 notify_touch(master, time,
308                              device->mt.slot,
309                              device->mt.x[device->mt.slot],
310                              device->mt.y[device->mt.slot],
311                              WL_INPUT_DEVICE_TOUCH_MOTION);
312                 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
313                 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
314         }
315         if (device->type & EVDEV_ABSOLUTE_MT_UP) {
316                 notify_touch(master, time, device->mt.slot, 0, 0,
317                              WL_INPUT_DEVICE_TOUCH_UP);
318                 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
319         }
320         if (device->type & EVDEV_ABSOLUTE_MOTION) {
321                 notify_motion(master, time, device->abs.x, device->abs.y);
322                 device->type &= ~EVDEV_ABSOLUTE_MOTION;
323         }
324 }
325
326 static int
327 evdev_input_device_data(int fd, uint32_t mask, void *data)
328 {
329         struct weston_compositor *ec;
330         struct evdev_input_device *device = data;
331         struct input_event ev[8], *e, *end;
332         int len;
333         uint32_t time = 0;
334
335         ec = device->master->base.compositor;
336         if (!ec->focus)
337                 return 1;
338
339         len = read(fd, &ev, sizeof ev);
340         if (len < 0 || len % sizeof e[0] != 0) {
341                 /* FIXME: call device_removed when errno is ENODEV. */;
342                 return 1;
343         }
344
345         device->type = 0;
346
347         e = ev;
348         end = (void *) ev + len;
349         for (e = ev; e < end; e++) {
350                 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
351
352                 /* we try to minimize the amount of notifications to be
353                  * forwarded to the compositor, so we accumulate motion
354                  * events and send as a bunch */
355                 if (!is_motion_event(e))
356                         evdev_flush_motion(device, time);
357                 switch (e->type) {
358                 case EV_REL:
359                         evdev_process_relative_motion(device, e);
360                         break;
361                 case EV_ABS:
362                         evdev_process_absolute(device, e);
363                         break;
364                 case EV_KEY:
365                         evdev_process_key(device, e, time);
366                         break;
367                 }
368         }
369
370         evdev_flush_motion(device, time);
371
372         return 1;
373 }
374
375
376 /* copied from udev/extras/input_id/input_id.c */
377 /* we must use this kernel-compatible implementation */
378 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
379 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
380 #define OFF(x)  ((x)%BITS_PER_LONG)
381 #define BIT(x)  (1UL<<OFF(x))
382 #define LONG(x) ((x)/BITS_PER_LONG)
383 #define TEST_BIT(array, bit)    ((array[LONG(bit)] >> OFF(bit)) & 1)
384 /* end copied */
385
386 static int
387 evdev_configure_device(struct evdev_input_device *device)
388 {
389         struct input_absinfo absinfo;
390         unsigned long ev_bits[NBITS(EV_MAX)];
391         unsigned long abs_bits[NBITS(ABS_MAX)];
392         unsigned long key_bits[NBITS(KEY_MAX)];
393         int has_key, has_abs;
394
395         has_key = 0;
396         has_abs = 0;
397
398         ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
399         if (TEST_BIT(ev_bits, EV_ABS)) {
400                 has_abs = 1;
401
402                 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
403                       abs_bits);
404                 if (TEST_BIT(abs_bits, ABS_X)) {
405                         ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
406                         device->abs.min_x = absinfo.minimum;
407                         device->abs.max_x = absinfo.maximum;
408                 }
409                 if (TEST_BIT(abs_bits, ABS_Y)) {
410                         ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
411                         device->abs.min_y = absinfo.minimum;
412                         device->abs.max_y = absinfo.maximum;
413                 }
414                 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
415                         device->is_mt = 1;
416                         device->mt.slot = 0;
417                 }
418         }
419         if (TEST_BIT(ev_bits, EV_KEY)) {
420                 has_key = 1;
421                 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
422                       key_bits);
423                 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
424                              !TEST_BIT(key_bits, BTN_TOOL_PEN))
425                         device->is_touchpad = 1;
426         }
427
428         /* This rule tries to catch accelerometer devices and opt out. We may
429          * want to adjust the protocol later adding a proper event for dealing
430          * with accelerometers and implement here accordingly */
431         if (has_abs && !has_key)
432                 return -1;
433
434         return 0;
435 }
436
437 static struct evdev_input_device *
438 evdev_input_device_create(struct evdev_input *master,
439                           struct wl_display *display, const char *path)
440 {
441         struct evdev_input_device *device;
442         struct wl_event_loop *loop;
443         struct weston_compositor *ec;
444         uid_t saved_uid, uid, euid;
445
446         device = malloc(sizeof *device);
447         if (device == NULL)
448                 return NULL;
449
450         ec = master->base.compositor;
451         device->output =
452                 container_of(ec->output_list.next, struct weston_output, link);
453
454         device->master = master;
455         device->is_touchpad = 0;
456         device->is_mt = 0;
457         device->devnode = strdup(path);
458         device->mt.slot = -1;
459         device->rel.dx = 0;
460         device->rel.dy = 0;
461
462         getresuid(&uid, &euid, &saved_uid);
463         seteuid(saved_uid);
464         device->fd = open(path, O_RDONLY);
465         seteuid(euid);
466         if (device->fd < 0)
467                 goto err0;
468
469         if (evdev_configure_device(device) == -1)
470                 goto err1;
471
472         loop = wl_display_get_event_loop(display);
473         device->source = wl_event_loop_add_fd(loop, device->fd,
474                                               WL_EVENT_READABLE,
475                                               evdev_input_device_data, device);
476         if (device->source == NULL)
477                 goto err1;
478
479         wl_list_insert(master->devices_list.prev, &device->link);
480
481         return device;
482
483 err1:
484         close(device->fd);
485 err0:
486         free(device->devnode);
487         free(device);
488         return NULL;
489 }
490
491 static const char default_seat[] = "seat0";
492
493 static void
494 device_added(struct udev_device *udev_device, struct evdev_input *master)
495 {
496         struct weston_compositor *c;
497         const char *devnode;
498         const char *device_seat;
499
500         device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
501         if (!device_seat)
502                 device_seat = default_seat;
503
504         if (strcmp(device_seat, master->seat_id))
505                 return;
506
507         c = master->base.compositor;
508         devnode = udev_device_get_devnode(udev_device);
509         evdev_input_device_create(master, c->wl_display, devnode);
510 }
511
512 static void
513 device_removed(struct udev_device *udev_device, struct evdev_input *master)
514 {
515         const char *devnode = udev_device_get_devnode(udev_device);
516         struct evdev_input_device *device, *next;
517
518         wl_list_for_each_safe(device, next, &master->devices_list, link) {
519                 if (!strcmp(device->devnode, devnode)) {
520                         wl_event_source_remove(device->source);
521                         wl_list_remove(&device->link);
522                         close(device->fd);
523                         free(device->devnode);
524                         free(device);
525                         break;
526                 }
527         }
528 }
529
530 void
531 evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
532 {
533         struct evdev_input *input = (struct evdev_input *) input_base;
534         struct udev_enumerate *e;
535         struct udev_list_entry *entry;
536         struct udev_device *device;
537         const char *path;
538
539         e = udev_enumerate_new(udev);
540         udev_enumerate_add_match_subsystem(e, "input");
541         udev_enumerate_scan_devices(e);
542         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
543                 path = udev_list_entry_get_name(entry);
544                 device = udev_device_new_from_syspath(udev, path);
545
546                 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
547                         continue;
548
549                 device_added(device, input);
550
551                 udev_device_unref(device);
552         }
553         udev_enumerate_unref(e);
554
555         if (wl_list_empty(&input->devices_list)) {
556                 fprintf(stderr,
557                         "warning: no input devices on entering Weston. "
558                         "Possible causes:\n"
559                         "\t- no permissions to read /dev/input/event*\n"
560                         "\t- seats misconfigured "
561                         "(Weston backend option 'seat', "
562                         "udev device property ID_SEAT)\n");
563         }
564 }
565
566 static int
567 evdev_udev_handler(int fd, uint32_t mask, void *data)
568 {
569         struct evdev_input *master = data;
570         struct udev_device *udev_device;
571         const char *action;
572
573         udev_device = udev_monitor_receive_device(master->udev_monitor);
574         if (!udev_device)
575                 return 1;
576
577         action = udev_device_get_action(udev_device);
578         if (action) {
579                 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
580                         return 0;
581
582                 if (!strcmp(action, "add")) {
583                         device_added(udev_device, master);
584                 }
585                 else if (!strcmp(action, "remove"))
586                         device_removed(udev_device, master);
587         }
588         udev_device_unref(udev_device);
589
590         return 0;
591 }
592
593 static int
594 evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
595 {
596         struct wl_event_loop *loop;
597         struct weston_compositor *c = master->base.compositor;
598
599         master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
600         if (!master->udev_monitor) {
601                 fprintf(stderr, "udev: failed to create the udev monitor\n");
602                 return 0;
603         }
604
605         udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
606                         "input", NULL);
607
608         if (udev_monitor_enable_receiving(master->udev_monitor)) {
609                 fprintf(stderr, "udev: failed to bind the udev monitor\n");
610                 return 0;
611         }
612
613         loop = wl_display_get_event_loop(c->wl_display);
614         wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
615                              WL_EVENT_READABLE, evdev_udev_handler, master);
616
617         return 1;
618 }
619
620 void
621 evdev_input_create(struct weston_compositor *c, struct udev *udev,
622                    const char *seat)
623 {
624         struct evdev_input *input;
625
626         input = malloc(sizeof *input);
627         if (input == NULL)
628                 return;
629
630         memset(input, 0, sizeof *input);
631         weston_input_device_init(&input->base, c);
632
633         wl_list_init(&input->devices_list);
634         input->seat_id = strdup(seat);
635         if (!evdev_config_udev_monitor(udev, input)) {
636                 free(input->seat_id);
637                 free(input);
638                 return;
639         }
640
641         evdev_add_devices(udev, &input->base);
642
643         c->input_device = &input->base.input_device;
644 }
645
646 void
647 evdev_remove_devices(struct weston_input_device *input_base)
648 {
649         struct evdev_input *input = (struct evdev_input *) input_base;
650         struct evdev_input_device *device, *next;
651
652         wl_list_for_each_safe(device, next, &input->devices_list, link) {
653                 wl_event_source_remove(device->source);
654                 wl_list_remove(&device->link);
655                 close(device->fd);
656                 free(device->devnode);
657                 free(device);
658         }
659 }
660
661 void
662 evdev_input_destroy(struct weston_input_device *input_base)
663 {
664         struct evdev_input *input = (struct evdev_input *) input_base;
665
666         evdev_remove_devices(input_base);
667         wl_list_remove(&input->base.link);
668         free(input->seat_id);
669         free(input);
670 }