1/* 2 * Copyright © 2013 Intel Corporation 3 * Copyright © 2013-2015 Red Hat, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 */ 24 25#include "config.h" 26 27#include <stdlib.h> 28#include <stdio.h> 29#include <string.h> 30 31#include "evdev.h" 32#include "udev-seat.h" 33 34static const char default_seat[] = "seat0"; 35static const char default_seat_name[] = "default"; 36 37static struct udev_seat * 38udev_seat_create(struct udev_input *input, 39 const char *device_seat, 40 const char *seat_name); 41static struct udev_seat * 42udev_seat_get_named(struct udev_input *input, const char *seat_name); 43 44static inline bool 45filter_duplicates(struct udev_seat *udev_seat, 46 struct udev_device *udev_device) 47{ 48 struct libinput_device *device; 49 const char *new_syspath = udev_device_get_syspath(udev_device); 50 bool ignore_device = false; 51 52 if (!udev_seat) 53 return false; 54 55 list_for_each(device, &udev_seat->base.devices_list, link) { 56 const char *syspath; 57 struct udev_device *ud; 58 59 ud = libinput_device_get_udev_device(device); 60 if (!ud) 61 continue; 62 63 syspath = udev_device_get_syspath(ud); 64 if (syspath && new_syspath && streq(syspath, new_syspath)) 65 ignore_device = true; 66 udev_device_unref(ud); 67 68 if (ignore_device) 69 break; 70 } 71 72 return ignore_device; 73} 74 75static int 76device_added(struct udev_device *udev_device, 77 struct udev_input *input, 78 const char *seat_name) 79{ 80 struct evdev_device *device; 81 const char *devnode, *sysname; 82 const char *device_seat, *output_name; 83 struct udev_seat *seat; 84 85 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT"); 86 if (!device_seat) 87 device_seat = default_seat; 88 89 if (!streq(device_seat, input->seat_id)) 90 return 0; 91 92 if (ignore_litest_test_suite_device(udev_device)) 93 return 0; 94 95 devnode = udev_device_get_devnode(udev_device); 96 sysname = udev_device_get_sysname(udev_device); 97 98 /* Search for matching logical seat */ 99 if (!seat_name) 100 seat_name = udev_device_get_property_value(udev_device, "WL_SEAT"); 101 if (!seat_name) 102 seat_name = default_seat_name; 103 104 seat = udev_seat_get_named(input, seat_name); 105 106 /* There is a race at startup: a device added between setting 107 * up the udev monitor and enumerating all current devices may show 108 * up in both lists. Filter those out. 109 */ 110 if (filter_duplicates(seat, udev_device)) 111 return 0; 112 113 if (seat) 114 libinput_seat_ref(&seat->base); 115 else { 116 seat = udev_seat_create(input, device_seat, seat_name); 117 if (!seat) 118 return -1; 119 } 120 121 device = evdev_device_create(&seat->base, udev_device); 122 libinput_seat_unref(&seat->base); 123 124 if (device == EVDEV_UNHANDLED_DEVICE) { 125 log_info(&input->base, 126 "%-7s - not using input device '%s'\n", 127 sysname, 128 devnode); 129 return 0; 130 } 131 132 if (device == NULL) { 133 log_info(&input->base, 134 "%-7s - failed to create input device '%s'\n", 135 sysname, 136 devnode); 137 return 0; 138 } 139 140 evdev_read_calibration_prop(device); 141 142 output_name = udev_device_get_property_value(udev_device, "WL_OUTPUT"); 143 device->output_name = safe_strdup(output_name); 144 145 return 0; 146} 147 148static void 149device_removed(struct udev_device *udev_device, struct udev_input *input) 150{ 151 struct evdev_device *device; 152 struct udev_seat *seat; 153 const char *syspath; 154 155 syspath = udev_device_get_syspath(udev_device); 156 list_for_each(seat, &input->base.seat_list, base.link) { 157 list_for_each_safe(device, 158 &seat->base.devices_list, base.link) { 159 if (streq(syspath, 160 udev_device_get_syspath(device->udev_device))) { 161 evdev_device_remove(device); 162 break; 163 } 164 } 165 } 166} 167 168static int 169udev_input_add_devices(struct udev_input *input, struct udev *udev) 170{ 171 struct udev_enumerate *e; 172 struct udev_list_entry *entry; 173 struct udev_device *device; 174 const char *path, *sysname; 175 176 e = udev_enumerate_new(udev); 177 udev_enumerate_add_match_subsystem(e, "input"); 178 udev_enumerate_scan_devices(e); 179 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { 180 path = udev_list_entry_get_name(entry); 181 device = udev_device_new_from_syspath(udev, path); 182 if (!device) 183 continue; 184 185 sysname = udev_device_get_sysname(device); 186 if (!strneq("event", sysname, 5)) { 187 udev_device_unref(device); 188 continue; 189 } 190 191 /* Skip unconfigured device. udev will send an event 192 * when device is fully configured */ 193 if (!udev_device_get_is_initialized(device)) { 194 log_debug(&input->base, 195 "%-7s - skip unconfigured input device '%s'\n", 196 sysname, 197 udev_device_get_devnode(device)); 198 udev_device_unref(device); 199 continue; 200 } 201 202 if (device_added(device, input, NULL) < 0) { 203 udev_device_unref(device); 204 udev_enumerate_unref(e); 205 return -1; 206 } 207 208 udev_device_unref(device); 209 } 210 udev_enumerate_unref(e); 211 212 return 0; 213} 214 215static void 216evdev_udev_handler(void *data) 217{ 218 struct udev_input *input = data; 219 struct udev_device *udev_device; 220 const char *action; 221 222 udev_device = udev_monitor_receive_device(input->udev_monitor); 223 if (!udev_device) 224 return; 225 226 action = udev_device_get_action(udev_device); 227 if (!action) 228 goto out; 229 230 if (!strneq("event", udev_device_get_sysname(udev_device), 5)) 231 goto out; 232 233 if (streq(action, "add")) 234 device_added(udev_device, input, NULL); 235 else if (streq(action, "remove")) 236 device_removed(udev_device, input); 237 238out: 239 udev_device_unref(udev_device); 240} 241 242static void 243udev_input_remove_devices(struct udev_input *input) 244{ 245 struct evdev_device *device; 246 struct udev_seat *seat; 247 248 list_for_each_safe(seat, &input->base.seat_list, base.link) { 249 libinput_seat_ref(&seat->base); 250 list_for_each_safe(device, 251 &seat->base.devices_list, base.link) { 252 evdev_device_remove(device); 253 } 254 libinput_seat_unref(&seat->base); 255 } 256} 257 258static void 259udev_input_disable(struct libinput *libinput) 260{ 261 struct udev_input *input = (struct udev_input*)libinput; 262 263 if (!input->udev_monitor) 264 return; 265 266 udev_monitor_unref(input->udev_monitor); 267 input->udev_monitor = NULL; 268 libinput_remove_source(&input->base, input->udev_monitor_source); 269 input->udev_monitor_source = NULL; 270 271 udev_input_remove_devices(input); 272} 273 274static int 275udev_input_enable(struct libinput *libinput) 276{ 277 struct udev_input *input = (struct udev_input*)libinput; 278 struct udev *udev = input->udev; 279 int fd; 280 281 if (input->udev_monitor || !input->seat_id) 282 return 0; 283 284 input->udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); 285 if (!input->udev_monitor) { 286 log_info(libinput, 287 "udev: failed to create the udev monitor\n"); 288 return -1; 289 } 290 291 if (udev_monitor_filter_add_match_subsystem_devtype( 292 input->udev_monitor, "input", NULL)) { 293 log_info(libinput, "udev: failed to set up filter\n"); 294 return -1; 295 } 296 297 if (udev_monitor_enable_receiving(input->udev_monitor)) { 298 log_info(libinput, "udev: failed to bind the udev monitor\n"); 299 udev_monitor_unref(input->udev_monitor); 300 input->udev_monitor = NULL; 301 return -1; 302 } 303 304 fd = udev_monitor_get_fd(input->udev_monitor); 305 input->udev_monitor_source = libinput_add_fd(&input->base, 306 fd, 307 evdev_udev_handler, 308 input); 309 if (!input->udev_monitor_source) { 310 udev_monitor_unref(input->udev_monitor); 311 input->udev_monitor = NULL; 312 return -1; 313 } 314 315 if (udev_input_add_devices(input, udev) < 0) { 316 udev_input_disable(libinput); 317 return -1; 318 } 319 320 return 0; 321} 322 323static void 324udev_input_destroy(struct libinput *input) 325{ 326 struct udev_input *udev_input = (struct udev_input*)input; 327 328 if (input == NULL) 329 return; 330 331 udev_unref(udev_input->udev); 332 free(udev_input->seat_id); 333} 334 335static void 336udev_seat_destroy(struct libinput_seat *seat) 337{ 338 struct udev_seat *useat = (struct udev_seat*)seat; 339 free(useat); 340} 341 342static struct udev_seat * 343udev_seat_create(struct udev_input *input, 344 const char *device_seat, 345 const char *seat_name) 346{ 347 struct udev_seat *seat; 348 349 seat = zalloc(sizeof *seat); 350 351 libinput_seat_init(&seat->base, &input->base, 352 device_seat, seat_name, 353 udev_seat_destroy); 354 355 return seat; 356} 357 358static struct udev_seat * 359udev_seat_get_named(struct udev_input *input, const char *seat_name) 360{ 361 struct udev_seat *seat; 362 363 list_for_each(seat, &input->base.seat_list, base.link) { 364 if (streq(seat->base.logical_name, seat_name)) 365 return seat; 366 } 367 368 return NULL; 369} 370 371static int 372udev_device_change_seat(struct libinput_device *device, 373 const char *seat_name) 374{ 375 struct libinput *libinput = device->seat->libinput; 376 struct udev_input *input = (struct udev_input *)libinput; 377 struct evdev_device *evdev = evdev_device(device); 378 struct udev_device *udev_device = evdev->udev_device; 379 int rc; 380 381 udev_device_ref(udev_device); 382 device_removed(udev_device, input); 383 rc = device_added(udev_device, input, seat_name); 384 udev_device_unref(udev_device); 385 386 return rc; 387} 388 389static const struct libinput_interface_backend interface_backend = { 390 .resume = udev_input_enable, 391 .suspend = udev_input_disable, 392 .destroy = udev_input_destroy, 393 .device_change_seat = udev_device_change_seat, 394}; 395 396LIBINPUT_EXPORT struct libinput * 397libinput_udev_create_context(const struct libinput_interface *interface, 398 void *user_data, 399 struct udev *udev) 400{ 401 struct udev_input *input; 402 403 if (!interface || !udev) 404 return NULL; 405 406 input = zalloc(sizeof *input); 407 408 if (libinput_init(&input->base, interface, 409 &interface_backend, user_data) != 0) { 410 libinput_unref(&input->base); 411 free(input); 412 return NULL; 413 } 414 415 input->udev = udev_ref(udev); 416 417 return &input->base; 418} 419 420LIBINPUT_EXPORT int 421libinput_udev_assign_seat(struct libinput *libinput, 422 const char *seat_id) 423{ 424 struct udev_input *input = (struct udev_input*)libinput; 425 426 if (!seat_id) 427 return -1; 428 429 if (strlen(seat_id) > 256) { 430 log_bug_client(libinput, 431 "Unexpected seat id, limited to 256 characters.\n"); 432 return -1; 433 } 434 435 if (libinput->interface_backend != &interface_backend) { 436 log_bug_client(libinput, "Mismatching backends.\n"); 437 return -1; 438 } 439 440 if (input->seat_id != NULL) 441 return -1; 442 443 /* We cannot do this during udev_create_context because the log 444 * handler isn't set up there but we really want to log to the right 445 * place if the quirks run into parser errors. So we have to do it 446 * here since we can expect the log handler to be set up by now. 447 */ 448 libinput_init_quirks(libinput); 449 450 input->seat_id = safe_strdup(seat_id); 451 452 if (udev_input_enable(&input->base) < 0) 453 return -1; 454 455 return 0; 456} 457