Skip to content

Commit bd5fb05

Browse files
committed
hidapi: Add a stub driver for Switch 2.
Thanks to kiddkaffeine for the Xcode updates!
1 parent 343ad3e commit bd5fb05

File tree

11 files changed

+191
-0
lines changed

11 files changed

+191
-0
lines changed

VisualC-GDK/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@
728728
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steam_hori.c" />
729729
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steamdeck.c" />
730730
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch.c" />
731+
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch2.c" />
731732
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_wii.c" />
732733
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
733734
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />

VisualC-GDK/SDL/SDL.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steam_hori.c" />
8080
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steamdeck.c" />
8181
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch.c" />
82+
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch2.c" />
8283
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_wii.c" />
8384
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
8485
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />

VisualC/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@
618618
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steam_hori.c" />
619619
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_steamdeck.c" />
620620
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch.c" />
621+
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch2.c" />
621622
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_wii.c" />
622623
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360.c" />
623624
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_xbox360w.c" />

VisualC/SDL/SDL.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,9 @@
12301230
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch.c">
12311231
<Filter>joystick\hidapi</Filter>
12321232
</ClCompile>
1233+
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_switch2.c">
1234+
<Filter>joystick\hidapi</Filter>
1235+
</ClCompile>
12331236
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_wii.c">
12341237
<Filter>joystick\hidapi</Filter>
12351238
</ClCompile>

Xcode/SDL/SDL.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
A7D8B54523E2514300DCD162 /* SDL_hidapijoystick.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C423E2513E00DCD162 /* SDL_hidapijoystick.c */; };
229229
A7D8B54B23E2514300DCD162 /* SDL_hidapi_xboxone.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C523E2513E00DCD162 /* SDL_hidapi_xboxone.c */; };
230230
A7D8B55123E2514300DCD162 /* SDL_hidapi_switch.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */; };
231+
A7D8B55123E2514300DCD163 /* SDL_hidapi_switch2.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */; };
231232
A7D8B55723E2514300DCD162 /* SDL_hidapijoystick_c.h in Headers */ = {isa = PBXBuildFile; fileRef = A7D8A7C723E2513E00DCD162 /* SDL_hidapijoystick_c.h */; };
232233
A7D8B55D23E2514300DCD162 /* SDL_hidapi_xbox360w.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */; };
233234
A7D8B56323E2514300DCD162 /* SDL_hidapi_gamecube.c in Sources */ = {isa = PBXBuildFile; fileRef = A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */; };
@@ -811,6 +812,7 @@
811812
A7D8A7C423E2513E00DCD162 /* SDL_hidapijoystick.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapijoystick.c; sourceTree = "<group>"; };
812813
A7D8A7C523E2513E00DCD162 /* SDL_hidapi_xboxone.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xboxone.c; sourceTree = "<group>"; };
813814
A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch.c; sourceTree = "<group>"; };
815+
A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch2.c; sourceTree = "<group>"; };
814816
A7D8A7C723E2513E00DCD162 /* SDL_hidapijoystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_hidapijoystick_c.h; sourceTree = "<group>"; };
815817
A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xbox360w.c; sourceTree = "<group>"; };
816818
A7D8A7C923E2513E00DCD162 /* SDL_hidapi_gamecube.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_gamecube.c; sourceTree = "<group>"; };
@@ -1948,6 +1950,7 @@
19481950
F3FD042D2C9B755700824C4C /* SDL_hidapi_steam_hori.c */,
19491951
A797456F2B2E9D39009D224A /* SDL_hidapi_steamdeck.c */,
19501952
A7D8A7C623E2513E00DCD162 /* SDL_hidapi_switch.c */,
1953+
A7D8A7C623E2513E00DCD163 /* SDL_hidapi_switch2.c */,
19511954
F3D60A8228C16A1800788A3A /* SDL_hidapi_wii.c */,
19521955
A7D8A7C223E2513E00DCD162 /* SDL_hidapi_xbox360.c */,
19531956
A7D8A7C823E2513E00DCD162 /* SDL_hidapi_xbox360w.c */,
@@ -3035,6 +3038,7 @@
30353038
F3FA5A222B59ACE000FEAD97 /* yuv_rgb_sse.c in Sources */,
30363039
F3C2CB232C5DDDB2004D7998 /* SDL_categories.c in Sources */,
30373040
A7D8B55123E2514300DCD162 /* SDL_hidapi_switch.c in Sources */,
3041+
A7D8B55123E2514300DCD163 /* SDL_hidapi_switch2.c in Sources */,
30383042
A7D8B96223E2514400DCD162 /* SDL_strtokr.c in Sources */,
30393043
A7D8BB7523E2514500DCD162 /* SDL_clipboardevents.c in Sources */,
30403044
E4F798202AD8D87F00669F54 /* SDL_video_unsupported.c in Sources */,

