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