Path: blob/master/RSDKv5/RSDK/Input/Input.cpp
1163 views
#include "RSDK/Core/RetroEngine.hpp"12using namespace RSDK;34InputDevice *RSDK::inputDeviceList[INPUTDEVICE_COUNT];5int32 RSDK::inputDeviceCount = 0;67int32 RSDK::inputSlots[PLAYER_COUNT] = { INPUT_NONE, INPUT_NONE, INPUT_NONE, INPUT_NONE };8InputDevice *RSDK::inputSlotDevices[PLAYER_COUNT] = { NULL, NULL, NULL, NULL };910ControllerState RSDK::controller[PLAYER_COUNT + 1];11AnalogState RSDK::stickL[PLAYER_COUNT + 1];12#if RETRO_REV0213AnalogState RSDK::stickR[PLAYER_COUNT + 1];14TriggerState RSDK::triggerL[PLAYER_COUNT + 1];15TriggerState RSDK::triggerR[PLAYER_COUNT + 1];16#endif17TouchInfo RSDK::touchInfo;1819GamePadMappings *RSDK::gamePadMappings = NULL;20int32 RSDK::gamePadCount = 0;2122#if RETRO_INPUTDEVICE_KEYBOARD23#include "Keyboard/KBInputDevice.cpp"24#endif2526#if RETRO_INPUTDEVICE_XINPUT27#include "XInput/XInputDevice.cpp"28#endif2930#if RETRO_INPUTDEVICE_RAWINPUT31#include "RawInput/RawInputDevice.cpp"32#endif3334#if RETRO_INPUTDEVICE_STEAM35#include "Steam/SteamInputDevice.cpp"36#endif3738#if RETRO_INPUTDEVICE_NX39#include "NX/NXInputDevice.cpp"40#endif4142#if RETRO_INPUTDEVICE_SDL243#include "SDL2/SDL2InputDevice.cpp"44#endif4546#if RETRO_INPUTDEVICE_GLFW47#include "GLFW/GLFWInputDevice.cpp"48#endif4950#if RETRO_INPUTDEVICE_PDBOAT51#include "Paddleboat/PDBInputDevice.cpp"52#endif5354void RSDK::RemoveInputDevice(InputDevice *targetDevice)55{56if (targetDevice) {57for (int32 d = 0; d < inputDeviceCount; ++d) {58if (inputDeviceList[d] && inputDeviceList[d] == targetDevice) {59uint32 deviceID = targetDevice->id;60targetDevice->CloseDevice();61inputDeviceCount--;6263delete inputDeviceList[d];64inputDeviceList[d] = NULL;6566for (int32 id = d + 1; id <= inputDeviceCount && id < INPUTDEVICE_COUNT; ++id) inputDeviceList[id - 1] = inputDeviceList[id];67// clear end device duplicate, prevents issues with new devices deleting stuff they shouldn't be68if (inputDeviceCount < INPUTDEVICE_COUNT)69inputDeviceList[inputDeviceCount] = NULL;7071for (int32 id = 0; id < PLAYER_COUNT; ++id) {72if (inputSlots[id] == deviceID) {73#if !RETRO_REV0274inputSlots[id] = INPUT_NONE;75#endif76inputSlotDevices[id] = NULL;77}78}7980for (int32 id = 0; id < PLAYER_COUNT; ++id) {81for (int32 c = 0; c < inputDeviceCount; ++c) {82if (inputDeviceList[c] && inputDeviceList[c]->id == inputSlots[id]) {83if (inputSlotDevices[id] != inputDeviceList[c])84inputSlotDevices[id] = inputDeviceList[c];85}86}87}88}89}90}91}9293void RSDK::InitInputDevices()94{95#if !RETRO_USE_ORIGINAL_CODE96// default the input slot state to "auto assign" rather than "none"97// this fixes the "controller disconnected" popup since the engine handles the autoassign98// without this, the engine has to wait for the game to tell the engine to start autoassignments99for (int32 i = 0; i < PLAYER_COUNT; ++i) inputSlots[i] = INPUT_AUTOASSIGN;100#endif101102#if RETRO_INPUTDEVICE_KEYBOARD103SKU::InitKeyboardInputAPI();104#endif105106#if RETRO_INPUTDEVICE_RAWINPUT107SKU::InitHIDAPI();108#endif109110#if RETRO_INPUTDEVICE_XINPUT111SKU::InitXInputAPI();112#endif113114#if RETRO_INPUTDEVICE_STEAM115SKU::InitSteamInputAPI();116#endif117118#if RETRO_INPUTDEVICE_NX119SKU::InitNXInputAPI();120#endif121122#if RETRO_INPUTDEVICE_SDL2123SKU::InitSDL2InputAPI();124#endif125126#if RETRO_INPUTDEVICE_GLFW127SKU::InitGLFWInputAPI();128#endif129130#if RETRO_INPUTDEVICE_PDBOAT131SKU::InitPaddleboatInputAPI();132#endif133}134135void RSDK::ReleaseInputDevices()136{137#if RETRO_INPUTDEVICE_SDL2138SKU::ReleaseSDL2InputAPI();139#endif140}141142void RSDK::ClearInput()143{144for (int32 i = 0; i <= PLAYER_COUNT; ++i) {145if (i != 0 && inputSlots[i - 1] == INPUT_UNASSIGNED)146continue;147148controller[i].keyUp.press = false;149controller[i].keyDown.press = false;150controller[i].keyLeft.press = false;151controller[i].keyRight.press = false;152controller[i].keyA.press = false;153controller[i].keyB.press = false;154controller[i].keyC.press = false;155controller[i].keyX.press = false;156controller[i].keyY.press = false;157controller[i].keyZ.press = false;158controller[i].keyStart.press = false;159controller[i].keySelect.press = false;160161stickL[i].keyUp.press = false;162stickL[i].keyDown.press = false;163stickL[i].keyLeft.press = false;164stickL[i].keyRight.press = false;165166#if RETRO_REV02167stickL[i].keyStick.press = false;168169stickR[i].keyUp.press = false;170stickR[i].keyDown.press = false;171stickR[i].keyLeft.press = false;172stickR[i].keyRight.press = false;173stickR[i].keyStick.press = false;174175triggerL[i].keyBumper.press = false;176triggerL[i].keyTrigger.press = false;177178triggerR[i].keyBumper.press = false;179triggerR[i].keyTrigger.press = false;180#else181controller[i].keyStickL.press = false;182controller[i].keyStickR.press = false;183184controller[i].keyBumperL.press = false;185controller[i].keyTriggerL.press = false;186187controller[i].keyBumperR.press = false;188controller[i].keyTriggerR.press = false;189#endif190}191}192193void RSDK::ProcessInput()194{195ClearInput();196197bool32 anyPress = false;198for (int32 i = 0; i < inputDeviceCount; ++i) {199if (inputDeviceList[i]) {200inputDeviceList[i]->UpdateInput();201202anyPress |= inputDeviceList[i]->anyPress;203}204}205206#if RETRO_REV02207if (anyPress || touchInfo.count)208videoSettings.dimTimer = 0;209else if (videoSettings.dimTimer < videoSettings.dimLimit)210++videoSettings.dimTimer;211#endif212213for (int32 i = 0; i < PLAYER_COUNT; ++i) {214int32 assign = inputSlots[i];215if (assign && assign != INPUT_UNASSIGNED) {216if (assign == INPUT_AUTOASSIGN) {217int32 id = GetAvaliableInputDevice();218inputSlots[i] = id;219if (id != INPUT_AUTOASSIGN)220AssignInputSlotToDevice(CONT_P1 + i, id);221}222else {223InputDevice *device = inputSlotDevices[i];224if (device && device->id == assign && device->active)225device->ProcessInput(CONT_P1 + i);226}227}228}229230#if !RETRO_REV02 && RETRO_INPUTDEVICE_KEYBOARD231RSDK::SKU::HandleSpecialKeys();232#endif233234for (int32 c = 0; c <= PLAYER_COUNT; ++c) {235if (c <= 0 || inputSlots[c - 1] != INPUT_UNASSIGNED) {236InputState *cont[] = {237&controller[c].keyUp, &controller[c].keyDown, &controller[c].keyLeft, &controller[c].keyRight,238&controller[c].keyA, &controller[c].keyB, &controller[c].keyC, &controller[c].keyX,239&controller[c].keyY, &controller[c].keyZ, &controller[c].keyStart, &controller[c].keySelect,240};241242#if RETRO_REV02243InputState *lStick[] = { &stickL[c].keyUp, &stickL[c].keyDown, &stickL[c].keyLeft, &stickL[c].keyRight, &stickL[c].keyStick };244InputState *rStick[] = { &stickR[c].keyUp, &stickR[c].keyDown, &stickR[c].keyLeft, &stickR[c].keyRight, &stickR[c].keyStick };245246InputState *lTrigger[] = { &triggerL[c].keyBumper, &triggerL[c].keyTrigger };247InputState *rTrigger[] = { &triggerR[c].keyBumper, &triggerR[c].keyTrigger };248#else249InputState *lStick[] = { &stickL[c].keyUp, &stickL[c].keyDown, &stickL[c].keyLeft, &stickL[c].keyRight, &controller[c].keyStickL };250InputState *rStick[] = { NULL, NULL, NULL, NULL, &controller[c].keyStickR };251252InputState *lTrigger[] = { &controller[c].keyBumperL, &controller[c].keyTriggerL };253InputState *rTrigger[] = { &controller[c].keyBumperR, &controller[c].keyTriggerR };254#endif255256for (int32 i = 0; i < 12; ++i) {257if (cont[i]->press) {258if (cont[i]->down)259cont[i]->press = false;260else261cont[i]->down = true;262}263else264cont[i]->down = false;265}266267for (int32 i = 0; i < 5; ++i) {268if (lStick[i]->press) {269if (lStick[i]->down)270lStick[i]->press = false;271else272lStick[i]->down = true;273}274else275lStick[i]->down = false;276277if (rStick[i]) {278if (rStick[i]->press) {279if (rStick[i]->down)280rStick[i]->press = false;281else282rStick[i]->down = true;283}284else285rStick[i]->down = false;286}287}288289for (int32 i = 0; i < 2; ++i) {290if (lTrigger[i]->press) {291if (lTrigger[i]->down)292lTrigger[i]->press = false;293else294lTrigger[i]->down = true;295}296else297lTrigger[i]->down = false;298299if (rTrigger[i]->press) {300if (rTrigger[i]->down)301rTrigger[i]->press = false;302else303rTrigger[i]->down = true;304}305else306rTrigger[i]->down = false;307}308}309}310}311312void RSDK::ProcessInputDevices()313{314#if RETRO_INPUTDEVICE_NX315SKU::ProcessNXInputDevices();316#endif317#if RETRO_INPUTDEVICE_PDBOAT318SKU::ProcessPaddleboatInputDevices();319#endif320}321322int32 RSDK::GetInputDeviceType(uint32 deviceID)323{324for (int32 i = 0; i < inputDeviceCount; ++i) {325if (inputDeviceList[i] && inputDeviceList[i]->id == deviceID)326return inputDeviceList[i]->gamepadType;327}328329#if RETRO_REV02330return SKU::userCore->GetDefaultGamepadType();331#else332int32 platform = gameVerInfo.platform;333334switch (platform) {335336#if RETRO_INPUTDEVICE_NX337return currentNXControllerType;338#else339return (DEVICE_API_NONE << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (DEVICE_SWITCH_HANDHELD << 0);340#endif341342default:343case PLATFORM_PS4:344case PLATFORM_XB1:345case PLATFORM_PC:346case PLATFORM_DEV: return (DEVICE_API_NONE << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (0 << 0); break;347}348#endif349}350351