include/SDL3/SDL_hints.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,23 @@ extern "C" {
18091809
*/
18101810
#define SDL_HINT_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED "SDL_JOYSTICK_HIDAPI_SWITCH_PLAYER_LED"
18111811

1812+
/**
1813+
* A variable controlling whether the HIDAPI driver for Nintendo Switch 2
1814+
* controllers should be used.
1815+
*
1816+
* The variable can be set to the following values:
1817+
*
1818+
* - "0": HIDAPI driver is not used.
1819+
* - "1": HIDAPI driver is used.
1820+
*
1821+
* The default is the value of SDL_HINT_JOYSTICK_HIDAPI.
1822+
*
1823+
* This hint should be set before initializing joysticks and gamepads.
1824+
*
1825+
* \since This hint is available since SDL 3.2.0.
1826+
*/
1827+
#define SDL_HINT_JOYSTICK_HIDAPI_SWITCH2 "SDL_JOYSTICK_HIDAPI_SWITCH2"
1828+
18121829
/**
18131830
* A variable controlling whether Nintendo Switch Joy-Con controllers will be
18141831
* in vertical mode when using the HIDAPI driver.

src/joystick/hidapi/SDL_hidapi_nintendo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ typedef enum
3838
k_eSwitchDeviceInfoControllerType_SEGA_Genesis = 13,
3939
} ESwitchDeviceInfoControllerType;
4040

41+
// TODO NS2: Is this from the hardware or do we get to make this one up?
42+
typedef enum
43+
{
44+
k_eSwitch2DeviceInfoControllerType_Unknown = 0,
45+
k_eSwitch2DeviceInfoControllerType_JoyConLeft = 1,
46+
k_eSwitch2DeviceInfoControllerType_JoyConRight = 2,
47+
k_eSwitch2DeviceInfoControllerType_ProController = 3,
48+
k_eSwitch2DeviceInfoControllerType_GameCube = 4,
49+
} ESwitch2DeviceInfoControllerType;
50+
4151
// These values are used internally but can be updated as needed
4252
typedef enum
4353
{
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
Simple DirectMedia Layer
3+
Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.
20+
*/
21+
/* This driver supports the Nintendo Switch Pro controller.
22+
Code and logic contributed by Valve Corporation under the SDL zlib license.
23+
*/
24+
#include "SDL_internal.h"
25+
26+
#ifdef SDL_JOYSTICK_HIDAPI
27+
28+
#include "../../SDL_hints_c.h"
29+
#include "../SDL_sysjoystick.h"
30+
#include "SDL_hidapijoystick_c.h"
31+
#include "SDL_hidapi_rumble.h"
32+
#include "SDL_hidapi_nintendo.h"
33+
34+
#ifdef SDL_JOYSTICK_HIDAPI_SWITCH2
35+
36+
static void HIDAPI_DriverSwitch2_RegisterHints(SDL_HintCallback callback, void *userdata)
37+
{
38+
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, callback, userdata);
39+
}
40+
41+
static void HIDAPI_DriverSwitch2_UnregisterHints(SDL_HintCallback callback, void *userdata)
42+
{
43+
SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, callback, userdata);
44+
}
45+
46+
static bool HIDAPI_DriverSwitch2_IsEnabled(void)
47+
{
48+
return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH2, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
49+
}
50+
51+
static bool HIDAPI_DriverSwitch2_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
52+
{
53+
if (vendor_id == USB_VENDOR_NINTENDO) {
54+
if (product_id == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER) {
55+
return true;
56+
}
57+
}
58+
59+
return false;
60+
}
61+
62+
static bool HIDAPI_DriverSwitch2_InitDevice(SDL_HIDAPI_Device *device)
63+
{
64+
return false;
65+
}
66+
67+
static int HIDAPI_DriverSwitch2_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
68+
{
69+
return 0;
70+
}
71+
72+
static void HIDAPI_DriverSwitch2_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
73+
{
74+
}
75+
76+
static bool HIDAPI_DriverSwitch2_UpdateDevice(SDL_HIDAPI_Device *device)
77+
{
78+
return false;
79+
}
80+
81+
static bool HIDAPI_DriverSwitch2_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
82+
{
83+
return false;
84+
}
85+
86+
static bool HIDAPI_DriverSwitch2_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
87+
{
88+
return false;
89+
}
90+
91+
static bool HIDAPI_DriverSwitch2_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
92+
{
93+
return false;
94+
}
95+
96+
static Uint32 HIDAPI_DriverSwitch2_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
97+
{
98+
return 0;
99+
}
100+
101+
static bool HIDAPI_DriverSwitch2_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
102+
{
103+
return false;
104+
}
105+
106+
static bool HIDAPI_DriverSwitch2_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
107+
{
108+
return false;
109+
}
110+
111+
static bool HIDAPI_DriverSwitch2_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
112+
{
113+
return false;
114+
}
115+
116+
static void HIDAPI_DriverSwitch2_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
117+
{
118+
}
119+
120+
static void HIDAPI_DriverSwitch2_FreeDevice(SDL_HIDAPI_Device *device)
121+
{
122+
}
123+
124+
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch2 = {
125+
SDL_HINT_JOYSTICK_HIDAPI_SWITCH2,
126+
true,
127+
HIDAPI_DriverSwitch2_RegisterHints,
128+
HIDAPI_DriverSwitch2_UnregisterHints,
129+
HIDAPI_DriverSwitch2_IsEnabled,
130+
HIDAPI_DriverSwitch2_IsSupportedDevice,
131+
HIDAPI_DriverSwitch2_InitDevice,
132+
HIDAPI_DriverSwitch2_GetDevicePlayerIndex,
133+
HIDAPI_DriverSwitch2_SetDevicePlayerIndex,
134+
HIDAPI_DriverSwitch2_UpdateDevice,
135+
HIDAPI_DriverSwitch2_OpenJoystick,
136+
HIDAPI_DriverSwitch2_RumbleJoystick,
137+
HIDAPI_DriverSwitch2_RumbleJoystickTriggers,
138+
HIDAPI_DriverSwitch2_GetJoystickCapabilities,
139+
HIDAPI_DriverSwitch2_SetJoystickLED,
140+
HIDAPI_DriverSwitch2_SendJoystickEffect,
141+
HIDAPI_DriverSwitch2_SetJoystickSensorsEnabled,
142+
HIDAPI_DriverSwitch2_CloseJoystick,
143+
HIDAPI_DriverSwitch2_FreeDevice,
144+
};
145+
146+
#endif // SDL_JOYSTICK_HIDAPI_SWITCH2
147+
148+
#endif // SDL_JOYSTICK_HIDAPI

