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