Skip to content

Conversation

flibitijibibo
Copy link
Collaborator

@flibitijibibo flibitijibibo commented Jul 5, 2025

Fixes #13178.

Device init and polling works, but I haven't translated the packets yet since the public code doesn't line up with the automatic mappings and I'm intentionally avoiding doing any further reverse engineering myself. Happy to add patches to get this PR working though!

udev rule for Linux:

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="057e", ATTRS{idProduct}=="2073", MODE="0666"

CC @Nohzockt: If you have a preferred name/email for Co-authored-by I'll apply it to the patch that implements the init/polling work.

@Nohzockt
Copy link

Nohzockt commented Jul 5, 2025

Fixes #13178.

Device init and polling works, but I haven't translated the packets yet since the public code doesn't line up with the automatic mappings and I'm intentionally avoiding doing any further reverse engineering myself. Happy to add patches to get this PR working though!

udev rule for Linux:

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="057e", ATTRS{idProduct}=="2073", MODE="0666"

CC @Nohzockt: If you have a preferred name/email for Co-authored-by I'll apply it to the patch that implements the init/polling work.

Well, my preferred name would be just Nohzockt or Noah, no email or something special. I feel honoured that you ask me. Thanks a lot.

@flibitijibibo
Copy link
Collaborator Author

Done!

I also cleaned up the button reading and made a working gamepad mapping, once the left/right stick are working this is ready to review.

@nilsojc
Copy link

nilsojc commented Jul 6, 2025

Quick question. I was able to generate an SDL3.dll file with your WIP stub, would an SDL2.dll be able to be generated as well or this still a work in progress? I am wondering how these can be built for executables like emulators or steam API.

@flibitijibibo flibitijibibo changed the title [DONOTMERGE] Switch 2 GameCube Controller Support Switch 2 GameCube Controller Support Jul 6, 2025
@flibitijibibo flibitijibibo marked this pull request as ready for review July 6, 2025 16:00
@flibitijibibo flibitijibibo requested a review from slouken July 6, 2025 16:00
@endrift
Copy link
Contributor

endrift commented Jul 8, 2025

I have some devices I can test in addition to the GC controller so I might want to add those to this PR if possible. I can create a branch off of your branch, or add them after this is merged.

@flibitijibibo
Copy link
Collaborator Author

Whatever works! I can add commits to this PR if Sam doesn't get to it first.

