diff --git a/.gitmodules b/.gitmodules index e3dda94f..6e4eac2b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -94,4 +94,7 @@ [submodule "externals/pugixml"] path = externals/pugixml url = https://github.com/zeux/pugixml.git - shallow = true \ No newline at end of file + shallow = true +[submodule "externals/discord-rpc"] + path = externals/discord-rpc + url = https://github.com/shadps4-emu/ext-discord-rpc diff --git a/CMakeLists.txt b/CMakeLists.txt index 59f15add..2db263b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -374,6 +374,8 @@ set(COMMON src/common/logging/backend.cpp src/common/debug.h src/common/decoder.cpp src/common/decoder.h + src/common/discord_rpc_handler.cpp + src/common/discord_rpc_handler.h src/common/elf_info.h src/common/endian.h src/common/enum.h @@ -857,4 +859,7 @@ if (UNIX AND NOT APPLE) find_package(OpenSSL REQUIRED) target_link_libraries(shadps4 PRIVATE ${OPENSSL_LIBRARIES}) endif() -endif() \ No newline at end of file +endif() + +# Discord RPC +target_link_libraries(shadps4 PRIVATE discord-rpc) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 37c0f689..a528eaed 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -184,5 +184,10 @@ if (NOT TARGET pugixml::pugixml) add_subdirectory(pugixml) endif() +# Discord RPC +set(BUILD_EXAMPLES OFF) +add_subdirectory(discord-rpc/) +target_include_directories(discord-rpc INTERFACE discord-rpc/include) + # GCN Headers add_subdirectory(gcn) \ No newline at end of file diff --git a/externals/discord-rpc b/externals/discord-rpc new file mode 160000 index 00000000..80d35b5f --- /dev/null +++ b/externals/discord-rpc @@ -0,0 +1 @@ +Subproject commit 80d35b5f86adc6557d0384771119cf167495dbae diff --git a/src/common/config.cpp b/src/common/config.cpp index a7d01f53..40be5ebe 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -34,6 +34,7 @@ static bool isNeo = false; static bool isFullscreen = false; static bool playBGM = false; static int BGMvolume = 50; +static bool enableDiscordRPC = false; static u32 screenWidth = 1280; static u32 screenHeight = 720; static s32 gpuId = -1; // Vulkan physical device index. Set to negative for auto select @@ -98,6 +99,10 @@ int getBGMvolume() { return BGMvolume; } +bool getEnableDiscordRPC() { + return enableDiscordRPC; +} + s16 getCursorState() { return cursorState; } @@ -266,6 +271,10 @@ void setBGMvolume(int volume) { BGMvolume = volume; } +void setEnableDiscordRPC(bool enable) { + enableDiscordRPC = enable; +} + void setCursorState(s16 newCursorState) { cursorState = newCursorState; } @@ -452,6 +461,7 @@ void load(const std::filesystem::path& path) { isFullscreen = toml::find_or(general, "Fullscreen", false); playBGM = toml::find_or(general, "playBGM", false); BGMvolume = toml::find_or(general, "BGMvolume", 50); + enableDiscordRPC = toml::find_or(general, "enableDiscordRPC", true); logFilter = toml::find_or(general, "logFilter", ""); logType = toml::find_or(general, "logType", "sync"); userName = toml::find_or(general, "userName", "shadPS4"); @@ -557,6 +567,7 @@ void save(const std::filesystem::path& path) { data["General"]["Fullscreen"] = isFullscreen; data["General"]["playBGM"] = playBGM; data["General"]["BGMvolume"] = BGMvolume; + data["General"]["enableDiscordRPC"] = enableDiscordRPC; data["General"]["logFilter"] = logFilter; data["General"]["logType"] = logType; data["General"]["userName"] = userName; @@ -614,6 +625,7 @@ void setDefaultValues() { isFullscreen = false; playBGM = false; BGMvolume = 50; + enableDiscordRPC = true; cursorState = HideCursorState::Idle; cursorHideTimeout = 5; screenWidth = 1280; diff --git a/src/common/config.h b/src/common/config.h index 32cfc605..90ebdb58 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -18,6 +18,7 @@ bool isNeoMode(); bool isFullscreenMode(); bool getPlayBGM(); int getBGMvolume(); +bool getEnableDiscordRPC(); s16 getCursorState(); int getCursorHideTimeout(); @@ -57,6 +58,7 @@ void setScreenHeight(u32 height); void setFullscreenMode(bool enable); void setPlayBGM(bool enable); void setBGMvolume(int volume); +void setEnableDiscordRPC(bool enable); void setCursorState(s16 cursorState); void setCursorHideTimeout(int newcursorHideTimeout); void setLanguage(u32 language); diff --git a/src/common/discord.cpp b/src/common/discord.cpp deleted file mode 100644 index cce799a3..00000000 --- a/src/common/discord.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include -#include -#include "common/discord.h" - -namespace Discord { - -void RPC::init() { - DiscordEventHandlers handlers{}; - Discord_Initialize("1139939140494971051", &handlers, 1, nullptr); - - startTimestamp = time(nullptr); - enabled = true; -} - -void RPC::update(Discord::RPCStatus status, const std::string& game) { - DiscordRichPresence rpc{}; - - if (status == Discord::RPCStatus::Playing) { - rpc.details = "Playing a game"; - rpc.state = game.c_str(); - } else { - rpc.details = "Idle"; - } - - rpc.largeImageKey = "shadps4"; - rpc.largeImageText = "ShadPS4 is a PS4 emulator"; - rpc.startTimestamp = startTimestamp; - - Discord_UpdatePresence(&rpc); -} - -void RPC::stop() { - if (enabled) { - enabled = false; - Discord_ClearPresence(); - Discord_Shutdown(); - } -} - -} // namespace Discord diff --git a/src/common/discord_rpc_handler.cpp b/src/common/discord_rpc_handler.cpp new file mode 100644 index 00000000..8b6e78c5 --- /dev/null +++ b/src/common/discord_rpc_handler.cpp @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include "src/common/discord_rpc_handler.h" + +namespace DiscordRPCHandler { + +void RPC::init() { + DiscordEventHandlers handlers{}; + + Discord_Initialize("1138176975865909360", &handlers, 1, nullptr); + startTimestamp = time(nullptr); + rpcEnabled = true; +} + +void RPC::setStatusIdling() { + DiscordRichPresence rpc{}; + rpc.largeImageKey = "https://github.com/shadps4-emu/shadPS4/raw/main/.github/shadps4.png"; + rpc.largeImageText = "shadPS4 is a PS4 emulator"; + rpc.startTimestamp = startTimestamp; + rpc.details = "Idle"; + + status = RPCStatus::Idling; + Discord_UpdatePresence(&rpc); +} + +void RPC::setStatusPlaying(const std::string& game_name, const std::string& game_id) { + DiscordRichPresence rpc{}; + + rpc.details = "Playing"; + rpc.state = game_name.c_str(); + std::string largeImageUrl = + "https://store.playstation.com/store/api/chihiro/00_09_000/titlecontainer/US/en/999/" + + game_id + "_00/image"; + rpc.largeImageKey = largeImageUrl.c_str(); + rpc.largeImageText = game_name.c_str(); + rpc.startTimestamp = startTimestamp; + + status = RPCStatus::Playing; + Discord_UpdatePresence(&rpc); +} + +void RPC::shutdown() { + if (rpcEnabled) { + rpcEnabled = false; + Discord_ClearPresence(); + Discord_Shutdown(); + } +} + +bool RPC::getRPCEnabled() { + return rpcEnabled; +} + +} // namespace DiscordRPCHandler diff --git a/src/common/discord.h b/src/common/discord_rpc_handler.h similarity index 53% rename from src/common/discord.h rename to src/common/discord_rpc_handler.h index 54aa6c17..1e451e18 100644 --- a/src/common/discord.h +++ b/src/common/discord_rpc_handler.h @@ -7,7 +7,7 @@ #include #include -namespace Discord { +namespace DiscordRPCHandler { enum class RPCStatus { Idling, @@ -16,12 +16,15 @@ enum class RPCStatus { class RPC { std::uint64_t startTimestamp; - bool enabled = false; + bool rpcEnabled = false; + RPCStatus status; public: void init(); - void update(RPCStatus status, const std::string& title); - void stop(); + void setStatusIdling(); + void setStatusPlaying(const std::string& game_name, const std::string& game_id); + void shutdown(); + bool getRPCEnabled(); }; -} // namespace Discord +} // namespace DiscordRPCHandler diff --git a/src/emulator.cpp b/src/emulator.cpp index 6649b7bb..a9d0f6de 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -11,6 +11,7 @@ #include "common/memory_patcher.h" #endif #include "common/assert.h" +#include "common/discord_rpc_handler.h" #include "common/elf_info.h" #include "common/ntapi.h" #include "common/path_util.h" @@ -210,6 +211,15 @@ void Emulator::Run(const std::filesystem::path& file) { } } + // Discord RPC + if (Config::getEnableDiscordRPC()) { + auto* rpc = Common::Singleton::Instance(); + if (rpc->getRPCEnabled() == false) { + rpc->init(); + } + rpc->setStatusPlaying(game_info.title, id); + } + // start execution std::jthread mainthread = std::jthread([this](std::stop_token stop_token) { linker->Execute(); }); diff --git a/src/qt_gui/main_window.cpp b/src/qt_gui/main_window.cpp index 8d8e1717..f93bdb06 100644 --- a/src/qt_gui/main_window.cpp +++ b/src/qt_gui/main_window.cpp @@ -69,6 +69,14 @@ bool MainWindow::Init() { QString statusMessage = "Games: " + QString::number(numGames) + " (" + QString::number(duration.count()) + "ms)"; statusBar->showMessage(statusMessage); + + // Initialize Discord RPC + if (Config::getEnableDiscordRPC()) { + auto* rpc = Common::Singleton::Instance(); + rpc->init(); + rpc->setStatusIdling(); + } + return true; } diff --git a/src/qt_gui/main_window.h b/src/qt_gui/main_window.h index a428f431..6264978a 100644 --- a/src/qt_gui/main_window.h +++ b/src/qt_gui/main_window.h @@ -9,6 +9,7 @@ #include "background_music_player.h" #include "common/config.h" +#include "common/discord_rpc_handler.h" #include "common/path_util.h" #include "core/file_format/psf.h" #include "core/file_sys/fs.h" diff --git a/src/qt_gui/settings_dialog.cpp b/src/qt_gui/settings_dialog.cpp index 3845ab0c..efc43845 100644 --- a/src/qt_gui/settings_dialog.cpp +++ b/src/qt_gui/settings_dialog.cpp @@ -165,6 +165,17 @@ SettingsDialog::SettingsDialog(std::span physical_devices, QWidge BackgroundMusicPlayer::getInstance().setVolume(val); }); + connect(ui->discordRPCCheckbox, &QCheckBox::stateChanged, this, [](int val) { + Config::setEnableDiscordRPC(val); + auto* rpc = Common::Singleton::Instance(); + if (val == Qt::Checked) { + rpc->init(); + rpc->setStatusIdling(); + } else { + rpc->shutdown(); + } + }); + connect(ui->backButtonBehaviorComboBox, QOverload::of(&QComboBox::currentIndexChanged), this, [this](int index) { if (index >= 0 && index < ui->backButtonBehaviorComboBox->count()) { @@ -273,6 +284,7 @@ void SettingsDialog::LoadValuesFromConfig() { ui->nullGpuCheckBox->setChecked(Config::nullGpu()); ui->playBGMCheckBox->setChecked(Config::getPlayBGM()); ui->BGMVolumeSlider->setValue((Config::getBGMvolume())); + ui->discordRPCCheckbox->setChecked(Config::getEnableDiscordRPC()); ui->fullscreenCheckBox->setChecked(Config::isFullscreenMode()); ui->showSplashCheckBox->setChecked(Config::showSplash()); ui->ps4proCheckBox->setChecked(Config::isNeoMode()); diff --git a/src/qt_gui/settings_dialog.ui b/src/qt_gui/settings_dialog.ui index b686c3f8..ad9cf5ce 100644 --- a/src/qt_gui/settings_dialog.ui +++ b/src/qt_gui/settings_dialog.ui @@ -148,6 +148,13 @@ + + + + Enable Discord Rich Presence + + +