Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xarcade2jstick opens the wrong FD #39

Open
univerio opened this issue Sep 15, 2016 · 4 comments
Open

xarcade2jstick opens the wrong FD #39

univerio opened this issue Sep 15, 2016 · 4 comments

Comments

@univerio
Copy link

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);
@petrockblog
Copy link
Owner

Would you say that this patch should be integrated into the master branch?

@univerio
Copy link
Author

univerio commented Mar 14, 2019 via email

@xnaron
Copy link

xnaron commented Oct 29, 2023

Edit: I applied the patch above and now it selects the "correct for me" input.

I have the same issue. Was this patched?

image

(event9 is correct) but picks 12

@petrockblog
Copy link
Owner

It would be great if someone could create a PR for the patch above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants