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