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 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
David Herrmann | e05a0cd | 2013-10-15 14:29:56 +0200 | [diff] [blame] | 25 | #include <errno.h> |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <linux/input.h> |
| 29 | #include <unistd.h> |
| 30 | #include <fcntl.h> |
Tiago Vignatti | 23fdeed | 2012-03-16 17:33:03 -0300 | [diff] [blame] | 31 | #include <mtdev.h> |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 32 | #include <assert.h> |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 33 | |
| 34 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 35 | #include "evdev.h" |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 36 | |
Jonas Ådahl | b984e40 | 2012-10-03 22:56:58 +0200 | [diff] [blame] | 37 | #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10) |
| 38 | |
Pekka Paalanen | 88594b6 | 2012-08-03 14:39:05 +0300 | [diff] [blame] | 39 | void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 40 | evdev_led_update(struct evdev_device *device, enum weston_led leds) |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 41 | { |
| 42 | static const struct { |
| 43 | enum weston_led weston; |
| 44 | int evdev; |
| 45 | } map[] = { |
| 46 | { LED_NUM_LOCK, LED_NUML }, |
| 47 | { LED_CAPS_LOCK, LED_CAPSL }, |
| 48 | { LED_SCROLL_LOCK, LED_SCROLLL }, |
| 49 | }; |
Rolf Morel | 14c9892 | 2013-08-09 16:32:17 +0200 | [diff] [blame] | 50 | struct input_event ev[ARRAY_LENGTH(map) + 1]; |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 51 | unsigned int i; |
| 52 | |
Pekka Paalanen | b9d38f4 | 2012-08-06 14:57:07 +0300 | [diff] [blame] | 53 | if (!device->caps & EVDEV_KEYBOARD) |
| 54 | return; |
| 55 | |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 56 | memset(ev, 0, sizeof(ev)); |
| 57 | for (i = 0; i < ARRAY_LENGTH(map); i++) { |
| 58 | ev[i].type = EV_LED; |
| 59 | ev[i].code = map[i].evdev; |
| 60 | ev[i].value = !!(leds & map[i].weston); |
| 61 | } |
Rolf Morel | 14c9892 | 2013-08-09 16:32:17 +0200 | [diff] [blame] | 62 | ev[i].type = EV_SYN; |
| 63 | ev[i].code = SYN_REPORT; |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 64 | |
Pekka Paalanen | b9d38f4 | 2012-08-06 14:57:07 +0300 | [diff] [blame] | 65 | i = write(device->fd, ev, sizeof ev); |
| 66 | (void)i; /* no, we really don't care about the return value */ |
Daniel Stone | 7bbd5b3 | 2012-05-30 16:31:49 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 69 | static void |
| 70 | transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y) |
| 71 | { |
| 72 | if (!device->abs.apply_calibration) { |
| 73 | *x = device->abs.x; |
| 74 | *y = device->abs.y; |
| 75 | return; |
| 76 | } else { |
| 77 | *x = device->abs.x * device->abs.calibration[0] + |
| 78 | device->abs.y * device->abs.calibration[1] + |
| 79 | device->abs.calibration[2]; |
| 80 | |
| 81 | *y = device->abs.x * device->abs.calibration[3] + |
| 82 | device->abs.y * device->abs.calibration[4] + |
| 83 | device->abs.calibration[5]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | evdev_flush_pending_event(struct evdev_device *device, uint32_t time) |
| 89 | { |
| 90 | struct weston_seat *master = device->seat; |
| 91 | wl_fixed_t x, y; |
| 92 | int32_t cx, cy; |
| 93 | int slot; |
| 94 | |
| 95 | slot = device->mt.slot; |
| 96 | |
| 97 | switch (device->pending_event) { |
| 98 | case EVDEV_NONE: |
| 99 | return; |
| 100 | case EVDEV_RELATIVE_MOTION: |
| 101 | notify_motion(master, time, device->rel.dx, device->rel.dy); |
| 102 | device->rel.dx = 0; |
| 103 | device->rel.dy = 0; |
| 104 | goto handled; |
| 105 | case EVDEV_ABSOLUTE_MT_DOWN: |
| 106 | weston_output_transform_coordinate(device->output, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame] | 107 | wl_fixed_from_int(device->mt.slots[slot].x), |
| 108 | wl_fixed_from_int(device->mt.slots[slot].y), |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 109 | &x, &y); |
| 110 | notify_touch(master, time, |
| 111 | slot, x, y, WL_TOUCH_DOWN); |
| 112 | goto handled; |
| 113 | case EVDEV_ABSOLUTE_MT_MOTION: |
| 114 | weston_output_transform_coordinate(device->output, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame] | 115 | wl_fixed_from_int(device->mt.slots[slot].x), |
| 116 | wl_fixed_from_int(device->mt.slots[slot].y), |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 117 | &x, &y); |
| 118 | notify_touch(master, time, |
| 119 | slot, x, y, WL_TOUCH_MOTION); |
| 120 | goto handled; |
| 121 | case EVDEV_ABSOLUTE_MT_UP: |
| 122 | notify_touch(master, time, slot, 0, 0, |
| 123 | WL_TOUCH_UP); |
| 124 | goto handled; |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 125 | case EVDEV_ABSOLUTE_TOUCH_DOWN: |
| 126 | transform_absolute(device, &cx, &cy); |
| 127 | weston_output_transform_coordinate(device->output, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame] | 128 | wl_fixed_from_int(cx), |
| 129 | wl_fixed_from_int(cy), |
| 130 | &x, &y); |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 131 | notify_touch(master, time, 0, x, y, WL_TOUCH_DOWN); |
| 132 | goto handled; |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 133 | case EVDEV_ABSOLUTE_MOTION: |
| 134 | transform_absolute(device, &cx, &cy); |
| 135 | weston_output_transform_coordinate(device->output, |
Jason Ekstrand | b6a3cc7 | 2013-10-27 22:25:00 -0500 | [diff] [blame] | 136 | wl_fixed_from_int(cx), |
| 137 | wl_fixed_from_int(cy), |
| 138 | &x, &y); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 139 | |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 140 | if (device->caps & EVDEV_TOUCH) |
| 141 | notify_touch(master, time, 0, x, y, WL_TOUCH_MOTION); |
| 142 | else |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 143 | notify_motion_absolute(master, time, x, y); |
| 144 | goto handled; |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 145 | case EVDEV_ABSOLUTE_TOUCH_UP: |
| 146 | notify_touch(master, time, 0, 0, 0, WL_TOUCH_UP); |
| 147 | goto handled; |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | assert(0 && "Unknown pending event type"); |
| 151 | |
| 152 | handled: |
| 153 | device->pending_event = EVDEV_NONE; |
| 154 | } |
| 155 | |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 156 | static void |
| 157 | evdev_process_touch_button(struct evdev_device *device, int time, int value) |
| 158 | { |
| 159 | if (device->pending_event != EVDEV_NONE && |
| 160 | device->pending_event != EVDEV_ABSOLUTE_MOTION) |
| 161 | evdev_flush_pending_event(device, time); |
| 162 | |
| 163 | device->pending_event = (value ? |
| 164 | EVDEV_ABSOLUTE_TOUCH_DOWN : |
| 165 | EVDEV_ABSOLUTE_TOUCH_UP); |
| 166 | } |
| 167 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 168 | static inline void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 169 | evdev_process_key(struct evdev_device *device, struct input_event *e, int time) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 170 | { |
Peter Hutterer | 89af60e | 2013-08-07 11:04:42 +1000 | [diff] [blame] | 171 | /* ignore kernel key repeat */ |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 172 | if (e->value == 2) |
| 173 | return; |
| 174 | |
Neil Roberts | be336c8 | 2013-09-24 20:05:07 +0100 | [diff] [blame] | 175 | if (e->code == BTN_TOUCH) { |
| 176 | if (!device->is_mt) |
| 177 | evdev_process_touch_button(device, time, e->value); |
| 178 | return; |
| 179 | } |
| 180 | |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 181 | evdev_flush_pending_event(device, time); |
| 182 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 183 | switch (e->code) { |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 184 | case BTN_LEFT: |
| 185 | case BTN_RIGHT: |
| 186 | case BTN_MIDDLE: |
| 187 | case BTN_SIDE: |
| 188 | case BTN_EXTRA: |
| 189 | case BTN_FORWARD: |
| 190 | case BTN_BACK: |
| 191 | case BTN_TASK: |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 192 | notify_button(device->seat, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 193 | time, e->code, |
| 194 | e->value ? WL_POINTER_BUTTON_STATE_PRESSED : |
| 195 | WL_POINTER_BUTTON_STATE_RELEASED); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | default: |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 199 | notify_key(device->seat, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 200 | time, e->code, |
| 201 | e->value ? WL_KEYBOARD_KEY_STATE_PRESSED : |
Daniel Stone | 1b4e11f | 2012-06-22 13:21:37 +0100 | [diff] [blame] | 202 | WL_KEYBOARD_KEY_STATE_RELEASED, |
| 203 | STATE_UPDATE_AUTOMATIC); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 208 | static void |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 209 | evdev_process_touch(struct evdev_device *device, |
| 210 | struct input_event *e, |
| 211 | uint32_t time) |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 212 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 213 | const int screen_width = device->output->current_mode->width; |
| 214 | const int screen_height = device->output->current_mode->height; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 215 | |
| 216 | switch (e->code) { |
| 217 | case ABS_MT_SLOT: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 218 | evdev_flush_pending_event(device, time); |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 219 | device->mt.slot = e->value; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 220 | break; |
| 221 | case ABS_MT_TRACKING_ID: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 222 | if (device->pending_event != EVDEV_NONE && |
| 223 | device->pending_event != EVDEV_ABSOLUTE_MT_MOTION) |
| 224 | evdev_flush_pending_event(device, time); |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 225 | if (e->value >= 0) |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 226 | device->pending_event = EVDEV_ABSOLUTE_MT_DOWN; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 227 | else |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 228 | device->pending_event = EVDEV_ABSOLUTE_MT_UP; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 229 | break; |
| 230 | case ABS_MT_POSITION_X: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 231 | device->mt.slots[device->mt.slot].x = |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 232 | (e->value - device->abs.min_x) * screen_width / |
Kristian Høgsberg | 97e806f | 2013-07-22 15:09:30 -0700 | [diff] [blame] | 233 | (device->abs.max_x - device->abs.min_x); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 234 | if (device->pending_event == EVDEV_NONE) |
| 235 | device->pending_event = EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 236 | break; |
| 237 | case ABS_MT_POSITION_Y: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 238 | device->mt.slots[device->mt.slot].y = |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 239 | (e->value - device->abs.min_y) * screen_height / |
Kristian Høgsberg | 97e806f | 2013-07-22 15:09:30 -0700 | [diff] [blame] | 240 | (device->abs.max_y - device->abs.min_y); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 241 | if (device->pending_event == EVDEV_NONE) |
| 242 | device->pending_event = EVDEV_ABSOLUTE_MT_MOTION; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 247 | static inline void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 248 | evdev_process_absolute_motion(struct evdev_device *device, |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 249 | struct input_event *e) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 250 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 251 | const int screen_width = device->output->current_mode->width; |
| 252 | const int screen_height = device->output->current_mode->height; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 253 | |
| 254 | switch (e->code) { |
| 255 | case ABS_X: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 256 | device->abs.x = |
| 257 | (e->value - device->abs.min_x) * screen_width / |
Kristian Høgsberg | cee407e | 2013-07-26 10:40:32 -0700 | [diff] [blame] | 258 | (device->abs.max_x - device->abs.min_x); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 259 | if (device->pending_event == EVDEV_NONE) |
| 260 | device->pending_event = EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 261 | break; |
| 262 | case ABS_Y: |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 263 | device->abs.y = |
| 264 | (e->value - device->abs.min_y) * screen_height / |
Kristian Høgsberg | cee407e | 2013-07-26 10:40:32 -0700 | [diff] [blame] | 265 | (device->abs.max_y - device->abs.min_y); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 266 | if (device->pending_event == EVDEV_NONE) |
| 267 | device->pending_event = EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static inline void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 273 | evdev_process_relative(struct evdev_device *device, |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 274 | struct input_event *e, uint32_t time) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 275 | { |
| 276 | switch (e->code) { |
| 277 | case REL_X: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 278 | if (device->pending_event != EVDEV_RELATIVE_MOTION) |
| 279 | evdev_flush_pending_event(device, time); |
Jonas Ådahl | c0ca399 | 2012-05-10 16:46:48 -0400 | [diff] [blame] | 280 | device->rel.dx += wl_fixed_from_int(e->value); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 281 | device->pending_event = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 282 | break; |
| 283 | case REL_Y: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 284 | if (device->pending_event != EVDEV_RELATIVE_MOTION) |
| 285 | evdev_flush_pending_event(device, time); |
Jonas Ådahl | c0ca399 | 2012-05-10 16:46:48 -0400 | [diff] [blame] | 286 | device->rel.dy += wl_fixed_from_int(e->value); |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 287 | device->pending_event = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 288 | break; |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 289 | case REL_WHEEL: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 290 | evdev_flush_pending_event(device, time); |
Jonas Ådahl | b984e40 | 2012-10-03 22:56:58 +0200 | [diff] [blame] | 291 | switch (e->value) { |
| 292 | case -1: |
| 293 | /* Scroll down */ |
| 294 | case 1: |
| 295 | /* Scroll up */ |
| 296 | notify_axis(device->seat, |
| 297 | time, |
| 298 | WL_POINTER_AXIS_VERTICAL_SCROLL, |
| 299 | -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE); |
| 300 | break; |
| 301 | default: |
| 302 | break; |
| 303 | } |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 304 | break; |
| 305 | case REL_HWHEEL: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 306 | evdev_flush_pending_event(device, time); |
Jonas Ådahl | b984e40 | 2012-10-03 22:56:58 +0200 | [diff] [blame] | 307 | switch (e->value) { |
| 308 | case -1: |
| 309 | /* Scroll left */ |
| 310 | case 1: |
| 311 | /* Scroll right */ |
| 312 | notify_axis(device->seat, |
| 313 | time, |
| 314 | WL_POINTER_AXIS_HORIZONTAL_SCROLL, |
| 315 | e->value * DEFAULT_AXIS_STEP_DISTANCE); |
| 316 | break; |
| 317 | default: |
| 318 | break; |
| 319 | |
| 320 | } |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 324 | static inline void |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 325 | evdev_process_absolute(struct evdev_device *device, |
| 326 | struct input_event *e, |
| 327 | uint32_t time) |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 328 | { |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 329 | if (device->is_mt) { |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 330 | evdev_process_touch(device, e, time); |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 331 | } else { |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 332 | evdev_process_absolute_motion(device, e); |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 336 | static void |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 337 | fallback_process(struct evdev_dispatch *dispatch, |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 338 | struct evdev_device *device, |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 339 | struct input_event *event, |
| 340 | uint32_t time) |
| 341 | { |
| 342 | switch (event->type) { |
| 343 | case EV_REL: |
| 344 | evdev_process_relative(device, event, time); |
| 345 | break; |
| 346 | case EV_ABS: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 347 | evdev_process_absolute(device, event, time); |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 348 | break; |
| 349 | case EV_KEY: |
| 350 | evdev_process_key(device, event, time); |
| 351 | break; |
Satyeshwar Singh | 964a342 | 2013-02-27 15:26:23 -0500 | [diff] [blame] | 352 | case EV_SYN: |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 353 | evdev_flush_pending_event(device, time); |
Satyeshwar Singh | 964a342 | 2013-02-27 15:26:23 -0500 | [diff] [blame] | 354 | break; |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | fallback_destroy(struct evdev_dispatch *dispatch) |
| 360 | { |
| 361 | free(dispatch); |
| 362 | } |
| 363 | |
| 364 | struct evdev_dispatch_interface fallback_interface = { |
| 365 | fallback_process, |
| 366 | fallback_destroy |
| 367 | }; |
| 368 | |
| 369 | static struct evdev_dispatch * |
| 370 | fallback_dispatch_create(void) |
| 371 | { |
| 372 | struct evdev_dispatch *dispatch = malloc(sizeof *dispatch); |
| 373 | if (dispatch == NULL) |
| 374 | return NULL; |
| 375 | |
| 376 | dispatch->interface = &fallback_interface; |
| 377 | |
| 378 | return dispatch; |
| 379 | } |
| 380 | |
| 381 | static void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 382 | evdev_process_events(struct evdev_device *device, |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 383 | struct input_event *ev, int count) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 384 | { |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 385 | struct evdev_dispatch *dispatch = device->dispatch; |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 386 | struct input_event *e, *end; |
Kristian Høgsberg | 865f9b8 | 2011-12-02 06:39:02 -0500 | [diff] [blame] | 387 | uint32_t time = 0; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 388 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 389 | e = ev; |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 390 | end = e + count; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 391 | for (e = ev; e < end; e++) { |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 392 | time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000; |
| 393 | |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 394 | dispatch->interface->process(dispatch, device, e, time); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 395 | } |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static int |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 399 | evdev_device_data(int fd, uint32_t mask, void *data) |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 400 | { |
| 401 | struct weston_compositor *ec; |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 402 | struct evdev_device *device = data; |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 403 | struct input_event ev[32]; |
| 404 | int len; |
| 405 | |
Pekka Paalanen | 5618d6f | 2012-08-03 14:39:00 +0300 | [diff] [blame] | 406 | ec = device->seat->compositor; |
Kristian Høgsberg | 10ddd97 | 2013-10-22 12:40:54 -0700 | [diff] [blame] | 407 | if (!ec->session_active) |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 408 | return 1; |
| 409 | |
| 410 | /* If the compositor is repainting, this function is called only once |
| 411 | * per frame and we have to process all the events available on the |
| 412 | * fd, otherwise there will be input lag. */ |
| 413 | do { |
| 414 | if (device->mtdev) |
| 415 | len = mtdev_get(device->mtdev, fd, ev, |
| 416 | ARRAY_LENGTH(ev)) * |
| 417 | sizeof (struct input_event); |
| 418 | else |
| 419 | len = read(fd, &ev, sizeof ev); |
| 420 | |
| 421 | if (len < 0 || len % sizeof ev[0] != 0) { |
David Herrmann | e05a0cd | 2013-10-15 14:29:56 +0200 | [diff] [blame] | 422 | if (len < 0 && errno != EAGAIN && errno != EINTR) { |
| 423 | weston_log("device %s died\n", |
| 424 | device->devnode); |
| 425 | wl_event_source_remove(device->source); |
| 426 | device->source = NULL; |
| 427 | } |
| 428 | |
Ander Conselvan de Oliveira | 29d9556 | 2012-03-20 19:52:57 -0400 | [diff] [blame] | 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | evdev_process_events(device, ev, len / sizeof ev[0]); |
| 433 | |
| 434 | } while (len > 0); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 435 | |
| 436 | return 1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 437 | } |
| 438 | |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 439 | static int |
Kristian Høgsberg | d24a64e | 2013-12-16 11:01:56 -0800 | [diff] [blame] | 440 | evdev_configure_device(struct evdev_device *device) |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 441 | { |
| 442 | struct input_absinfo absinfo; |
| 443 | unsigned long ev_bits[NBITS(EV_MAX)]; |
| 444 | unsigned long abs_bits[NBITS(ABS_MAX)]; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 445 | unsigned long rel_bits[NBITS(REL_MAX)]; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 446 | unsigned long key_bits[NBITS(KEY_MAX)]; |
Kristian Høgsberg | 77b0d23 | 2013-12-16 14:43:29 -0800 | [diff] [blame^] | 447 | int has_key, has_abs, has_rel, has_mt, has_button; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 448 | unsigned int i; |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 449 | |
| 450 | has_key = 0; |
Kristian Høgsberg | 015e73d | 2013-12-16 11:25:19 -0800 | [diff] [blame] | 451 | has_rel = 0; |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 452 | has_abs = 0; |
Kristian Høgsberg | 8d31a3a | 2013-12-16 13:42:40 -0800 | [diff] [blame] | 453 | has_mt = 0; |
Kristian Høgsberg | 77b0d23 | 2013-12-16 14:43:29 -0800 | [diff] [blame^] | 454 | has_button = 0; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 455 | device->caps = 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 456 | |
| 457 | ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); |
| 458 | if (TEST_BIT(ev_bits, EV_ABS)) { |
| 459 | ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), |
| 460 | abs_bits); |
Kristian Høgsberg | b1c02a8 | 2013-08-13 14:55:39 -0700 | [diff] [blame] | 461 | |
| 462 | if (TEST_BIT(abs_bits, ABS_WHEEL) || |
| 463 | TEST_BIT(abs_bits, ABS_GAS) || |
| 464 | TEST_BIT(abs_bits, ABS_BRAKE) || |
| 465 | TEST_BIT(abs_bits, ABS_HAT0X)) { |
| 466 | weston_log("device %s is a joystick, ignoring\n", |
| 467 | device->devnode); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 471 | if (TEST_BIT(abs_bits, ABS_X)) { |
| 472 | ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 473 | device->abs.min_x = absinfo.minimum; |
| 474 | device->abs.max_x = absinfo.maximum; |
Kristian Høgsberg | 8d31a3a | 2013-12-16 13:42:40 -0800 | [diff] [blame] | 475 | has_abs = 1; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 476 | } |
| 477 | if (TEST_BIT(abs_bits, ABS_Y)) { |
| 478 | ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 479 | device->abs.min_y = absinfo.minimum; |
| 480 | device->abs.max_y = absinfo.maximum; |
Kristian Høgsberg | 8d31a3a | 2013-12-16 13:42:40 -0800 | [diff] [blame] | 481 | has_abs = 1; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 482 | } |
Peter Hutterer | 45d659d | 2013-08-08 12:03:08 +1000 | [diff] [blame] | 483 | /* We only handle the slotted Protocol B in weston. |
| 484 | Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT |
| 485 | require mtdev for conversion. */ |
| 486 | if (TEST_BIT(abs_bits, ABS_MT_POSITION_X) && |
| 487 | TEST_BIT(abs_bits, ABS_MT_POSITION_Y)) { |
Pekka Paalanen | 2fc7cce | 2012-07-31 13:21:07 +0300 | [diff] [blame] | 488 | ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_X), |
| 489 | &absinfo); |
| 490 | device->abs.min_x = absinfo.minimum; |
| 491 | device->abs.max_x = absinfo.maximum; |
| 492 | ioctl(device->fd, EVIOCGABS(ABS_MT_POSITION_Y), |
| 493 | &absinfo); |
| 494 | device->abs.min_y = absinfo.minimum; |
| 495 | device->abs.max_y = absinfo.maximum; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 496 | device->is_mt = 1; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 497 | device->caps |= EVDEV_TOUCH; |
Kristian Høgsberg | 8d31a3a | 2013-12-16 13:42:40 -0800 | [diff] [blame] | 498 | has_mt = 1; |
Peter Hutterer | 0d061e3 | 2013-08-07 11:04:45 +1000 | [diff] [blame] | 499 | |
| 500 | if (!TEST_BIT(abs_bits, ABS_MT_SLOT)) { |
| 501 | device->mtdev = mtdev_new_open(device->fd); |
| 502 | if (!device->mtdev) { |
Kristian Høgsberg | d24a64e | 2013-12-16 11:01:56 -0800 | [diff] [blame] | 503 | device->caps = 0; |
Peter Hutterer | 0d061e3 | 2013-08-07 11:04:45 +1000 | [diff] [blame] | 504 | weston_log("mtdev required but failed to open for %s\n", |
| 505 | device->devnode); |
| 506 | return 0; |
| 507 | } |
Peter Hutterer | df66c5b | 2013-08-07 11:04:46 +1000 | [diff] [blame] | 508 | device->mt.slot = device->mtdev->caps.slot.value; |
| 509 | } else { |
| 510 | ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT), |
| 511 | &absinfo); |
| 512 | device->mt.slot = absinfo.value; |
Peter Hutterer | 0d061e3 | 2013-08-07 11:04:45 +1000 | [diff] [blame] | 513 | } |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 514 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 515 | } |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 516 | if (TEST_BIT(ev_bits, EV_REL)) { |
| 517 | ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof(rel_bits)), |
| 518 | rel_bits); |
| 519 | if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y)) |
Kristian Høgsberg | 015e73d | 2013-12-16 11:25:19 -0800 | [diff] [blame] | 520 | has_rel = 1; |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 521 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 522 | if (TEST_BIT(ev_bits, EV_KEY)) { |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 523 | has_key = 1; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 524 | ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), |
| 525 | key_bits); |
| 526 | if (TEST_BIT(key_bits, BTN_TOOL_FINGER) && |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 527 | !TEST_BIT(key_bits, BTN_TOOL_PEN) && |
Kristian Høgsberg | 8d31a3a | 2013-12-16 13:42:40 -0800 | [diff] [blame] | 528 | (has_abs || has_mt)) { |
Jonas Ådahl | 1df17af | 2012-05-17 12:18:17 +0200 | [diff] [blame] | 529 | device->dispatch = evdev_touchpad_create(device); |
Peter Hutterer | 4477fee | 2013-08-07 11:04:49 +1000 | [diff] [blame] | 530 | weston_log("input device %s, %s is a touchpad\n", |
| 531 | device->devname, device->devnode); |
| 532 | } |
Daniel Stone | 33965c2 | 2012-05-30 16:31:48 +0100 | [diff] [blame] | 533 | for (i = KEY_ESC; i < KEY_MAX; i++) { |
| 534 | if (i >= BTN_MISC && i < KEY_OK) |
| 535 | continue; |
| 536 | if (TEST_BIT(key_bits, i)) { |
| 537 | device->caps |= EVDEV_KEYBOARD; |
| 538 | break; |
| 539 | } |
| 540 | } |
Kristian Høgsberg | 0af26c4 | 2013-07-26 10:43:26 -0700 | [diff] [blame] | 541 | if (TEST_BIT(key_bits, BTN_TOUCH)) { |
| 542 | device->caps |= EVDEV_TOUCH; |
| 543 | } |
Kristian Høgsberg | c133fc4 | 2013-10-14 15:46:13 -0700 | [diff] [blame] | 544 | for (i = BTN_MISC; i < BTN_JOYSTICK; i++) { |
| 545 | if (TEST_BIT(key_bits, i)) { |
Kristian Høgsberg | 77b0d23 | 2013-12-16 14:43:29 -0800 | [diff] [blame^] | 546 | has_button = 1; |
Kristian Høgsberg | c133fc4 | 2013-10-14 15:46:13 -0700 | [diff] [blame] | 547 | device->caps &= ~EVDEV_TOUCH; |
| 548 | break; |
| 549 | } |
| 550 | } |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 551 | } |
Daniel Stone | cfd0e72 | 2012-06-01 12:13:59 +0100 | [diff] [blame] | 552 | if (TEST_BIT(ev_bits, EV_LED)) { |
| 553 | device->caps |= EVDEV_KEYBOARD; |
| 554 | } |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 555 | |
| 556 | /* This rule tries to catch accelerometer devices and opt out. We may |
| 557 | * want to adjust the protocol later adding a proper event for dealing |
| 558 | * with accelerometers and implement here accordingly */ |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 559 | if (has_abs && !has_key && !device->is_mt) { |
| 560 | weston_log("input device %s, %s " |
| 561 | "ignored: unsupported device type\n", |
| 562 | device->devname, device->devnode); |
Kristian Høgsberg | d24a64e | 2013-12-16 11:01:56 -0800 | [diff] [blame] | 563 | device->caps = 0; |
Kristian Høgsberg | 2f07ef6 | 2013-02-16 14:29:24 -0500 | [diff] [blame] | 564 | return 0; |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 565 | } |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 566 | |
Kristian Høgsberg | 77b0d23 | 2013-12-16 14:43:29 -0800 | [diff] [blame^] | 567 | if ((has_abs || has_rel) && has_button) { |
Pekka Paalanen | 5618d6f | 2012-08-03 14:39:00 +0300 | [diff] [blame] | 568 | weston_seat_init_pointer(device->seat); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 569 | device->seat_caps |= EVDEV_SEAT_POINTER; |
Rob Bradford | 80137f3 | 2012-12-03 19:44:13 +0000 | [diff] [blame] | 570 | weston_log("input device %s, %s is a pointer caps =%s%s%s\n", |
| 571 | device->devname, device->devnode, |
Kristian Høgsberg | b7c58de | 2013-12-16 13:55:48 -0800 | [diff] [blame] | 572 | has_abs ? " absolute-motion" : "", |
Kristian Høgsberg | 015e73d | 2013-12-16 11:25:19 -0800 | [diff] [blame] | 573 | has_rel ? " relative-motion": "", |
Kristian Høgsberg | 77b0d23 | 2013-12-16 14:43:29 -0800 | [diff] [blame^] | 574 | has_button ? " button" : ""); |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 575 | } |
| 576 | if ((device->caps & EVDEV_KEYBOARD)) { |
Kristian Høgsberg | 2f07ef6 | 2013-02-16 14:29:24 -0500 | [diff] [blame] | 577 | if (weston_seat_init_keyboard(device->seat, NULL) < 0) |
| 578 | return -1; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 579 | device->seat_caps |= EVDEV_SEAT_KEYBOARD; |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 580 | weston_log("input device %s, %s is a keyboard\n", |
| 581 | device->devname, device->devnode); |
| 582 | } |
| 583 | if ((device->caps & EVDEV_TOUCH)) { |
Pekka Paalanen | 5618d6f | 2012-08-03 14:39:00 +0300 | [diff] [blame] | 584 | weston_seat_init_touch(device->seat); |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 585 | device->seat_caps |= EVDEV_SEAT_TOUCH; |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 586 | weston_log("input device %s, %s is a touch device\n", |
| 587 | device->devname, device->devnode); |
| 588 | } |
Daniel Stone | 74419a2 | 2012-05-30 16:32:02 +0100 | [diff] [blame] | 589 | |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 590 | return 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 591 | } |
| 592 | |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 593 | struct evdev_device * |
| 594 | evdev_device_create(struct weston_seat *seat, const char *path, int device_fd) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 595 | { |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 596 | struct evdev_device *device; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 597 | struct weston_compositor *ec; |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 598 | char devname[256] = "unknown"; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 599 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 600 | device = zalloc(sizeof *device); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 601 | if (device == NULL) |
| 602 | return NULL; |
| 603 | |
Pekka Paalanen | 43f0a1e | 2012-08-03 14:39:03 +0300 | [diff] [blame] | 604 | ec = seat->compositor; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 605 | device->output = |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 606 | container_of(ec->output_list.next, struct weston_output, link); |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 607 | |
Pekka Paalanen | 43f0a1e | 2012-08-03 14:39:03 +0300 | [diff] [blame] | 608 | device->seat = seat; |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 609 | device->seat_caps = 0; |
Tiago Vignatti | 22c6bce | 2011-12-21 19:34:09 +0200 | [diff] [blame] | 610 | device->is_mt = 0; |
Tiago Vignatti | 23fdeed | 2012-03-16 17:33:03 -0300 | [diff] [blame] | 611 | device->mtdev = NULL; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 612 | device->devnode = strdup(path); |
Kristian Høgsberg | 3937354 | 2011-12-21 22:18:36 -0500 | [diff] [blame] | 613 | device->mt.slot = -1; |
| 614 | device->rel.dx = 0; |
| 615 | device->rel.dy = 0; |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 616 | device->dispatch = NULL; |
Pekka Paalanen | 5a6383b | 2012-08-03 14:38:58 +0300 | [diff] [blame] | 617 | device->fd = device_fd; |
Neil Roberts | daf7d47 | 2013-09-24 12:09:03 +0100 | [diff] [blame] | 618 | device->pending_event = EVDEV_NONE; |
Kristian Høgsberg | 4e55d06 | 2013-08-26 14:35:32 -0700 | [diff] [blame] | 619 | wl_list_init(&device->link); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 620 | |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 621 | ioctl(device->fd, EVIOCGNAME(sizeof(devname)), devname); |
Peter Hutterer | 11f5bfb | 2013-08-07 11:04:41 +1000 | [diff] [blame] | 622 | devname[sizeof(devname) - 1] = '\0'; |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 623 | device->devname = strdup(devname); |
| 624 | |
Kristian Høgsberg | d24a64e | 2013-12-16 11:01:56 -0800 | [diff] [blame] | 625 | if (evdev_configure_device(device) == -1) |
| 626 | goto err; |
| 627 | |
| 628 | if (device->seat_caps == 0) { |
Peter Hutterer | 6bb15cd | 2013-08-07 11:04:48 +1000 | [diff] [blame] | 629 | evdev_device_destroy(device); |
Kristian Høgsberg | 2f07ef6 | 2013-02-16 14:29:24 -0500 | [diff] [blame] | 630 | return EVDEV_UNHANDLED_DEVICE; |
| 631 | } |
| 632 | |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 633 | /* If the dispatch was not set up use the fallback. */ |
| 634 | if (device->dispatch == NULL) |
| 635 | device->dispatch = fallback_dispatch_create(); |
| 636 | if (device->dispatch == NULL) |
Peter Hutterer | 6bb15cd | 2013-08-07 11:04:48 +1000 | [diff] [blame] | 637 | goto err; |
Jonas Ådahl | 4136d82 | 2012-05-17 12:18:16 +0200 | [diff] [blame] | 638 | |
Kristian Høgsberg | 7ea1086 | 2012-03-05 17:49:30 -0500 | [diff] [blame] | 639 | 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] | 640 | WL_EVENT_READABLE, |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 641 | evdev_device_data, device); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 642 | if (device->source == NULL) |
Peter Hutterer | 6bb15cd | 2013-08-07 11:04:48 +1000 | [diff] [blame] | 643 | goto err; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 644 | |
| 645 | return device; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 646 | |
Peter Hutterer | 6bb15cd | 2013-08-07 11:04:48 +1000 | [diff] [blame] | 647 | err: |
| 648 | evdev_device_destroy(device); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 649 | return NULL; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 650 | } |
| 651 | |
Pekka Paalanen | 88594b6 | 2012-08-03 14:39:05 +0300 | [diff] [blame] | 652 | void |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 653 | evdev_device_destroy(struct evdev_device *device) |
Pekka Paalanen | a123e5c | 2012-08-03 14:38:57 +0300 | [diff] [blame] | 654 | { |
| 655 | struct evdev_dispatch *dispatch; |
| 656 | |
Jonas Ådahl | d6e1c34 | 2013-10-17 23:04:05 +0200 | [diff] [blame] | 657 | if (device->seat_caps & EVDEV_SEAT_POINTER) |
| 658 | weston_seat_release_pointer(device->seat); |
| 659 | if (device->seat_caps & EVDEV_SEAT_KEYBOARD) |
| 660 | weston_seat_release_keyboard(device->seat); |
| 661 | if (device->seat_caps & EVDEV_SEAT_TOUCH) |
| 662 | weston_seat_release_touch(device->seat); |
| 663 | |
Pekka Paalanen | a123e5c | 2012-08-03 14:38:57 +0300 | [diff] [blame] | 664 | dispatch = device->dispatch; |
| 665 | if (dispatch) |
| 666 | dispatch->interface->destroy(dispatch); |
| 667 | |
Peter Hutterer | 6bb15cd | 2013-08-07 11:04:48 +1000 | [diff] [blame] | 668 | if (device->source) |
| 669 | wl_event_source_remove(device->source); |
Kristian Høgsberg | 4e55d06 | 2013-08-26 14:35:32 -0700 | [diff] [blame] | 670 | wl_list_remove(&device->link); |
Pekka Paalanen | a123e5c | 2012-08-03 14:38:57 +0300 | [diff] [blame] | 671 | if (device->mtdev) |
| 672 | mtdev_close_delete(device->mtdev); |
| 673 | close(device->fd); |
Pekka Paalanen | bf639ab | 2012-08-03 14:39:07 +0300 | [diff] [blame] | 674 | free(device->devname); |
Pekka Paalanen | a123e5c | 2012-08-03 14:38:57 +0300 | [diff] [blame] | 675 | free(device->devnode); |
| 676 | free(device); |
| 677 | } |
| 678 | |
Pekka Paalanen | 88594b6 | 2012-08-03 14:39:05 +0300 | [diff] [blame] | 679 | void |
Pekka Paalanen | 3bfb201 | 2012-08-03 14:39:02 +0300 | [diff] [blame] | 680 | evdev_notify_keyboard_focus(struct weston_seat *seat, |
| 681 | struct wl_list *evdev_devices) |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 682 | { |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 683 | struct evdev_device *device; |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 684 | struct wl_array keys; |
| 685 | unsigned int i, set; |
Pekka Paalanen | d858351 | 2012-08-03 14:39:11 +0300 | [diff] [blame] | 686 | char evdev_keys[(KEY_CNT + 7) / 8]; |
| 687 | char all_keys[(KEY_CNT + 7) / 8]; |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 688 | uint32_t *k; |
| 689 | int ret; |
| 690 | |
Jonas Ådahl | e82f8e4 | 2013-11-12 22:55:05 +0100 | [diff] [blame] | 691 | if (!seat->keyboard_device_count > 0) |
Pekka Paalanen | d858351 | 2012-08-03 14:39:11 +0300 | [diff] [blame] | 692 | return; |
| 693 | |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 694 | memset(all_keys, 0, sizeof all_keys); |
Pekka Paalanen | 3bfb201 | 2012-08-03 14:39:02 +0300 | [diff] [blame] | 695 | wl_list_for_each(device, evdev_devices, link) { |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 696 | memset(evdev_keys, 0, sizeof evdev_keys); |
| 697 | ret = ioctl(device->fd, |
| 698 | EVIOCGKEY(sizeof evdev_keys), evdev_keys); |
| 699 | if (ret < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 700 | weston_log("failed to get keys for device %s\n", |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 701 | device->devnode); |
| 702 | continue; |
| 703 | } |
| 704 | for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++) |
| 705 | all_keys[i] |= evdev_keys[i]; |
| 706 | } |
| 707 | |
| 708 | wl_array_init(&keys); |
| 709 | for (i = 0; i < KEY_CNT; i++) { |
| 710 | set = all_keys[i >> 3] & (1 << (i & 7)); |
| 711 | if (set) { |
| 712 | k = wl_array_add(&keys, sizeof *k); |
| 713 | *k = i; |
| 714 | } |
| 715 | } |
| 716 | |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 717 | notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC); |
Kristian Høgsberg | a00d60f | 2012-04-10 00:03:30 -0400 | [diff] [blame] | 718 | |
| 719 | wl_array_release(&keys); |
| 720 | } |