blob: 16f0e9358e5458daccb74ed00830edc461237aef [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
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <linux/input.h>
27#include <unistd.h>
28#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030029#include <mtdev.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050030
31#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020032#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050033
34struct evdev_input {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050035 struct weston_input_device base;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040036 struct wl_list devices_list;
37 struct udev_monitor *udev_monitor;
Jonas Ådahlc97af922012-03-28 22:36:09 +020038 struct wl_event_source *udev_monitor_source;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040039 char *seat_id;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050040};
41
Kristian Høgsberg558949b2011-12-21 22:54:49 -050042#define MAX_SLOTS 16
Kristian Høgsberg39373542011-12-21 22:18:36 -050043
Kristian Høgsberg43db4012011-01-14 14:45:42 -050044struct evdev_input_device {
45 struct evdev_input *master;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040046 struct wl_list link;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050047 struct wl_event_source *source;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050048 struct weston_output *output;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040049 char *devnode;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050050 int fd;
Tiago Vignattia157fc12011-11-21 16:39:55 +020051 struct {
52 int min_x, max_x, min_y, max_y;
53 int old_x, old_y, reset_x, reset_y;
Kristian Høgsberg39373542011-12-21 22:18:36 -050054 int32_t x, y;
Tiago Vignattia157fc12011-11-21 16:39:55 +020055 } abs;
Kristian Høgsberg39373542011-12-21 22:18:36 -050056
57 struct {
58 int slot;
59 int32_t x[MAX_SLOTS];
60 int32_t y[MAX_SLOTS];
61 } mt;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030062 struct mtdev *mtdev;
Kristian Høgsberg39373542011-12-21 22:18:36 -050063
64 struct {
65 int dx, dy;
66 } rel;
67
68 int type; /* event type flags */
69
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020070 int is_touchpad, is_mt;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050071};
72
Tiago Vignattia52b2e42011-11-21 17:40:30 +020073/* event type flags */
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020074#define EVDEV_ABSOLUTE_MOTION (1 << 0)
75#define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
76#define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
77#define EVDEV_ABSOLUTE_MT_UP (1 << 3)
78#define EVDEV_RELATIVE_MOTION (1 << 4)
Tiago Vignattia52b2e42011-11-21 17:40:30 +020079
Tiago Vignatti8be003b2011-09-01 19:00:03 +030080static inline void
81evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020082 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030083{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020084 if (e->value == 2)
85 return;
86
Tiago Vignatti8be003b2011-09-01 19:00:03 +030087 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030088 case BTN_TOOL_PEN:
89 case BTN_TOOL_RUBBER:
90 case BTN_TOOL_BRUSH:
91 case BTN_TOOL_PENCIL:
92 case BTN_TOOL_AIRBRUSH:
93 case BTN_TOOL_FINGER:
94 case BTN_TOOL_MOUSE:
95 case BTN_TOOL_LENS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +030096 if (device->is_touchpad)
97 {
Tiago Vignattia157fc12011-11-21 16:39:55 +020098 device->abs.reset_x = 1;
99 device->abs.reset_y = 1;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300100 }
101 break;
Tiago Vignattid9043592011-09-01 19:00:05 +0300102 case BTN_TOUCH:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200103 /* Multitouch touchscreen devices might not send individually
104 * button events each time a new finger is down. So we don't
105 * send notification for such devices and we solve the button
106 * case emulating on compositor side. */
107 if (device->is_mt)
108 break;
109
Tiago Vignattid9043592011-09-01 19:00:05 +0300110 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
111 * BTN_LEFT */
112 e->code = BTN_LEFT;
113 /* Intentional fallthrough! */
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300114 case BTN_LEFT:
115 case BTN_RIGHT:
116 case BTN_MIDDLE:
117 case BTN_SIDE:
118 case BTN_EXTRA:
119 case BTN_FORWARD:
120 case BTN_BACK:
121 case BTN_TASK:
122 notify_button(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200123 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300124 break;
125
126 default:
127 notify_key(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200128 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300129 break;
130 }
131}
132
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200133static void
134evdev_process_touch(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500135 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200136{
137 const int screen_width = device->output->current->width;
138 const int screen_height = device->output->current->height;
139
140 switch (e->code) {
141 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500142 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200143 break;
144 case ABS_MT_TRACKING_ID:
145 if (e->value >= 0)
Kristian Høgsberg39373542011-12-21 22:18:36 -0500146 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200147 else
Kristian Høgsberg39373542011-12-21 22:18:36 -0500148 device->type |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200149 break;
150 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500151 device->mt.x[device->mt.slot] =
152 (e->value - device->abs.min_x) * screen_width /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200153 (device->abs.max_x - device->abs.min_x) +
154 device->output->x;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500155 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200156 break;
157 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500158 device->mt.y[device->mt.slot] =
159 (e->value - device->abs.min_y) * screen_height /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200160 (device->abs.max_y - device->abs.min_y) +
161 device->output->y;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500162 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200163 break;
164 }
165}
166
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300167static inline void
168evdev_process_absolute_motion(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500169 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300170{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400171 const int screen_width = device->output->current->width;
172 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300173
174 switch (e->code) {
175 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500176 device->abs.x =
177 (e->value - device->abs.min_x) * screen_width /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200178 (device->abs.max_x - device->abs.min_x) +
179 device->output->x;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500180 device->type |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300181 break;
182 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500183 device->abs.y =
184 (e->value - device->abs.min_y) * screen_height /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200185 (device->abs.max_y - device->abs.min_y) +
186 device->output->y;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500187 device->type |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300188 break;
189 }
190}
191
192static inline void
193evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500194 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300195{
196 /* FIXME: Make this configurable somehow. */
197 const int touchpad_speed = 700;
198
199 switch (e->code) {
200 case ABS_X:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200201 e->value -= device->abs.min_x;
202 if (device->abs.reset_x)
203 device->abs.reset_x = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300204 else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500205 device->rel.dx =
206 (e->value - device->abs.old_x) *
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200207 touchpad_speed /
Tiago Vignattia157fc12011-11-21 16:39:55 +0200208 (device->abs.max_x - device->abs.min_x);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300209 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200210 device->abs.old_x = e->value;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500211 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300212 break;
213 case ABS_Y:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200214 e->value -= device->abs.min_y;
215 if (device->abs.reset_y)
216 device->abs.reset_y = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300217 else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500218 device->rel.dy =
219 (e->value - device->abs.old_y) *
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200220 touchpad_speed /
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300221 /* maybe use x size here to have the same scale? */
Tiago Vignattia157fc12011-11-21 16:39:55 +0200222 (device->abs.max_y - device->abs.min_y);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300223 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200224 device->abs.old_y = e->value;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500225 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300226 break;
227 }
228}
229
230static inline void
Scott Moreau210d0792012-03-22 10:47:01 -0600231evdev_process_relative(struct evdev_input_device *device,
232 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300233{
234 switch (e->code) {
235 case REL_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500236 device->rel.dx += e->value;
237 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300238 break;
239 case REL_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500240 device->rel.dy += e->value;
241 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300242 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600243 case REL_WHEEL:
244 notify_axis(&device->master->base.input_device,
245 time,
246 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
247 break;
248 case REL_HWHEEL:
249 notify_axis(&device->master->base.input_device,
250 time,
251 WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
252 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300253 }
254}
255
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200256static inline void
257evdev_process_absolute(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500258 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200259{
260 if (device->is_touchpad) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500261 evdev_process_absolute_motion_touchpad(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200262 } else if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500263 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200264 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500265 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200266 }
267}
268
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400269static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200270is_motion_event(struct input_event *e)
271{
272 switch (e->type) {
273 case EV_REL:
274 switch (e->code) {
275 case REL_X:
276 case REL_Y:
277 return 1;
278 }
279 case EV_ABS:
280 switch (e->code) {
281 case ABS_X:
282 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200283 case ABS_MT_POSITION_X:
284 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200285 return 1;
286 }
287 }
288
289 return 0;
290}
291
292static void
Kristian Høgsberg39373542011-12-21 22:18:36 -0500293evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200294{
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500295 struct wl_input_device *master = &device->master->base.input_device;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500296
297 if (!device->type)
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200298 return;
299
Kristian Høgsberg39373542011-12-21 22:18:36 -0500300 if (device->type & EVDEV_RELATIVE_MOTION) {
301 notify_motion(master, time,
302 master->x + device->rel.dx,
303 master->y + device->rel.dy);
304 device->type &= ~EVDEV_RELATIVE_MOTION;
305 device->rel.dx = 0;
306 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200307 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500308 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
309 notify_touch(master, time,
310 device->mt.slot,
311 device->mt.x[device->mt.slot],
312 device->mt.y[device->mt.slot],
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200313 WL_INPUT_DEVICE_TOUCH_DOWN);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500314 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
315 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200316 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500317 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
318 notify_touch(master, time,
319 device->mt.slot,
320 device->mt.x[device->mt.slot],
321 device->mt.y[device->mt.slot],
322 WL_INPUT_DEVICE_TOUCH_MOTION);
323 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
324 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200325 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500326 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
327 notify_touch(master, time, device->mt.slot, 0, 0,
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200328 WL_INPUT_DEVICE_TOUCH_UP);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500329 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200330 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500331 if (device->type & EVDEV_ABSOLUTE_MOTION) {
332 notify_motion(master, time, device->abs.x, device->abs.y);
333 device->type &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200334 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200335}
336
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400337static void
338evdev_process_events(struct evdev_input_device *device,
339 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500340{
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400341 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500342 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500343
Tiago Vignattia12d6112012-01-20 18:47:46 +0200344 device->type = 0;
345
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500346 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400347 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500348 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500349 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
350
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200351 /* we try to minimize the amount of notifications to be
352 * forwarded to the compositor, so we accumulate motion
353 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200354 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500355 evdev_flush_motion(device, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500356 switch (e->type) {
357 case EV_REL:
Scott Moreau210d0792012-03-22 10:47:01 -0600358 evdev_process_relative(device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500359 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500360 case EV_ABS:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500361 evdev_process_absolute(device, e);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500362 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500363 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200364 evdev_process_key(device, e, time);
365 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500366 }
367 }
368
Kristian Høgsberg39373542011-12-21 22:18:36 -0500369 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400370}
371
372static int
373evdev_input_device_data(int fd, uint32_t mask, void *data)
374{
375 struct weston_compositor *ec;
376 struct evdev_input_device *device = data;
377 struct input_event ev[32];
378 int len;
379
380 ec = device->master->base.compositor;
381 if (!ec->focus)
382 return 1;
383
384 /* If the compositor is repainting, this function is called only once
385 * per frame and we have to process all the events available on the
386 * fd, otherwise there will be input lag. */
387 do {
388 if (device->mtdev)
389 len = mtdev_get(device->mtdev, fd, ev,
390 ARRAY_LENGTH(ev)) *
391 sizeof (struct input_event);
392 else
393 len = read(fd, &ev, sizeof ev);
394
395 if (len < 0 || len % sizeof ev[0] != 0) {
396 /* FIXME: call device_removed when errno is ENODEV. */
397 return 1;
398 }
399
400 evdev_process_events(device, ev, len / sizeof ev[0]);
401
402 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400403
404 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500405}
406
Matt Peterson63900ec2011-08-01 13:46:29 -0600407/* copied from udev/extras/input_id/input_id.c */
408/* we must use this kernel-compatible implementation */
409#define BITS_PER_LONG (sizeof(unsigned long) * 8)
410#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
411#define OFF(x) ((x)%BITS_PER_LONG)
412#define BIT(x) (1UL<<OFF(x))
413#define LONG(x) ((x)/BITS_PER_LONG)
414#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
415/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500416
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300417static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300418evdev_configure_device(struct evdev_input_device *device)
419{
420 struct input_absinfo absinfo;
421 unsigned long ev_bits[NBITS(EV_MAX)];
422 unsigned long abs_bits[NBITS(ABS_MAX)];
423 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300424 int has_key, has_abs;
425
426 has_key = 0;
427 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300428
429 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
430 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300431 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200432
Tiago Vignattid9c82502011-08-19 15:06:20 +0300433 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
434 abs_bits);
435 if (TEST_BIT(abs_bits, ABS_X)) {
436 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200437 device->abs.min_x = absinfo.minimum;
438 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300439 }
440 if (TEST_BIT(abs_bits, ABS_Y)) {
441 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200442 device->abs.min_y = absinfo.minimum;
443 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300444 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200445 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
446 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500447 device->mt.slot = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200448 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300449 }
450 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300451 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300452 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
453 key_bits);
454 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
455 !TEST_BIT(key_bits, BTN_TOOL_PEN))
456 device->is_touchpad = 1;
457 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300458
459 /* This rule tries to catch accelerometer devices and opt out. We may
460 * want to adjust the protocol later adding a proper event for dealing
461 * with accelerometers and implement here accordingly */
462 if (has_abs && !has_key)
463 return -1;
464
465 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300466}
467
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500468static struct evdev_input_device *
469evdev_input_device_create(struct evdev_input *master,
470 struct wl_display *display, const char *path)
471{
472 struct evdev_input_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500473 struct weston_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500474
475 device = malloc(sizeof *device);
476 if (device == NULL)
477 return NULL;
478
Kristian Høgsberga8873122011-11-23 10:39:34 -0500479 ec = master->base.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400480 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500481 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400482
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500483 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600484 device->is_touchpad = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200485 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300486 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400487 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500488 device->mt.slot = -1;
489 device->rel.dx = 0;
490 device->rel.dy = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500491
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400492 /* Use non-blocking mode so that we can loop on read on
493 * evdev_input_device_data() until all events on the fd are
494 * read. mtdev_get() also expects this. */
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300495 device->fd = open(path, O_RDONLY | O_NONBLOCK);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400496 if (device->fd < 0)
497 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500498
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400499 if (evdev_configure_device(device) == -1)
500 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500501
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300502 if (device->is_mt) {
503 device->mtdev = mtdev_new_open(device->fd);
504 if (!device->mtdev)
505 fprintf(stderr, "mtdev failed to open for %s\n", path);
506 }
507
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500508 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500509 WL_EVENT_READABLE,
510 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400511 if (device->source == NULL)
512 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500513
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400514 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400515
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500516 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400517
518err1:
519 close(device->fd);
520err0:
521 free(device->devnode);
522 free(device);
523 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500524}
525
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700526static const char default_seat[] = "seat0";
527
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400528static void
529device_added(struct udev_device *udev_device, struct evdev_input *master)
530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500531 struct weston_compositor *c;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400532 const char *devnode;
533 const char *device_seat;
534
535 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
536 if (!device_seat)
537 device_seat = default_seat;
538
539 if (strcmp(device_seat, master->seat_id))
540 return;
541
Kristian Høgsberga8873122011-11-23 10:39:34 -0500542 c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400543 devnode = udev_device_get_devnode(udev_device);
Kristian Høgsberg23a47a82012-01-16 10:54:07 -0500544 evdev_input_device_create(master, c->wl_display, devnode);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400545}
546
547static void
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300548device_removed(struct evdev_input_device *device)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400549{
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300550 wl_event_source_remove(device->source);
551 wl_list_remove(&device->link);
552 if (device->mtdev)
553 mtdev_close_delete(device->mtdev);
554 close(device->fd);
555 free(device->devnode);
556 free(device);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400557}
558
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200559void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500560evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200561{
562 struct evdev_input *input = (struct evdev_input *) input_base;
563 struct udev_enumerate *e;
564 struct udev_list_entry *entry;
565 struct udev_device *device;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200566 const char *path, *sysname;
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200567
568 e = udev_enumerate_new(udev);
569 udev_enumerate_add_match_subsystem(e, "input");
570 udev_enumerate_scan_devices(e);
571 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
572 path = udev_list_entry_get_name(entry);
573 device = udev_device_new_from_syspath(udev, path);
574
Jonas Ådahlc97af922012-03-28 22:36:09 +0200575 sysname = udev_device_get_sysname(device);
576 if (strncmp("event", sysname, 5) != 0) {
577 udev_device_unref(device);
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200578 continue;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200579 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200580
581 device_added(device, input);
582
583 udev_device_unref(device);
584 }
585 udev_enumerate_unref(e);
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200586
587 if (wl_list_empty(&input->devices_list)) {
588 fprintf(stderr,
589 "warning: no input devices on entering Weston. "
590 "Possible causes:\n"
Olivier Le Thanh Duong643eac52012-01-14 15:57:34 +0100591 "\t- no permissions to read /dev/input/event*\n"
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200592 "\t- seats misconfigured "
593 "(Weston backend option 'seat', "
594 "udev device property ID_SEAT)\n");
595 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200596}
597
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400598static int
599evdev_udev_handler(int fd, uint32_t mask, void *data)
600{
601 struct evdev_input *master = data;
602 struct udev_device *udev_device;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300603 struct evdev_input_device *device, *next;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400604 const char *action;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300605 const char *devnode;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400606
607 udev_device = udev_monitor_receive_device(master->udev_monitor);
608 if (!udev_device)
609 return 1;
610
611 action = udev_device_get_action(udev_device);
612 if (action) {
613 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
614 return 0;
615
616 if (!strcmp(action, "add")) {
617 device_added(udev_device, master);
618 }
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300619 else if (!strcmp(action, "remove")) {
620 devnode = udev_device_get_devnode(udev_device);
621 wl_list_for_each_safe(device, next,
622 &master->devices_list, link)
623 if (!strcmp(device->devnode, devnode)) {
624 device_removed(device);
625 break;
626 }
627 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400628 }
629 udev_device_unref(udev_device);
630
631 return 0;
632}
633
634static int
635evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master)
636{
637 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500638 struct weston_compositor *c = master->base.compositor;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200639 int fd;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400640
641 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200642 if (!master->udev_monitor) {
643 fprintf(stderr, "udev: failed to create the udev monitor\n");
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400644 return 0;
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200645 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400646
647 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
648 "input", NULL);
649
650 if (udev_monitor_enable_receiving(master->udev_monitor)) {
651 fprintf(stderr, "udev: failed to bind the udev monitor\n");
Jonas Ådahlc97af922012-03-28 22:36:09 +0200652 udev_monitor_unref(master->udev_monitor);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400653 return 0;
654 }
655
656 loop = wl_display_get_event_loop(c->wl_display);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200657 fd = udev_monitor_get_fd(master->udev_monitor);
658 master->udev_monitor_source =
659 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
660 evdev_udev_handler, master);
661 if (!master->udev_monitor_source) {
662 udev_monitor_unref(master->udev_monitor);
663 return 0;
664 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400665
666 return 1;
667}
668
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500669void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500670evdev_input_create(struct weston_compositor *c, struct udev *udev,
Tiago Vignattice03ec32011-12-19 01:14:03 +0200671 const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500672{
673 struct evdev_input *input;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500674
675 input = malloc(sizeof *input);
676 if (input == NULL)
677 return;
678
679 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500680 weston_input_device_init(&input->base, c);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500681
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400682 wl_list_init(&input->devices_list);
683 input->seat_id = strdup(seat);
684 if (!evdev_config_udev_monitor(udev, input)) {
685 free(input->seat_id);
686 free(input);
687 return;
688 }
689
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200690 evdev_add_devices(udev, &input->base);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500691
692 c->input_device = &input->base.input_device;
693}
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200694
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200695void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500696evdev_remove_devices(struct weston_input_device *input_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200697{
698 struct evdev_input *input = (struct evdev_input *) input_base;
699 struct evdev_input_device *device, *next;
700
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300701 wl_list_for_each_safe(device, next, &input->devices_list, link)
702 device_removed(device);
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200703}
704
705void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500706evdev_input_destroy(struct weston_input_device *input_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200707{
708 struct evdev_input *input = (struct evdev_input *) input_base;
709
710 evdev_remove_devices(input_base);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200711
712 udev_monitor_unref(input->udev_monitor);
713 wl_event_source_remove(input->udev_monitor_source);
714
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200715 wl_list_remove(&input->base.link);
716 free(input->seat_id);
717 free(input);
718}