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