2 * Copyright © 2010 Intel Corporation
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.
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.
25 #include <linux/input.h>
30 #include "compositor.h"
34 evdev_led_update(struct wl_list *evdev_devices, enum weston_led leds)
37 enum weston_led weston;
40 { LED_NUM_LOCK, LED_NUML },
41 { LED_CAPS_LOCK, LED_CAPSL },
42 { LED_SCROLL_LOCK, LED_SCROLLL },
44 struct evdev_input_device *device;
45 struct input_event ev[ARRAY_LENGTH(map)];
48 memset(ev, 0, sizeof(ev));
49 for (i = 0; i < ARRAY_LENGTH(map); i++) {
51 ev[i].code = map[i].evdev;
52 ev[i].value = !!(leds & map[i].weston);
55 wl_list_for_each(device, evdev_devices, link) {
56 if (device->caps & EVDEV_KEYBOARD)
57 write(device->fd, ev, sizeof ev);
62 evdev_process_key(struct evdev_input_device *device,
63 struct input_event *e, int time)
77 notify_button(&device->seat->seat,
79 e->value ? WL_POINTER_BUTTON_STATE_PRESSED :
80 WL_POINTER_BUTTON_STATE_RELEASED);
84 notify_key(&device->seat->seat,
86 e->value ? WL_KEYBOARD_KEY_STATE_PRESSED :
87 WL_KEYBOARD_KEY_STATE_RELEASED,
88 STATE_UPDATE_AUTOMATIC);
94 evdev_process_touch(struct evdev_input_device *device,
95 struct input_event *e)
97 const int screen_width = device->output->current->width;
98 const int screen_height = device->output->current->height;
102 device->mt.slot = e->value;
104 case ABS_MT_TRACKING_ID:
106 device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN;
108 device->pending_events |= EVDEV_ABSOLUTE_MT_UP;
110 case ABS_MT_POSITION_X:
111 device->mt.x[device->mt.slot] =
112 (e->value - device->abs.min_x) * screen_width /
113 (device->abs.max_x - device->abs.min_x) +
115 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
117 case ABS_MT_POSITION_Y:
118 device->mt.y[device->mt.slot] =
119 (e->value - device->abs.min_y) * screen_height /
120 (device->abs.max_y - device->abs.min_y) +
122 device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION;
128 evdev_process_absolute_motion(struct evdev_input_device *device,
129 struct input_event *e)
131 const int screen_width = device->output->current->width;
132 const int screen_height = device->output->current->height;
137 (e->value - device->abs.min_x) * screen_width /
138 (device->abs.max_x - device->abs.min_x) +
140 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
144 (e->value - device->abs.min_y) * screen_height /
145 (device->abs.max_y - device->abs.min_y) +
147 device->pending_events |= EVDEV_ABSOLUTE_MOTION;
153 evdev_process_relative(struct evdev_input_device *device,
154 struct input_event *e, uint32_t time)
158 device->rel.dx += wl_fixed_from_int(e->value);
159 device->pending_events |= EVDEV_RELATIVE_MOTION;
162 device->rel.dy += wl_fixed_from_int(e->value);
163 device->pending_events |= EVDEV_RELATIVE_MOTION;
166 notify_axis(&device->seat->seat,
168 WL_POINTER_AXIS_VERTICAL_SCROLL,
169 wl_fixed_from_int(e->value));
172 notify_axis(&device->seat->seat,
174 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
175 wl_fixed_from_int(e->value));
181 evdev_process_absolute(struct evdev_input_device *device,
182 struct input_event *e)
185 evdev_process_touch(device, e);
187 evdev_process_absolute_motion(device, e);
192 is_motion_event(struct input_event *e)
205 case ABS_MT_POSITION_X:
206 case ABS_MT_POSITION_Y:
215 evdev_flush_motion(struct evdev_input_device *device, uint32_t time)
217 struct weston_seat *master = device->seat;
219 if (!device->pending_events)
222 if (device->pending_events & EVDEV_RELATIVE_MOTION) {
223 notify_motion(&master->seat, time,
224 master->seat.pointer->x + device->rel.dx,
225 master->seat.pointer->y + device->rel.dy);
226 device->pending_events &= ~EVDEV_RELATIVE_MOTION;
230 if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) {
231 notify_touch(&master->seat, time,
233 wl_fixed_from_int(device->mt.x[device->mt.slot]),
234 wl_fixed_from_int(device->mt.y[device->mt.slot]),
236 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
237 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
239 if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) {
240 notify_touch(&master->seat, time,
242 wl_fixed_from_int(device->mt.x[device->mt.slot]),
243 wl_fixed_from_int(device->mt.y[device->mt.slot]),
245 device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN;
246 device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION;
248 if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) {
249 notify_touch(&master->seat, time, device->mt.slot, 0, 0,
251 device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP;
253 if (device->pending_events & EVDEV_ABSOLUTE_MOTION) {
254 notify_motion(&master->seat, time,
255 wl_fixed_from_int(device->abs.x),
256 wl_fixed_from_int(device->abs.y));
257 device->pending_events &= ~EVDEV_ABSOLUTE_MOTION;
262 fallback_process(struct evdev_dispatch *dispatch,
263 struct evdev_input_device *device,
264 struct input_event *event,
267 switch (event->type) {
269 evdev_process_relative(device, event, time);
272 evdev_process_absolute(device, event);
275 evdev_process_key(device, event, time);
281 fallback_destroy(struct evdev_dispatch *dispatch)
286 struct evdev_dispatch_interface fallback_interface = {
291 static struct evdev_dispatch *
292 fallback_dispatch_create(void)
294 struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
295 if (dispatch == NULL)
298 dispatch->interface = &fallback_interface;
304 evdev_process_events(struct evdev_input_device *device,
305 struct input_event *ev, int count)
307 struct evdev_dispatch *dispatch = device->dispatch;
308 struct input_event *e, *end;
311 device->pending_events = 0;
315 for (e = ev; e < end; e++) {
316 time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000;
318 /* we try to minimize the amount of notifications to be
319 * forwarded to the compositor, so we accumulate motion
320 * events and send as a bunch */
321 if (!is_motion_event(e))
322 evdev_flush_motion(device, time);
324 dispatch->interface->process(dispatch, device, e, time);
327 evdev_flush_motion(device, time);
331 evdev_input_device_data(int fd, uint32_t mask, void *data)
333 struct weston_compositor *ec;
334 struct evdev_input_device *device = data;
335 struct input_event ev[32];
338 ec = device->seat->compositor;
342 /* If the compositor is repainting, this function is called only once
343 * per frame and we have to process all the events available on the
344 * fd, otherwise there will be input lag. */
347 len = mtdev_get(device->mtdev, fd, ev,
349 sizeof (struct input_event);
351 len = read(fd, &ev, sizeof ev);
353 if (len < 0 || len % sizeof ev[0] != 0) {
354 /* FIXME: call evdev_input_device_destroy when errno is ENODEV. */
358 evdev_process_events(device, ev, len / sizeof ev[0]);
366 evdev_configure_device(struct evdev_input_device *device)
368 struct input_absinfo absinfo;
369 unsigned long ev_bits[NBITS(EV_MAX)];
370 unsigned long abs_bits[NBITS(ABS_MAX)];
371 unsigned long rel_bits[NBITS(REL_MAX)];
372 unsigned long key_bits[NBITS(KEY_MAX)];
373 int has_key, has_abs;
380 ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
381 if (TEST_BIT(ev_bits, EV_ABS)) {
384 ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)),
386 if (TEST_BIT(abs_bits, ABS_X)) {
387 ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo);
388 device->abs.min_x = absinfo.minimum;
389 device->abs.max_x = absinfo.maximum;
390 device->caps |= EVDEV_MOTION_ABS;
392 if (TEST_BIT(abs_bits, ABS_Y)) {
393 ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo);
394 device->abs.min_y = absinfo.minimum;
395 device->abs.max_y = absinfo.maximum;
396 device->caps |= EVDEV_MOTION_ABS;
398 if (TEST_BIT(abs_bits, ABS_MT_SLOT)) {
399 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X),
401 device->abs.min_x = absinfo.minimum;
402 device->abs.max_x = absinfo.maximum;
403 ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y),
405 device->abs.min_y = absinfo.minimum;
406 device->abs.max_y = absinfo.maximum;
409 device->caps |= EVDEV_TOUCH;
412 if (TEST_BIT(ev_bits, EV_REL)) {
413 ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)),
415 if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
416 device->caps |= EVDEV_MOTION_REL;
418 if (TEST_BIT(ev_bits, EV_KEY)) {
420 ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)),
422 if (TEST_BIT(key_bits, BTN_TOOL_FINGER) &&
423 !TEST_BIT(key_bits, BTN_TOOL_PEN) &&
425 device->dispatch = evdev_touchpad_create(device);
426 for (i = KEY_ESC; i < KEY_MAX; i++) {
427 if (i >= BTN_MISC && i < KEY_OK)
429 if (TEST_BIT(key_bits, i)) {
430 device->caps |= EVDEV_KEYBOARD;
434 for (i = BTN_MISC; i < KEY_OK; i++) {
435 if (TEST_BIT(key_bits, i)) {
436 device->caps |= EVDEV_BUTTON;
441 if (TEST_BIT(ev_bits, EV_LED)) {
442 device->caps |= EVDEV_KEYBOARD;
445 /* This rule tries to catch accelerometer devices and opt out. We may
446 * want to adjust the protocol later adding a proper event for dealing
447 * with accelerometers and implement here accordingly */
448 if (has_abs && !has_key && !device->is_mt)
452 (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON)))
453 weston_seat_init_pointer(device->seat);
454 if ((device->caps & EVDEV_KEYBOARD))
455 weston_seat_init_keyboard(device->seat, NULL);
456 if ((device->caps & EVDEV_TOUCH))
457 weston_seat_init_touch(device->seat);
462 struct evdev_input_device *
463 evdev_input_device_create(struct weston_seat *seat,
464 const char *path, int device_fd)
466 struct evdev_input_device *device;
467 struct weston_compositor *ec;
469 device = malloc(sizeof *device);
472 memset(device, 0, sizeof *device);
474 ec = seat->compositor;
476 container_of(ec->output_list.next, struct weston_output, link);
480 device->mtdev = NULL;
481 device->devnode = strdup(path);
482 device->mt.slot = -1;
485 device->dispatch = NULL;
486 device->fd = device_fd;
488 if (evdev_configure_device(device) == -1)
491 /* If the dispatch was not set up use the fallback. */
492 if (device->dispatch == NULL)
493 device->dispatch = fallback_dispatch_create();
494 if (device->dispatch == NULL)
499 device->mtdev = mtdev_new_open(device->fd);
501 weston_log("mtdev failed to open for %s\n", path);
504 device->source = wl_event_loop_add_fd(ec->input_loop, device->fd,
506 evdev_input_device_data, device);
507 if (device->source == NULL)
513 device->dispatch->interface->destroy(device->dispatch);
515 free(device->devnode);
521 evdev_input_device_destroy(struct evdev_input_device *device)
523 struct evdev_dispatch *dispatch;
525 dispatch = device->dispatch;
527 dispatch->interface->destroy(dispatch);
529 wl_event_source_remove(device->source);
530 wl_list_remove(&device->link);
532 mtdev_close_delete(device->mtdev);
534 free(device->devnode);
539 evdev_notify_keyboard_focus(struct weston_seat *seat,
540 struct wl_list *evdev_devices)
542 struct evdev_input_device *device;
543 struct wl_array keys;
545 char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8];
549 memset(all_keys, 0, sizeof all_keys);
550 wl_list_for_each(device, evdev_devices, link) {
551 memset(evdev_keys, 0, sizeof evdev_keys);
552 ret = ioctl(device->fd,
553 EVIOCGKEY(sizeof evdev_keys), evdev_keys);
555 weston_log("failed to get keys for device %s\n",
559 for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
560 all_keys[i] |= evdev_keys[i];
563 wl_array_init(&keys);
564 for (i = 0; i < KEY_CNT; i++) {
565 set = all_keys[i >> 3] & (1 << (i & 7));
567 k = wl_array_add(&keys, sizeof *k);
572 notify_keyboard_focus_in(&seat->seat, &keys, STATE_UPDATE_AUTOMATIC);
574 wl_array_release(&keys);