blob: dba33babcad50147b6f03909797da054ae1208bb [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
Kristian Høgsberg43db4012011-01-14 14:45:42 -050023#include <stdlib.h>
24#include <string.h>
25#include <linux/input.h>
26#include <unistd.h>
27#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030028#include <mtdev.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050029
30#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020031#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050032
Pekka Paalanen88594b62012-08-03 14:39:05 +030033void
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030034evdev_led_update(struct evdev_input_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010035{
36 static const struct {
37 enum weston_led weston;
38 int evdev;
39 } map[] = {
40 { LED_NUM_LOCK, LED_NUML },
41 { LED_CAPS_LOCK, LED_CAPSL },
42 { LED_SCROLL_LOCK, LED_SCROLLL },
43 };
Daniel Stone7bbd5b32012-05-30 16:31:49 +010044 struct input_event ev[ARRAY_LENGTH(map)];
45 unsigned int i;
46
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030047 if (!device->caps & EVDEV_KEYBOARD)
48 return;
49
Daniel Stone7bbd5b32012-05-30 16:31:49 +010050 memset(ev, 0, sizeof(ev));
51 for (i = 0; i < ARRAY_LENGTH(map); i++) {
52 ev[i].type = EV_LED;
53 ev[i].code = map[i].evdev;
54 ev[i].value = !!(leds & map[i].weston);
55 }
56
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030057 i = write(device->fd, ev, sizeof ev);
58 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010059}
60
Tiago Vignatti8be003b2011-09-01 19:00:03 +030061static inline void
62evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020063 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030064{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020065 if (e->value == 2)
66 return;
67
Tiago Vignatti8be003b2011-09-01 19:00:03 +030068 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030069 case BTN_LEFT:
70 case BTN_RIGHT:
71 case BTN_MIDDLE:
72 case BTN_SIDE:
73 case BTN_EXTRA:
74 case BTN_FORWARD:
75 case BTN_BACK:
76 case BTN_TASK:
Pekka Paalanen5618d6f2012-08-03 14:39:00 +030077 notify_button(&device->seat->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +010078 time, e->code,
79 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
80 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030081 break;
82
83 default:
Pekka Paalanen5618d6f2012-08-03 14:39:00 +030084 notify_key(&device->seat->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +010085 time, e->code,
86 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +010087 WL_KEYBOARD_KEY_STATE_RELEASED,
88 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030089 break;
90 }
91}
92
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020093static void
94evdev_process_touch(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -050095 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020096{
97 const int screen_width = device->output->current->width;
98 const int screen_height = device->output->current->height;
99
100 switch (e->code) {
101 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500102 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200103 break;
104 case ABS_MT_TRACKING_ID:
105 if (e->value >= 0)
Daniel Stone1d637772012-05-30 16:31:47 +0100106 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200107 else
Daniel Stone1d637772012-05-30 16:31:47 +0100108 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200109 break;
110 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500111 device->mt.x[device->mt.slot] =
112 (e->value - device->abs.min_x) * screen_width /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200113 (device->abs.max_x - device->abs.min_x) +
114 device->output->x;
Daniel Stone1d637772012-05-30 16:31:47 +0100115 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200116 break;
117 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500118 device->mt.y[device->mt.slot] =
119 (e->value - device->abs.min_y) * screen_height /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200120 (device->abs.max_y - device->abs.min_y) +
121 device->output->y;
Daniel Stone1d637772012-05-30 16:31:47 +0100122 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200123 break;
124 }
125}
126
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300127static inline void
128evdev_process_absolute_motion(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500129 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300130{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400131 const int screen_width = device->output->current->width;
132 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300133
134 switch (e->code) {
135 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500136 device->abs.x =
137 (e->value - device->abs.min_x) * screen_width /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200138 (device->abs.max_x - device->abs.min_x) +
139 device->output->x;
Daniel Stone1d637772012-05-30 16:31:47 +0100140 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300141 break;
142 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500143 device->abs.y =
144 (e->value - device->abs.min_y) * screen_height /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200145 (device->abs.max_y - device->abs.min_y) +
146 device->output->y;
Daniel Stone1d637772012-05-30 16:31:47 +0100147 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300148 break;
149 }
150}
151
152static inline void
Scott Moreau210d0792012-03-22 10:47:01 -0600153evdev_process_relative(struct evdev_input_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200154 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300155{
156 switch (e->code) {
157 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400158 device->rel.dx += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100159 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300160 break;
161 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400162 device->rel.dy += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100163 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300164 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600165 case REL_WHEEL:
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300166 notify_axis(&device->seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600167 time,
Daniel Stone878f0b72012-05-30 16:31:57 +0100168 WL_POINTER_AXIS_VERTICAL_SCROLL,
169 wl_fixed_from_int(e->value));
Scott Moreau210d0792012-03-22 10:47:01 -0600170 break;
171 case REL_HWHEEL:
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300172 notify_axis(&device->seat->seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600173 time,
Daniel Stone878f0b72012-05-30 16:31:57 +0100174 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
175 wl_fixed_from_int(e->value));
Scott Moreau210d0792012-03-22 10:47:01 -0600176 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300177 }
178}
179
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200180static inline void
181evdev_process_absolute(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500182 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200183{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200184 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500185 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200186 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500187 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200188 }
189}
190
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400191static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200192is_motion_event(struct input_event *e)
193{
194 switch (e->type) {
195 case EV_REL:
196 switch (e->code) {
197 case REL_X:
198 case REL_Y:
199 return 1;
200 }
201 case EV_ABS:
202 switch (e->code) {
203 case ABS_X:
204 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200205 case ABS_MT_POSITION_X:
206 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200207 return 1;
208 }
209 }
210
211 return 0;
212}
213
214static void
Kristian Høgsberg39373542011-12-21 22:18:36 -0500215evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200216{
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300217 struct weston_seat *master = device->seat;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500218
Daniel Stone1d637772012-05-30 16:31:47 +0100219 if (!device->pending_events)
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200220 return;
221
Daniel Stone1d637772012-05-30 16:31:47 +0100222 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100223 notify_motion(&master->seat, time,
224 master->seat.pointer->x + device->rel.dx,
225 master->seat.pointer->y + device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100226 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500227 device->rel.dx = 0;
228 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200229 }
Daniel Stone1d637772012-05-30 16:31:47 +0100230 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Daniel Stone37816df2012-05-16 18:45:18 +0100231 notify_touch(&master->seat, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500232 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400233 wl_fixed_from_int(device->mt.x[device->mt.slot]),
234 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100235 WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100236 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
237 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200238 }
Daniel Stone1d637772012-05-30 16:31:47 +0100239 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100240 notify_touch(&master->seat, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500241 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400242 wl_fixed_from_int(device->mt.x[device->mt.slot]),
243 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100244 WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100245 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
246 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200247 }
Daniel Stone1d637772012-05-30 16:31:47 +0100248 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Daniel Stone37816df2012-05-16 18:45:18 +0100249 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
250 WL_TOUCH_UP);
Daniel Stone1d637772012-05-30 16:31:47 +0100251 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200252 }
Daniel Stone1d637772012-05-30 16:31:47 +0100253 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
Daniel Stone37816df2012-05-16 18:45:18 +0100254 notify_motion(&master->seat, time,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400255 wl_fixed_from_int(device->abs.x),
256 wl_fixed_from_int(device->abs.y));
Daniel Stone1d637772012-05-30 16:31:47 +0100257 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200258 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200259}
260
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400261static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200262fallback_process(struct evdev_dispatch *dispatch,
263 struct evdev_input_device *device,
264 struct input_event *event,
265 uint32_t time)
266{
267 switch (event->type) {
268 case EV_REL:
269 evdev_process_relative(device, event, time);
270 break;
271 case EV_ABS:
272 evdev_process_absolute(device, event);
273 break;
274 case EV_KEY:
275 evdev_process_key(device, event, time);
276 break;
277 }
278}
279
280static void
281fallback_destroy(struct evdev_dispatch *dispatch)
282{
283 free(dispatch);
284}
285
286struct evdev_dispatch_interface fallback_interface = {
287 fallback_process,
288 fallback_destroy
289};
290
291static struct evdev_dispatch *
292fallback_dispatch_create(void)
293{
294 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
295 if (dispatch == NULL)
296 return NULL;
297
298 dispatch->interface = &fallback_interface;
299
300 return dispatch;
301}
302
303static void
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400304evdev_process_events(struct evdev_input_device *device,
305 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500306{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200307 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400308 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500309 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500310
Daniel Stone1d637772012-05-30 16:31:47 +0100311 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200312
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500313 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400314 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500315 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500316 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
317
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200318 /* we try to minimize the amount of notifications to be
319 * forwarded to the compositor, so we accumulate motion
320 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200321 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500322 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200323
324 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500325 }
326
Kristian Høgsberg39373542011-12-21 22:18:36 -0500327 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400328}
329
330static int
331evdev_input_device_data(int fd, uint32_t mask, void *data)
332{
333 struct weston_compositor *ec;
334 struct evdev_input_device *device = data;
335 struct input_event ev[32];
336 int len;
337
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300338 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400339 if (!ec->focus)
340 return 1;
341
342 /* If the compositor is repainting, this function is called only once
343 * per frame and we have to process all the events available on the
344 * fd, otherwise there will be input lag. */
345 do {
346 if (device->mtdev)
347 len = mtdev_get(device->mtdev, fd, ev,
348 ARRAY_LENGTH(ev)) *
349 sizeof (struct input_event);
350 else
351 len = read(fd, &ev, sizeof ev);
352
353 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300354 /* FIXME: call evdev_input_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400355 return 1;
356 }
357
358 evdev_process_events(device, ev, len / sizeof ev[0]);
359
360 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400361
362 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500363}
364
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300365static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300366evdev_configure_device(struct evdev_input_device *device)
367{
368 struct input_absinfo absinfo;
369 unsigned long ev_bits[NBITS(EV_MAX)];
370 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100371 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300372 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300373 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100374 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300375
376 has_key = 0;
377 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100378 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300379
380 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
381 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300382 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200383
Tiago Vignattid9c82502011-08-19 15:06:20 +0300384 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
385 abs_bits);
386 if (TEST_BIT(abs_bits, ABS_X)) {
387 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200388 device->abs.min_x = absinfo.minimum;
389 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100390 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300391 }
392 if (TEST_BIT(abs_bits, ABS_Y)) {
393 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200394 device->abs.min_y = absinfo.minimum;
395 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100396 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300397 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200398 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300399 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
400 &absinfo);
401 device->abs.min_x = absinfo.minimum;
402 device->abs.max_x = absinfo.maximum;
403 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
404 &absinfo);
405 device->abs.min_y = absinfo.minimum;
406 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200407 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500408 device->mt.slot = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100409 device->caps |= EVDEV_TOUCH;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200410 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300411 }
Daniel Stone33965c22012-05-30 16:31:48 +0100412 if (TEST_BIT(ev_bits, EV_REL)) {
413 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
414 rel_bits);
415 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
416 device->caps |= EVDEV_MOTION_REL;
417 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300418 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300419 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300420 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
421 key_bits);
422 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200423 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
424 has_abs)
425 device->dispatch = evdev_touchpad_create(device);
Daniel Stone33965c22012-05-30 16:31:48 +0100426 for (i = KEY_ESC; i < KEY_MAX; i++) {
427 if (i >= BTN_MISC && i < KEY_OK)
428 continue;
429 if (TEST_BIT(key_bits, i)) {
430 device->caps |= EVDEV_KEYBOARD;
431 break;
432 }
433 }
434 for (i = BTN_MISC; i < KEY_OK; i++) {
435 if (TEST_BIT(key_bits, i)) {
436 device->caps |= EVDEV_BUTTON;
437 break;
438 }
439 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300440 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100441 if (TEST_BIT(ev_bits, EV_LED)) {
442 device->caps |= EVDEV_KEYBOARD;
443 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300444
445 /* This rule tries to catch accelerometer devices and opt out. We may
446 * want to adjust the protocol later adding a proper event for dealing
447 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300448 if (has_abs && !has_key && !device->is_mt) {
449 weston_log("input device %s, %s "
450 "ignored: unsupported device type\n",
451 device->devname, device->devnode);
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300452 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300453 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300454
Daniel Stone74419a22012-05-30 16:32:02 +0100455 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300456 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300457 weston_seat_init_pointer(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300458 weston_log("input device %s, %s is a pointer\n",
459 device->devname, device->devnode);
460 }
461 if ((device->caps & EVDEV_KEYBOARD)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300462 weston_seat_init_keyboard(device->seat, NULL);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300463 weston_log("input device %s, %s is a keyboard\n",
464 device->devname, device->devnode);
465 }
466 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300467 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300468 weston_log("input device %s, %s is a touch device\n",
469 device->devname, device->devnode);
470 }
Daniel Stone74419a22012-05-30 16:32:02 +0100471
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300472 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300473}
474
Pekka Paalanen88594b62012-08-03 14:39:05 +0300475struct evdev_input_device *
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300476evdev_input_device_create(struct weston_seat *seat,
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300477 const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500478{
479 struct evdev_input_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500480 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300481 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500482
483 device = malloc(sizeof *device);
484 if (device == NULL)
485 return NULL;
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300486 memset(device, 0, sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500487
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300488 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400489 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500490 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400491
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300492 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200493 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300494 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400495 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500496 device->mt.slot = -1;
497 device->rel.dx = 0;
498 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200499 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300500 device->fd = device_fd;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500501
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300502 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
503 device->devname = strdup(devname);
504
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400505 if (evdev_configure_device(device) == -1)
506 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500507
Jonas Ådahl4136d822012-05-17 12:18:16 +0200508 /* If the dispatch was not set up use the fallback. */
509 if (device->dispatch == NULL)
510 device->dispatch = fallback_dispatch_create();
511 if (device->dispatch == NULL)
512 goto err1;
513
514
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300515 if (device->is_mt) {
516 device->mtdev = mtdev_new_open(device->fd);
517 if (!device->mtdev)
Martin Minarik6d118362012-06-07 18:01:59 +0200518 weston_log("mtdev failed to open for %s\n", path);
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300519 }
520
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500521 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500522 WL_EVENT_READABLE,
523 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400524 if (device->source == NULL)
Jonas Ådahl4136d822012-05-17 12:18:16 +0200525 goto err2;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500526
527 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400528
Jonas Ådahl4136d822012-05-17 12:18:16 +0200529err2:
530 device->dispatch->interface->destroy(device->dispatch);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400531err1:
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300532 free(device->devname);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400533 free(device->devnode);
534 free(device);
535 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500536}
537
Pekka Paalanen88594b62012-08-03 14:39:05 +0300538void
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300539evdev_input_device_destroy(struct evdev_input_device *device)
540{
541 struct evdev_dispatch *dispatch;
542
543 dispatch = device->dispatch;
544 if (dispatch)
545 dispatch->interface->destroy(dispatch);
546
547 wl_event_source_remove(device->source);
548 wl_list_remove(&device->link);
549 if (device->mtdev)
550 mtdev_close_delete(device->mtdev);
551 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300552 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300553 free(device->devnode);
554 free(device);
555}
556
Pekka Paalanen88594b62012-08-03 14:39:05 +0300557void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300558evdev_notify_keyboard_focus(struct weston_seat *seat,
559 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400560{
561 struct evdev_input_device *device;
562 struct wl_array keys;
563 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300564 char evdev_keys[(KEY_CNT + 7) / 8];
565 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400566 uint32_t *k;
567 int ret;
568
Pekka Paalanend8583512012-08-03 14:39:11 +0300569 if (!seat->seat.keyboard)
570 return;
571
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400572 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300573 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400574 memset(evdev_keys, 0, sizeof evdev_keys);
575 ret = ioctl(device->fd,
576 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
577 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200578 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400579 device->devnode);
580 continue;
581 }
582 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
583 all_keys[i] |= evdev_keys[i];
584 }
585
586 wl_array_init(&keys);
587 for (i = 0; i < KEY_CNT; i++) {
588 set = all_keys[i >> 3] & (1 << (i & 7));
589 if (set) {
590 k = wl_array_add(&keys, sizeof *k);
591 *k = i;
592 }
593 }
594
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300595 notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400596
597 wl_array_release(&keys);
598}