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

Joysticks are enumerated in the opposite order on Linux #4430

Closed
clebercasali opened this issue Jun 9, 2021 · 11 comments
Closed

Joysticks are enumerated in the opposite order on Linux #4430

clebercasali opened this issue Jun 9, 2021 · 11 comments
Assignees
Milestone

Comments

@clebercasali
Copy link

On Linux, joysticks should by default be sorted by their device files (/dev/input/js*).
Joystick 1 should be "/dev/input/js0", joystick 2 should be "/dev/input/js1", and so on. This was the default behavior on SDL 1.2, and it simply worked.
Currently they are sorted in the opposite order. Last joystick always gets enumerated as "joystick 1", the last but one always gets enumerated as "joystick 2". This is super counterintuitive.

@clebercasali
Copy link
Author

I think this is the same issue: #2832

@icculus
Copy link
Collaborator

icculus commented Jun 14, 2021

@slouken is it going to break anything if we change this?

@clebercasali
Copy link
Author

I'm developing a simple MAME frontend.
SDL2 is the only joystick provider available for MAME on Linux. And right now it's impossible to manage multiple joysticks, specially if there are more than one with the same GUID.
Strangely I have an USB adapter for Mega Drive/Genesis gamepads with 2 ports, and port 1 does get enumerated before port 2.
It makes things even more complicated, because simply reversing the order SDL2 gives will not solve all cases then.
Sorting by device file would make it totally manageable.

@slouken
Copy link
Collaborator

slouken commented Jun 14, 2021

@slouken is it going to break anything if we change this?

No, I don't think so. The intent is for controllers to be listed in the order attached, which corresponds to js0..N on Linux.

@TiZ-HugLife
Copy link

TiZ-HugLife commented Jun 24, 2021

Hi there, all. I've been chasing some joystick jank down and finally made it here to this issue. It seems that if udev is used to enumerate controllers, they come out in the correct order, but if the filesystem is used--and it has to be in sandbox environments--it uses readdir and the event devices come back in arbitrary order. According to this stackoverflow page you can use ls -f to check what order readdir would return entries in, and indeed on my system, the event devices are in reverse order.

My testing found that in SDL 2.0.10, the version in Ubuntu 20.04's repositories, the enumeration order is correct if SDL_JOYSTICK_DISABLE_UDEV is set, and incorrect if I install 2.0.14 from source and recompile sdl2-jstest against it. But I don't know if that's because that version does enumeration differently, or because that environment var didn't exist then (probably the more likely case).

You can work around this with the SDL_JOYSTICK_DEVICE environment var. Its naming suggests you can only provide one, but you can actually delimit multiple devices with a colon. I have a script that enumerates joystick event devices in numerical order and sets the env var accordingly.

@clebercasali
Copy link
Author

You can work around this with the SDL_JOYSTICK_DEVICE environment var. Its naming suggests you can only provide one, but you can actually delimit multiple devices with a colon. I have a script that enumerates joystick event devices in numerical order and sets the env var accordingly.

Hey!
Wow, so SDL_JOYSTICK_DEVICE can take multiple event files separated by ":". I didn't know that.
It works! And it makes things much easier for my project, while I wait for the proper fix.
Thank you so much!

@clebercasali
Copy link
Author

Retroarch has the same issue with its "udev" joystick driver.

@TiZ-HugLife
Copy link

Retroarch also has a SDL2 joystick driver; you can use that and the workaround to get our desired behavior, otherwise that's a problem for their issue tracker.

@clebercasali
Copy link
Author

Retroarch also has a SDL2 joystick driver; you can use that and the workaround to get our desired behavior, otherwise that's a problem for their issue tracker.

Yes, using the SDL2 joystick driver and 'SDL_JOYSTICK_DEVICE' fixes it.

Once this gets fixed in SDL, I will have an argument to ask there too.

@clebercasali
Copy link
Author

Sorry, @slouken . The fix doesn't seem to work.
Controllers are still enumerated in the opposite order.

For example, running gamepad-tool:

[LOG] SDL2 Gamepad Tool v1.1.2 by General Arcade (compiled with SDL version 2.0.4, DLL version 2.0.16)
[LOG] Website: http://generealarcade.com/gamepadtool/
[LOG] Searching gamepads...
[LOG] Found 2 gamepad(s):
[LOG] "GameSir G3w", 03000000bc2000000055000011010000 (mapping available)
[LOG] "X360 Controller", 030000005e0400008e02000010010000 (mapping available)

But "X360 Controller" is /dev/input/js0, /dev/input/event20
"GameSir G3w" is /dev/input/js1, /dev/input/event21

@clebercasali
Copy link
Author

@icculus ?

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

No branches or pull requests

4 participants