diff --git a/CMakeLists.txt b/CMakeLists.txt index eba133d6..c3d28f90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -542,8 +542,6 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp set(INPUT src/input/controller.cpp src/input/controller.h - src/input/keysmappingprovider.h - src/input/keysmappingprovider.cpp src/input/keys_constants.h ) diff --git a/src/common/config.cpp b/src/common/config.cpp index e12c7aea..83a05648 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -48,7 +48,7 @@ std::vector m_elf_viewer; std::vector m_recent_files; // Settings u32 m_language = 1; // english -std::map m_keyboard_binding_map; +std::map m_keyboard_binding_map; bool isLleLibc() { return isLibc; @@ -248,11 +248,11 @@ void setRecentFiles(std::vector recentFiles) { m_recent_files = recentFiles; } -void setKeyboardBindingMap(std::map map) { +void setKeyboardBindingMap(std::map map) { m_keyboard_binding_map = map; } -std::map getKeyboardBindingMap() { +const std::map& getKeyboardBindingMap() { return m_keyboard_binding_map; } @@ -405,10 +405,10 @@ void load(const std::filesystem::path& path) { keyboardBindings = it->second.as_table(); } - // Convert TOML table to std::map + // Convert TOML table to std::map for (const auto& [key, value] : keyboardBindings) { try { - Uint32 int_key = static_cast(std::stoll(key)); + Uint32 int_key = static_cast(std::stoll(key)); if (value.is_integer()) { // Convert the TOML integer value to KeysMapping (int) int int_value = value.as_integer(); diff --git a/src/common/config.h b/src/common/config.h index 3dc75667..eaf4be0c 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -73,8 +73,8 @@ void setMainWindowHeight(u32 height); void setPkgViewer(std::vector pkgList); void setElfViewer(std::vector elfList); void setRecentFiles(std::vector recentFiles); -void setKeyboardBindingMap(std::map map); -std::map getKeyboardBindingMap(); +void setKeyboardBindingMap(std::map map); +const std::map& getKeyboardBindingMap(); u32 getMainWindowGeometryX(); u32 getMainWindowGeometryY(); diff --git a/src/emulator.cpp b/src/emulator.cpp index 37e227dd..5e28dd85 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -116,7 +116,7 @@ void Emulator::Run(const std::filesystem::path& file) { } window = std::make_unique( Config::getScreenWidth(), Config::getScreenHeight(), controller, window_title); - + window->setKeysBindingsMap(Config::getKeyboardBindingMap()); g_window = window.get(); const auto& mount_data_dir = Common::FS::GetUserPath(Common::FS::PathType::GameDataDir) / id; diff --git a/src/emulator.h b/src/emulator.h index 307f2a49..b1f77989 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -9,7 +9,6 @@ #include "common/singleton.h" #include "core/linker.h" #include "input/controller.h" -#include "input/keysmappingprovider.h" #include "sdl_window.h" namespace Core { @@ -35,6 +34,6 @@ private: Input::GameController* controller; Core::Linker* linker; std::unique_ptr window; - std::unique_ptr m_keysMappingProvider; + std::map m_keysBindingsMap; }; } // namespace Core diff --git a/src/input/keysmappingprovider.cpp b/src/input/keysmappingprovider.cpp deleted file mode 100644 index 5c70ced3..00000000 --- a/src/input/keysmappingprovider.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "input/keysmappingprovider.h" - -KeysMappingProvider::KeysMappingProvider(std::map bindingsMap) - : m_bindingsMap{bindingsMap} {} - -std::optional KeysMappingProvider::mapKey(SDL_Keycode sdkKey) { - auto foundIt = m_bindingsMap.find(sdkKey); - if (foundIt != m_bindingsMap.end()) { - return foundIt->second; - } else { - return {}; - } -} diff --git a/src/input/keysmappingprovider.h b/src/input/keysmappingprovider.h deleted file mode 100644 index de1a8c65..00000000 --- a/src/input/keysmappingprovider.h +++ /dev/null @@ -1,21 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include - -#include -#include - -#include "input/keys_constants.h" - -class KeysMappingProvider { -public: - KeysMappingProvider(std::map bindingsMap); - - std::optional mapKey(SDL_Keycode sdkKey); - -private: - std::map m_bindingsMap; -}; \ No newline at end of file diff --git a/src/qt_gui/keyboardcontrolswindow.cpp b/src/qt_gui/keyboardcontrolswindow.cpp index f539cb9b..bd534727 100644 --- a/src/qt_gui/keyboardcontrolswindow.cpp +++ b/src/qt_gui/keyboardcontrolswindow.cpp @@ -108,7 +108,7 @@ KeyboardControlsWindow::~KeyboardControlsWindow() { delete ui; } -const std::map& KeyboardControlsWindow::getKeysMapping() const { +const std::map& KeyboardControlsWindow::getKeysMapping() const { return m_keysMap; } diff --git a/src/qt_gui/keyboardcontrolswindow.h b/src/qt_gui/keyboardcontrolswindow.h index fadd61b3..3649917e 100644 --- a/src/qt_gui/keyboardcontrolswindow.h +++ b/src/qt_gui/keyboardcontrolswindow.h @@ -23,7 +23,7 @@ public: KeyboardControlsWindow(QWidget* parent = nullptr); ~KeyboardControlsWindow(); - const std::map& getKeysMapping() const; + const std::map& getKeysMapping() const; private slots: void onEditingFinished(); @@ -35,6 +35,6 @@ private: Ui::KeyboardControlsWindow* ui; QSet m_listOfKeySequenceEdits; - std::map m_keysMap; - std::map m_reverseKeysMap; + std::map m_keysMap; + std::map m_reverseKeysMap; }; diff --git a/src/qt_gui/main_window.cpp b/src/qt_gui/main_window.cpp index 16d915a0..89f3e856 100644 --- a/src/qt_gui/main_window.cpp +++ b/src/qt_gui/main_window.cpp @@ -659,7 +659,7 @@ void MainWindow::InstallDirectory() { RefreshGameTable(); } -std::map MainWindow::getKeysMapping() { +std::map MainWindow::getKeysMapping() { return m_keyboardControlsDialog->getKeysMapping(); } diff --git a/src/qt_gui/main_window.h b/src/qt_gui/main_window.h index 74b66205..a402dc76 100644 --- a/src/qt_gui/main_window.h +++ b/src/qt_gui/main_window.h @@ -42,7 +42,7 @@ public: void InstallDirectory(); void StartGame(); - std::map getKeysMapping(); + std::map getKeysMapping(); private Q_SLOTS: void ConfigureGuiFromSettings(); diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index 8fa41f75..ed7d4e3f 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -108,8 +108,8 @@ void WindowSDL::waitEvent() { } } -void WindowSDL::setKeysMappingProvider(KeysMappingProvider* provider) { - keysMappingProvider = provider; +void WindowSDL::setKeysBindingsMap(const std::map& bindingsMap) { + keysBindingsMap = bindingsMap; } void WindowSDL::onResize() { @@ -125,8 +125,13 @@ void WindowSDL::onKeyPress(const SDL_Event* event) { int ax = 0; bool keyHandlingPending = true; - if (keysMappingProvider != nullptr) { - auto ps4KeyOpt = keysMappingProvider->mapKey(event->key.key); + if (!keysBindingsMap.empty()) { + + std::optional ps4KeyOpt; + auto foundIt = keysBindingsMap.find(event->key.key); + if (foundIt != keysBindingsMap.end()) { + ps4KeyOpt = foundIt->second; + } // No support for modifiers (yet) if (ps4KeyOpt.has_value() && (event->key.mod == SDL_KMOD_NONE)) { @@ -387,4 +392,71 @@ void WindowSDL::handleRAnalogDownKey(const SDL_Event* event, u32& button, Input: ax = Input::GetAxis(-0x80, 0x80, axisvalue); } +void WindowSDL::onGamepadEvent(const SDL_Event* event) { + using Libraries::Pad::OrbisPadButtonDataOffset; + + u32 button = 0; + Input::Axis axis = Input::Axis::AxisMax; + switch (event->type) { + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + case SDL_EVENT_GAMEPAD_BUTTON_UP: + button = sdlGamepadToOrbisButton(event->gbutton.button); + if (button != 0) { + controller->CheckButton(0, button, event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN); + } + break; + case SDL_EVENT_GAMEPAD_AXIS_MOTION: + axis = event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX ? Input::Axis::LeftX + : event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFTY ? Input::Axis::LeftY + : event->gaxis.axis == SDL_GAMEPAD_AXIS_RIGHTX ? Input::Axis::RightX + : event->gaxis.axis == SDL_GAMEPAD_AXIS_RIGHTY ? Input::Axis::RightY + : event->gaxis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ? Input::Axis::TriggerLeft + : event->gaxis.axis == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER ? Input::Axis::TriggerRight + : Input::Axis::AxisMax; + if (axis != Input::Axis::AxisMax) { + controller->Axis(0, axis, Input::GetAxis(-0x8000, 0x8000, event->gaxis.value)); + } + break; + } +} + +int WindowSDL::sdlGamepadToOrbisButton(u8 button) { + using Libraries::Pad::OrbisPadButtonDataOffset; + + switch (button) { + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_DOWN; + case SDL_GAMEPAD_BUTTON_DPAD_UP: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_UP; + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_LEFT; + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_RIGHT; + case SDL_GAMEPAD_BUTTON_SOUTH: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CROSS; + case SDL_GAMEPAD_BUTTON_NORTH: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TRIANGLE; + case SDL_GAMEPAD_BUTTON_WEST: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_SQUARE; + case SDL_GAMEPAD_BUTTON_EAST: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CIRCLE; + case SDL_GAMEPAD_BUTTON_START: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_OPTIONS; + case SDL_GAMEPAD_BUTTON_TOUCHPAD: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TOUCH_PAD; + case SDL_GAMEPAD_BUTTON_BACK: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TOUCH_PAD; + case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L1; + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R1; + case SDL_GAMEPAD_BUTTON_LEFT_STICK: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L3; + case SDL_GAMEPAD_BUTTON_RIGHT_STICK: + return OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R3; + default: + return 0; + } +} + } // namespace Frontend diff --git a/src/sdl_window.h b/src/sdl_window.h index 823f59c1..e5bfdf83 100644 --- a/src/sdl_window.h +++ b/src/sdl_window.h @@ -3,9 +3,10 @@ #pragma once +#include #include #include "common/types.h" -#include "input/keysmappingprovider.h" +#include "input/keys_constants.h" struct SDL_Window; struct SDL_Gamepad; @@ -66,7 +67,7 @@ public: void waitEvent(); - void setKeysMappingProvider(KeysMappingProvider* provider); + void setKeysBindingsMap(const std::map& bindingsMap); private: void onResize(); @@ -102,7 +103,7 @@ private: Input::GameController* controller; WindowSystemInfo window_info{}; SDL_Window* window{}; - KeysMappingProvider* keysMappingProvider = nullptr; + std::map keysBindingsMap; bool is_shown{}; bool is_open{true}; };