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