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" |
| 31 | |
| 32 | struct evdev_input { |
| 33 | struct wlsc_input_device base; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 34 | struct wl_list devices_list; |
| 35 | struct udev_monitor *udev_monitor; |
| 36 | char *seat_id; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct evdev_input_device { |
| 40 | struct evdev_input *master; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 41 | struct wl_list link; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 42 | struct wl_event_source *source; |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 43 | struct wlsc_output *output; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 44 | char *devnode; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 45 | int fd; |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 46 | struct { |
| 47 | int min_x, max_x, min_y, max_y; |
| 48 | int old_x, old_y, reset_x, reset_y; |
| 49 | } abs; |
| 50 | int is_touchpad; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 53 | /* event type flags */ |
| 54 | #define EVDEV_ABSOLUTE_MOTION (1 << 0) |
| 55 | #define EVDEV_RELATIVE_MOTION (1 << 1) |
| 56 | |
| 57 | struct evdev_motion_accumulator { |
| 58 | int x, y; |
| 59 | int dx, dy; |
| 60 | int type; /* event type flags */ |
| 61 | }; |
| 62 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 63 | static inline void |
| 64 | evdev_process_key(struct evdev_input_device *device, |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 65 | struct input_event *e, int time) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 66 | { |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 67 | if (e->value == 2) |
| 68 | return; |
| 69 | |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 70 | switch (e->code) { |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 71 | case BTN_TOOL_PEN: |
| 72 | case BTN_TOOL_RUBBER: |
| 73 | case BTN_TOOL_BRUSH: |
| 74 | case BTN_TOOL_PENCIL: |
| 75 | case BTN_TOOL_AIRBRUSH: |
| 76 | case BTN_TOOL_FINGER: |
| 77 | case BTN_TOOL_MOUSE: |
| 78 | case BTN_TOOL_LENS: |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 79 | if (device->is_touchpad) |
| 80 | { |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 81 | device->abs.reset_x = 1; |
| 82 | device->abs.reset_y = 1; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 83 | } |
| 84 | break; |
| 85 | |
Tiago Vignatti | d904359 | 2011-09-01 19:00:05 +0300 | [diff] [blame] | 86 | case BTN_TOUCH: |
| 87 | /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as |
| 88 | * BTN_LEFT */ |
| 89 | e->code = BTN_LEFT; |
| 90 | /* Intentional fallthrough! */ |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 91 | case BTN_LEFT: |
| 92 | case BTN_RIGHT: |
| 93 | case BTN_MIDDLE: |
| 94 | case BTN_SIDE: |
| 95 | case BTN_EXTRA: |
| 96 | case BTN_FORWARD: |
| 97 | case BTN_BACK: |
| 98 | case BTN_TASK: |
| 99 | notify_button(&device->master->base.input_device, |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 100 | time, e->code, e->value); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 101 | break; |
| 102 | |
| 103 | default: |
| 104 | notify_key(&device->master->base.input_device, |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 105 | time, e->code, e->value); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static inline void |
| 111 | evdev_process_absolute_motion(struct evdev_input_device *device, |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 112 | struct input_event *e, struct evdev_motion_accumulator *accum) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 113 | { |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 114 | const int screen_width = device->output->current->width; |
| 115 | const int screen_height = device->output->current->height; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 116 | |
| 117 | switch (e->code) { |
| 118 | case ABS_X: |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 119 | accum->x = (e->value - device->abs.min_x) * screen_width / |
| 120 | (device->abs.max_x - device->abs.min_x) + |
| 121 | device->output->x; |
| 122 | accum->type = EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 123 | break; |
| 124 | case ABS_Y: |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 125 | accum->y = (e->value - device->abs.min_y) * screen_height / |
| 126 | (device->abs.max_y - device->abs.min_y) + |
| 127 | device->output->y; |
| 128 | accum->type = EVDEV_ABSOLUTE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static inline void |
| 134 | evdev_process_absolute_motion_touchpad(struct evdev_input_device *device, |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 135 | struct input_event *e, struct evdev_motion_accumulator *accum) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 136 | { |
| 137 | /* FIXME: Make this configurable somehow. */ |
| 138 | const int touchpad_speed = 700; |
| 139 | |
| 140 | switch (e->code) { |
| 141 | case ABS_X: |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 142 | e->value -= device->abs.min_x; |
| 143 | if (device->abs.reset_x) |
| 144 | device->abs.reset_x = 0; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 145 | else { |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 146 | accum->dx = (e->value - device->abs.old_x) * |
| 147 | touchpad_speed / |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 148 | (device->abs.max_x - device->abs.min_x); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 149 | } |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 150 | device->abs.old_x = e->value; |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 151 | accum->type = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 152 | break; |
| 153 | case ABS_Y: |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 154 | e->value -= device->abs.min_y; |
| 155 | if (device->abs.reset_y) |
| 156 | device->abs.reset_y = 0; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 157 | else { |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 158 | accum->dy = (e->value - device->abs.old_y) * |
| 159 | touchpad_speed / |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 160 | /* maybe use x size here to have the same scale? */ |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 161 | (device->abs.max_y - device->abs.min_y); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 162 | } |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 163 | device->abs.old_y = e->value; |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 164 | accum->type = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static inline void |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 170 | evdev_process_relative_motion(struct input_event *e, |
| 171 | struct evdev_motion_accumulator *accum) |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 172 | { |
| 173 | switch (e->code) { |
| 174 | case REL_X: |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 175 | accum->dx += e->value; |
| 176 | accum->type = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 177 | break; |
| 178 | case REL_Y: |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 179 | accum->dy += e->value; |
| 180 | accum->type = EVDEV_RELATIVE_MOTION; |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 185 | static int |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 186 | is_motion_event(struct input_event *e) |
| 187 | { |
| 188 | switch (e->type) { |
| 189 | case EV_REL: |
| 190 | switch (e->code) { |
| 191 | case REL_X: |
| 192 | case REL_Y: |
| 193 | return 1; |
| 194 | } |
| 195 | case EV_ABS: |
| 196 | switch (e->code) { |
| 197 | case ABS_X: |
| 198 | case ABS_Y: |
| 199 | return 1; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 207 | evdev_flush_motion(struct wl_input_device *device, uint32_t time, |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 208 | struct evdev_motion_accumulator *accum) |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 209 | { |
Kristian Høgsberg | db0fa54 | 2011-11-22 19:24:25 -0500 | [diff] [blame^] | 210 | if (accum->type == EVDEV_RELATIVE_MOTION) |
| 211 | notify_motion(device, time, |
| 212 | device->x + accum->dx, device->y + accum->dy); |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 213 | if (accum->type == EVDEV_ABSOLUTE_MOTION) |
| 214 | notify_motion(device, time, accum->x, accum->y); |
| 215 | |
Kristian Høgsberg | db0fa54 | 2011-11-22 19:24:25 -0500 | [diff] [blame^] | 216 | memset(accum, 0, sizeof *accum); |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static int |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 220 | evdev_input_device_data(int fd, uint32_t mask, void *data) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 221 | { |
| 222 | struct wlsc_compositor *ec; |
| 223 | struct evdev_input_device *device = data; |
| 224 | struct input_event ev[8], *e, *end; |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 225 | int len; |
| 226 | struct evdev_motion_accumulator accumulator; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 227 | uint32_t time; |
| 228 | |
| 229 | ec = (struct wlsc_compositor *) |
| 230 | device->master->base.input_device.compositor; |
| 231 | if (!ec->focus) |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 232 | return 1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 233 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 234 | len = read(fd, &ev, sizeof ev); |
| 235 | if (len < 0 || len % sizeof e[0] != 0) { |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 236 | /* FIXME: call device_removed when errno is ENODEV. */; |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 237 | return 1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Tiago Vignatti | f547bd3 | 2011-11-22 12:05:22 +0200 | [diff] [blame] | 240 | memset(&accumulator, 0, sizeof accumulator); |
| 241 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 242 | e = ev; |
| 243 | end = (void *) ev + len; |
| 244 | for (e = ev; e < end; e++) { |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 245 | time = e->time.tv_sec * 1000 + e->time.tv_usec / 1000; |
| 246 | |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 247 | /* we try to minimize the amount of notifications to be |
| 248 | * forwarded to the compositor, so we accumulate motion |
| 249 | * events and send as a bunch */ |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 250 | if (!is_motion_event(e)) |
Tiago Vignatti | 52e158d | 2011-11-18 14:56:58 +0200 | [diff] [blame] | 251 | evdev_flush_motion(&device->master->base.input_device, |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 252 | time, &accumulator); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 253 | switch (e->type) { |
| 254 | case EV_REL: |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 255 | evdev_process_relative_motion(e, &accumulator); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 256 | break; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 257 | case EV_ABS: |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 258 | if (device->is_touchpad) |
| 259 | evdev_process_absolute_motion_touchpad(device, |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 260 | e, &accumulator); |
Tiago Vignatti | 8be003b | 2011-09-01 19:00:03 +0300 | [diff] [blame] | 261 | else |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 262 | evdev_process_absolute_motion(device, e, |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 263 | &accumulator); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 264 | break; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 265 | case EV_KEY: |
Tiago Vignatti | 8755ff9 | 2011-11-10 14:47:30 +0200 | [diff] [blame] | 266 | evdev_process_key(device, e, time); |
| 267 | break; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Tiago Vignatti | a52b2e4 | 2011-11-21 17:40:30 +0200 | [diff] [blame] | 271 | evdev_flush_motion(&device->master->base.input_device, time, |
Tiago Vignatti | 80885e1 | 2011-11-21 17:59:31 +0200 | [diff] [blame] | 272 | &accumulator); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 273 | |
| 274 | return 1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Matt Peterson | 63900ec | 2011-08-01 13:46:29 -0600 | [diff] [blame] | 277 | |
| 278 | /* copied from udev/extras/input_id/input_id.c */ |
| 279 | /* we must use this kernel-compatible implementation */ |
| 280 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 281 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) |
| 282 | #define OFF(x) ((x)%BITS_PER_LONG) |
| 283 | #define BIT(x) (1UL<<OFF(x)) |
| 284 | #define LONG(x) ((x)/BITS_PER_LONG) |
| 285 | #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1) |
| 286 | /* end copied */ |
Kristian Høgsberg | 96c8be9 | 2011-01-14 14:49:46 -0500 | [diff] [blame] | 287 | |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 288 | static int |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 289 | evdev_configure_device(struct evdev_input_device *device) |
| 290 | { |
| 291 | struct input_absinfo absinfo; |
| 292 | unsigned long ev_bits[NBITS(EV_MAX)]; |
| 293 | unsigned long abs_bits[NBITS(ABS_MAX)]; |
| 294 | unsigned long key_bits[NBITS(KEY_MAX)]; |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 295 | int has_key, has_abs; |
| 296 | |
| 297 | has_key = 0; |
| 298 | has_abs = 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 299 | |
| 300 | ioctl(device->fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); |
| 301 | if (TEST_BIT(ev_bits, EV_ABS)) { |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 302 | has_abs = 1; |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 303 | |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 304 | ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bits)), |
| 305 | abs_bits); |
| 306 | if (TEST_BIT(abs_bits, ABS_X)) { |
| 307 | ioctl(device->fd, EVIOCGABS(ABS_X), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 308 | device->abs.min_x = absinfo.minimum; |
| 309 | device->abs.max_x = absinfo.maximum; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 310 | } |
| 311 | if (TEST_BIT(abs_bits, ABS_Y)) { |
| 312 | ioctl(device->fd, EVIOCGABS(ABS_Y), &absinfo); |
Tiago Vignatti | a157fc1 | 2011-11-21 16:39:55 +0200 | [diff] [blame] | 313 | device->abs.min_y = absinfo.minimum; |
| 314 | device->abs.max_y = absinfo.maximum; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | if (TEST_BIT(ev_bits, EV_KEY)) { |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 318 | has_key = 1; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 319 | ioctl(device->fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), |
| 320 | key_bits); |
| 321 | if (TEST_BIT(key_bits, BTN_TOOL_FINGER) && |
| 322 | !TEST_BIT(key_bits, BTN_TOOL_PEN)) |
| 323 | device->is_touchpad = 1; |
| 324 | } |
Tiago Vignatti | c0827fd | 2011-08-19 17:07:40 +0300 | [diff] [blame] | 325 | |
| 326 | /* This rule tries to catch accelerometer devices and opt out. We may |
| 327 | * want to adjust the protocol later adding a proper event for dealing |
| 328 | * with accelerometers and implement here accordingly */ |
| 329 | if (has_abs && !has_key) |
| 330 | return -1; |
| 331 | |
| 332 | return 0; |
Tiago Vignatti | d9c8250 | 2011-08-19 15:06:20 +0300 | [diff] [blame] | 333 | } |
| 334 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 335 | static struct evdev_input_device * |
| 336 | evdev_input_device_create(struct evdev_input *master, |
| 337 | struct wl_display *display, const char *path) |
| 338 | { |
| 339 | struct evdev_input_device *device; |
| 340 | struct wl_event_loop *loop; |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 341 | struct wlsc_compositor *ec; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 342 | |
| 343 | device = malloc(sizeof *device); |
| 344 | if (device == NULL) |
| 345 | return NULL; |
| 346 | |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 347 | ec = (struct wlsc_compositor *) master->base.input_device.compositor; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 348 | device->output = |
Kristian Høgsberg | e7b5b41 | 2011-09-01 13:25:50 -0400 | [diff] [blame] | 349 | container_of(ec->output_list.next, struct wlsc_output, link); |
| 350 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 351 | device->master = master; |
Matt Peterson | 63900ec | 2011-08-01 13:46:29 -0600 | [diff] [blame] | 352 | device->is_touchpad = 0; |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 353 | device->devnode = strdup(path); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 354 | |
| 355 | device->fd = open(path, O_RDONLY); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 356 | if (device->fd < 0) |
| 357 | goto err0; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 358 | |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 359 | if (evdev_configure_device(device) == -1) |
| 360 | goto err1; |
Kristian Høgsberg | 96c8be9 | 2011-01-14 14:49:46 -0500 | [diff] [blame] | 361 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 362 | loop = wl_display_get_event_loop(display); |
| 363 | device->source = wl_event_loop_add_fd(loop, device->fd, |
| 364 | WL_EVENT_READABLE, |
| 365 | evdev_input_device_data, device); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 366 | if (device->source == NULL) |
| 367 | goto err1; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 368 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 369 | wl_list_insert(master->devices_list.prev, &device->link); |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 370 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 371 | return device; |
Tiago Vignatti | ac9cfd3 | 2011-10-28 13:15:25 -0400 | [diff] [blame] | 372 | |
| 373 | err1: |
| 374 | close(device->fd); |
| 375 | err0: |
| 376 | free(device->devnode); |
| 377 | free(device); |
| 378 | return NULL; |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 381 | static const char default_seat[] = "seat0"; |
| 382 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 383 | static void |
| 384 | device_added(struct udev_device *udev_device, struct evdev_input *master) |
| 385 | { |
| 386 | struct wlsc_compositor *c; |
| 387 | const char *devnode; |
| 388 | const char *device_seat; |
| 389 | |
| 390 | device_seat = udev_device_get_property_value(udev_device, "ID_SEAT"); |
| 391 | if (!device_seat) |
| 392 | device_seat = default_seat; |
| 393 | |
| 394 | if (strcmp(device_seat, master->seat_id)) |
| 395 | return; |
| 396 | |
| 397 | c = (struct wlsc_compositor *) master->base.input_device.compositor; |
| 398 | devnode = udev_device_get_devnode(udev_device); |
| 399 | if (evdev_input_device_create(master, c->wl_display, devnode)) |
| 400 | fprintf(stderr, "evdev input device: added: %s\n", devnode); |
| 401 | } |
| 402 | |
| 403 | static void |
| 404 | device_removed(struct udev_device *udev_device, struct evdev_input *master) |
| 405 | { |
| 406 | const char *devnode = udev_device_get_devnode(udev_device); |
| 407 | struct evdev_input_device *device, *next; |
| 408 | |
| 409 | wl_list_for_each_safe(device, next, &master->devices_list, link) { |
| 410 | if (!strcmp(device->devnode, devnode)) { |
| 411 | wl_event_source_remove(device->source); |
| 412 | wl_list_remove(&device->link); |
| 413 | close(device->fd); |
| 414 | free(device->devnode); |
| 415 | free(device); |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | fprintf(stderr, "evdev input device: removed: %s\n", devnode); |
| 420 | } |
| 421 | |
| 422 | static int |
| 423 | evdev_udev_handler(int fd, uint32_t mask, void *data) |
| 424 | { |
| 425 | struct evdev_input *master = data; |
| 426 | struct udev_device *udev_device; |
| 427 | const char *action; |
| 428 | |
| 429 | udev_device = udev_monitor_receive_device(master->udev_monitor); |
| 430 | if (!udev_device) |
| 431 | return 1; |
| 432 | |
| 433 | action = udev_device_get_action(udev_device); |
| 434 | if (action) { |
| 435 | if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0) |
| 436 | return 0; |
| 437 | |
| 438 | if (!strcmp(action, "add")) { |
| 439 | device_added(udev_device, master); |
| 440 | } |
| 441 | else if (!strcmp(action, "remove")) |
| 442 | device_removed(udev_device, master); |
| 443 | } |
| 444 | udev_device_unref(udev_device); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int |
| 450 | evdev_config_udev_monitor(struct udev *udev, struct evdev_input *master) |
| 451 | { |
| 452 | struct wl_event_loop *loop; |
| 453 | struct wlsc_compositor *c = |
| 454 | (struct wlsc_compositor *) master->base.input_device.compositor; |
| 455 | |
| 456 | master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); |
| 457 | if (!master->udev_monitor) |
| 458 | return 0; |
| 459 | |
| 460 | udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor, |
| 461 | "input", NULL); |
| 462 | |
| 463 | if (udev_monitor_enable_receiving(master->udev_monitor)) { |
| 464 | fprintf(stderr, "udev: failed to bind the udev monitor\n"); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | loop = wl_display_get_event_loop(c->wl_display); |
| 469 | wl_event_loop_add_fd(loop, udev_monitor_get_fd(master->udev_monitor), |
| 470 | WL_EVENT_READABLE, evdev_udev_handler, master); |
| 471 | |
| 472 | return 1; |
| 473 | } |
| 474 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 475 | void |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 476 | evdev_input_add_devices(struct wlsc_compositor *c, |
| 477 | struct udev *udev, const char *seat) |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 478 | { |
| 479 | struct evdev_input *input; |
| 480 | struct udev_enumerate *e; |
| 481 | struct udev_list_entry *entry; |
| 482 | struct udev_device *device; |
| 483 | const char *path; |
| 484 | |
| 485 | input = malloc(sizeof *input); |
| 486 | if (input == NULL) |
| 487 | return; |
| 488 | |
| 489 | memset(input, 0, sizeof *input); |
| 490 | wlsc_input_device_init(&input->base, c); |
| 491 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 492 | wl_list_init(&input->devices_list); |
| 493 | input->seat_id = strdup(seat); |
| 494 | if (!evdev_config_udev_monitor(udev, input)) { |
| 495 | free(input->seat_id); |
| 496 | free(input); |
| 497 | return; |
| 498 | } |
| 499 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 500 | e = udev_enumerate_new(udev); |
| 501 | udev_enumerate_add_match_subsystem(e, "input"); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 502 | udev_enumerate_scan_devices(e); |
| 503 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 504 | path = udev_list_entry_get_name(entry); |
| 505 | device = udev_device_new_from_syspath(udev, path); |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 506 | |
Kristian Høgsberg | 1d26603 | 2011-07-21 06:46:26 -0700 | [diff] [blame] | 507 | if (strncmp("event", udev_device_get_sysname(device), 5) != 0) |
| 508 | continue; |
| 509 | |
Tiago Vignatti | ac2dc6a | 2011-10-28 13:05:06 -0400 | [diff] [blame] | 510 | device_added(device, input); |
Kristian Høgsberg | 86ec8e8 | 2011-07-19 16:10:11 -0700 | [diff] [blame] | 511 | |
| 512 | udev_device_unref(device); |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 513 | } |
| 514 | udev_enumerate_unref(e); |
| 515 | |
| 516 | c->input_device = &input->base.input_device; |
| 517 | } |