Create I/O thread to read and process events

This change adds a separate thread to initialize and read reports from
the supported X52 device. This will then process and raise input events
for a virtual device.
reverse-scroll
nirenjan 2021-09-14 13:33:36 -07:00
parent b9e5f34aa4
commit 42850bc4cd
8 changed files with 251 additions and 59 deletions

View File

@ -38,6 +38,7 @@ x52d_LDADD = \
if HAVE_EVDEV
x52d_SOURCES += \
daemon/x52d_io.c \
daemon/x52d_mouse_evdev.c
x52d_CFLAGS += -DHAVE_EVDEV @EVDEV_CFLAGS@
@ -60,6 +61,7 @@ EXTRA_DIST += \
daemon/x52d_config.h \
daemon/x52d_const.h \
daemon/x52d_device.h \
daemon/x52d_io.h \
daemon/x52d_mouse.h \
daemon/x52d.conf

View File

@ -14,7 +14,6 @@
#include "x52d_config.h"
#include "x52d_device.h"
#include "libx52.h"
#include "libx52io.h"
#include "pinelog.h"
static libx52_device *x52_dev;

112
daemon/x52d_io.c 100644
View File

@ -0,0 +1,112 @@
/*
* Saitek X52 Pro MFD & LED driver - I/O driver
*
* Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org)
*
* SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0
*/
#include "config.h"
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#include "x52d_const.h"
#include "x52d_config.h"
#include "x52d_io.h"
#include "libx52io.h"
#include "pinelog.h"
static libx52io_context *io_ctx;
static pthread_t io_thr;
static void process_report(libx52io_report *report, libx52io_report *prev)
{
// TODO: Process changes
memcpy(prev, report, sizeof(*prev));
}
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 */
PINELOG_INFO(_("Starting X52 I/O thread"));
// Reset the previous report, so that process_report can handle changes.
memset(&prev_report, 0, sizeof(prev_report));
for (;;) {
if (!connected) {
rc = libx52io_open(io_ctx);
if (rc == LIBX52IO_SUCCESS) {
connected = true;
} else {
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);
}
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;
case LIBX52IO_ERROR_TIMEOUT:
// No report received within the timeout
break;
case LIBX52IO_ERROR_NO_DEVICE:
PINELOG_TRACE("Device disconnected, signaling I/O connect thread");
connected = false;
break;
default:
PINELOG_ERROR(_("Error %d reading from X52 I/O device: %s"),
rc, libx52io_strerror(rc));
break;
}
}
}
#undef IO_READ_TIMEOUT
#undef IO_ACQ_TIMEOUT
return NULL;
}
void x52d_io_init(void)
{
int rc;
PINELOG_TRACE("Initializing I/O driver");
rc = libx52io_init(&io_ctx);
if (rc != LIBX52IO_SUCCESS) {
PINELOG_FATAL(_("Error %d initializing X52 I/O library: %s"),
rc, libx52io_strerror(rc));
}
rc = pthread_create(&io_thr, NULL, x52_io_thr, NULL);
if (rc != 0) {
PINELOG_FATAL(_("Error %d initializing I/O driver thread: %s"),
rc, strerror(rc));
}
}
void x52d_io_exit(void)
{
PINELOG_INFO(_("Shutting down X52 I/O driver thread"));
pthread_cancel(io_thr);
libx52io_exit(io_ctx);
}

15
daemon/x52d_io.h 100644
View File

@ -0,0 +1,15 @@
/*
* Saitek X52 Pro MFD & LED driver - I/O driver
*
* Copyright (C) 2021 Nirenjan Krishnan (nirenjan@nirenjan.org)
*
* SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0
*/
#ifndef X52D_IO_H
#define X52D_IO_H
void x52d_io_init(void);
void x52d_io_exit(void);
#endif // !defined X52D_IO_H

View File

@ -20,6 +20,7 @@
#include "x52d_const.h"
#include "x52d_config.h"
#include "x52d_device.h"
#include "x52d_io.h"
#include "pinelog.h"
static volatile int flag_quit;
@ -293,6 +294,9 @@ int main(int argc, char **argv)
// Start device threads
x52d_dev_init();
x52d_clock_init();
#if defined(HAVE_EVDEV)
x52d_io_init();
#endif
// Apply configuration
x52d_config_apply();
@ -323,6 +327,9 @@ int main(int argc, char **argv)
// Stop device threads
x52d_clock_exit();
x52d_dev_exit();
#if defined(HAVE_EVDEV)
x52d_io_exit();
#endif
// Remove the PID file
PINELOG_TRACE("Removing PID file %s", pid_file);

