blob: c4a861fc32202d7b98ba444041be5cac5764aaf2 [file] [log] [blame]
Kristian Høgsberg43db4012011-01-14 14:45:42 -05001/*
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
32struct evdev_input {
33 struct wlsc_input_device base;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040034 struct wl_list devices_list;
35 struct udev_monitor *udev_monitor;
36 char *seat_id;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050037};
38
39struct evdev_input_device {
40 struct evdev_input *master;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040041 struct wl_list link;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050042 struct wl_event_source *source;
Kristian Høgsberge7b5b412011-09-01 13:25:50 -040043 struct wlsc_output *output;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040044 char *devnode;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050045 int fd;
Tiago Vignattia157fc12011-11-21 16:39:55 +020046 struct {
47 int min_x, max_x, min_y, max_y;
48 int old_x, old_y, reset_x, reset_y;
49 } abs;
50 int is_touchpad;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050051};
52
Tiago Vignattia52b2e42011-11-21 17:40:30 +020053/* event type flags */
54#define EVDEV_ABSOLUTE_MOTION (1 << 0)
55#define EVDEV_RELATIVE_MOTION (1 << 1)
56
57struct evdev_motion_accumulator {
58 int x, y;
59 int dx, dy;
60 int type; /* event type flags */
61};
62
Tiago Vignatti8be003b2011-09-01 19:00:03 +030063static inline void
64evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020065 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030066{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020067 if (e->value == 2)
68 return;
69
Tiago Vignatti8be003b2011-09-01 19:00:03 +030070 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030071 case BTN_TOOL_PEN:
72 case BTN_TOOL_RUBBER:
73 case BTN_TOOL_BRUSH:
74 case BTN_TOOL_PENCIL:
75 case BTN_TOOL_AIRBRUSH:
76 case BTN_TOOL_FINGER:
77 case BTN_TOOL_MOUSE:
78 case BTN_TOOL_LENS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +030079 if (device->is_touchpad)
80 {
Tiago Vignattia157fc12011-11-21 16:39:55 +020081 device->abs.reset_x = 1;
82 device->abs.reset_y = 1;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030083 }
84 break;
85
Tiago Vignattid9043592011-09-01 19:00:05 +030086 case BTN_TOUCH:
87 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
88 * BTN_LEFT */
89 e->code = BTN_LEFT;
90 /* Intentional fallthrough! */
Tiago Vignatti8be003b2011-09-01 19:00:03 +030091 case BTN_LEFT:
92 case BTN_RIGHT:
93 case BTN_MIDDLE:
94 case BTN_SIDE:
95 case BTN_EXTRA:
96 case BTN_FORWARD:
97 case BTN_BACK:
98 case BTN_TASK:
99 notify_button(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200100 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300101 break;
102
103 default:
104 notify_key(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200105 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300106 break;
107 }
108}
109
110static inline void
111evdev_process_absolute_motion(struct evdev_input_device *device,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200112 struct input_event *e, struct evdev_motion_accumulator *accum)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300113{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400114 const int screen_width = device->output->current->width;
115 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300116
117 switch (e->code) {
118 case ABS_X:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200119 accum->x = (e->value - device->abs.min_x) * screen_width /
120 (device->abs.max_x - device->abs.min_x) +
121 device->output->x;
122 accum->type = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300123 break;
124 case ABS_Y:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200125 accum->y = (e->value - device->abs.min_y) * screen_height /
126 (device->abs.max_y - device->abs.min_y) +
127 device->output->y;
128 accum->type = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300129 break;
130 }
131}
132
133static inline void
134evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200135 struct input_event *e, struct evdev_motion_accumulator *accum)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300136{
137 /* FIXME: Make this configurable somehow. */
138 const int touchpad_speed = 700;
139
140 switch (e->code) {
141 case ABS_X:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200142 e->value -= device->abs.min_x;
143 if (device->abs.reset_x)
144 device->abs.reset_x = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300145 else {
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200146 accum->dx = (e->value - device->abs.old_x) *
147 touchpad_speed /
Tiago Vignattia157fc12011-11-21 16:39:55 +0200148 (device->abs.max_x - device->abs.min_x);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300149 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200150 device->abs.old_x = e->value;
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200151 accum->type = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300152 break;
153 case ABS_Y:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200154 e->value -= device->abs.min_y;
155 if (device->abs.reset_y)
156 device->abs.reset_y = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300157 else {
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200158 accum->dy = (e->value - device->abs.old_y) *
159 touchpad_speed /
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300160 /* maybe use x size here to have the same scale? */
Tiago Vignattia157fc12011-11-21 16:39:55 +0200161 (device->abs.max_y - device->abs.min_y);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300162 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200163 device->abs.old_y = e->value;
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200164 accum->type = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300165 break;
166 }
167}
168
169static inline void
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200170evdev_process_relative_motion(struct input_event *e,
171 struct evdev_motion_accumulator *accum)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300172{
173 switch (e->code) {
174 case REL_X:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200175 accum->dx += e->value;
176 accum->type = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300177 break;
178 case REL_Y:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200179 accum->dy += e->value;
180 accum->type = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300181 break;
182 }
183}
184
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400185static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200186is_motion_event(struct input_event *e)
187{
188 switch (e->type) {
189 case EV_REL:
190 switch (e->code) {
191 case REL_X:
192 case REL_Y:
193 return 1;
194 }
195 case EV_ABS:
196 switch (e->code) {
197 case ABS_X:
198 case ABS_Y:
199 return 1;
200 }
201 }
202
203 return 0;
204}
205
206static void
Tiago Vignatti1c2bcb12011-12-08 12:18:16 +0200207evdev_reset_accum(struct wl_input_device *device,
208 struct evdev_motion_accumulator *accum)
209{
210 memset(accum, 0, sizeof *accum);
211
212 /* There are cases where only one axis on ts devices can be sent
213 * through the bytestream whereas the other could be omitted. For
214 * this, we have to save the old value that will be forwarded without
215 * modifications to the compositor. */
216 accum->x = device->x;
217 accum->y = device->y;
218}
219
220static void
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200221evdev_flush_motion(struct wl_input_device *device, uint32_t time,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200222 struct evdev_motion_accumulator *accum)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200223{
Kristian Høgsbergdb0fa542011-11-22 19:24:25 -0500224 if (accum->type == EVDEV_RELATIVE_MOTION)
225 notify_motion(device, time,
226 device->x + accum->dx, device->y + accum->dy);
Tiago Vignatti80885e12011-11-21 17:59:31 +0200227 if (accum->type == EVDEV_ABSOLUTE_MOTION)
228 notify_motion(device, time, accum->x, accum->y);
229
Tiago Vignatti1c2bcb12011-12-08 12:18:16 +0200230 evdev_reset_accum(device, accum);
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200231}
232
233static int
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400234evdev_input_device_data(int fd, uint32_t mask, void *data)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500235{
236 struct wlsc_compositor *ec;
237 struct evdev_input_device *device = data;
238 struct input_event ev[8], *e, *end;
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200239 int len;
240 struct evdev_motion_accumulator accumulator;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500241 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500242
Kristian Høgsberga8873122011-11-23 10:39:34 -0500243 ec = device->master->base.compositor;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500244 if (!ec->focus)
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400245 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500246
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500247 len = read(fd, &ev, sizeof ev);
248 if (len < 0 || len % sizeof e[0] != 0) {
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400249 /* FIXME: call device_removed when errno is ENODEV. */;
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400250 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500251 }
252
Tiago Vignatti1c2bcb12011-12-08 12:18:16 +0200253 evdev_reset_accum(&device->master->base.input_device, &accumulator);
Tiago Vignattif547bd32011-11-22 12:05:22 +0200254
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500255 e = ev;
256 end = (void *) ev + len;
257 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500258 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
259
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200260 /* we try to minimize the amount of notifications to be
261 * forwarded to the compositor, so we accumulate motion
262 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200263 if (!is_motion_event(e))
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200264 evdev_flush_motion(&device->master->base.input_device,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200265 time, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500266 switch (e->type) {
267 case EV_REL:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200268 evdev_process_relative_motion(e, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500269 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500270 case EV_ABS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300271 if (device->is_touchpad)
272 evdev_process_absolute_motion_touchpad(device,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200273 e, &accumulator);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300274 else
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200275 evdev_process_absolute_motion(device, e,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200276 &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500277 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500278 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200279 evdev_process_key(device, e, time);
280 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500281 }
282 }
283
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500284 evdev_flush_motion(&device->master->base.input_device, time, &accumulator);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400285
286 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500287}
288
Matt Peterson63900ec2011-08-01 13:46:29 -0600289
290/* copied from udev/extras/input_id/input_id.c */
291/* we must use this kernel-compatible implementation */
292#define BITS_PER_LONG (sizeof(unsigned long) * 8)
293#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
294#define OFF(x) ((x)%BITS_PER_LONG)
295#define BIT(x) (1UL<<OFF(x))
296#define LONG(x) ((x)/BITS_PER_LONG)
297#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
298/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500299
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300300static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300301evdev_configure_device(struct evdev_input_device *device)
302{
303 struct input_absinfo absinfo;
304 unsigned long ev_bits[NBITS(EV_MAX)];
305 unsigned long abs_bits[NBITS(ABS_MAX)];
306 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300307 int has_key, has_abs;
308
309 has_key = 0;
310 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300311
312 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
313 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300314 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200315
Tiago Vignattid9c82502011-08-19 15:06:20 +0300316 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
317 abs_bits);
318 if (TEST_BIT(abs_bits, ABS_X)) {
319 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200320 device->abs.min_x = absinfo.minimum;
321 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300322 }
323 if (TEST_BIT(abs_bits, ABS_Y)) {
324 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200325 device->abs.min_y = absinfo.minimum;
326 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300327 }
328 }
329 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300330 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300331 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
332 key_bits);
333 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
334 !TEST_BIT(key_bits, BTN_TOOL_PEN))
335 device->is_touchpad = 1;
336 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300337
338 /* This rule tries to catch accelerometer devices and opt out. We may
339 * want to adjust the protocol later adding a proper event for dealing
340 * with accelerometers and implement here accordingly */
341 if (has_abs && !has_key)
342 return -1;
343
344 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300345}
346
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500347static struct evdev_input_device *
348evdev_input_device_create(struct evdev_input *master,
349 struct wl_display *display, const char *path)
350{
351 struct evdev_input_device *device;
352 struct wl_event_loop *loop;
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400353 struct wlsc_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500354
355 device = malloc(sizeof *device);
356 if (device == NULL)
357 return NULL;
358
Kristian Høgsberga8873122011-11-23 10:39:34 -0500359 ec = master->base.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400360 device->output =
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400361 container_of(ec->output_list.next, struct wlsc_output, link);
362
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500363 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600364 device->is_touchpad = 0;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400365 device->devnode = strdup(path);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500366
367 device->fd = open(path, O_RDONLY);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400368 if (device->fd < 0)
369 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500370
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400371 if (evdev_configure_device(device) == -1)
372 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500373
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500374 loop = wl_display_get_event_loop(display);
375 device->source = wl_event_loop_add_fd(loop, device->fd,
376 WL_EVENT_READABLE,
377 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400378 if (device->source == NULL)
379 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500380
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400381 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400382
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500383 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400384
385err1:
386 close(device->fd);
387err0:
388 free(device->devnode);
389 free(device);
390 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500391}
392
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700393static const char default_seat[] = "seat0";
394
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400395static void
396device_added(struct udev_device *udev_device, struct evdev_input *master)
397{
398 struct wlsc_compositor *c;
399 const char *devnode;
400 const char *device_seat;
401
402 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
403 if (!device_seat)
404 device_seat = default_seat;
405
406 if (strcmp(device_seat, master->seat_id))
407 return;
408
Kristian Høgsberga8873122011-11-23 10:39:34 -0500409 c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400410 devnode = udev_device_get_devnode(udev_device);
411 if (evdev_input_device_create(master, c->wl_display, devnode))
412 fprintf(stderr, "evdev input device: added: %s\n", devnode);
413}
414
415static void
416device_removed(struct udev_device *udev_device, struct evdev_input *master)
417{
418 const char *devnode = udev_device_get_devnode(udev_device);
419 struct evdev_input_device *device, *next;
420
421 wl_list_for_each_safe(device, next, &master->devices_list, link) {
422 if (!strcmp(device->devnode, devnode)) {
423 wl_event_source_remove(device->source);
424 wl_list_remove(&device->link);
425 close(device->fd);
426 free(device->devnode);
427 free(device);
428 break;
429 }
430 }
431 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
432}
433
434static int
435evdev_udev_handler(int fd, uint32_t mask, void *data)
436{
437 struct evdev_input *master = data;
438 struct udev_device *udev_device;
439 const char *action;
440
441 udev_device = udev_monitor_receive_device(master->udev_monitor);
442 if (!udev_device)
443 return 1;
444
445 action = udev_device_get_action(udev_device);
446 if (action) {
447 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
448 return 0;
449
450 if (!strcmp(action, "add")) {
451 device_added(udev_device, master);
452 }
453 else if (!strcmp(action, "remove"))
454 device_removed(udev_device, master);
455 }
456 udev_device_unref(udev_device);
457
458 return 0;
459}
460
461static int
462evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
463{
464 struct wl_event_loop *loop;
Kristian Høgsberga8873122011-11-23 10:39:34 -0500465 struct wlsc_compositor *c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400466
467 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
468 if (!master->udev_monitor)
469 return 0;
470
471 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
472 "input", NULL);
473
474 if (udev_monitor_enable_receiving(master->udev_monitor)) {
475 fprintf(stderr, "udev: failed to bind the udev monitor\n");
476 return 0;
477 }
478
479 loop = wl_display_get_event_loop(c->wl_display);
480 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
481 WL_EVENT_READABLE, evdev_udev_handler, master);
482
483 return 1;
484}
485
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500486void
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700487evdev_input_add_devices(struct wlsc_compositor *c,
488 struct udev *udev, const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500489{
490 struct evdev_input *input;
491 struct udev_enumerate *e;
492 struct udev_list_entry *entry;
493 struct udev_device *device;
494 const char *path;
495
496 input = malloc(sizeof *input);
497 if (input == NULL)
498 return;
499
500 memset(input, 0, sizeof *input);
501 wlsc_input_device_init(&input->base, c);
502
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400503 wl_list_init(&input->devices_list);
504 input->seat_id = strdup(seat);
505 if (!evdev_config_udev_monitor(udev, input)) {
506 free(input->seat_id);
507 free(input);
508 return;
509 }
510
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500511 e = udev_enumerate_new(udev);
512 udev_enumerate_add_match_subsystem(e, "input");
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500513 udev_enumerate_scan_devices(e);
514 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
515 path = udev_list_entry_get_name(entry);
516 device = udev_device_new_from_syspath(udev, path);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700517
Kristian Høgsberg1d266032011-07-21 06:46:26 -0700518 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
519 continue;
520
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400521 device_added(device, input);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700522
523 udev_device_unref(device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500524 }
525 udev_enumerate_unref(e);
526
527 c->input_device = &input->base.input_device;
528}