blob: 48e5470d0b48c77d2e4489d79623df030932c9e6 [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
Kristian Høgsberg43db4012011-01-14 14:45:42 -050025#include <stdlib.h>
26#include <string.h>
27#include <linux/input.h>
28#include <unistd.h>
29#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030030#include <mtdev.h>
Neil Robertsdaf7d472013-09-24 12:09:03 +010031#include <assert.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050032
33#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020034#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050035
Jonas Ådahlb984e402012-10-03 22:56:58 +020036#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
37
Pekka Paalanen88594b62012-08-03 14:39:05 +030038void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030039evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010040{
41 static const struct {
42 enum weston_led weston;
43 int evdev;
44 } map[] = {
45 { LED_NUM_LOCK, LED_NUML },
46 { LED_CAPS_LOCK, LED_CAPSL },
47 { LED_SCROLL_LOCK, LED_SCROLLL },
48 };
Rolf Morel14c98922013-08-09 16:32:17 +020049 struct input_event ev[ARRAY_LENGTH(map) + 1];
Daniel Stone7bbd5b32012-05-30 16:31:49 +010050 unsigned int i;
51
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030052 if (!device->caps & EVDEV_KEYBOARD)
53 return;
54
Daniel Stone7bbd5b32012-05-30 16:31:49 +010055 memset(ev, 0, sizeof(ev));
56 for (i = 0; i < ARRAY_LENGTH(map); i++) {
57 ev[i].type = EV_LED;
58 ev[i].code = map[i].evdev;
59 ev[i].value = !!(leds & map[i].weston);
60 }
Rolf Morel14c98922013-08-09 16:32:17 +020061 ev[i].type = EV_SYN;
62 ev[i].code = SYN_REPORT;
Daniel Stone7bbd5b32012-05-30 16:31:49 +010063
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030064 i = write(device->fd, ev, sizeof ev);
65 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010066}
67
Neil Robertsdaf7d472013-09-24 12:09:03 +010068static void
69transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
70{
71 if (!device->abs.apply_calibration) {
72 *x = device->abs.x;
73 *y = device->abs.y;
74 return;
75 } else {
76 *x = device->abs.x * device->abs.calibration[0] +
77 device->abs.y * device->abs.calibration[1] +
78 device->abs.calibration[2];
79
80 *y = device->abs.x * device->abs.calibration[3] +
81 device->abs.y * device->abs.calibration[4] +
82 device->abs.calibration[5];
83 }
84}
85
86static void
87evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
88{
89 struct weston_seat *master = device->seat;
90 wl_fixed_t x, y;
91 int32_t cx, cy;
92 int slot;
93
94 slot = device->mt.slot;
95
96 switch (device->pending_event) {
97 case EVDEV_NONE:
98 return;
99 case EVDEV_RELATIVE_MOTION:
100 notify_motion(master, time, device->rel.dx, device->rel.dy);
101 device->rel.dx = 0;
102 device->rel.dy = 0;
103 goto handled;
104 case EVDEV_ABSOLUTE_MT_DOWN:
105 weston_output_transform_coordinate(device->output,
106 device->mt.slots[slot].x,
107 device->mt.slots[slot].y,
108 &x, &y);
109 notify_touch(master, time,
110 slot, x, y, WL_TOUCH_DOWN);
111 goto handled;
112 case EVDEV_ABSOLUTE_MT_MOTION:
113 weston_output_transform_coordinate(device->output,
114 device->mt.slots[slot].x,
115 device->mt.slots[slot].y,
116 &x, &y);
117 notify_touch(master, time,
118 slot, x, y, WL_TOUCH_MOTION);
119 goto handled;
120 case EVDEV_ABSOLUTE_MT_UP:
121 notify_touch(master, time, slot, 0, 0,
122 WL_TOUCH_UP);
123 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100124 case EVDEV_ABSOLUTE_TOUCH_DOWN:
125 transform_absolute(device, &cx, &cy);
126 weston_output_transform_coordinate(device->output,
127 cx, cy, &x, &y);
128 notify_touch(master, time, 0, x, y, WL_TOUCH_DOWN);
129 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100130 case EVDEV_ABSOLUTE_MOTION:
131 transform_absolute(device, &cx, &cy);
132 weston_output_transform_coordinate(device->output,
133 cx, cy, &x, &y);
134
Neil Robertsbe336c82013-09-24 20:05:07 +0100135 if (device->caps & EVDEV_TOUCH)
136 notify_touch(master, time, 0, x, y, WL_TOUCH_MOTION);
137 else
Neil Robertsdaf7d472013-09-24 12:09:03 +0100138 notify_motion_absolute(master, time, x, y);
139 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100140 case EVDEV_ABSOLUTE_TOUCH_UP:
141 notify_touch(master, time, 0, 0, 0, WL_TOUCH_UP);
142 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100143 }
144
145 assert(0 && "Unknown pending event type");
146
147handled:
148 device->pending_event = EVDEV_NONE;
149}
150
Neil Robertsbe336c82013-09-24 20:05:07 +0100151static void
152evdev_process_touch_button(struct evdev_device *device, int time, int value)
153{
154 if (device->pending_event != EVDEV_NONE &&
155 device->pending_event != EVDEV_ABSOLUTE_MOTION)
156 evdev_flush_pending_event(device, time);
157
158 device->pending_event = (value ?
159 EVDEV_ABSOLUTE_TOUCH_DOWN :
160 EVDEV_ABSOLUTE_TOUCH_UP);
161}
162
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300163static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300164evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300165{
Peter Hutterer89af60e2013-08-07 11:04:42 +1000166 /* ignore kernel key repeat */
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200167 if (e->value == 2)
168 return;
169
Neil Robertsbe336c82013-09-24 20:05:07 +0100170 if (e->code == BTN_TOUCH) {
171 if (!device->is_mt)
172 evdev_process_touch_button(device, time, e->value);
173 return;
174 }
175
Neil Robertsdaf7d472013-09-24 12:09:03 +0100176 evdev_flush_pending_event(device, time);
177
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300178 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300179 case BTN_LEFT:
180 case BTN_RIGHT:
181 case BTN_MIDDLE:
182 case BTN_SIDE:
183 case BTN_EXTRA:
184 case BTN_FORWARD:
185 case BTN_BACK:
186 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400187 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100188 time, e->code,
189 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
190 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300191 break;
192
193 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400194 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100195 time, e->code,
196 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100197 WL_KEYBOARD_KEY_STATE_RELEASED,
198 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300199 break;
200 }
201}
202
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200203static void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100204evdev_process_touch(struct evdev_device *device,
205 struct input_event *e,
206 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200207{
Hardeningff39efa2013-09-18 23:56:35 +0200208 const int screen_width = device->output->current_mode->width;
209 const int screen_height = device->output->current_mode->height;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200210
211 switch (e->code) {
212 case ABS_MT_SLOT:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100213 evdev_flush_pending_event(device, time);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500214 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200215 break;
216 case ABS_MT_TRACKING_ID:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100217 if (device->pending_event != EVDEV_NONE &&
218 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
219 evdev_flush_pending_event(device, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200220 if (e->value >= 0)
Neil Robertsdaf7d472013-09-24 12:09:03 +0100221 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200222 else
Neil Robertsdaf7d472013-09-24 12:09:03 +0100223 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200224 break;
225 case ABS_MT_POSITION_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100226 device->mt.slots[device->mt.slot].x =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500227 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700228 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100229 if (device->pending_event == EVDEV_NONE)
230 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200231 break;
232 case ABS_MT_POSITION_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100233 device->mt.slots[device->mt.slot].y =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500234 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700235 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100236 if (device->pending_event == EVDEV_NONE)
237 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200238 break;
239 }
240}
241
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300242static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300243evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500244 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300245{
Hardeningff39efa2013-09-18 23:56:35 +0200246 const int screen_width = device->output->current_mode->width;
247 const int screen_height = device->output->current_mode->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300248
249 switch (e->code) {
250 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500251 device->abs.x =
252 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700253 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100254 if (device->pending_event == EVDEV_NONE)
255 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300256 break;
257 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500258 device->abs.y =
259 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700260 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100261 if (device->pending_event == EVDEV_NONE)
262 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300263 break;
264 }
265}
266
267static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300268evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200269 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300270{
271 switch (e->code) {
272 case REL_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100273 if (device->pending_event != EVDEV_RELATIVE_MOTION)
274 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400275 device->rel.dx += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100276 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300277 break;
278 case REL_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100279 if (device->pending_event != EVDEV_RELATIVE_MOTION)
280 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400281 device->rel.dy += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100282 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300283 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600284 case REL_WHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100285 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200286 switch (e->value) {
287 case -1:
288 /* Scroll down */
289 case 1:
290 /* Scroll up */
291 notify_axis(device->seat,
292 time,
293 WL_POINTER_AXIS_VERTICAL_SCROLL,
294 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
295 break;
296 default:
297 break;
298 }
Scott Moreau210d0792012-03-22 10:47:01 -0600299 break;
300 case REL_HWHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100301 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200302 switch (e->value) {
303 case -1:
304 /* Scroll left */
305 case 1:
306 /* Scroll right */
307 notify_axis(device->seat,
308 time,
309 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
310 e->value * DEFAULT_AXIS_STEP_DISTANCE);
311 break;
312 default:
313 break;
314
315 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300316 }
317}
318
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200319static inline void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100320evdev_process_absolute(struct evdev_device *device,
321 struct input_event *e,
322 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200323{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200324 if (device->is_mt) {
Neil Robertsdaf7d472013-09-24 12:09:03 +0100325 evdev_process_touch(device, e, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200326 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500327 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200328 }
329}
330
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400331static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200332fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300333 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200334 struct input_event *event,
335 uint32_t time)
336{
337 switch (event->type) {
338 case EV_REL:
339 evdev_process_relative(device, event, time);
340 break;
341 case EV_ABS:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100342 evdev_process_absolute(device, event, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200343 break;
344 case EV_KEY:
345 evdev_process_key(device, event, time);
346 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500347 case EV_SYN:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100348 evdev_flush_pending_event(device, time);
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500349 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200350 }
351}
352
353static void
354fallback_destroy(struct evdev_dispatch *dispatch)
355{
356 free(dispatch);
357}
358
359struct evdev_dispatch_interface fallback_interface = {
360 fallback_process,
361 fallback_destroy
362};
363
364static struct evdev_dispatch *
365fallback_dispatch_create(void)
366{
367 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
368 if (dispatch == NULL)
369 return NULL;
370
371 dispatch->interface = &fallback_interface;
372
373 return dispatch;
374}
375
376static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300377evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400378 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500379{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200380 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400381 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500382 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500383
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500384 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400385 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500386 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500387 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
388
Jonas Ådahl4136d822012-05-17 12:18:16 +0200389 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500390 }
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400391}
392
393static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300394evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400395{
396 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300397 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400398 struct input_event ev[32];
399 int len;
400
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300401 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400402 if (!ec->focus)
403 return 1;
404
405 /* If the compositor is repainting, this function is called only once
406 * per frame and we have to process all the events available on the
407 * fd, otherwise there will be input lag. */
408 do {
409 if (device->mtdev)
410 len = mtdev_get(device->mtdev, fd, ev,
411 ARRAY_LENGTH(ev)) *
412 sizeof (struct input_event);
413 else
414 len = read(fd, &ev, sizeof ev);
415
416 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300417 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400418 return 1;
419 }
420
421 evdev_process_events(device, ev, len / sizeof ev[0]);
422
423 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400424
425 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500426}
427
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300428static int
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500429evdev_handle_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300430{
431 struct input_absinfo absinfo;
432 unsigned long ev_bits[NBITS(EV_MAX)];
433 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100434 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300435 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300436 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100437 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300438
439 has_key = 0;
440 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100441 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300442
443 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
444 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300445 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200446
Tiago Vignattid9c82502011-08-19 15:06:20 +0300447 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
448 abs_bits);
Kristian Høgsbergb1c02a82013-08-13 14:55:39 -0700449
450 if (TEST_BIT(abs_bits, ABS_WHEEL) ||
451 TEST_BIT(abs_bits, ABS_GAS) ||
452 TEST_BIT(abs_bits, ABS_BRAKE) ||
453 TEST_BIT(abs_bits, ABS_HAT0X)) {
454 weston_log("device %s is a joystick, ignoring\n",
455 device->devnode);
456 return 0;
457 }
458
Tiago Vignattid9c82502011-08-19 15:06:20 +0300459 if (TEST_BIT(abs_bits, ABS_X)) {
460 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200461 device->abs.min_x = absinfo.minimum;
462 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100463 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300464 }
465 if (TEST_BIT(abs_bits, ABS_Y)) {
466 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200467 device->abs.min_y = absinfo.minimum;
468 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100469 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300470 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000471 /* We only handle the slotted Protocol B in weston.
472 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
473 require mtdev for conversion. */
474 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
475 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300476 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
477 &absinfo);
478 device->abs.min_x = absinfo.minimum;
479 device->abs.max_x = absinfo.maximum;
480 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
481 &absinfo);
482 device->abs.min_y = absinfo.minimum;
483 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200484 device->is_mt = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100485 device->caps |= EVDEV_TOUCH;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000486
487 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
488 device->mtdev = mtdev_new_open(device->fd);
489 if (!device->mtdev) {
490 weston_log("mtdev required but failed to open for %s\n",
491 device->devnode);
492 return 0;
493 }
Peter Huttererdf66c5b2013-08-07 11:04:46 +1000494 device->mt.slot = device->mtdev->caps.slot.value;
495 } else {
496 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
497 &absinfo);
498 device->mt.slot = absinfo.value;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000499 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200500 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300501 }
Daniel Stone33965c22012-05-30 16:31:48 +0100502 if (TEST_BIT(ev_bits, EV_REL)) {
503 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
504 rel_bits);
505 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
506 device->caps |= EVDEV_MOTION_REL;
507 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300508 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300509 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300510 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
511 key_bits);
512 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200513 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
Peter Hutterer4477fee2013-08-07 11:04:49 +1000514 has_abs) {
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200515 device->dispatch = evdev_touchpad_create(device);
Peter Hutterer4477fee2013-08-07 11:04:49 +1000516 weston_log("input device %s, %s is a touchpad\n",
517 device->devname, device->devnode);
518 }
Daniel Stone33965c22012-05-30 16:31:48 +0100519 for (i = KEY_ESC; i < KEY_MAX; i++) {
520 if (i >= BTN_MISC && i < KEY_OK)
521 continue;
522 if (TEST_BIT(key_bits, i)) {
523 device->caps |= EVDEV_KEYBOARD;
524 break;
525 }
526 }
527 for (i = BTN_MISC; i < KEY_OK; i++) {
528 if (TEST_BIT(key_bits, i)) {
529 device->caps |= EVDEV_BUTTON;
530 break;
531 }
532 }
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700533 if (TEST_BIT(key_bits, BTN_TOUCH)) {
534 device->caps |= EVDEV_TOUCH;
535 }
536
Tiago Vignattid9c82502011-08-19 15:06:20 +0300537 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100538 if (TEST_BIT(ev_bits, EV_LED)) {
539 device->caps |= EVDEV_KEYBOARD;
540 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300541
542 /* This rule tries to catch accelerometer devices and opt out. We may
543 * want to adjust the protocol later adding a proper event for dealing
544 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300545 if (has_abs && !has_key && !device->is_mt) {
546 weston_log("input device %s, %s "
547 "ignored: unsupported device type\n",
548 device->devname, device->devnode);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500549 return 0;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300550 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300551
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500552 return 1;
553}
554
555static int
556evdev_configure_device(struct evdev_device *device)
557{
Daniel Stone74419a22012-05-30 16:32:02 +0100558 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300559 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300560 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000561 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
562 device->devname, device->devnode,
563 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
564 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
565 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300566 }
567 if ((device->caps & EVDEV_KEYBOARD)) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500568 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
569 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300570 weston_log("input device %s, %s is a keyboard\n",
571 device->devname, device->devnode);
572 }
573 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300574 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300575 weston_log("input device %s, %s is a touch device\n",
576 device->devname, device->devnode);
577 }
Daniel Stone74419a22012-05-30 16:32:02 +0100578
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300579 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300580}
581
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300582struct evdev_device *
583evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500584{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300585 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500586 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300587 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500588
Peter Huttererf3d62272013-08-08 11:57:05 +1000589 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500590 if (device == NULL)
591 return NULL;
592
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300593 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400594 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500595 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400596
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300597 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200598 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300599 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400600 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500601 device->mt.slot = -1;
602 device->rel.dx = 0;
603 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200604 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300605 device->fd = device_fd;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100606 device->pending_event = EVDEV_NONE;
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700607 wl_list_init(&device->link);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500608
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300609 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
Peter Hutterer11f5bfb2013-08-07 11:04:41 +1000610 devname[sizeof(devname) - 1] = '\0';
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300611 device->devname = strdup(devname);
612
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500613 if (!evdev_handle_device(device)) {
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000614 evdev_device_destroy(device);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500615 return EVDEV_UNHANDLED_DEVICE;
616 }
617
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400618 if (evdev_configure_device(device) == -1)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000619 goto err;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500620
Jonas Ådahl4136d822012-05-17 12:18:16 +0200621 /* If the dispatch was not set up use the fallback. */
622 if (device->dispatch == NULL)
623 device->dispatch = fallback_dispatch_create();
624 if (device->dispatch == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000625 goto err;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200626
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500627 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500628 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300629 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400630 if (device->source == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000631 goto err;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500632
633 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400634
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000635err:
636 evdev_device_destroy(device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400637 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500638}
639
Pekka Paalanen88594b62012-08-03 14:39:05 +0300640void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300641evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300642{
643 struct evdev_dispatch *dispatch;
644
645 dispatch = device->dispatch;
646 if (dispatch)
647 dispatch->interface->destroy(dispatch);
648
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000649 if (device->source)
650 wl_event_source_remove(device->source);
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700651 wl_list_remove(&device->link);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300652 if (device->mtdev)
653 mtdev_close_delete(device->mtdev);
654 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300655 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300656 free(device->devnode);
657 free(device);
658}
659
Pekka Paalanen88594b62012-08-03 14:39:05 +0300660void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300661evdev_notify_keyboard_focus(struct weston_seat *seat,
662 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400663{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300664 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400665 struct wl_array keys;
666 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300667 char evdev_keys[(KEY_CNT + 7) / 8];
668 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400669 uint32_t *k;
670 int ret;
671
Kristian Høgsberge3148752013-05-06 23:19:49 -0400672 if (!seat->keyboard)
Pekka Paalanend8583512012-08-03 14:39:11 +0300673 return;
674
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400675 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300676 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400677 memset(evdev_keys, 0, sizeof evdev_keys);
678 ret = ioctl(device->fd,
679 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
680 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200681 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400682 device->devnode);
683 continue;
684 }
685 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
686 all_keys[i] |= evdev_keys[i];
687 }
688
689 wl_array_init(&keys);
690 for (i = 0; i < KEY_CNT; i++) {
691 set = all_keys[i >> 3] & (1 << (i & 7));
692 if (set) {
693 k = wl_array_add(&keys, sizeof *k);
694 *k = i;
695 }
696 }
697
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400698 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400699
700 wl_array_release(&keys);
701}