Fix code review remarks from Turtle

This commit is contained in:
Vasyl Baran 2024-08-20 09:14:01 +03:00
parent 990233336f
commit 0a2a261a02
13 changed files with 95 additions and 62 deletions

View File

@ -542,8 +542,6 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp
set(INPUT src/input/controller.cpp set(INPUT src/input/controller.cpp
src/input/controller.h src/input/controller.h
src/input/keysmappingprovider.h
src/input/keysmappingprovider.cpp
src/input/keys_constants.h src/input/keys_constants.h
) )

View File

@ -48,7 +48,7 @@ std::vector<std::string> m_elf_viewer;
std::vector<std::string> m_recent_files; std::vector<std::string> m_recent_files;
// Settings // Settings
u32 m_language = 1; // english u32 m_language = 1; // english
std::map<Uint32, KeysMapping> m_keyboard_binding_map; std::map<u32, KeysMapping> m_keyboard_binding_map;
bool isLleLibc() { bool isLleLibc() {
return isLibc; return isLibc;
@ -248,11 +248,11 @@ void setRecentFiles(std::vector<std::string> recentFiles) {
m_recent_files = recentFiles; m_recent_files = recentFiles;
} }
void setKeyboardBindingMap(std::map<Uint32, KeysMapping> map) { void setKeyboardBindingMap(std::map<u32, KeysMapping> map) {
m_keyboard_binding_map = map; m_keyboard_binding_map = map;
} }
std::map<Uint32, KeysMapping> getKeyboardBindingMap() { const std::map<u32, KeysMapping>& getKeyboardBindingMap() {
return m_keyboard_binding_map; return m_keyboard_binding_map;
} }
@ -405,10 +405,10 @@ void load(const std::filesystem::path& path) {
keyboardBindings = it->second.as_table(); keyboardBindings = it->second.as_table();
} }
// Convert TOML table to std::map<Uint32, KeysMapping> // Convert TOML table to std::map<u32, KeysMapping>
for (const auto& [key, value] : keyboardBindings) { for (const auto& [key, value] : keyboardBindings) {
try { try {
Uint32 int_key = static_cast<Uint32>(std::stoll(key)); Uint32 int_key = static_cast<u32>(std::stoll(key));
if (value.is_integer()) { if (value.is_integer()) {
// Convert the TOML integer value to KeysMapping (int) // Convert the TOML integer value to KeysMapping (int)
int int_value = value.as_integer(); int int_value = value.as_integer();

View File

@ -73,8 +73,8 @@ void setMainWindowHeight(u32 height);
void setPkgViewer(std::vector<std::string> pkgList); void setPkgViewer(std::vector<std::string> pkgList);
void setElfViewer(std::vector<std::string> elfList); void setElfViewer(std::vector<std::string> elfList);
void setRecentFiles(std::vector<std::string> recentFiles); void setRecentFiles(std::vector<std::string> recentFiles);
void setKeyboardBindingMap(std::map<Uint32, KeysMapping> map); void setKeyboardBindingMap(std::map<u32, KeysMapping> map);
std::map<Uint32, KeysMapping> getKeyboardBindingMap(); const std::map<u32, KeysMapping>& getKeyboardBindingMap();
u32 getMainWindowGeometryX(); u32 getMainWindowGeometryX();
u32 getMainWindowGeometryY(); u32 getMainWindowGeometryY();

View File

@ -116,7 +116,7 @@ void Emulator::Run(const std::filesystem::path& file) {
} }
window = std::make_unique<Frontend::WindowSDL>( window = std::make_unique<Frontend::WindowSDL>(
Config::getScreenWidth(), Config::getScreenHeight(), controller, window_title); Config::getScreenWidth(), Config::getScreenHeight(), controller, window_title);
window->setKeysBindingsMap(Config::getKeyboardBindingMap());
g_window = window.get(); g_window = window.get();
const auto& mount_data_dir = Common::FS::GetUserPath(Common::FS::PathType::GameDataDir) / id; const auto& mount_data_dir = Common::FS::GetUserPath(Common::FS::PathType::GameDataDir) / id;

View File

@ -9,7 +9,6 @@
#include "common/singleton.h" #include "common/singleton.h"
#include "core/linker.h" #include "core/linker.h"
#include "input/controller.h" #include "input/controller.h"
#include "input/keysmappingprovider.h"
#include "sdl_window.h" #include "sdl_window.h"
namespace Core { namespace Core {
@ -35,6 +34,6 @@ private:
Input::GameController* controller; Input::GameController* controller;
Core::Linker* linker; Core::Linker* linker;
std::unique_ptr<Frontend::WindowSDL> window; std::unique_ptr<Frontend::WindowSDL> window;
std::unique_ptr<KeysMappingProvider> m_keysMappingProvider; std::map<u32, KeysMapping> m_keysBindingsMap;
}; };
} // namespace Core } // namespace Core

View File

@ -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<Uint32, KeysMapping> bindingsMap)
: m_bindingsMap{bindingsMap} {}
std::optional<KeysMapping> KeysMappingProvider::mapKey(SDL_Keycode sdkKey) {
auto foundIt = m_bindingsMap.find(sdkKey);
if (foundIt != m_bindingsMap.end()) {
return foundIt->second;
} else {
return {};
}
}

View File

@ -1,21 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <SDL3/SDL_keycode.h>
#include <map>
#include <optional>
#include "input/keys_constants.h"
class KeysMappingProvider {
public:
KeysMappingProvider(std::map<Uint32, KeysMapping> bindingsMap);
std::optional<KeysMapping> mapKey(SDL_Keycode sdkKey);
private:
std::map<Uint32, KeysMapping> m_bindingsMap;
};

View File

@ -108,7 +108,7 @@ KeyboardControlsWindow::~KeyboardControlsWindow() {
delete ui; delete ui;
} }
const std::map<Uint32, KeysMapping>& KeyboardControlsWindow::getKeysMapping() const { const std::map<u32, KeysMapping>& KeyboardControlsWindow::getKeysMapping() const {
return m_keysMap; return m_keysMap;
} }

View File

@ -23,7 +23,7 @@ public:
KeyboardControlsWindow(QWidget* parent = nullptr); KeyboardControlsWindow(QWidget* parent = nullptr);
~KeyboardControlsWindow(); ~KeyboardControlsWindow();
const std::map<Uint32, KeysMapping>& getKeysMapping() const; const std::map<u32, KeysMapping>& getKeysMapping() const;
private slots: private slots:
void onEditingFinished(); void onEditingFinished();
@ -35,6 +35,6 @@ private:
Ui::KeyboardControlsWindow* ui; Ui::KeyboardControlsWindow* ui;
QSet<QKeySequenceEdit*> m_listOfKeySequenceEdits; QSet<QKeySequenceEdit*> m_listOfKeySequenceEdits;
std::map<Uint32, KeysMapping> m_keysMap; std::map<u32, KeysMapping> m_keysMap;
std::map<KeysMapping, Uint32> m_reverseKeysMap; std::map<KeysMapping, u32> m_reverseKeysMap;
}; };

