Path: blob/master/thirdparty/sdl/joystick/hidapi/SDL_hidapi_gamecube.c
9906 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// #define DEBUG_GAMECUBE_PROTOCOL3435#define MAX_CONTROLLERS 43637typedef struct38{39bool pc_mode;40SDL_JoystickID joysticks[MAX_CONTROLLERS];41Uint8 wireless[MAX_CONTROLLERS];42Uint8 min_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];43Uint8 max_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];44Uint8 rumbleAllowed[MAX_CONTROLLERS];45Uint8 rumble[1 + MAX_CONTROLLERS];46// Without this variable, hid_write starts to lag a TON47bool rumbleUpdate;48bool useRumbleBrake;49} SDL_DriverGameCube_Context;5051static void HIDAPI_DriverGameCube_RegisterHints(SDL_HintCallback callback, void *userdata)52{53SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);54}5556static void HIDAPI_DriverGameCube_UnregisterHints(SDL_HintCallback callback, void *userdata)57{58SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);59}6061static bool HIDAPI_DriverGameCube_IsEnabled(void)62{63return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,64SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,65SDL_HIDAPI_DEFAULT));66}6768static 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)69{70if (vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {71// Nintendo Co., Ltd. Wii U GameCube Controller Adapter72return true;73}74if (vendor_id == USB_VENDOR_DRAGONRISE &&75(product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1 ||76product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2)) {77// EVORETRO GameCube Controller Adapter78return true;79}80return false;81}8283static void ResetAxisRange(SDL_DriverGameCube_Context *ctx, int joystick_index)84{85SDL_memset(&ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 - 88, SDL_GAMEPAD_AXIS_COUNT);86SDL_memset(&ctx->max_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 + 88, SDL_GAMEPAD_AXIS_COUNT);8788// Trigger axes may have a higher resting value89ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_LEFT_TRIGGER] = 40;90ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = 40;91}9293static void SDLCALL SDL_JoystickGameCubeRumbleBrakeHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)94{95if (hint) {96SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;97ctx->useRumbleBrake = SDL_GetStringBoolean(hint, false);98}99}100101static bool HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)102{103SDL_DriverGameCube_Context *ctx;104Uint8 packet[37];105Uint8 *curSlot;106Uint8 i;107int size;108Uint8 initMagic = 0x13;109Uint8 rumbleMagic = 0x11;110111#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS112SDL_EnableGameCubeAdaptors();113#endif114115ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));116if (!ctx) {117return false;118}119device->context = ctx;120121ctx->joysticks[0] = 0;122ctx->joysticks[1] = 0;123ctx->joysticks[2] = 0;124ctx->joysticks[3] = 0;125ctx->rumble[0] = rumbleMagic;126ctx->useRumbleBrake = false;127128if (device->vendor_id != USB_VENDOR_NINTENDO) {129ctx->pc_mode = true;130}131132if (ctx->pc_mode) {133for (i = 0; i < MAX_CONTROLLERS; ++i) {134ResetAxisRange(ctx, i);135HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);136}137} else {138// This is all that's needed to initialize the device. Really!139if (SDL_hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {140SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,141"HIDAPI_DriverGameCube_InitDevice(): Couldn't initialize WUP-028");142return false;143}144145// Wait for the adapter to initialize146SDL_Delay(10);147148// Add all the applicable joysticks149while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {150#ifdef DEBUG_GAMECUBE_PROTOCOL151HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);152#endif153if (size < 37 || packet[0] != 0x21) {154continue; // Nothing to do yet...?155}156157// Go through all 4 slots158curSlot = packet + 1;159for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {160ctx->wireless[i] = (curSlot[0] & 0x20) != 0;161162// Only allow rumble if the adapter's second USB cable is connected163ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];164165if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless166if (ctx->joysticks[i] == 0) {167ResetAxisRange(ctx, i);168HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);169}170} else {171if (ctx->joysticks[i] != 0) {172HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);173ctx->joysticks[i] = 0;174}175continue;176}177}178}179}180181SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,182SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);183184HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller");185186return true;187}188189static int HIDAPI_DriverGameCube_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)190{191SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;192Uint8 i;193194for (i = 0; i < 4; ++i) {195if (instance_id == ctx->joysticks[i]) {196return i;197}198}199return -1;200}201202static void HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)203{204}205206static void HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, const Uint8 *packet, int size)207{208SDL_Joystick *joystick;209Uint8 i, v;210Sint16 axis_value;211Uint64 timestamp = SDL_GetTicksNS();212213if (size != 10) {214return; // How do we handle this packet?215}216217i = packet[0] - 1;218if (i >= MAX_CONTROLLERS) {219return; // How do we handle this packet?220}221222joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);223if (!joystick) {224// Hasn't been opened yet, skip225return;226}227228#define READ_BUTTON(off, flag, button) \229SDL_SendJoystickButton( \230timestamp, \231joystick, \232button, \233((packet[off] & flag) != 0));234READ_BUTTON(1, 0x02, 0) // A235READ_BUTTON(1, 0x04, 1) // B236READ_BUTTON(1, 0x08, 3) // Y237READ_BUTTON(1, 0x01, 2) // X238READ_BUTTON(2, 0x80, 4) // DPAD_LEFT239READ_BUTTON(2, 0x20, 5) // DPAD_RIGHT240READ_BUTTON(2, 0x40, 6) // DPAD_DOWN241READ_BUTTON(2, 0x10, 7) // DPAD_UP242READ_BUTTON(2, 0x02, 8) // START243READ_BUTTON(1, 0x80, 9) // RIGHTSHOULDER244/* These two buttons are for the bottoms of the analog triggers.245* More than likely, you're going to want to read the axes instead!246* -flibit247*/248READ_BUTTON(1, 0x20, 10) // TRIGGERRIGHT249READ_BUTTON(1, 0x10, 11) // TRIGGERLEFT250#undef READ_BUTTON251252#define READ_AXIS(off, axis, invert) \253v = invert ? (0xff - packet[off]) : packet[off]; \254if (v < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \255ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \256if (v > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \257ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \258axis_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); \259SDL_SendJoystickAxis( \260timestamp, \261joystick, \262axis, axis_value);263READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTX, 0)264READ_AXIS(4, SDL_GAMEPAD_AXIS_LEFTY, 1)265READ_AXIS(6, SDL_GAMEPAD_AXIS_RIGHTX, 0)266READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTY, 1)267READ_AXIS(7, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0)268READ_AXIS(8, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0)269#undef READ_AXIS270}271272static void HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)273{274SDL_Joystick *joystick;275Uint8 *curSlot;276Uint8 i;277Sint16 axis_value;278Uint64 timestamp = SDL_GetTicksNS();279280if (size < 37 || packet[0] != 0x21) {281return; // Nothing to do right now...?282}283284// Go through all 4 slots285curSlot = packet + 1;286for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {287ctx->wireless[i] = (curSlot[0] & 0x20) != 0;288289// Only allow rumble if the adapter's second USB cable is connected290ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];291292if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless293if (ctx->joysticks[i] == 0) {294ResetAxisRange(ctx, i);295HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);296}297joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);298299// Hasn't been opened yet, skip300if (!joystick) {301continue;302}303} else {304if (ctx->joysticks[i] != 0) {305HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);306ctx->joysticks[i] = 0;307}308continue;309}310311#define READ_BUTTON(off, flag, button) \312SDL_SendJoystickButton( \313timestamp, \314joystick, \315button, \316((curSlot[off] & flag) != 0));317READ_BUTTON(1, 0x01, 0) // A318READ_BUTTON(1, 0x02, 1) // B319READ_BUTTON(1, 0x04, 2) // X320READ_BUTTON(1, 0x08, 3) // Y321READ_BUTTON(1, 0x10, 4) // DPAD_LEFT322READ_BUTTON(1, 0x20, 5) // DPAD_RIGHT323READ_BUTTON(1, 0x40, 6) // DPAD_DOWN324READ_BUTTON(1, 0x80, 7) // DPAD_UP325READ_BUTTON(2, 0x01, 8) // START326READ_BUTTON(2, 0x02, 9) // RIGHTSHOULDER327/* These two buttons are for the bottoms of the analog triggers.328* More than likely, you're going to want to read the axes instead!329* -flibit330*/331READ_BUTTON(2, 0x04, 10) // TRIGGERRIGHT332READ_BUTTON(2, 0x08, 11) // TRIGGERLEFT333#undef READ_BUTTON334335#define READ_AXIS(off, axis) \336if (curSlot[off] < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \337ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \338if (curSlot[off] > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \339ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \340axis_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); \341SDL_SendJoystickAxis( \342timestamp, \343joystick, \344axis, axis_value);345READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTX)346READ_AXIS(4, SDL_GAMEPAD_AXIS_LEFTY)347READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTX)348READ_AXIS(6, SDL_GAMEPAD_AXIS_RIGHTY)349READ_AXIS(7, SDL_GAMEPAD_AXIS_LEFT_TRIGGER)350READ_AXIS(8, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)351#undef READ_AXIS352}353}354355static bool HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)356{357SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;358Uint8 packet[USB_PACKET_LENGTH];359int size;360361// Read input packet362while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {363#ifdef DEBUG_GAMECUBE_PROTOCOL364HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);365#endif366if (ctx->pc_mode) {367HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet, size);368} else {369HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);370}371}372373// Write rumble packet374if (ctx->rumbleUpdate) {375SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));376ctx->rumbleUpdate = false;377}378379// If we got here, nothing bad happened!380return true;381}382383static bool HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)384{385SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;386Uint8 i;387388SDL_AssertJoysticksLocked();389390for (i = 0; i < MAX_CONTROLLERS; i += 1) {391if (joystick->instance_id == ctx->joysticks[i]) {392joystick->nbuttons = 12;393joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;394if (ctx->wireless[i]) {395joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;396} else {397joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;398}399return true;400}401}402return false; // Should never get here!403}404405static bool HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)406{407SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;408Uint8 i, val;409410SDL_AssertJoysticksLocked();411412if (ctx->pc_mode) {413return SDL_Unsupported();414}415416for (i = 0; i < MAX_CONTROLLERS; i += 1) {417if (joystick->instance_id == ctx->joysticks[i]) {418if (ctx->wireless[i]) {419return SDL_SetError("Nintendo GameCube WaveBird controllers do not support rumble");420}421if (!ctx->rumbleAllowed[i]) {422return SDL_SetError("Second USB cable for WUP-028 not connected");423}424if (ctx->useRumbleBrake) {425if (low_frequency_rumble == 0 && high_frequency_rumble > 0) {426val = 0; // if only low is 0 we want to do a regular stop427} else if (low_frequency_rumble == 0 && high_frequency_rumble == 0) {428val = 2; // if both frequencies are 0 we want to do a hard stop429} else {430val = 1; // normal rumble431}432} else {433val = (low_frequency_rumble > 0 || high_frequency_rumble > 0);434}435if (val != ctx->rumble[i + 1]) {436ctx->rumble[i + 1] = val;437ctx->rumbleUpdate = true;438}439return true;440}441}442443// Should never get here!444return SDL_SetError("Couldn't find joystick");445}446447static bool HIDAPI_DriverGameCube_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)448{449return SDL_Unsupported();450}451452static Uint32 HIDAPI_DriverGameCube_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)453{454SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;455Uint32 result = 0;456457SDL_AssertJoysticksLocked();458459if (!ctx->pc_mode) {460Uint8 i;461462for (i = 0; i < MAX_CONTROLLERS; i += 1) {463if (joystick->instance_id == ctx->joysticks[i]) {464if (!ctx->wireless[i] && ctx->rumbleAllowed[i]) {465result |= SDL_JOYSTICK_CAP_RUMBLE;466break;467}468}469}470}471472return result;473}474475static bool HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)476{477return SDL_Unsupported();478}479480static bool HIDAPI_DriverGameCube_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)481{482return SDL_Unsupported();483}484485static bool HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)486{487return SDL_Unsupported();488}489490static void HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)491{492SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;493494// Stop rumble activity495if (ctx->rumbleUpdate) {496SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));497ctx->rumbleUpdate = false;498}499}500501static void HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)502{503SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;504505SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,506SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);507}508509SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube = {510SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,511true,512HIDAPI_DriverGameCube_RegisterHints,513HIDAPI_DriverGameCube_UnregisterHints,514HIDAPI_DriverGameCube_IsEnabled,515HIDAPI_DriverGameCube_IsSupportedDevice,516HIDAPI_DriverGameCube_InitDevice,517HIDAPI_DriverGameCube_GetDevicePlayerIndex,518HIDAPI_DriverGameCube_SetDevicePlayerIndex,519HIDAPI_DriverGameCube_UpdateDevice,520HIDAPI_DriverGameCube_OpenJoystick,521HIDAPI_DriverGameCube_RumbleJoystick,522HIDAPI_DriverGameCube_RumbleJoystickTriggers,523HIDAPI_DriverGameCube_GetJoystickCapabilities,524HIDAPI_DriverGameCube_SetJoystickLED,525HIDAPI_DriverGameCube_SendJoystickEffect,526HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,527HIDAPI_DriverGameCube_CloseJoystick,528HIDAPI_DriverGameCube_FreeDevice,529};530531#endif // SDL_JOYSTICK_HIDAPI_GAMECUBE532533#endif // SDL_JOYSTICK_HIDAPI534535536