Skip to content

Commit

Permalink
Move KeepScreenAwake to platform specific code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Aug 10, 2023
1 parent b7b3e81 commit be708e3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Common/System/System.h
Expand Up @@ -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);
Expand Down
27 changes: 0 additions & 27 deletions Core/Core.cpp
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -84,10 +78,6 @@ bool Core_IsWindowHidden() {
return windowHidden;
}

void Core_NotifyActivity() {
lastActivity = time_now_d();
}

void Core_ListenLifecycle(CoreLifecycleFunc func) {
lifecycleFuncs.insert(func);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Core/Core.h
Expand Up @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions UI/EmuScreen.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions Windows/main.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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), "/", "\\");
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit be708e3

Please sign in to comment.