blob: d36c9b4e9258e2ad5d1b8fad7a08dd559a699359 [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 Vignattia52b2e42011-11-21 17:40:30 +0200207evdev_flush_motion(struct wl_input_device *device, uint32_t time,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200208 struct evdev_motion_accumulator *accum)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200209{
Kristian Høgsbergdb0fa542011-11-22 19:24:25 -0500210 if (accum->type == EVDEV_RELATIVE_MOTION)
211 notify_motion(device, time,
212 device->x + accum->dx, device->y + accum->dy);
Tiago Vignatti80885e12011-11-21 17:59:31 +0200213 if (accum->type == EVDEV_ABSOLUTE_MOTION)
214 notify_motion(device, time, accum->x, accum->y);
215
Kristian Høgsbergdb0fa542011-11-22 19:24:25 -0500216 memset(accum, 0, sizeof *accum);
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200217}
218
219static int
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400220evdev_input_device_data(int fd, uint32_t mask, void *data)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500221{
222 struct wlsc_compositor *ec;
223 struct evdev_input_device *device = data;
224 struct input_event ev[8], *e, *end;
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200225 int len;
226 struct evdev_motion_accumulator accumulator;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500227 uint32_t time;
228
229 ec = (struct wlsc_compositor *)
230 device->master->base.input_device.compositor;
231 if (!ec->focus)
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400232 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500233
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500234 len = read(fd, &ev, sizeof ev);
235 if (len < 0 || len % sizeof e[0] != 0) {
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400236 /* FIXME: call device_removed when errno is ENODEV. */;
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400237 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500238 }
239
Tiago Vignattif547bd32011-11-22 12:05:22 +0200240 memset(&accumulator, 0, sizeof accumulator);
241
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500242 e = ev;
243 end = (void *) ev + len;
244 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500245 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
246
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200247 /* we try to minimize the amount of notifications to be
248 * forwarded to the compositor, so we accumulate motion
249 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200250 if (!is_motion_event(e))
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200251 evdev_flush_motion(&device->master->base.input_device,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200252 time, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500253 switch (e->type) {
254 case EV_REL:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200255 evdev_process_relative_motion(e, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500256 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500257 case EV_ABS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300258 if (device->is_touchpad)
259 evdev_process_absolute_motion_touchpad(device,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200260 e, &accumulator);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300261 else
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200262 evdev_process_absolute_motion(device, e,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200263 &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500264 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500265 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200266 evdev_process_key(device, e, time);
267 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500268 }
269 }
270
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200271 evdev_flush_motion(&device->master->base.input_device, time,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200272 &accumulator);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400273
274 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500275}
276
Matt Peterson63900ec2011-08-01 13:46:29 -0600277
278/* copied from udev/extras/input_id/input_id.c */
279/* we must use this kernel-compatible implementation */
280#define BITS_PER_LONG (sizeof(unsigned long) * 8)
281#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
282#define OFF(x) ((x)%BITS_PER_LONG)
283#define BIT(x) (1UL<<OFF(x))
284#define LONG(x) ((x)/BITS_PER_LONG)
285#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
286/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500287
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300288static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300289evdev_configure_device(struct evdev_input_device *device)
290{
291 struct input_absinfo absinfo;
292 unsigned long ev_bits[NBITS(EV_MAX)];
293 unsigned long abs_bits[NBITS(ABS_MAX)];
294 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300295 int has_key, has_abs;
296
297 has_key = 0;
298 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300299
300 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
301 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300302 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200303
Tiago Vignattid9c82502011-08-19 15:06:20 +0300304 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
305 abs_bits);
306 if (TEST_BIT(abs_bits, ABS_X)) {
307 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200308 device->abs.min_x = absinfo.minimum;
309 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300310 }
311 if (TEST_BIT(abs_bits, ABS_Y)) {
312 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200313 device->abs.min_y = absinfo.minimum;
314 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300315 }
316 }
317 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300318 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300319 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
320 key_bits);
321 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
322 !TEST_BIT(key_bits, BTN_TOOL_PEN))
323 device->is_touchpad = 1;
324 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300325
326 /* This rule tries to catch accelerometer devices and opt out. We may
327 * want to adjust the protocol later adding a proper event for dealing
328 * with accelerometers and implement here accordingly */
329 if (has_abs && !has_key)
330 return -1;
331
332 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300333}
334
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500335static struct evdev_input_device *
336evdev_input_device_create(struct evdev_input *master,
337 struct wl_display *display, const char *path)
338{
339 struct evdev_input_device *device;
340 struct wl_event_loop *loop;
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400341 struct wlsc_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500342
343 device = malloc(sizeof *device);
344 if (device == NULL)
345 return NULL;
346
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400347 ec = (struct wlsc_compositor *) master->base.input_device.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400348 device->output =
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400349 container_of(ec->output_list.next, struct wlsc_output, link);
350
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500351 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600352 device->is_touchpad = 0;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400353 device->devnode = strdup(path);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500354
355 device->fd = open(path, O_RDONLY);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400356 if (device->fd < 0)
357 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500358
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400359 if (evdev_configure_device(device) == -1)
360 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500361
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500362 loop = wl_display_get_event_loop(display);
363 device->source = wl_event_loop_add_fd(loop, device->fd,
364 WL_EVENT_READABLE,
365 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400366 if (device->source == NULL)
367 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500368
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400369 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400370
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500371 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400372
373err1:
374 close(device->fd);
375err0:
376 free(device->devnode);
377 free(device);
378 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500379}
380
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700381static const char default_seat[] = "seat0";
382
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400383static void
384device_added(struct udev_device *udev_device, struct evdev_input *master)
385{
386 struct wlsc_compositor *c;
387 const char *devnode;
388 const char *device_seat;
389
390 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
391 if (!device_seat)
392 device_seat = default_seat;
393
394 if (strcmp(device_seat, master->seat_id))
395 return;
396
397 c = (struct wlsc_compositor *) master->base.input_device.compositor;
398 devnode = udev_device_get_devnode(udev_device);
399 if (evdev_input_device_create(master, c->wl_display, devnode))
400 fprintf(stderr, "evdev input device: added: %s\n", devnode);
401}
402
403static void
404device_removed(struct udev_device *udev_device, struct evdev_input *master)
405{
406 const char *devnode = udev_device_get_devnode(udev_device);
407 struct evdev_input_device *device, *next;
408
409 wl_list_for_each_safe(device, next, &master->devices_list, link) {
410 if (!strcmp(device->devnode, devnode)) {
411 wl_event_source_remove(device->source);
412 wl_list_remove(&device->link);
413 close(device->fd);
414 free(device->devnode);
415 free(device);
416 break;
417 }
418 }
419 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
420}
421
422static int
423evdev_udev_handler(int fd, uint32_t mask, void *data)
424{
425 struct evdev_input *master = data;
426 struct udev_device *udev_device;
427 const char *action;
428
429 udev_device = udev_monitor_receive_device(master->udev_monitor);
430 if (!udev_device)
431 return 1;
432
433 action = udev_device_get_action(udev_device);
434 if (action) {
435 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
436 return 0;
437
438 if (!strcmp(action, "add")) {
439 device_added(udev_device, master);
440 }
441 else if (!strcmp(action, "remove"))
442 device_removed(udev_device, master);
443 }
444 udev_device_unref(udev_device);
445
446 return 0;
447}
448
449static int
450evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
451{
452 struct wl_event_loop *loop;
453 struct wlsc_compositor *c =
454 (struct wlsc_compositor *) master->base.input_device.compositor;
455
456 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
457 if (!master->udev_monitor)
458 return 0;
459
460 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
461 "input", NULL);
462
463 if (udev_monitor_enable_receiving(master->udev_monitor)) {
464 fprintf(stderr, "udev: failed to bind the udev monitor\n");
465 return 0;
466 }
467
468 loop = wl_display_get_event_loop(c->wl_display);
469 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
470 WL_EVENT_READABLE, evdev_udev_handler, master);
471
472 return 1;
473}
474
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500475void
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700476evdev_input_add_devices(struct wlsc_compositor *c,
477 struct udev *udev, const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500478{
479 struct evdev_input *input;
480 struct udev_enumerate *e;
481 struct udev_list_entry *entry;
482 struct udev_device *device;
483 const char *path;
484
485 input = malloc(sizeof *input);
486 if (input == NULL)
487 return;
488
489 memset(input, 0, sizeof *input);
490 wlsc_input_device_init(&input->base, c);
491
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400492 wl_list_init(&input->devices_list);
493 input->seat_id = strdup(seat);
494 if (!evdev_config_udev_monitor(udev, input)) {
495 free(input->seat_id);
496 free(input);
497 return;
498 }
499
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500500 e = udev_enumerate_new(udev);
501 udev_enumerate_add_match_subsystem(e, "input");
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500502 udev_enumerate_scan_devices(e);
503 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
504 path = udev_list_entry_get_name(entry);
505 device = udev_device_new_from_syspath(udev, path);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700506
Kristian Høgsberg1d266032011-07-21 06:46:26 -0700507 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
508 continue;
509
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400510 device_added(device, input);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700511
512 udev_device_unref(device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500513 }
514 udev_enumerate_unref(e);
515
516 c->input_device = &input->base.input_device;
517}