On my system the board shows up as two distinct devices under /dev/input. Due to precarious circumstances, they are /dev/input/event9 and /dev/input/event10, but because xarcade2jstick simply opens the first one it finds and glob(3) returns the entries in lexicographical order, xarcade2jstick opens /dev/input/event10 instead of the correct one, /dev/input/event9. This (event9 and event10) is the only case where it happens because the numbers straddle the single- and double-digit boundary. (I suppose it also happens at event99 and event100.)
Operating under the assumption that the lower number is better, I've patched the driver to sort the entries based on the number. This may not be a correct assumption, which is why I've created this as an issue instead of a PR. The patch follows:
diff --git a/src/input_xarcade.c b/src/input_xarcade.c
index 795edc5..19cf6b0 100755
--- a/src/input_xarcade.c
+++ b/src/input_xarcade.c
@@ -16,6 +16,7 @@
/* Copyright (c) 2014, Florian Mueller */
/* ======================================================================== */
+#include <stdlib.h>
#include <glob.h>
#include <errno.h>
#include "input_xarcade.h"
@@ -57,6 +58,24 @@ int16_t input_xarcade_close(INP_XARC_DEV* const xdev) {
// supplementary functions -------------------
+static int compareGlobEntry(const void *e1, const void * e2) {
+ const char *name1 = *(const char **)e1;
+ const char *name2 = *(const char **)e2;
+ char *end1, *end2;
+ long i1, i2;
+
+ name1 += 16; // skip over /dev/input/event
+ name2 += 16;
+ end1 = end2 = NULL;
+ i1 = strtol(name1, &end1, 10);
+ i2 = strtol(name2, &end2, 10);
+
+ if (i1 == i2) {
+ return strcmp(end1, end2);
+ }
+ return i1 < i2 ? -1 : 1;
+}
+
int findXarcadeDevice(void) {
char name[256];
char *filename;
@@ -65,12 +84,15 @@ int findXarcadeDevice(void) {
int rc;
glob_t pglob;
- rc = glob("/dev/input/event*", 0, NULL, &pglob);
+ rc = glob("/dev/input/event*", GLOB_NOSORT, NULL, &pglob);
if (rc) {
printf("Failed to open event devices\n");
return -1;
}
+ // sort matched filenames
+ qsort(pglob.gl_pathv, pglob.gl_pathc, sizeof(char *), compareGlobEntry);
+
for (ctr = 0; ctr < pglob.gl_pathc; ++ctr) {
filename = pglob.gl_pathv[ctr];
fevdev = open(filename, O_RDONLY);
On my system the board shows up as two distinct devices under
/dev/input. Due to precarious circumstances, they are/dev/input/event9and/dev/input/event10, but because xarcade2jstick simply opens the first one it finds andglob(3)returns the entries in lexicographical order, xarcade2jstick opens/dev/input/event10instead of the correct one,/dev/input/event9. This (event9andevent10) is the only case where it happens because the numbers straddle the single- and double-digit boundary. (I suppose it also happens atevent99andevent100.)Operating under the assumption that the lower number is better, I've patched the driver to sort the entries based on the number. This may not be a correct assumption, which is why I've created this as an issue instead of a PR. The patch follows: