From 24f04225883597f9c01d6b26c91a298a8a91ff67 Mon Sep 17 00:00:00 2001 From: Valeri Date: Fri, 15 Sep 2023 21:27:47 +0300 Subject: [PATCH] Warn if opening a device failed (#12) --- src/Server.vala | 25 ++++++++++++++++++++++++- vapi/libudev.vapi | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Server.vala b/src/Server.vala index f637c3c..5de0745 100644 --- a/src/Server.vala +++ b/src/Server.vala @@ -86,7 +86,30 @@ namespace Evdevhook { IOChannel iochan; { int fd = Posix.open(udevdev.devnode, Posix.O_RDONLY | Posix.O_NONBLOCK); - if (fd == -1) return; + var saved_errno = errno; + if (fd == -1) { + string? devname = null; + + // Search up the device tree until name is found or we leave input subsystem + for(unowned var? parent = udevdev; + parent != null && devname == null; + parent = parent.get_parent_with_subsystem_devtype("input", null) + ) { + devname = parent.get_sysattr_value("name"); + } + + if (devname != null) { + printerr("\nFailed to open %s (%s) - %s\n", devname, udevdev.devnode, strerror(saved_errno)); + } else { + printerr("\nFailed to open %s - %s\n", udevdev.devnode, strerror(saved_errno)); + } + + if (saved_errno == Posix.EACCES) { + printerr("The recommended way to fix this is by creating an appropriate udev rule and/or adding your user to a correct group.\n"); + printerr("Running as root also works, but is not advised.\n"); + } + return; + } iochan = new IOChannel.unix_new(fd); iochan.set_close_on_unref(true); diff --git a/vapi/libudev.vapi b/vapi/libudev.vapi index 222617b..59e7323 100644 --- a/vapi/libudev.vapi +++ b/vapi/libudev.vapi @@ -36,6 +36,8 @@ namespace Udev { public ListEntry? tags_list_entry { get; } public unowned string? get_property_value(string key); + public unowned string? get_sysattr_value(string key); + public unowned Device? get_parent_with_subsystem_devtype(string? subsystem, string? devtype); } [CCode (cname = "struct udev_enumerate", ref_function = "udev_enumerate_ref", unref_function = "udev_enumerate_unref")]