blob: 43776c8a5ddad00b9c475dbaeb6cbd7e239ed1b2 [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{
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200224 if (!accum->type)
225 return;
226
Kristian Høgsbergdb0fa542011-11-22 19:24:25 -0500227 if (accum->type == EVDEV_RELATIVE_MOTION)
228 notify_motion(device, time,
229 device->x + accum->dx, device->y + accum->dy);
Tiago Vignatti80885e12011-11-21 17:59:31 +0200230 if (accum->type == EVDEV_ABSOLUTE_MOTION)
231 notify_motion(device, time, accum->x, accum->y);
232
Tiago Vignatti1c2bcb12011-12-08 12:18:16 +0200233 evdev_reset_accum(device, accum);
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200234}
235
236static int
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400237evdev_input_device_data(int fd, uint32_t mask, void *data)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500238{
239 struct wlsc_compositor *ec;
240 struct evdev_input_device *device = data;
241 struct input_event ev[8], *e, *end;
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200242 int len;
243 struct evdev_motion_accumulator accumulator;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500244 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500245
Kristian Høgsberga8873122011-11-23 10:39:34 -0500246 ec = device->master->base.compositor;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500247 if (!ec->focus)
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400248 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500249
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500250 len = read(fd, &ev, sizeof ev);
251 if (len < 0 || len % sizeof e[0] != 0) {
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400252 /* FIXME: call device_removed when errno is ENODEV. */;
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400253 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500254 }
255
Tiago Vignatti1c2bcb12011-12-08 12:18:16 +0200256 evdev_reset_accum(&device->master->base.input_device, &accumulator);
Tiago Vignattif547bd32011-11-22 12:05:22 +0200257
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500258 e = ev;
259 end = (void *) ev + len;
260 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500261 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
262
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200263 /* we try to minimize the amount of notifications to be
264 * forwarded to the compositor, so we accumulate motion
265 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200266 if (!is_motion_event(e))
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200267 evdev_flush_motion(&device->master->base.input_device,
Tiago Vignatti80885e12011-11-21 17:59:31 +0200268 time, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500269 switch (e->type) {
270 case EV_REL:
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200271 evdev_process_relative_motion(e, &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500272 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500273 case EV_ABS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300274 if (device->is_touchpad)
275 evdev_process_absolute_motion_touchpad(device,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200276 e, &accumulator);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300277 else
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200278 evdev_process_absolute_motion(device, e,
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200279 &accumulator);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500280 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500281 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200282 evdev_process_key(device, e, time);
283 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500284 }
285 }
286
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500287 evdev_flush_motion(&device->master->base.input_device, time, &accumulator);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400288
289 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500290}
291
Matt Peterson63900ec2011-08-01 13:46:29 -0600292
293/* copied from udev/extras/input_id/input_id.c */
294/* we must use this kernel-compatible implementation */
295#define BITS_PER_LONG (sizeof(unsigned long) * 8)
296#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
297#define OFF(x) ((x)%BITS_PER_LONG)
298#define BIT(x) (1UL<<OFF(x))
299#define LONG(x) ((x)/BITS_PER_LONG)
300#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
301/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500302
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300303static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300304evdev_configure_device(struct evdev_input_device *device)
305{
306 struct input_absinfo absinfo;
307 unsigned long ev_bits[NBITS(EV_MAX)];
308 unsigned long abs_bits[NBITS(ABS_MAX)];
309 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300310 int has_key, has_abs;
311
312 has_key = 0;
313 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300314
315 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
316 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300317 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200318
Tiago Vignattid9c82502011-08-19 15:06:20 +0300319 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
320 abs_bits);
321 if (TEST_BIT(abs_bits, ABS_X)) {
322 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200323 device->abs.min_x = absinfo.minimum;
324 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300325 }
326 if (TEST_BIT(abs_bits, ABS_Y)) {
327 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200328 device->abs.min_y = absinfo.minimum;
329 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300330 }
331 }
332 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300333 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300334 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
335 key_bits);
336 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
337 !TEST_BIT(key_bits, BTN_TOOL_PEN))
338 device->is_touchpad = 1;
339 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300340
341 /* This rule tries to catch accelerometer devices and opt out. We may
342 * want to adjust the protocol later adding a proper event for dealing
343 * with accelerometers and implement here accordingly */
344 if (has_abs && !has_key)
345 return -1;
346
347 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300348}
349
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500350static struct evdev_input_device *
351evdev_input_device_create(struct evdev_input *master,
352 struct wl_display *display, const char *path)
353{
354 struct evdev_input_device *device;
355 struct wl_event_loop *loop;
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400356 struct wlsc_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500357
358 device = malloc(sizeof *device);
359 if (device == NULL)
360 return NULL;
361
Kristian Høgsberga8873122011-11-23 10:39:34 -0500362 ec = master->base.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400363 device->output =
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400364 container_of(ec->output_list.next, struct wlsc_output, link);
365
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500366 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600367 device->is_touchpad = 0;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400368 device->devnode = strdup(path);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500369
370 device->fd = open(path, O_RDONLY);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400371 if (device->fd < 0)
372 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500373
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400374 if (evdev_configure_device(device) == -1)
375 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500376
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500377 loop = wl_display_get_event_loop(display);
378 device->source = wl_event_loop_add_fd(loop, device->fd,
379 WL_EVENT_READABLE,
380 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400381 if (device->source == NULL)
382 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500383
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400384 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400385
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500386 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400387
388err1:
389 close(device->fd);
390err0:
391 free(device->devnode);
392 free(device);
393 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500394}
395
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700396static const char default_seat[] = "seat0";
397
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400398static void
399device_added(struct udev_device *udev_device, struct evdev_input *master)
400{
401 struct wlsc_compositor *c;
402 const char *devnode;
403 const char *device_seat;
404
405 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
406 if (!device_seat)
407 device_seat = default_seat;
408
409 if (strcmp(device_seat, master->seat_id))
410 return;
411
Kristian Høgsberga8873122011-11-23 10:39:34 -0500412 c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400413 devnode = udev_device_get_devnode(udev_device);
414 if (evdev_input_device_create(master, c->wl_display, devnode))
415 fprintf(stderr, "evdev input device: added: %s\n", devnode);
416}
417
418static void
419device_removed(struct udev_device *udev_device, struct evdev_input *master)
420{
421 const char *devnode = udev_device_get_devnode(udev_device);
422 struct evdev_input_device *device, *next;
423
424 wl_list_for_each_safe(device, next, &master->devices_list, link) {
425 if (!strcmp(device->devnode, devnode)) {
426 wl_event_source_remove(device->source);
427 wl_list_remove(&device->link);
428 close(device->fd);
429 free(device->devnode);
430 free(device);
431 break;
432 }
433 }
434 fprintf(stderr, "evdev input device: removed: %s\n", devnode);
435}
436
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200437static void
438evdev_add_devices(struct udev *udev, struct wlsc_input_device *input_base)
439{
440 struct evdev_input *input = (struct evdev_input *) input_base;
441 struct udev_enumerate *e;
442 struct udev_list_entry *entry;
443 struct udev_device *device;
444 const char *path;
445
446 e = udev_enumerate_new(udev);
447 udev_enumerate_add_match_subsystem(e, "input");
448 udev_enumerate_scan_devices(e);
449 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
450 path = udev_list_entry_get_name(entry);
451 device = udev_device_new_from_syspath(udev, path);
452
453 if (strncmp("event", udev_device_get_sysname(device), 5) != 0)
454 continue;
455
456 device_added(device, input);
457
458 udev_device_unref(device);
459 }
460 udev_enumerate_unref(e);
461}
462
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400463static int
464evdev_udev_handler(int fd, uint32_t mask, void *data)
465{
466 struct evdev_input *master = data;
467 struct udev_device *udev_device;
468 const char *action;
469
470 udev_device = udev_monitor_receive_device(master->udev_monitor);
471 if (!udev_device)
472 return 1;
473
474 action = udev_device_get_action(udev_device);
475 if (action) {
476 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
477 return 0;
478
479 if (!strcmp(action, "add")) {
480 device_added(udev_device, master);
481 }
482 else if (!strcmp(action, "remove"))
483 device_removed(udev_device, master);
484 }
485 udev_device_unref(udev_device);
486
487 return 0;
488}
489
490static int
491evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
492{
493 struct wl_event_loop *loop;
Kristian Høgsberga8873122011-11-23 10:39:34 -0500494 struct wlsc_compositor *c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400495
496 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
497 if (!master->udev_monitor)
498 return 0;
499
500 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
501 "input", NULL);
502
503 if (udev_monitor_enable_receiving(master->udev_monitor)) {
504 fprintf(stderr, "udev: failed to bind the udev monitor\n");
505 return 0;
506 }
507
508 loop = wl_display_get_event_loop(c->wl_display);
509 wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor),
510 WL_EVENT_READABLE, evdev_udev_handler, master);
511
512 return 1;
513}
514
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500515void
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700516evdev_input_add_devices(struct wlsc_compositor *c,
517 struct udev *udev, const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500518{
519 struct evdev_input *input;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500520
521 input = malloc(sizeof *input);
522 if (input == NULL)
523 return;
524
525 memset(input, 0, sizeof *input);
526 wlsc_input_device_init(&input->base, c);
527
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400528 wl_list_init(&input->devices_list);
529 input->seat_id = strdup(seat);
530 if (!evdev_config_udev_monitor(udev, input)) {
531 free(input->seat_id);
532 free(input);
533 return;
534 }
535
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200536 evdev_add_devices(udev, &input->base);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500537
538 c->input_device = &input->base.input_device;
539}