Move KeepScreenAwake to platform specific code.

This commit is contained in:
Henrik Rydgård 2023-08-10 13:21:36 +02:00
parent b7b3e81e2f
commit be708e3e02
5 changed files with 39 additions and 32 deletions

View File

@ -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);

View File

@ -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<CoreLifecycleFunc> lifecycleFuncs;
static std::set<CoreStopRequestFunc> 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;
}
}
}
}
}

View File

@ -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();

View File

@ -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)

View File

@ -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;
}
}
}