Atlas - SDL_hidapi_combined.c
Home / ext / SDL / src / joystick / hidapi Lines: 1 | Size: 7524 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]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 Joy-Cons pair controllers 22#include "SDL_internal.h" 23 24#ifdef SDL_JOYSTICK_HIDAPI 25 26#include "SDL_hidapijoystick_c.h" 27#include "../SDL_sysjoystick.h" 28 29static void HIDAPI_DriverCombined_RegisterHints(SDL_HintCallback callback, void *userdata) 30{ 31} 32 33static void HIDAPI_DriverCombined_UnregisterHints(SDL_HintCallback callback, void *userdata) 34{ 35} 36 37static bool HIDAPI_DriverCombined_IsEnabled(void) 38{ 39 return true; 40} 41 42static bool HIDAPI_DriverCombined_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) 43{ 44 // This is always explicitly created for combined devices 45 return false; 46} 47 48static bool HIDAPI_DriverCombined_InitDevice(SDL_HIDAPI_Device *device) 49{ 50 return HIDAPI_JoystickConnected(device, NULL); 51} 52 53static int HIDAPI_DriverCombined_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id) 54{ 55 return -1; 56} 57 58static void HIDAPI_DriverCombined_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index) 59{ 60} 61 62static bool HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) 63{ 64 int i; 65 char *serial = NULL, *new_serial; 66 size_t serial_length = 0, new_length; 67 68 SDL_AssertJoysticksLocked(); 69 70 for (i = 0; i < device->num_children; ++i) { 71 SDL_HIDAPI_Device *child = device->children[i]; 72 if (!child->driver->OpenJoystick(child, joystick)) { 73 child->broken = true; 74 75 while (i-- > 0) { 76 child = device->children[i]; 77 child->driver->CloseJoystick(child, joystick); 78 } 79 SDL_free(serial); 80 return false; 81 } 82 83 // Extend the serial number with the child serial number 84 if (joystick->serial) { 85 new_length = serial_length + 1 + SDL_strlen(joystick->serial); 86 new_serial = (char *)SDL_realloc(serial, new_length); 87 if (new_serial) { 88 if (serial) { 89 SDL_strlcat(new_serial, ",", new_length); 90 SDL_strlcat(new_serial, joystick->serial, new_length); 91 } else { 92 SDL_strlcpy(new_serial, joystick->serial, new_length); 93 } 94 serial = new_serial; 95 serial_length = new_length; 96 } 97 SDL_free(joystick->serial); 98 joystick->serial = NULL; 99 } 100 } 101 102 // Update the joystick with the combined serial numbers 103 SDL_free(joystick->serial); 104 joystick->serial = serial; 105 106 return true; 107} 108 109static bool HIDAPI_DriverCombined_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble) 110{ 111 int i; 112 bool result = false; 113 114 for (i = 0; i < device->num_children; ++i) { 115 SDL_HIDAPI_Device *child = device->children[i]; 116 if (child->driver->RumbleJoystick(child, joystick, low_frequency_rumble, high_frequency_rumble)) { 117 result = true; 118 } 119 } 120 return result; 121} 122 123static bool HIDAPI_DriverCombined_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble) 124{ 125 int i; 126 bool result = false; 127 128 for (i = 0; i < device->num_children; ++i) { 129 SDL_HIDAPI_Device *child = device->children[i]; 130 if (child->driver->RumbleJoystickTriggers(child, joystick, left_rumble, right_rumble)) { 131 result = true; 132 } 133 } 134 return result; 135} 136 137static Uint32 HIDAPI_DriverCombined_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) 138{ 139 int i; 140 Uint32 caps = 0; 141 142 for (i = 0; i < device->num_children; ++i) { 143 SDL_HIDAPI_Device *child = device->children[i]; 144 caps |= child->driver->GetJoystickCapabilities(child, joystick); 145 } 146 return caps; 147} 148 149static bool HIDAPI_DriverCombined_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) 150{ 151 int i; 152 bool result = false; 153 154 for (i = 0; i < device->num_children; ++i) { 155 SDL_HIDAPI_Device *child = device->children[i]; 156 if (child->driver->SetJoystickLED(child, joystick, red, green, blue)) { 157 result = true; 158 } 159 } 160 return result; 161} 162 163static bool HIDAPI_DriverCombined_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size) 164{ 165 return SDL_Unsupported(); 166} 167 168static bool HIDAPI_DriverCombined_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled) 169{ 170 int i; 171 bool result = false; 172 173 for (i = 0; i < device->num_children; ++i) { 174 SDL_HIDAPI_Device *child = device->children[i]; 175 if (child->driver->SetJoystickSensorsEnabled(child, joystick, enabled)) { 176 result = true; 177 } 178 } 179 return result; 180} 181 182static bool HIDAPI_DriverCombined_UpdateDevice(SDL_HIDAPI_Device *device) 183{ 184 int i; 185 int result = true; 186 187 for (i = 0; i < device->num_children; ++i) { 188 SDL_HIDAPI_Device *child = device->children[i]; 189 if (!child->driver->UpdateDevice(child)) { 190 result = false; 191 } 192 } 193 return result; 194} 195 196static void HIDAPI_DriverCombined_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) 197{ 198 int i; 199 200 for (i = 0; i < device->num_children; ++i) { 201 SDL_HIDAPI_Device *child = device->children[i]; 202 child->driver->CloseJoystick(child, joystick); 203 } 204} 205 206static void HIDAPI_DriverCombined_FreeDevice(SDL_HIDAPI_Device *device) 207{ 208} 209 210SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverCombined = { 211 "SDL_JOYSTICK_HIDAPI_COMBINED", 212 true, 213 HIDAPI_DriverCombined_RegisterHints, 214 HIDAPI_DriverCombined_UnregisterHints, 215 HIDAPI_DriverCombined_IsEnabled, 216 HIDAPI_DriverCombined_IsSupportedDevice, 217 HIDAPI_DriverCombined_InitDevice, 218 HIDAPI_DriverCombined_GetDevicePlayerIndex, 219 HIDAPI_DriverCombined_SetDevicePlayerIndex, 220 HIDAPI_DriverCombined_UpdateDevice, 221 HIDAPI_DriverCombined_OpenJoystick, 222 HIDAPI_DriverCombined_RumbleJoystick, 223 HIDAPI_DriverCombined_RumbleJoystickTriggers, 224 HIDAPI_DriverCombined_GetJoystickCapabilities, 225 HIDAPI_DriverCombined_SetJoystickLED, 226 HIDAPI_DriverCombined_SendJoystickEffect, 227 HIDAPI_DriverCombined_SetJoystickSensorsEnabled, 228 HIDAPI_DriverCombined_CloseJoystick, 229 HIDAPI_DriverCombined_FreeDevice, 230}; 231 232#endif // SDL_JOYSTICK_HIDAPI 233[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.