Use _unref_ for the udev handling in path and udev seat implementations

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1184>
This commit is contained in:
Peter Hutterer 2025-04-07 15:39:01 +10:00
parent d72df04e7d
commit a19671d0da
2 changed files with 12 additions and 36 deletions

View file

@ -302,24 +302,22 @@ libinput_path_create_context(const struct libinput_interface *interface,
void *user_data) void *user_data)
{ {
struct path_input *input; struct path_input *input;
struct udev *udev;
if (!interface) if (!interface)
return NULL; return NULL;
udev = udev_new(); _unref_(udev) *udev = udev_new();
if (!udev) if (!udev)
return NULL; return NULL;
input = zalloc(sizeof *input); input = zalloc(sizeof *input);
if (libinput_init(&input->base, interface, if (libinput_init(&input->base, interface,
&interface_backend, user_data) != 0) { &interface_backend, user_data) != 0) {
udev_unref(udev);
free(input); free(input);
return NULL; return NULL;
} }
input->udev = udev; input->udev = udev_ref(udev);
list_init(&input->path_list); list_init(&input->path_list);
return &input->base; return &input->base;
@ -361,7 +359,6 @@ libinput_path_add_device(struct libinput *libinput,
{ {
struct path_input *input = (struct path_input *)libinput; struct path_input *input = (struct path_input *)libinput;
struct udev *udev = input->udev; struct udev *udev = input->udev;
struct udev_device *udev_device;
struct libinput_device *device; struct libinput_device *device;
if (strlen(path) > PATH_MAX) { if (strlen(path) > PATH_MAX) {
@ -376,14 +373,13 @@ libinput_path_add_device(struct libinput *libinput,
return NULL; return NULL;
} }
udev_device = udev_device_from_devnode(libinput, udev, path); _unref_(udev_device) *udev_device = udev_device_from_devnode(libinput, udev, path);
if (!udev_device) { if (!udev_device) {
log_bug_client(libinput, "Invalid path %s\n", path); log_bug_client(libinput, "Invalid path %s\n", path);
return NULL; return NULL;
} }
if (ignore_litest_test_suite_device(udev_device)) { if (ignore_litest_test_suite_device(udev_device)) {
udev_device_unref(udev_device);
return NULL; return NULL;
} }
@ -395,7 +391,6 @@ libinput_path_add_device(struct libinput *libinput,
libinput_init_quirks(libinput); libinput_init_quirks(libinput);
device = path_create_device(libinput, udev_device, NULL); device = path_create_device(libinput, udev_device, NULL);
udev_device_unref(udev_device);
return device; return device;
} }

View file

@ -54,16 +54,14 @@ filter_duplicates(struct udev_seat *udev_seat,
list_for_each(device, &udev_seat->base.devices_list, link) { list_for_each(device, &udev_seat->base.devices_list, link) {
const char *syspath; const char *syspath;
struct udev_device *ud;
ud = libinput_device_get_udev_device(device); _unref_(udev_device) *ud = libinput_device_get_udev_device(device);
if (!ud) if (!ud)
continue; continue;
syspath = udev_device_get_syspath(ud); syspath = udev_device_get_syspath(ud);
if (syspath && new_syspath && streq(syspath, new_syspath)) if (syspath && new_syspath && streq(syspath, new_syspath))
ignore_device = true; ignore_device = true;
udev_device_unref(ud);
if (ignore_device) if (ignore_device)
break; break;
@ -168,23 +166,19 @@ device_removed(struct udev_device *udev_device, struct udev_input *input)
static int static int
udev_input_add_devices(struct udev_input *input, struct udev *udev) udev_input_add_devices(struct udev_input *input, struct udev *udev)
{ {
struct udev_enumerate *e;
struct udev_list_entry *entry; struct udev_list_entry *entry;
struct udev_device *device;
const char *path, *sysname;
e = udev_enumerate_new(udev); _unref_(udev_enumerate) *e = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(e, "input"); udev_enumerate_add_match_subsystem(e, "input");
udev_enumerate_scan_devices(e); udev_enumerate_scan_devices(e);
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
path = udev_list_entry_get_name(entry); const char *path = udev_list_entry_get_name(entry);
device = udev_device_new_from_syspath(udev, path); _unref_(udev_device) *device = udev_device_new_from_syspath(udev, path);
if (!device) if (!device)
continue; continue;
sysname = udev_device_get_sysname(device); const char *sysname = udev_device_get_sysname(device);
if (!strstartswith(sysname, "event")) { if (!strstartswith(sysname, "event")) {
udev_device_unref(device);
continue; continue;
} }
@ -195,20 +189,13 @@ udev_input_add_devices(struct udev_input *input, struct udev *udev)
"%-7s - skip unconfigured input device '%s'\n", "%-7s - skip unconfigured input device '%s'\n",
sysname, sysname,
udev_device_get_devnode(device)); udev_device_get_devnode(device));
udev_device_unref(device);
continue; continue;
} }
if (device_added(device, input, NULL) < 0) { if (device_added(device, input, NULL) < 0) {
udev_device_unref(device);
udev_enumerate_unref(e);
return -1; return -1;
} }
udev_device_unref(device);
} }
udev_enumerate_unref(e);
return 0; return 0;
} }
@ -216,27 +203,21 @@ static void
evdev_udev_handler(void *data) evdev_udev_handler(void *data)
{ {
struct udev_input *input = data; struct udev_input *input = data;
struct udev_device *udev_device;
const char *action; const char *action;
udev_device = udev_monitor_receive_device(input->udev_monitor); _unref_(udev_device) *udev_device = udev_monitor_receive_device(input->udev_monitor);
if (!udev_device) if (!udev_device)
return; return;
action = udev_device_get_action(udev_device); action = udev_device_get_action(udev_device);
if (!action) if (!action ||
goto out; !strstartswith(udev_device_get_sysname(udev_device), "event"))
return;
if (!strstartswith(udev_device_get_sysname(udev_device), "event"))
goto out;
if (streq(action, "add")) if (streq(action, "add"))
device_added(udev_device, input, NULL); device_added(udev_device, input, NULL);
else if (streq(action, "remove")) else if (streq(action, "remove"))
device_removed(udev_device, input); device_removed(udev_device, input);
out:
udev_device_unref(udev_device);
} }
static void static void