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