Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * Copyright © 2013 Jonas Ådahl |
| 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <libinput.h> |
| 34 | #include <libudev.h> |
| 35 | |
| 36 | #include "compositor.h" |
| 37 | #include "launcher-util.h" |
| 38 | #include "libinput-seat.h" |
| 39 | #include "libinput-device.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 40 | #include "shared/helpers.h" |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 41 | |
| 42 | static const char default_seat[] = "seat0"; |
| 43 | static const char default_seat_name[] = "default"; |
| 44 | |
| 45 | static void |
| 46 | process_events(struct udev_input *input); |
| 47 | static struct udev_seat * |
| 48 | udev_seat_create(struct udev_input *input, const char *seat_name); |
| 49 | static void |
| 50 | udev_seat_destroy(struct udev_seat *seat); |
| 51 | |
| 52 | static void |
| 53 | device_added(struct udev_input *input, struct libinput_device *libinput_device) |
| 54 | { |
| 55 | struct weston_compositor *c; |
| 56 | struct evdev_device *device; |
| 57 | struct weston_output *output; |
| 58 | const char *seat_name; |
| 59 | const char *output_name; |
| 60 | struct libinput_seat *libinput_seat; |
| 61 | struct weston_seat *seat; |
| 62 | struct udev_seat *udev_seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 63 | struct weston_pointer *pointer; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 64 | |
| 65 | c = input->compositor; |
| 66 | libinput_seat = libinput_device_get_seat(libinput_device); |
| 67 | |
| 68 | seat_name = libinput_seat_get_logical_name(libinput_seat); |
| 69 | udev_seat = udev_seat_get_named(input, seat_name); |
| 70 | if (!udev_seat) |
| 71 | return; |
| 72 | |
| 73 | seat = &udev_seat->base; |
| 74 | device = evdev_device_create(libinput_device, seat); |
| 75 | if (device == NULL) |
| 76 | return; |
| 77 | |
| 78 | udev_seat = (struct udev_seat *) seat; |
| 79 | wl_list_insert(udev_seat->devices_list.prev, &device->link); |
| 80 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 81 | pointer = weston_seat_get_pointer(seat); |
| 82 | if (seat->output && pointer) |
| 83 | weston_pointer_clamp(pointer, |
| 84 | &pointer->x, |
| 85 | &pointer->y); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 86 | |
| 87 | output_name = libinput_device_get_output_name(libinput_device); |
| 88 | if (output_name) { |
| 89 | device->output_name = strdup(output_name); |
| 90 | wl_list_for_each(output, &c->output_list, link) |
Derek Foreman | f3723d9 | 2015-03-17 13:22:35 -0500 | [diff] [blame] | 91 | if (output->name && |
| 92 | strcmp(output->name, device->output_name) == 0) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 93 | evdev_device_set_output(device, output); |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 94 | } else if (device->output == NULL && !wl_list_empty(&c->output_list)) { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 95 | output = container_of(c->output_list.next, |
| 96 | struct weston_output, link); |
| 97 | evdev_device_set_output(device, output); |
| 98 | } |
| 99 | |
| 100 | if (!input->suspended) |
| 101 | weston_seat_repick(seat); |
| 102 | } |
| 103 | |
| 104 | static void |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 105 | device_removed(struct udev_input *input, struct libinput_device *libinput_device) |
| 106 | { |
| 107 | struct evdev_device *device; |
| 108 | device = libinput_device_get_user_data(libinput_device); |
| 109 | evdev_device_destroy(device); |
| 110 | } |
| 111 | |
| 112 | static void |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 113 | udev_seat_remove_devices(struct udev_seat *seat) |
| 114 | { |
| 115 | struct evdev_device *device, *next; |
| 116 | |
| 117 | wl_list_for_each_safe(device, next, &seat->devices_list, link) { |
| 118 | evdev_device_destroy(device); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | udev_input_disable(struct udev_input *input) |
| 124 | { |
| 125 | if (input->suspended) |
| 126 | return; |
| 127 | |
| 128 | libinput_suspend(input->libinput); |
| 129 | process_events(input); |
| 130 | input->suspended = 1; |
| 131 | } |
| 132 | |
| 133 | static int |
| 134 | udev_input_process_event(struct libinput_event *event) |
| 135 | { |
| 136 | struct libinput *libinput = libinput_event_get_context(event); |
| 137 | struct libinput_device *libinput_device = |
| 138 | libinput_event_get_device(event); |
| 139 | struct udev_input *input = libinput_get_user_data(libinput); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 140 | int handled = 1; |
| 141 | |
| 142 | switch (libinput_event_get_type(event)) { |
| 143 | case LIBINPUT_EVENT_DEVICE_ADDED: |
| 144 | device_added(input, libinput_device); |
| 145 | break; |
| 146 | case LIBINPUT_EVENT_DEVICE_REMOVED: |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 147 | device_removed(input, libinput_device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 148 | break; |
| 149 | default: |
| 150 | handled = 0; |
| 151 | } |
| 152 | |
| 153 | return handled; |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | process_event(struct libinput_event *event) |
| 158 | { |
| 159 | if (udev_input_process_event(event)) |
| 160 | return; |
| 161 | if (evdev_device_process_event(event)) |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | process_events(struct udev_input *input) |
| 167 | { |
| 168 | struct libinput_event *event; |
| 169 | |
| 170 | while ((event = libinput_get_event(input->libinput))) { |
| 171 | process_event(event); |
| 172 | libinput_event_destroy(event); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static int |
| 177 | udev_input_dispatch(struct udev_input *input) |
| 178 | { |
| 179 | if (libinput_dispatch(input->libinput) != 0) |
| 180 | weston_log("libinput: Failed to dispatch libinput\n"); |
| 181 | |
| 182 | process_events(input); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int |
| 188 | libinput_source_dispatch(int fd, uint32_t mask, void *data) |
| 189 | { |
| 190 | struct udev_input *input = data; |
| 191 | |
| 192 | return udev_input_dispatch(input) != 0; |
| 193 | } |
| 194 | |
| 195 | static int |
| 196 | open_restricted(const char *path, int flags, void *user_data) |
| 197 | { |
| 198 | struct udev_input *input = user_data; |
| 199 | struct weston_launcher *launcher = input->compositor->launcher; |
| 200 | |
| 201 | return weston_launcher_open(launcher, path, flags); |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | close_restricted(int fd, void *user_data) |
| 206 | { |
| 207 | struct udev_input *input = user_data; |
| 208 | struct weston_launcher *launcher = input->compositor->launcher; |
| 209 | |
| 210 | weston_launcher_close(launcher, fd); |
| 211 | } |
| 212 | |
| 213 | const struct libinput_interface libinput_interface = { |
| 214 | open_restricted, |
| 215 | close_restricted, |
| 216 | }; |
| 217 | |
| 218 | int |
| 219 | udev_input_enable(struct udev_input *input) |
| 220 | { |
| 221 | struct wl_event_loop *loop; |
| 222 | struct weston_compositor *c = input->compositor; |
| 223 | int fd; |
| 224 | struct udev_seat *seat; |
| 225 | int devices_found = 0; |
| 226 | |
| 227 | loop = wl_display_get_event_loop(c->wl_display); |
| 228 | fd = libinput_get_fd(input->libinput); |
| 229 | input->libinput_source = |
| 230 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 231 | libinput_source_dispatch, input); |
| 232 | if (!input->libinput_source) { |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (input->suspended) { |
| 237 | if (libinput_resume(input->libinput) != 0) { |
| 238 | wl_event_source_remove(input->libinput_source); |
| 239 | input->libinput_source = NULL; |
| 240 | return -1; |
| 241 | } |
| 242 | input->suspended = 0; |
| 243 | process_events(input); |
| 244 | } |
| 245 | |
| 246 | wl_list_for_each(seat, &input->compositor->seat_list, base.link) { |
| 247 | evdev_notify_keyboard_focus(&seat->base, &seat->devices_list); |
| 248 | |
| 249 | if (!wl_list_empty(&seat->devices_list)) |
| 250 | devices_found = 1; |
| 251 | } |
| 252 | |
| 253 | if (devices_found == 0) { |
| 254 | weston_log( |
| 255 | "warning: no input devices on entering Weston. " |
| 256 | "Possible causes:\n" |
| 257 | "\t- no permissions to read /dev/input/event*\n" |
| 258 | "\t- seats misconfigured " |
| 259 | "(Weston backend option 'seat', " |
| 260 | "udev device property ID_SEAT)\n"); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
U. Artie Eoff | 71db0fd | 2014-04-17 07:53:22 -0700 | [diff] [blame] | 267 | static void |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 268 | libinput_log_func(struct libinput *libinput, |
| 269 | enum libinput_log_priority priority, |
| 270 | const char *format, va_list args) |
U. Artie Eoff | 71db0fd | 2014-04-17 07:53:22 -0700 | [diff] [blame] | 271 | { |
| 272 | weston_vlog(format, args); |
| 273 | } |
| 274 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 275 | int |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 276 | udev_input_init(struct udev_input *input, struct weston_compositor *c, |
| 277 | struct udev *udev, const char *seat_id) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 278 | { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 279 | enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO; |
U. Artie Eoff | c81c424 | 2014-04-17 07:53:23 -0700 | [diff] [blame] | 280 | const char *log_priority = NULL; |
| 281 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 282 | memset(input, 0, sizeof *input); |
| 283 | |
| 284 | input->compositor = c; |
| 285 | |
U. Artie Eoff | c81c424 | 2014-04-17 07:53:23 -0700 | [diff] [blame] | 286 | log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY"); |
U. Artie Eoff | 24713f6 | 2014-05-09 11:24:40 -0700 | [diff] [blame] | 287 | |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 288 | input->libinput = libinput_udev_create_context(&libinput_interface, |
| 289 | input, udev); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 290 | if (!input->libinput) { |
| 291 | return -1; |
| 292 | } |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 293 | |
| 294 | libinput_log_set_handler(input->libinput, &libinput_log_func); |
| 295 | |
| 296 | if (log_priority) { |
| 297 | if (strcmp(log_priority, "debug") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 298 | priority = LIBINPUT_LOG_PRIORITY_DEBUG; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 299 | } else if (strcmp(log_priority, "info") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 300 | priority = LIBINPUT_LOG_PRIORITY_INFO; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 301 | } else if (strcmp(log_priority, "error") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 302 | priority = LIBINPUT_LOG_PRIORITY_ERROR; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 306 | libinput_log_set_priority(input->libinput, priority); |
| 307 | |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 308 | if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) { |
| 309 | libinput_unref(input->libinput); |
| 310 | return -1; |
| 311 | } |
| 312 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 313 | process_events(input); |
| 314 | |
| 315 | return udev_input_enable(input); |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | udev_input_destroy(struct udev_input *input) |
| 320 | { |
| 321 | struct udev_seat *seat, *next; |
| 322 | |
| 323 | wl_event_source_remove(input->libinput_source); |
| 324 | wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link) |
| 325 | udev_seat_destroy(seat); |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 326 | libinput_unref(input->libinput); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void |
| 330 | udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds) |
| 331 | { |
| 332 | struct udev_seat *seat = (struct udev_seat *) seat_base; |
| 333 | struct evdev_device *device; |
| 334 | |
| 335 | wl_list_for_each(device, &seat->devices_list, link) |
| 336 | evdev_led_update(device, leds); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | notify_output_create(struct wl_listener *listener, void *data) |
| 341 | { |
| 342 | struct udev_seat *seat = container_of(listener, struct udev_seat, |
| 343 | output_create_listener); |
| 344 | struct evdev_device *device; |
| 345 | struct weston_output *output = data; |
| 346 | |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 347 | wl_list_for_each(device, &seat->devices_list, link) { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 348 | if (device->output_name && |
| 349 | strcmp(output->name, device->output_name) == 0) { |
| 350 | evdev_device_set_output(device, output); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 351 | } |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 352 | |
| 353 | if (device->output_name == NULL && device->output == NULL) |
| 354 | evdev_device_set_output(device, output); |
| 355 | } |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static struct udev_seat * |
| 359 | udev_seat_create(struct udev_input *input, const char *seat_name) |
| 360 | { |
| 361 | struct weston_compositor *c = input->compositor; |
| 362 | struct udev_seat *seat; |
| 363 | |
| 364 | seat = zalloc(sizeof *seat); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 365 | if (!seat) |
| 366 | return NULL; |
Bryce W. Harrington | bfd74f4 | 2014-04-21 23:51:02 +0000 | [diff] [blame] | 367 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 368 | weston_seat_init(&seat->base, c, seat_name); |
| 369 | seat->base.led_update = udev_seat_led_update; |
| 370 | |
| 371 | seat->output_create_listener.notify = notify_output_create; |
| 372 | wl_signal_add(&c->output_created_signal, |
| 373 | &seat->output_create_listener); |
| 374 | |
| 375 | wl_list_init(&seat->devices_list); |
| 376 | |
| 377 | return seat; |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | udev_seat_destroy(struct udev_seat *seat) |
| 382 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 383 | struct weston_keyboard *keyboard = |
| 384 | weston_seat_get_keyboard(&seat->base); |
| 385 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 386 | if (keyboard) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 387 | notify_keyboard_focus_out(&seat->base); |
Derek Foreman | 87c862a | 2015-08-06 12:19:51 -0500 | [diff] [blame^] | 388 | |
| 389 | udev_seat_remove_devices(seat); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 390 | weston_seat_release(&seat->base); |
| 391 | wl_list_remove(&seat->output_create_listener.link); |
| 392 | free(seat); |
| 393 | } |
| 394 | |
| 395 | struct udev_seat * |
| 396 | udev_seat_get_named(struct udev_input *input, const char *seat_name) |
| 397 | { |
| 398 | struct udev_seat *seat; |
| 399 | |
| 400 | wl_list_for_each(seat, &input->compositor->seat_list, base.link) { |
| 401 | if (strcmp(seat->base.seat_name, seat_name) == 0) |
| 402 | return seat; |
| 403 | } |
| 404 | |
| 405 | return udev_seat_create(input, seat_name); |
| 406 | } |