blob: fc5004c24281f155deb2529adcb2ebe09f71970b [file] [log] [blame]
Kristian Høgsberg43db4012011-01-14 14:45:42 -05001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
24
Kristian Høgsberg43db4012011-01-14 14:45:42 -050025#include <stdlib.h>
26#include <string.h>
27#include <linux/input.h>
28#include <unistd.h>
29#include <fcntl.h>
Tiago Vignatti23fdeed2012-03-16 17:33:03 -030030#include <mtdev.h>
Kristian Høgsberg43db4012011-01-14 14:45:42 -050031
32#include "compositor.h"
Tiago Vignattice03ec32011-12-19 01:14:03 +020033#include "evdev.h"
Kristian Høgsberg43db4012011-01-14 14:45:42 -050034
Jonas Ådahlb984e402012-10-03 22:56:58 +020035#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
36
Pekka Paalanen88594b62012-08-03 14:39:05 +030037void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030038evdev_led_update(struct evdev_device *device, enum weston_led leds)
Daniel Stone7bbd5b32012-05-30 16:31:49 +010039{
40 static const struct {
41 enum weston_led weston;
42 int evdev;
43 } map[] = {
44 { LED_NUM_LOCK, LED_NUML },
45 { LED_CAPS_LOCK, LED_CAPSL },
46 { LED_SCROLL_LOCK, LED_SCROLLL },
47 };
Daniel Stone7bbd5b32012-05-30 16:31:49 +010048 struct input_event ev[ARRAY_LENGTH(map)];
49 unsigned int i;
50
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030051 if (!device->caps & EVDEV_KEYBOARD)
52 return;
53
Daniel Stone7bbd5b32012-05-30 16:31:49 +010054 memset(ev, 0, sizeof(ev));
55 for (i = 0; i < ARRAY_LENGTH(map); i++) {
56 ev[i].type = EV_LED;
57 ev[i].code = map[i].evdev;
58 ev[i].value = !!(leds & map[i].weston);
59 }
60
Pekka Paalanenb9d38f42012-08-06 14:57:07 +030061 i = write(device->fd, ev, sizeof ev);
62 (void)i; /* no, we really don't care about the return value */
Daniel Stone7bbd5b32012-05-30 16:31:49 +010063}
64
Tiago Vignatti8be003b2011-09-01 19:00:03 +030065static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +030066evdev_process_key(struct evdev_device *device, struct input_event *e, int time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +030067{
Tiago Vignatti8755ff92011-11-10 14:47:30 +020068 if (e->value == 2)
69 return;
70
Tiago Vignatti8be003b2011-09-01 19:00:03 +030071 switch (e->code) {
Tiago Vignatti8be003b2011-09-01 19:00:03 +030072 case BTN_LEFT:
73 case BTN_RIGHT:
74 case BTN_MIDDLE:
75 case BTN_SIDE:
76 case BTN_EXTRA:
77 case BTN_FORWARD:
78 case BTN_BACK:
79 case BTN_TASK:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040080 notify_button(device->seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +010081 time, e->code,
82 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
83 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030084 break;
85
Kristian Høgsberg0af26c42013-07-26 10:43:26 -070086 case BTN_TOUCH:
87 if (e->value == 0 && !device->is_mt)
88 notify_touch(device->seat, time, device->mt.slot, 0, 0,
89 WL_TOUCH_UP);
90 break;
Tiago Vignatti8be003b2011-09-01 19:00:03 +030091 default:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -040092 notify_key(device->seat,
Daniel Stonec9785ea2012-05-30 16:31:52 +010093 time, e->code,
94 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +010095 WL_KEYBOARD_KEY_STATE_RELEASED,
96 STATE_UPDATE_AUTOMATIC);
Tiago Vignatti8be003b2011-09-01 19:00:03 +030097 break;
98 }
99}
100
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200101static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300102evdev_process_touch(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200103{
104 const int screen_width = device->output->current->width;
105 const int screen_height = device->output->current->height;
106
107 switch (e->code) {
108 case ABS_MT_SLOT:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500109 device->mt.slot = e->value;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200110 break;
111 case ABS_MT_TRACKING_ID:
112 if (e->value >= 0)
Daniel Stone1d637772012-05-30 16:31:47 +0100113 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200114 else
Daniel Stone1d637772012-05-30 16:31:47 +0100115 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200116 break;
117 case ABS_MT_POSITION_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500118 device->mt.x[device->mt.slot] =
119 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700120 (device->abs.max_x - device->abs.min_x);
Daniel Stone1d637772012-05-30 16:31:47 +0100121 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200122 break;
123 case ABS_MT_POSITION_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500124 device->mt.y[device->mt.slot] =
125 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700126 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100127 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200128 break;
129 }
130}
131
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300132static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300133evdev_process_absolute_motion(struct evdev_device *device,
Kristian Høgsberg39373542011-12-21 22:18:36 -0500134 struct input_event *e)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300135{
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400136 const int screen_width = device->output->current->width;
137 const int screen_height = device->output->current->height;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300138
139 switch (e->code) {
140 case ABS_X:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500141 device->abs.x =
142 (e->value - device->abs.min_x) * screen_width /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700143 (device->abs.max_x - device->abs.min_x);
Daniel Stone1d637772012-05-30 16:31:47 +0100144 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300145 break;
146 case ABS_Y:
Kristian Høgsberg39373542011-12-21 22:18:36 -0500147 device->abs.y =
148 (e->value - device->abs.min_y) * screen_height /
Kristian Høgsbergcee407e2013-07-26 10:40:32 -0700149 (device->abs.max_y - device->abs.min_y);
Daniel Stone1d637772012-05-30 16:31:47 +0100150 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300151 break;
152 }
153}
154
155static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300156evdev_process_relative(struct evdev_device *device,
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200157 struct input_event *e, uint32_t time)
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300158{
159 switch (e->code) {
160 case REL_X:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400161 device->rel.dx += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100162 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300163 break;
164 case REL_Y:
Jonas Ådahlc0ca3992012-05-10 16:46:48 -0400165 device->rel.dy += wl_fixed_from_int(e->value);
Daniel Stone1d637772012-05-30 16:31:47 +0100166 device->pending_events |= EVDEV_RELATIVE_MOTION;
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300167 break;
Scott Moreau210d0792012-03-22 10:47:01 -0600168 case REL_WHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200169 switch (e->value) {
170 case -1:
171 /* Scroll down */
172 case 1:
173 /* Scroll up */
174 notify_axis(device->seat,
175 time,
176 WL_POINTER_AXIS_VERTICAL_SCROLL,
177 -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
178 break;
179 default:
180 break;
181 }
Scott Moreau210d0792012-03-22 10:47:01 -0600182 break;
183 case REL_HWHEEL:
Jonas Ådahlb984e402012-10-03 22:56:58 +0200184 switch (e->value) {
185 case -1:
186 /* Scroll left */
187 case 1:
188 /* Scroll right */
189 notify_axis(device->seat,
190 time,
191 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
192 e->value * DEFAULT_AXIS_STEP_DISTANCE);
193 break;
194 default:
195 break;
196
197 }
Tiago Vignatti8be003b2011-09-01 19:00:03 +0300198 }
199}
200
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200201static inline void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300202evdev_process_absolute(struct evdev_device *device, struct input_event *e)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200203{
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200204 if (device->is_mt) {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500205 evdev_process_touch(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200206 } else {
Kristian Høgsberg39373542011-12-21 22:18:36 -0500207 evdev_process_absolute_motion(device, e);
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200208 }
209}
210
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400211static int
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200212is_motion_event(struct input_event *e)
213{
214 switch (e->type) {
215 case EV_REL:
216 switch (e->code) {
217 case REL_X:
218 case REL_Y:
219 return 1;
220 }
Rob Bradford4b997e42012-10-09 18:44:31 +0100221 break;
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200222 case EV_ABS:
223 switch (e->code) {
224 case ABS_X:
225 case ABS_Y:
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200226 case ABS_MT_POSITION_X:
227 case ABS_MT_POSITION_Y:
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200228 return 1;
229 }
230 }
231
232 return 0;
233}
234
235static void
Rob Bradfordea23b282012-12-03 19:44:16 +0000236transform_absolute(struct evdev_device *device)
237{
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700238 int32_t x, y;
239
Rob Bradfordea23b282012-12-03 19:44:16 +0000240 if (!device->abs.apply_calibration)
241 return;
242
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700243 x = device->abs.x * device->abs.calibration[0] +
244 device->abs.y * device->abs.calibration[1] +
245 device->abs.calibration[2];
Rob Bradfordea23b282012-12-03 19:44:16 +0000246
Kristian Høgsberg58014bb2013-07-26 10:41:43 -0700247 y = device->abs.x * device->abs.calibration[3] +
248 device->abs.y * device->abs.calibration[4] +
249 device->abs.calibration[5];
250
251 device->abs.x = x;
252 device->abs.y = y;
Rob Bradfordea23b282012-12-03 19:44:16 +0000253}
254
255static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300256evdev_flush_motion(struct evdev_device *device, uint32_t time)
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200257{
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300258 struct weston_seat *master = device->seat;
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700259 wl_fixed_t x, y;
260 int slot;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500261
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500262 if (!(device->pending_events & EVDEV_SYN))
Tiago Vignatti12c05b72011-12-08 13:20:46 +0200263 return;
264
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700265 slot = device->mt.slot;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500266 device->pending_events &= ~EVDEV_SYN;
Daniel Stone1d637772012-05-30 16:31:47 +0100267 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500268 notify_motion(master, time, device->rel.dx, device->rel.dy);
Daniel Stone1d637772012-05-30 16:31:47 +0100269 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500270 device->rel.dx = 0;
271 device->rel.dy = 0;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200272 }
Daniel Stone1d637772012-05-30 16:31:47 +0100273 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700274 weston_output_transform_coordinate(device->output,
275 device->mt.x[slot],
276 device->mt.y[slot],
277 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400278 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700279 device->mt.slot, x, y, WL_TOUCH_DOWN);
Daniel Stone1d637772012-05-30 16:31:47 +0100280 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
281 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200282 }
Daniel Stone1d637772012-05-30 16:31:47 +0100283 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700284 weston_output_transform_coordinate(device->output,
285 device->mt.x[slot],
286 device->mt.y[slot],
287 &x, &y);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400288 notify_touch(master, time,
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700289 device->mt.slot, x, y, WL_TOUCH_MOTION);
Daniel Stone1d637772012-05-30 16:31:47 +0100290 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
291 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200292 }
Daniel Stone1d637772012-05-30 16:31:47 +0100293 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400294 notify_touch(master, time, device->mt.slot, 0, 0,
Daniel Stone37816df2012-05-16 18:45:18 +0100295 WL_TOUCH_UP);
Daniel Stone1d637772012-05-30 16:31:47 +0100296 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200297 }
Daniel Stone1d637772012-05-30 16:31:47 +0100298 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
Rob Bradfordea23b282012-12-03 19:44:16 +0000299 transform_absolute(device);
Kristian Høgsberg97e806f2013-07-22 15:09:30 -0700300 weston_output_transform_coordinate(device->output,
301 device->abs.x,
302 device->abs.y, &x, &y);
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700303
304 if (device->caps & EVDEV_TOUCH) {
305 if (master->num_tp == 0)
306 notify_touch(master, time, 0,
307 x, y, WL_TOUCH_DOWN);
308 else
309 notify_touch(master, time, 0,
310 x, y, WL_TOUCH_MOTION);
311 } else
312 notify_motion_absolute(master, time, x, y);
Daniel Stone1d637772012-05-30 16:31:47 +0100313 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200314 }
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200315}
316
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400317static void
Jonas Ådahl4136d822012-05-17 12:18:16 +0200318fallback_process(struct evdev_dispatch *dispatch,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300319 struct evdev_device *device,
Jonas Ådahl4136d822012-05-17 12:18:16 +0200320 struct input_event *event,
321 uint32_t time)
322{
323 switch (event->type) {
324 case EV_REL:
325 evdev_process_relative(device, event, time);
326 break;
327 case EV_ABS:
328 evdev_process_absolute(device, event);
329 break;
330 case EV_KEY:
331 evdev_process_key(device, event, time);
332 break;
Satyeshwar Singh964a3422013-02-27 15:26:23 -0500333 case EV_SYN:
334 device->pending_events |= EVDEV_SYN;
335 break;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200336 }
337}
338
339static void
340fallback_destroy(struct evdev_dispatch *dispatch)
341{
342 free(dispatch);
343}
344
345struct evdev_dispatch_interface fallback_interface = {
346 fallback_process,
347 fallback_destroy
348};
349
350static struct evdev_dispatch *
351fallback_dispatch_create(void)
352{
353 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
354 if (dispatch == NULL)
355 return NULL;
356
357 dispatch->interface = &fallback_interface;
358
359 return dispatch;
360}
361
362static void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300363evdev_process_events(struct evdev_device *device,
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400364 struct input_event *ev, int count)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500365{
Jonas Ådahl4136d822012-05-17 12:18:16 +0200366 struct evdev_dispatch *dispatch = device->dispatch;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400367 struct input_event *e, *end;
Kristian Høgsberg865f9b82011-12-02 06:39:02 -0500368 uint32_t time = 0;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500369
Daniel Stone1d637772012-05-30 16:31:47 +0100370 device->pending_events = 0;
Tiago Vignattia12d6112012-01-20 18:47:46 +0200371
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500372 e = ev;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400373 end = e + count;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500374 for (e = ev; e < end; e++) {
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500375 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
376
Tiago Vignatti52e158d2011-11-18 14:56:58 +0200377 /* we try to minimize the amount of notifications to be
378 * forwarded to the compositor, so we accumulate motion
379 * events and send as a bunch */
Tiago Vignatti80885e12011-11-21 17:59:31 +0200380 if (!is_motion_event(e))
Kristian Høgsberg39373542011-12-21 22:18:36 -0500381 evdev_flush_motion(device, time);
Jonas Ådahl4136d822012-05-17 12:18:16 +0200382
383 dispatch->interface->process(dispatch, device, e, time);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500384 }
385
Kristian Høgsberg39373542011-12-21 22:18:36 -0500386 evdev_flush_motion(device, time);
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400387}
388
389static int
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300390evdev_device_data(int fd, uint32_t mask, void *data)
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400391{
392 struct weston_compositor *ec;
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300393 struct evdev_device *device = data;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400394 struct input_event ev[32];
395 int len;
396
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300397 ec = device->seat->compositor;
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400398 if (!ec->focus)
399 return 1;
400
401 /* If the compositor is repainting, this function is called only once
402 * per frame and we have to process all the events available on the
403 * fd, otherwise there will be input lag. */
404 do {
405 if (device->mtdev)
406 len = mtdev_get(device->mtdev, fd, ev,
407 ARRAY_LENGTH(ev)) *
408 sizeof (struct input_event);
409 else
410 len = read(fd, &ev, sizeof ev);
411
412 if (len < 0 || len % sizeof ev[0] != 0) {
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300413 /* FIXME: call evdev_device_destroy when errno is ENODEV. */
Ander Conselvan de Oliveira29d95562012-03-20 19:52:57 -0400414 return 1;
415 }
416
417 evdev_process_events(device, ev, len / sizeof ev[0]);
418
419 } while (len > 0);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400420
421 return 1;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500422}
423
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300424static int
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500425evdev_handle_device(struct evdev_device *device)
Tiago Vignattid9c82502011-08-19 15:06:20 +0300426{
427 struct input_absinfo absinfo;
428 unsigned long ev_bits[NBITS(EV_MAX)];
429 unsigned long abs_bits[NBITS(ABS_MAX)];
Daniel Stone33965c22012-05-30 16:31:48 +0100430 unsigned long rel_bits[NBITS(REL_MAX)];
Tiago Vignattid9c82502011-08-19 15:06:20 +0300431 unsigned long key_bits[NBITS(KEY_MAX)];
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300432 int has_key, has_abs;
Daniel Stone33965c22012-05-30 16:31:48 +0100433 unsigned int i;
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300434
435 has_key = 0;
436 has_abs = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100437 device->caps = 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300438
439 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
440 if (TEST_BIT(ev_bits, EV_ABS)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300441 has_abs = 1;
Tiago Vignattia157fc12011-11-21 16:39:55 +0200442
Tiago Vignattid9c82502011-08-19 15:06:20 +0300443 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
444 abs_bits);
445 if (TEST_BIT(abs_bits, ABS_X)) {
446 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200447 device->abs.min_x = absinfo.minimum;
448 device->abs.max_x = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100449 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300450 }
451 if (TEST_BIT(abs_bits, ABS_Y)) {
452 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
Tiago Vignattia157fc12011-11-21 16:39:55 +0200453 device->abs.min_y = absinfo.minimum;
454 device->abs.max_y = absinfo.maximum;
Daniel Stone33965c22012-05-30 16:31:48 +0100455 device->caps |= EVDEV_MOTION_ABS;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300456 }
Peter Hutterer45d659d2013-08-08 12:03:08 +1000457 /* We only handle the slotted Protocol B in weston.
458 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
459 require mtdev for conversion. */
460 if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) &&
461 TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) {
Pekka Paalanen2fc7cce2012-07-31 13:21:07 +0300462 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
463 &absinfo);
464 device->abs.min_x = absinfo.minimum;
465 device->abs.max_x = absinfo.maximum;
466 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
467 &absinfo);
468 device->abs.min_y = absinfo.minimum;
469 device->abs.max_y = absinfo.maximum;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200470 device->is_mt = 1;
Kristian Høgsberg39373542011-12-21 22:18:36 -0500471 device->mt.slot = 0;
Daniel Stone33965c22012-05-30 16:31:48 +0100472 device->caps |= EVDEV_TOUCH;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200473 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300474 }
Daniel Stone33965c22012-05-30 16:31:48 +0100475 if (TEST_BIT(ev_bits, EV_REL)) {
476 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
477 rel_bits);
478 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
479 device->caps |= EVDEV_MOTION_REL;
480 }
Tiago Vignattid9c82502011-08-19 15:06:20 +0300481 if (TEST_BIT(ev_bits, EV_KEY)) {
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300482 has_key = 1;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300483 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
484 key_bits);
485 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
Jonas Ådahl1df17af2012-05-17 12:18:17 +0200486 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
487 has_abs)
488 device->dispatch = evdev_touchpad_create(device);
Daniel Stone33965c22012-05-30 16:31:48 +0100489 for (i = KEY_ESC; i < KEY_MAX; i++) {
490 if (i >= BTN_MISC && i < KEY_OK)
491 continue;
492 if (TEST_BIT(key_bits, i)) {
493 device->caps |= EVDEV_KEYBOARD;
494 break;
495 }
496 }
497 for (i = BTN_MISC; i < KEY_OK; i++) {
498 if (TEST_BIT(key_bits, i)) {
499 device->caps |= EVDEV_BUTTON;
500 break;
501 }
502 }
Kristian Høgsberg0af26c42013-07-26 10:43:26 -0700503 if (TEST_BIT(key_bits, BTN_TOUCH)) {
504 device->caps |= EVDEV_TOUCH;
505 }
506
Tiago Vignattid9c82502011-08-19 15:06:20 +0300507 }
Daniel Stonecfd0e722012-06-01 12:13:59 +0100508 if (TEST_BIT(ev_bits, EV_LED)) {
509 device->caps |= EVDEV_KEYBOARD;
510 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300511
512 /* This rule tries to catch accelerometer devices and opt out. We may
513 * want to adjust the protocol later adding a proper event for dealing
514 * with accelerometers and implement here accordingly */
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300515 if (has_abs && !has_key && !device->is_mt) {
516 weston_log("input device %s, %s "
517 "ignored: unsupported device type\n",
518 device->devname, device->devnode);
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500519 return 0;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300520 }
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300521
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500522 return 1;
523}
524
525static int
526evdev_configure_device(struct evdev_device *device)
527{
Daniel Stone74419a22012-05-30 16:32:02 +0100528 if ((device->caps &
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300529 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300530 weston_seat_init_pointer(device->seat);
Rob Bradford80137f32012-12-03 19:44:13 +0000531 weston_log("input device %s, %s is a pointer caps =%s%s%s\n",
532 device->devname, device->devnode,
533 device->caps & EVDEV_MOTION_ABS ? " absolute-motion" : "",
534 device->caps & EVDEV_MOTION_REL ? " relative-motion": "",
535 device->caps & EVDEV_BUTTON ? " button" : "");
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300536 }
537 if ((device->caps & EVDEV_KEYBOARD)) {
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500538 if (weston_seat_init_keyboard(device->seat, NULL) < 0)
539 return -1;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300540 weston_log("input device %s, %s is a keyboard\n",
541 device->devname, device->devnode);
542 }
543 if ((device->caps & EVDEV_TOUCH)) {
Pekka Paalanen5618d6f2012-08-03 14:39:00 +0300544 weston_seat_init_touch(device->seat);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300545 weston_log("input device %s, %s is a touch device\n",
546 device->devname, device->devnode);
547 }
Daniel Stone74419a22012-05-30 16:32:02 +0100548
Tiago Vignattic0827fd2011-08-19 17:07:40 +0300549 return 0;
Tiago Vignattid9c82502011-08-19 15:06:20 +0300550}
551
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300552struct evdev_device *
553evdev_device_create(struct weston_seat *seat, const char *path, int device_fd)
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500554{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300555 struct evdev_device *device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500556 struct weston_compositor *ec;
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300557 char devname[256] = "unknown";
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500558
Peter Huttererf3d62272013-08-08 11:57:05 +1000559 device = zalloc(sizeof *device);
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500560 if (device == NULL)
561 return NULL;
562
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300563 ec = seat->compositor;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400564 device->output =
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500565 container_of(ec->output_list.next, struct weston_output, link);
Kristian Høgsberge7b5b412011-09-01 13:25:50 -0400566
Pekka Paalanen43f0a1e2012-08-03 14:39:03 +0300567 device->seat = seat;
Tiago Vignatti22c6bce2011-12-21 19:34:09 +0200568 device->is_mt = 0;
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300569 device->mtdev = NULL;
Tiago Vignattiac2dc6a2011-10-28 13:05:06 -0400570 device->devnode = strdup(path);
Kristian Høgsberg39373542011-12-21 22:18:36 -0500571 device->mt.slot = -1;
572 device->rel.dx = 0;
573 device->rel.dy = 0;
Jonas Ådahl4136d822012-05-17 12:18:16 +0200574 device->dispatch = NULL;
Pekka Paalanen5a6383b2012-08-03 14:38:58 +0300575 device->fd = device_fd;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500576
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300577 ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname);
578 device->devname = strdup(devname);
579
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -0500580 if (!evdev_handle_device(device)) {
581 free(device->devnode);
582 free(device->devname);
583 free(device);
584 return EVDEV_UNHANDLED_DEVICE;
585 }
586
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400587 if (evdev_configure_device(device) == -1)
588 goto err1;
Kristian Høgsberg96c8be92011-01-14 14:49:46 -0500589
Jonas Ådahl4136d822012-05-17 12:18:16 +0200590 /* If the dispatch was not set up use the fallback. */
591 if (device->dispatch == NULL)
592 device->dispatch = fallback_dispatch_create();
593 if (device->dispatch == NULL)
594 goto err1;
595
596
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300597 if (device->is_mt) {
598 device->mtdev = mtdev_new_open(device->fd);
599 if (!device->mtdev)
Martin Minarik6d118362012-06-07 18:01:59 +0200600 weston_log("mtdev failed to open for %s\n", path);
Tiago Vignatti23fdeed2012-03-16 17:33:03 -0300601 }
602
Kristian Høgsberg7ea10862012-03-05 17:49:30 -0500603 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500604 WL_EVENT_READABLE,
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300605 evdev_device_data, device);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400606 if (device->source == NULL)
Jonas Ådahl4136d822012-05-17 12:18:16 +0200607 goto err2;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500608
609 return device;
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400610
Jonas Ådahl4136d822012-05-17 12:18:16 +0200611err2:
612 device->dispatch->interface->destroy(device->dispatch);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400613err1:
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300614 free(device->devname);
Tiago Vignattiac9cfd32011-10-28 13:15:25 -0400615 free(device->devnode);
616 free(device);
617 return NULL;
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500618}
619
Pekka Paalanen88594b62012-08-03 14:39:05 +0300620void
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300621evdev_device_destroy(struct evdev_device *device)
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300622{
623 struct evdev_dispatch *dispatch;
624
625 dispatch = device->dispatch;
626 if (dispatch)
627 dispatch->interface->destroy(dispatch);
628
629 wl_event_source_remove(device->source);
630 wl_list_remove(&device->link);
631 if (device->mtdev)
632 mtdev_close_delete(device->mtdev);
633 close(device->fd);
Pekka Paalanenbf639ab2012-08-03 14:39:07 +0300634 free(device->devname);
Pekka Paalanena123e5c2012-08-03 14:38:57 +0300635 free(device->devnode);
636 free(device);
637}
638
Pekka Paalanen88594b62012-08-03 14:39:05 +0300639void
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300640evdev_notify_keyboard_focus(struct weston_seat *seat,
641 struct wl_list *evdev_devices)
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400642{
Pekka Paalanen3eb47612012-08-06 14:57:08 +0300643 struct evdev_device *device;
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400644 struct wl_array keys;
645 unsigned int i, set;
Pekka Paalanend8583512012-08-03 14:39:11 +0300646 char evdev_keys[(KEY_CNT + 7) / 8];
647 char all_keys[(KEY_CNT + 7) / 8];
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400648 uint32_t *k;
649 int ret;
650
Kristian Høgsberge3148752013-05-06 23:19:49 -0400651 if (!seat->keyboard)
Pekka Paalanend8583512012-08-03 14:39:11 +0300652 return;
653
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400654 memset(all_keys, 0, sizeof all_keys);
Pekka Paalanen3bfb2012012-08-03 14:39:02 +0300655 wl_list_for_each(device, evdev_devices, link) {
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400656 memset(evdev_keys, 0, sizeof evdev_keys);
657 ret = ioctl(device->fd,
658 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
659 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200660 weston_log("failed to get keys for device %s\n",
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400661 device->devnode);
662 continue;
663 }
664 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
665 all_keys[i] |= evdev_keys[i];
666 }
667
668 wl_array_init(&keys);
669 for (i = 0; i < KEY_CNT; i++) {
670 set = all_keys[i >> 3] & (1 << (i & 7));
671 if (set) {
672 k = wl_array_add(&keys, sizeof *k);
673 *k = i;
674 }
675 }
676
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400677 notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
Kristian Høgsberga00d60f2012-04-10 00:03:30 -0400678
679 wl_array_release(&keys);
680}