blob: af60dbc49f99f15f4521d809237f41088e11ec33 [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>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050031
32#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020033#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050034
Jonas Ådahlb984e402012-10-03 22:56:58 +020035#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
36
Pekka Paalanen88594b62012-08-03 14:39:05 +030037void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030038evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010039{
40 static const struct {
41 enum weston_led weston;
42 int evdev;
43 } map[] = {
44 { LED_NUM_LOCK, LED_NUML },
45 { LED_CAPS_LOCK, LED_CAPSL },
46 { LED_SCROLL_LOCK, LED_SCROLLL },
47 };
Daniel Stone7bbd5b32012-05-30 16:31:49 +010048 struct input_event ev[ARRAY_LENGTH(map)];
49 unsigned int i;
50
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030051 if (!device->caps & EVDEV_KEYBOARD)
52 return;
53
Daniel Stone7bbd5b32012-05-30 16:31:49 +010054 memset(ev, 0, sizeof(ev));
55 for (i = 0; i < ARRAY_LENGTH(map); i++) {
56 ev[i].type = EV_LED;
57 ev[i].code = map[i].evdev;
58 ev[i].value = !!(leds & map[i].weston);
59 }
60
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030061 i = write(device->fd, ev, sizeof ev);
62 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010063}
64
Tiago Vignatti8be003b2011-09-01 19:00:03 +030065static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030066evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030067{
Peter Hutterer89af60e2013-08-07 11:04:42 +100068 /* ignore kernel key repeat */
Tiago Vignatti8755ff92011-11-10 14:47:30 +020069 if (e->value == 2)
70 return;
71
Tiago Vignatti8be003b2011-09-01 19:00:03 +030072 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030073 case BTN_LEFT:
74 case BTN_RIGHT:
75 case BTN_MIDDLE:
76 case BTN_SIDE:
77 case BTN_EXTRA:
78 case BTN_FORWARD:
79 case BTN_BACK:
80 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040081 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +010082 time, e->code,
83 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
84 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030085 break;
86
Kristian Høgsberg0af26c42013-07-26 10:43:26 -070087 case BTN_TOUCH:
88 if (e->value == 0 && !device->is_mt)
89 notify_touch(device->seat, time, device->mt.slot, 0, 0,
90 WL_TOUCH_UP);
91 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030092 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040093 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +010094 time, e->code,
95 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +010096 WL_KEYBOARD_KEY_STATE_RELEASED,
97 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030098 break;
99 }
100}
101
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200102static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300103evdev_process_touch(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200104{
105 const int screen_width = device->output->current->width;
106 const int screen_height = device->output->current->height;
107
108 switch (e->code) {
109 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500110 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200111 break;
112 case ABS_MT_TRACKING_ID:
113 if (e->value >= 0)
Daniel Stone1d637772012-05-30 16:31:47 +0100114 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200115 else
Daniel Stone1d637772012-05-30 16:31:47 +0100116 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200117 break;
118 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500119 device->mt.x[device->mt.slot] =
120 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700121 (device->abs.max_x - device->abs.min_x);
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 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500125 device->mt.y[device->mt.slot] =
126 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700127 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100128 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200129 break;
130 }
131}
132
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300133static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300134evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500135 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300136{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400137 const int screen_width = device->output->current->width;
138 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300139
140 switch (e->code) {
141 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500142 device->abs.x =
143 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700144 (device->abs.max_x - device->abs.min_x);
Daniel Stone1d637772012-05-30 16:31:47 +0100145 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300146 break;
147 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500148 device->abs.y =
149 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700150 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100151 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300152 break;
153 }
154}
155
156static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300157evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200158 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300159{
160 switch (e->code) {
161 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400162 device->rel.dx += 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;
165 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400166 device->rel.dy += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100167 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300168 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600169 case REL_WHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200170 switch (e->value) {
171 case -1:
172 /* Scroll down */
173 case 1:
174 /* Scroll up */
175 notify_axis(device->seat,
176 time,
177 WL_POINTER_AXIS_VERTICAL_SCROLL,
178 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
179 break;
180 default:
181 break;
182 }
Scott Moreau210d0792012-03-22 10:47:01 -0600183 break;
184 case REL_HWHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200185 switch (e->value) {
186 case -1:
187 /* Scroll left */
188 case 1:
189 /* Scroll right */
190 notify_axis(device->seat,
191 time,
192 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
193 e->value * DEFAULT_AXIS_STEP_DISTANCE);
194 break;
195 default:
196 break;
197
198 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300199 }
200}
201
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200202static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300203evdev_process_absolute(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200204{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200205 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500206 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200207 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500208 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200209 }
210}
211
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400212static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200213is_motion_event(struct input_event *e)
214{
215 switch (e->type) {
216 case EV_REL:
217 switch (e->code) {
218 case REL_X:
219 case REL_Y:
220 return 1;
221 }
Rob Bradford4b997e42012-10-09 18:44:31 +0100222 break;
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200223 case EV_ABS:
224 switch (e->code) {
225 case ABS_X:
226 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200227 case ABS_MT_POSITION_X:
228 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200229 return 1;
230 }
231 }
232
233 return 0;
234}
235
236static void
Rob Bradfordea23b282012-12-03 19:44:16 +0000237transform_absolute(struct evdev_device *device)
238{
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700239 int32_t x, y;
240
Rob Bradfordea23b282012-12-03 19:44:16 +0000241 if (!device->abs.apply_calibration)
242 return;
243
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700244 x = device->abs.x * device->abs.calibration[0] +
245 device->abs.y * device->abs.calibration[1] +
246 device->abs.calibration[2];
Rob Bradfordea23b282012-12-03 19:44:16 +0000247
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700248 y = device->abs.x * device->abs.calibration[3] +
249 device->abs.y * device->abs.calibration[4] +
250 device->abs.calibration[5];
251
252 device->abs.x = x;
253 device->abs.y = y;
Rob Bradfordea23b282012-12-03 19:44:16 +0000254}
255
256static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300257evdev_flush_motion(struct evdev_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200258{
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300259 struct weston_seat *master = device->seat;
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700260 wl_fixed_t x, y;
261 int slot;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500262
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500263 if (!(device->pending_events & EVDEV_SYN))
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200264 return;
265
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700266 slot = device->mt.slot;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500267 device->pending_events &= ~EVDEV_SYN;
Daniel Stone1d637772012-05-30 16:31:47 +0100268 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500269 notify_motion(master, time, device->rel.dx, device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100270 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500271 device->rel.dx = 0;
272 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200273 }
Daniel Stone1d637772012-05-30 16:31:47 +0100274 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700275 weston_output_transform_coordinate(device->output,
276 device->mt.x[slot],
277 device->mt.y[slot],
278 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400279 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700280 device->mt.slot, x, y, WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100281 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
282 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200283 }
Daniel Stone1d637772012-05-30 16:31:47 +0100284 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700285 weston_output_transform_coordinate(device->output,
286 device->mt.x[slot],
287 device->mt.y[slot],
288 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400289 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700290 device->mt.slot, x, y, WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100291 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
292 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200293 }
Daniel Stone1d637772012-05-30 16:31:47 +0100294 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400295 notify_touch(master, time, device->mt.slot, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +0100296 WL_TOUCH_UP);
Daniel Stone1d637772012-05-30 16:31:47 +0100297 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200298 }
Daniel Stone1d637772012-05-30 16:31:47 +0100299 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
Rob Bradfordea23b282012-12-03 19:44:16 +0000300 transform_absolute(device);
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700301 weston_output_transform_coordinate(device->output,
302 device->abs.x,
303 device->abs.y, &x, &y);
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700304
305 if (device->caps & EVDEV_TOUCH) {
306 if (master->num_tp == 0)
307 notify_touch(master, time, 0,
308 x, y, WL_TOUCH_DOWN);
309 else
310 notify_touch(master, time, 0,
311 x, y, WL_TOUCH_MOTION);
312 } else
313 notify_motion_absolute(master, time, x, y);
Daniel Stone1d637772012-05-30 16:31:47 +0100314 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200315 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200316}
317
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400318static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200319fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300320 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200321 struct input_event *event,
322 uint32_t time)
323{
324 switch (event->type) {
325 case EV_REL:
326 evdev_process_relative(device, event, time);
327 break;
328 case EV_ABS:
329 evdev_process_absolute(device, event);
330 break;
331 case EV_KEY:
332 evdev_process_key(device, event, time);
333 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500334 case EV_SYN:
335 device->pending_events |= EVDEV_SYN;
336 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200337 }
338}
339
340static void
341fallback_destroy(struct evdev_dispatch *dispatch)
342{
343 free(dispatch);
344}
345
346struct evdev_dispatch_interface fallback_interface = {
347 fallback_process,
348 fallback_destroy
349};
350
351static struct evdev_dispatch *
352fallback_dispatch_create(void)
353{
354 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
355 if (dispatch == NULL)
356 return NULL;
357
358 dispatch->interface = &fallback_interface;
359
360 return dispatch;
361}
362
363static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300364evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400365 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500366{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200367 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400368 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500369 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500370
Daniel Stone1d637772012-05-30 16:31:47 +0100371 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200372
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500373 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400374 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500375 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500376 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
377
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200378 /* we try to minimize the amount of notifications to be
379 * forwarded to the compositor, so we accumulate motion
380 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200381 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500382 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200383
384 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500385 }
386
Kristian Høgsberg39373542011-12-21 22:18:36 -0500387 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400388}
389
390static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300391evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400392{
393 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300394 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400395 struct input_event ev[32];
396 int len;
397
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300398 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400399 if (!ec->focus)
400 return 1;
401
402 /* If the compositor is repainting, this function is called only once
403 * per frame and we have to process all the events available on the
404 * fd, otherwise there will be input lag. */
405 do {
406 if (device->mtdev)
407 len = mtdev_get(device->mtdev, fd, ev,
408 ARRAY_LENGTH(ev)) *
409 sizeof (struct input_event);
410 else
411 len = read(fd, &ev, sizeof ev);
412
413 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300414 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400415 return 1;
416 }
417
418 evdev_process_events(device, ev, len / sizeof ev[0]);
419
420 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400421
422 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500423}
424
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300425static int
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500426evdev_handle_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300427{
428 struct input_absinfo absinfo;
429 unsigned long ev_bits[NBITS(EV_MAX)];
430 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100431 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300432 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300433 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100434 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300435
436 has_key = 0;
437 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100438 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300439
440 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
441 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300442 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200443
Tiago Vignattid9c82502011-08-19 15:06:20 +0300444 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
445 abs_bits);
446 if (TEST_BIT(abs_bits, ABS_X)) {
447 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200448 device->abs.min_x = absinfo.minimum;
449 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100450 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300451 }
452 if (TEST_BIT(abs_bits, ABS_Y)) {
453 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200454 device->abs.min_y = absinfo.minimum;
455 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100456 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300457 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000458 /* We only handle the slotted Protocol B in weston.
459 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
460 require mtdev for conversion. */
461 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
462 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300463 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
464 &absinfo);
465 device->abs.min_x = absinfo.minimum;
466 device->abs.max_x = absinfo.maximum;
467 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
468 &absinfo);
469 device->abs.min_y = absinfo.minimum;
470 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200471 device->is_mt = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100472 device->caps |= EVDEV_TOUCH;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000473
474 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
475 device->mtdev = mtdev_new_open(device->fd);
476 if (!device->mtdev) {
477 weston_log("mtdev required but failed to open for %s\n",
478 device->devnode);
479 return 0;
480 }
Peter Huttererdf66c5b2013-08-07 11:04:46 +1000481 device->mt.slot = device->mtdev->caps.slot.value;
482 } else {
483 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
484 &absinfo);
485 device->mt.slot = absinfo.value;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000486 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200487 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300488 }
Daniel Stone33965c22012-05-30 16:31:48 +0100489 if (TEST_BIT(ev_bits, EV_REL)) {
490 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
491 rel_bits);
492 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
493 device->caps |= EVDEV_MOTION_REL;
494 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300495 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300496 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300497 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
498 key_bits);
499 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200500 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
501 has_abs)
502 device->dispatch = evdev_touchpad_create(device);
Daniel Stone33965c22012-05-30 16:31:48 +0100503 for (i = KEY_ESC; i < KEY_MAX; i++) {
504 if (i >= BTN_MISC && i < KEY_OK)
505 continue;
506 if (TEST_BIT(key_bits, i)) {
507 device->caps |= EVDEV_KEYBOARD;
508 break;
509 }
510 }
511 for (i = BTN_MISC; i < KEY_OK; i++) {
512 if (TEST_BIT(key_bits, i)) {
513 device->caps |= EVDEV_BUTTON;
514 break;
515 }
516 }
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700517 if (TEST_BIT(key_bits, BTN_TOUCH)) {
518 device->caps |= EVDEV_TOUCH;
519 }
520
Tiago Vignattid9c82502011-08-19 15:06:20 +0300521 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100522 if (TEST_BIT(ev_bits, EV_LED)) {
523 device->caps |= EVDEV_KEYBOARD;
524 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300525
526 /* This rule tries to catch accelerometer devices and opt out. We may
527 * want to adjust the protocol later adding a proper event for dealing
528 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300529 if (has_abs && !has_key && !device->is_mt) {
530 weston_log("input device %s, %s "
531 "ignored: unsupported device type\n",
532 device->devname, device->devnode);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500533 return 0;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300534 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300535
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500536 return 1;
537}
538
539static int
540evdev_configure_device(struct evdev_device *device)
541{
Daniel Stone74419a22012-05-30 16:32:02 +0100542 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300543 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300544 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000545 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
546 device->devname, device->devnode,
547 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
548 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
549 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300550 }
551 if ((device->caps & EVDEV_KEYBOARD)) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500552 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
553 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300554 weston_log("input device %s, %s is a keyboard\n",
555 device->devname, device->devnode);
556 }
557 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300558 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300559 weston_log("input device %s, %s is a touch device\n",
560 device->devname, device->devnode);
561 }
Daniel Stone74419a22012-05-30 16:32:02 +0100562
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300563 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300564}
565
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300566struct evdev_device *
567evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500568{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300569 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500570 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300571 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500572
Peter Huttererf3d62272013-08-08 11:57:05 +1000573 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500574 if (device == NULL)
575 return NULL;
576
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300577 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400578 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500579 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400580
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300581 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200582 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300583 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400584 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500585 device->mt.slot = -1;
586 device->rel.dx = 0;
587 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200588 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300589 device->fd = device_fd;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500590
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300591 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
Peter Hutterer11f5bfb2013-08-07 11:04:41 +1000592 devname[sizeof(devname) - 1] = '\0';
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300593 device->devname = strdup(devname);
594
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500595 if (!evdev_handle_device(device)) {
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000596 evdev_device_destroy(device);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500597 return EVDEV_UNHANDLED_DEVICE;
598 }
599
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400600 if (evdev_configure_device(device) == -1)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000601 goto err;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500602
Jonas Ådahl4136d822012-05-17 12:18:16 +0200603 /* If the dispatch was not set up use the fallback. */
604 if (device->dispatch == NULL)
605 device->dispatch = fallback_dispatch_create();
606 if (device->dispatch == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000607 goto err;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200608
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500609 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500610 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300611 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400612 if (device->source == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000613 goto err;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500614
615 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400616
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000617err:
618 evdev_device_destroy(device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400619 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500620}
621
Pekka Paalanen88594b62012-08-03 14:39:05 +0300622void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300623evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300624{
625 struct evdev_dispatch *dispatch;
626
627 dispatch = device->dispatch;
628 if (dispatch)
629 dispatch->interface->destroy(dispatch);
630
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000631 if (device->source)
632 wl_event_source_remove(device->source);
633 if (!wl_list_empty(&device->link))
634 wl_list_remove(&device->link);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300635 if (device->mtdev)
636 mtdev_close_delete(device->mtdev);
637 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300638 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300639 free(device->devnode);
640 free(device);
641}
642
Pekka Paalanen88594b62012-08-03 14:39:05 +0300643void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300644evdev_notify_keyboard_focus(struct weston_seat *seat,
645 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400646{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300647 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400648 struct wl_array keys;
649 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300650 char evdev_keys[(KEY_CNT + 7) / 8];
651 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400652 uint32_t *k;
653 int ret;
654
Kristian Høgsberge3148752013-05-06 23:19:49 -0400655 if (!seat->keyboard)
Pekka Paalanend8583512012-08-03 14:39:11 +0300656 return;
657
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400658 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300659 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400660 memset(evdev_keys, 0, sizeof evdev_keys);
661 ret = ioctl(device->fd,
662 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
663 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200664 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400665 device->devnode);
666 continue;
667 }
668 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
669 all_keys[i] |= evdev_keys[i];
670 }
671
672 wl_array_init(&keys);
673 for (i = 0; i < KEY_CNT; i++) {
674 set = all_keys[i >> 3] & (1 << (i & 7));
675 if (set) {
676 k = wl_array_add(&keys, sizeof *k);
677 *k = i;
678 }
679 }
680
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400681 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400682
683 wl_array_release(&keys);
684}