Path: blob/master/RSDKv5/RSDK/Input/GLFW/GLFWInputDevice.cpp
1172 views
#ifdef STFU_INTELLISENSE1#include <glad/glad.h>2#include <GLFW/glfw3.h>3#include "GLFWInputDevice.hpp"4#endif56using namespace RSDK;78void RSDK::SKU::InputDeviceGLFW::UpdateInput()9{10currentState ^= 1;11glfwGetGamepadState(jid, &states[currentState]);12bool32 changedButtons = (bool32)(memcmp(&states[currentState], &states[currentState ^ 1], sizeof(GLFWgamepadstate)));1314if (changedButtons) {15this->inactiveTimer[0] = 0;16this->anyPress = true;17}18else {19++this->inactiveTimer[0];20this->anyPress = false;21}2223if (changedButtons24&& ((states[currentState].buttons[GLFW_GAMEPAD_BUTTON_A] != states[currentState ^ 1].buttons[GLFW_GAMEPAD_BUTTON_A])25|| states[currentState].buttons[GLFW_GAMEPAD_BUTTON_START] != states[currentState ^ 1].buttons[GLFW_GAMEPAD_BUTTON_START])) {26this->inactiveTimer[1] = 0;27}28else29++this->inactiveTimer[1];3031ProcessInput(CONT_ANY);32}3334void RSDK::SKU::InputDeviceGLFW::ProcessInput(int32 controllerID)35{36controller[controllerID].keyUp.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_UP];37controller[controllerID].keyDown.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_DOWN];38controller[controllerID].keyLeft.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_LEFT];39controller[controllerID].keyRight.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT];40controller[controllerID].keyA.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_B : GLFW_GAMEPAD_BUTTON_A];41controller[controllerID].keyB.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_A : GLFW_GAMEPAD_BUTTON_B];42// controller[controllerID].keyC.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_C];43controller[controllerID].keyX.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_Y : GLFW_GAMEPAD_BUTTON_X];44controller[controllerID].keyY.press |= this->states[currentState].buttons[swapABXY ? GLFW_GAMEPAD_BUTTON_X : GLFW_GAMEPAD_BUTTON_Y];45// controller[controllerID].keyZ.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_Z];46controller[controllerID].keyStart.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_START];47controller[controllerID].keySelect.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_BACK];4849#if RETRO_REV0250stickL[controllerID].keyStick.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB];51stickL[controllerID].hDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_X];52stickL[controllerID].vDelta = -this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_Y];53stickL[controllerID].keyUp.press |= stickL[controllerID].vDelta > INPUT_DEADZONE;54stickL[controllerID].keyDown.press |= stickL[controllerID].vDelta < -INPUT_DEADZONE;55stickL[controllerID].keyLeft.press |= stickL[controllerID].hDelta < -INPUT_DEADZONE;56stickL[controllerID].keyRight.press |= stickL[controllerID].hDelta > INPUT_DEADZONE;5758stickR[controllerID].keyStick.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB];59stickR[controllerID].hDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_X];60stickR[controllerID].vDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];61stickR[controllerID].keyUp.press |= stickR[controllerID].vDelta > INPUT_DEADZONE;62stickR[controllerID].keyDown.press |= stickR[controllerID].vDelta < -INPUT_DEADZONE;63stickR[controllerID].keyLeft.press |= stickR[controllerID].hDelta < -INPUT_DEADZONE;64stickR[controllerID].keyRight.press |= stickR[controllerID].hDelta > INPUT_DEADZONE;6566triggerL[controllerID].keyBumper.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER];67triggerL[controllerID].keyTrigger.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] > INPUT_DEADZONE;68triggerL[controllerID].bumperDelta = triggerL[controllerID].keyBumper.press ? 1.0f : 0.0f;69triggerL[controllerID].triggerDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];7071triggerR[controllerID].keyBumper.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER];72triggerR[controllerID].keyTrigger.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] > INPUT_DEADZONE;73triggerR[controllerID].bumperDelta = triggerR[controllerID].keyBumper.press ? 1.0f : 0.0f;74triggerR[controllerID].triggerDelta = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];75#else76controller[controllerID].keyStickL.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_THUMB];77stickL[controllerID].hDeltaL = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_X];78stickL[controllerID].vDeltaL = -this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_Y];79stickL[controllerID].keyUp.press |= stickL[controllerID].vDeltaL > INPUT_DEADZONE;80stickL[controllerID].keyDown.press |= stickL[controllerID].vDeltaL < -INPUT_DEADZONE;81stickL[controllerID].keyLeft.press |= stickL[controllerID].hDeltaL < -INPUT_DEADZONE;82stickL[controllerID].keyRight.press |= stickL[controllerID].hDeltaL > INPUT_DEADZONE;8384controller[controllerID].keyStickR.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB];85stickL[controllerID].hDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_X];86stickL[controllerID].vDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];8788controller[controllerID].keyBumperL.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER];89controller[controllerID].keyTriggerL.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] > INPUT_DEADZONE;90stickL[controllerID].triggerDeltaL = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];9192controller[controllerID].keyBumperR.press |= this->states[currentState].buttons[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER];93controller[controllerID].keyTriggerR.press |= this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] > INPUT_DEADZONE;94stickL[controllerID].triggerDeltaR = this->states[currentState].axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];95#endif96}9798void RSDK::SKU::InputDeviceGLFW::CloseDevice()99{100this->active = false;101this->isAssigned = false;102this->jid = GLFW_JOYSTICK_LAST;103}104105RSDK::SKU::InputDeviceGLFW *RSDK::SKU::InitGLFWInputDevice(uint32 id, uint8 controllerID)106{107if (inputDeviceCount >= INPUTDEVICE_COUNT)108return NULL;109110if (inputDeviceList[inputDeviceCount] && inputDeviceList[inputDeviceCount]->active)111return NULL;112113if (inputDeviceList[inputDeviceCount])114delete inputDeviceList[inputDeviceCount];115116inputDeviceList[inputDeviceCount] = new InputDeviceGLFW();117118InputDeviceGLFW *device = (InputDeviceGLFW *)inputDeviceList[inputDeviceCount];119120device->jid = controllerID;121122const char *name = glfwGetGamepadName(controllerID);123124device->swapABXY = false;125uint8 controllerType = DEVICE_XBOX;126127if (strstr(name, "Xbox"))128controllerType = DEVICE_XBOX;129else if (strstr(name, "PS4") || strstr(name, "PS5"))130controllerType = DEVICE_PS4;131else if (strstr(name, "Nintendo") || strstr(name, "Switch")) {132controllerType = DEVICE_SWITCH_PRO;133device->swapABXY = true;134}135else if (strstr(name, "Saturn"))136controllerType = DEVICE_SATURN;137138device->active = true;139device->disabled = false;140device->gamepadType = (DEVICE_API_GLFW << 16) | (DEVICE_TYPE_CONTROLLER << 8) | (controllerType << 0);141device->id = id;142143for (int32 i = 0; i < PLAYER_COUNT; ++i) {144if (inputSlots[i] == id) {145inputSlotDevices[i] = device;146device->isAssigned = true;147}148}149150inputDeviceCount++;151return device;152}153154void RSDK::SKU::InitGLFWInputAPI()155{156char buffer[0x100];157#if RETRO_PLATFORM == RETRO_SWITCH158glfwUpdateGamepadMappings("53776974636820436f6e74726f6c6c65,Switch "159"Controller,a:b0,b:b1,back:b11,dpdown:b15,dpleft:b12,dpright:b14,dpup:b13,leftshoulder:b6,leftstick:b4,lefttrigger:"160"b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b5,righttrigger:b9,rightx:a2,righty:a3,start:b10,x:b3,y:b2,");161// Support for extra controller types SDL doesn't recognise162#endif // ! RETRO_PLATFORM == RETRO_SWITCH163164FileInfo f;165InitFileInfo(&f);166f.externalFile = true;167char path[0x100];168sprintf_s(path, sizeof(path), "%sgamecontrollerdb.txt", SKU::userFileDir);169170if (LoadFile(&f, path, FMODE_RB)) {171char* buf;172AllocateStorage((void**)&buf, f.fileSize + 1, DATASET_TMP, false);173buf[f.fileSize] = 0;174ReadBytes(&f, buf, f.fileSize);175RSDK::PrintLog(PRINT_NORMAL, "[GLFW] gamecontrollerdb size %d: %s", f.fileSize, buf);176177if (glfwUpdateGamepadMappings(buf) == GLFW_FALSE)178RSDK::PrintLog(PRINT_NORMAL, "[GLFW] failed to load from gamecontrollerdb");179180RemoveStorageEntry((void**)&buf);181CloseFile(&f);182}183184for (int32 i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i) {185RSDK::PrintLog(PRINT_NORMAL, "[GLFW] joystick %d is '%s', isGamepad %d", i, glfwGetJoystickName(i), glfwJoystickIsGamepad(i));186if (glfwJoystickIsGamepad(i)) {187uint32 hash;188char idBuffer[0x20];189sprintf_s(idBuffer, sizeof(idBuffer), "%s%d", "GLFWDevice", i);190GenerateHashCRC(&hash, idBuffer);191192SKU::InitGLFWInputDevice(hash, i);193}194}195}196197