Introduced EmulatorState (#223)

This commit is contained in:
georgemoralis
2025-12-27 12:37:50 +02:00
committed by GitHub
parent f7f3936339
commit 5df9a153f0
10 changed files with 88 additions and 31 deletions

View File

@@ -299,6 +299,8 @@ set(CORE src/core/file_format/elf.cpp
src/core/file_format/psf.h src/core/file_format/psf.h
src/core/file_format/trp.cpp src/core/file_format/trp.cpp
src/core/file_format/trp.h src/core/file_format/trp.h
src/core/emulator_state.cpp
src/core/emulator_state.h
) )
set(INPUT src/input/controller.cpp set(INPUT src/input/controller.cpp

View File

@@ -222,15 +222,6 @@ static string config_version = Common::g_scm_rev;
// These entries aren't stored in the config // These entries aren't stored in the config
static bool overrideControllerColor = false; static bool overrideControllerColor = false;
static int controllerCustomColorRGB[3] = {0, 0, 255}; static int controllerCustomColorRGB[3] = {0, 0, 255};
static bool isGameRunning = false;
bool getGameRunning() {
return isGameRunning;
}
void setGameRunning(bool running) {
isGameRunning = running;
}
std::filesystem::path getSysModulesPath() { std::filesystem::path getSysModulesPath() {
if (sys_modules_path.empty()) { if (sys_modules_path.empty()) {

View File

@@ -27,8 +27,6 @@ void load(const std::filesystem::path& path, bool is_game_specific = false);
void save(const std::filesystem::path& path, bool is_game_specific = false); void save(const std::filesystem::path& path, bool is_game_specific = false);
void resetGameSpecificValue(std::string entry); void resetGameSpecificValue(std::string entry);
bool getGameRunning();
void setGameRunning(bool running);
int getVolumeSlider(); int getVolumeSlider();
void setVolumeSlider(int volumeValue, bool is_game_specific = false); void setVolumeSlider(int volumeValue, bool is_game_specific = false);
std::string getTrophyKey(); std::string getTrophyKey();

View File

@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: Copyright 2025 shadLauncher4 Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "emulator_state.h"
std::shared_ptr<EmulatorState> EmulatorState::s_instance = nullptr;
std::mutex EmulatorState::s_mutex;
EmulatorState::EmulatorState() {}
EmulatorState::~EmulatorState() {}
std::shared_ptr<EmulatorState> EmulatorState::GetInstance() {
std::lock_guard<std::mutex> lock(s_mutex);
if (!s_instance)
s_instance = std::make_shared<EmulatorState>();
return s_instance;
}
void EmulatorState::SetInstance(std::shared_ptr<EmulatorState> instance) {
std::lock_guard<std::mutex> lock(s_mutex);
s_instance = instance;
}
bool EmulatorState::IsGameRunning() const {
return m_running;
}
void EmulatorState::SetGameRunning(bool running) {
m_running = running;
}

26
src/core/emulator_state.h Normal file
View File

@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: Copyright 2025 shadLauncher4 Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <memory>
#include <mutex>
class EmulatorState {
public:
EmulatorState();
~EmulatorState();
static std::shared_ptr<EmulatorState> GetInstance();
static void SetInstance(std::shared_ptr<EmulatorState> instance);
bool IsGameRunning() const;
void SetGameRunning(bool running);
private:
static std::shared_ptr<EmulatorState> s_instance;
static std::mutex s_mutex;
// state variables
bool m_running = false;
};

View File

@@ -13,6 +13,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
#include <core/emulator_state.h>
// Custom message handler to ignore Qt logs // Custom message handler to ignore Qt logs
void customMessageHandler(QtMsgType, const QMessageLogContext&, const QString&) {} void customMessageHandler(QtMsgType, const QMessageLogContext&, const QString&) {}
@@ -29,6 +30,8 @@ int main(int argc, char* argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
QApplication::setDesktopFileName("net.shadps4.qtlauncher"); QApplication::setDesktopFileName("net.shadps4.qtlauncher");
std::shared_ptr<EmulatorState> m_emu_state = std::make_shared<EmulatorState>();
EmulatorState::SetInstance(m_emu_state);
// Load configurations and initialize Qt application // Load configurations and initialize Qt application
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);

View File

@@ -32,6 +32,7 @@
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/memory_patcher.h" #include "common/memory_patcher.h"
#include "common/path_util.h" #include "common/path_util.h"
#include "core/emulator_state.h"
CheatsPatches::CheatsPatches(std::shared_ptr<gui_settings> gui_settings, CheatsPatches::CheatsPatches(std::shared_ptr<gui_settings> gui_settings,
std::shared_ptr<IpcClient> ipc_client, const QString& gameName, std::shared_ptr<IpcClient> ipc_client, const QString& gameName,
@@ -1246,7 +1247,7 @@ void CheatsPatches::applyCheat(const QString& modName, bool enabled) {
if (!m_cheats.contains(modName)) if (!m_cheats.contains(modName))
return; return;
if (!Config::getGameRunning() && enabled) { if (!EmulatorState::GetInstance()->IsGameRunning() && enabled) {
QMessageBox::critical(this, tr("Error"), QMessageBox::critical(this, tr("Error"),
tr("Can't apply cheats before the game is started")); tr("Can't apply cheats before the game is started"));
uncheckAllCheatCheckBoxes(); uncheckAllCheatCheckBoxes();
@@ -1262,7 +1263,7 @@ void CheatsPatches::applyCheat(const QString& modName, bool enabled) {
std::string offsetStr = memoryMod.offset.toStdString(); std::string offsetStr = memoryMod.offset.toStdString();
std::string valueStr = value.toStdString(); std::string valueStr = value.toStdString();
if (!Config::getGameRunning()) if (!EmulatorState::GetInstance()->IsGameRunning())
return; return;
// Determine if the hint field is present // Determine if the hint field is present

View File

@@ -19,6 +19,7 @@
#include "common/path_util.h" #include "common/path_util.h"
#include "common/scm_rev.h" #include "common/scm_rev.h"
#include "compatibility_info.h" #include "compatibility_info.h"
#include "core/emulator_state.h"
#include "create_shortcut.h" #include "create_shortcut.h"
#include "game_info.h" #include "game_info.h"
#include "gui_settings.h" #include "gui_settings.h"
@@ -446,9 +447,9 @@ public:
} }
if (selected == &gameConfigConfigure || selected == &gameConfigCreate) { if (selected == &gameConfigConfigure || selected == &gameConfigCreate) {
auto settingsWindow = auto settingsWindow = new SettingsDialog(
new SettingsDialog(m_gui_settings, m_compat_info, m_ipc_client, widget, m_gui_settings, m_compat_info, m_ipc_client, widget,
Config::getGameRunning(), true, serialStr.toStdString()); EmulatorState::GetInstance()->IsGameRunning(), true, serialStr.toStdString());
settingsWindow->exec(); settingsWindow->exec();
} }

View File

@@ -18,6 +18,7 @@
#include "common/scm_rev.h" #include "common/scm_rev.h"
#include "common/versions.h" #include "common/versions.h"
#include "control_settings.h" #include "control_settings.h"
#include "core/emulator_state.h"
#include "dimensions_dialog.h" #include "dimensions_dialog.h"
#include "game_install_dialog.h" #include "game_install_dialog.h"
#include "hotkeys.h" #include "hotkeys.h"
@@ -140,7 +141,7 @@ void MainWindow::StopGame() {
} }
void MainWindow::onGameClosed() { void MainWindow::onGameClosed() {
Config::setGameRunning(false); EmulatorState::GetInstance()->SetGameRunning(false);
is_paused = false; is_paused = false;
// clear dialogs when game closed // clear dialogs when game closed
@@ -160,7 +161,7 @@ void MainWindow::toggleLabelsUnderIcons() {
bool showLabels = ui->toggleLabelsAct->isChecked(); bool showLabels = ui->toggleLabelsAct->isChecked();
m_gui_settings->SetValue(gui::mw_showLabelsUnderIcons, showLabels); m_gui_settings->SetValue(gui::mw_showLabelsUnderIcons, showLabels);
UpdateToolbarLabels(); UpdateToolbarLabels();
if (Config::getGameRunning()) { if (EmulatorState::GetInstance()->IsGameRunning()) {
UpdateToolbarButtons(); UpdateToolbarButtons();
} }
} }
@@ -485,7 +486,7 @@ void MainWindow::CreateConnects() {
connect(ui->configureAct, &QAction::triggered, this, [this]() { connect(ui->configureAct, &QAction::triggered, this, [this]() {
auto settingsDialog = new SettingsDialog(m_gui_settings, m_compat_info, m_ipc_client, this, auto settingsDialog = new SettingsDialog(m_gui_settings, m_compat_info, m_ipc_client, this,
Config::getGameRunning()); EmulatorState::GetInstance()->IsGameRunning());
connect(settingsDialog, &SettingsDialog::LanguageChanged, this, connect(settingsDialog, &SettingsDialog::LanguageChanged, this,
&MainWindow::OnLanguageChanged); &MainWindow::OnLanguageChanged);
@@ -520,7 +521,7 @@ void MainWindow::CreateConnects() {
connect(ui->settingsButton, &QPushButton::clicked, this, [this]() { connect(ui->settingsButton, &QPushButton::clicked, this, [this]() {
auto settingsDialog = new SettingsDialog(m_gui_settings, m_compat_info, m_ipc_client, this, auto settingsDialog = new SettingsDialog(m_gui_settings, m_compat_info, m_ipc_client, this,
Config::getGameRunning()); EmulatorState::GetInstance()->IsGameRunning());
connect(settingsDialog, &SettingsDialog::LanguageChanged, this, connect(settingsDialog, &SettingsDialog::LanguageChanged, this,
&MainWindow::OnLanguageChanged); &MainWindow::OnLanguageChanged);
@@ -555,13 +556,15 @@ void MainWindow::CreateConnects() {
connect(ui->controllerButton, &QPushButton::clicked, this, [this]() { connect(ui->controllerButton, &QPushButton::clicked, this, [this]() {
ControlSettings* remapWindow = new ControlSettings( ControlSettings* remapWindow = new ControlSettings(
m_game_info, m_ipc_client, Config::getGameRunning(), runningGameSerial, this); m_game_info, m_ipc_client, EmulatorState::GetInstance()->IsGameRunning(),
runningGameSerial, this);
remapWindow->exec(); remapWindow->exec();
}); });
connect(ui->keyboardButton, &QPushButton::clicked, this, [this]() { connect(ui->keyboardButton, &QPushButton::clicked, this, [this]() {
auto kbmWindow = new KBMSettings(m_game_info, m_ipc_client, Config::getGameRunning(), auto kbmWindow =
runningGameSerial, this); new KBMSettings(m_game_info, m_ipc_client,
EmulatorState::GetInstance()->IsGameRunning(), runningGameSerial, this);
kbmWindow->exec(); kbmWindow->exec();
}); });
@@ -584,7 +587,8 @@ void MainWindow::CreateConnects() {
}); });
connect(ui->configureHotkeys, &QAction::triggered, this, [this]() { connect(ui->configureHotkeys, &QAction::triggered, this, [this]() {
auto hotkeyDialog = new Hotkeys(m_ipc_client, Config::getGameRunning(), this); auto hotkeyDialog =
new Hotkeys(m_ipc_client, EmulatorState::GetInstance()->IsGameRunning(), this);
hotkeyDialog->exec(); hotkeyDialog->exec();
}); });
@@ -1338,7 +1342,7 @@ bool MainWindow::eventFilter(QObject* obj, QEvent* event) {
} }
void MainWindow::StartEmulator(std::filesystem::path path, QStringList args) { void MainWindow::StartEmulator(std::filesystem::path path, QStringList args) {
if (Config::getGameRunning()) { if (EmulatorState::GetInstance()->IsGameRunning()) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Game is already running!"))); QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Game is already running!")));
return; return;
} }
@@ -1366,7 +1370,7 @@ tr("No emulator version was selected.\nThe Version Manager menu will then open.\
final_args.append(args); final_args.append(args);
Config::setGameRunning(true); EmulatorState::GetInstance()->SetGameRunning(true);
last_game_path = path; last_game_path = path;
QString workDir = QDir::currentPath(); QString workDir = QDir::currentPath();
@@ -1376,7 +1380,7 @@ tr("No emulator version was selected.\nThe Version Manager menu will then open.\
void MainWindow::StartEmulatorExecutable(std::filesystem::path emuPath, QString gameArg, void MainWindow::StartEmulatorExecutable(std::filesystem::path emuPath, QString gameArg,
QStringList args, bool disable_ipc) { QStringList args, bool disable_ipc) {
if (Config::getGameRunning()) { if (EmulatorState::GetInstance()->IsGameRunning()) {
QMessageBox::critical(nullptr, tr("Run Emulator"), QMessageBox::critical(nullptr, tr("Run Emulator"),
QString(tr("Emulator is already running!"))); QString(tr("Emulator is already running!")));
return; return;
@@ -1436,7 +1440,7 @@ void MainWindow::StartEmulatorExecutable(std::filesystem::path emuPath, QString
return; return;
} }
Config::setGameRunning(true); EmulatorState::GetInstance()->SetGameRunning(true);
QString workDir = QDir::currentPath(); QString workDir = QDir::currentPath();
m_ipc_client->startEmulator(fileInfo, args, workDir, disable_ipc); m_ipc_client->startEmulator(fileInfo, args, workDir, disable_ipc);
} }

View File

@@ -22,6 +22,7 @@
#include "background_music_player.h" #include "background_music_player.h"
#include "common/logging/backend.h" #include "common/logging/backend.h"
#include "common/logging/filter.h" #include "common/logging/filter.h"
#include "core/emulator_state.h"
#include "log_presets_dialog.h" #include "log_presets_dialog.h"
#include "sdl_event_wrapper.h" #include "sdl_event_wrapper.h"
#include "settings_dialog.h" #include "settings_dialog.h"
@@ -243,7 +244,7 @@ SettingsDialog::SettingsDialog(std::shared_ptr<gui_settings> gui_settings,
connect(ui->horizontalVolumeSlider, &QSlider::valueChanged, this, [this](int value) { connect(ui->horizontalVolumeSlider, &QSlider::valueChanged, this, [this](int value) {
VolumeSliderChange(value); VolumeSliderChange(value);
if (Config::getGameRunning()) if (EmulatorState::GetInstance()->IsGameRunning())
m_ipc_client->adjustVol(value, is_game_specific); m_ipc_client->adjustVol(value, is_game_specific);
}); });
@@ -448,7 +449,7 @@ SettingsDialog::SettingsDialog(std::shared_ptr<gui_settings> gui_settings,
ui->RCASValue->setText(RCASValue); ui->RCASValue->setText(RCASValue);
}); });
if (Config::getGameRunning()) { if (EmulatorState::GetInstance()->IsGameRunning()) {
connect(ui->RCASSlider, &QSlider::valueChanged, this, connect(ui->RCASSlider, &QSlider::valueChanged, this,
[this](int value) { m_ipc_client->setRcasAttenuation(value); }); [this](int value) { m_ipc_client->setRcasAttenuation(value); });
connect(ui->FSRCheckBox, &QCheckBox::checkStateChanged, this, connect(ui->FSRCheckBox, &QCheckBox::checkStateChanged, this,
@@ -1213,7 +1214,7 @@ void SettingsDialog::SyncRealTimeWidgetstoConfig() {
is_game_specific ? Config::resetGameSpecificValue("volumeSlider") is_game_specific ? Config::resetGameSpecificValue("volumeSlider")
: Config::setVolumeSlider(sliderValue); : Config::setVolumeSlider(sliderValue);
if (Config::getGameRunning()) { if (EmulatorState::GetInstance()->IsGameRunning()) {
m_ipc_client->setFsr(toml::find_or<bool>(gs_data, "GPU", "fsrEnabled", true)); m_ipc_client->setFsr(toml::find_or<bool>(gs_data, "GPU", "fsrEnabled", true));
m_ipc_client->setRcas(toml::find_or<bool>(gs_data, "GPU", "rcasEnabled", true)); m_ipc_client->setRcas(toml::find_or<bool>(gs_data, "GPU", "rcasEnabled", true));
m_ipc_client->setRcasAttenuation( m_ipc_client->setRcasAttenuation(