blob: d9b7d32051288cc8837a4258c4559f91fe242f2b [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
David Herrmanne05a0cd2013-10-15 14:29:56 +020025#include <errno.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050026#include <stdlib.h>
27#include <string.h>
28#include <linux/input.h>
29#include <unistd.h>
30#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030031#include <mtdev.h>
Neil Robertsdaf7d472013-09-24 12:09:03 +010032#include <assert.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050033
34#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020035#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050036
Jonas Ådahlb984e402012-10-03 22:56:58 +020037#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
38
Pekka Paalanen88594b62012-08-03 14:39:05 +030039void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030040evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010041{
42 static const struct {
43 enum weston_led weston;
44 int evdev;
45 } map[] = {
46 { LED_NUM_LOCK, LED_NUML },
47 { LED_CAPS_LOCK, LED_CAPSL },
48 { LED_SCROLL_LOCK, LED_SCROLLL },
49 };
Rolf Morel14c98922013-08-09 16:32:17 +020050 struct input_event ev[ARRAY_LENGTH(map) + 1];
Daniel Stone7bbd5b32012-05-30 16:31:49 +010051 unsigned int i;
52
Kristian Høgsberg3d793c92013-12-16 15:07:59 -080053 if (!(device->seat_caps & EVDEV_SEAT_KEYBOARD))
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030054 return;
55
Daniel Stone7bbd5b32012-05-30 16:31:49 +010056 memset(ev, 0, sizeof(ev));
57 for (i = 0; i < ARRAY_LENGTH(map); i++) {
58 ev[i].type = EV_LED;
59 ev[i].code = map[i].evdev;
60 ev[i].value = !!(leds & map[i].weston);
61 }
Rolf Morel14c98922013-08-09 16:32:17 +020062 ev[i].type = EV_SYN;
63 ev[i].code = SYN_REPORT;
Daniel Stone7bbd5b32012-05-30 16:31:49 +010064
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030065 i = write(device->fd, ev, sizeof ev);
66 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010067}
68
Neil Robertsdaf7d472013-09-24 12:09:03 +010069static void
70transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
71{
72 if (!device->abs.apply_calibration) {
73 *x = device->abs.x;
74 *y = device->abs.y;
75 return;
76 } else {
77 *x = device->abs.x * device->abs.calibration[0] +
78 device->abs.y * device->abs.calibration[1] +
79 device->abs.calibration[2];
80
81 *y = device->abs.x * device->abs.calibration[3] +
82 device->abs.y * device->abs.calibration[4] +
83 device->abs.calibration[5];
84 }
85}
86
87static void
88evdev_flush_pending_event(struct evdev_device *device, uint32_t time)
89{
90 struct weston_seat *master = device->seat;
91 wl_fixed_t x, y;
92 int32_t cx, cy;
Kristian Høgsberg2fce4802014-01-08 16:21:24 -080093 int slot, seat_slot;
Neil Robertsdaf7d472013-09-24 12:09:03 +010094
95 slot = device->mt.slot;
Neil Robertsdaf7d472013-09-24 12:09:03 +010096 switch (device->pending_event) {
97 case EVDEV_NONE:
98 return;
99 case EVDEV_RELATIVE_MOTION:
100 notify_motion(master, time, device->rel.dx, device->rel.dy);
101 device->rel.dx = 0;
102 device->rel.dy = 0;
103 goto handled;
104 case EVDEV_ABSOLUTE_MT_DOWN:
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800105 if (device->output == NULL)
106 break;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100107 weston_output_transform_coordinate(device->output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -0500108 wl_fixed_from_int(device->mt.slots[slot].x),
109 wl_fixed_from_int(device->mt.slots[slot].y),
Neil Robertsdaf7d472013-09-24 12:09:03 +0100110 &x, &y);
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800111 seat_slot = ffs(~master->slot_map) - 1;
112 device->mt.slots[slot].seat_slot = seat_slot;
113 master->slot_map |= 1 << seat_slot;
114
115 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100116 goto handled;
117 case EVDEV_ABSOLUTE_MT_MOTION:
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800118 if (device->output == NULL)
119 break;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100120 weston_output_transform_coordinate(device->output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -0500121 wl_fixed_from_int(device->mt.slots[slot].x),
122 wl_fixed_from_int(device->mt.slots[slot].y),
Neil Robertsdaf7d472013-09-24 12:09:03 +0100123 &x, &y);
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800124 seat_slot = device->mt.slots[slot].seat_slot;
125 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_MOTION);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100126 goto handled;
127 case EVDEV_ABSOLUTE_MT_UP:
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800128 seat_slot = device->mt.slots[slot].seat_slot;
129 master->slot_map &= ~(1 << seat_slot);
130 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100131 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100132 case EVDEV_ABSOLUTE_TOUCH_DOWN:
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800133 if (device->output == NULL)
134 break;
Neil Robertsbe336c82013-09-24 20:05:07 +0100135 transform_absolute(device, &cx, &cy);
136 weston_output_transform_coordinate(device->output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -0500137 wl_fixed_from_int(cx),
138 wl_fixed_from_int(cy),
139 &x, &y);
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800140 seat_slot = ffs(~master->slot_map) - 1;
141 device->abs.seat_slot = seat_slot;
142 master->slot_map |= 1 << seat_slot;
143 notify_touch(master, time, seat_slot, x, y, WL_TOUCH_DOWN);
Neil Robertsbe336c82013-09-24 20:05:07 +0100144 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100145 case EVDEV_ABSOLUTE_MOTION:
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800146 if (device->output == NULL)
147 break;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100148 transform_absolute(device, &cx, &cy);
149 weston_output_transform_coordinate(device->output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -0500150 wl_fixed_from_int(cx),
151 wl_fixed_from_int(cy),
152 &x, &y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100153
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800154 if (device->seat_caps & EVDEV_SEAT_TOUCH)
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800155 notify_touch(master, time, device->abs.seat_slot,
156 x, y, WL_TOUCH_MOTION);
Kristian Høgsbergcd2f46f2013-12-16 15:51:22 -0800157 else if (device->seat_caps & EVDEV_SEAT_POINTER)
Neil Robertsdaf7d472013-09-24 12:09:03 +0100158 notify_motion_absolute(master, time, x, y);
159 goto handled;
Neil Robertsbe336c82013-09-24 20:05:07 +0100160 case EVDEV_ABSOLUTE_TOUCH_UP:
Kristian Høgsberg2fce4802014-01-08 16:21:24 -0800161 seat_slot = device->abs.seat_slot;
162 master->slot_map &= ~(1 << seat_slot);
163 notify_touch(master, time, seat_slot, 0, 0, WL_TOUCH_UP);
Neil Robertsbe336c82013-09-24 20:05:07 +0100164 goto handled;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100165 }
166
167 assert(0 && "Unknown pending event type");
168
169handled:
170 device->pending_event = EVDEV_NONE;
171}
172
Neil Robertsbe336c82013-09-24 20:05:07 +0100173static void
174evdev_process_touch_button(struct evdev_device *device, int time, int value)
175{
176 if (device->pending_event != EVDEV_NONE &&
177 device->pending_event != EVDEV_ABSOLUTE_MOTION)
178 evdev_flush_pending_event(device, time);
179
180 device->pending_event = (value ?
181 EVDEV_ABSOLUTE_TOUCH_DOWN :
182 EVDEV_ABSOLUTE_TOUCH_UP);
183}
184
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300185static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300186evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300187{
Peter Hutterer89af60e2013-08-07 11:04:42 +1000188 /* ignore kernel key repeat */
Tiago Vignatti8755ff92011-11-10 14:47:30 +0200189 if (e->value == 2)
190 return;
191
Neil Robertsbe336c82013-09-24 20:05:07 +0100192 if (e->code == BTN_TOUCH) {
193 if (!device->is_mt)
194 evdev_process_touch_button(device, time, e->value);
195 return;
196 }
197
Neil Robertsdaf7d472013-09-24 12:09:03 +0100198 evdev_flush_pending_event(device, time);
199
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300200 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300201 case BTN_LEFT:
202 case BTN_RIGHT:
203 case BTN_MIDDLE:
204 case BTN_SIDE:
205 case BTN_EXTRA:
206 case BTN_FORWARD:
207 case BTN_BACK:
208 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400209 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100210 time, e->code,
211 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
212 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300213 break;
214
215 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400216 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100217 time, e->code,
218 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100219 WL_KEYBOARD_KEY_STATE_RELEASED,
220 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300221 break;
222 }
223}
224
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200225static void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100226evdev_process_touch(struct evdev_device *device,
227 struct input_event *e,
228 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200229{
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800230 int screen_width, screen_height;
231
232 if (device->output == NULL)
233 return;
234
235 screen_width = device->output->current_mode->width;
236 screen_height = device->output->current_mode->height;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200237
238 switch (e->code) {
239 case ABS_MT_SLOT:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100240 evdev_flush_pending_event(device, time);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500241 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200242 break;
243 case ABS_MT_TRACKING_ID:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100244 if (device->pending_event != EVDEV_NONE &&
245 device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
246 evdev_flush_pending_event(device, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200247 if (e->value >= 0)
Neil Robertsdaf7d472013-09-24 12:09:03 +0100248 device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200249 else
Neil Robertsdaf7d472013-09-24 12:09:03 +0100250 device->pending_event = EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200251 break;
252 case ABS_MT_POSITION_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100253 device->mt.slots[device->mt.slot].x =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500254 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700255 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100256 if (device->pending_event == EVDEV_NONE)
257 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200258 break;
259 case ABS_MT_POSITION_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100260 device->mt.slots[device->mt.slot].y =
Kristian Høgsberg39373542011-12-21 22:18:36 -0500261 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700262 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100263 if (device->pending_event == EVDEV_NONE)
264 device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200265 break;
266 }
267}
268
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300269static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300270evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500271 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300272{
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800273 int screen_width, screen_height;
274
275 if (device->output == NULL)
276 return;
277
278 screen_width = device->output->current_mode->width;
279 screen_height = device->output->current_mode->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300280
281 switch (e->code) {
282 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500283 device->abs.x =
284 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700285 (device->abs.max_x - device->abs.min_x);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100286 if (device->pending_event == EVDEV_NONE)
287 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300288 break;
289 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500290 device->abs.y =
291 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700292 (device->abs.max_y - device->abs.min_y);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100293 if (device->pending_event == EVDEV_NONE)
294 device->pending_event = EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300295 break;
296 }
297}
298
299static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300300evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200301 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300302{
303 switch (e->code) {
304 case REL_X:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100305 if (device->pending_event != EVDEV_RELATIVE_MOTION)
306 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400307 device->rel.dx += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100308 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300309 break;
310 case REL_Y:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100311 if (device->pending_event != EVDEV_RELATIVE_MOTION)
312 evdev_flush_pending_event(device, time);
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400313 device->rel.dy += wl_fixed_from_int(e->value);
Neil Robertsdaf7d472013-09-24 12:09:03 +0100314 device->pending_event = EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300315 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600316 case REL_WHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100317 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200318 switch (e->value) {
319 case -1:
320 /* Scroll down */
321 case 1:
322 /* Scroll up */
323 notify_axis(device->seat,
324 time,
325 WL_POINTER_AXIS_VERTICAL_SCROLL,
326 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
327 break;
328 default:
329 break;
330 }
Scott Moreau210d0792012-03-22 10:47:01 -0600331 break;
332 case REL_HWHEEL:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100333 evdev_flush_pending_event(device, time);
Jonas Ådahlb984e402012-10-03 22:56:58 +0200334 switch (e->value) {
335 case -1:
336 /* Scroll left */
337 case 1:
338 /* Scroll right */
339 notify_axis(device->seat,
340 time,
341 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
342 e->value * DEFAULT_AXIS_STEP_DISTANCE);
343 break;
344 default:
345 break;
346
347 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300348 }
349}
350
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200351static inline void
Neil Robertsdaf7d472013-09-24 12:09:03 +0100352evdev_process_absolute(struct evdev_device *device,
353 struct input_event *e,
354 uint32_t time)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200355{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200356 if (device->is_mt) {
Neil Robertsdaf7d472013-09-24 12:09:03 +0100357 evdev_process_touch(device, e, time);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200358 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500359 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200360 }
361}
362
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400363static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200364fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300365 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200366 struct input_event *event,
367 uint32_t time)
368{
369 switch (event->type) {
370 case EV_REL:
371 evdev_process_relative(device, event, time);
372 break;
373 case EV_ABS:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100374 evdev_process_absolute(device, event, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200375 break;
376 case EV_KEY:
377 evdev_process_key(device, event, time);
378 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500379 case EV_SYN:
Neil Robertsdaf7d472013-09-24 12:09:03 +0100380 evdev_flush_pending_event(device, time);
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500381 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200382 }
383}
384
385static void
386fallback_destroy(struct evdev_dispatch *dispatch)
387{
388 free(dispatch);
389}
390
391struct evdev_dispatch_interface fallback_interface = {
392 fallback_process,
393 fallback_destroy
394};
395
396static struct evdev_dispatch *
397fallback_dispatch_create(void)
398{
399 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
400 if (dispatch == NULL)
401 return NULL;
402
403 dispatch->interface = &fallback_interface;
404
405 return dispatch;
406}
407
408static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300409evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400410 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500411{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200412 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400413 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500414 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500415
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500416 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400417 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500418 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500419 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
420
Jonas Ådahl4136d822012-05-17 12:18:16 +0200421 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500422 }
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400423}
424
425static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300426evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400427{
428 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300429 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400430 struct input_event ev[32];
431 int len;
432
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300433 ec = device->seat->compositor;
Kristian Høgsberg10ddd972013-10-22 12:40:54 -0700434 if (!ec->session_active)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400435 return 1;
436
437 /* If the compositor is repainting, this function is called only once
438 * per frame and we have to process all the events available on the
439 * fd, otherwise there will be input lag. */
440 do {
441 if (device->mtdev)
442 len = mtdev_get(device->mtdev, fd, ev,
443 ARRAY_LENGTH(ev)) *
444 sizeof (struct input_event);
445 else
446 len = read(fd, &ev, sizeof ev);
447
448 if (len < 0 || len % sizeof ev[0] != 0) {
David Herrmanne05a0cd2013-10-15 14:29:56 +0200449 if (len < 0 && errno != EAGAIN && errno != EINTR) {
450 weston_log("device %s died\n",
451 device->devnode);
452 wl_event_source_remove(device->source);
453 device->source = NULL;
454 }
455
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400456 return 1;
457 }
458
459 evdev_process_events(device, ev, len / sizeof ev[0]);
460
461 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400462
463 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500464}
465
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300466static int
Kristian Høgsbergd24a64e2013-12-16 11:01:56 -0800467evdev_configure_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300468{
469 struct input_absinfo absinfo;
470 unsigned long ev_bits[NBITS(EV_MAX)];
471 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100472 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300473 unsigned long key_bits[NBITS(KEY_MAX)];
Kristian Høgsbergc67fd1c2013-12-16 15:37:16 -0800474 int has_abs, has_rel, has_mt;
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800475 int has_button, has_keyboard, has_touch;
Daniel Stone33965c22012-05-30 16:31:48 +0100476 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300477
Kristian Høgsberg015e73d2013-12-16 11:25:19 -0800478 has_rel = 0;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300479 has_abs = 0;
Kristian Høgsberg8d31a3a2013-12-16 13:42:40 -0800480 has_mt = 0;
Kristian Høgsberg77b0d232013-12-16 14:43:29 -0800481 has_button = 0;
Kristian Høgsberg3d793c92013-12-16 15:07:59 -0800482 has_keyboard = 0;
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800483 has_touch = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300484
485 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
486 if (TEST_BIT(ev_bits, EV_ABS)) {
487 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
488 abs_bits);
Kristian Høgsbergb1c02a82013-08-13 14:55:39 -0700489
Tiago Vignattid9c82502011-08-19 15:06:20 +0300490 if (TEST_BIT(abs_bits, ABS_X)) {
491 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200492 device->abs.min_x = absinfo.minimum;
493 device->abs.max_x = absinfo.maximum;
Kristian Høgsberg8d31a3a2013-12-16 13:42:40 -0800494 has_abs = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300495 }
496 if (TEST_BIT(abs_bits, ABS_Y)) {
497 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200498 device->abs.min_y = absinfo.minimum;
499 device->abs.max_y = absinfo.maximum;
Kristian Høgsberg8d31a3a2013-12-16 13:42:40 -0800500 has_abs = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300501 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000502 /* We only handle the slotted Protocol B in weston.
503 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
504 require mtdev for conversion. */
505 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
506 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300507 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
508 &absinfo);
509 device->abs.min_x = absinfo.minimum;
510 device->abs.max_x = absinfo.maximum;
511 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
512 &absinfo);
513 device->abs.min_y = absinfo.minimum;
514 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200515 device->is_mt = 1;
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800516 has_touch = 1;
Kristian Høgsberg8d31a3a2013-12-16 13:42:40 -0800517 has_mt = 1;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000518
519 if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) {
520 device->mtdev = mtdev_new_open(device->fd);
521 if (!device->mtdev) {
522 weston_log("mtdev required but failed to open for %s\n",
523 device->devnode);
524 return 0;
525 }
Peter Huttererdf66c5b2013-08-07 11:04:46 +1000526 device->mt.slot = device->mtdev->caps.slot.value;
527 } else {
528 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT),
529 &absinfo);
530 device->mt.slot = absinfo.value;
Peter Hutterer0d061e32013-08-07 11:04:45 +1000531 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200532 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300533 }
Daniel Stone33965c22012-05-30 16:31:48 +0100534 if (TEST_BIT(ev_bits, EV_REL)) {
535 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
536 rel_bits);
537 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
Kristian Høgsberg015e73d2013-12-16 11:25:19 -0800538 has_rel = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100539 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300540 if (TEST_BIT(ev_bits, EV_KEY)) {
541 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
542 key_bits);
543 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200544 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
Kristian Høgsberg8d31a3a2013-12-16 13:42:40 -0800545 (has_abs || has_mt)) {
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200546 device->dispatch = evdev_touchpad_create(device);
Peter Hutterer4477fee2013-08-07 11:04:49 +1000547 weston_log("input device %s, %s is a touchpad\n",
548 device->devname, device->devnode);
549 }
Daniel Stone33965c22012-05-30 16:31:48 +0100550 for (i = KEY_ESC; i < KEY_MAX; i++) {
551 if (i >= BTN_MISC && i < KEY_OK)
552 continue;
553 if (TEST_BIT(key_bits, i)) {
Kristian Høgsberg3d793c92013-12-16 15:07:59 -0800554 has_keyboard = 1;
Daniel Stone33965c22012-05-30 16:31:48 +0100555 break;
556 }
557 }
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800558 if (TEST_BIT(key_bits, BTN_TOUCH))
559 has_touch = 1;
Kristian Høgsbergc133fc42013-10-14 15:46:13 -0700560 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
561 if (TEST_BIT(key_bits, i)) {
Kristian Høgsberg77b0d232013-12-16 14:43:29 -0800562 has_button = 1;
Kristian Høgsbergc133fc42013-10-14 15:46:13 -0700563 break;
564 }
565 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300566 }
Kristian Høgsberg3d793c92013-12-16 15:07:59 -0800567 if (TEST_BIT(ev_bits, EV_LED))
568 has_keyboard = 1;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300569
Kristian Høgsberg77b0d232013-12-16 14:43:29 -0800570 if ((has_abs || has_rel) && has_button) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300571 weston_seat_init_pointer(device->seat);
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200572 device->seat_caps |= EVDEV_SEAT_POINTER;
Rob Bradford80137f32012-12-03 19:44:13 +0000573 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
574 device->devname, device->devnode,
Kristian Høgsbergb7c58de2013-12-16 13:55:48 -0800575 has_abs ? " absolute-motion" : "",
Kristian Høgsberg015e73d2013-12-16 11:25:19 -0800576 has_rel ? " relative-motion": "",
Kristian Høgsberg77b0d232013-12-16 14:43:29 -0800577 has_button ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300578 }
Kristian Høgsberg3d793c92013-12-16 15:07:59 -0800579 if (has_keyboard) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500580 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
581 return -1;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200582 device->seat_caps |= EVDEV_SEAT_KEYBOARD;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300583 weston_log("input device %s, %s is a keyboard\n",
584 device->devname, device->devnode);
585 }
Kristian Høgsbergde5f82c2013-12-16 15:19:30 -0800586 if (has_touch && !has_button) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300587 weston_seat_init_touch(device->seat);
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200588 device->seat_caps |= EVDEV_SEAT_TOUCH;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300589 weston_log("input device %s, %s is a touch device\n",
590 device->devname, device->devnode);
591 }
Daniel Stone74419a22012-05-30 16:32:02 +0100592
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300593 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300594}
595
Kristian Høgsberg17bccae2014-01-16 16:46:28 -0800596static void
597notify_output_destroy(struct wl_listener *listener, void *data)
598{
599 struct evdev_device *device =
600 container_of(listener,
601 struct evdev_device, output_destroy_listener);
602 struct weston_compositor *c = device->seat->compositor;
603 struct weston_output *output;
604
605 if (device->output_name) {
606 output = container_of(c->output_list.next,
607 struct weston_output, link);
608 evdev_device_set_output(device, output);
609 } else {
610 device->output = NULL;
611 }
612}
613
614void
615evdev_device_set_output(struct evdev_device *device,
616 struct weston_output *output)
617{
618 device->output = output;
619 device->output_destroy_listener.notify = notify_output_destroy;
620 wl_signal_add(&output->destroy_signal,
621 &device->output_destroy_listener);
622}
623
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300624struct evdev_device *
625evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500626{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300627 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500628 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300629 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500630
Peter Huttererf3d62272013-08-08 11:57:05 +1000631 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500632 if (device == NULL)
633 return NULL;
634
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300635 ec = seat->compositor;
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300636 device->seat = seat;
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200637 device->seat_caps = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200638 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300639 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400640 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500641 device->mt.slot = -1;
642 device->rel.dx = 0;
643 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200644 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300645 device->fd = device_fd;
Neil Robertsdaf7d472013-09-24 12:09:03 +0100646 device->pending_event = EVDEV_NONE;
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700647 wl_list_init(&device->link);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500648
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300649 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
Peter Hutterer11f5bfb2013-08-07 11:04:41 +1000650 devname[sizeof(devname) - 1] = '\0';
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300651 device->devname = strdup(devname);
652
Kristian Høgsbergd24a64e2013-12-16 11:01:56 -0800653 if (evdev_configure_device(device) == -1)
654 goto err;
655
656 if (device->seat_caps == 0) {
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000657 evdev_device_destroy(device);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500658 return EVDEV_UNHANDLED_DEVICE;
659 }
660
Jonas Ådahl4136d822012-05-17 12:18:16 +0200661 /* If the dispatch was not set up use the fallback. */
662 if (device->dispatch == NULL)
663 device->dispatch = fallback_dispatch_create();
664 if (device->dispatch == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000665 goto err;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200666
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500667 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500668 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300669 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400670 if (device->source == NULL)
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000671 goto err;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500672
673 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400674
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000675err:
676 evdev_device_destroy(device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400677 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500678}
679
Pekka Paalanen88594b62012-08-03 14:39:05 +0300680void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300681evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300682{
683 struct evdev_dispatch *dispatch;
684
Jonas Ådahld6e1c342013-10-17 23:04:05 +0200685 if (device->seat_caps & EVDEV_SEAT_POINTER)
686 weston_seat_release_pointer(device->seat);
687 if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
688 weston_seat_release_keyboard(device->seat);
689 if (device->seat_caps & EVDEV_SEAT_TOUCH)
690 weston_seat_release_touch(device->seat);
691
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300692 dispatch = device->dispatch;
693 if (dispatch)
694 dispatch->interface->destroy(dispatch);
695
Peter Hutterer6bb15cd2013-08-07 11:04:48 +1000696 if (device->source)
697 wl_event_source_remove(device->source);
Kristian Høgsberg82597282014-01-22 11:03:50 -0800698 if (device->output)
699 wl_list_remove(&device->output_destroy_listener.link);
Kristian Høgsberg4e55d062013-08-26 14:35:32 -0700700 wl_list_remove(&device->link);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300701 if (device->mtdev)
702 mtdev_close_delete(device->mtdev);
703 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300704 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300705 free(device->devnode);
Kristian Høgsberg8ca95442014-01-09 16:41:58 -0800706 free(device->output_name);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300707 free(device);
708}
709
Pekka Paalanen88594b62012-08-03 14:39:05 +0300710void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300711evdev_notify_keyboard_focus(struct weston_seat *seat,
712 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400713{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300714 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400715 struct wl_array keys;
716 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300717 char evdev_keys[(KEY_CNT + 7) / 8];
718 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400719 uint32_t *k;
720 int ret;
721
Jonas Ådahle82f8e42013-11-12 22:55:05 +0100722 if (!seat->keyboard_device_count > 0)
Pekka Paalanend8583512012-08-03 14:39:11 +0300723 return;
724
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400725 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300726 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400727 memset(evdev_keys, 0, sizeof evdev_keys);
728 ret = ioctl(device->fd,
729 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
730 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200731 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400732 device->devnode);
733 continue;
734 }
735 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
736 all_keys[i] |= evdev_keys[i];
737 }
738
739 wl_array_init(&keys);
740 for (i = 0; i < KEY_CNT; i++) {
741 set = all_keys[i >> 3] & (1 << (i & 7));
742 if (set) {
743 k = wl_array_add(&keys, sizeof *k);
744 *k = i;
745 }
746 }
747
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400748 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400749
750 wl_array_release(&keys);
751}