From 0ecd08c13439cf4f2fe4e251a9f7a5e27468efb2 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 1 Apr 2025 13:42:27 +1000 Subject: [PATCH] tools: use __attribute__(cleanup) Part-of: --- tools/libinput-debug-events.c | 3 +- tools/libinput-quirks.c | 56 ++++------ tools/libinput-record.c | 205 +++++++++++++--------------------- tools/ptraccel-debug.c | 8 +- tools/shared.c | 118 ++++++------------- 5 files changed, 140 insertions(+), 250 deletions(-) diff --git a/tools/libinput-debug-events.c b/tools/libinput-debug-events.c index c25005a5..a6d7a69d 100644 --- a/tools/libinput-debug-events.c +++ b/tools/libinput-debug-events.c @@ -100,7 +100,7 @@ handle_and_print_events(struct libinput *li, const struct libinput_print_options } if (type != LIBINPUT_EVENT_TOUCH_FRAME || !compress_motion_events) { - char *event_str = libinput_event_to_str(ev, event_repeat_count + 1, opts); + _autofree_ char *event_str = libinput_event_to_str(ev, event_repeat_count + 1, opts); switch (type) { case LIBINPUT_EVENT_DEVICE_ADDED: @@ -120,7 +120,6 @@ handle_and_print_events(struct libinput *li, const struct libinput_print_options } printq("%s\n", event_str); - free(event_str); } last_device = device; diff --git a/tools/libinput-quirks.c b/tools/libinput-quirks.c index a41fc3b5..21f2f49f 100644 --- a/tools/libinput-quirks.c +++ b/tools/libinput-quirks.c @@ -32,6 +32,8 @@ #include "quirks.h" #include "shared.h" #include "builddir.h" +#include "util-mem.h" +#include "libinput-util.h" static bool verbose = false; @@ -94,13 +96,8 @@ simple_printf(void *userdata, const char *val) int main(int argc, char **argv) { - struct udev *udev = NULL; - struct udev_device *device = NULL; - const char *path; const char *data_path = NULL, *override_file = NULL; - int rc = 1; - struct quirks_context *quirks; bool validate = false; while (1) { @@ -137,31 +134,31 @@ main(int argc, char **argv) break; default: usage(); - return 1; + return EXIT_FAILURE; } } if (optind >= argc) { usage(); - return 1; + return EXIT_FAILURE; } if (streq(argv[optind], "list")) { optind++; if (optind >= argc) { usage(); - return 1; + return EXIT_FAILURE; } } else if (streq(argv[optind], "validate")) { optind++; if (optind < argc) { usage(); - return 1; + return EXIT_FAILURE; } validate = true; } else { fprintf(stderr, "Unnkown action '%s'\n", argv[optind]); - return 1; + return EXIT_FAILURE; } /* Overriding the data dir means no custom override file */ @@ -174,53 +171,44 @@ main(int argc, char **argv) } } - quirks = quirks_init_subsystem(data_path, - override_file, - log_handler, - NULL, - QLOG_CUSTOM_LOG_PRIORITIES); + _unref_(quirks_context) *quirks = quirks_init_subsystem(data_path, + override_file, + log_handler, + NULL, + QLOG_CUSTOM_LOG_PRIORITIES); if (!quirks) { fprintf(stderr, "Failed to initialize the device quirks. " "Please see the above errors " "and/or re-run with --verbose for more details\n"); - return 1; + return EXIT_FAILURE; } - if (validate) { - rc = 0; - goto out; - } + if (validate) + return EXIT_SUCCESS; - udev = udev_new(); + _unref_(udev) *udev = udev_new(); if (!udev) - goto out; + return EXIT_FAILURE; - path = argv[optind]; + _unref_(udev_device) *device = NULL; + const char *path = argv[optind]; if (strstartswith(path, "/sys/")) { device = udev_device_new_from_syspath(udev, path); } else { struct stat st; if (stat(path, &st) < 0) { fprintf(stderr, "Error: %s: %m\n", path); - goto out; + return EXIT_FAILURE; } device = udev_device_new_from_devnum(udev, 'c', st.st_rdev); } if (device) { tools_list_device_quirks(quirks, device, simple_printf, NULL); - rc = 0; + return EXIT_SUCCESS; } else { usage(); - rc = 1; + return EXIT_FAILURE; } - - udev_device_unref(device); -out: - udev_unref(udev); - - quirks_context_unref(quirks); - - return rc; } diff --git a/tools/libinput-record.c b/tools/libinput-record.c index cbb5b4cb..9ee4fbe7 100644 --- a/tools/libinput-record.c +++ b/tools/libinput-record.c @@ -49,11 +49,14 @@ #include "shared.h" #include "builddir.h" #include "util-bits.h" +#include "util-files.h" #include "util-list.h" #include "util-time.h" #include "util-input-event.h" #include "util-macros.h" +#include "util-mem.h" #include "util-strings.h" +#include "libinput-util.h" static const int FILE_VERSION_NUMBER = 1; @@ -785,28 +788,26 @@ buffer_tablet_axes(struct libinput_event_tablet_tool *t) { const int MAX_AXES = 10; struct libinput_tablet_tool *tool; - char *s = NULL; int idx = 0; int len; double x, y; - char **strv; tool = libinput_event_tablet_tool_get_tool(t); - strv = zalloc(MAX_AXES * sizeof *strv); + _autostrvfree_ char **strv = zalloc(MAX_AXES * sizeof *strv); x = libinput_event_tablet_tool_get_x(t); y = libinput_event_tablet_tool_get_y(t); len = xasprintf(&strv[idx++], "point: [%.2f, %.2f]", x, y); if (len <= 0) - goto out; + return NULL; if (libinput_tablet_tool_has_tilt(tool)) { x = libinput_event_tablet_tool_get_tilt_x(t); y = libinput_event_tablet_tool_get_tilt_y(t); len = xasprintf(&strv[idx++], "tilt: [%.2f, %.2f]", x, y); if (len <= 0) - goto out; + return NULL; } if (libinput_tablet_tool_has_distance(tool) || @@ -820,7 +821,7 @@ buffer_tablet_axes(struct libinput_event_tablet_tool *t) else len = xasprintf(&strv[idx++], "pressure: %.2f", pressure); if (len <= 0) - goto out; + return NULL; } if (libinput_tablet_tool_has_rotation(tool)) { @@ -829,7 +830,7 @@ buffer_tablet_axes(struct libinput_event_tablet_tool *t) rotation = libinput_event_tablet_tool_get_rotation(t); len = xasprintf(&strv[idx++], "rotation: %.2f", rotation); if (len <= 0) - goto out; + return NULL; } if (libinput_tablet_tool_has_slider(tool)) { @@ -838,8 +839,7 @@ buffer_tablet_axes(struct libinput_event_tablet_tool *t) slider = libinput_event_tablet_tool_get_slider_position(t); len = xasprintf(&strv[idx++], "slider: %.2f", slider); if (len <= 0) - goto out; - + return NULL; } if (libinput_tablet_tool_has_wheel(tool)) { @@ -849,20 +849,17 @@ buffer_tablet_axes(struct libinput_event_tablet_tool *t) wheel = libinput_event_tablet_tool_get_wheel_delta(t); len = xasprintf(&strv[idx++], "wheel: %.2f", wheel); if (len <= 0) - goto out; + return NULL; delta = libinput_event_tablet_tool_get_wheel_delta_discrete(t); len = xasprintf(&strv[idx++], "wheel-discrete: %d", delta); if (len <= 0) - goto out; + return NULL; } assert(idx < MAX_AXES); - s = strv_join(strv, ", "); -out: - strv_free(strv); - return s; + return strv_join(strv, ", "); } static void @@ -874,7 +871,6 @@ print_tablet_tool_proximity_event(struct record_device *dev, struct libinput_eve libinput_event_tablet_tool_get_tool(t); uint64_t time; const char *type, *tool_type; - char *axes; char caps[10] = {0}; enum libinput_tablet_tool_proximity_state prox; size_t idx; @@ -916,7 +912,7 @@ print_tablet_tool_proximity_event(struct record_device *dev, struct libinput_eve prox = libinput_event_tablet_tool_get_proximity_state(t); time = time_offset(dev->ctx, libinput_event_tablet_tool_get_time_usec(t)); - axes = buffer_tablet_axes(t); + _autofree_ char *axes = buffer_tablet_axes(t); idx = 0; if (libinput_tablet_tool_has_pressure(tool)) @@ -944,7 +940,6 @@ print_tablet_tool_proximity_event(struct record_device *dev, struct libinput_eve libinput_tablet_tool_get_serial(tool), caps, axes); - free(axes); } static void @@ -987,7 +982,6 @@ print_tablet_tool_event(struct record_device *dev, struct libinput_event *e) libinput_event_get_tablet_tool_event(e); uint64_t time; const char *type; - char *axes; enum libinput_tablet_tool_tip_state tip; char btn_buffer[30] = {0}; @@ -1019,7 +1013,7 @@ print_tablet_tool_event(struct record_device *dev, struct libinput_event *e) tip = libinput_event_tablet_tool_get_tip_state(t); time = time_offset(dev->ctx, libinput_event_tablet_tool_get_time_usec(t)); - axes = buffer_tablet_axes(t); + _autofree_ char *axes = buffer_tablet_axes(t); iprintf(dev->fp, I_EVENT, @@ -1030,7 +1024,6 @@ print_tablet_tool_event(struct record_device *dev, struct libinput_event *e) btn_buffer, /* may be empty string */ tip ? "down" : "up", axes); - free(axes); } static void @@ -1349,17 +1342,15 @@ print_system_header(FILE *fp) { struct utsname u; const char *kernel = "unknown"; - FILE *dmi, *osrelease; - char dmistr[2048] = "unknown"; iprintf(fp, I_TOPLEVEL, "system:\n"); /* /etc/os-release version and distribution name */ - osrelease = fopen("/etc/os-release", "r"); + _autofclose_ FILE *osrelease = fopen("/etc/os-release", "r"); if (!osrelease) osrelease = fopen("/usr/lib/os-release", "r"); if (osrelease) { - char *distro = NULL, *version = NULL; + _autofree_ char *distro = NULL, *version = NULL; char osrstr[256] = "unknown"; while (fgets(osrstr, sizeof(osrstr), osrelease)) { @@ -1370,18 +1361,17 @@ print_system_header(FILE *fp) else if (!version && strstartswith(osrstr, "VERSION_ID=")) version = strstrip(&osrstr[11], "\"'"); - if (distro && version) { - iprintf(fp, - I_SYSTEM, - "os: \"%s:%s\"\n", - distro, - version); + if (distro && version) break; - } } - free(distro); - free(version); - fclose(osrelease); + + if (distro && version) { // NOLINT: unix.Stream + iprintf(fp, + I_SYSTEM, + "os: \"%s:%s\"\n", + distro, + version); + } } /* kernel version */ @@ -1390,14 +1380,15 @@ print_system_header(FILE *fp) iprintf(fp, I_SYSTEM, "kernel: \"%s\"\n", kernel); /* dmi modalias */ - dmi = fopen("/sys/class/dmi/id/modalias", "r"); + _autofclose_ FILE *dmi = fopen("/sys/class/dmi/id/modalias", "r"); + _autofree_ char *dmistr = strdup("unknown"); // if (dmi) { - if (fgets(dmistr, sizeof(dmistr), dmi)) { - dmistr[strlen(dmistr) - 1] = '\0'; /* linebreak */ - } else { - sprintf(dmistr, "unknown"); + char buf[2048] = "unknown"; + size_t n = fread(buf, sizeof(buf), 1, dmi); // NOLINT: unix.Stream + if (n > 0) { + free(dmistr); + dmistr = strndup(buf, n - 1); } - fclose(dmi); } iprintf(fp, I_SYSTEM, "dmi: \"%s\"\n", dmistr); } @@ -1720,27 +1711,24 @@ print_hid_report_descriptor(struct record_device *dev) static void print_udev_properties(struct record_device *dev) { - struct udev *udev = NULL; - struct udev_device *udev_device = NULL; - struct udev_list_entry *entry; struct stat st; if (stat(dev->devnode, &st) < 0) return; - udev = udev_new(); + _unref_(udev) *udev = udev_new(); if (!udev) - goto out; + return; - udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev); + _unref_(udev_device) *udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev); if (!udev_device) - goto out; + return; iprintf(dev->fp, I_DEVICE, "udev:\n"); iprintf(dev->fp, I_UDEV, "properties:\n"); - entry = udev_device_get_properties_list_entry(udev_device); + struct udev_list_entry *entry = udev_device_get_properties_list_entry(udev_device); while (entry) { const char *key, *value; @@ -1768,10 +1756,6 @@ print_udev_properties(struct record_device *dev) break; } } - -out: - udev_device_unref(udev_device); - udev_unref(udev); } static void @@ -1793,10 +1777,7 @@ list_print(void *userdata, const char *val) static void print_device_quirks(struct record_device *dev) { - struct udev *udev = NULL; - struct udev_device *udev_device = NULL; struct stat st; - struct quirks_context *quirks; const char *data_path = LIBINPUT_QUIRKS_DIR; const char *override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE; @@ -1809,11 +1790,11 @@ print_device_quirks(struct record_device *dev) override_file = NULL; } - quirks = quirks_init_subsystem(data_path, - override_file, - quirks_log_handler, - NULL, - QLOG_CUSTOM_LOG_PRIORITIES); + _unref_(quirks_context) *quirks = quirks_init_subsystem(data_path, + override_file, + quirks_log_handler, + NULL, + QLOG_CUSTOM_LOG_PRIORITIES); if (!quirks) { fprintf(stderr, "Failed to load the device quirks from %s%s%s. " @@ -1826,20 +1807,15 @@ print_device_quirks(struct record_device *dev) return; } - udev = udev_new(); + _unref_(udev) *udev = udev_new(); if (!udev) - goto out; + return; - udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev); - if (!udev_device) - goto out; - - iprintf(dev->fp, I_DEVICE, "quirks:\n"); - tools_list_device_quirks(quirks, udev_device, list_print, dev->fp); -out: - udev_device_unref(udev_device); - udev_unref(udev); - quirks_context_unref(quirks); + _unref_(udev_device) *udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev); + if (udev_device) { + iprintf(dev->fp, I_DEVICE, "quirks:\n"); + tools_list_device_quirks(quirks, udev_device, list_print, dev->fp); + } } static void @@ -1903,7 +1879,7 @@ static int is_event_node(const struct dirent *dir) { static char * select_device(void) { - struct dirent **namelist; + _autofree_ struct dirent **namelist; int ndev, selected_device; int rc; char *device_path; @@ -1920,23 +1896,19 @@ select_device(void) fprintf(stderr, "%sAvailable devices:\n", prefix); for (int i = 0; i < ndev; i++) { - struct libevdev *device; + _autofree_ struct dirent *entry = namelist[i]; char path[PATH_MAX]; - int fd = -1; - snprintf(path, - sizeof(path), - "/dev/input/%s", - namelist[i]->d_name); - fd = open(path, O_RDONLY); + snprintf(path, sizeof(path), "/dev/input/%s", entry->d_name); + _cleanup_(xclose) int fd = open(path, O_RDONLY); if (fd < 0) { if (errno == EACCES) has_eaccess = true; continue; } + struct libevdev *device; rc = libevdev_new_from_fd(fd, &device); - close(fd); if (rc != 0) continue; @@ -1945,10 +1917,6 @@ select_device(void) available_devices++; } - for (int i = 0; i < ndev; i++) - free(namelist[i]); - free(namelist); - if (available_devices == 0) { fprintf(stderr, "No devices available.%s\n", @@ -1972,9 +1940,9 @@ select_device(void) static char ** all_devices(void) { - struct dirent **namelist; + _autofree_ struct dirent **namelist; int ndev; - char **devices = NULL; + _autostrvfree_ char **devices = NULL; ndev = scandir("/dev/input", &namelist, is_event_node, versionsort); if (ndev <= 0) @@ -1982,26 +1950,17 @@ all_devices(void) devices = zalloc((ndev + 1)* sizeof *devices); /* NULL-terminated */ for (int i = 0; i < ndev; i++) { + _autofree_ struct dirent *entry = namelist[i]; char *device_path; - int rc = xasprintf(&device_path, - "/dev/input/%s", - namelist[i]->d_name); + int rc = xasprintf(&device_path, "/dev/input/%s", entry->d_name); if (rc == -1) - goto error; + return NULL; devices[i] = device_path; } - return devices; - -error: - for (int i = 0; i < ndev; i++) - free(namelist[i]); - free(namelist); - if (devices) - strv_free(devices); - return NULL; + return steal(&devices); } static char * @@ -2111,27 +2070,24 @@ add_source(struct record_context *ctx, source_dispatch_t dispatch, void *user_data) { - struct source *source; - struct epoll_event ep; - assert(fd != -1); - source = zalloc(sizeof *source); + _autofree_ struct source *source = zalloc(sizeof *source); source->dispatch = dispatch; source->user_data = user_data; source->fd = fd; - list_append(&ctx->sources, &source->link); + struct epoll_event ep; memset(&ep, 0, sizeof ep); ep.events = EPOLLIN; ep.data.ptr = source; if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, fd, &ep) < 0) { - free(source); return NULL; } - return source; + list_append(&ctx->sources, &source->link); + return steal(&source); } static void @@ -2408,24 +2364,21 @@ mainloop(struct record_context *ctx) static bool init_device(struct record_context *ctx, const char *path, bool grab) { - struct record_device *d; - int fd, rc; - - d = zalloc(sizeof(*d)); + _autofree_ struct record_device *d = zalloc(sizeof(*d)); d->ctx = ctx; d->devnode = safe_strdup(path); list_init(&d->hidraw_devices); - fd = open(d->devnode, O_RDONLY|O_NONBLOCK); + _cleanup_(xclose) int fd = open(d->devnode, O_RDONLY|O_NONBLOCK); if (fd < 0) { fprintf(stderr, "Failed to open device %s (%m)\n", d->devnode); - goto error; + return false; } - rc = libevdev_new_from_fd(fd, &d->evdev); + int rc = libevdev_new_from_fd(fd, &d->evdev); if (rc == 0) rc = libevdev_new_from_fd(fd, &d->evdev_prev); if (rc != 0) { @@ -2433,7 +2386,7 @@ init_device(struct record_context *ctx, const char *path, bool grab) "Failed to create context for %s (%s)\n", d->devnode, strerror(-rc)); - goto error; + return false; } if (grab) { @@ -2443,7 +2396,7 @@ init_device(struct record_context *ctx, const char *path, bool grab) "Grab failed on %s: %s\n", path, strerror(-rc)); - goto error; + return false; } } @@ -2452,18 +2405,17 @@ init_device(struct record_context *ctx, const char *path, bool grab) if (libevdev_get_num_slots(d->evdev) > 0) d->touch.is_touch_device = true; - list_append(&ctx->devices, &d->link); if (!ctx->first_device) ctx->first_device = d; + list_take_append(&ctx->devices, d, link); ctx->ndevices++; - return true; -error: - close(fd); - free(d); - return false; + // Shut up clang-tidy + steal_fd(&fd); + return true; } + static int open_restricted(const char *path, int flags, void *user_data) { @@ -2709,7 +2661,7 @@ main(int argc, char **argv) grab = false; int ndevices; int rc = EXIT_FAILURE; - char **paths = NULL; + _autostrvfree_ char **paths = NULL; list_init(&ctx.devices); list_init(&ctx.sources); @@ -2813,13 +2765,11 @@ main(int argc, char **argv) } else if (ndevices >= 1) { paths = strv_from_argv(ndevices, &argv[optind]); } else { - char *path = select_device(); + _autofree_ char *path = select_device(); if (path == NULL) { goto out; } - paths = strv_from_argv(1, &path); - free(path); } for (char **p = paths; *p; p++) { @@ -2836,7 +2786,6 @@ main(int argc, char **argv) rc = mainloop(&ctx); out: - strv_free(paths); list_for_each_safe(d, &ctx.devices, link) { struct hidraw *hidraw; diff --git a/tools/ptraccel-debug.c b/tools/ptraccel-debug.c index c8080980..2ed6ecfe 100644 --- a/tools/ptraccel-debug.c +++ b/tools/ptraccel-debug.c @@ -324,9 +324,9 @@ main(int argc, char **argv) break; case OPT_CUSTOM_POINTS: { size_t npoints; - double *points = double_array_from_string(optarg, - ";", - &npoints); + _autofree_ double *points = double_array_from_string(optarg, + ";", + &npoints); if (!points || npoints < LIBINPUT_ACCEL_NPOINTS_MIN || npoints > LIBINPUT_ACCEL_NPOINTS_MAX) { @@ -334,14 +334,12 @@ main(int argc, char **argv) "Invalid --custom-points\n" "Please provide at least 2 points separated by a semicolon\n" " e.g. --custom-points=\"1.0;1.5\"\n"); - free(points); return 1; } custom_func.npoints = npoints; memcpy(custom_func.points, points, sizeof(*points) * npoints); - free(points); break; } case OPT_CUSTOM_STEP: diff --git a/tools/shared.c b/tools/shared.c index 22c522e8..b0d382cb 100644 --- a/tools/shared.c +++ b/tools/shared.c @@ -40,6 +40,7 @@ #include "builddir.h" #include "libinput.h" +#include "libinput-util.h" #include "shared.h" #include "util-macros.h" #include "util-strings.h" @@ -381,15 +382,13 @@ tools_parse_option(int option, return 1; size_t npoints = 0; - double *range = double_array_from_string(optarg, ":", &npoints); + _autofree_ double *range = double_array_from_string(optarg, ":", &npoints); if (npoints != 2 || !range || range[0] < 0.0 || range[1] > 1.0 || range[0] >= range[1]) { - free(range); fprintf(stderr, "Invalid pressure range, must be in format \"min:max\"\n"); return 1; } options->pressure_range[0] = range[0]; options->pressure_range[1] = range[1]; - free(range); break; } case OPT_CALIBRATION: { @@ -397,15 +396,13 @@ tools_parse_option(int option, return 1; size_t npoints = 0; - double *matrix = double_array_from_string(optarg, " ", &npoints); + _autofree_ double *matrix = double_array_from_string(optarg, " ", &npoints); if (!matrix || npoints != 6) { - free(matrix); fprintf(stderr, "Invalid calibration matrix, must be 6 space-separated values\n"); return 1; } for (size_t i = 0; i < 6; i++) options->calibration[i] = matrix[i]; - free(matrix); break; } case OPT_AREA: { @@ -473,18 +470,16 @@ static const struct libinput_interface interface = { static struct libinput * tools_open_udev(const char *seat, bool verbose, bool *grab) { - struct libinput *li; - struct udev *udev = udev_new(); - + _unref_(udev) *udev = udev_new(); if (!udev) { fprintf(stderr, "Failed to initialize udev\n"); return NULL; } - li = libinput_udev_create_context(&interface, grab, udev); + _unref_(libinput) *li = libinput_udev_create_context(&interface, grab, udev); if (!li) { fprintf(stderr, "Failed to initialize context from udev\n"); - goto out; + return NULL; } libinput_log_set_handler(li, log_handler); @@ -493,24 +488,16 @@ tools_open_udev(const char *seat, bool verbose, bool *grab) if (libinput_udev_assign_seat(li, seat)) { fprintf(stderr, "Failed to set seat\n"); - libinput_unref(li); - li = NULL; - goto out; + return NULL; } -out: - udev_unref(udev); - return li; + return steal(&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); + _unref_(libinput) *li = libinput_path_create_context(&interface, grab); if (!li) { fprintf(stderr, "Failed to initialize path context\n"); return NULL; @@ -521,26 +508,24 @@ tools_open_device(const char **paths, bool verbose, bool *grab) libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG); } + const char **p = paths; while (*p) { - device = libinput_path_add_device(li, *p); + struct libinput_device *device = libinput_path_add_device(li, *p); if (!device) { fprintf(stderr, "Failed to initialize device %s\n", *p); - libinput_unref(li); - li = NULL; - break; + return NULL; } p++; } - return li; + return steal(&li); } static void tools_setenv_quirks_dir(void) { - if (builddir_lookup(NULL)) { + if (builddir_lookup(NULL)) setenv("LIBINPUT_QUIRKS_DIR", LIBINPUT_QUIRKS_SRCDIR, 0); - } } struct libinput * @@ -638,7 +623,7 @@ tools_device_apply_config(struct libinput_device *device, } if (options->profile == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) { - struct libinput_config_accel *config = + _destroy_(libinput_config_accel) *config = libinput_config_accel_create(LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM); libinput_config_accel_set_points(config, options->custom_type, @@ -646,7 +631,6 @@ tools_device_apply_config(struct libinput_device *device, options->custom_npoints, options->custom_points); libinput_device_config_accel_apply(device, config); - libinput_config_accel_destroy(config); } if (options->angle != 0) @@ -674,57 +658,43 @@ tools_tablet_tool_apply_config(struct libinput_tablet_tool *tool, 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(); + _unref_(udev) *udev = udev_new(); if (!udev) - goto out; + return NULL; - e = udev_enumerate_new(udev); + _unref_(udev_enumerate) *e = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(e, "input"); udev_enumerate_scan_devices(e); + struct udev_list_entry *entry = NULL; 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); + const char *path = udev_list_entry_get_name(entry); + _unref_(udev_device) *device = udev_device_new_from_syspath(udev, path); if (!device) continue; - sysname = udev_device_get_sysname(device); + const char *sysname = udev_device_get_sysname(device); if (!strstartswith("event", sysname)) { - 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; + if (udev_device_get_property_value(device, udev_tag)) { + char *device_node = safe_strdup(udev_device_get_devnode(device)); + if (device_node) + return device_node; + } } -out: - udev_enumerate_unref(e); - udev_unref(udev); - return device_node; + return NULL; } bool find_touchpad_device(char *path, size_t path_len) { - char *devnode = find_device("ID_INPUT_TOUCHPAD"); + _autofree_ char *devnode = find_device("ID_INPUT_TOUCHPAD"); - if (devnode) { + if (devnode) snprintf(path, path_len, "%s", devnode); - free(devnode); - } return devnode != NULL; } @@ -732,29 +702,19 @@ find_touchpad_device(char *path, size_t path_len) 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(); + _unref_(udev) *udev = udev_new(); if (!udev) - goto out; + return false; - dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev); + _unref_(udev_device) *dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev); if (!dev) - goto out; + return false; - is_touchpad = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"); -out: - if (dev) - udev_device_unref(dev); - udev_unref(udev); - - return is_touchpad; + return udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD"); } static inline void @@ -763,7 +723,7 @@ setup_path(void) const char *path = getenv("PATH"); char new_path[PATH_MAX]; const char *extra_path = LIBINPUT_TOOL_PATH; - char *builddir = NULL; + _autofree_ char *builddir = NULL; builddir_lookup(&builddir); snprintf(new_path, @@ -772,7 +732,6 @@ setup_path(void) builddir ? builddir : extra_path, path ? path : ""); setenv("PATH", new_path, 1); - free(builddir); } int @@ -881,10 +840,9 @@ tools_list_device_quirks(struct quirks_context *ctx, { char buf[256]; - struct quirks *quirks; enum quirk q; - quirks = quirks_fetch_for_device(ctx, device); + _unref_(quirks) *quirks = quirks_fetch_for_device(ctx, device); if (!quirks) return; @@ -969,6 +927,4 @@ tools_list_device_quirks(struct quirks_context *ctx, } } } while(++q < _QUIRK_LAST_ATTR_QUIRK_); - - quirks_unref(quirks); }