Merge device acquisition and update threads

reverse-scroll
nirenjan 2021-09-15 00:17:20 -07:00
parent c56f715155
commit 8deb6a1513
3 changed files with 50 additions and 130 deletions

View File

@ -9,6 +9,7 @@
#include "config.h"
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include "x52d_const.h"
#include "x52d_config.h"
@ -20,28 +21,18 @@ static libx52_device *x52_dev;
static pthread_mutex_t device_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Device acquisition thread
* This is a thread that scans for and opens a supported X52 joystick.
*/
static pthread_t device_acq_thr;
static volatile bool device_acq_thr_enable;
static volatile bool device_upd_thr_enable;
static pthread_t device_thr;
static volatile bool device_update_needed;
static void *x52_dev_acq(void *param)
static void *x52_dev_thr(void *param)
{
int rc;
PINELOG_INFO(_("Starting X52 device acquisition thread"));
// Check if the device is connected in a loop
for (;;) {
#define RECONNECT_DELAY 5
if (!device_acq_thr_enable) {
PINELOG_TRACE("Device acquisition thread disabled. Checking again in %d seconds", RECONNECT_DELAY);
sleep(RECONNECT_DELAY);
continue;
}
#define DEV_ACQ_DELAY 5 // seconds
#define DEV_UPD_DELAY 50000 // microseconds
PINELOG_INFO(_("Starting X52 device manager thread"));
for (;;) {
if (!libx52_is_connected(x52_dev)) {
PINELOG_TRACE("Attempting to connect to X52 device");
rc = libx52_connect(x52_dev);
@ -52,60 +43,25 @@ static void *x52_dev_acq(void *param)
} else {
PINELOG_TRACE("No compatible X52 device found");
}
PINELOG_TRACE("Sleeping for %d seconds before trying to acquire device again", RECONNECT_DELAY);
sleep(RECONNECT_DELAY);
PINELOG_TRACE("Sleeping for %d seconds before trying to acquire device again", DEV_ACQ_DELAY);
sleep(DEV_ACQ_DELAY);
} else {
/* Successfully connected */
PINELOG_INFO(_("Device connected, writing configuration"));
x52d_config_apply();
PINELOG_TRACE("Found device, disabling acquisition thread, enable update thread");
device_acq_thr_enable = false;
device_upd_thr_enable = true;
}
} else {
PINELOG_TRACE("Device is connected, disable acquisition thread, enable update thread");
device_acq_thr_enable = false;
device_upd_thr_enable = true;
}
#undef RECONNECT_DELAY
}
return NULL;
}
/*
* Device update thread
* This is a thread that updates the joystick.
*/
static pthread_t device_upd_thr;
static volatile bool device_update_needed;
static void *x52_dev_upd(void *param)
{
PINELOG_INFO(_("Starting X52 device update thread"));
// Check if the device needs to be updated in a loop
for (;;) {
#define UPDATE_CHECK_DELAY 50000 // Wait for this many useconds
if (!device_update_needed || !device_upd_thr_enable) {
usleep(UPDATE_CHECK_DELAY);
/* Check if the device is still connected */
if (device_upd_thr_enable && !libx52_is_connected(x52_dev)) {
// Detach and spawn thread to reconnect
PINELOG_TRACE("Disconnecting detached device");
libx52_disconnect(x52_dev);
PINELOG_TRACE("Disabling device update thread");
device_upd_thr_enable = false;
PINELOG_TRACE("Signaling device search thread");
device_acq_thr_enable = true;
}
if (!device_update_needed) {
usleep(DEV_UPD_DELAY);
continue;
}
(void)x52d_dev_update();
#undef UPDATE_CHECK_DELAY
}
}
#undef DEV_ACQ_DELAY
#undef DEV_UPD_DELAY
return NULL;
}
@ -121,26 +77,15 @@ void x52d_dev_init(void)
rc, libx52_strerror(rc));
}
// Create and initialize the threads
pthread_create(&device_acq_thr, NULL, x52_dev_acq, NULL);
// With libx52.so.2.3.0, libx52_init will also attempt to connect to a
// supported joystick. Check if a device is already connected before
// enabling the device acquisition thread.
device_acq_thr_enable = !libx52_is_connected(x52_dev);
pthread_create(&device_upd_thr, NULL, x52_dev_upd, NULL);
device_update_needed = false;
device_upd_thr_enable = libx52_is_connected(x52_dev);
// Create and initialize the thread
pthread_create(&device_thr, NULL, x52_dev_thr, NULL);
}
void x52d_dev_exit(void)
{
// Shutdown any threads
PINELOG_INFO(_("Shutting down X52 device acquisition thread"));
pthread_cancel(device_acq_thr);
PINELOG_INFO(_("Shutting down X52 device update thread"));
pthread_cancel(device_upd_thr);
PINELOG_INFO(_("Shutting down X52 device manager thread"));
pthread_cancel(device_thr);
libx52_exit(x52_dev);
}
@ -222,15 +167,10 @@ int x52d_dev_update(void)
if (rc != LIBX52_SUCCESS) {
if (rc == LIBX52_ERROR_NO_DEVICE) {
// Detach and spawn thread to reconnect
// Detach from the existing device, the next thread run will
// pick it up.
PINELOG_TRACE("Disconnecting detached device");
libx52_disconnect(x52_dev);
PINELOG_TRACE("Disabling device update thread");
device_upd_thr_enable = false;
PINELOG_TRACE("Signaling device search thread");
device_acq_thr_enable = true;
} else {
PINELOG_ERROR(_("Error %d when updating X52 device: %s"),
rc, libx52_strerror(rc));

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-15 00:04-0700\n"
"POT-Creation-Date: 2021-09-15 00:09-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"
@ -671,46 +671,38 @@ msgstr ""
msgid "Error processing override '%s.%s=%s'"
msgstr ""
#: daemon/x52d_device.c:35
msgid "Starting X52 device acquisition thread"
#: daemon/x52d_device.c:34
msgid "Starting X52 device manager thread"
msgstr ""
#: daemon/x52d_device.c:50
#: daemon/x52d_device.c:41
#, c-format
msgid "Error %d connecting to device: %s"
msgstr ""
#: daemon/x52d_device.c:58
#: daemon/x52d_device.c:50
msgid "Device connected, writing configuration"
msgstr ""
#: daemon/x52d_device.c:85
msgid "Starting X52 device update thread"
msgstr ""
#: daemon/x52d_device.c:116
#: daemon/x52d_device.c:72
msgid "Initializing libx52"
msgstr ""
#: daemon/x52d_device.c:120
#: daemon/x52d_device.c:76
#, c-format
msgid "Failure %d initializing libx52: %s"
msgstr ""
#: daemon/x52d_device.c:139
msgid "Shutting down X52 device acquisition thread"
#: daemon/x52d_device.c:87
msgid "Shutting down X52 device manager thread"
msgstr ""
#: daemon/x52d_device.c:142
msgid "Shutting down X52 device update thread"
msgstr ""
#: daemon/x52d_device.c:155
#: daemon/x52d_device.c:100
#, c-format
msgid "Error %d when updating X52 parameter: %s"
msgstr ""
#: daemon/x52d_device.c:235
#: daemon/x52d_device.c:175
#, c-format
msgid "Error %d when updating X52 device: %s"
msgstr ""

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-15 00:04-0700\n"
"PO-Revision-Date: 2021-09-14 17:07-0700\n"
"POT-Creation-Date: 2021-09-15 00:09-0700\n"
"PO-Revision-Date: 2021-09-15 00:12-0700\n"
"Last-Translator: Nirenjan Krishnan <nirenjan@gmail.com>\n"
"Language-Team: Dummy Language for testing i18n\n"
"Language: xx_PL\n"
@ -720,46 +720,38 @@ 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:35
msgid "Starting X52 device acquisition thread"
msgstr "Artingstay X52 eviceday acquisitionay eadthray"
#: daemon/x52d_device.c:34
msgid "Starting X52 device manager thread"
msgstr "Artingstay X52 eviceday anagermay eadthray"
#: daemon/x52d_device.c:50
#: daemon/x52d_device.c:41
#, c-format
msgid "Error %d connecting to device: %s"
msgstr "Erroray %d onnectingcay otay eviceday: %s"
#: daemon/x52d_device.c:58
#: daemon/x52d_device.c:50
msgid "Device connected, writing configuration"
msgstr "Eviceday onnectedcay, itingwray onfigurationcay"
#: daemon/x52d_device.c:85
msgid "Starting X52 device update thread"
msgstr "Artingstay X52 eviceday updateay eadthray"
#: daemon/x52d_device.c:116
#: daemon/x52d_device.c:72
msgid "Initializing libx52"
msgstr "Initializingay libx52"
#: daemon/x52d_device.c:120
#: daemon/x52d_device.c:76
#, c-format
msgid "Failure %d initializing libx52: %s"
msgstr "Ailurefay %d initializeay libx52: %s"
#: daemon/x52d_device.c:139
msgid "Shutting down X52 device acquisition thread"
msgstr "Uttingshay ownday X52 eviceday acquisitionay eadthray"
#: daemon/x52d_device.c:87
msgid "Shutting down X52 device manager thread"
msgstr "Uttingshay ownday X52 eviceday anagermay eadthray"
#: daemon/x52d_device.c:142
msgid "Shutting down X52 device update thread"
msgstr "Uttingshay ownday X52 eviceday updateay eadthray"
#: daemon/x52d_device.c:155
#: daemon/x52d_device.c:100
#, c-format
msgid "Error %d when updating X52 parameter: %s"
msgstr "Erroray %d enwhay updatingay X52 arameterpay: %s"
#: daemon/x52d_device.c:235
#: daemon/x52d_device.c:175
#, c-format
msgid "Error %d when updating X52 device: %s"
msgstr "Erroray %d enwhay updatingay X52 eviceday: %s"
@ -847,7 +839,3 @@ msgstr "Irtualvay ousemay otnay eatedcray. Ignoringa eadthray atestay angechay"
#, c-format
msgid "Error %d creating X52 virtual mouse: %s"
msgstr "Erroray %d eatingcray X52 irtualvay ousemay: %s"
#, fuzzy
#~ msgid "Starting X52 mouse manager thread"
#~ msgstr "Artingstay X52 ockclay anagermay eadthray"