blob: 0b99c43649b76428fd8cc853a35ad8635b53ec17 [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
Jonas Ådahlb984e402012-10-03 22:56:58 +020033#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
34
Pekka Paalanen88594b62012-08-03 14:39:05 +030035void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030036evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010037{
38 static const struct {
39 enum weston_led weston;
40 int evdev;
41 } map[] = {
42 { LED_NUM_LOCK, LED_NUML },
43 { LED_CAPS_LOCK, LED_CAPSL },
44 { LED_SCROLL_LOCK, LED_SCROLLL },
45 };
Daniel Stone7bbd5b32012-05-30 16:31:49 +010046 struct input_event ev[ARRAY_LENGTH(map)];
47 unsigned int i;
48
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030049 if (!device->caps & EVDEV_KEYBOARD)
50 return;
51
Daniel Stone7bbd5b32012-05-30 16:31:49 +010052 memset(ev, 0, sizeof(ev));
53 for (i = 0; i < ARRAY_LENGTH(map); i++) {
54 ev[i].type = EV_LED;
55 ev[i].code = map[i].evdev;
56 ev[i].value = !!(leds & map[i].weston);
57 }
58
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030059 i = write(device->fd, ev, sizeof ev);
60 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010061}
62
Tiago Vignatti8be003b2011-09-01 19:00:03 +030063static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030064evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030065{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020066 if (e->value == 2)
67 return;
68
Tiago Vignatti8be003b2011-09-01 19:00:03 +030069 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030070 case BTN_LEFT:
71 case BTN_RIGHT:
72 case BTN_MIDDLE:
73 case BTN_SIDE:
74 case BTN_EXTRA:
75 case BTN_FORWARD:
76 case BTN_BACK:
77 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040078 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +010079 time, e->code,
80 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
81 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030082 break;
83
84 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040085 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +010086 time, e->code,
87 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +010088 WL_KEYBOARD_KEY_STATE_RELEASED,
89 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030090 break;
91 }
92}
93
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020094static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030095evdev_process_touch(struct evdev_device *device, 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
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300128evdev_process_absolute_motion(struct evdev_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
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300153evdev_process_relative(struct evdev_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:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200166 switch (e->value) {
167 case -1:
168 /* Scroll down */
169 case 1:
170 /* Scroll up */
171 notify_axis(device->seat,
172 time,
173 WL_POINTER_AXIS_VERTICAL_SCROLL,
174 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
175 break;
176 default:
177 break;
178 }
Scott Moreau210d0792012-03-22 10:47:01 -0600179 break;
180 case REL_HWHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200181 switch (e->value) {
182 case -1:
183 /* Scroll left */
184 case 1:
185 /* Scroll right */
186 notify_axis(device->seat,
187 time,
188 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
189 e->value * DEFAULT_AXIS_STEP_DISTANCE);
190 break;
191 default:
192 break;
193
194 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300195 }
196}
197
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200198static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300199evdev_process_absolute(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200200{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200201 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500202 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200203 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500204 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200205 }
206}
207
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400208static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200209is_motion_event(struct input_event *e)
210{
211 switch (e->type) {
212 case EV_REL:
213 switch (e->code) {
214 case REL_X:
215 case REL_Y:
216 return 1;
217 }
Rob Bradford4b997e42012-10-09 18:44:31 +0100218 break;
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200219 case EV_ABS:
220 switch (e->code) {
221 case ABS_X:
222 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200223 case ABS_MT_POSITION_X:
224 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200225 return 1;
226 }
227 }
228
229 return 0;
230}
231
232static void
Rob Bradfordea23b282012-12-03 19:44:16 +0000233transform_absolute(struct evdev_device *device)
234{
235 if (!device->abs.apply_calibration)
236 return;
237
238 device->abs.x = device->abs.x * device->abs.calibration[0] +
239 device->abs.y * device->abs.calibration[1] +
240 device->abs.calibration[2];
241
242 device->abs.y = device->abs.x * device->abs.calibration[3] +
243 device->abs.y * device->abs.calibration[4] +
244 device->abs.calibration[5];
245}
246
247static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300248evdev_flush_motion(struct evdev_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200249{
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300250 struct weston_seat *master = device->seat;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500251
Daniel Stone1d637772012-05-30 16:31:47 +0100252 if (!device->pending_events)
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200253 return;
254
Daniel Stone1d637772012-05-30 16:31:47 +0100255 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400256 notify_motion(master, time,
Daniel Stone37816df2012-05-16 18:45:18 +0100257 master->seat.pointer->x + device->rel.dx,
258 master->seat.pointer->y + device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100259 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500260 device->rel.dx = 0;
261 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200262 }
Daniel Stone1d637772012-05-30 16:31:47 +0100263 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400264 notify_touch(master, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500265 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400266 wl_fixed_from_int(device->mt.x[device->mt.slot]),
267 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100268 WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100269 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
270 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200271 }
Daniel Stone1d637772012-05-30 16:31:47 +0100272 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400273 notify_touch(master, time,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500274 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400275 wl_fixed_from_int(device->mt.x[device->mt.slot]),
276 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Daniel Stone37816df2012-05-16 18:45:18 +0100277 WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100278 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
279 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200280 }
Daniel Stone1d637772012-05-30 16:31:47 +0100281 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400282 notify_touch(master, time, device->mt.slot, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +0100283 WL_TOUCH_UP);
Daniel Stone1d637772012-05-30 16:31:47 +0100284 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200285 }
Daniel Stone1d637772012-05-30 16:31:47 +0100286 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
Rob Bradfordea23b282012-12-03 19:44:16 +0000287 transform_absolute(device);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400288 notify_motion(master, time,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400289 wl_fixed_from_int(device->abs.x),
290 wl_fixed_from_int(device->abs.y));
Daniel Stone1d637772012-05-30 16:31:47 +0100291 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200292 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200293}
294
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400295static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200296fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300297 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200298 struct input_event *event,
299 uint32_t time)
300{
301 switch (event->type) {
302 case EV_REL:
303 evdev_process_relative(device, event, time);
304 break;
305 case EV_ABS:
306 evdev_process_absolute(device, event);
307 break;
308 case EV_KEY:
309 evdev_process_key(device, event, time);
310 break;
311 }
312}
313
314static void
315fallback_destroy(struct evdev_dispatch *dispatch)
316{
317 free(dispatch);
318}
319
320struct evdev_dispatch_interface fallback_interface = {
321 fallback_process,
322 fallback_destroy
323};
324
325static struct evdev_dispatch *
326fallback_dispatch_create(void)
327{
328 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
329 if (dispatch == NULL)
330 return NULL;
331
332 dispatch->interface = &fallback_interface;
333
334 return dispatch;
335}
336
337static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300338evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400339 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500340{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200341 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400342 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500343 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500344
Daniel Stone1d637772012-05-30 16:31:47 +0100345 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200346
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500347 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400348 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500349 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500350 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
351
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200352 /* we try to minimize the amount of notifications to be
353 * forwarded to the compositor, so we accumulate motion
354 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200355 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500356 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200357
358 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500359 }
360
Kristian Høgsberg39373542011-12-21 22:18:36 -0500361 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400362}
363
364static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300365evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400366{
367 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300368 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400369 struct input_event ev[32];
370 int len;
371
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300372 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400373 if (!ec->focus)
374 return 1;
375
376 /* If the compositor is repainting, this function is called only once
377 * per frame and we have to process all the events available on the
378 * fd, otherwise there will be input lag. */
379 do {
380 if (device->mtdev)
381 len = mtdev_get(device->mtdev, fd, ev,
382 ARRAY_LENGTH(ev)) *
383 sizeof (struct input_event);
384 else
385 len = read(fd, &ev, sizeof ev);
386
387 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300388 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400389 return 1;
390 }
391
392 evdev_process_events(device, ev, len / sizeof ev[0]);
393
394 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400395
396 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500397}
398
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300399static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300400evdev_configure_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300401{
402 struct input_absinfo absinfo;
403 unsigned long ev_bits[NBITS(EV_MAX)];
404 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100405 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300406 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300407 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100408 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300409
410 has_key = 0;
411 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100412 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300413
414 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
415 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300416 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200417
Tiago Vignattid9c82502011-08-19 15:06:20 +0300418 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
419 abs_bits);
420 if (TEST_BIT(abs_bits, ABS_X)) {
421 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200422 device->abs.min_x = absinfo.minimum;
423 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100424 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300425 }
426 if (TEST_BIT(abs_bits, ABS_Y)) {
427 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200428 device->abs.min_y = absinfo.minimum;
429 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100430 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300431 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200432 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300433 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
434 &absinfo);
435 device->abs.min_x = absinfo.minimum;
436 device->abs.max_x = absinfo.maximum;
437 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
438 &absinfo);
439 device->abs.min_y = absinfo.minimum;
440 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200441 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500442 device->mt.slot = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100443 device->caps |= EVDEV_TOUCH;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200444 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300445 }
Daniel Stone33965c22012-05-30 16:31:48 +0100446 if (TEST_BIT(ev_bits, EV_REL)) {
447 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
448 rel_bits);
449 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
450 device->caps |= EVDEV_MOTION_REL;
451 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300452 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300453 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300454 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
455 key_bits);
456 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200457 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
458 has_abs)
459 device->dispatch = evdev_touchpad_create(device);
Daniel Stone33965c22012-05-30 16:31:48 +0100460 for (i = KEY_ESC; i < KEY_MAX; i++) {
461 if (i >= BTN_MISC && i < KEY_OK)
462 continue;
463 if (TEST_BIT(key_bits, i)) {
464 device->caps |= EVDEV_KEYBOARD;
465 break;
466 }
467 }
468 for (i = BTN_MISC; i < KEY_OK; i++) {
469 if (TEST_BIT(key_bits, i)) {
470 device->caps |= EVDEV_BUTTON;
471 break;
472 }
473 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300474 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100475 if (TEST_BIT(ev_bits, EV_LED)) {
476 device->caps |= EVDEV_KEYBOARD;
477 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300478
479 /* This rule tries to catch accelerometer devices and opt out. We may
480 * want to adjust the protocol later adding a proper event for dealing
481 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300482 if (has_abs && !has_key && !device->is_mt) {
483 weston_log("input device %s, %s "
484 "ignored: unsupported device type\n",
485 device->devname, device->devnode);
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300486 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300487 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300488
Daniel Stone74419a22012-05-30 16:32:02 +0100489 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300490 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300491 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000492 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
493 device->devname, device->devnode,
494 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
495 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
496 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300497 }
498 if ((device->caps & EVDEV_KEYBOARD)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300499 weston_seat_init_keyboard(device->seat, NULL);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300500 weston_log("input device %s, %s is a keyboard\n",
501 device->devname, device->devnode);
502 }
503 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300504 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300505 weston_log("input device %s, %s is a touch device\n",
506 device->devname, device->devnode);
507 }
Daniel Stone74419a22012-05-30 16:32:02 +0100508
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300509 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300510}
511
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300512struct evdev_device *
513evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500514{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300515 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500516 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300517 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500518
519 device = malloc(sizeof *device);
520 if (device == NULL)
521 return NULL;
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300522 memset(device, 0, sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500523
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300524 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400525 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500526 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400527
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300528 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200529 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300530 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400531 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500532 device->mt.slot = -1;
533 device->rel.dx = 0;
534 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200535 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300536 device->fd = device_fd;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500537
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300538 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
539 device->devname = strdup(devname);
540
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400541 if (evdev_configure_device(device) == -1)
542 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500543
Jonas Ådahl4136d822012-05-17 12:18:16 +0200544 /* If the dispatch was not set up use the fallback. */
545 if (device->dispatch == NULL)
546 device->dispatch = fallback_dispatch_create();
547 if (device->dispatch == NULL)
548 goto err1;
549
550
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300551 if (device->is_mt) {
552 device->mtdev = mtdev_new_open(device->fd);
553 if (!device->mtdev)
Martin Minarik6d118362012-06-07 18:01:59 +0200554 weston_log("mtdev failed to open for %s\n", path);
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300555 }
556
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500557 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500558 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300559 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400560 if (device->source == NULL)
Jonas Ådahl4136d822012-05-17 12:18:16 +0200561 goto err2;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500562
563 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400564
Jonas Ådahl4136d822012-05-17 12:18:16 +0200565err2:
566 device->dispatch->interface->destroy(device->dispatch);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400567err1:
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300568 free(device->devname);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400569 free(device->devnode);
570 free(device);
571 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500572}
573
Pekka Paalanen88594b62012-08-03 14:39:05 +0300574void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300575evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300576{
577 struct evdev_dispatch *dispatch;
578
579 dispatch = device->dispatch;
580 if (dispatch)
581 dispatch->interface->destroy(dispatch);
582
583 wl_event_source_remove(device->source);
584 wl_list_remove(&device->link);
585 if (device->mtdev)
586 mtdev_close_delete(device->mtdev);
587 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300588 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300589 free(device->devnode);
590 free(device);
591}
592
Pekka Paalanen88594b62012-08-03 14:39:05 +0300593void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300594evdev_notify_keyboard_focus(struct weston_seat *seat,
595 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400596{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300597 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400598 struct wl_array keys;
599 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300600 char evdev_keys[(KEY_CNT + 7) / 8];
601 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400602 uint32_t *k;
603 int ret;
604
Pekka Paalanend8583512012-08-03 14:39:11 +0300605 if (!seat->seat.keyboard)
606 return;
607
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400608 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300609 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400610 memset(evdev_keys, 0, sizeof evdev_keys);
611 ret = ioctl(device->fd,
612 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
613 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200614 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400615 device->devnode);
616 continue;
617 }
618 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
619 all_keys[i] |= evdev_keys[i];
620 }
621
622 wl_array_init(&keys);
623 for (i = 0; i < KEY_CNT; i++) {
624 set = all_keys[i >> 3] & (1 << (i & 7));
625 if (set) {
626 k = wl_array_add(&keys, sizeof *k);
627 *k = i;
628 }
629 }
630
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400631 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400632
633 wl_array_release(&keys);
634}