blob: 7397ea1d06f14885e99fee12652c9a7ca958feee [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
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
David Herrmanne05a0cd2013-10-15 14:29:56 +020025#include <errno.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050026#include <stdlib.h>
27#include <string.h>
28#include <linux/input.h>
29#include <unistd.h>
30#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030031#include <mtdev.h>
Neil Robertsdaf7d472013-09-24 12:09:03 +010032#include <assert.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050033
34#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020035#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050036
Jonas Ådahlb984e402012-10-03 22:56:58 +020037#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
38
Pekka Paalanen88594b62012-08-03 14:39:05 +030039void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030040evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010041{
42 static const struct {
43 enum weston_led weston;
44 int evdev;
45 } map[] = {
46 { LED_NUM_LOCK, LED_NUML },
47 { LED_CAPS_LOCK, LED_CAPSL },
48 { LED_SCROLL_LOCK, LED_SCROLLL },
49 };
Rolf Morel14c98922013-08-09 16:32:17 +020050 struct input_event ev[ARRAY_LENGTH(map) + 1];
Daniel Stone7bbd5b32012-05-30 16:31:49 +010051 unsigned int i;
52
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030053 if (!device->caps & EVDEV_KEYBOARD)
54 return;
55
Daniel Stone7bbd5b32012-05-30 16:31:49 +010056 memset(ev, 0, sizeof(ev));
57 for (i = 0; i < ARRAY_LENGTH(map); i++) {
58 ev[i].type = EV_LED;
59 ev[i].code = map[i].evdev;
60 ev[i].value = !!(leds & map[i].weston);
61 }
Rolf Morel14c98922013-08-09 16:32:17 +020062 ev[i].type = EV_SYN;
63 ev[i].code = SYN_REPORT;
Daniel Stone7bbd5b32012-05-30 16:31:49 +010064
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030065 i = write(device->fd, ev, sizeof ev);
66 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010067}
68
Neil Robertsdaf7d472013-09-24 12:09:03 +010069static void
70transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
71{
72 if (!device->abs.apply_calibration) {
73 *x = device->abs.x;
74 *y = device->abs.y;
75 return;
76 } else {
77 *x = device->abs.x * device->abs.calibration[0] +
78 device->abs.y * device->abs.calibration[1] +
79 device->abs.calibration[2];
80
81 *y = device->abs.x * device->abs.calibration[3] +
82 device->abs.y * device->abs.calibration[4] +
83 device->abs.calibration[5];
84 }
85}
86
87static void
88evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
89{
90 struct weston_seat *master = device->seat;
91 wl_fixed_t x, y;
92 int32_t cx, cy;
93 int slot;
94
95 slot = device->mt.slot;
96
97 switch (device->pending_event) {
98 case EVDEV_NONE:
99 return;
100 case EVDEV_RELATIVE_MOTION:
101 notify_motion(master, time, device->rel.dx, device->rel.dy);
102 device->rel.dx = 0;
103 device->rel.dy = 0;
104 goto handled;
105 case EVDEV_ABSOLUTE_MT_DOWN:
106 weston_output_transform_coordinate(device->output,
107 device->mt.slots[slot].x,
108 device->mt.slots[slot].y,
109 &x, &y);
110 notify_touch(master, time,
111 slot, x, y, WL_TOUCH_DOWN);
112 goto handled;
113 case EVDEV_ABSOLUTE_MT_MOTION:
114 weston_output_transform_coordinate(device->output,
115 device->mt.slots[slot].x,
116 device->mt.slots[slot].y,
117 &x, &y);
118 notify_touch(master, time,
119 slot, x, y, WL_TOUCH_MOTION);
120 goto handled;
121 case EVDEV_ABSOLUTE_MT_UP:
122 notify_touch(master, time, slot, 0, 0,
123 WL_TOUCH_UP);
124 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100125 case EVDEV_ABSOLUTE_TOUCH_DOWN:
126 transform_absolute(device, &cx, &cy);
127 weston_output_transform_coordinate(device->output,
128 cx, cy, &x, &y);
129 notify_touch(master, time, 0, x, y, WL_TOUCH_DOWN);
130 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100131 case EVDEV_ABSOLUTE_MOTION:
132 transform_absolute(device, &cx, &cy);
133 weston_output_transform_coordinate(device->output,
134 cx, cy, &x, &y);
135
Neil Robertsbe336c82013-09-24 20:05:07 +0100136 if (device->caps & EVDEV_TOUCH)
137 notify_touch(master, time, 0, x, y, WL_TOUCH_MOTION);
138 else
Neil Robertsdaf7d472013-09-24 12:09:03 +0100139 notify_motion_absolute(master, time, x, y);
140 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100141 case EVDEV_ABSOLUTE_TOUCH_UP:
142 notify_touch(master, time, 0, 0, 0, WL_TOUCH_UP);
143 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100144 }
145
146 assert(0 && "Unknown pending event type");
147
148handled:
149 device->pending_event = EVDEV_NONE;
150}
151
Neil Robertsbe336c82013-09-24 20:05:07 +0100152static void
153evdev_process_touch_button(struct evdev_device *device, int time, int value)
154{
155 if (device->pending_event != EVDEV_NONE &&
156 device->pending_event != EVDEV_ABSOLUTE_MOTION)
157 evdev_flush_pending_event(device, time);
158
159 device->pending_event = (value ?
160 EVDEV_ABSOLUTE_TOUCH_DOWN :
161 EVDEV_ABSOLUTE_TOUCH_UP);
162}
163
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300164static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300165evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300166{
Peter Hutterer89af60e2013-08-07 11:04:42 +1000167 /* ignore kernel key repeat */
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200168 if (e->value == 2)
169 return;
170
Neil Robertsbe336c82013-09-24 20:05:07 +0100171 if (e->code == BTN_TOUCH) {
172 if (!device->is_mt)
173 evdev_process_touch_button(device, time, e->value);
174 return;
175 }
176
Neil Robertsdaf7d472013-09-24 12:09:03 +0100177 evdev_flush_pending_event(device, time);
178
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300179 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300180 case BTN_LEFT:
181 case BTN_RIGHT:
182 case BTN_MIDDLE:
183 case BTN_SIDE:
184 case BTN_EXTRA:
185 case BTN_FORWARD:
186 case BTN_BACK:
187 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400188 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100189 time, e->code,
190 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
191 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300192 break;
193
194 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400195 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100196 time, e->code,
197 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100198 WL_KEYBOARD_KEY_STATE_RELEASED,
199 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300200 break;
201 }
202}
203
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200204static void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100205evdev_process_touch(struct evdev_device *device,
206 struct input_event *e,
207 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200208{
Hardeningff39efa2013-09-18 23:56:35 +0200209 const int screen_width = device->output->current_mode->width;
210 const int screen_height = device->output->current_mode->height;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200211
212 switch (e->code) {
213 case ABS_MT_SLOT:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100214 evdev_flush_pending_event(device, time);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500215 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200216 break;
217 case ABS_MT_TRACKING_ID:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100218 if (device->pending_event != EVDEV_NONE &&
219 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
220 evdev_flush_pending_event(device, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200221 if (e->value >= 0)
Neil Robertsdaf7d472013-09-24 12:09:03 +0100222 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200223 else
Neil Robertsdaf7d472013-09-24 12:09:03 +0100224 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200225 break;
226 case ABS_MT_POSITION_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100227 device->mt.slots[device->mt.slot].x =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500228 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700229 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100230 if (device->pending_event == EVDEV_NONE)
231 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200232 break;
233 case ABS_MT_POSITION_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100234 device->mt.slots[device->mt.slot].y =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500235 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700236 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100237 if (device->pending_event == EVDEV_NONE)
238 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200239 break;
240 }
241}
242
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300243static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300244evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500245 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300246{
Hardeningff39efa2013-09-18 23:56:35 +0200247 const int screen_width = device->output->current_mode->width;
248 const int screen_height = device->output->current_mode->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300249
250 switch (e->code) {
251 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500252 device->abs.x =
253 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700254 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100255 if (device->pending_event == EVDEV_NONE)
256 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300257 break;
258 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500259 device->abs.y =
260 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700261 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100262 if (device->pending_event == EVDEV_NONE)
263 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300264 break;
265 }
266}
267
268static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300269evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200270 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300271{
272 switch (e->code) {
273 case REL_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100274 if (device->pending_event != EVDEV_RELATIVE_MOTION)
275 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400276 device->rel.dx += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100277 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300278 break;
279 case REL_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100280 if (device->pending_event != EVDEV_RELATIVE_MOTION)
281 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400282 device->rel.dy += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100283 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300284 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600285 case REL_WHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100286 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200287 switch (e->value) {
288 case -1:
289 /* Scroll down */
290 case 1:
291 /* Scroll up */
292 notify_axis(device->seat,
293 time,
294 WL_POINTER_AXIS_VERTICAL_SCROLL,
295 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
296 break;
297 default:
298 break;
299 }
Scott Moreau210d0792012-03-22 10:47:01 -0600300 break;
301 case REL_HWHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100302 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200303 switch (e->value) {
304 case -1:
305 /* Scroll left */
306 case 1:
307 /* Scroll right */
308 notify_axis(device->seat,
309 time,
310 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
311 e->value * DEFAULT_AXIS_STEP_DISTANCE);
312 break;
313 default:
314 break;
315
316 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300317 }
318}
319
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200320static inline void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100321evdev_process_absolute(struct evdev_device *device,
322 struct input_event *e,
323 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200324{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200325 if (device->is_mt) {
Neil Robertsdaf7d472013-09-24 12:09:03 +0100326 evdev_process_touch(device, e, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200327 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500328 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200329 }
330}
331
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400332static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200333fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300334 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200335 struct input_event *event,
336 uint32_t time)
337{
338 switch (event->type) {
339 case EV_REL:
340 evdev_process_relative(device, event, time);
341 break;
342 case EV_ABS:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100343 evdev_process_absolute(device, event, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200344 break;
345 case EV_KEY:
346 evdev_process_key(device, event, time);
347 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500348 case EV_SYN:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100349 evdev_flush_pending_event(device, time);
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500350 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200351 }
352}
353
354static void
355fallback_destroy(struct evdev_dispatch *dispatch)
356{
357 free(dispatch);
358}
359
360struct evdev_dispatch_interface fallback_interface = {
361 fallback_process,
362 fallback_destroy
363};
364
365static struct evdev_dispatch *
366fallback_dispatch_create(void)
367{
368 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
369 if (dispatch == NULL)
370 return NULL;
371
372 dispatch->interface = &fallback_interface;
373
374 return dispatch;
375}
376
377static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300378evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400379 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500380{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200381 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400382 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500383 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500384
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500385 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400386 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500387 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500388 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
389
Jonas Ådahl4136d822012-05-17 12:18:16 +0200390 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500391 }
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400392}
393
394static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300395evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400396{
397 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300398 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400399 struct input_event ev[32];
400 int len;
401
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300402 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400403 if (!ec->focus)
404 return 1;
405
406 /* If the compositor is repainting, this function is called only once
407 * per frame and we have to process all the events available on the
408 * fd, otherwise there will be input lag. */
409 do {
410 if (device->mtdev)
411 len = mtdev_get(device->mtdev, fd, ev,
412 ARRAY_LENGTH(ev)) *
413 sizeof (struct input_event);
414 else
415 len = read(fd, &ev, sizeof ev);
416
417 if (len < 0 || len % sizeof ev[0] != 0) {
David Herrmanne05a0cd2013-10-15 14:29:56 +0200418 if (len < 0 && errno != EAGAIN && errno != EINTR) {
419 weston_log("device %s died\n",
420 device->devnode);
421 wl_event_source_remove(device->source);
422 device->source = NULL;
423 }
424
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400425 return 1;
426 }
427
428 evdev_process_events(device, ev, len / sizeof ev[0]);
429
430 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400431
432 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500433}
434
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300435static int
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500436evdev_handle_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300437{
438 struct input_absinfo absinfo;
439 unsigned long ev_bits[NBITS(EV_MAX)];
440 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100441 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300442 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300443 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100444 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300445
446 has_key = 0;
447 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100448 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300449
450 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
451 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300452 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200453
Tiago Vignattid9c82502011-08-19 15:06:20 +0300454 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
455 abs_bits);
Kristian Høgsbergb1c02a82013-08-13 14:55:39 -0700456
457 if (TEST_BIT(abs_bits, ABS_WHEEL) ||
458 TEST_BIT(abs_bits, ABS_GAS) ||
459 TEST_BIT(abs_bits, ABS_BRAKE) ||
460 TEST_BIT(abs_bits, ABS_HAT0X)) {
461 weston_log("device %s is a joystick, ignoring\n",
462 device->devnode);
463 return 0;
464 }
465
Tiago Vignattid9c82502011-08-19 15:06:20 +0300466 if (TEST_BIT(abs_bits, ABS_X)) {
467 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200468 device->abs.min_x = absinfo.minimum;
469 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100470 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300471 }
472 if (TEST_BIT(abs_bits, ABS_Y)) {
473 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200474 device->abs.min_y = absinfo.minimum;
475 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100476 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300477 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000478 /* We only handle the slotted Protocol B in weston.
479 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
480 require mtdev for conversion. */
481 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
482 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300483 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
484 &absinfo);
485 device->abs.min_x = absinfo.minimum;
486 device->abs.max_x = absinfo.maximum;
487 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
488 &absinfo);
489 device->abs.min_y = absinfo.minimum;
490 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200491 device->is_mt = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100492 device->caps |= EVDEV_TOUCH;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000493
494 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
495 device->mtdev = mtdev_new_open(device->fd);
496 if (!device->mtdev) {
497 weston_log("mtdev required but failed to open for %s\n",
498 device->devnode);
499 return 0;
500 }
Peter Huttererdf66c5b2013-08-07 11:04:46 +1000501 device->mt.slot = device->mtdev->caps.slot.value;
502 } else {
503 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
504 &absinfo);
505 device->mt.slot = absinfo.value;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000506 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200507 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300508 }
Daniel Stone33965c22012-05-30 16:31:48 +0100509 if (TEST_BIT(ev_bits, EV_REL)) {
510 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
511 rel_bits);
512 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
513 device->caps |= EVDEV_MOTION_REL;
514 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300515 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300516 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300517 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
518 key_bits);
519 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200520 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
Peter Hutterer4477fee2013-08-07 11:04:49 +1000521 has_abs) {
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200522 device->dispatch = evdev_touchpad_create(device);
Peter Hutterer4477fee2013-08-07 11:04:49 +1000523 weston_log("input device %s, %s is a touchpad\n",
524 device->devname, device->devnode);
525 }
Daniel Stone33965c22012-05-30 16:31:48 +0100526 for (i = KEY_ESC; i < KEY_MAX; i++) {
527 if (i >= BTN_MISC && i < KEY_OK)
528 continue;
529 if (TEST_BIT(key_bits, i)) {
530 device->caps |= EVDEV_KEYBOARD;
531 break;
532 }
533 }
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700534 if (TEST_BIT(key_bits, BTN_TOUCH)) {
535 device->caps |= EVDEV_TOUCH;
536 }
Kristian Høgsbergc133fc42013-10-14 15:46:13 -0700537 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
538 if (TEST_BIT(key_bits, i)) {
539 device->caps |= EVDEV_BUTTON;
540 device->caps &= ~EVDEV_TOUCH;
541 break;
542 }
543 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300544 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100545 if (TEST_BIT(ev_bits, EV_LED)) {
546 device->caps |= EVDEV_KEYBOARD;
547 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300548
549 /* This rule tries to catch accelerometer devices and opt out. We may
550 * want to adjust the protocol later adding a proper event for dealing
551 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300552 if (has_abs && !has_key && !device->is_mt) {
553 weston_log("input device %s, %s "
554 "ignored: unsupported device type\n",
555 device->devname, device->devnode);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500556 return 0;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300557 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300558
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500559 return 1;
560}
561
562static int
563evdev_configure_device(struct evdev_device *device)
564{
Kristian Høgsberg9df41e12013-10-14 15:32:08 -0700565 if ((device->caps & (EVDEV_MOTION_ABS | EVDEV_MOTION_REL)) &&
566 (device->caps & EVDEV_BUTTON)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300567 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000568 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
569 device->devname, device->devnode,
570 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
571 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
572 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300573 }
574 if ((device->caps & EVDEV_KEYBOARD)) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500575 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
576 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300577 weston_log("input device %s, %s is a keyboard\n",
578 device->devname, device->devnode);
579 }
580 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300581 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300582 weston_log("input device %s, %s is a touch device\n",
583 device->devname, device->devnode);
584 }
Daniel Stone74419a22012-05-30 16:32:02 +0100585
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300586 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300587}
588
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300589struct evdev_device *
590evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500591{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300592 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300594 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500595
Peter Huttererf3d62272013-08-08 11:57:05 +1000596 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500597 if (device == NULL)
598 return NULL;
599
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300600 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400601 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500602 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400603
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300604 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200605 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300606 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400607 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500608 device->mt.slot = -1;
609 device->rel.dx = 0;
610 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200611 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300612 device->fd = device_fd;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100613 device->pending_event = EVDEV_NONE;
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700614 wl_list_init(&device->link);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500615
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300616 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
Peter Hutterer11f5bfb2013-08-07 11:04:41 +1000617 devname[sizeof(devname) - 1] = '\0';
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300618 device->devname = strdup(devname);
619
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500620 if (!evdev_handle_device(device)) {
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000621 evdev_device_destroy(device);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500622 return EVDEV_UNHANDLED_DEVICE;
623 }
624
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400625 if (evdev_configure_device(device) == -1)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000626 goto err;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500627
Jonas Ådahl4136d822012-05-17 12:18:16 +0200628 /* If the dispatch was not set up use the fallback. */
629 if (device->dispatch == NULL)
630 device->dispatch = fallback_dispatch_create();
631 if (device->dispatch == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000632 goto err;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200633
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500634 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500635 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300636 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400637 if (device->source == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000638 goto err;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500639
640 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400641
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000642err:
643 evdev_device_destroy(device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400644 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500645}
646
Pekka Paalanen88594b62012-08-03 14:39:05 +0300647void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300648evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300649{
650 struct evdev_dispatch *dispatch;
651
652 dispatch = device->dispatch;
653 if (dispatch)
654 dispatch->interface->destroy(dispatch);
655
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000656 if (device->source)
657 wl_event_source_remove(device->source);
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700658 wl_list_remove(&device->link);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300659 if (device->mtdev)
660 mtdev_close_delete(device->mtdev);
661 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300662 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300663 free(device->devnode);
664 free(device);
665}
666
Pekka Paalanen88594b62012-08-03 14:39:05 +0300667void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300668evdev_notify_keyboard_focus(struct weston_seat *seat,
669 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400670{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300671 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400672 struct wl_array keys;
673 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300674 char evdev_keys[(KEY_CNT + 7) / 8];
675 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400676 uint32_t *k;
677 int ret;
678
Kristian Høgsberge3148752013-05-06 23:19:49 -0400679 if (!seat->keyboard)
Pekka Paalanend8583512012-08-03 14:39:11 +0300680 return;
681
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400682 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300683 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400684 memset(evdev_keys, 0, sizeof evdev_keys);
685 ret = ioctl(device->fd,
686 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
687 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200688 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400689 device->devnode);
690 continue;
691 }
692 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
693 all_keys[i] |= evdev_keys[i];
694 }
695
696 wl_array_init(&keys);
697 for (i = 0; i < KEY_CNT; i++) {
698 set = all_keys[i >> 3] & (1 << (i & 7));
699 if (set) {
700 k = wl_array_add(&keys, sizeof *k);
701 *k = i;
702 }
703 }
704
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400705 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400706
707 wl_array_release(&keys);
708}