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

sdl_joypad: fix hotplug, order ports #13527

Closed

Conversation

Informatic
Copy link
Contributor

@Informatic Informatic commented Jan 21, 2022

Description

Minor SDL joypad handling cleanup.

With joypad ignoring code used on webOS we were ending up with joypads registered at port 3+ on certain platforms. This also broke input port device index rebinding (Device Index list was always empty, even for devices on port 3+ which were working properly otherwise; not sure if missing reported devices is a violation of input device reporting API, or it's just a bug there)

This now properly attempts to store joypads continuously, by indexing them based on port number in an internal joypads state array.

Additionally, this fixes hotplug/unplug detection on SDL2. event.jdevice.which on unplug contains a Joystick Instance ID (monotonously increasing on every device connected/disconnected device), not Joystick Index used on open, thus a list of opened joysticks needs to be walked through and compared to received ID. Unplug detection would break after second replug before.

Note: this has not been tested outside of webOS, especially on SDL1.

Related Issues

[Any issues this pull request may be addressing]

Related Pull Requests

[Any other PRs from related repositories that might be needed for this pull request to work]

Reviewers

[If possible @mention all the people that should review your pull request]

@TiZ-HugLife
Copy link

TiZ-HugLife commented Aug 14, 2023

Is there a particular reason this PR has sat dormant? Hotplugging does not work on Steam Deck. Emudeck configures SDL as the gamepad backend because the normal user doesn't have the permissions necessary for the udev backend to work. This seems like a particularly relevant fix for Deck.

@hizzlekizzle
Copy link
Contributor

@sonninnos @LibretroAdmin @Myaats Any thoughts? (including Mats due to Steam/Deck angle)

@@ -55,10 +56,10 @@ static const char *sdl_joypad_name(unsigned pad)

#ifdef HAVE_SDL2
if (sdl_pads[pad].controller)
return SDL_GameControllerNameForIndex(pad);
return SDL_JoystickNameForIndex(pad);
return SDL_GameControllerNameForIndex(sdl_pads[pad].sdl_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really needed, but we could likely use SDL_GameControllerName here.

Suggested change
return SDL_GameControllerNameForIndex(sdl_pads[pad].sdl_id);
return SDL_GameControllerName(sdl_pads[pad].controller);

@@ -447,10 +475,20 @@ static void sdl_joypad_poll(void)
switch (event.type)
{
case SDL_JOYDEVICEADDED:
RARCH_WARN("[SDL_JOYPAD] hotplug added: %d\n", event.jdevice.which);
sdl_pad_connect(event.jdevice.which);
break;
case SDL_JOYDEVICEREMOVED:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider implementing SDL_ControllerDeviceEvents SDL_CONTROLLERDEVICEADDED and SDL_CONTROLLERDEVICEREMOVED?

Copy link
Member

@RobLoach RobLoach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a solid change and clean up to SDL's gamepad control system.

@sonninnos
Copy link
Collaborator

Most of those loggings don't really seem like "warnings" but rather regular "info"..

@TiZ-HugLife
Copy link

Thanks for the review, @RobLoach. I would love to see this merged in time for the next release.


for (int i = 0; i < MAX_USERS; i++) {
if (sdl_pads[i].joypad != NULL && sdl_pads[i].sdl_id == id) {
RARCH_WARN("[SDL_JOYPAD]: %d already registered on port %d\n", id, i);
Copy link
Member

@RobLoach RobLoach Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD]: %d already registered on port %d\n", id, i);
RARCH_LOG("[SDL_JOYPAD]: %d already registered on port %d\n", id, i);


for (int i = 0; i < MAX_USERS; i++) {
if (sdl_pads[i].joypad == NULL) {
RARCH_WARN("[SDL_JOYPAD]: using port: %d\n", i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD]: using port: %d\n", i);
RARCH_LOG("[SDL_JOYPAD]: using port: %d\n", i);

@@ -104,6 +123,7 @@ static void sdl_pad_connect(unsigned id)

if (SDL_IsGameController(id))
{
RARCH_WARN("[SDL_JOYSTICK]: opening #%d as GameController\n", id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYSTICK]: opening #%d as GameController\n", id);
RARCH_LOG("[SDL_JOYSTICK]: opening #%d as GameController\n", id);

@@ -112,6 +132,7 @@ static void sdl_pad_connect(unsigned id)
else
#endif
{
RARCH_WARN("[SDL_JOYSTICK]: opening #%d as Joystick\n", id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYSTICK]: opening #%d as Joystick\n", id);
RARCH_LOG("[SDL_JOYSTICK]: opening #%d as Joystick\n", id);

RARCH_WARN("[SDL_JOYPAD]: Ignoring pad #%d (vendor: %d; product: %d)\n", id, vendor, product);
if (pad->joypad)
SDL_JoystickClose(pad->joypad);
RARCH_WARN("[SDL_JOYPAD]: Ignoring pad #%d (vendor: %04x; product: %04x)\n", id, vendor, product);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD]: Ignoring pad #%d (vendor: %04x; product: %04x)\n", id, vendor, product);
RARCH_LOG("[SDL_JOYPAD]: Ignoring pad #%d (vendor: %04x; product: %04x)\n", id, vendor, product);

return;
}
#endif
#endif

pad->sdl_id = id;

RARCH_WARN("[SDL_JOYPAD]: registering %d as port %d\n", id, port_num);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD]: registering %d as port %d\n", id, port_num);
RARCH_LOG("[SDL_JOYPAD]: registering %d as port %d\n", id, port_num);

@@ -447,10 +475,20 @@ static void sdl_joypad_poll(void)
switch (event.type)
{
case SDL_JOYDEVICEADDED:
RARCH_WARN("[SDL_JOYPAD] hotplug added: %d\n", event.jdevice.which);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD] hotplug added: %d\n", event.jdevice.which);
RARCH_LOG("[SDL_JOYPAD] hotplug added: %d\n", event.jdevice.which);

sdl_pad_connect(event.jdevice.which);
break;
case SDL_JOYDEVICEREMOVED:
sdl_pad_disconnect(event.jdevice.which);
RARCH_WARN("[SDL_JOYPAD] hotplug removed: %d\n", event.jdevice.which);
Copy link
Member

@RobLoach RobLoach Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("[SDL_JOYPAD] hotplug removed: %d\n", event.jdevice.which);
RARCH_LOG("[SDL_JOYPAD] hotplug removed: %d\n", event.jdevice.which);


for (int i = 0 ; i < MAX_USERS ; i++) {
if (SDL_JoystickInstanceID(sdl_pads[i].joypad) == event.jdevice.which) {
RARCH_WARN("Found joystick: %d\n", i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RARCH_WARN("Found joystick: %d\n", i);
RARCH_LOG("[SDL_JOYPAD] Found joystick: %d\n", i);

@gouchi
Copy link
Member

gouchi commented Jul 29, 2024

@Informatic If you can resolve latest suggested changes.

Thank you.

@RobLoach
Copy link
Member

The changes represented here have been brought into #16941 .

@RobLoach RobLoach closed this Aug 30, 2024
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

Successfully merging this pull request may close these issues.

6 participants