blob: 48e8d7ccae332e896320dbbad23b3141e4bb3091 [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;
Tiago Vignattid9f7d1f2011-10-28 13:09:42 -040045 int tool;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050046 int fd;
Tiago Vignattia157fc12011-11-21 16:39:55 +020047 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;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050052};
53
Tiago Vignatti8be003b2011-09-01 19:00:03 +030054static inline void
55evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020056 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030057{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020058 if (e->value == 2)
59 return;
60
Tiago Vignatti8be003b2011-09-01 19:00:03 +030061 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030062 case BTN_TOOL_PEN:
63 case BTN_TOOL_RUBBER:
64 case BTN_TOOL_BRUSH:
65 case BTN_TOOL_PENCIL:
66 case BTN_TOOL_AIRBRUSH:
67 case BTN_TOOL_FINGER:
68 case BTN_TOOL_MOUSE:
69 case BTN_TOOL_LENS:
Tiago Vignatti8755ff92011-11-10 14:47:30 +020070 device->tool = e->value ? e->code : 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030071 if (device->is_touchpad)
72 {
Tiago Vignattia157fc12011-11-21 16:39:55 +020073 device->abs.reset_x = 1;
74 device->abs.reset_y = 1;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030075 }
76 break;
77
Tiago Vignattid9043592011-09-01 19:00:05 +030078 case BTN_TOUCH:
79 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
80 * BTN_LEFT */
81 e->code = BTN_LEFT;
82 /* Intentional fallthrough! */
Tiago Vignatti8be003b2011-09-01 19:00:03 +030083 case BTN_LEFT:
84 case BTN_RIGHT:
85 case BTN_MIDDLE:
86 case BTN_SIDE:
87 case BTN_EXTRA:
88 case BTN_FORWARD:
89 case BTN_BACK:
90 case BTN_TASK:
91 notify_button(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020092 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030093 break;
94
95 default:
96 notify_key(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020097 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030098 break;
99 }
100}
101
102static inline void
103evdev_process_absolute_motion(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200104 struct input_event *e, int *x, int *y,
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400105 int *absolute_event)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300106{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400107 const int screen_width = device->output->current->width;
108 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300109
110 switch (e->code) {
111 case ABS_X:
112 *absolute_event = device->tool;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200113 *x = (e->value - device->abs.min_x) * screen_width /
114 (device->abs.max_x - device->abs.min_x) + device->output->x;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300115 break;
116 case ABS_Y:
117 *absolute_event = device->tool;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200118 *y = (e->value - device->abs.min_y) * screen_height /
119 (device->abs.max_y - device->abs.min_y) + device->output->y;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300120 break;
121 }
122}
123
124static inline void
125evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200126 struct input_event *e, int *dx, int *dy)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300127{
128 /* FIXME: Make this configurable somehow. */
129 const int touchpad_speed = 700;
130
131 switch (e->code) {
132 case ABS_X:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200133 e->value -= device->abs.min_x;
134 if (device->abs.reset_x)
135 device->abs.reset_x = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300136 else {
Tiago Vignattia157fc12011-11-21 16:39:55 +0200137 *dx = (e->value - device->abs.old_x) * touchpad_speed /
138 (device->abs.max_x - device->abs.min_x);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300139 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200140 device->abs.old_x = e->value;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300141 break;
142 case ABS_Y:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200143 e->value -= device->abs.min_y;
144 if (device->abs.reset_y)
145 device->abs.reset_y = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300146 else {
Tiago Vignattia157fc12011-11-21 16:39:55 +0200147 *dy = (e->value - device->abs.old_y) * touchpad_speed /
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300148 /* maybe use x size here to have the same scale? */
Tiago Vignattia157fc12011-11-21 16:39:55 +0200149 (device->abs.max_y - device->abs.min_y);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300150 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200151 device->abs.old_y = e->value;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300152 break;
153 }
154}
155
156static inline void
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200157evdev_process_relative_motion(struct input_event *e, int *dx, int *dy)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300158{
159 switch (e->code) {
160 case REL_X:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200161 *dx += e->value;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300162 break;
163 case REL_Y:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200164 *dy += e->value;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300165 break;
166 }
167}
168
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400169static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200170is_motion_event(struct input_event *e)
171{
172 switch (e->type) {
173 case EV_REL:
174 switch (e->code) {
175 case REL_X:
176 case REL_Y:
177 return 1;
178 }
179 case EV_ABS:
180 switch (e->code) {
181 case ABS_X:
182 case ABS_Y:
183 return 1;
184 }
185 }
186
187 return 0;
188}
189
190static void
191evdev_flush_motion(struct wl_input_device *device, uint32_t time, int x, int y,
192 int dx, int dy, int absolute_event)
193{
194 if (dx != 0 || dy != 0)
195 notify_motion(device, time, x + dx, y + dy);
196 if (absolute_event)
197 notify_motion(device, time, x, y);
198}
199
200static int
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400201evdev_input_device_data(int fd, uint32_t mask, void *data)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500202{
203 struct wlsc_compositor *ec;
204 struct evdev_input_device *device = data;
205 struct input_event ev[8], *e, *end;
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200206 int len, dx, dy, absolute_event;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500207 int x, y;
208 uint32_t time;
209
210 ec = (struct wlsc_compositor *)
211 device->master->base.input_device.compositor;
212 if (!ec->focus)
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400213 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500214
215 dx = 0;
216 dy = 0;
217 absolute_event = 0;
218 x = device->master->base.input_device.x;
219 y = device->master->base.input_device.y;
220
221 len = read(fd, &ev, sizeof ev);
222 if (len < 0 || len % sizeof e[0] != 0) {
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400223 /* FIXME: call device_removed when errno is ENODEV. */;
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400224 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500225 }
226
227 e = ev;
228 end = (void *) ev + len;
229 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500230 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
231
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200232 /* we try to minimize the amount of notifications to be
233 * forwarded to the compositor, so we accumulate motion
234 * events and send as a bunch */
Kristian Høgsberg8b568802011-11-18 10:42:34 -0500235 if (!is_motion_event(e)) {
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200236 evdev_flush_motion(&device->master->base.input_device,
237 time, x, y, dx, dy, absolute_event);
Kristian Høgsberg8b568802011-11-18 10:42:34 -0500238 dx = 0;
239 dy = 0;
240 absolute_event = 0;
241 }
242
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500243 switch (e->type) {
244 case EV_REL:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200245 evdev_process_relative_motion(e, &dx, &dy);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500246 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500247 case EV_ABS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300248 if (device->is_touchpad)
249 evdev_process_absolute_motion_touchpad(device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200250 e, &dx, &dy);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300251 else
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200252 evdev_process_absolute_motion(device, e,
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400253 &x, &y, &absolute_event);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500254 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500255 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200256 evdev_process_key(device, e, time);
257 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500258 }
259 }
260
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200261 evdev_flush_motion(&device->master->base.input_device, time, x, y, dx,
262 dy, absolute_event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400263
264 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500265}
266
Matt Peterson63900ec2011-08-01 13:46:29 -0600267
268/* copied from udev/extras/input_id/input_id.c */
269/* we must use this kernel-compatible implementation */
270#define BITS_PER_LONG (sizeof(unsigned long) * 8)
271#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
272#define OFF(x) ((x)%BITS_PER_LONG)
273#define BIT(x) (1UL<<OFF(x))
274#define LONG(x) ((x)/BITS_PER_LONG)
275#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
276/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500277
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300278static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300279evdev_configure_device(struct evdev_input_device *device)
280{
281 struct input_absinfo absinfo;
282 unsigned long ev_bits[NBITS(EV_MAX)];
283 unsigned long abs_bits[NBITS(ABS_MAX)];
284 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300285 int has_key, has_abs;
286
287 has_key = 0;
288 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300289
290 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
291 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300292 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200293
Tiago Vignattid9c82502011-08-19 15:06:20 +0300294 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
295 abs_bits);
296 if (TEST_BIT(abs_bits, ABS_X)) {
297 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200298 device->abs.min_x = absinfo.minimum;
299 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300300 }
301 if (TEST_BIT(abs_bits, ABS_Y)) {
302 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200303 device->abs.min_y = absinfo.minimum;
304 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300305 }
306 }
307 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300308 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300309 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
310 key_bits);
311 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
312 !TEST_BIT(key_bits, BTN_TOOL_PEN))
313 device->is_touchpad = 1;
314 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300315
316 /* This rule tries to catch accelerometer devices and opt out. We may
317 * want to adjust the protocol later adding a proper event for dealing
318 * with accelerometers and implement here accordingly */
319 if (has_abs && !has_key)
320 return -1;
321
322 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300323}
324
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500325static struct evdev_input_device *
326evdev_input_device_create(struct evdev_input *master,
327 struct wl_display *display, const char *path)
328{
329 struct evdev_input_device *device;
330 struct wl_event_loop *loop;
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400331 struct wlsc_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500332
333 device = malloc(sizeof *device);
334 if (device == NULL)
335 return NULL;
336
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400337 ec = (struct wlsc_compositor *) master->base.input_device.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400338 device->output =
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400339 container_of(ec->output_list.next, struct wlsc_output, link);
340
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500341 device->tool = 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500342 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600343 device->is_touchpad = 0;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400344 device->devnode = strdup(path);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500345
346 device->fd = open(path, O_RDONLY);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400347 if (device->fd < 0)
348 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500349
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400350 if (evdev_configure_device(device) == -1)
351 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500352
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500353 loop = wl_display_get_event_loop(display);
354 device->source = wl_event_loop_add_fd(loop, device->fd,
355 WL_EVENT_READABLE,
356 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400357 if (device->source == NULL)
358 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500359
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400360 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400361
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500362 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400363
364err1:
365 close(device->fd);
366err0:
367 free(device->devnode);
368 free(device);
369 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500370}
371
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700372static const char default_seat[] = "seat0";
373
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400374static void
375device_added(struct udev_device *udev_device, struct evdev_input *master)
376{
377 struct wlsc_compositor *c;
378 const char *devnode;
379 const char *device_seat;
380
381 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
382 if (!device_seat)
383 device_seat = default_seat;
384
385 if (strcmp(device_seat, master->seat_id))
386 return;
387
388 c = (struct wlsc_compositor *) master->base.input_device.compositor;
389 devnode = udev_device_get_devnode(udev_device);
390 if (evdev_input_device_create(master, c->wl_display, devnode))
391 fprintf(stderr, "evdev input device: added: %s\n", devnode);
392}
393
394static void
395device_removed(struct udev_device *udev_device, struct evdev_input *master)
396{
397 const char *devnode = udev_device_get_devnode(udev_device);
398 struct evdev_input_device *device, *next;
399
400 wl_list_for_each_safe(device, next, &master->devices_list, link) {
401 if (!strcmp(device->devnode, devnode)) {
402 wl_event_source_remove(device->source);
403 wl_list_remove(&device->link);
404 close(device->fd);
405 free(device->devnode);
406 free(device);
407 break;
408 }
409 }
410 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
411}
412
413static int
414evdev_udev_handler(int fd, uint32_t mask, void *data)
415{
416 struct evdev_input *master = data;
417 struct udev_device *udev_device;
418 const char *action;
419
420 udev_device = udev_monitor_receive_device(master->udev_monitor);
421 if (!udev_device)
422 return 1;
423
424 action = udev_device_get_action(udev_device);
425 if (action) {
426 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
427 return 0;
428
429 if (!strcmp(action, "add")) {
430 device_added(udev_device, master);
431 }
432 else if (!strcmp(action, "remove"))
433 device_removed(udev_device, master);
434 }
435 udev_device_unref(udev_device);
436
437 return 0;
438}
439
440static int
441evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
442{
443 struct wl_event_loop *loop;
444 struct wlsc_compositor *c =
445 (struct wlsc_compositor *) master->base.input_device.compositor;
446
447 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
448 if (!master->udev_monitor)
449 return 0;
450
451 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
452 "input", NULL);
453
454 if (udev_monitor_enable_receiving(master->udev_monitor)) {
455 fprintf(stderr, "udev: failed to bind the udev monitor\n");
456 return 0;
457 }
458
459 loop = wl_display_get_event_loop(c->wl_display);
460 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
461 WL_EVENT_READABLE, evdev_udev_handler, master);
462
463 return 1;
464}
465
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500466void
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700467evdev_input_add_devices(struct wlsc_compositor *c,
468 struct udev *udev, const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500469{
470 struct evdev_input *input;
471 struct udev_enumerate *e;
472 struct udev_list_entry *entry;
473 struct udev_device *device;
474 const char *path;
475
476 input = malloc(sizeof *input);
477 if (input == NULL)
478 return;
479
480 memset(input, 0, sizeof *input);
481 wlsc_input_device_init(&input->base, c);
482
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400483 wl_list_init(&input->devices_list);
484 input->seat_id = strdup(seat);
485 if (!evdev_config_udev_monitor(udev, input)) {
486 free(input->seat_id);
487 free(input);
488 return;
489 }
490
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500491 e = udev_enumerate_new(udev);
492 udev_enumerate_add_match_subsystem(e, "input");
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500493 udev_enumerate_scan_devices(e);
494 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
495 path = udev_list_entry_get_name(entry);
496 device = udev_device_new_from_syspath(udev, path);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700497
Kristian Høgsberg1d266032011-07-21 06:46:26 -0700498 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
499 continue;
500
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400501 device_added(device, input);
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700502
503 udev_device_unref(device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500504 }
505 udev_enumerate_unref(e);
506
507 c->input_device = &input->base.input_device;
508}