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