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 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 29 | #include <stdint.h> |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <libinput.h> |
| 35 | #include <libudev.h> |
| 36 | |
Pekka Paalanen | 3d5d947 | 2019-03-28 16:28:47 +0200 | [diff] [blame] | 37 | #include <libweston/libweston.h> |
Marius Vlad | 5d649b6 | 2019-07-16 23:44:21 +0300 | [diff] [blame] | 38 | #include "backend.h" |
Marius Vlad | 0bf3f5a | 2019-07-10 17:32:42 +0300 | [diff] [blame] | 39 | #include "libweston-internal.h" |
Marius Vlad | 4e03629 | 2019-07-09 00:36:20 +0300 | [diff] [blame] | 40 | #include "weston-log-internal.h" |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 41 | #include "launcher-util.h" |
| 42 | #include "libinput-seat.h" |
| 43 | #include "libinput-device.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 44 | #include "shared/helpers.h" |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 45 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 46 | static void |
| 47 | process_events(struct udev_input *input); |
| 48 | static struct udev_seat * |
| 49 | udev_seat_create(struct udev_input *input, const char *seat_name); |
| 50 | static void |
| 51 | udev_seat_destroy(struct udev_seat *seat); |
| 52 | |
Derek Foreman | a6714fa | 2015-05-05 15:01:51 -0500 | [diff] [blame] | 53 | static struct udev_seat * |
| 54 | get_udev_seat(struct udev_input *input, struct libinput_device *device) |
| 55 | { |
| 56 | struct libinput_seat *libinput_seat; |
| 57 | const char *seat_name; |
| 58 | |
| 59 | libinput_seat = libinput_device_get_seat(device); |
| 60 | seat_name = libinput_seat_get_logical_name(libinput_seat); |
| 61 | return udev_seat_get_named(input, seat_name); |
| 62 | } |
| 63 | |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 64 | static struct weston_output * |
| 65 | output_find_by_head_name(struct weston_compositor *compositor, |
| 66 | const char *head_name) |
| 67 | { |
| 68 | struct weston_output *output; |
| 69 | struct weston_head *head; |
| 70 | |
| 71 | if (!head_name) |
| 72 | return NULL; |
| 73 | |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 74 | /* Only enabled outputs with connected heads. |
| 75 | * This means force-enabled outputs but with disconnected heads |
| 76 | * will be ignored; if the touchscreen doesn't have a video signal, |
| 77 | * touching it is meaningless. |
| 78 | */ |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 79 | wl_list_for_each(output, &compositor->output_list, link) { |
| 80 | wl_list_for_each(head, &output->head_list, output_link) { |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 81 | if (!weston_head_is_connected(head)) |
| 82 | continue; |
| 83 | |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 84 | if (strcmp(head_name, head->name) == 0) |
| 85 | return output; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 92 | static int |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 93 | device_added(struct udev_input *input, struct libinput_device *libinput_device) |
| 94 | { |
| 95 | struct weston_compositor *c; |
| 96 | struct evdev_device *device; |
| 97 | struct weston_output *output; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 98 | const char *output_name; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 99 | struct weston_seat *seat; |
| 100 | struct udev_seat *udev_seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 101 | struct weston_pointer *pointer; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 102 | |
| 103 | c = input->compositor; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 104 | |
Derek Foreman | a6714fa | 2015-05-05 15:01:51 -0500 | [diff] [blame] | 105 | udev_seat = get_udev_seat(input, libinput_device); |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 106 | if (!udev_seat) { |
| 107 | weston_log("Failed to get a seat\n"); |
| 108 | return 1; |
| 109 | } |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 110 | |
| 111 | seat = &udev_seat->base; |
| 112 | device = evdev_device_create(libinput_device, seat); |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 113 | if (device == NULL) { |
| 114 | weston_log("Failed to create a device\n"); |
| 115 | return 1; |
| 116 | } |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 117 | |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 118 | if (input->configure_device != NULL) |
| 119 | input->configure_device(c, device->device); |
| 120 | evdev_device_set_calibration(device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 121 | udev_seat = (struct udev_seat *) seat; |
| 122 | wl_list_insert(udev_seat->devices_list.prev, &device->link); |
| 123 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 124 | pointer = weston_seat_get_pointer(seat); |
| 125 | if (seat->output && pointer) |
| 126 | weston_pointer_clamp(pointer, |
| 127 | &pointer->x, |
| 128 | &pointer->y); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 129 | |
| 130 | output_name = libinput_device_get_output_name(libinput_device); |
| 131 | if (output_name) { |
| 132 | device->output_name = strdup(output_name); |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 133 | output = output_find_by_head_name(c, output_name); |
| 134 | evdev_device_set_output(device, output); |
| 135 | } else if (!wl_list_empty(&c->output_list)) { |
| 136 | /* default assignment to an arbitrary output */ |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 137 | output = container_of(c->output_list.next, |
| 138 | struct weston_output, link); |
| 139 | evdev_device_set_output(device, output); |
| 140 | } |
| 141 | |
Samu Nuutamo | 58a2faf | 2018-10-29 17:42:48 +0200 | [diff] [blame] | 142 | if (!input->suspended) { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 143 | weston_seat_repick(seat); |
Samu Nuutamo | 58a2faf | 2018-10-29 17:42:48 +0200 | [diff] [blame] | 144 | |
| 145 | /* Sync device leds with the actual state */ |
| 146 | if (seat->led_update && seat->keyboard_state) |
| 147 | seat->led_update(seat, seat->keyboard_state->xkb_state.leds); |
| 148 | } |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 149 | |
| 150 | return 0; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 153 | static int |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 154 | device_removed(struct udev_input *input, struct libinput_device *libinput_device) |
| 155 | { |
| 156 | struct evdev_device *device; |
Derek Foreman | 0b7da01 | 2015-09-30 13:34:57 -0500 | [diff] [blame] | 157 | |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 158 | device = libinput_device_get_user_data(libinput_device); |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 159 | if (!device) { |
| 160 | weston_log("Failed to retrieve device\n"); |
| 161 | return 1; |
| 162 | } |
Marius Vlad | eb34f82 | 2021-03-30 23:09:05 +0300 | [diff] [blame] | 163 | |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 164 | evdev_device_destroy(device); |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 165 | return 0; |
Derek Foreman | 6b557a7 | 2015-05-05 15:01:52 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 169 | udev_seat_remove_devices(struct udev_seat *seat) |
| 170 | { |
| 171 | struct evdev_device *device, *next; |
| 172 | |
| 173 | wl_list_for_each_safe(device, next, &seat->devices_list, link) { |
| 174 | evdev_device_destroy(device); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | udev_input_disable(struct udev_input *input) |
| 180 | { |
| 181 | if (input->suspended) |
| 182 | return; |
| 183 | |
Derek Foreman | 0e4e570 | 2017-07-25 16:39:20 -0500 | [diff] [blame] | 184 | wl_event_source_remove(input->libinput_source); |
| 185 | input->libinput_source = NULL; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 186 | libinput_suspend(input->libinput); |
| 187 | process_events(input); |
| 188 | input->suspended = 1; |
| 189 | } |
| 190 | |
| 191 | static int |
| 192 | udev_input_process_event(struct libinput_event *event) |
| 193 | { |
| 194 | struct libinput *libinput = libinput_event_get_context(event); |
| 195 | struct libinput_device *libinput_device = |
| 196 | libinput_event_get_device(event); |
| 197 | struct udev_input *input = libinput_get_user_data(libinput); |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 198 | int ret = 0; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 199 | |
| 200 | switch (libinput_event_get_type(event)) { |
| 201 | case LIBINPUT_EVENT_DEVICE_ADDED: |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 202 | ret = device_added(input, libinput_device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 203 | break; |
| 204 | case LIBINPUT_EVENT_DEVICE_REMOVED: |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 205 | ret = device_removed(input, libinput_device); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 206 | break; |
| 207 | default: |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 208 | evdev_device_process_event(event); |
| 209 | break; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 212 | return ret; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void |
| 216 | process_event(struct libinput_event *event) |
| 217 | { |
| 218 | if (udev_input_process_event(event)) |
Marius Vlad | e9a0c06 | 2021-08-27 14:01:38 +0300 | [diff] [blame^] | 219 | exit(EXIT_FAILURE); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void |
| 223 | process_events(struct udev_input *input) |
| 224 | { |
| 225 | struct libinput_event *event; |
| 226 | |
| 227 | while ((event = libinput_get_event(input->libinput))) { |
| 228 | process_event(event); |
| 229 | libinput_event_destroy(event); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | udev_input_dispatch(struct udev_input *input) |
| 235 | { |
| 236 | if (libinput_dispatch(input->libinput) != 0) |
| 237 | weston_log("libinput: Failed to dispatch libinput\n"); |
| 238 | |
| 239 | process_events(input); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int |
| 245 | libinput_source_dispatch(int fd, uint32_t mask, void *data) |
| 246 | { |
| 247 | struct udev_input *input = data; |
| 248 | |
| 249 | return udev_input_dispatch(input) != 0; |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | open_restricted(const char *path, int flags, void *user_data) |
| 254 | { |
| 255 | struct udev_input *input = user_data; |
| 256 | struct weston_launcher *launcher = input->compositor->launcher; |
| 257 | |
| 258 | return weston_launcher_open(launcher, path, flags); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | close_restricted(int fd, void *user_data) |
| 263 | { |
| 264 | struct udev_input *input = user_data; |
| 265 | struct weston_launcher *launcher = input->compositor->launcher; |
| 266 | |
| 267 | weston_launcher_close(launcher, fd); |
| 268 | } |
| 269 | |
| 270 | const struct libinput_interface libinput_interface = { |
| 271 | open_restricted, |
| 272 | close_restricted, |
| 273 | }; |
| 274 | |
| 275 | int |
| 276 | udev_input_enable(struct udev_input *input) |
| 277 | { |
| 278 | struct wl_event_loop *loop; |
| 279 | struct weston_compositor *c = input->compositor; |
| 280 | int fd; |
| 281 | struct udev_seat *seat; |
| 282 | int devices_found = 0; |
| 283 | |
| 284 | loop = wl_display_get_event_loop(c->wl_display); |
| 285 | fd = libinput_get_fd(input->libinput); |
| 286 | input->libinput_source = |
| 287 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 288 | libinput_source_dispatch, input); |
| 289 | if (!input->libinput_source) { |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | if (input->suspended) { |
| 294 | if (libinput_resume(input->libinput) != 0) { |
| 295 | wl_event_source_remove(input->libinput_source); |
| 296 | input->libinput_source = NULL; |
| 297 | return -1; |
| 298 | } |
| 299 | input->suspended = 0; |
| 300 | process_events(input); |
| 301 | } |
| 302 | |
| 303 | wl_list_for_each(seat, &input->compositor->seat_list, base.link) { |
| 304 | evdev_notify_keyboard_focus(&seat->base, &seat->devices_list); |
| 305 | |
| 306 | if (!wl_list_empty(&seat->devices_list)) |
| 307 | devices_found = 1; |
| 308 | } |
| 309 | |
Daniel Díaz | 75b7197 | 2016-10-21 14:03:13 -0500 | [diff] [blame] | 310 | if (devices_found == 0 && !c->require_input) { |
| 311 | weston_log("warning: no input devices found, but none required " |
| 312 | "as per configuration.\n"); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 316 | if (devices_found == 0) { |
| 317 | weston_log( |
| 318 | "warning: no input devices on entering Weston. " |
| 319 | "Possible causes:\n" |
| 320 | "\t- no permissions to read /dev/input/event*\n" |
| 321 | "\t- seats misconfigured " |
| 322 | "(Weston backend option 'seat', " |
| 323 | "udev device property ID_SEAT)\n"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
U. Artie Eoff | 71db0fd | 2014-04-17 07:53:22 -0700 | [diff] [blame] | 330 | static void |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 331 | libinput_log_func(struct libinput *libinput, |
| 332 | enum libinput_log_priority priority, |
| 333 | const char *format, va_list args) |
U. Artie Eoff | 71db0fd | 2014-04-17 07:53:22 -0700 | [diff] [blame] | 334 | { |
| 335 | weston_vlog(format, args); |
| 336 | } |
| 337 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 338 | int |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 339 | udev_input_init(struct udev_input *input, struct weston_compositor *c, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 340 | struct udev *udev, const char *seat_id, |
| 341 | udev_configure_device_t configure_device) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 342 | { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 343 | enum libinput_log_priority priority = LIBINPUT_LOG_PRIORITY_INFO; |
U. Artie Eoff | c81c424 | 2014-04-17 07:53:23 -0700 | [diff] [blame] | 344 | const char *log_priority = NULL; |
| 345 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 346 | memset(input, 0, sizeof *input); |
| 347 | |
| 348 | input->compositor = c; |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 349 | input->configure_device = configure_device; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 350 | |
U. Artie Eoff | c81c424 | 2014-04-17 07:53:23 -0700 | [diff] [blame] | 351 | log_priority = getenv("WESTON_LIBINPUT_LOG_PRIORITY"); |
U. Artie Eoff | 24713f6 | 2014-05-09 11:24:40 -0700 | [diff] [blame] | 352 | |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 353 | input->libinput = libinput_udev_create_context(&libinput_interface, |
| 354 | input, udev); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 355 | if (!input->libinput) { |
| 356 | return -1; |
| 357 | } |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 358 | |
| 359 | libinput_log_set_handler(input->libinput, &libinput_log_func); |
| 360 | |
| 361 | if (log_priority) { |
| 362 | if (strcmp(log_priority, "debug") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 363 | priority = LIBINPUT_LOG_PRIORITY_DEBUG; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 364 | } else if (strcmp(log_priority, "info") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 365 | priority = LIBINPUT_LOG_PRIORITY_INFO; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 366 | } else if (strcmp(log_priority, "error") == 0) { |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 367 | priority = LIBINPUT_LOG_PRIORITY_ERROR; |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Pekka Paalanen | eb4fd35 | 2014-09-12 12:07:43 +0300 | [diff] [blame] | 371 | libinput_log_set_priority(input->libinput, priority); |
| 372 | |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 373 | if (libinput_udev_assign_seat(input->libinput, seat_id) != 0) { |
| 374 | libinput_unref(input->libinput); |
| 375 | return -1; |
| 376 | } |
| 377 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 378 | process_events(input); |
| 379 | |
| 380 | return udev_input_enable(input); |
| 381 | } |
| 382 | |
| 383 | void |
| 384 | udev_input_destroy(struct udev_input *input) |
| 385 | { |
| 386 | struct udev_seat *seat, *next; |
| 387 | |
Derek Foreman | 0e4e570 | 2017-07-25 16:39:20 -0500 | [diff] [blame] | 388 | if (input->libinput_source) |
| 389 | wl_event_source_remove(input->libinput_source); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 390 | wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link) |
| 391 | udev_seat_destroy(seat); |
Peter Hutterer | 3b843d3 | 2014-06-25 14:07:36 +1000 | [diff] [blame] | 392 | libinput_unref(input->libinput); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void |
| 396 | udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds) |
| 397 | { |
| 398 | struct udev_seat *seat = (struct udev_seat *) seat_base; |
| 399 | struct evdev_device *device; |
| 400 | |
| 401 | wl_list_for_each(device, &seat->devices_list, link) |
| 402 | evdev_led_update(device, leds); |
| 403 | } |
| 404 | |
| 405 | static void |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 406 | udev_seat_output_changed(struct udev_seat *seat, struct weston_output *output) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 407 | { |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 408 | struct evdev_device *device; |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 409 | struct weston_output *found; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 410 | |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 411 | wl_list_for_each(device, &seat->devices_list, link) { |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 412 | /* If we find any input device without an associated output |
| 413 | * or an output name to associate with, just tie it with the |
Emmanuel Gil Peyrot | 426c246 | 2019-02-20 16:33:32 +0100 | [diff] [blame] | 414 | * output we got here - the default assignment. |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 415 | */ |
| 416 | if (!device->output_name) { |
| 417 | if (!device->output) |
| 418 | evdev_device_set_output(device, output); |
| 419 | |
| 420 | continue; |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 421 | } |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 422 | |
Pekka Paalanen | fd02efb | 2018-03-19 14:55:41 +0200 | [diff] [blame] | 423 | /* Update all devices' output associations, may they gain or |
| 424 | * lose it. |
| 425 | */ |
| 426 | found = output_find_by_head_name(output->compositor, |
| 427 | device->output_name); |
| 428 | evdev_device_set_output(device, found); |
Ander Conselvan de Oliveira | a7caef9 | 2014-04-24 15:11:17 +0300 | [diff] [blame] | 429 | } |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 432 | static void |
| 433 | notify_output_create(struct wl_listener *listener, void *data) |
| 434 | { |
| 435 | struct udev_seat *seat = container_of(listener, struct udev_seat, |
| 436 | output_create_listener); |
| 437 | struct weston_output *output = data; |
| 438 | |
| 439 | udev_seat_output_changed(seat, output); |
| 440 | } |
| 441 | |
| 442 | static void |
| 443 | notify_output_heads_changed(struct wl_listener *listener, void *data) |
| 444 | { |
| 445 | struct udev_seat *seat = container_of(listener, struct udev_seat, |
| 446 | output_heads_listener); |
| 447 | struct weston_output *output = data; |
| 448 | |
| 449 | udev_seat_output_changed(seat, output); |
| 450 | } |
| 451 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 452 | static struct udev_seat * |
| 453 | udev_seat_create(struct udev_input *input, const char *seat_name) |
| 454 | { |
| 455 | struct weston_compositor *c = input->compositor; |
| 456 | struct udev_seat *seat; |
| 457 | |
| 458 | seat = zalloc(sizeof *seat); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 459 | if (!seat) |
| 460 | return NULL; |
Bryce W. Harrington | bfd74f4 | 2014-04-21 23:51:02 +0000 | [diff] [blame] | 461 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 462 | weston_seat_init(&seat->base, c, seat_name); |
| 463 | seat->base.led_update = udev_seat_led_update; |
| 464 | |
| 465 | seat->output_create_listener.notify = notify_output_create; |
| 466 | wl_signal_add(&c->output_created_signal, |
| 467 | &seat->output_create_listener); |
| 468 | |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 469 | seat->output_heads_listener.notify = notify_output_heads_changed; |
| 470 | wl_signal_add(&c->output_heads_changed_signal, |
| 471 | &seat->output_heads_listener); |
| 472 | |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 473 | wl_list_init(&seat->devices_list); |
| 474 | |
| 475 | return seat; |
| 476 | } |
| 477 | |
| 478 | static void |
| 479 | udev_seat_destroy(struct udev_seat *seat) |
| 480 | { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 481 | struct weston_keyboard *keyboard = |
| 482 | weston_seat_get_keyboard(&seat->base); |
| 483 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 484 | if (keyboard) |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 485 | notify_keyboard_focus_out(&seat->base); |
Derek Foreman | 87c862a | 2015-08-06 12:19:51 -0500 | [diff] [blame] | 486 | |
| 487 | udev_seat_remove_devices(seat); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 488 | weston_seat_release(&seat->base); |
| 489 | wl_list_remove(&seat->output_create_listener.link); |
Pekka Paalanen | 8dc6db8 | 2018-03-20 13:29:40 +0200 | [diff] [blame] | 490 | wl_list_remove(&seat->output_heads_listener.link); |
Jonas Ådahl | e0de3c2 | 2014-03-12 22:08:42 +0100 | [diff] [blame] | 491 | free(seat); |
| 492 | } |
| 493 | |
| 494 | struct udev_seat * |
| 495 | udev_seat_get_named(struct udev_input *input, const char *seat_name) |
| 496 | { |
| 497 | struct udev_seat *seat; |
| 498 | |
| 499 | wl_list_for_each(seat, &input->compositor->seat_list, base.link) { |
| 500 | if (strcmp(seat->base.seat_name, seat_name) == 0) |
| 501 | return seat; |
| 502 | } |
| 503 | |
| 504 | return udev_seat_create(input, seat_name); |
| 505 | } |