View File

@ -17,5 +17,6 @@ daemon/x52d_clock.c
daemon/x52d_config.c
daemon/x52d_config_parser.c
daemon/x52d_device.c
daemon/x52d_io.c
daemon/x52d_mouse.c
daemon/x52d_mouse_evdev.c

View File

@ -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 10:32-0700\n"
"POT-Creation-Date: 2021-09-14 13:28-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -491,17 +491,17 @@ msgstr ""
msgid "OK"
msgstr ""
#: daemon/x52d_main.c:58
#: daemon/x52d_main.c:59
#, c-format
msgid "Error %d setting log file: %s\n"
msgstr ""
#: daemon/x52d_main.c:74
#: daemon/x52d_main.c:75
#, c-format
msgid "Error %d installing handler for signal %d: %s"
msgstr ""
#: daemon/x52d_main.c:85
#: daemon/x52d_main.c:86
#, c-format
msgid ""
"Usage: %s [-f] [-v] [-q]\n"
@ -509,63 +509,63 @@ msgid ""
"\t[-c config-file] [-p pid-file]\n"
msgstr ""
#: daemon/x52d_main.c:115
#: daemon/x52d_main.c:116
#, c-format
msgid "Daemon is already running as PID %u"
msgstr ""
#: daemon/x52d_main.c:253
#: daemon/x52d_main.c:254
#, c-format
msgid "Unable to parse configuration override '%s'\n"
msgstr ""
#: daemon/x52d_main.c:277
#: daemon/x52d_main.c:278
#, c-format
msgid "Foreground = %s"
msgstr ""
#: daemon/x52d_main.c:277 daemon/x52d_main.c:278
#: daemon/x52d_main.c:278 daemon/x52d_main.c:279
msgid "true"
msgstr ""
#: daemon/x52d_main.c:277 daemon/x52d_main.c:278
#: daemon/x52d_main.c:278 daemon/x52d_main.c:279
msgid "false"
msgstr ""
#: daemon/x52d_main.c:278
#, c-format
msgid "Quiet = %s"
msgstr ""
#: daemon/x52d_main.c:279
#, c-format
msgid "Verbosity = %d"
msgid "Quiet = %s"
msgstr ""
#: daemon/x52d_main.c:280
#, c-format
msgid "Log file = %s"
msgid "Verbosity = %d"
msgstr ""
#: daemon/x52d_main.c:281
#, c-format
msgid "Log file = %s"
msgstr ""
#: daemon/x52d_main.c:282
#, c-format
msgid "Config file = %s"
msgstr ""
#: daemon/x52d_main.c:308
#: daemon/x52d_main.c:312
msgid "Reloading X52 configuration"
msgstr ""
#: daemon/x52d_main.c:315
#: daemon/x52d_main.c:319
msgid "Saving X52 configuration to disk"
msgstr ""
#: daemon/x52d_main.c:321
#: daemon/x52d_main.c:325
#, c-format
msgid "Received termination signal %s"
msgstr ""
#: daemon/x52d_main.c:331
#: daemon/x52d_main.c:338
msgid "Shutting down X52 daemon"
msgstr ""
@ -671,50 +671,78 @@ msgstr ""
msgid "Error processing override '%s.%s=%s'"
msgstr ""
#: daemon/x52d_device.c:36
#: daemon/x52d_device.c:35
msgid "Starting X52 device acquisition thread"
msgstr ""
#: daemon/x52d_device.c:51
#: daemon/x52d_device.c:50
#, c-format
msgid "Error %d connecting to device: %s"
msgstr ""
#: daemon/x52d_device.c:59
#: daemon/x52d_device.c:58
msgid "Device connected, writing configuration"
msgstr ""
#: daemon/x52d_device.c:86
#: daemon/x52d_device.c:85
msgid "Starting X52 device update thread"
msgstr ""
#: daemon/x52d_device.c:117
#: daemon/x52d_device.c:116
msgid "Initializing libx52"
msgstr ""
#: daemon/x52d_device.c:121
#: daemon/x52d_device.c:120
#, c-format
msgid "Failure %d initializing libx52: %s"
msgstr ""
#: daemon/x52d_device.c:140
#: daemon/x52d_device.c:139
msgid "Shutting down X52 device acquisition thread"
msgstr ""
#: daemon/x52d_device.c:143
#: daemon/x52d_device.c:142
msgid "Shutting down X52 device update thread"
msgstr ""
#: daemon/x52d_device.c:156
#: daemon/x52d_device.c:155
#, c-format
msgid "Error %d when updating X52 parameter: %s"
msgstr ""
#: daemon/x52d_device.c:236
#: daemon/x52d_device.c:235
#, c-format
msgid "Error %d when updating X52 device: %s"
msgstr ""
#: daemon/x52d_io.c:39
msgid "Starting X52 I/O thread"
msgstr ""
#: daemon/x52d_io.c:51
#, c-format
msgid "Error %d opening X52 I/O device: %s"
msgstr ""
#: daemon/x52d_io.c:76
#, c-format
msgid "Error %d reading from X52 I/O device: %s"
msgstr ""
#: daemon/x52d_io.c:95
#, c-format
msgid "Error %d initializing X52 I/O library: %s"
msgstr ""
#: daemon/x52d_io.c:101
#, c-format
msgid "Error %d initializing I/O driver thread: %s"
msgstr ""
#: daemon/x52d_io.c:108
msgid "Shutting down X52 I/O driver thread"
msgstr ""
#: daemon/x52d_mouse.c:35
#, c-format
msgid "Setting mouse enable to %s"

