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