Path: blob/master/thirdparty/sdl/joystick/hidapi/SDL_hidapi_combined.c
9913 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20// This driver supports the Nintendo Switch Joy-Cons pair controllers21#include "SDL_internal.h"2223#ifdef SDL_JOYSTICK_HIDAPI2425#include "SDL_hidapijoystick_c.h"26#include "../SDL_sysjoystick.h"2728static void HIDAPI_DriverCombined_RegisterHints(SDL_HintCallback callback, void *userdata)29{30}3132static void HIDAPI_DriverCombined_UnregisterHints(SDL_HintCallback callback, void *userdata)33{34}3536static bool HIDAPI_DriverCombined_IsEnabled(void)37{38return true;39}4041static 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)42{43// This is always explicitly created for combined devices44return false;45}4647static bool HIDAPI_DriverCombined_InitDevice(SDL_HIDAPI_Device *device)48{49return HIDAPI_JoystickConnected(device, NULL);50}5152static int HIDAPI_DriverCombined_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)53{54return -1;55}5657static void HIDAPI_DriverCombined_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)58{59}6061static bool HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)62{63int i;64char *serial = NULL, *new_serial;65size_t serial_length = 0, new_length;6667SDL_AssertJoysticksLocked();6869for (i = 0; i < device->num_children; ++i) {70SDL_HIDAPI_Device *child = device->children[i];71if (!child->driver->OpenJoystick(child, joystick)) {72child->broken = true;7374while (i-- > 0) {75child = device->children[i];76child->driver->CloseJoystick(child, joystick);77}78if (serial) {79SDL_free(serial);80}81return false;82}8384// Extend the serial number with the child serial number85if (joystick->serial) {86new_length = serial_length + 1 + SDL_strlen(joystick->serial);87new_serial = (char *)SDL_realloc(serial, new_length);88if (new_serial) {89if (serial) {90SDL_strlcat(new_serial, ",", new_length);91SDL_strlcat(new_serial, joystick->serial, new_length);92} else {93SDL_strlcpy(new_serial, joystick->serial, new_length);94}95serial = new_serial;96serial_length = new_length;97}98SDL_free(joystick->serial);99joystick->serial = NULL;100}101}102103// Update the joystick with the combined serial numbers104if (joystick->serial) {105SDL_free(joystick->serial);106}107joystick->serial = serial;108109return true;110}111112static bool HIDAPI_DriverCombined_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)113{114int i;115bool result = false;116117for (i = 0; i < device->num_children; ++i) {118SDL_HIDAPI_Device *child = device->children[i];119if (child->driver->RumbleJoystick(child, joystick, low_frequency_rumble, high_frequency_rumble)) {120result = true;121}122}123return result;124}125126static bool HIDAPI_DriverCombined_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)127{128int i;129bool result = false;130131for (i = 0; i < device->num_children; ++i) {132SDL_HIDAPI_Device *child = device->children[i];133if (child->driver->RumbleJoystickTriggers(child, joystick, left_rumble, right_rumble)) {134result = true;135}136}137return result;138}139140static Uint32 HIDAPI_DriverCombined_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)141{142int i;143Uint32 caps = 0;144145for (i = 0; i < device->num_children; ++i) {146SDL_HIDAPI_Device *child = device->children[i];147caps |= child->driver->GetJoystickCapabilities(child, joystick);148}149return caps;150}151152static bool HIDAPI_DriverCombined_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)153{154int i;155bool result = false;156157for (i = 0; i < device->num_children; ++i) {158SDL_HIDAPI_Device *child = device->children[i];159if (child->driver->SetJoystickLED(child, joystick, red, green, blue)) {160result = true;161}162}163return result;164}165166static bool HIDAPI_DriverCombined_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)167{168return SDL_Unsupported();169}170171static bool HIDAPI_DriverCombined_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)172{173int i;174bool result = false;175176for (i = 0; i < device->num_children; ++i) {177SDL_HIDAPI_Device *child = device->children[i];178if (child->driver->SetJoystickSensorsEnabled(child, joystick, enabled)) {179result = true;180}181}182return result;183}184185static bool HIDAPI_DriverCombined_UpdateDevice(SDL_HIDAPI_Device *device)186{187int i;188int result = true;189190for (i = 0; i < device->num_children; ++i) {191SDL_HIDAPI_Device *child = device->children[i];192if (!child->driver->UpdateDevice(child)) {193result = false;194}195}196return result;197}198199static void HIDAPI_DriverCombined_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)200{201int i;202203for (i = 0; i < device->num_children; ++i) {204SDL_HIDAPI_Device *child = device->children[i];205child->driver->CloseJoystick(child, joystick);206}207}208209static void HIDAPI_DriverCombined_FreeDevice(SDL_HIDAPI_Device *device)210{211}212213SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverCombined = {214"SDL_JOYSTICK_HIDAPI_COMBINED",215true,216HIDAPI_DriverCombined_RegisterHints,217HIDAPI_DriverCombined_UnregisterHints,218HIDAPI_DriverCombined_IsEnabled,219HIDAPI_DriverCombined_IsSupportedDevice,220HIDAPI_DriverCombined_InitDevice,221HIDAPI_DriverCombined_GetDevicePlayerIndex,222HIDAPI_DriverCombined_SetDevicePlayerIndex,223HIDAPI_DriverCombined_UpdateDevice,224HIDAPI_DriverCombined_OpenJoystick,225HIDAPI_DriverCombined_RumbleJoystick,226HIDAPI_DriverCombined_RumbleJoystickTriggers,227HIDAPI_DriverCombined_GetJoystickCapabilities,228HIDAPI_DriverCombined_SetJoystickLED,229HIDAPI_DriverCombined_SendJoystickEffect,230HIDAPI_DriverCombined_SetJoystickSensorsEnabled,231HIDAPI_DriverCombined_CloseJoystick,232HIDAPI_DriverCombined_FreeDevice,233};234235#endif // SDL_JOYSTICK_HIDAPI236237238