int size;
while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
if (size > 14) {
Uint64 timestamp = SDL_GetTicksNS();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This block should be refactored out into a separate function to handle devices with varying capabilities and report formats.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe all NS2 devices report the same layout (passing 0 for unsupported axes like the GCN triggers) but @endrift can confirm this.

@flibitijibibo flibitijibibo force-pushed the s2stub branch 2 times, most recently from 040524d to 01f8dea Compare July 11, 2025 23:46
@slouken slouken requested a review from endrift July 12, 2025 00:05
@slouken
Copy link
Collaborator

slouken commented Jul 12, 2025

Does this support Bluetooth? What changes would need to be made for rumble and gyro support?

@endrift
Copy link
Contributor

endrift commented Jul 12, 2025

I haven't looked over the code yet, but I tested the controller and discovered a few things that might want improvements:

  • The ranging of everything analog isn't saturating. Wholly unpressed triggers have non-zero values, and wholly pressed triggers as well as wholly tilted sticks don't reach anywhere close to the full values.
  • The share and C buttons register, but aren't mapped to any gamepad buttons. The Xbox One controller maps the share button to Misc1, so we might want to do the same thing for the share button here. Though we might want to have a named share button in SDL too.

@endrift
Copy link
Contributor

endrift commented Aug 8, 2025

Unfortunately no, the only interface that's exposed as-is is the first interrupt endpoint

@juanmanjarres
Copy link

Hi, I've been following the development of this for a bit since I got a GC NS2 controller, I saw on the open issue that there's some work to be done for the sticks and triggers, I wonder if there's any way I could help? I'm not super familiar with the code here, but I'd be happy to contribute. I currently have a mac if that helps

@endrift
Copy link
Contributor

endrift commented Aug 20, 2025

Unfortunately the current holdup is mostly about internal design issues. The calibration stuff can be fairly easily solved once that's resolved based on information provided both in this thread and on other sites in the meantime.

@endrift
Copy link
Contributor

endrift commented Aug 23, 2025

I've started looking into how to plumb through the necessary pointers and I'm not really making much progress. Short of really hacky smuggling of pointers and type punning, I'm not sure any even halfway-decent way to architect this. The SDL_hidapi abstraction layer doesn't provide a way of determining the backend so you can safely cast the device pointer, afaict, but maybe I'm overlooking how the ifdefs are used.

I would appreciate it if someone who's more familiar with the codebase could "bless" a way to get the libusb_device pointer out of a SDL_HIDAPI_Device.

The alternative, however, is adding "side channels" to SDL_HIDAPI_Devices that you can look up and communicate with. While most game controllers only have one channel to worry about, we will need to deal with side channels once we get to bluetooth too.

@flibitijibibo
Copy link
Collaborator Author

My plan was likely going to be to do all the bulk interactions on init and add the calibration data as a pointer property to the hid_device; this assumes that all the work could fit in init_ns2 though... I'm just skipping rumble for now, that's a whole other issue that might become obsoleted if we ever figure out BLE support.

@slouken
Copy link
Collaborator

slouken commented Aug 25, 2025

I have a Nintendo Switch 2 Pro controller now, and a plan to plumb the secondary device through. I'll take a swing at this when I have time, hopefully sometime this week.

@endrift
Copy link
Contributor

endrift commented Aug 26, 2025

After that's plumbed through, adding calibration should be enough for an initial merge. Rumble will have to wait a bit, as will bluetooth, but those can come later I think

@slouken slouken added this to the 3.4.0 milestone Aug 27, 2025
@slouken
Copy link
Collaborator

slouken commented Aug 27, 2025

I checked out your branch and built with libusb support, but I'm not seeing any packets come in, after successful initialization. I'm running on Windows, hopefully that doesn't matter. Any ideas?

@flibitijibibo
Copy link
Collaborator Author

Only thing I can think of is it needing the WinUSB fuss like the GameCube adapter did, but I haven't seen anyone else use it - @endrift have you tried this on Windows by chance?

@slouken
Copy link
Collaborator

slouken commented Aug 27, 2025

I checked out your branch and built with libusb support, but I'm not seeing any packets come in, after successful initialization. I'm running on Windows, hopefully that doesn't matter. Any ideas?

Ah, the problem was that the interface wasn't claimed. I'll add a commit to the PR to fix that.

@slouken
Copy link
Collaborator

slouken commented Aug 27, 2025

Okay, the libusb bulk endpoint is now available to the driver. Party on!

#include "SDL_libusb.h"

#ifdef HAVE_LIBUSB

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#ifdef SDL_LIBUSB_DYNAMIC
SDL_ELF_NOTE_DLOPEN(
"libusb",
"Support for joysticks through libusb",
SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
SDL_LIBUSB_DYNAMIC
);
#endif

Copy link
Collaborator

Choose a reason for hiding this comment

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

That won't compile yet, this PR is based on older SDL3 main. We'll have to add that after it's merged.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's also why I didn't regenerate the dynapi files, because I don't want to cause conflicts there.

@slouken
Copy link
Collaborator

slouken commented Aug 28, 2025

Okay, I think this is ready for iteration in main, so I'll go ahead and merge it. Thanks everyone who contributed!

@slouken slouken merged commit ac5ccbe into libsdl-org:main Aug 28, 2025
41 checks passed
@OddOttAllen
Copy link

OddOttAllen commented Aug 28, 2025

hey, is it a case where we had to have connected the controllers to a Switch 2 system already for it to work on PC via SDL? or are we all good out of the box?

@flibitijibibo flibitijibibo deleted the s2stub branch August 28, 2025 16:55
@slouken
Copy link
Collaborator

slouken commented Aug 28, 2025

hey, is it a case where we had to have connected the controllers to a Switch 2 system already for it to work on PC via SDL? or are we all good out of the box?

They'll work out of the box. Note that SDL needs to be built with libusb support and have access to low level USB devices which isn't usually the case. We'll have to figure out messaging around that.

@OddOttAllen
Copy link

They'll work out of the box. Note that SDL needs to be built with libusb support and have access to low level USB devices which isn't usually the case. We'll have to figure out messaging around that.

does steam do that with the version of SDL in their client? sorry for being a total newbie/outsider/etc asking all this LOL

@slouken
Copy link
Collaborator

slouken commented Aug 28, 2025

They'll work out of the box. Note that SDL needs to be built with libusb support and have access to low level USB devices which isn't usually the case. We'll have to figure out messaging around that.

does steam do that with the version of SDL in their client? sorry for being a total newbie/outsider/etc asking all this LOL

Steam does not build SDL with libusb enabled, since that normally requires root permissions and you should not run Steam as root. We'll figure out the right path forward once this driver matures and is ready for prime time.

@endrift
Copy link
Contributor

endrift commented Aug 28, 2025

I'm pretty frustrated you just went ahead and merged this without additional feedback. It wasn't ready and now I'm gonna have to fix it on main instead in a PR.

@slouken
Copy link
Collaborator

slouken commented Aug 29, 2025

I'm pretty frustrated you just went ahead and merged this without additional feedback. It wasn't ready and now I'm gonna have to fix it on main instead in a PR.

Sorry, I didn't realize you were actively working on this, feel free to submit a PR when you're ready.

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.

Add support for NS2 GameCube controller