From 84d8944f7569bf9e2e8bd7f77a7350630ed51655 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 5 May 2019 01:10:05 +0200 Subject: proto/libevdev: fix wrong error check Issue: #944 --- proto-libevdev/ftnoir_protocol_libevdev.cpp | 58 ++++++++++++++++++----------- proto-libevdev/ftnoir_protocol_libevdev.h | 6 ++- 2 files changed, 41 insertions(+), 23 deletions(-) (limited to 'proto-libevdev') diff --git a/proto-libevdev/ftnoir_protocol_libevdev.cpp b/proto-libevdev/ftnoir_protocol_libevdev.cpp index 4536204d..ba3c3fcb 100644 --- a/proto-libevdev/ftnoir_protocol_libevdev.cpp +++ b/proto-libevdev/ftnoir_protocol_libevdev.cpp @@ -2,31 +2,44 @@ #include "api/plugin-api.hpp" #include "compat/math.hpp" -#include +#include +#include +#include +#include + #include #include #include #include #include -#include -#include +#include -#define CHECK_LIBEVDEV(expr) if ((error = (expr)) != 0) goto error; +#define CHECK_LIBEVDEV(expr) \ + do { \ + if ((int error = (expr)); error != 0) \ + { \ + error_code = -error; \ + error_str = #expr; \ + goto fail; \ + } \ + } while (false) -static const int max_input = 65535; -static const int mid_input = 32767; -static const int min_input = 0; +static constexpr int max_input = 1 << 16; +static constexpr int mid_input = (1 << 15) - 1; +static constexpr int min_input = 0; -evdev::evdev() : dev(NULL), uidev(NULL) +evdev::evdev() { - int error = 0; - dev = libevdev_new(); if (!dev) - goto error; + { + error_code = errno; + error_str = "libevdev_new();"; + goto fail; + } CHECK_LIBEVDEV(libevdev_enable_property(dev, INPUT_PROP_BUTTONPAD)); @@ -57,15 +70,17 @@ evdev::evdev() : dev(NULL), uidev(NULL) CHECK_LIBEVDEV(libevdev_uinput_create_from_device(dev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)); return; -error: + +fail: if (uidev) libevdev_uinput_destroy(uidev); if (dev) libevdev_free(dev); - if (error) - fprintf(stderr, "libevdev error: %d\n", error); - uidev = NULL; - dev = NULL; + + qDebug() << "libevdev error" << error_code; + + uidev = nullptr; + dev = nullptr; } evdev::~evdev() @@ -103,14 +118,15 @@ void evdev::pose(const double* headpose) { module_status evdev::initialize() { - if (access("/dev/uinput", R_OK | W_OK)) + if (error_code) { char buf[128] {}; - (void) strerror_r(errno, buf, sizeof(buf)); - return error(tr("Can't open /dev/uinput: %1").arg(buf)); + (void)strerror_r(errno, buf, sizeof(buf)); + return error(QStringLiteral("libevdev call '%1' failed with error '%2' (%3)") + .arg(error_str ? "" : error_str, buf, error_code)); } - - return status_ok(); + else + return {}; } OPENTRACK_DECLARE_PROTOCOL(evdev, LibevdevControls, evdevDll) diff --git a/proto-libevdev/ftnoir_protocol_libevdev.h b/proto-libevdev/ftnoir_protocol_libevdev.h index 1a8f2236..34032396 100644 --- a/proto-libevdev/ftnoir_protocol_libevdev.h +++ b/proto-libevdev/ftnoir_protocol_libevdev.h @@ -26,8 +26,10 @@ public: module_status initialize() override; private: - struct libevdev* dev; - struct libevdev_uinput* uidev; + struct libevdev* dev = nullptr; + struct libevdev_uinput* uidev = nullptr; + int error_code = 0; + const char* error_expr = nullptr; }; class LibevdevControls: public IProtocolDialog -- cgit v1.2.3