View File

@ -7,8 +7,8 @@ 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 10:32-0700\n"
"PO-Revision-Date: 2021-09-14 10:34-0700\n"
"POT-Creation-Date: 2021-09-14 13:28-0700\n"
"PO-Revision-Date: 2021-09-14 13:28-0700\n"
"Last-Translator: Nirenjan Krishnan <nirenjan@gmail.com>\n"
"Language-Team: Dummy Language for testing i18n\n"
"Language: xx_PL\n"
@ -534,17 +534,17 @@ msgstr "Estingtay aracterchay 0x%02x..."
msgid "OK"
msgstr "OKay"
#: daemon/x52d_main.c:58
#: daemon/x52d_main.c:59
#, c-format
msgid "Error %d setting log file: %s\n"
msgstr "Erroray %d ettingsay oglay ilefay: %s\n"
#: daemon/x52d_main.c:74
#: daemon/x52d_main.c:75
#, c-format
msgid "Error %d installing handler for signal %d: %s"
msgstr "Erroray %d installingay andlerhay orfay ignalsay %d: %s"
#: daemon/x52d_main.c:85
#: daemon/x52d_main.c:86
#, c-format
msgid ""
"Usage: %s [-f] [-v] [-q]\n"
@ -555,63 +555,63 @@ msgstr ""
"\t[-l oglay-ilefay] [-o overrideay]\n"
"\t[-c onfigcay-ilefay] [-p idpay-ilefay]\n"
#: daemon/x52d_main.c:115
#: daemon/x52d_main.c:116
#, c-format
msgid "Daemon is already running as PID %u"
msgstr "Aemonday isay alreadyay unningray asay IDPay %u"
#: daemon/x52d_main.c:253
#: daemon/x52d_main.c:254
#, c-format
msgid "Unable to parse configuration override '%s'\n"
msgstr "Unableay otay arsepay onfigurationcay overrideay '%s'\n"
#: daemon/x52d_main.c:277
#: daemon/x52d_main.c:278
#, c-format
msgid "Foreground = %s"
msgstr "Oregroundfay = %s"
#: daemon/x52d_main.c:277 daemon/x52d_main.c:278
#: daemon/x52d_main.c:278 daemon/x52d_main.c:279
msgid "true"
msgstr "uetray"
#: daemon/x52d_main.c:277 daemon/x52d_main.c:278
#: daemon/x52d_main.c:278 daemon/x52d_main.c:279
msgid "false"
msgstr "alsefay"
#: daemon/x52d_main.c:278
#: daemon/x52d_main.c:279
#, c-format
msgid "Quiet = %s"
msgstr "Uietqay = %s"
#: daemon/x52d_main.c:279
#: daemon/x52d_main.c:280
#, c-format
msgid "Verbosity = %d"
msgstr "Erbosityvay = %d"
#: daemon/x52d_main.c:280
#: daemon/x52d_main.c:281
#, c-format
msgid "Log file = %s"
msgstr "Oglay ilefay = %s"
#: daemon/x52d_main.c:281
#: daemon/x52d_main.c:282
#, c-format
msgid "Config file = %s"
msgstr "Onfigcay ilefay = %s"
#: daemon/x52d_main.c:308
#: daemon/x52d_main.c:312
msgid "Reloading X52 configuration"
msgstr "Eloadingray X52 onfigurationcay"
#: daemon/x52d_main.c:315
#: daemon/x52d_main.c:319
msgid "Saving X52 configuration to disk"
msgstr "Avingsay X52 onfigurationcay otay iskday"
#: daemon/x52d_main.c:321
#: daemon/x52d_main.c:325
#, c-format
msgid "Received termination signal %s"
msgstr "Eceivedray erminationtay ignalsay %s"
#: daemon/x52d_main.c:331
#: daemon/x52d_main.c:338
msgid "Shutting down X52 daemon"
msgstr "Uttingshay ownday X52 aemonday"
@ -720,50 +720,78 @@ msgstr "Onay aluevay oundfay inay overrideay ingstray '%s'"
msgid "Error processing override '%s.%s=%s'"
msgstr "Erroray ocessingpray overriday '%s.%s=%s'"
#: daemon/x52d_device.c:36
#: daemon/x52d_device.c:35
msgid "Starting X52 device acquisition thread"
msgstr "Artingstay X52 eviceday acquisitionay eadthray"
#: daemon/x52d_device.c:51
#: daemon/x52d_device.c:50
#, c-format
msgid "Error %d connecting to device: %s"
msgstr "Erroray %d onnectingcay otay eviceday: %s"
#: daemon/x52d_device.c:59
#: daemon/x52d_device.c:58
msgid "Device connected, writing configuration"
msgstr "Eviceday onnectedcay, itingwray onfigurationcay"
#: daemon/x52d_device.c:86
#: daemon/x52d_device.c:85
msgid "Starting X52 device update thread"
msgstr "Artingstay X52 eviceday updateay eadthray"
#: daemon/x52d_device.c:117
#: daemon/x52d_device.c:116
msgid "Initializing libx52"
msgstr "Initializingay libx52"
#: daemon/x52d_device.c:121
#: daemon/x52d_device.c:120
#, c-format
msgid "Failure %d initializing libx52: %s"
msgstr "Ailurefay %d initializeay libx52: %s"
#: daemon/x52d_device.c:140
#: daemon/x52d_device.c:139
msgid "Shutting down X52 device acquisition thread"
msgstr "Uttingshay ownday X52 eviceday acquisitionay eadthray"
#: daemon/x52d_device.c:143
#: daemon/x52d_device.c:142
msgid "Shutting down X52 device update thread"
msgstr "Uttingshay ownday X52 eviceday updateay eadthray"
#: daemon/x52d_device.c:156
#: daemon/x52d_device.c:155
#, c-format
msgid "Error %d when updating X52 parameter: %s"
msgstr "Erroray %d enwhay updatingay X52 arameterpay: %s"
#: daemon/x52d_device.c:236
#: daemon/x52d_device.c:235
#, c-format
msgid "Error %d when updating X52 device: %s"
msgstr "Erroray %d enwhay updatingay X52 eviceday: %s"
#: daemon/x52d_io.c:39
msgid "Starting X52 I/O thread"
msgstr "Artingstay X52 I/O eadthray"
#: daemon/x52d_io.c:51
#, c-format
msgid "Error %d opening X52 I/O device: %s"
msgstr "Erroray %d openingay X52 I/O eviceday: %s"
#: daemon/x52d_io.c:76
#, 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:95
#, c-format
msgid "Error %d initializing X52 I/O library: %s"
msgstr "Erroray %d initializingay X52 ibrarylay: %s"
#: daemon/x52d_io.c:101
#, c-format
msgid "Error %d initializing I/O driver thread: %s"
msgstr "Erroray %d initializingay I/O iverdray eadthray: %s"
#: daemon/x52d_io.c:108
msgid "Shutting down X52 I/O driver thread"
msgstr "Uttingshay ownday X52 I/O iverdray eadthray"
#: daemon/x52d_mouse.c:35
#, c-format
msgid "Setting mouse enable to %s"