src/joystick/hidapi/SDL_hidapijoystick.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
7575
&SDL_HIDAPI_DriverJoyCons,
7676
&SDL_HIDAPI_DriverSwitch,
7777
#endif
78+
#ifdef SDL_JOYSTICK_HIDAPI_SWITCH2
79+
&SDL_HIDAPI_DriverSwitch2,
80+
#endif
7881
#ifdef SDL_JOYSTICK_HIDAPI_WII
7982
&SDL_HIDAPI_DriverWii,
8083
#endif

src/joystick/hidapi/SDL_hidapijoystick_c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#define SDL_JOYSTICK_HIDAPI_STEAM
3636
#define SDL_JOYSTICK_HIDAPI_STEAMDECK
3737
#define SDL_JOYSTICK_HIDAPI_SWITCH
38+
#define SDL_JOYSTICK_HIDAPI_SWITCH2
3839
#define SDL_JOYSTICK_HIDAPI_WII
3940
#define SDL_JOYSTICK_HIDAPI_XBOX360
4041
#define SDL_JOYSTICK_HIDAPI_XBOXONE
@@ -157,6 +158,7 @@ extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia;
157158
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
158159
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamDeck;
159160
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
161+
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch2;
160162
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverWii;
161163
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
162164
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;

0 commit comments

Comments
 (0)