Path: blob/master/thirdparty/sdl/joystick/hidapi/SDL_hidapi_gamecube.c
21409 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#include "SDL_internal.h"2122#ifdef SDL_JOYSTICK_HIDAPI2324#include "../../SDL_hints_c.h"25#include "../SDL_sysjoystick.h"26#include "SDL_hidapijoystick_c.h"27#include "SDL_hidapi_rumble.h"28#include "../../hidapi/SDL_hidapi_c.h"2930#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE3132// Define this if you want to log all packets from the controller33#if 034#define DEBUG_GAMECUBE_PROTOCOL35#endif3637#define MAX_CONTROLLERS 43839typedef struct40{41bool pc_mode;42SDL_JoystickID joysticks[MAX_CONTROLLERS];43Uint8 wireless[MAX_CONTROLLERS];44Uint8 min_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];45Uint8 max_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];46Uint8 rumbleAllowed[MAX_CONTROLLERS];47Uint8 rumble[1 + MAX_CONTROLLERS];48// Without this variable, hid_write starts to lag a TON49bool rumbleUpdate;50bool useRumbleBrake;51} SDL_DriverGameCube_Context;5253static void HIDAPI_DriverGameCube_RegisterHints(SDL_HintCallback callback, void *userdata)54{55SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);56}5758static void HIDAPI_DriverGameCube_UnregisterHints(SDL_HintCallback callback, void *userdata)59{60SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);61}6263static bool HIDAPI_DriverGameCube_IsEnabled(void)64{65return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,66SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,67SDL_HIDAPI_DEFAULT));68}6970static bool HIDAPI_DriverGameCube_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)71{72if (vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {73// Nintendo Co., Ltd. Wii U GameCube Controller Adapter74return true;75}76if (vendor_id == USB_VENDOR_DRAGONRISE &&77(product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1 ||78product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2)) {79// EVORETRO GameCube Controller Adapter80return true;81}82return false;83}8485static void ResetAxisRange(SDL_DriverGameCube_Context *ctx, int joystick_index)86{87SDL_memset(&ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 - 88, SDL_GAMEPAD_AXIS_COUNT);88SDL_memset(&ctx->max_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 + 88, SDL_GAMEPAD_AXIS_COUNT);8990// Trigger axes may have a higher resting value91ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_LEFT_TRIGGER] = 40;92ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = 40;93}9495static void SDLCALL SDL_JoystickGameCubeRumbleBrakeHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)96{97if (hint) {98SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;99ctx->useRumbleBrake = SDL_GetStringBoolean(hint, false);100}101}102103static bool HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)104{105SDL_DriverGameCube_Context *ctx;106Uint8 packet[37];107Uint8 *curSlot;108Uint8 i;109int size;110Uint8 initMagic = 0x13;111Uint8 rumbleMagic = 0x11;112113#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS114SDL_EnableGameCubeAdaptors();115#endif116117ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));118if (!ctx) {119return false;120}121device->context = ctx;122123ctx->rumble[0] = rumbleMagic;124125if (device->vendor_id != USB_VENDOR_NINTENDO) {126ctx->pc_mode = true;127}128129if (ctx->pc_mode) {130ResetAxisRange(ctx, 0);131HIDAPI_JoystickConnected(device, &ctx->joysticks[0]);132} else {133// This is all that's needed to initialize the device. Really!134if (SDL_hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {135SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,136"HIDAPI_DriverGameCube_InitDevice(): Couldn't initialize WUP-028");137return false;138}139140// Wait for the adapter to initialize141SDL_Delay(10);142143// Add all the applicable joysticks144while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {145#ifdef DEBUG_GAMECUBE_PROTOCOL146HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);147#endif148if (size < 37 || packet[0] != 0x21) {149continue; // Nothing to do yet...?150}151152// Go through all 4 slots153curSlot = packet + 1;154for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {155ctx->wireless[i] = (curSlot[0] & 0x20) != 0;156157// Only allow rumble if the adapter's second USB cable is connected158ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];159160if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless161if (ctx->joysticks[i] == 0) {162ResetAxisRange(ctx, i);163HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);164}165} else {166if (ctx->joysticks[i] != 0) {167HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);168ctx->joysticks[i] = 0;169}170continue;171}172}173}174}175176SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,177SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);178179HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller");180181return true;182}183184static int HIDAPI_DriverGameCube_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)185{186SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;187Uint8 i;188189for (i = 0; i < 4; ++i) {190if (instance_id == ctx->joysticks[i]) {191return i;192}193}194return -1;195}196197static void HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)198{199}200201static void HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, const Uint8 *packet, bool invert_c_stick)202{203SDL_Joystick *joystick;204const Uint8 i = 0; // We have a separate context for each connected controller in PC mode, just use the first index205Uint8 v;206Sint16 axis_value;207Uint64 timestamp = SDL_GetTicksNS();208209joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);210if (!joystick) {211// Hasn't been opened yet, skip212return;213}214215#define READ_BUTTON(off, flag, button) \216SDL_SendJoystickButton( \217timestamp, \218joystick, \219button, \220((packet[off] & flag) != 0));221READ_BUTTON(0, 0x02, 0) // A222READ_BUTTON(0, 0x04, 1) // B223READ_BUTTON(0, 0x08, 3) // Y224READ_BUTTON(0, 0x01, 2) // X225READ_BUTTON(1, 0x80, 4) // DPAD_LEFT226READ_BUTTON(1, 0x20, 5) // DPAD_RIGHT227READ_BUTTON(1, 0x40, 6) // DPAD_DOWN228READ_BUTTON(1, 0x10, 7) // DPAD_UP229READ_BUTTON(1, 0x02, 8) // START230READ_BUTTON(0, 0x80, 9) // RIGHTSHOULDER231/* These two buttons are for the bottoms of the analog triggers.232* More than likely, you're going to want to read the axes instead!233* -flibit234*/235READ_BUTTON(0, 0x20, 10) // TRIGGERRIGHT236READ_BUTTON(0, 0x10, 11) // TRIGGERLEFT237#undef READ_BUTTON238239#define READ_AXIS(off, axis, invert) \240v = (invert) ? (0xff - packet[off]) : packet[off]; \241if (v < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \242ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \243if (v > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \244ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \245axis_value = (Sint16)HIDAPI_RemapVal(v, ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \246SDL_SendJoystickAxis( \247timestamp, \248joystick, \249axis, axis_value);250READ_AXIS(2, SDL_GAMEPAD_AXIS_LEFTX, 0)251READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTY, 1)252READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTX, invert_c_stick ? 1 : 0)253READ_AXIS(4, SDL_GAMEPAD_AXIS_RIGHTY, invert_c_stick ? 0 : 1)254READ_AXIS(6, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0)255READ_AXIS(7, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0)256#undef READ_AXIS257}258259static void HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)260{261SDL_Joystick *joystick;262Uint8 *curSlot;263Uint8 i;264Sint16 axis_value;265Uint64 timestamp = SDL_GetTicksNS();266267if (size < 37 || packet[0] != 0x21) {268return; // Nothing to do right now...?269}270271// Go through all 4 slots272curSlot = packet + 1;273for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {274ctx->wireless[i] = (curSlot[0] & 0x20) != 0;275276// Only allow rumble if the adapter's second USB cable is connected277ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];278279if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless280if (ctx->joysticks[i] == 0) {281ResetAxisRange(ctx, i);282HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);283}284joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);285286// Hasn't been opened yet, skip287if (!joystick) {288continue;289}290} else {291if (ctx->joysticks[i] != 0) {292HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);293ctx->joysticks[i] = 0;294}295continue;296}297298#define READ_BUTTON(off, flag, button) \299SDL_SendJoystickButton( \300timestamp, \301joystick, \302button, \303((curSlot[off] & flag) != 0));304READ_BUTTON(1, 0x01, 0) // A305READ_BUTTON(1, 0x02, 1) // B306READ_BUTTON(1, 0x04, 2) // X307READ_BUTTON(1, 0x08, 3) // Y308READ_BUTTON(1, 0x10, 4) // DPAD_LEFT309READ_BUTTON(1, 0x20, 5) // DPAD_RIGHT310READ_BUTTON(1, 0x40, 6) // DPAD_DOWN311READ_BUTTON(1, 0x80, 7) // DPAD_UP312READ_BUTTON(2, 0x01, 8) // START313READ_BUTTON(2, 0x02, 9) // RIGHTSHOULDER314/* These two buttons are for the bottoms of the analog triggers.315* More than likely, you're going to want to read the axes instead!316* -flibit317*/318READ_BUTTON(2, 0x04, 10) // TRIGGERRIGHT319READ_BUTTON(2, 0x08, 11) // TRIGGERLEFT320#undef READ_BUTTON321322#define READ_AXIS(off, axis) \323if (curSlot[off] < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \324ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \325if (curSlot[off] > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \326ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \327axis_value = (Sint16)HIDAPI_RemapVal(curSlot[off], ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \328SDL_SendJoystickAxis( \329timestamp, \330joystick, \331axis, axis_value);332READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTX)333READ_AXIS(4, SDL_GAMEPAD_AXIS_LEFTY)334READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTX)335READ_AXIS(6, SDL_GAMEPAD_AXIS_RIGHTY)336READ_AXIS(7, SDL_GAMEPAD_AXIS_LEFT_TRIGGER)337READ_AXIS(8, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)338#undef READ_AXIS339}340}341342static bool HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)343{344SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;345Uint8 packet[USB_PACKET_LENGTH];346int size;347348// Read input packet349while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {350#ifdef DEBUG_GAMECUBE_PROTOCOL351HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);352#endif353if (ctx->pc_mode) {354if (size == 10) {355// This is the older firmware356// The first byte is the index of the connected controller357// The C stick has an inverted value range compared to the left stick358HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, &packet[1], true);359} else if (size == 9) {360// This is the newer firmware (version 0x7)361// The C stick has the same value range compared to the left stick362HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet, false);363} else {364// How do we handle this packet?365}366} else {367HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);368}369}370371// Write rumble packet372if (ctx->rumbleUpdate) {373SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));374ctx->rumbleUpdate = false;375}376377// If we got here, nothing bad happened!378return true;379}380381static bool HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)382{383SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;384Uint8 i;385386SDL_AssertJoysticksLocked();387388for (i = 0; i < MAX_CONTROLLERS; i += 1) {389if (joystick->instance_id == ctx->joysticks[i]) {390joystick->nbuttons = 12;391joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;392if (ctx->wireless[i]) {393joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;394} else {395joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;396}397return true;398}399}400return false; // Should never get here!401}402403static bool HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)404{405SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;406Uint8 i, val;407408SDL_AssertJoysticksLocked();409410if (ctx->pc_mode) {411return SDL_Unsupported();412}413414for (i = 0; i < MAX_CONTROLLERS; i += 1) {415if (joystick->instance_id == ctx->joysticks[i]) {416if (ctx->wireless[i]) {417return SDL_SetError("Nintendo GameCube WaveBird controllers do not support rumble");418}419if (!ctx->rumbleAllowed[i]) {420return SDL_SetError("Second USB cable for WUP-028 not connected");421}422if (ctx->useRumbleBrake) {423if (low_frequency_rumble == 0 && high_frequency_rumble > 0) {424val = 0; // if only low is 0 we want to do a regular stop425} else if (low_frequency_rumble == 0 && high_frequency_rumble == 0) {426val = 2; // if both frequencies are 0 we want to do a hard stop427} else {428val = 1; // normal rumble429}430} else {431val = (low_frequency_rumble > 0 || high_frequency_rumble > 0);432}433if (val != ctx->rumble[i + 1]) {434ctx->rumble[i + 1] = val;435ctx->rumbleUpdate = true;436}437return true;438}439}440441// Should never get here!442return SDL_SetError("Couldn't find joystick");443}444445static bool HIDAPI_DriverGameCube_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)446{447return SDL_Unsupported();448}449450static Uint32 HIDAPI_DriverGameCube_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)451{452SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;453Uint32 result = 0;454455SDL_AssertJoysticksLocked();456457if (!ctx->pc_mode) {458Uint8 i;459460for (i = 0; i < MAX_CONTROLLERS; i += 1) {461if (joystick->instance_id == ctx->joysticks[i]) {462if (!ctx->wireless[i] && ctx->rumbleAllowed[i]) {463result |= SDL_JOYSTICK_CAP_RUMBLE;464break;465}466}467}468}469470return result;471}472473static bool HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)474{475return SDL_Unsupported();476}477478static bool HIDAPI_DriverGameCube_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)479{480return SDL_Unsupported();481}482483static bool HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)484{485return SDL_Unsupported();486}487488static void HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)489{490SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;491492// Stop rumble activity493if (ctx->rumbleUpdate) {494SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));495ctx->rumbleUpdate = false;496}497}498499static void HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)500{501SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;502503SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,504SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);505}506507SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube = {508SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,509true,510HIDAPI_DriverGameCube_RegisterHints,511HIDAPI_DriverGameCube_UnregisterHints,512HIDAPI_DriverGameCube_IsEnabled,513HIDAPI_DriverGameCube_IsSupportedDevice,514HIDAPI_DriverGameCube_InitDevice,515HIDAPI_DriverGameCube_GetDevicePlayerIndex,516HIDAPI_DriverGameCube_SetDevicePlayerIndex,517HIDAPI_DriverGameCube_UpdateDevice,518HIDAPI_DriverGameCube_OpenJoystick,519HIDAPI_DriverGameCube_RumbleJoystick,520HIDAPI_DriverGameCube_RumbleJoystickTriggers,521HIDAPI_DriverGameCube_GetJoystickCapabilities,522HIDAPI_DriverGameCube_SetJoystickLED,523HIDAPI_DriverGameCube_SendJoystickEffect,524HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,525HIDAPI_DriverGameCube_CloseJoystick,526HIDAPI_DriverGameCube_FreeDevice,527};528529#endif // SDL_JOYSTICK_HIDAPI_GAMECUBE530531#endif // SDL_JOYSTICK_HIDAPI532533534