blob: 3ccdd3ca02c7cf8681ecbcde9e1309224ce16173 [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 };
Rolf Morel14c98922013-08-09 16:32:17 +020048 struct input_event ev[ARRAY_LENGTH(map) + 1];
Daniel Stone7bbd5b32012-05-30 16:31:49 +010049 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 }
Rolf Morel14c98922013-08-09 16:32:17 +020060 ev[i].type = EV_SYN;
61 ev[i].code = SYN_REPORT;
Daniel Stone7bbd5b32012-05-30 16:31:49 +010062
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030063 i = write(device->fd, ev, sizeof ev);
64 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010065}
66
Tiago Vignatti8be003b2011-09-01 19:00:03 +030067static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030068evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030069{
Peter Hutterer89af60e2013-08-07 11:04:42 +100070 /* ignore kernel key repeat */
Tiago Vignatti8755ff92011-11-10 14:47:30 +020071 if (e->value == 2)
72 return;
73
Tiago Vignatti8be003b2011-09-01 19:00:03 +030074 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030075 case BTN_LEFT:
76 case BTN_RIGHT:
77 case BTN_MIDDLE:
78 case BTN_SIDE:
79 case BTN_EXTRA:
80 case BTN_FORWARD:
81 case BTN_BACK:
82 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040083 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +010084 time, e->code,
85 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
86 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030087 break;
88
Kristian Høgsberg0af26c42013-07-26 10:43:26 -070089 case BTN_TOUCH:
90 if (e->value == 0 && !device->is_mt)
Rusty Lynch92e83922013-08-08 21:08:17 -070091 notify_touch(device->seat, time, 0, 0, 0,
Kristian Høgsberg0af26c42013-07-26 10:43:26 -070092 WL_TOUCH_UP);
93 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030094 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040095 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +010096 time, e->code,
97 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +010098 WL_KEYBOARD_KEY_STATE_RELEASED,
99 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300100 break;
101 }
102}
103
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200104static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300105evdev_process_touch(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200106{
Hardeningff39efa2013-09-18 23:56:35 +0200107 const int screen_width = device->output->current_mode->width;
108 const int screen_height = device->output->current_mode->height;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200109
110 switch (e->code) {
111 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500112 device->mt.slot = e->value;
Neil Robertsf65c4862013-09-20 15:03:29 +0100113 device->pending_events |= EVDEV_SYN_OR_SLOT;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200114 break;
115 case ABS_MT_TRACKING_ID:
116 if (e->value >= 0)
Daniel Stone1d637772012-05-30 16:31:47 +0100117 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200118 else
Daniel Stone1d637772012-05-30 16:31:47 +0100119 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200120 break;
121 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500122 device->mt.x[device->mt.slot] =
123 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700124 (device->abs.max_x - device->abs.min_x);
Daniel Stone1d637772012-05-30 16:31:47 +0100125 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200126 break;
127 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500128 device->mt.y[device->mt.slot] =
129 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700130 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100131 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200132 break;
133 }
134}
135
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300136static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300137evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500138 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300139{
Hardeningff39efa2013-09-18 23:56:35 +0200140 const int screen_width = device->output->current_mode->width;
141 const int screen_height = device->output->current_mode->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300142
143 switch (e->code) {
144 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500145 device->abs.x =
146 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700147 (device->abs.max_x - device->abs.min_x);
Daniel Stone1d637772012-05-30 16:31:47 +0100148 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300149 break;
150 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500151 device->abs.y =
152 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700153 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100154 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300155 break;
156 }
157}
158
159static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300160evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200161 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300162{
163 switch (e->code) {
164 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400165 device->rel.dx += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100166 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300167 break;
168 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400169 device->rel.dy += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100170 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300171 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600172 case REL_WHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200173 switch (e->value) {
174 case -1:
175 /* Scroll down */
176 case 1:
177 /* Scroll up */
178 notify_axis(device->seat,
179 time,
180 WL_POINTER_AXIS_VERTICAL_SCROLL,
181 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
182 break;
183 default:
184 break;
185 }
Scott Moreau210d0792012-03-22 10:47:01 -0600186 break;
187 case REL_HWHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200188 switch (e->value) {
189 case -1:
190 /* Scroll left */
191 case 1:
192 /* Scroll right */
193 notify_axis(device->seat,
194 time,
195 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
196 e->value * DEFAULT_AXIS_STEP_DISTANCE);
197 break;
198 default:
199 break;
200
201 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300202 }
203}
204
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200205static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300206evdev_process_absolute(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200207{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200208 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500209 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200210 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500211 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200212 }
213}
214
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400215static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200216is_motion_event(struct input_event *e)
217{
218 switch (e->type) {
219 case EV_REL:
220 switch (e->code) {
221 case REL_X:
222 case REL_Y:
223 return 1;
224 }
Rob Bradford4b997e42012-10-09 18:44:31 +0100225 break;
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200226 case EV_ABS:
227 switch (e->code) {
228 case ABS_X:
229 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200230 case ABS_MT_POSITION_X:
231 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200232 return 1;
233 }
234 }
235
236 return 0;
237}
238
239static void
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700240transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
Rob Bradfordea23b282012-12-03 19:44:16 +0000241{
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700242 if (!device->abs.apply_calibration) {
243 *x = device->abs.x;
244 *y = device->abs.y;
245 return;
246 } else {
247 *x = device->abs.x * device->abs.calibration[0] +
248 device->abs.y * device->abs.calibration[1] +
249 device->abs.calibration[2];
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700250
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700251 *y = device->abs.x * device->abs.calibration[3] +
252 device->abs.y * device->abs.calibration[4] +
253 device->abs.calibration[5];
254 }
Rob Bradfordea23b282012-12-03 19:44:16 +0000255}
256
257static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300258evdev_flush_motion(struct evdev_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200259{
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700260 struct weston_seat *master = device->seat;
261 wl_fixed_t x, y;
262 int32_t cx, cy;
263 int slot;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500264
Neil Robertsf65c4862013-09-20 15:03:29 +0100265 if (!(device->pending_events & EVDEV_SYN_OR_SLOT))
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200266 return;
267
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700268 slot = device->mt.slot;
Neil Robertsf65c4862013-09-20 15:03:29 +0100269 device->pending_events &= ~EVDEV_SYN_OR_SLOT;
Daniel Stone1d637772012-05-30 16:31:47 +0100270 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500271 notify_motion(master, time, device->rel.dx, device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100272 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500273 device->rel.dx = 0;
274 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200275 }
Daniel Stone1d637772012-05-30 16:31:47 +0100276 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700277 weston_output_transform_coordinate(device->output,
278 device->mt.x[slot],
279 device->mt.y[slot],
280 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400281 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700282 device->mt.slot, x, y, WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100283 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
284 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200285 }
Daniel Stone1d637772012-05-30 16:31:47 +0100286 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700287 weston_output_transform_coordinate(device->output,
288 device->mt.x[slot],
289 device->mt.y[slot],
290 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400291 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700292 device->mt.slot, x, y, WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100293 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
294 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200295 }
Daniel Stone1d637772012-05-30 16:31:47 +0100296 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400297 notify_touch(master, time, device->mt.slot, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +0100298 WL_TOUCH_UP);
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700299 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
300 }
301 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
302 transform_absolute(device, &cx, &cy);
303 weston_output_transform_coordinate(device->output,
304 cx, cy, &x, &y);
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700305
Kristian Høgsberg6c7583d2013-08-28 22:12:24 -0700306 if (device->caps & EVDEV_TOUCH) {
307 if (master->num_tp == 0)
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700308 notify_touch(master, time, 0,
309 x, y, WL_TOUCH_DOWN);
310 else
311 notify_touch(master, time, 0,
312 x, y, WL_TOUCH_MOTION);
313 } else
314 notify_motion_absolute(master, time, x, y);
Daniel Stone1d637772012-05-30 16:31:47 +0100315 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200316 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200317}
318
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400319static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200320fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300321 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200322 struct input_event *event,
323 uint32_t time)
324{
325 switch (event->type) {
326 case EV_REL:
327 evdev_process_relative(device, event, time);
328 break;
329 case EV_ABS:
330 evdev_process_absolute(device, event);
331 break;
332 case EV_KEY:
333 evdev_process_key(device, event, time);
334 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500335 case EV_SYN:
Neil Robertsf65c4862013-09-20 15:03:29 +0100336 device->pending_events |= EVDEV_SYN_OR_SLOT;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500337 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200338 }
339}
340
341static void
342fallback_destroy(struct evdev_dispatch *dispatch)
343{
344 free(dispatch);
345}
346
347struct evdev_dispatch_interface fallback_interface = {
348 fallback_process,
349 fallback_destroy
350};
351
352static struct evdev_dispatch *
353fallback_dispatch_create(void)
354{
355 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
356 if (dispatch == NULL)
357 return NULL;
358
359 dispatch->interface = &fallback_interface;
360
361 return dispatch;
362}
363
364static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300365evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400366 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500367{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200368 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400369 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500370 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500371
Daniel Stone1d637772012-05-30 16:31:47 +0100372 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200373
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500374 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400375 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500376 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500377 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
378
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200379 /* we try to minimize the amount of notifications to be
380 * forwarded to the compositor, so we accumulate motion
381 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200382 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500383 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200384
385 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500386 }
387
Kristian Høgsberg39373542011-12-21 22:18:36 -0500388 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400389}
390
391static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300392evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400393{
394 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300395 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400396 struct input_event ev[32];
397 int len;
398
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300399 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400400 if (!ec->focus)
401 return 1;
402
403 /* If the compositor is repainting, this function is called only once
404 * per frame and we have to process all the events available on the
405 * fd, otherwise there will be input lag. */
406 do {
407 if (device->mtdev)
408 len = mtdev_get(device->mtdev, fd, ev,
409 ARRAY_LENGTH(ev)) *
410 sizeof (struct input_event);
411 else
412 len = read(fd, &ev, sizeof ev);
413
414 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300415 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400416 return 1;
417 }
418
419 evdev_process_events(device, ev, len / sizeof ev[0]);
420
421 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400422
423 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500424}
425
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300426static int
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500427evdev_handle_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300428{
429 struct input_absinfo absinfo;
430 unsigned long ev_bits[NBITS(EV_MAX)];
431 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100432 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300433 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300434 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100435 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300436
437 has_key = 0;
438 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100439 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300440
441 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
442 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300443 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200444
Tiago Vignattid9c82502011-08-19 15:06:20 +0300445 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
446 abs_bits);
Kristian Høgsbergb1c02a82013-08-13 14:55:39 -0700447
448 if (TEST_BIT(abs_bits, ABS_WHEEL) ||
449 TEST_BIT(abs_bits, ABS_GAS) ||
450 TEST_BIT(abs_bits, ABS_BRAKE) ||
451 TEST_BIT(abs_bits, ABS_HAT0X)) {
452 weston_log("device %s is a joystick, ignoring\n",
453 device->devnode);
454 return 0;
455 }
456
Tiago Vignattid9c82502011-08-19 15:06:20 +0300457 if (TEST_BIT(abs_bits, ABS_X)) {
458 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200459 device->abs.min_x = absinfo.minimum;
460 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100461 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300462 }
463 if (TEST_BIT(abs_bits, ABS_Y)) {
464 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200465 device->abs.min_y = absinfo.minimum;
466 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100467 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300468 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000469 /* We only handle the slotted Protocol B in weston.
470 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
471 require mtdev for conversion. */
472 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
473 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300474 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
475 &absinfo);
476 device->abs.min_x = absinfo.minimum;
477 device->abs.max_x = absinfo.maximum;
478 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
479 &absinfo);
480 device->abs.min_y = absinfo.minimum;
481 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200482 device->is_mt = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100483 device->caps |= EVDEV_TOUCH;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000484
485 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
486 device->mtdev = mtdev_new_open(device->fd);
487 if (!device->mtdev) {
488 weston_log("mtdev required but failed to open for %s\n",
489 device->devnode);
490 return 0;
491 }
Peter Huttererdf66c5b2013-08-07 11:04:46 +1000492 device->mt.slot = device->mtdev->caps.slot.value;
493 } else {
494 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
495 &absinfo);
496 device->mt.slot = absinfo.value;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000497 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200498 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300499 }
Daniel Stone33965c22012-05-30 16:31:48 +0100500 if (TEST_BIT(ev_bits, EV_REL)) {
501 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
502 rel_bits);
503 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
504 device->caps |= EVDEV_MOTION_REL;
505 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300506 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300507 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300508 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
509 key_bits);
510 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200511 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
Peter Hutterer4477fee2013-08-07 11:04:49 +1000512 has_abs) {
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200513 device->dispatch = evdev_touchpad_create(device);
Peter Hutterer4477fee2013-08-07 11:04:49 +1000514 weston_log("input device %s, %s is a touchpad\n",
515 device->devname, device->devnode);
516 }
Daniel Stone33965c22012-05-30 16:31:48 +0100517 for (i = KEY_ESC; i < KEY_MAX; i++) {
518 if (i >= BTN_MISC && i < KEY_OK)
519 continue;
520 if (TEST_BIT(key_bits, i)) {
521 device->caps |= EVDEV_KEYBOARD;
522 break;
523 }
524 }
525 for (i = BTN_MISC; i < KEY_OK; i++) {
526 if (TEST_BIT(key_bits, i)) {
527 device->caps |= EVDEV_BUTTON;
528 break;
529 }
530 }
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700531 if (TEST_BIT(key_bits, BTN_TOUCH)) {
532 device->caps |= EVDEV_TOUCH;
533 }
534
Tiago Vignattid9c82502011-08-19 15:06:20 +0300535 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100536 if (TEST_BIT(ev_bits, EV_LED)) {
537 device->caps |= EVDEV_KEYBOARD;
538 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300539
540 /* This rule tries to catch accelerometer devices and opt out. We may
541 * want to adjust the protocol later adding a proper event for dealing
542 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300543 if (has_abs && !has_key && !device->is_mt) {
544 weston_log("input device %s, %s "
545 "ignored: unsupported device type\n",
546 device->devname, device->devnode);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500547 return 0;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300548 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300549
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500550 return 1;
551}
552
553static int
554evdev_configure_device(struct evdev_device *device)
555{
Daniel Stone74419a22012-05-30 16:32:02 +0100556 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300557 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300558 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000559 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
560 device->devname, device->devnode,
561 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
562 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
563 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300564 }
565 if ((device->caps & EVDEV_KEYBOARD)) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500566 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
567 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300568 weston_log("input device %s, %s is a keyboard\n",
569 device->devname, device->devnode);
570 }
571 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300572 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300573 weston_log("input device %s, %s is a touch device\n",
574 device->devname, device->devnode);
575 }
Daniel Stone74419a22012-05-30 16:32:02 +0100576
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300577 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300578}
579
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300580struct evdev_device *
581evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500582{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300583 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500584 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300585 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500586
Peter Huttererf3d62272013-08-08 11:57:05 +1000587 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500588 if (device == NULL)
589 return NULL;
590
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300591 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400592 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500593 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400594
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300595 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200596 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300597 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400598 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500599 device->mt.slot = -1;
600 device->rel.dx = 0;
601 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200602 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300603 device->fd = device_fd;
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700604 wl_list_init(&device->link);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500605
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300606 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
Peter Hutterer11f5bfb2013-08-07 11:04:41 +1000607 devname[sizeof(devname) - 1] = '\0';
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300608 device->devname = strdup(devname);
609
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500610 if (!evdev_handle_device(device)) {
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000611 evdev_device_destroy(device);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500612 return EVDEV_UNHANDLED_DEVICE;
613 }
614
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400615 if (evdev_configure_device(device) == -1)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000616 goto err;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500617
Jonas Ådahl4136d822012-05-17 12:18:16 +0200618 /* If the dispatch was not set up use the fallback. */
619 if (device->dispatch == NULL)
620 device->dispatch = fallback_dispatch_create();
621 if (device->dispatch == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000622 goto err;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200623
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500624 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500625 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300626 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400627 if (device->source == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000628 goto err;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500629
630 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400631
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000632err:
633 evdev_device_destroy(device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400634 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500635}
636
Pekka Paalanen88594b62012-08-03 14:39:05 +0300637void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300638evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300639{
640 struct evdev_dispatch *dispatch;
641
642 dispatch = device->dispatch;
643 if (dispatch)
644 dispatch->interface->destroy(dispatch);
645
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000646 if (device->source)
647 wl_event_source_remove(device->source);
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700648 wl_list_remove(&device->link);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300649 if (device->mtdev)
650 mtdev_close_delete(device->mtdev);
651 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300652 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300653 free(device->devnode);
654 free(device);
655}
656
Pekka Paalanen88594b62012-08-03 14:39:05 +0300657void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300658evdev_notify_keyboard_focus(struct weston_seat *seat,
659 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400660{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300661 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400662 struct wl_array keys;
663 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300664 char evdev_keys[(KEY_CNT + 7) / 8];
665 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400666 uint32_t *k;
667 int ret;
668
Kristian Høgsberge3148752013-05-06 23:19:49 -0400669 if (!seat->keyboard)
Pekka Paalanend8583512012-08-03 14:39:11 +0300670 return;
671
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400672 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300673 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400674 memset(evdev_keys, 0, sizeof evdev_keys);
675 ret = ioctl(device->fd,
676 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
677 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200678 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400679 device->devnode);
680 continue;
681 }
682 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
683 all_keys[i] |= evdev_keys[i];
684 }
685
686 wl_array_init(&keys);
687 for (i = 0; i < KEY_CNT; i++) {
688 set = all_keys[i >> 3] & (1 << (i & 7));
689 if (set) {
690 k = wl_array_add(&keys, sizeof *k);
691 *k = i;
692 }
693 }
694
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400695 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400696
697 wl_array_release(&keys);
698}