View File

@ -659,7 +659,7 @@ void MainWindow::InstallDirectory() {
RefreshGameTable(); RefreshGameTable();
} }
std::map<Uint32, KeysMapping> MainWindow::getKeysMapping() { std::map<u32, KeysMapping> MainWindow::getKeysMapping() {
return m_keyboardControlsDialog->getKeysMapping(); return m_keyboardControlsDialog->getKeysMapping();
} }

View File

@ -42,7 +42,7 @@ public:
void InstallDirectory(); void InstallDirectory();
void StartGame(); void StartGame();
std::map<Uint32, KeysMapping> getKeysMapping(); std::map<u32, KeysMapping> getKeysMapping();
private Q_SLOTS: private Q_SLOTS:
void ConfigureGuiFromSettings(); void ConfigureGuiFromSettings();

View File

@ -108,8 +108,8 @@ void WindowSDL::waitEvent() {
} }
} }
void WindowSDL::setKeysMappingProvider(KeysMappingProvider* provider) { void WindowSDL::setKeysBindingsMap(const std::map<u32, KeysMapping>& bindingsMap) {
keysMappingProvider = provider; keysBindingsMap = bindingsMap;
} }
void WindowSDL::onResize() { void WindowSDL::onResize() {
@ -125,8 +125,13 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
int ax = 0; int ax = 0;
bool keyHandlingPending = true; bool keyHandlingPending = true;
if (keysMappingProvider != nullptr) { if (!keysBindingsMap.empty()) {
auto ps4KeyOpt = keysMappingProvider->mapKey(event->key.key);
std::optional<KeysMapping> ps4KeyOpt;
auto foundIt = keysBindingsMap.find(event->key.key);
if (foundIt != keysBindingsMap.end()) {
ps4KeyOpt = foundIt->second;
}
// No support for modifiers (yet) // No support for modifiers (yet)
if (ps4KeyOpt.has_value() && (event->key.mod == SDL_KMOD_NONE)) { 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); 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 } // namespace Frontend

View File

@ -3,9 +3,10 @@
#pragma once #pragma once
#include <map>
#include <string> #include <string>
#include "common/types.h" #include "common/types.h"
#include "input/keysmappingprovider.h" #include "input/keys_constants.h"
struct SDL_Window; struct SDL_Window;
struct SDL_Gamepad; struct SDL_Gamepad;
@ -66,7 +67,7 @@ public:
void waitEvent(); void waitEvent();
void setKeysMappingProvider(KeysMappingProvider* provider); void setKeysBindingsMap(const std::map<u32, KeysMapping>& bindingsMap);
private: private:
void onResize(); void onResize();
@ -102,7 +103,7 @@ private:
Input::GameController* controller; Input::GameController* controller;
WindowSystemInfo window_info{}; WindowSystemInfo window_info{};
SDL_Window* window{}; SDL_Window* window{};
KeysMappingProvider* keysMappingProvider = nullptr; std::map<u32, KeysMapping> keysBindingsMap;
bool is_shown{}; bool is_shown{};
bool is_open{true}; bool is_open{true};
}; };