Merge branch 'fixDRPTimer' into 'main'

fix: Discord Rich Presence Timer

See merge request citron/emulator!76
This commit is contained in:
Zephyron
2025-10-03 10:54:09 +10:00
2 changed files with 37 additions and 24 deletions

View File

@@ -39,6 +39,11 @@ namespace DiscordRPC {
Discord_Initialize("1361252452329848892", &handlers, 1, nullptr); Discord_Initialize("1361252452329848892", &handlers, 1, nullptr);
// Initialize the timer for the first state (being in the menu).
current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
was_powered_on = false; // Start in the "off" state.
discord_thread_running = true; discord_thread_running = true;
discord_thread = std::thread(&DiscordImpl::ThreadRun, this); discord_thread = std::thread(&DiscordImpl::ThreadRun, this);
} }
@@ -61,9 +66,6 @@ namespace DiscordRPC {
void DiscordImpl::UpdateGameStatus(bool use_default) { void DiscordImpl::UpdateGameStatus(bool use_default) {
const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch"; const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch";
const std::string default_image = "citron_logo"; const std::string default_image = "citron_logo";
s64 start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
DiscordRichPresence presence{}; DiscordRichPresence presence{};
if (!use_default && !game_title_id.empty()) { if (!use_default && !game_title_id.empty()) {
@@ -79,15 +81,23 @@ namespace DiscordRPC {
presence.smallImageText = default_text.c_str(); presence.smallImageText = default_text.c_str();
presence.details = game_title.c_str(); presence.details = game_title.c_str();
presence.state = "Currently in game"; presence.state = "Currently in game";
presence.startTimestamp = start_time; presence.startTimestamp = current_state_start_time; // Use the state-based timer
Discord_UpdatePresence(&presence); Discord_UpdatePresence(&presence);
} }
void DiscordImpl::Update() { void DiscordImpl::Update() {
const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch"; const bool is_powered_on = system.IsPoweredOn();
const std::string default_image = "citron_logo";
if (system.IsPoweredOn()) { if (is_powered_on != was_powered_on) {
// State changed! Reset the timer
current_state_start_time = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
// Update the state tracker.
was_powered_on = is_powered_on;
}
if (is_powered_on) {
// Game is running.
system.GetAppLoader().ReadTitle(game_title); system.GetAppLoader().ReadTitle(game_title);
system.GetAppLoader().ReadProgramId(program_id); system.GetAppLoader().ReadProgramId(program_id);
game_title_id = fmt::format("{:016X}", program_id); game_title_id = fmt::format("{:016X}", program_id);
@@ -96,7 +106,7 @@ namespace DiscordRPC {
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QUrl(QString::fromStdString( request.setUrl(QUrl(QString::fromStdString(
fmt::format("https://tinfoil.media/ti/{}/256/256", game_title_id)))); fmt::format("https://tinfoil.media/ti/{}/256/256", game_title_id))));
request.setTransferTimeout(5000); // 5 second timeout request.setTransferTimeout(5000);
QNetworkReply* reply = manager.head(request); QNetworkReply* reply = manager.head(request);
QEventLoop request_event_loop; QEventLoop request_event_loop;
QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit); QObject::connect(reply, &QNetworkReply::finished, &request_event_loop, &QEventLoop::quit);
@@ -105,21 +115,21 @@ namespace DiscordRPC {
UpdateGameStatus(reply->error() != QNetworkReply::NoError); UpdateGameStatus(reply->error() != QNetworkReply::NoError);
reply->deleteLater(); reply->deleteLater();
} else { } else {
s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( // Game is NOT running (in menus).
std::chrono::system_clock::now().time_since_epoch()) const std::string default_text = "Citron Is A Homebrew Emulator For The Nintendo Switch";
.count(); const std::string default_image = "citron_logo";
DiscordRichPresence presence{}; DiscordRichPresence presence{};
presence.largeImageKey = default_image.c_str(); presence.largeImageKey = default_image.c_str();
presence.largeImageText = default_text.c_str(); presence.largeImageText = default_text.c_str();
presence.details = "Currently not in game"; presence.details = "In the Menus";
presence.startTimestamp = start_time; presence.startTimestamp = current_state_start_time; // Use the state-based timer
Discord_UpdatePresence(&presence); Discord_UpdatePresence(&presence);
} }
} }
void DiscordImpl::ThreadRun() { void DiscordImpl::ThreadRun() {
while (discord_thread_running) { while (discord_thread_running) {
Update(); // This is the line that was likely missing. Update();
Discord_RunCallbacks(); Discord_RunCallbacks();
std::this_thread::sleep_for(std::chrono::seconds(15)); std::this_thread::sleep_for(std::chrono::seconds(15));
} }

View File

@@ -4,8 +4,8 @@
#pragma once #pragma once
#include <atomic> #include <atomic>
#include <cstdint>
#include <thread> #include <thread>
#include "citron/discord.h" #include "citron/discord.h"
@@ -23,7 +23,6 @@ namespace DiscordRPC {
void Pause() override; void Pause() override;
void Update() override; void Update() override;
// FIX: Add the function and members for the background thread
void ThreadRun(); void ThreadRun();
std::thread discord_thread; std::thread discord_thread;
std::atomic<bool> discord_thread_running; std::atomic<bool> discord_thread_running;
@@ -38,6 +37,10 @@ namespace DiscordRPC {
Core::System& system; Core::System& system;
u64 program_id = 0; u64 program_id = 0;
s64 current_state_start_time = 0;
bool was_powered_on = false;
}; };
} // namespace DiscordRPC } // namespace DiscordRPC