/* Adapted from test/shared.c from the libinput distribution */ /* tools/shared.c 1.23.0: * * Copyright © 2014 Red Hat, Inc. * Modifications: Copyright © 2019 Bill Allombert * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "shared.h" #include "util-strings.h" static void log_handler(struct libinput *li, enum libinput_log_priority priority, const char *format, va_list args) { vprintf(format, args); } void tools_init_options(struct tools_options *options) { memset(options, 0, sizeof(*options)); options->tapping = -1; options->tap_map = -1; options->drag = -1; options->drag_lock = -1; options->natural_scroll = -1; options->left_handed = -1; options->middlebutton = -1; options->dwt = -1; options->dwtp = -1; options->click_method = -1; options->scroll_method = -1; options->scroll_button = -1; options->scroll_button_lock = -1; options->speed = 0.0; options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_NONE; /* initialize accel args */ #ifdef HAVE_LIBINPUT_1_23 static double points[] = {0.0, 1.0}; options->custom_points = points; options->custom_npoints = ARRAY_LENGTH(points); options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK; options->custom_step = 1.0; #endif } int tools_parse_option(int option, const char *optarg, struct tools_options *options) { switch(option) { case OPT_TAP_ENABLE: options->tapping = 1; break; case OPT_TAP_DISABLE: options->tapping = 0; break; case OPT_TAP_MAP: if (!optarg) return 1; if (streq(optarg, "lrm")) { options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LRM; } else if (streq(optarg, "lmr")) { options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LMR; } else { return 1; } break; case OPT_DRAG_ENABLE: options->drag = 1; break; case OPT_DRAG_DISABLE: options->drag = 0; break; case OPT_DRAG_LOCK_ENABLE: options->drag_lock = 1; break; case OPT_DRAG_LOCK_DISABLE: options->drag_lock = 0; break; case OPT_NATURAL_SCROLL_ENABLE: options->natural_scroll = 1; break; case OPT_NATURAL_SCROLL_DISABLE: options->natural_scroll = 0; break; case OPT_LEFT_HANDED_ENABLE: options->left_handed = 1; break; case OPT_LEFT_HANDED_DISABLE: options->left_handed = 0; break; case OPT_MIDDLEBUTTON_ENABLE: options->middlebutton = 1; break; case OPT_MIDDLEBUTTON_DISABLE: options->middlebutton = 0; break; case OPT_DWT_ENABLE: options->dwt = LIBINPUT_CONFIG_DWT_ENABLED; break; case OPT_DWT_DISABLE: options->dwt = LIBINPUT_CONFIG_DWT_DISABLED; break; case OPT_DWTP_ENABLE: #ifdef HAVE_LIBINPUT_1_21 options->dwtp = LIBINPUT_CONFIG_DWTP_ENABLED; #endif break; case OPT_DWTP_DISABLE: #ifdef HAVE_LIBINPUT_1_21 options->dwtp = LIBINPUT_CONFIG_DWTP_DISABLED; #endif break; case OPT_CLICK_METHOD: if (!optarg) return 1; if (streq(optarg, "none")) { options->click_method = LIBINPUT_CONFIG_CLICK_METHOD_NONE; } else if (streq(optarg, "clickfinger")) { options->click_method = LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER; } else if (streq(optarg, "buttonareas")) { options->click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS; } else { return 1; } break; case OPT_CLICKFINGER_MAP: if (!optarg) return 1; if (streq(optarg, "lrm")) { #ifdef HAVE_LIBINPUT_1_26 options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LRM; #endif } else if (streq(optarg, "lmr")) { #ifdef HAVE_LIBINPUT_1_26 options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LMR; #endif } else { return 1; } break; case OPT_SCROLL_METHOD: if (!optarg) return 1; if (streq(optarg, "none")) { options->scroll_method = LIBINPUT_CONFIG_SCROLL_NO_SCROLL; } else if (streq(optarg, "twofinger")) { options->scroll_method = LIBINPUT_CONFIG_SCROLL_2FG; } else if (streq(optarg, "edge")) { options->scroll_method = LIBINPUT_CONFIG_SCROLL_EDGE; } else if (streq(optarg, "button")) { options->scroll_method = LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN; } else { return 1; } break; case OPT_SCROLL_BUTTON: if (!optarg) { return 1; } options->scroll_button = libevdev_event_code_from_name(EV_KEY, optarg); if (options->scroll_button == -1) { fprintf(stderr, "Invalid button %s\n", optarg); return 1; } break; case OPT_SCROLL_BUTTON_LOCK_ENABLE: options->scroll_button_lock = true; break; case OPT_SCROLL_BUTTON_LOCK_DISABLE: options->scroll_button_lock = false; break; case OPT_SPEED: if (!optarg) return 1; options->speed = atof(optarg); break; case OPT_PROFILE: if (!optarg) return 1; if (streq(optarg, "adaptive")) options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE; else if (streq(optarg, "flat")) options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT; #ifdef HAVE_LIBINPUT_1_23 else if (streq(optarg, "custom")) options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM; #endif else return 1; break; case OPT_DISABLE_SENDEVENTS: if (!optarg) return 1; snprintf(options->disable_pattern, sizeof(options->disable_pattern), "%s", optarg); break; case OPT_APPLY_TO: if (!optarg) return 1; snprintf(options->match, sizeof(options->match), "%s", optarg); break; case OPT_CUSTOM_POINTS: if (!optarg) return 1; #ifdef HAVE_LIBINPUT_1_23 options->custom_points = double_array_from_string(optarg, ";", &options->custom_npoints); if (!options->custom_points || options->custom_npoints < 2) { fprintf(stderr, "Invalid --set-custom-points\n" "Please provide at least 2 points separated by a semicolon\n" " e.g. --set-custom-points=\"1.0;1.5\"\n"); return 1; } #endif break; case OPT_CUSTOM_STEP: if (!optarg) return 1; #ifdef HAVE_LIBINPUT_1_23 options->custom_step = strtod(optarg, NULL); #endif break; case OPT_CUSTOM_TYPE: if (!optarg) return 1; #ifdef HAVE_LIBINPUT_1_23 if (streq(optarg, "fallback")) options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK; else if (streq(optarg, "motion")) options->custom_type = LIBINPUT_ACCEL_TYPE_MOTION; else if (streq(optarg, "scroll")) options->custom_type = LIBINPUT_ACCEL_TYPE_SCROLL; else { fprintf(stderr, "Invalid --set-custom-type\n" "Valid custom types: fallback|motion|scroll\n"); return 1; } #endif break; case OPT_ROTATION_ANGLE: if (!optarg) return 1; if (!safe_atou(optarg, &options->angle)) { fprintf(stderr, "Invalid --set-rotation-angle value\n"); return 1; } } return 0; } static int open_restricted(const char *path, int flags, void *user_data) { bool *grab = user_data; int fd = open(path, flags); if (fd < 0) fprintf(stderr, "Failed to open %s (%s)\n", path, strerror(errno)); else if (grab && *grab && ioctl(fd, EVIOCGRAB, (void*)1) == -1) fprintf(stderr, "Grab requested, but failed for %s (%s)\n", path, strerror(errno)); return fd < 0 ? -errno : fd; } static void close_restricted(int fd, void *user_data) { close(fd); } static const struct libinput_interface interface = { .open_restricted = open_restricted, .close_restricted = close_restricted, }; static struct libinput * tools_open_udev(const char *seat, bool verbose, bool *grab) { struct libinput *li; struct udev *udev = udev_new(); if (!udev) { fprintf(stderr, "Failed to initialize udev\n"); return NULL; } li = libinput_udev_create_context(&interface, grab, udev); if (!li) { fprintf(stderr, "Failed to initialize context from udev\n"); goto out; } libinput_log_set_handler(li, log_handler); if (verbose) libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG); if (libinput_udev_assign_seat(li, seat)) { fprintf(stderr, "Failed to set seat\n"); libinput_unref(li); li = NULL; goto out; } out: udev_unref(udev); return li; } static struct libinput * tools_open_device(const char **paths, bool verbose, bool *grab) { struct libinput_device *device; struct libinput *li; const char **p = paths; li = libinput_path_create_context(&interface, grab); if (!li) { fprintf(stderr, "Failed to initialize path context\n"); return NULL; } if (verbose) { libinput_log_set_handler(li, log_handler); libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG); } while (*p) { device = libinput_path_add_device(li, *p); if (!device) { fprintf(stderr, "Failed to initialize device %s\n", *p); libinput_unref(li); li = NULL; break; } p++; } return li; } struct libinput * tools_open_backend(enum tools_backend which, const char **seat_or_device, bool verbose, bool *grab) { struct libinput *li; switch (which) { case BACKEND_UDEV: li = tools_open_udev(seat_or_device[0], verbose, grab); break; case BACKEND_DEVICE: li = tools_open_device(seat_or_device, verbose, grab); break; default: abort(); } return li; } void tools_device_apply_config(struct libinput_device *device, struct tools_options *options) { const char *name = libinput_device_get_name(device); if (libinput_device_config_send_events_get_modes(device) & LIBINPUT_CONFIG_SEND_EVENTS_DISABLED && fnmatch(options->disable_pattern, name, 0) != FNM_NOMATCH) { libinput_device_config_send_events_set_mode(device, LIBINPUT_CONFIG_SEND_EVENTS_DISABLED); } if (strlen(options->match) > 0 && fnmatch(options->match, name, 0) == FNM_NOMATCH) return; if (options->tapping != -1) libinput_device_config_tap_set_enabled(device, options->tapping); if (options->tap_map != (enum libinput_config_tap_button_map)-1) libinput_device_config_tap_set_button_map(device, options->tap_map); if (options->drag != -1) libinput_device_config_tap_set_drag_enabled(device, options->drag); if (options->drag_lock != -1) libinput_device_config_tap_set_drag_lock_enabled(device, options->drag_lock); if (options->natural_scroll != -1) libinput_device_config_scroll_set_natural_scroll_enabled(device, options->natural_scroll); if (options->left_handed != -1) libinput_device_config_left_handed_set(device, options->left_handed); if (options->middlebutton != -1) libinput_device_config_middle_emulation_set_enabled(device, options->middlebutton); if (options->dwt != -1) libinput_device_config_dwt_set_enabled(device, options->dwt); #ifdef HAVE_LIBINPUT_1_21 if (options->dwtp != -1) libinput_device_config_dwtp_set_enabled(device, options->dwtp); #endif if (options->click_method != (enum libinput_config_click_method)-1) libinput_device_config_click_set_method(device, options->click_method); #ifdef HAVE_LIBINPUT_1_26 if (options->clickfinger_map != (enum libinput_config_clickfinger_button_map)-1) libinput_device_config_click_set_clickfinger_button_map(device, options->clickfinger_map); #endif if (options->scroll_method != (enum libinput_config_scroll_method)-1) libinput_device_config_scroll_set_method(device, options->scroll_method); if (options->scroll_button != -1) libinput_device_config_scroll_set_button(device, options->scroll_button); #ifdef HAVE_LIBINPUT_1_15 if (options->scroll_button_lock != -1) libinput_device_config_scroll_set_button_lock(device, options->scroll_button_lock); #endif if (libinput_device_config_accel_is_available(device)) { libinput_device_config_accel_set_speed(device, options->speed); if (options->profile != LIBINPUT_CONFIG_ACCEL_PROFILE_NONE) libinput_device_config_accel_set_profile(device, options->profile); } #ifdef HAVE_LIBINPUT_1_23 if (options->profile == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) { struct libinput_config_accel *config = libinput_config_accel_create(LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM); libinput_config_accel_set_points(config, options->custom_type, options->custom_step, options->custom_npoints, options->custom_points); libinput_device_config_accel_apply(device, config); libinput_config_accel_destroy(config); } #endif if (options->angle != 0) libinput_device_config_rotation_set_angle(device, options->angle % 360); } static char* find_device(const char *udev_tag) { struct udev *udev; struct udev_enumerate *e = NULL; struct udev_list_entry *entry = NULL; struct udev_device *device; const char *path, *sysname; char *device_node = NULL; udev = udev_new(); if (!udev) goto out; e = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(e, "input"); udev_enumerate_scan_devices(e); udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { path = udev_list_entry_get_name(entry); device = udev_device_new_from_syspath(udev, path); if (!device) continue; sysname = udev_device_get_sysname(device); if (!strneq("event", sysname, 5)) { udev_device_unref(device); continue; } if (udev_device_get_property_value(device, udev_tag)) device_node = safe_strdup(udev_device_get_devnode(device)); udev_device_unref(device); if (device_node) break; } out: udev_enumerate_unref(e); udev_unref(udev); return device_node; } bool find_touchpad_device(char *path, size_t path_len) { char *devnode = find_device("ID_INPUT_TOUCHPAD"); if (devnode) { snprintf(path, path_len, "%s", devnode); free(devnode); } return devnode != NULL; } bool is_touchpad_device(const char *devnode) { struct udev *udev; struct udev_device *dev = NULL; struct stat st; bool is_touchpad = false; if (stat(devnode, &st) < 0) return false; udev = udev_new(); if (!udev) goto out; dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev); if (!dev) goto out; is_touchpad = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"); out: if (dev) udev_device_unref(dev); udev_unref(udev); return is_touchpad; }