blob: e73f343ce5cf2ad05353e9a5e96934f14d33b395 [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>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030029#include <mtdev.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050030
31#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020032#include "evdev.h"
Jonas Ådahl4136d822012-05-17 12:18:16 +020033#include "evdev-private.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010034#include "launcher-util.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050035
Daniel Stone7bbd5b32012-05-30 16:31:49 +010036static void
37evdev_led_update(struct weston_seat *seat_base, enum weston_led leds)
38{
39 static const struct {
40 enum weston_led weston;
41 int evdev;
42 } map[] = {
43 { LED_NUM_LOCK, LED_NUML },
44 { LED_CAPS_LOCK, LED_CAPSL },
45 { LED_SCROLL_LOCK, LED_SCROLLL },
46 };
47 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
48 struct evdev_input_device *device;
49 struct input_event ev[ARRAY_LENGTH(map)];
50 unsigned int i;
51
52 memset(ev, 0, sizeof(ev));
53 for (i = 0; i < ARRAY_LENGTH(map); i++) {
54 ev[i].type = EV_LED;
55 ev[i].code = map[i].evdev;
56 ev[i].value = !!(leds & map[i].weston);
57 }
58
59 wl_list_for_each(device, &seat->devices_list, link) {
60 if (device->caps & EVDEV_KEYBOARD)
61 write(device->fd, ev, sizeof *ev);
62 }
63}
64
Tiago Vignatti8be003b2011-09-01 19:00:03 +030065static inline void
66evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020067 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030068{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020069 if (e->value == 2)
70 return;
71
Tiago Vignatti8be003b2011-09-01 19:00:03 +030072 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030073 case BTN_LEFT:
74 case BTN_RIGHT:
75 case BTN_MIDDLE:
76 case BTN_SIDE:
77 case BTN_EXTRA:
78 case BTN_FORWARD:
79 case BTN_BACK:
80 case BTN_TASK:
Daniel Stone37816df2012-05-16 18:45:18 +010081 notify_button(&device->master->base.seat,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020082 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030083 break;
84
85 default:
Daniel Stone37816df2012-05-16 18:45:18 +010086 notify_key(&device->master->base.seat,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020087 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030088 break;
89 }
90}
91
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020092static void
93evdev_process_touch(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -050094 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020095{
96 const int screen_width = device->output->current->width;
97 const int screen_height = device->output->current->height;
98
99 switch (e->code) {
100 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500101 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200102 break;
103 case ABS_MT_TRACKING_ID:
104 if (e->value >= 0)
Daniel Stone1d637772012-05-30 16:31:47 +0100105 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200106 else
Daniel Stone1d637772012-05-30 16:31:47 +0100107 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200108 break;
109 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500110 device->mt.x[device->mt.slot] =
111 (e->value - device->abs.min_x) * screen_width /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200112 (device->abs.max_x - device->abs.min_x) +
113 device->output->x;
Daniel Stone1d637772012-05-30 16:31:47 +0100114 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200115 break;
116 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500117 device->mt.y[device->mt.slot] =
118 (e->value - device->abs.min_y) * screen_height /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200119 (device->abs.max_y - device->abs.min_y) +
120 device->output->y;
Daniel Stone1d637772012-05-30 16:31:47 +0100121 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200122 break;
123 }
124}
125
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300126static inline void
127evdev_process_absolute_motion(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500128 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300129{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400130 const int screen_width = device->output->current->width;
131 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300132
133 switch (e->code) {
134 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500135 device->abs.x =
136 (e->value - device->abs.min_x) * screen_width /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200137 (device->abs.max_x - device->abs.min_x) +
138 device->output->x;
Daniel Stone1d637772012-05-30 16:31:47 +0100139 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300140 break;
141 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500142 device->abs.y =
143 (e->value - device->abs.min_y) * screen_height /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200144 (device->abs.max_y - device->abs.min_y) +
145 device->output->y;
Daniel Stone1d637772012-05-30 16:31:47 +0100146 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300147 break;
148 }
149}
150
151static inline void
Scott Moreau210d0792012-03-22 10:47:01 -0600152evdev_process_relative(struct evdev_input_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200153 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300154{
155 switch (e->code) {
156 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400157 device->rel.dx += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100158 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300159 break;
160 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400161 device->rel.dy += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100162 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300163 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600164 case REL_WHEEL:
Daniel Stone37816df2012-05-16 18:45:18 +0100165 notify_axis(&device->master->base.seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600166 time,
Daniel Stone37816df2012-05-16 18:45:18 +0100167 WL_POINTER_AXIS_VERTICAL_SCROLL, e->value);
Scott Moreau210d0792012-03-22 10:47:01 -0600168 break;
169 case REL_HWHEEL:
Daniel Stone37816df2012-05-16 18:45:18 +0100170 notify_axis(&device->master->base.seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600171 time,
Daniel Stone37816df2012-05-16 18:45:18 +0100172 WL_POINTER_AXIS_HORIZONTAL_SCROLL, e->value);
Scott Moreau210d0792012-03-22 10:47:01 -0600173 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300174 }
175}
176
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200177static inline void
178evdev_process_absolute(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500179 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200180{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200181 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500182 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200183 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500184 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200185 }
186}
187
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400188static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200189is_motion_event(struct input_event *e)
190{
191 switch (e->type) {
192 case EV_REL:
193 switch (e->code) {
194 case REL_X:
195 case REL_Y:
196 return 1;
197 }
198 case EV_ABS:
199 switch (e->code) {
200 case ABS_X:
201 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200202 case ABS_MT_POSITION_X:
203 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200204 return 1;
205 }
206 }
207
208 return 0;
209}
210
211static void
Kristian Høgsberg39373542011-12-21 22:18:36 -0500212evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200213{
Daniel Stone37816df2012-05-16 18:45:18 +0100214 struct weston_seat *master = &device->master->base;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500215
Daniel Stone1d637772012-05-30 16:31:47 +0100216 if (!device->pending_events)
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200217 return;
218
Daniel Stone1d637772012-05-30 16:31:47 +0100219 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100220 notify_motion(&master->seat, time,
221 master->seat.pointer->x + device->rel.dx,
222 master->seat.pointer->y + device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100223 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500224 device->rel.dx = 0;
225 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200226 }
Daniel Stone1d637772012-05-30 16:31:47 +0100227 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Daniel Stone37816df2012-05-16 18:45:18 +0100228 notify_touch(&master->seat, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500229 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400230 wl_fixed_from_int(device->mt.x[device->mt.slot]),
231 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100232 WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100233 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
234 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200235 }
Daniel Stone1d637772012-05-30 16:31:47 +0100236 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100237 notify_touch(&master->seat, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500238 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400239 wl_fixed_from_int(device->mt.x[device->mt.slot]),
240 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100241 WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100242 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
243 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200244 }
Daniel Stone1d637772012-05-30 16:31:47 +0100245 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Daniel Stone37816df2012-05-16 18:45:18 +0100246 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
247 WL_TOUCH_UP);
Daniel Stone1d637772012-05-30 16:31:47 +0100248 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200249 }
Daniel Stone1d637772012-05-30 16:31:47 +0100250 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100251 notify_motion(&master->seat, time,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400252 wl_fixed_from_int(device->abs.x),
253 wl_fixed_from_int(device->abs.y));
Daniel Stone1d637772012-05-30 16:31:47 +0100254 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200255 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200256}
257
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400258static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200259fallback_process(struct evdev_dispatch *dispatch,
260 struct evdev_input_device *device,
261 struct input_event *event,
262 uint32_t time)
263{
264 switch (event->type) {
265 case EV_REL:
266 evdev_process_relative(device, event, time);
267 break;
268 case EV_ABS:
269 evdev_process_absolute(device, event);
270 break;
271 case EV_KEY:
272 evdev_process_key(device, event, time);
273 break;
274 }
275}
276
277static void
278fallback_destroy(struct evdev_dispatch *dispatch)
279{
280 free(dispatch);
281}
282
283struct evdev_dispatch_interface fallback_interface = {
284 fallback_process,
285 fallback_destroy
286};
287
288static struct evdev_dispatch *
289fallback_dispatch_create(void)
290{
291 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
292 if (dispatch == NULL)
293 return NULL;
294
295 dispatch->interface = &fallback_interface;
296
297 return dispatch;
298}
299
300static void
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400301evdev_process_events(struct evdev_input_device *device,
302 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500303{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200304 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400305 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500306 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500307
Daniel Stone1d637772012-05-30 16:31:47 +0100308 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200309
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500310 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400311 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500312 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500313 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
314
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200315 /* we try to minimize the amount of notifications to be
316 * forwarded to the compositor, so we accumulate motion
317 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200318 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500319 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200320
321 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500322 }
323
Kristian Høgsberg39373542011-12-21 22:18:36 -0500324 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400325}
326
327static int
328evdev_input_device_data(int fd, uint32_t mask, void *data)
329{
330 struct weston_compositor *ec;
331 struct evdev_input_device *device = data;
332 struct input_event ev[32];
333 int len;
334
335 ec = device->master->base.compositor;
336 if (!ec->focus)
337 return 1;
338
339 /* If the compositor is repainting, this function is called only once
340 * per frame and we have to process all the events available on the
341 * fd, otherwise there will be input lag. */
342 do {
343 if (device->mtdev)
344 len = mtdev_get(device->mtdev, fd, ev,
345 ARRAY_LENGTH(ev)) *
346 sizeof (struct input_event);
347 else
348 len = read(fd, &ev, sizeof ev);
349
350 if (len < 0 || len % sizeof ev[0] != 0) {
351 /* FIXME: call device_removed when errno is ENODEV. */
352 return 1;
353 }
354
355 evdev_process_events(device, ev, len / sizeof ev[0]);
356
357 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400358
359 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500360}
361
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300362static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300363evdev_configure_device(struct evdev_input_device *device)
364{
365 struct input_absinfo absinfo;
366 unsigned long ev_bits[NBITS(EV_MAX)];
367 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100368 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300369 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300370 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100371 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300372
373 has_key = 0;
374 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100375 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300376
377 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
378 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300379 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200380
Tiago Vignattid9c82502011-08-19 15:06:20 +0300381 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
382 abs_bits);
383 if (TEST_BIT(abs_bits, ABS_X)) {
384 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200385 device->abs.min_x = absinfo.minimum;
386 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100387 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300388 }
389 if (TEST_BIT(abs_bits, ABS_Y)) {
390 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200391 device->abs.min_y = absinfo.minimum;
392 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100393 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300394 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200395 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
396 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500397 device->mt.slot = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100398 device->caps |= EVDEV_TOUCH;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200399 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300400 }
Daniel Stone33965c22012-05-30 16:31:48 +0100401 if (TEST_BIT(ev_bits, EV_REL)) {
402 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
403 rel_bits);
404 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
405 device->caps |= EVDEV_MOTION_REL;
406 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300407 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300408 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300409 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
410 key_bits);
411 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200412 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
413 has_abs)
414 device->dispatch = evdev_touchpad_create(device);
Daniel Stone33965c22012-05-30 16:31:48 +0100415 for (i = KEY_ESC; i < KEY_MAX; i++) {
416 if (i >= BTN_MISC && i < KEY_OK)
417 continue;
418 if (TEST_BIT(key_bits, i)) {
419 device->caps |= EVDEV_KEYBOARD;
420 break;
421 }
422 }
423 for (i = BTN_MISC; i < KEY_OK; i++) {
424 if (TEST_BIT(key_bits, i)) {
425 device->caps |= EVDEV_BUTTON;
426 break;
427 }
428 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300429 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300430
431 /* This rule tries to catch accelerometer devices and opt out. We may
432 * want to adjust the protocol later adding a proper event for dealing
433 * with accelerometers and implement here accordingly */
434 if (has_abs && !has_key)
435 return -1;
436
437 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300438}
439
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500440static struct evdev_input_device *
Daniel Stone37816df2012-05-16 18:45:18 +0100441evdev_input_device_create(struct evdev_seat *master,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500442 struct wl_display *display, const char *path)
443{
444 struct evdev_input_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500445 struct weston_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500446
447 device = malloc(sizeof *device);
448 if (device == NULL)
449 return NULL;
450
Kristian Høgsberga8873122011-11-23 10:39:34 -0500451 ec = master->base.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400452 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500453 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400454
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500455 device->master = master;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200456 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300457 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400458 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500459 device->mt.slot = -1;
460 device->rel.dx = 0;
461 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200462 device->dispatch = NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500463
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400464 /* Use non-blocking mode so that we can loop on read on
465 * evdev_input_device_data() until all events on the fd are
466 * read. mtdev_get() also expects this. */
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100467 device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400468 if (device->fd < 0)
469 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500470
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400471 if (evdev_configure_device(device) == -1)
472 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500473
Jonas Ådahl4136d822012-05-17 12:18:16 +0200474 /* If the dispatch was not set up use the fallback. */
475 if (device->dispatch == NULL)
476 device->dispatch = fallback_dispatch_create();
477 if (device->dispatch == NULL)
478 goto err1;
479
480
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300481 if (device->is_mt) {
482 device->mtdev = mtdev_new_open(device->fd);
483 if (!device->mtdev)
484 fprintf(stderr, "mtdev failed to open for %s\n", path);
485 }
486
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500487 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500488 WL_EVENT_READABLE,
489 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400490 if (device->source == NULL)
Jonas Ådahl4136d822012-05-17 12:18:16 +0200491 goto err2;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500492
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400493 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400494
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500495 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400496
Jonas Ådahl4136d822012-05-17 12:18:16 +0200497err2:
498 device->dispatch->interface->destroy(device->dispatch);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400499err1:
500 close(device->fd);
501err0:
502 free(device->devnode);
503 free(device);
504 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500505}
506
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700507static const char default_seat[] = "seat0";
508
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400509static void
Daniel Stone37816df2012-05-16 18:45:18 +0100510device_added(struct udev_device *udev_device, struct evdev_seat *master)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500512 struct weston_compositor *c;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400513 const char *devnode;
514 const char *device_seat;
515
516 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
517 if (!device_seat)
518 device_seat = default_seat;
519
520 if (strcmp(device_seat, master->seat_id))
521 return;
522
Kristian Høgsberga8873122011-11-23 10:39:34 -0500523 c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400524 devnode = udev_device_get_devnode(udev_device);
Kristian Høgsberg23a47a82012-01-16 10:54:07 -0500525 evdev_input_device_create(master, c->wl_display, devnode);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400526}
527
528static void
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300529device_removed(struct evdev_input_device *device)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400530{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200531 struct evdev_dispatch *dispatch;
532
533 dispatch = device->dispatch;
534 if (dispatch)
535 dispatch->interface->destroy(dispatch);
536
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300537 wl_event_source_remove(device->source);
538 wl_list_remove(&device->link);
539 if (device->mtdev)
540 mtdev_close_delete(device->mtdev);
541 close(device->fd);
542 free(device->devnode);
543 free(device);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400544}
545
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400546static void
Daniel Stone37816df2012-05-16 18:45:18 +0100547evdev_notify_keyboard_focus(struct evdev_seat *seat)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400548{
549 struct evdev_input_device *device;
550 struct wl_array keys;
551 unsigned int i, set;
552 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
553 uint32_t *k;
554 int ret;
555
556 memset(all_keys, 0, sizeof all_keys);
Daniel Stone37816df2012-05-16 18:45:18 +0100557 wl_list_for_each(device, &seat->devices_list, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400558 memset(evdev_keys, 0, sizeof evdev_keys);
559 ret = ioctl(device->fd,
560 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
561 if (ret < 0) {
562 fprintf(stderr, "failed to get keys for device %s\n",
563 device->devnode);
564 continue;
565 }
566 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
567 all_keys[i] |= evdev_keys[i];
568 }
569
570 wl_array_init(&keys);
571 for (i = 0; i < KEY_CNT; i++) {
572 set = all_keys[i >> 3] & (1 << (i & 7));
573 if (set) {
574 k = wl_array_add(&keys, sizeof *k);
575 *k = i;
576 }
577 }
578
Daniel Stone37816df2012-05-16 18:45:18 +0100579 notify_keyboard_focus(&seat->base.seat, &keys);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400580
581 wl_array_release(&keys);
582}
583
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200584void
Daniel Stone37816df2012-05-16 18:45:18 +0100585evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200586{
Daniel Stone37816df2012-05-16 18:45:18 +0100587 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200588 struct udev_enumerate *e;
589 struct udev_list_entry *entry;
590 struct udev_device *device;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200591 const char *path, *sysname;
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200592
593 e = udev_enumerate_new(udev);
594 udev_enumerate_add_match_subsystem(e, "input");
595 udev_enumerate_scan_devices(e);
596 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
597 path = udev_list_entry_get_name(entry);
598 device = udev_device_new_from_syspath(udev, path);
599
Jonas Ådahlc97af922012-03-28 22:36:09 +0200600 sysname = udev_device_get_sysname(device);
601 if (strncmp("event", sysname, 5) != 0) {
602 udev_device_unref(device);
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200603 continue;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200604 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200605
Daniel Stone37816df2012-05-16 18:45:18 +0100606 device_added(device, seat);
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200607
608 udev_device_unref(device);
609 }
610 udev_enumerate_unref(e);
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200611
Daniel Stone37816df2012-05-16 18:45:18 +0100612 evdev_notify_keyboard_focus(seat);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400613
Daniel Stone37816df2012-05-16 18:45:18 +0100614 if (wl_list_empty(&seat->devices_list)) {
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200615 fprintf(stderr,
616 "warning: no input devices on entering Weston. "
617 "Possible causes:\n"
Olivier Le Thanh Duong643eac52012-01-14 15:57:34 +0100618 "\t- no permissions to read /dev/input/event*\n"
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200619 "\t- seats misconfigured "
620 "(Weston backend option 'seat', "
621 "udev device property ID_SEAT)\n");
622 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200623}
624
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400625static int
626evdev_udev_handler(int fd, uint32_t mask, void *data)
627{
Daniel Stone37816df2012-05-16 18:45:18 +0100628 struct evdev_seat *master = data;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400629 struct udev_device *udev_device;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300630 struct evdev_input_device *device, *next;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400631 const char *action;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300632 const char *devnode;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400633
634 udev_device = udev_monitor_receive_device(master->udev_monitor);
635 if (!udev_device)
636 return 1;
637
638 action = udev_device_get_action(udev_device);
639 if (action) {
640 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
641 return 0;
642
643 if (!strcmp(action, "add")) {
644 device_added(udev_device, master);
645 }
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300646 else if (!strcmp(action, "remove")) {
647 devnode = udev_device_get_devnode(udev_device);
648 wl_list_for_each_safe(device, next,
649 &master->devices_list, link)
650 if (!strcmp(device->devnode, devnode)) {
651 device_removed(device);
652 break;
653 }
654 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400655 }
656 udev_device_unref(udev_device);
657
658 return 0;
659}
660
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200661int
Daniel Stone37816df2012-05-16 18:45:18 +0100662evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400663{
Daniel Stone37816df2012-05-16 18:45:18 +0100664 struct evdev_seat *master = (struct evdev_seat *) seat_base;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400665 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500666 struct weston_compositor *c = master->base.compositor;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200667 int fd;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400668
669 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200670 if (!master->udev_monitor) {
671 fprintf(stderr, "udev: failed to create the udev monitor\n");
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400672 return 0;
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200673 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400674
675 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
676 "input", NULL);
677
678 if (udev_monitor_enable_receiving(master->udev_monitor)) {
679 fprintf(stderr, "udev: failed to bind the udev monitor\n");
Jonas Ådahlc97af922012-03-28 22:36:09 +0200680 udev_monitor_unref(master->udev_monitor);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400681 return 0;
682 }
683
684 loop = wl_display_get_event_loop(c->wl_display);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200685 fd = udev_monitor_get_fd(master->udev_monitor);
686 master->udev_monitor_source =
687 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
688 evdev_udev_handler, master);
689 if (!master->udev_monitor_source) {
690 udev_monitor_unref(master->udev_monitor);
691 return 0;
692 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400693
694 return 1;
695}
696
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500697void
Daniel Stone37816df2012-05-16 18:45:18 +0100698evdev_disable_udev_monitor(struct weston_seat *seat_base)
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200699{
Daniel Stone37816df2012-05-16 18:45:18 +0100700 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200701
Daniel Stone37816df2012-05-16 18:45:18 +0100702 if (!seat->udev_monitor)
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200703 return;
704
Daniel Stone37816df2012-05-16 18:45:18 +0100705 udev_monitor_unref(seat->udev_monitor);
706 seat->udev_monitor = NULL;
707 wl_event_source_remove(seat->udev_monitor_source);
708 seat->udev_monitor_source = NULL;
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200709}
710
711void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500712evdev_input_create(struct weston_compositor *c, struct udev *udev,
Daniel Stone37816df2012-05-16 18:45:18 +0100713 const char *seat_id)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500714{
Daniel Stone37816df2012-05-16 18:45:18 +0100715 struct evdev_seat *seat;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500716
Daniel Stone37816df2012-05-16 18:45:18 +0100717 seat = malloc(sizeof *seat);
718 if (seat == NULL)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500719 return;
720
Daniel Stone37816df2012-05-16 18:45:18 +0100721 memset(seat, 0, sizeof *seat);
722 weston_seat_init(&seat->base, c);
Daniel Stone7bbd5b32012-05-30 16:31:49 +0100723 seat->base.led_update = evdev_led_update;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500724
Daniel Stone37816df2012-05-16 18:45:18 +0100725 wl_list_init(&seat->devices_list);
726 seat->seat_id = strdup(seat_id);
727 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
728 free(seat->seat_id);
729 free(seat);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400730 return;
731 }
732
Daniel Stone37816df2012-05-16 18:45:18 +0100733 evdev_add_devices(udev, &seat->base);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500734
Daniel Stone37816df2012-05-16 18:45:18 +0100735 c->seat = &seat->base;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500736}
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200737
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200738void
Daniel Stone37816df2012-05-16 18:45:18 +0100739evdev_remove_devices(struct weston_seat *seat_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200740{
Daniel Stone37816df2012-05-16 18:45:18 +0100741 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200742 struct evdev_input_device *device, *next;
743
Daniel Stone37816df2012-05-16 18:45:18 +0100744 wl_list_for_each_safe(device, next, &seat->devices_list, link)
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300745 device_removed(device);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400746
Daniel Stone37816df2012-05-16 18:45:18 +0100747 notify_keyboard_focus(&seat->base.seat, NULL);
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200748}
749
750void
Daniel Stone37816df2012-05-16 18:45:18 +0100751evdev_input_destroy(struct weston_seat *seat_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200752{
Daniel Stone37816df2012-05-16 18:45:18 +0100753 struct evdev_seat *seat = (struct evdev_seat *) seat_base;
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200754
Daniel Stone37816df2012-05-16 18:45:18 +0100755 evdev_remove_devices(seat_base);
756 evdev_disable_udev_monitor(&seat->base);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200757
Daniel Stone37816df2012-05-16 18:45:18 +0100758 wl_list_remove(&seat->base.link);
759 free(seat->seat_id);
760 free(seat);
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200761}