From c56f7151554c0d272c7b2bef150979e621cc82fd Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 15 Sep 2021 00:06:43 -0700 Subject: [PATCH] Fix use of libx52io return codes Prior to this change, we were treating ERROR_NO_DEVICE as if the hidapi library itself would return such a code. However, that is not the case, and we should be treating any error condition as a critical issue and treat it as if the device was disconnected. The worst case scenario is that it would have to re-enumerate the HID device list and reopen the joystick. --- daemon/x52d_io.c | 55 +++++++++++++++++++++++---------------------- po/x52pro-linux.pot | 14 ++++++------ po/xx_PL.po | 14 ++++++------ 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/daemon/x52d_io.c b/daemon/x52d_io.c index 7c88b31..0563a5e 100644 --- a/daemon/x52d_io.c +++ b/daemon/x52d_io.c @@ -34,7 +34,6 @@ static void *x52_io_thr(void *param) int rc; libx52io_report report; libx52io_report prev_report; - bool connected = false; #define IO_READ_TIMEOUT 50 /* milliseconds */ #define IO_ACQ_TIMEOUT 5 /* seconds */ @@ -44,43 +43,45 @@ static void *x52_io_thr(void *param) memset(&prev_report, 0, sizeof(prev_report)); for (;;) { - if (!connected) { + rc = libx52io_read_timeout(io_ctx, &report, IO_READ_TIMEOUT); + switch (rc) { + case LIBX52IO_SUCCESS: + // Found a report + process_report(&report, &prev_report); + break; + + case LIBX52IO_ERROR_TIMEOUT: + // No report received within the timeout + break; + + case LIBX52IO_ERROR_NO_DEVICE: + PINELOG_TRACE("Device disconnected, trying to connect"); rc = libx52io_open(io_ctx); - if (rc == LIBX52IO_SUCCESS) { - connected = true; - } else { + if (rc != LIBX52IO_SUCCESS) { if (rc != LIBX52IO_ERROR_NO_DEVICE) { PINELOG_ERROR(_("Error %d opening X52 I/O device: %s"), rc, libx52io_strerror(rc)); } else { - PINELOG_TRACE("No compatible X52 I/O device found. Waiting %d seconds before trying again.", IO_ACQ_TIMEOUT); + PINELOG_TRACE("No compatible X52 I/O device found. Sleeping %d seconds before trying again.", + IO_ACQ_TIMEOUT); } sleep(IO_ACQ_TIMEOUT); } - } else { - rc = libx52io_read_timeout(io_ctx, &report, IO_READ_TIMEOUT); - switch (rc) { - case LIBX52IO_SUCCESS: - // Found a report - process_report(&report, &prev_report); - break; + break; - case LIBX52IO_ERROR_TIMEOUT: - // No report received within the timeout - break; + default: + PINELOG_ERROR(_("Error %d reading from X52 I/O device: %s"), + rc, libx52io_strerror(rc)); - case LIBX52IO_ERROR_NO_DEVICE: - PINELOG_TRACE("Device disconnected, signaling I/O connect thread"); - /* Report a NULL report to reset the mouse to default state */ - x52d_mouse_report_event(NULL); - connected = false; - break; + /* + * Possibly disconnected, better to force disconnect now, and try + * to reconnect later + */ + libx52io_close(io_ctx); - default: - PINELOG_ERROR(_("Error %d reading from X52 I/O device: %s"), - rc, libx52io_strerror(rc)); - break; - } + /* Report a NULL report to reset the mouse to default state */ + x52d_mouse_report_event(NULL); + break; } } #undef IO_READ_TIMEOUT diff --git a/po/x52pro-linux.pot b/po/x52pro-linux.pot index 8bf28c0..ebc56c0 100644 --- a/po/x52pro-linux.pot +++ b/po/x52pro-linux.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: x52pro-linux 0.2.2\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/x52pro-linux/issues\n" -"POT-Creation-Date: 2021-09-14 23:34-0700\n" +"POT-Creation-Date: 2021-09-15 00:04-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -715,31 +715,31 @@ msgstr "" msgid "Error %d when updating X52 device: %s" msgstr "" -#: daemon/x52d_io.c:41 +#: daemon/x52d_io.c:40 msgid "Starting X52 I/O thread" msgstr "" -#: daemon/x52d_io.c:53 +#: daemon/x52d_io.c:62 #, c-format msgid "Error %d opening X52 I/O device: %s" msgstr "" -#: daemon/x52d_io.c:80 +#: daemon/x52d_io.c:73 #, c-format msgid "Error %d reading from X52 I/O device: %s" msgstr "" -#: daemon/x52d_io.c:99 +#: daemon/x52d_io.c:100 #, c-format msgid "Error %d initializing X52 I/O library: %s" msgstr "" -#: daemon/x52d_io.c:105 +#: daemon/x52d_io.c:106 #, c-format msgid "Error %d initializing I/O driver thread: %s" msgstr "" -#: daemon/x52d_io.c:112 +#: daemon/x52d_io.c:113 msgid "Shutting down X52 I/O driver thread" msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index 312745d..fcf52eb 100644 --- a/po/xx_PL.po +++ b/po/xx_PL.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: x52pro-linux 0.2.1\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/x52pro-linux/issues\n" -"POT-Creation-Date: 2021-09-14 23:34-0700\n" +"POT-Creation-Date: 2021-09-15 00:04-0700\n" "PO-Revision-Date: 2021-09-14 17:07-0700\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -764,31 +764,31 @@ msgstr "Erroray %d enwhay updatingay X52 arameterpay: %s" msgid "Error %d when updating X52 device: %s" msgstr "Erroray %d enwhay updatingay X52 eviceday: %s" -#: daemon/x52d_io.c:41 +#: daemon/x52d_io.c:40 msgid "Starting X52 I/O thread" msgstr "Artingstay X52 I/O eadthray" -#: daemon/x52d_io.c:53 +#: daemon/x52d_io.c:62 #, c-format msgid "Error %d opening X52 I/O device: %s" msgstr "Erroray %d openingay X52 I/O eviceday: %s" -#: daemon/x52d_io.c:80 +#: daemon/x52d_io.c:73 #, c-format msgid "Error %d reading from X52 I/O device: %s" msgstr "Erroray %d eadingray omfray X52 I/O eviceday: %s" -#: daemon/x52d_io.c:99 +#: daemon/x52d_io.c:100 #, c-format msgid "Error %d initializing X52 I/O library: %s" msgstr "Erroray %d initializingay X52 ibrarylay: %s" -#: daemon/x52d_io.c:105 +#: daemon/x52d_io.c:106 #, c-format msgid "Error %d initializing I/O driver thread: %s" msgstr "Erroray %d initializingay I/O iverdray eadthray: %s" -#: daemon/x52d_io.c:112 +#: daemon/x52d_io.c:113 msgid "Shutting down X52 I/O driver thread" msgstr "Uttingshay ownday X52 I/O iverdray eadthray"