Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 1 | /* |
| 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 Vignatti | 23fdeed | 2012-03-16 17:33:03 -0300 | [diff] [blame] | 29 | #include <mtdev.h> |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 30 | |
| 31 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 32 | #include "evdev.h" |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 33 | #include "evdev-private.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 34 | #include "launcher-util.h" |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 35 | |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 36 | static void |
| 37 | evdev_led_update(struct weston_seat *seat_base, enum weston_led leds) |
| 38 | { |
| 39 | static const struct { |
| 40 | enum weston_led weston; |
| 41 | int evdev; |
| 42 | } map[] = { |
| 43 | { LED_NUM_LOCK, LED_NUML }, |
| 44 | { LED_CAPS_LOCK, LED_CAPSL }, |
| 45 | { LED_SCROLL_LOCK, LED_SCROLLL }, |
| 46 | }; |
| 47 | struct evdev_seat *seat = (struct evdev_seat *) seat_base; |
| 48 | struct evdev_input_device *device; |
| 49 | struct input_event ev[ARRAY_LENGTH(map)]; |
| 50 | unsigned int i; |
| 51 | |
| 52 | memset(ev, 0, sizeof(ev)); |
| 53 | for (i = 0; i < ARRAY_LENGTH(map); i++) { |
| 54 | ev[i].type = EV_LED; |
| 55 | ev[i].code = map[i].evdev; |
| 56 | ev[i].value = !!(leds & map[i].weston); |
| 57 | } |
| 58 | |
| 59 | wl_list_for_each(device, &seat->devices_list, link) { |
| 60 | if (device->caps & EVDEV_KEYBOARD) |
| 61 | write(device->fd, ev, sizeof *ev); |
| 62 | } |
| 63 | } |
| 64 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 65 | static inline void |
| 66 | evdev_process_key(struct evdev_input_device *device, |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 67 | struct input_event *e, int time) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 68 | { |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 69 | if (e->value == 2) |
| 70 | return; |
| 71 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 72 | switch (e->code) { |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 73 | case BTN_LEFT: |
| 74 | case BTN_RIGHT: |
| 75 | case BTN_MIDDLE: |
| 76 | case BTN_SIDE: |
| 77 | case BTN_EXTRA: |
| 78 | case BTN_FORWARD: |
| 79 | case BTN_BACK: |
| 80 | case BTN_TASK: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 81 | notify_button(&device->master->base.seat, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 82 | time, e->code, |
| 83 | e->value ? WL_POINTER_BUTTON_STATE_PRESSED : |
| 84 | WL_POINTER_BUTTON_STATE_RELEASED); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 85 | break; |
| 86 | |
| 87 | default: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 88 | notify_key(&device->master->base.seat, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 89 | time, e->code, |
| 90 | e->value ? WL_KEYBOARD_KEY_STATE_PRESSED : |
| 91 | WL_KEYBOARD_KEY_STATE_RELEASED); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 96 | static void |
| 97 | evdev_process_touch(struct evdev_input_device *device, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 98 | struct input_event *e) |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 99 | { |
| 100 | const int screen_width = device->output->current->width; |
| 101 | const int screen_height = device->output->current->height; |
| 102 | |
| 103 | switch (e->code) { |
| 104 | case ABS_MT_SLOT: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 105 | device->mt.slot = e->value; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 106 | break; |
| 107 | case ABS_MT_TRACKING_ID: |
| 108 | if (e->value >= 0) |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 109 | device->pending_events |= EVDEV_ABSOLUTE_MT_DOWN; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 110 | else |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 111 | device->pending_events |= EVDEV_ABSOLUTE_MT_UP; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 112 | break; |
| 113 | case ABS_MT_POSITION_X: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 114 | device->mt.x[device->mt.slot] = |
| 115 | (e->value - device->abs.min_x) * screen_width / |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 116 | (device->abs.max_x - device->abs.min_x) + |
| 117 | device->output->x; |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 118 | device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 119 | break; |
| 120 | case ABS_MT_POSITION_Y: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 121 | device->mt.y[device->mt.slot] = |
| 122 | (e->value - device->abs.min_y) * screen_height / |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 123 | (device->abs.max_y - device->abs.min_y) + |
| 124 | device->output->y; |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 125 | device->pending_events |= EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 130 | static inline void |
| 131 | evdev_process_absolute_motion(struct evdev_input_device *device, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 132 | struct input_event *e) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 133 | { |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 134 | const int screen_width = device->output->current->width; |
| 135 | const int screen_height = device->output->current->height; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 136 | |
| 137 | switch (e->code) { |
| 138 | case ABS_X: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 139 | device->abs.x = |
| 140 | (e->value - device->abs.min_x) * screen_width / |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 141 | (device->abs.max_x - device->abs.min_x) + |
| 142 | device->output->x; |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 143 | device->pending_events |= EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 144 | break; |
| 145 | case ABS_Y: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 146 | device->abs.y = |
| 147 | (e->value - device->abs.min_y) * screen_height / |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 148 | (device->abs.max_y - device->abs.min_y) + |
| 149 | device->output->y; |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 150 | device->pending_events |= EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static inline void |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 156 | evdev_process_relative(struct evdev_input_device *device, |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 157 | struct input_event *e, uint32_t time) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 158 | { |
| 159 | switch (e->code) { |
| 160 | case REL_X: |
Jonas Ådahl | c0ca399 | 2012-05-10 16:46:48 -0400 | [diff] [blame] | 161 | device->rel.dx += wl_fixed_from_int(e->value); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 162 | device->pending_events |= EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 163 | break; |
| 164 | case REL_Y: |
Jonas Ådahl | c0ca399 | 2012-05-10 16:46:48 -0400 | [diff] [blame] | 165 | device->rel.dy += wl_fixed_from_int(e->value); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 166 | device->pending_events |= EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 167 | break; |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 168 | case REL_WHEEL: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 169 | notify_axis(&device->master->base.seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 170 | time, |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 171 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 172 | wl_fixed_from_int(e->value)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 173 | break; |
| 174 | case REL_HWHEEL: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 175 | notify_axis(&device->master->base.seat, |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 176 | time, |
Daniel Stone | 878f0b7 | 2012-05-30 16:31:57 +0100 | [diff] [blame] | 177 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 178 | wl_fixed_from_int(e->value)); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 179 | break; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 183 | static inline void |
| 184 | evdev_process_absolute(struct evdev_input_device *device, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 185 | struct input_event *e) |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 186 | { |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 187 | if (device->is_mt) { |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 188 | evdev_process_touch(device, e); |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 189 | } else { |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 190 | evdev_process_absolute_motion(device, e); |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 194 | static int |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 195 | is_motion_event(struct input_event *e) |
| 196 | { |
| 197 | switch (e->type) { |
| 198 | case EV_REL: |
| 199 | switch (e->code) { |
| 200 | case REL_X: |
| 201 | case REL_Y: |
| 202 | return 1; |
| 203 | } |
| 204 | case EV_ABS: |
| 205 | switch (e->code) { |
| 206 | case ABS_X: |
| 207 | case ABS_Y: |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 208 | case ABS_MT_POSITION_X: |
| 209 | case ABS_MT_POSITION_Y: |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 210 | return 1; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static void |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 218 | evdev_flush_motion(struct evdev_input_device *device, uint32_t time) |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 219 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 220 | struct weston_seat *master = &device->master->base; |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 221 | |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 222 | if (!device->pending_events) |
Tiago Vignatti | 12c05b7 | 2011-12-08 13:20:46 +0200 | [diff] [blame] | 223 | return; |
| 224 | |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 225 | if (device->pending_events & EVDEV_RELATIVE_MOTION) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 226 | notify_motion(&master->seat, time, |
| 227 | master->seat.pointer->x + device->rel.dx, |
| 228 | master->seat.pointer->y + device->rel.dy); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 229 | device->pending_events &= ~EVDEV_RELATIVE_MOTION; |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 230 | device->rel.dx = 0; |
| 231 | device->rel.dy = 0; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 232 | } |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 233 | if (device->pending_events & EVDEV_ABSOLUTE_MT_DOWN) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 234 | notify_touch(&master->seat, time, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 235 | device->mt.slot, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 236 | wl_fixed_from_int(device->mt.x[device->mt.slot]), |
| 237 | wl_fixed_from_int(device->mt.y[device->mt.slot]), |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 238 | WL_TOUCH_DOWN); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 239 | device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN; |
| 240 | device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 241 | } |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 242 | if (device->pending_events & EVDEV_ABSOLUTE_MT_MOTION) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 243 | notify_touch(&master->seat, time, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 244 | device->mt.slot, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 245 | wl_fixed_from_int(device->mt.x[device->mt.slot]), |
| 246 | wl_fixed_from_int(device->mt.y[device->mt.slot]), |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 247 | WL_TOUCH_MOTION); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 248 | device->pending_events &= ~EVDEV_ABSOLUTE_MT_DOWN; |
| 249 | device->pending_events &= ~EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 250 | } |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 251 | if (device->pending_events & EVDEV_ABSOLUTE_MT_UP) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 252 | notify_touch(&master->seat, time, device->mt.slot, 0, 0, |
| 253 | WL_TOUCH_UP); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 254 | device->pending_events &= ~EVDEV_ABSOLUTE_MT_UP; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 255 | } |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 256 | if (device->pending_events & EVDEV_ABSOLUTE_MOTION) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 257 | notify_motion(&master->seat, time, |
Kristian Høgsberg | e11bbe4 | 2012-05-09 12:19:04 -0400 | [diff] [blame] | 258 | wl_fixed_from_int(device->abs.x), |
| 259 | wl_fixed_from_int(device->abs.y)); |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 260 | device->pending_events &= ~EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 261 | } |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 264 | static void |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 265 | fallback_process(struct evdev_dispatch *dispatch, |
| 266 | struct evdev_input_device *device, |
| 267 | struct input_event *event, |
| 268 | uint32_t time) |
| 269 | { |
| 270 | switch (event->type) { |
| 271 | case EV_REL: |
| 272 | evdev_process_relative(device, event, time); |
| 273 | break; |
| 274 | case EV_ABS: |
| 275 | evdev_process_absolute(device, event); |
| 276 | break; |
| 277 | case EV_KEY: |
| 278 | evdev_process_key(device, event, time); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | fallback_destroy(struct evdev_dispatch *dispatch) |
| 285 | { |
| 286 | free(dispatch); |
| 287 | } |
| 288 | |
| 289 | struct evdev_dispatch_interface fallback_interface = { |
| 290 | fallback_process, |
| 291 | fallback_destroy |
| 292 | }; |
| 293 | |
| 294 | static struct evdev_dispatch * |
| 295 | fallback_dispatch_create(void) |
| 296 | { |
| 297 | struct evdev_dispatch *dispatch = malloc(sizeof *dispatch); |
| 298 | if (dispatch == NULL) |
| 299 | return NULL; |
| 300 | |
| 301 | dispatch->interface = &fallback_interface; |
| 302 | |
| 303 | return dispatch; |
| 304 | } |
| 305 | |
| 306 | static void |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 307 | evdev_process_events(struct evdev_input_device *device, |
| 308 | struct input_event *ev, int count) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 309 | { |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 310 | struct evdev_dispatch *dispatch = device->dispatch; |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 311 | struct input_event *e, *end; |
Kristian Høgsberg | 865f9b8 | 2011-12-02 06:39:02 -0500 | [diff] [blame] | 312 | uint32_t time = 0; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 313 | |
Daniel Stone | 1d63777 | 2012-05-30 16:31:47 +0100 | [diff] [blame] | 314 | device->pending_events = 0; |
Tiago Vignatti | a12d611 | 2012-01-20 18:47:46 +0200 | [diff] [blame] | 315 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 316 | e = ev; |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 317 | end = e + count; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 318 | for (e = ev; e < end; e++) { |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 319 | time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000; |
| 320 | |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 321 | /* we try to minimize the amount of notifications to be |
| 322 | * forwarded to the compositor, so we accumulate motion |
| 323 | * events and send as a bunch */ |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 324 | if (!is_motion_event(e)) |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 325 | evdev_flush_motion(device, time); |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 326 | |
| 327 | dispatch->interface->process(dispatch, device, e, time); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 330 | evdev_flush_motion(device, time); |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int |
| 334 | evdev_input_device_data(int fd, uint32_t mask, void *data) |
| 335 | { |
| 336 | struct weston_compositor *ec; |
| 337 | struct evdev_input_device *device = data; |
| 338 | struct input_event ev[32]; |
| 339 | int len; |
| 340 | |
| 341 | ec = device->master->base.compositor; |
| 342 | if (!ec->focus) |
| 343 | return 1; |
| 344 | |
| 345 | /* If the compositor is repainting, this function is called only once |
| 346 | * per frame and we have to process all the events available on the |
| 347 | * fd, otherwise there will be input lag. */ |
| 348 | do { |
| 349 | if (device->mtdev) |
| 350 | len = mtdev_get(device->mtdev, fd, ev, |
| 351 | ARRAY_LENGTH(ev)) * |
| 352 | sizeof (struct input_event); |
| 353 | else |
| 354 | len = read(fd, &ev, sizeof ev); |
| 355 | |
| 356 | if (len < 0 || len % sizeof ev[0] != 0) { |
| 357 | /* FIXME: call device_removed when errno is ENODEV. */ |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | evdev_process_events(device, ev, len / sizeof ev[0]); |
| 362 | |
| 363 | } while (len > 0); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 364 | |
| 365 | return 1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 368 | static int |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 369 | evdev_configure_device(struct evdev_input_device *device) |
| 370 | { |
| 371 | struct input_absinfo absinfo; |
| 372 | unsigned long ev_bits[NBITS(EV_MAX)]; |
| 373 | unsigned long abs_bits[NBITS(ABS_MAX)]; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 374 | unsigned long rel_bits[NBITS(REL_MAX)]; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 375 | unsigned long key_bits[NBITS(KEY_MAX)]; |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 376 | int has_key, has_abs; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 377 | unsigned int i; |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 378 | |
| 379 | has_key = 0; |
| 380 | has_abs = 0; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 381 | device->caps = 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 382 | |
| 383 | ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); |
| 384 | if (TEST_BIT(ev_bits, EV_ABS)) { |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 385 | has_abs = 1; |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 386 | |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 387 | ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), |
| 388 | abs_bits); |
| 389 | if (TEST_BIT(abs_bits, ABS_X)) { |
| 390 | ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 391 | device->abs.min_x = absinfo.minimum; |
| 392 | device->abs.max_x = absinfo.maximum; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 393 | device->caps |= EVDEV_MOTION_ABS; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 394 | } |
| 395 | if (TEST_BIT(abs_bits, ABS_Y)) { |
| 396 | ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 397 | device->abs.min_y = absinfo.minimum; |
| 398 | device->abs.max_y = absinfo.maximum; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 399 | device->caps |= EVDEV_MOTION_ABS; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 400 | } |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 401 | if (TEST_BIT(abs_bits, ABS_MT_SLOT)) { |
| 402 | device->is_mt = 1; |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 403 | device->mt.slot = 0; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 404 | device->caps |= EVDEV_TOUCH; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 405 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 406 | } |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 407 | if (TEST_BIT(ev_bits, EV_REL)) { |
| 408 | ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)), |
| 409 | rel_bits); |
| 410 | if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y)) |
| 411 | device->caps |= EVDEV_MOTION_REL; |
| 412 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 413 | if (TEST_BIT(ev_bits, EV_KEY)) { |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 414 | has_key = 1; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 415 | ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), |
| 416 | key_bits); |
| 417 | if (TEST_BIT(key_bits, BTN_TOOL_FINGER) && |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 418 | !TEST_BIT(key_bits, BTN_TOOL_PEN) && |
| 419 | has_abs) |
| 420 | device->dispatch = evdev_touchpad_create(device); |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 421 | for (i = KEY_ESC; i < KEY_MAX; i++) { |
| 422 | if (i >= BTN_MISC && i < KEY_OK) |
| 423 | continue; |
| 424 | if (TEST_BIT(key_bits, i)) { |
| 425 | device->caps |= EVDEV_KEYBOARD; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | for (i = BTN_MISC; i < KEY_OK; i++) { |
| 430 | if (TEST_BIT(key_bits, i)) { |
| 431 | device->caps |= EVDEV_BUTTON; |
| 432 | break; |
| 433 | } |
| 434 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 435 | } |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 436 | |
| 437 | /* This rule tries to catch accelerometer devices and opt out. We may |
| 438 | * want to adjust the protocol later adding a proper event for dealing |
| 439 | * with accelerometers and implement here accordingly */ |
| 440 | if (has_abs && !has_key) |
| 441 | return -1; |
| 442 | |
Daniel Stone | 74419a2 | 2012-05-30 16:32:02 +0100 | [diff] [blame^] | 443 | if ((device->caps & |
| 444 | (EVDEV_MOTION_ABS | EVDEV_MOTION_REL | EVDEV_BUTTON))) |
| 445 | weston_seat_init_pointer(&device->master->base); |
| 446 | if ((device->caps & EVDEV_KEYBOARD)) |
| 447 | weston_seat_init_keyboard(&device->master->base); |
| 448 | if ((device->caps & EVDEV_TOUCH)) |
| 449 | weston_seat_init_touch(&device->master->base); |
| 450 | |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 451 | return 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 452 | } |
| 453 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 454 | static struct evdev_input_device * |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 455 | evdev_input_device_create(struct evdev_seat *master, |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 456 | struct wl_display *display, const char *path) |
| 457 | { |
| 458 | struct evdev_input_device *device; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 459 | struct weston_compositor *ec; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 460 | |
| 461 | device = malloc(sizeof *device); |
| 462 | if (device == NULL) |
| 463 | return NULL; |
| 464 | |
Kristian Høgsberg | a887312 | 2011-11-23 10:39:34 -0500 | [diff] [blame] | 465 | ec = master->base.compositor; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 466 | device->output = |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 467 | container_of(ec->output_list.next, struct weston_output, link); |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 468 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 469 | device->master = master; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 470 | device->is_mt = 0; |
Tiago Vignatti | 23fdeed | 2012-03-16 17:33:03 -0300 | [diff] [blame] | 471 | device->mtdev = NULL; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 472 | device->devnode = strdup(path); |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 473 | device->mt.slot = -1; |
| 474 | device->rel.dx = 0; |
| 475 | device->rel.dy = 0; |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 476 | device->dispatch = NULL; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 477 | |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 478 | /* Use non-blocking mode so that we can loop on read on |
| 479 | * evdev_input_device_data() until all events on the fd are |
| 480 | * read. mtdev_get() also expects this. */ |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 481 | device->fd = weston_launcher_open(ec, path, O_RDONLY | O_NONBLOCK); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 482 | if (device->fd < 0) |
| 483 | goto err0; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 484 | |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 485 | if (evdev_configure_device(device) == -1) |
| 486 | goto err1; |
Kristian Høgsberg | 96c8be9 | 2011-01-14 14:49:46 -0500 | [diff] [blame] | 487 | |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 488 | /* If the dispatch was not set up use the fallback. */ |
| 489 | if (device->dispatch == NULL) |
| 490 | device->dispatch = fallback_dispatch_create(); |
| 491 | if (device->dispatch == NULL) |
| 492 | goto err1; |
| 493 | |
| 494 | |
Tiago Vignatti | 23fdeed | 2012-03-16 17:33:03 -0300 | [diff] [blame] | 495 | if (device->is_mt) { |
| 496 | device->mtdev = mtdev_new_open(device->fd); |
| 497 | if (!device->mtdev) |
| 498 | fprintf(stderr, "mtdev failed to open for %s\n", path); |
| 499 | } |
| 500 | |
Kristian Høgsberg | 7ea1086 | 2012-03-05 17:49:30 -0500 | [diff] [blame] | 501 | device->source = wl_event_loop_add_fd(ec->input_loop, device->fd, |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 502 | WL_EVENT_READABLE, |
| 503 | evdev_input_device_data, device); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 504 | if (device->source == NULL) |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 505 | goto err2; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 506 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 507 | wl_list_insert(master->devices_list.prev, &device->link); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 508 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 509 | return device; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 510 | |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 511 | err2: |
| 512 | device->dispatch->interface->destroy(device->dispatch); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 513 | err1: |
| 514 | close(device->fd); |
| 515 | err0: |
| 516 | free(device->devnode); |
| 517 | free(device); |
| 518 | return NULL; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 521 | static const char default_seat[] = "seat0"; |
| 522 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 523 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 524 | device_added(struct udev_device *udev_device, struct evdev_seat *master) |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 525 | { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 526 | struct weston_compositor *c; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 527 | const char *devnode; |
| 528 | const char *device_seat; |
| 529 | |
| 530 | device_seat = udev_device_get_property_value(udev_device, "ID_SEAT"); |
| 531 | if (!device_seat) |
| 532 | device_seat = default_seat; |
| 533 | |
| 534 | if (strcmp(device_seat, master->seat_id)) |
| 535 | return; |
| 536 | |
Kristian Høgsberg | a887312 | 2011-11-23 10:39:34 -0500 | [diff] [blame] | 537 | c = master->base.compositor; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 538 | devnode = udev_device_get_devnode(udev_device); |
Kristian Høgsberg | 23a47a8 | 2012-01-16 10:54:07 -0500 | [diff] [blame] | 539 | evdev_input_device_create(master, c->wl_display, devnode); |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static void |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 543 | device_removed(struct evdev_input_device *device) |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 544 | { |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 545 | struct evdev_dispatch *dispatch; |
| 546 | |
| 547 | dispatch = device->dispatch; |
| 548 | if (dispatch) |
| 549 | dispatch->interface->destroy(dispatch); |
| 550 | |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 551 | wl_event_source_remove(device->source); |
| 552 | wl_list_remove(&device->link); |
| 553 | if (device->mtdev) |
| 554 | mtdev_close_delete(device->mtdev); |
| 555 | close(device->fd); |
| 556 | free(device->devnode); |
| 557 | free(device); |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 560 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 561 | evdev_notify_keyboard_focus(struct evdev_seat *seat) |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 562 | { |
| 563 | struct evdev_input_device *device; |
| 564 | struct wl_array keys; |
| 565 | unsigned int i, set; |
| 566 | char evdev_keys[(KEY_CNT + 7) / 8], all_keys[(KEY_CNT + 7) / 8]; |
| 567 | uint32_t *k; |
| 568 | int ret; |
| 569 | |
| 570 | memset(all_keys, 0, sizeof all_keys); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 571 | wl_list_for_each(device, &seat->devices_list, link) { |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 572 | memset(evdev_keys, 0, sizeof evdev_keys); |
| 573 | ret = ioctl(device->fd, |
| 574 | EVIOCGKEY(sizeof evdev_keys), evdev_keys); |
| 575 | if (ret < 0) { |
| 576 | fprintf(stderr, "failed to get keys for device %s\n", |
| 577 | device->devnode); |
| 578 | continue; |
| 579 | } |
| 580 | for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++) |
| 581 | all_keys[i] |= evdev_keys[i]; |
| 582 | } |
| 583 | |
| 584 | wl_array_init(&keys); |
| 585 | for (i = 0; i < KEY_CNT; i++) { |
| 586 | set = all_keys[i >> 3] & (1 << (i & 7)); |
| 587 | if (set) { |
| 588 | k = wl_array_add(&keys, sizeof *k); |
| 589 | *k = i; |
| 590 | } |
| 591 | } |
| 592 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 593 | notify_keyboard_focus(&seat->base.seat, &keys); |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 594 | |
| 595 | wl_array_release(&keys); |
| 596 | } |
| 597 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 598 | void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 599 | evdev_add_devices(struct udev *udev, struct weston_seat *seat_base) |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 600 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 601 | struct evdev_seat *seat = (struct evdev_seat *) seat_base; |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 602 | struct udev_enumerate *e; |
| 603 | struct udev_list_entry *entry; |
| 604 | struct udev_device *device; |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 605 | const char *path, *sysname; |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 606 | |
| 607 | e = udev_enumerate_new(udev); |
| 608 | udev_enumerate_add_match_subsystem(e, "input"); |
| 609 | udev_enumerate_scan_devices(e); |
| 610 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 611 | path = udev_list_entry_get_name(entry); |
| 612 | device = udev_device_new_from_syspath(udev, path); |
| 613 | |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 614 | sysname = udev_device_get_sysname(device); |
| 615 | if (strncmp("event", sysname, 5) != 0) { |
| 616 | udev_device_unref(device); |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 617 | continue; |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 618 | } |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 619 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 620 | device_added(device, seat); |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 621 | |
| 622 | udev_device_unref(device); |
| 623 | } |
| 624 | udev_enumerate_unref(e); |
Pekka Paalanen | b07876d | 2012-01-05 16:41:21 +0200 | [diff] [blame] | 625 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 626 | evdev_notify_keyboard_focus(seat); |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 627 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 628 | if (wl_list_empty(&seat->devices_list)) { |
Pekka Paalanen | b07876d | 2012-01-05 16:41:21 +0200 | [diff] [blame] | 629 | fprintf(stderr, |
| 630 | "warning: no input devices on entering Weston. " |
| 631 | "Possible causes:\n" |
Olivier Le Thanh Duong | 643eac5 | 2012-01-14 15:57:34 +0100 | [diff] [blame] | 632 | "\t- no permissions to read /dev/input/event*\n" |
Pekka Paalanen | b07876d | 2012-01-05 16:41:21 +0200 | [diff] [blame] | 633 | "\t- seats misconfigured " |
| 634 | "(Weston backend option 'seat', " |
| 635 | "udev device property ID_SEAT)\n"); |
| 636 | } |
Tiago Vignatti | 0db1d5f | 2011-12-18 16:47:15 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 639 | static int |
| 640 | evdev_udev_handler(int fd, uint32_t mask, void *data) |
| 641 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 642 | struct evdev_seat *master = data; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 643 | struct udev_device *udev_device; |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 644 | struct evdev_input_device *device, *next; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 645 | const char *action; |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 646 | const char *devnode; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 647 | |
| 648 | udev_device = udev_monitor_receive_device(master->udev_monitor); |
| 649 | if (!udev_device) |
| 650 | return 1; |
| 651 | |
| 652 | action = udev_device_get_action(udev_device); |
| 653 | if (action) { |
| 654 | if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0) |
| 655 | return 0; |
| 656 | |
| 657 | if (!strcmp(action, "add")) { |
| 658 | device_added(udev_device, master); |
| 659 | } |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 660 | else if (!strcmp(action, "remove")) { |
| 661 | devnode = udev_device_get_devnode(udev_device); |
| 662 | wl_list_for_each_safe(device, next, |
| 663 | &master->devices_list, link) |
| 664 | if (!strcmp(device->devnode, devnode)) { |
| 665 | device_removed(device); |
| 666 | break; |
| 667 | } |
| 668 | } |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 669 | } |
| 670 | udev_device_unref(udev_device); |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 675 | int |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 676 | evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base) |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 677 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 678 | struct evdev_seat *master = (struct evdev_seat *) seat_base; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 679 | struct wl_event_loop *loop; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 680 | struct weston_compositor *c = master->base.compositor; |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 681 | int fd; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 682 | |
| 683 | master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); |
Pekka Paalanen | b07876d | 2012-01-05 16:41:21 +0200 | [diff] [blame] | 684 | if (!master->udev_monitor) { |
| 685 | fprintf(stderr, "udev: failed to create the udev monitor\n"); |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 686 | return 0; |
Pekka Paalanen | b07876d | 2012-01-05 16:41:21 +0200 | [diff] [blame] | 687 | } |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 688 | |
| 689 | udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor, |
| 690 | "input", NULL); |
| 691 | |
| 692 | if (udev_monitor_enable_receiving(master->udev_monitor)) { |
| 693 | fprintf(stderr, "udev: failed to bind the udev monitor\n"); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 694 | udev_monitor_unref(master->udev_monitor); |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | loop = wl_display_get_event_loop(c->wl_display); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 699 | fd = udev_monitor_get_fd(master->udev_monitor); |
| 700 | master->udev_monitor_source = |
| 701 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 702 | evdev_udev_handler, master); |
| 703 | if (!master->udev_monitor_source) { |
| 704 | udev_monitor_unref(master->udev_monitor); |
| 705 | return 0; |
| 706 | } |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 707 | |
| 708 | return 1; |
| 709 | } |
| 710 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 711 | void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 712 | evdev_disable_udev_monitor(struct weston_seat *seat_base) |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 713 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 714 | struct evdev_seat *seat = (struct evdev_seat *) seat_base; |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 715 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 716 | if (!seat->udev_monitor) |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 717 | return; |
| 718 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 719 | udev_monitor_unref(seat->udev_monitor); |
| 720 | seat->udev_monitor = NULL; |
| 721 | wl_event_source_remove(seat->udev_monitor_source); |
| 722 | seat->udev_monitor_source = NULL; |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 726 | evdev_input_create(struct weston_compositor *c, struct udev *udev, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 727 | const char *seat_id) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 728 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 729 | struct evdev_seat *seat; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 730 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 731 | seat = malloc(sizeof *seat); |
| 732 | if (seat == NULL) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 733 | return; |
| 734 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 735 | memset(seat, 0, sizeof *seat); |
| 736 | weston_seat_init(&seat->base, c); |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 737 | seat->base.led_update = evdev_led_update; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 738 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 739 | wl_list_init(&seat->devices_list); |
| 740 | seat->seat_id = strdup(seat_id); |
| 741 | if (!evdev_enable_udev_monitor(udev, &seat->base)) { |
| 742 | free(seat->seat_id); |
| 743 | free(seat); |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 747 | evdev_add_devices(udev, &seat->base); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 748 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 749 | c->seat = &seat->base; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 750 | } |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 751 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 752 | void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 753 | evdev_remove_devices(struct weston_seat *seat_base) |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 754 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 755 | struct evdev_seat *seat = (struct evdev_seat *) seat_base; |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 756 | struct evdev_input_device *device, *next; |
| 757 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 758 | wl_list_for_each_safe(device, next, &seat->devices_list, link) |
Tiago Vignatti | db4ecc6 | 2012-03-27 21:26:01 +0300 | [diff] [blame] | 759 | device_removed(device); |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 760 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 761 | notify_keyboard_focus(&seat->base.seat, NULL); |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 765 | evdev_input_destroy(struct weston_seat *seat_base) |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 766 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 767 | struct evdev_seat *seat = (struct evdev_seat *) seat_base; |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 768 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 769 | evdev_remove_devices(seat_base); |
| 770 | evdev_disable_udev_monitor(&seat->base); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 771 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 772 | wl_list_remove(&seat->base.link); |
| 773 | free(seat->seat_id); |
| 774 | free(seat); |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 775 | } |