blob: 22634aa165590a6cb8f52b62abf96bd6ead21804 [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"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010033#include "launcher-util.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050034
35struct evdev_input {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050036 struct weston_input_device base;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040037 struct wl_list devices_list;
38 struct udev_monitor *udev_monitor;
Jonas Ådahlc97af922012-03-28 22:36:09 +020039 struct wl_event_source *udev_monitor_source;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040040 char *seat_id;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050041};
42
Kristian Høgsberg558949b2011-12-21 22:54:49 -050043#define MAX_SLOTS 16
Kristian Høgsberg39373542011-12-21 22:18:36 -050044
Kristian Høgsberg43db4012011-01-14 14:45:42 -050045struct evdev_input_device {
46 struct evdev_input *master;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040047 struct wl_list link;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050048 struct wl_event_source *source;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050049 struct weston_output *output;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -040050 char *devnode;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050051 int fd;
Tiago Vignattia157fc12011-11-21 16:39:55 +020052 struct {
53 int min_x, max_x, min_y, max_y;
54 int old_x, old_y, reset_x, reset_y;
Kristian Høgsberg39373542011-12-21 22:18:36 -050055 int32_t x, y;
Tiago Vignattia157fc12011-11-21 16:39:55 +020056 } abs;
Kristian Høgsberg39373542011-12-21 22:18:36 -050057
58 struct {
59 int slot;
60 int32_t x[MAX_SLOTS];
61 int32_t y[MAX_SLOTS];
62 } mt;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030063 struct mtdev *mtdev;
Kristian Høgsberg39373542011-12-21 22:18:36 -050064
65 struct {
Jonas Ådahlc0ca3992012-05-10 16:46:48 -040066 wl_fixed_t dx, dy;
Kristian Høgsberg39373542011-12-21 22:18:36 -050067 } rel;
68
69 int type; /* event type flags */
70
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020071 int is_touchpad, is_mt;
Kristian Høgsberg43db4012011-01-14 14:45:42 -050072};
73
Tiago Vignattia52b2e42011-11-21 17:40:30 +020074/* event type flags */
Tiago Vignatti22c6bce2011-12-21 19:34:09 +020075#define EVDEV_ABSOLUTE_MOTION (1 << 0)
76#define EVDEV_ABSOLUTE_MT_DOWN (1 << 1)
77#define EVDEV_ABSOLUTE_MT_MOTION (1 << 2)
78#define EVDEV_ABSOLUTE_MT_UP (1 << 3)
79#define EVDEV_RELATIVE_MOTION (1 << 4)
Tiago Vignattia52b2e42011-11-21 17:40:30 +020080
Tiago Vignatti8be003b2011-09-01 19:00:03 +030081static inline void
82evdev_process_key(struct evdev_input_device *device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +020083 struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030084{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020085 if (e->value == 2)
86 return;
87
Tiago Vignatti8be003b2011-09-01 19:00:03 +030088 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030089 case BTN_TOOL_PEN:
90 case BTN_TOOL_RUBBER:
91 case BTN_TOOL_BRUSH:
92 case BTN_TOOL_PENCIL:
93 case BTN_TOOL_AIRBRUSH:
94 case BTN_TOOL_FINGER:
95 case BTN_TOOL_MOUSE:
96 case BTN_TOOL_LENS:
Tiago Vignatti8be003b2011-09-01 19:00:03 +030097 if (device->is_touchpad)
98 {
Tiago Vignattia157fc12011-11-21 16:39:55 +020099 device->abs.reset_x = 1;
100 device->abs.reset_y = 1;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300101 }
102 break;
Tiago Vignattid9043592011-09-01 19:00:05 +0300103 case BTN_TOUCH:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200104 /* Multitouch touchscreen devices might not send individually
105 * button events each time a new finger is down. So we don't
106 * send notification for such devices and we solve the button
107 * case emulating on compositor side. */
108 if (device->is_mt)
109 break;
110
Tiago Vignattid9043592011-09-01 19:00:05 +0300111 /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
112 * BTN_LEFT */
113 e->code = BTN_LEFT;
114 /* Intentional fallthrough! */
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300115 case BTN_LEFT:
116 case BTN_RIGHT:
117 case BTN_MIDDLE:
118 case BTN_SIDE:
119 case BTN_EXTRA:
120 case BTN_FORWARD:
121 case BTN_BACK:
122 case BTN_TASK:
123 notify_button(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200124 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300125 break;
126
127 default:
128 notify_key(&device->master->base.input_device,
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200129 time, e->code, e->value);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300130 break;
131 }
132}
133
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200134static void
135evdev_process_touch(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500136 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200137{
138 const int screen_width = device->output->current->width;
139 const int screen_height = device->output->current->height;
140
141 switch (e->code) {
142 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500143 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200144 break;
145 case ABS_MT_TRACKING_ID:
146 if (e->value >= 0)
Kristian Høgsberg39373542011-12-21 22:18:36 -0500147 device->type |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200148 else
Kristian Høgsberg39373542011-12-21 22:18:36 -0500149 device->type |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200150 break;
151 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500152 device->mt.x[device->mt.slot] =
153 (e->value - device->abs.min_x) * screen_width /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200154 (device->abs.max_x - device->abs.min_x) +
155 device->output->x;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500156 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200157 break;
158 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500159 device->mt.y[device->mt.slot] =
160 (e->value - device->abs.min_y) * screen_height /
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200161 (device->abs.max_y - device->abs.min_y) +
162 device->output->y;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500163 device->type |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200164 break;
165 }
166}
167
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300168static inline void
169evdev_process_absolute_motion(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500170 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300171{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400172 const int screen_width = device->output->current->width;
173 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300174
175 switch (e->code) {
176 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500177 device->abs.x =
178 (e->value - device->abs.min_x) * screen_width /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200179 (device->abs.max_x - device->abs.min_x) +
180 device->output->x;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500181 device->type |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300182 break;
183 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500184 device->abs.y =
185 (e->value - device->abs.min_y) * screen_height /
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200186 (device->abs.max_y - device->abs.min_y) +
187 device->output->y;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500188 device->type |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300189 break;
190 }
191}
192
193static inline void
194evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500195 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300196{
197 /* FIXME: Make this configurable somehow. */
198 const int touchpad_speed = 700;
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400199 int dx, dy;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300200
201 switch (e->code) {
202 case ABS_X:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200203 e->value -= device->abs.min_x;
204 if (device->abs.reset_x)
205 device->abs.reset_x = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300206 else {
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400207 dx =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500208 (e->value - device->abs.old_x) *
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200209 touchpad_speed /
Tiago Vignattia157fc12011-11-21 16:39:55 +0200210 (device->abs.max_x - device->abs.min_x);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400211 device->rel.dx = wl_fixed_from_int(dx);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300212 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200213 device->abs.old_x = e->value;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500214 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300215 break;
216 case ABS_Y:
Tiago Vignattia157fc12011-11-21 16:39:55 +0200217 e->value -= device->abs.min_y;
218 if (device->abs.reset_y)
219 device->abs.reset_y = 0;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300220 else {
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400221 dy =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500222 (e->value - device->abs.old_y) *
Tiago Vignattia52b2e42011-11-21 17:40:30 +0200223 touchpad_speed /
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300224 /* maybe use x size here to have the same scale? */
Tiago Vignattia157fc12011-11-21 16:39:55 +0200225 (device->abs.max_y - device->abs.min_y);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400226 device->rel.dy = wl_fixed_from_int(dy);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300227 }
Tiago Vignattia157fc12011-11-21 16:39:55 +0200228 device->abs.old_y = e->value;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500229 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300230 break;
231 }
232}
233
234static inline void
Scott Moreau210d0792012-03-22 10:47:01 -0600235evdev_process_relative(struct evdev_input_device *device,
236 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300237{
238 switch (e->code) {
239 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400240 device->rel.dx += wl_fixed_from_int(e->value);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500241 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300242 break;
243 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400244 device->rel.dy += wl_fixed_from_int(e->value);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500245 device->type |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300246 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600247 case REL_WHEEL:
248 notify_axis(&device->master->base.input_device,
249 time,
250 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, e->value);
251 break;
252 case REL_HWHEEL:
253 notify_axis(&device->master->base.input_device,
254 time,
255 WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, e->value);
256 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300257 }
258}
259
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200260static inline void
261evdev_process_absolute(struct evdev_input_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500262 struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200263{
264 if (device->is_touchpad) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500265 evdev_process_absolute_motion_touchpad(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200266 } else if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500267 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200268 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500269 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200270 }
271}
272
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400273static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200274is_motion_event(struct input_event *e)
275{
276 switch (e->type) {
277 case EV_REL:
278 switch (e->code) {
279 case REL_X:
280 case REL_Y:
281 return 1;
282 }
283 case EV_ABS:
284 switch (e->code) {
285 case ABS_X:
286 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200287 case ABS_MT_POSITION_X:
288 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200289 return 1;
290 }
291 }
292
293 return 0;
294}
295
296static void
Kristian Høgsberg39373542011-12-21 22:18:36 -0500297evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200298{
Kristian Høgsberg558949b2011-12-21 22:54:49 -0500299 struct wl_input_device *master = &device->master->base.input_device;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500300
301 if (!device->type)
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200302 return;
303
Kristian Høgsberg39373542011-12-21 22:18:36 -0500304 if (device->type & EVDEV_RELATIVE_MOTION) {
305 notify_motion(master, time,
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400306 master->x + device->rel.dx,
307 master->y + device->rel.dy);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500308 device->type &= ~EVDEV_RELATIVE_MOTION;
309 device->rel.dx = 0;
310 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200311 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500312 if (device->type & EVDEV_ABSOLUTE_MT_DOWN) {
313 notify_touch(master, time,
314 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400315 wl_fixed_from_int(device->mt.x[device->mt.slot]),
316 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200317 WL_INPUT_DEVICE_TOUCH_DOWN);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500318 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
319 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200320 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500321 if (device->type & EVDEV_ABSOLUTE_MT_MOTION) {
322 notify_touch(master, time,
323 device->mt.slot,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400324 wl_fixed_from_int(device->mt.x[device->mt.slot]),
325 wl_fixed_from_int(device->mt.y[device->mt.slot]),
Kristian Høgsberg39373542011-12-21 22:18:36 -0500326 WL_INPUT_DEVICE_TOUCH_MOTION);
327 device->type &= ~EVDEV_ABSOLUTE_MT_DOWN;
328 device->type &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200329 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500330 if (device->type & EVDEV_ABSOLUTE_MT_UP) {
331 notify_touch(master, time, device->mt.slot, 0, 0,
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200332 WL_INPUT_DEVICE_TOUCH_UP);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500333 device->type &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200334 }
Kristian Høgsberg39373542011-12-21 22:18:36 -0500335 if (device->type & EVDEV_ABSOLUTE_MOTION) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400336 notify_motion(master, time,
337 wl_fixed_from_int(device->abs.x),
338 wl_fixed_from_int(device->abs.y));
Kristian Høgsberg39373542011-12-21 22:18:36 -0500339 device->type &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200340 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200341}
342
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400343static void
344evdev_process_events(struct evdev_input_device *device,
345 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500346{
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400347 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500348 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500349
Tiago Vignattia12d6112012-01-20 18:47:46 +0200350 device->type = 0;
351
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500352 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400353 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500354 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500355 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
356
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200357 /* we try to minimize the amount of notifications to be
358 * forwarded to the compositor, so we accumulate motion
359 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200360 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500361 evdev_flush_motion(device, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500362 switch (e->type) {
363 case EV_REL:
Scott Moreau210d0792012-03-22 10:47:01 -0600364 evdev_process_relative(device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500365 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500366 case EV_ABS:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500367 evdev_process_absolute(device, e);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500368 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500369 case EV_KEY:
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200370 evdev_process_key(device, e, time);
371 break;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500372 }
373 }
374
Kristian Høgsberg39373542011-12-21 22:18:36 -0500375 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400376}
377
378static int
379evdev_input_device_data(int fd, uint32_t mask, void *data)
380{
381 struct weston_compositor *ec;
382 struct evdev_input_device *device = data;
383 struct input_event ev[32];
384 int len;
385
386 ec = device->master->base.compositor;
387 if (!ec->focus)
388 return 1;
389
390 /* If the compositor is repainting, this function is called only once
391 * per frame and we have to process all the events available on the
392 * fd, otherwise there will be input lag. */
393 do {
394 if (device->mtdev)
395 len = mtdev_get(device->mtdev, fd, ev,
396 ARRAY_LENGTH(ev)) *
397 sizeof (struct input_event);
398 else
399 len = read(fd, &ev, sizeof ev);
400
401 if (len < 0 || len % sizeof ev[0] != 0) {
402 /* FIXME: call device_removed when errno is ENODEV. */
403 return 1;
404 }
405
406 evdev_process_events(device, ev, len / sizeof ev[0]);
407
408 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400409
410 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500411}
412
Matt Peterson63900ec2011-08-01 13:46:29 -0600413/* copied from udev/extras/input_id/input_id.c */
414/* we must use this kernel-compatible implementation */
415#define BITS_PER_LONG (sizeof(unsigned long) * 8)
416#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
417#define OFF(x) ((x)%BITS_PER_LONG)
418#define BIT(x) (1UL<<OFF(x))
419#define LONG(x) ((x)/BITS_PER_LONG)
420#define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
421/* end copied */
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500422
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300423static int
Tiago Vignattid9c82502011-08-19 15:06:20 +0300424evdev_configure_device(struct evdev_input_device *device)
425{
426 struct input_absinfo absinfo;
427 unsigned long ev_bits[NBITS(EV_MAX)];
428 unsigned long abs_bits[NBITS(ABS_MAX)];
429 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300430 int has_key, has_abs;
431
432 has_key = 0;
433 has_abs = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300434
435 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
436 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300437 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200438
Tiago Vignattid9c82502011-08-19 15:06:20 +0300439 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
440 abs_bits);
441 if (TEST_BIT(abs_bits, ABS_X)) {
442 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200443 device->abs.min_x = absinfo.minimum;
444 device->abs.max_x = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300445 }
446 if (TEST_BIT(abs_bits, ABS_Y)) {
447 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200448 device->abs.min_y = absinfo.minimum;
449 device->abs.max_y = absinfo.maximum;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300450 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200451 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
452 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500453 device->mt.slot = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200454 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300455 }
456 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300457 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300458 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
459 key_bits);
460 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
461 !TEST_BIT(key_bits, BTN_TOOL_PEN))
462 device->is_touchpad = 1;
463 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300464
465 /* This rule tries to catch accelerometer devices and opt out. We may
466 * want to adjust the protocol later adding a proper event for dealing
467 * with accelerometers and implement here accordingly */
468 if (has_abs && !has_key)
469 return -1;
470
471 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300472}
473
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500474static struct evdev_input_device *
475evdev_input_device_create(struct evdev_input *master,
476 struct wl_display *display, const char *path)
477{
478 struct evdev_input_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500479 struct weston_compositor *ec;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500480
481 device = malloc(sizeof *device);
482 if (device == NULL)
483 return NULL;
484
Kristian Høgsberga8873122011-11-23 10:39:34 -0500485 ec = master->base.compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400486 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500487 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400488
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500489 device->master = master;
Matt Peterson63900ec2011-08-01 13:46:29 -0600490 device->is_touchpad = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200491 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300492 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400493 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500494 device->mt.slot = -1;
495 device->rel.dx = 0;
496 device->rel.dy = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500497
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400498 /* Use non-blocking mode so that we can loop on read on
499 * evdev_input_device_data() until all events on the fd are
500 * read. mtdev_get() also expects this. */
Benjamin Franzkebfeda132012-01-30 14:04:04 +0100501 device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400502 if (device->fd < 0)
503 goto err0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500504
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400505 if (evdev_configure_device(device) == -1)
506 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500507
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300508 if (device->is_mt) {
509 device->mtdev = mtdev_new_open(device->fd);
510 if (!device->mtdev)
511 fprintf(stderr, "mtdev failed to open for %s\n", path);
512 }
513
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500514 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500515 WL_EVENT_READABLE,
516 evdev_input_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400517 if (device->source == NULL)
518 goto err1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500519
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400520 wl_list_insert(master->devices_list.prev, &device->link);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400521
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500522 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400523
524err1:
525 close(device->fd);
526err0:
527 free(device->devnode);
528 free(device);
529 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500530}
531
Kristian Høgsberg86ec8e82011-07-19 16:10:11 -0700532static const char default_seat[] = "seat0";
533
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400534static void
535device_added(struct udev_device *udev_device, struct evdev_input *master)
536{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500537 struct weston_compositor *c;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400538 const char *devnode;
539 const char *device_seat;
540
541 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
542 if (!device_seat)
543 device_seat = default_seat;
544
545 if (strcmp(device_seat, master->seat_id))
546 return;
547
Kristian Høgsberga8873122011-11-23 10:39:34 -0500548 c = master->base.compositor;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400549 devnode = udev_device_get_devnode(udev_device);
Kristian Høgsberg23a47a82012-01-16 10:54:07 -0500550 evdev_input_device_create(master, c->wl_display, devnode);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400551}
552
553static void
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300554device_removed(struct evdev_input_device *device)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400555{
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300556 wl_event_source_remove(device->source);
557 wl_list_remove(&device->link);
558 if (device->mtdev)
559 mtdev_close_delete(device->mtdev);
560 close(device->fd);
561 free(device->devnode);
562 free(device);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400563}
564
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400565static void
566evdev_notify_keyboard_focus(struct evdev_input *input)
567{
568 struct evdev_input_device *device;
569 struct wl_array keys;
570 unsigned int i, set;
571 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
572 uint32_t *k;
573 int ret;
574
575 memset(all_keys, 0, sizeof all_keys);
576 wl_list_for_each(device, &input->devices_list, link) {
577 memset(evdev_keys, 0, sizeof evdev_keys);
578 ret = ioctl(device->fd,
579 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
580 if (ret < 0) {
581 fprintf(stderr, "failed to get keys for device %s\n",
582 device->devnode);
583 continue;
584 }
585 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
586 all_keys[i] |= evdev_keys[i];
587 }
588
589 wl_array_init(&keys);
590 for (i = 0; i < KEY_CNT; i++) {
591 set = all_keys[i >> 3] & (1 << (i & 7));
592 if (set) {
593 k = wl_array_add(&keys, sizeof *k);
594 *k = i;
595 }
596 }
597
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400598 notify_keyboard_focus(&input->base.input_device, &keys);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400599
600 wl_array_release(&keys);
601}
602
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200603void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604evdev_add_devices(struct udev *udev, struct weston_input_device *input_base)
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200605{
606 struct evdev_input *input = (struct evdev_input *) input_base;
607 struct udev_enumerate *e;
608 struct udev_list_entry *entry;
609 struct udev_device *device;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200610 const char *path, *sysname;
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200611
612 e = udev_enumerate_new(udev);
613 udev_enumerate_add_match_subsystem(e, "input");
614 udev_enumerate_scan_devices(e);
615 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
616 path = udev_list_entry_get_name(entry);
617 device = udev_device_new_from_syspath(udev, path);
618
Jonas Ådahlc97af922012-03-28 22:36:09 +0200619 sysname = udev_device_get_sysname(device);
620 if (strncmp("event", sysname, 5) != 0) {
621 udev_device_unref(device);
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200622 continue;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200623 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200624
625 device_added(device, input);
626
627 udev_device_unref(device);
628 }
629 udev_enumerate_unref(e);
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200630
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400631 evdev_notify_keyboard_focus(input);
632
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200633 if (wl_list_empty(&input->devices_list)) {
634 fprintf(stderr,
635 "warning: no input devices on entering Weston. "
636 "Possible causes:\n"
Olivier Le Thanh Duong643eac52012-01-14 15:57:34 +0100637 "\t- no permissions to read /dev/input/event*\n"
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200638 "\t- seats misconfigured "
639 "(Weston backend option 'seat', "
640 "udev device property ID_SEAT)\n");
641 }
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200642}
643
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400644static int
645evdev_udev_handler(int fd, uint32_t mask, void *data)
646{
647 struct evdev_input *master = data;
648 struct udev_device *udev_device;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300649 struct evdev_input_device *device, *next;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400650 const char *action;
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300651 const char *devnode;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400652
653 udev_device = udev_monitor_receive_device(master->udev_monitor);
654 if (!udev_device)
655 return 1;
656
657 action = udev_device_get_action(udev_device);
658 if (action) {
659 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
660 return 0;
661
662 if (!strcmp(action, "add")) {
663 device_added(udev_device, master);
664 }
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300665 else if (!strcmp(action, "remove")) {
666 devnode = udev_device_get_devnode(udev_device);
667 wl_list_for_each_safe(device, next,
668 &master->devices_list, link)
669 if (!strcmp(device->devnode, devnode)) {
670 device_removed(device);
671 break;
672 }
673 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400674 }
675 udev_device_unref(udev_device);
676
677 return 0;
678}
679
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200680int
681evdev_enable_udev_monitor(struct udev *udev, struct weston_input_device *input_base)
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400682{
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200683 struct evdev_input *master = (struct evdev_input *) input_base;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400684 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500685 struct weston_compositor *c = master->base.compositor;
Jonas Ådahlc97af922012-03-28 22:36:09 +0200686 int fd;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400687
688 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200689 if (!master->udev_monitor) {
690 fprintf(stderr, "udev: failed to create the udev monitor\n");
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400691 return 0;
Pekka Paalanenb07876d2012-01-05 16:41:21 +0200692 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400693
694 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
695 "input", NULL);
696
697 if (udev_monitor_enable_receiving(master->udev_monitor)) {
698 fprintf(stderr, "udev: failed to bind the udev monitor\n");
Jonas Ådahlc97af922012-03-28 22:36:09 +0200699 udev_monitor_unref(master->udev_monitor);
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400700 return 0;
701 }
702
703 loop = wl_display_get_event_loop(c->wl_display);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200704 fd = udev_monitor_get_fd(master->udev_monitor);
705 master->udev_monitor_source =
706 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
707 evdev_udev_handler, master);
708 if (!master->udev_monitor_source) {
709 udev_monitor_unref(master->udev_monitor);
710 return 0;
711 }
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400712
713 return 1;
714}
715
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500716void
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200717evdev_disable_udev_monitor(struct weston_input_device *input_base)
718{
719 struct evdev_input *input = (struct evdev_input *) input_base;
720
721 if (!input->udev_monitor)
722 return;
723
724 udev_monitor_unref(input->udev_monitor);
725 input->udev_monitor = NULL;
726 wl_event_source_remove(input->udev_monitor_source);
727 input->udev_monitor_source = NULL;
728}
729
730void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500731evdev_input_create(struct weston_compositor *c, struct udev *udev,
Tiago Vignattice03ec32011-12-19 01:14:03 +0200732 const char *seat)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500733{
734 struct evdev_input *input;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500735
736 input = malloc(sizeof *input);
737 if (input == NULL)
738 return;
739
740 memset(input, 0, sizeof *input);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500741 weston_input_device_init(&input->base, c);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500742
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400743 wl_list_init(&input->devices_list);
744 input->seat_id = strdup(seat);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200745 if (!evdev_enable_udev_monitor(udev, &input->base)) {
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400746 free(input->seat_id);
747 free(input);
748 return;
749 }
750
Tiago Vignatti0db1d5f2011-12-18 16:47:15 +0200751 evdev_add_devices(udev, &input->base);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500752
753 c->input_device = &input->base.input_device;
754}
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200755
Tiago Vignatti6e2d5f12011-12-19 00:32:48 +0200756void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500757evdev_remove_devices(struct weston_input_device *input_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200758{
759 struct evdev_input *input = (struct evdev_input *) input_base;
760 struct evdev_input_device *device, *next;
761
Tiago Vignattidb4ecc62012-03-27 21:26:01 +0300762 wl_list_for_each_safe(device, next, &input->devices_list, link)
763 device_removed(device);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400764
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400765 notify_keyboard_focus(&input->base.input_device, NULL);
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200766}
767
768void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769evdev_input_destroy(struct weston_input_device *input_base)
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200770{
771 struct evdev_input *input = (struct evdev_input *) input_base;
772
773 evdev_remove_devices(input_base);
Benjamin Franzke78d3afe2012-04-09 18:14:58 +0200774 evdev_disable_udev_monitor(&input->base);
Jonas Ådahlc97af922012-03-28 22:36:09 +0200775
Tiago Vignattic349e1d2011-12-18 23:52:18 +0200776 wl_list_remove(&input->base.link);
777 free(input->seat_id);
778 free(input);
779}