From be708e3e02aa551a844dd83f78fa525e645eede1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 10 Aug 2023 13:21:36 +0200 Subject: [PATCH] Move KeepScreenAwake to platform specific code. --- Common/System/System.h | 2 ++ Core/Core.cpp | 27 --------------------------- Core/Core.h | 1 - UI/EmuScreen.cpp | 11 +++++++---- Windows/main.cpp | 30 ++++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Common/System/System.h b/Common/System/System.h index 17eeda194d..9652c0d32a 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -200,6 +200,8 @@ enum class SystemNotification { POLL_CONTROLLERS, TOGGLE_DEBUG_CONSOLE, // TODO: Kinda weird, just ported forward. TEST_JAVA_EXCEPTION, + KEEP_SCREEN_AWAKE, + ACTIVITY, }; std::string System_GetProperty(SystemProperty prop); diff --git a/Core/Core.cpp b/Core/Core.cpp index a48759e130..6c75ea419d 100644 --- a/Core/Core.cpp +++ b/Core/Core.cpp @@ -48,10 +48,6 @@ #include "Windows/InputDevice.h" #endif -// Time until we stop considering the core active without user input. -// Should this be configurable? 2 hours currently. -static const double ACTIVITY_IDLE_TIMEOUT = 2.0 * 3600.0; - static std::condition_variable m_StepCond; static std::mutex m_hStepMutex; static std::condition_variable m_InactiveCond; @@ -63,8 +59,6 @@ static uint32_t steppingAddress = 0; static std::set lifecycleFuncs; static std::set stopFuncs; static bool windowHidden = false; -static double lastActivity = 0.0; -static double lastKeepAwake = 0.0; static GraphicsContext *graphicsContext; static bool powerSaving = false; @@ -84,10 +78,6 @@ bool Core_IsWindowHidden() { return windowHidden; } -void Core_NotifyActivity() { - lastActivity = time_now_d(); -} - void Core_ListenLifecycle(CoreLifecycleFunc func) { lifecycleFuncs.insert(func); } @@ -217,12 +207,6 @@ void UpdateRunLoop() { NativeFrame(graphicsContext); } -void KeepScreenAwake() { -#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP) - SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED); -#endif -} - void Core_RunLoop(GraphicsContext *ctx) { float refreshRate = System_GetPropertyFloat(SYSPROP_DISPLAY_REFRESH_RATE); @@ -247,17 +231,6 @@ void Core_RunLoop(GraphicsContext *ctx) { UpdateRunLoop(); if (!windowHidden && !Core_IsStepping()) { ctx->SwapBuffers(); - - // Keep the system awake for longer than normal for cutscenes and the like. - const double now = time_now_d(); - if (now < lastActivity + ACTIVITY_IDLE_TIMEOUT) { - // Only resetting it ever prime number seconds in case the call is expensive. - // Using a prime number to ensure there's no interaction with other periodic events. - if (now - lastKeepAwake > 89.0 || now < lastKeepAwake) { - KeepScreenAwake(); - lastKeepAwake = now; - } - } } } } diff --git a/Core/Core.h b/Core/Core.h index 74d50a337e..65e78e6280 100644 --- a/Core/Core.h +++ b/Core/Core.h @@ -84,7 +84,6 @@ bool UpdateScreenScale(int width, int height); // Don't run the core when minimized etc. void Core_NotifyWindowHidden(bool hidden); bool Core_IsWindowHidden(); -void Core_NotifyActivity(); void Core_SetPowerSaving(bool mode); bool Core_GetPowerSaving(); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 5d159f07b3..597e5a4874 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -564,7 +564,7 @@ void EmuScreen::sendMessage(const char *message, const char *value) { } void EmuScreen::UnsyncTouch(const TouchInput &touch) { - Core_NotifyActivity(); + System_Notify(SystemNotification::ACTIVITY); if (chatMenu_ && chatMenu_->GetVisibility() == UI::V_VISIBLE) { // Avoid pressing touch button behind the chat @@ -830,7 +830,7 @@ void EmuScreen::onVKeyAnalog(int virtualKeyCode, float value) { } bool EmuScreen::UnsyncKey(const KeyInput &key) { - Core_NotifyActivity(); + System_Notify(SystemNotification::ACTIVITY); if (UI::IsFocusMovementEnabled()) { if (UIScreen::UnsyncKey(key)) { @@ -849,8 +849,7 @@ bool EmuScreen::UnsyncKey(const KeyInput &key) { } void EmuScreen::UnsyncAxis(const AxisInput &axis) { - Core_NotifyActivity(); - + System_Notify(SystemNotification::ACTIVITY); return controlMapper_.Axis(axis); } @@ -1399,6 +1398,10 @@ void EmuScreen::render() { g_OSD.NudgeSidebar(); + if (screenManager()->topScreen() == this) { + System_Notify(SystemNotification::KEEP_SCREEN_AWAKE); + } + if (invalid_) { // Loading, or after shutdown? if (loadingTextView_->GetVisibility() == UI::V_VISIBLE) diff --git a/Windows/main.cpp b/Windows/main.cpp index 6cbd2e6243..658ec85b86 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -43,6 +43,7 @@ #include "Common/Thread/ThreadUtil.h" #include "Common/Data/Encoding/Utf8.h" #include "Common/Net/Resolve.h" +#include "Common/TimeUtil.h" #include "W32Util/DarkMode.h" #include "W32Util/ShellUtil.h" @@ -115,6 +116,12 @@ WindowsInputManager g_inputManager; int g_lastNumInstances = 0; +static double g_lastActivity = 0.0; +static double g_lastKeepAwake = 0.0; +// Time until we stop considering the core active without user input. +// Should this be configurable? 2 hours currently. +static const double ACTIVITY_IDLE_TIMEOUT = 2.0 * 3600.0; + void System_ShowFileInFolder(const char *path) { // SHParseDisplayName can't handle relative paths, so normalize first. std::string resolved = ReplaceAll(File::ResolvePath(path), "/", "\\"); @@ -444,6 +451,29 @@ void System_Notify(SystemNotification notification) { case SystemNotification::TOGGLE_DEBUG_CONSOLE: MainWindow::ToggleDebugConsoleVisibility(); break; + + case SystemNotification::ACTIVITY: + g_lastActivity = time_now_d(); + break; + + case SystemNotification::KEEP_SCREEN_AWAKE: + { + // Keep the system awake for longer than normal for cutscenes and the like. + const double now = time_now_d(); + if (now < g_lastActivity + ACTIVITY_IDLE_TIMEOUT) { + // Only resetting it ever prime number seconds in case the call is expensive. + // Using a prime number to ensure there's no interaction with other periodic events. + if (now - g_lastKeepAwake > 89.0 || now < g_lastKeepAwake) { + // Note that this needs to be called periodically. + // It's also possible to set ES_CONTINUOUS but let's not, for simplicity. +#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP) + SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED); +#endif + g_lastKeepAwake = now; + } + } + break; + } } }