Clean up screen saver inhibition and apply setting change immediately.

This commit is contained in:
Jordan Woyak 2020-10-18 13:06:11 -05:00
parent fa83a3a114
commit 50ec747840
5 changed files with 33 additions and 48 deletions

View File

@ -107,6 +107,9 @@ bool PlatformWin32::Init()
ProcessEvents(); ProcessEvents();
} }
if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER))
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
UpdateWindowPosition(); UpdateWindowPosition();
return true; return true;
} }

View File

@ -737,7 +737,6 @@ void MainWindow::Play(const std::optional<std::string>& savestate_path)
if (selection) if (selection)
{ {
StartGame(selection->GetFilePath(), ScanForSecondDisc::Yes, savestate_path); StartGame(selection->GetFilePath(), ScanForSecondDisc::Yes, savestate_path);
EnableScreenSaver(false);
} }
else else
{ {
@ -745,7 +744,6 @@ void MainWindow::Play(const std::optional<std::string>& savestate_path)
if (!default_path.isEmpty() && QFile::exists(default_path)) if (!default_path.isEmpty() && QFile::exists(default_path))
{ {
StartGame(default_path, ScanForSecondDisc::Yes, savestate_path); StartGame(default_path, ScanForSecondDisc::Yes, savestate_path);
EnableScreenSaver(false);
} }
else else
{ {
@ -776,7 +774,6 @@ void MainWindow::OnStopComplete()
{ {
m_stop_requested = false; m_stop_requested = false;
HideRenderWidget(); HideRenderWidget();
EnableScreenSaver(true);
#ifdef USE_DISCORD_PRESENCE #ifdef USE_DISCORD_PRESENCE
if (!m_netplay_dialog->isVisible()) if (!m_netplay_dialog->isVisible())
Discord::UpdateDiscordPresence(); Discord::UpdateDiscordPresence();
@ -993,13 +990,6 @@ void MainWindow::StartGame(std::unique_ptr<BootParameters>&& parameters)
if (Config::Get(Config::MAIN_FULLSCREEN)) if (Config::Get(Config::MAIN_FULLSCREEN))
m_fullscreen_requested = true; m_fullscreen_requested = true;
#ifdef Q_OS_WIN
// Prevents Windows from sleeping, turning off the display, or idling
EXECUTION_STATE shouldScreenSave =
Config::Get(Config::MAIN_DISABLE_SCREENSAVER) ? ES_DISPLAY_REQUIRED : 0;
SetThreadExecutionState(ES_CONTINUOUS | shouldScreenSave | ES_SYSTEM_REQUIRED);
#endif
} }
void MainWindow::SetFullScreenResolution(bool fullscreen) void MainWindow::SetFullScreenResolution(bool fullscreen)
@ -1304,6 +1294,10 @@ void MainWindow::NetPlayInit()
Discord::InitNetPlayFunctionality(*m_netplay_discord); Discord::InitNetPlayFunctionality(*m_netplay_discord);
m_netplay_discord->Start(); m_netplay_discord->Start();
#endif #endif
connect(&Settings::Instance(), &Settings::ConfigChanged, this,
&MainWindow::UpdateScreenSaverInhibition);
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
&MainWindow::UpdateScreenSaverInhibition);
} }
bool MainWindow::NetPlayJoin() bool MainWindow::NetPlayJoin()
@ -1435,13 +1429,16 @@ void MainWindow::NetPlayQuit()
#endif #endif
} }
void MainWindow::EnableScreenSaver(bool enable) void MainWindow::UpdateScreenSaverInhibition()
{ {
const bool inhibit =
Config::Get(Config::MAIN_DISABLE_SCREENSAVER) && (Core::GetState() == Core::State::Running);
#if defined(HAVE_XRANDR) && HAVE_XRANDR #if defined(HAVE_XRANDR) && HAVE_XRANDR
if (GetWindowSystemType() == WindowSystemType::X11) if (GetWindowSystemType() == WindowSystemType::X11)
UICommon::EnableScreenSaver(winId(), enable); UICommon::InhibitScreenSaver(winId(), inhibit);
#else #else
UICommon::EnableScreenSaver(enable); UICommon::InhibitScreenSaver(inhibit);
#endif #endif
} }

View File

@ -182,7 +182,7 @@ private:
QStringList PromptFileNames(); QStringList PromptFileNames();
void EnableScreenSaver(bool enable); void UpdateScreenSaverInhibition();
void OnStopComplete(); void OnStopComplete();
void dragEnterEvent(QDragEnterEvent* event) override; void dragEnterEvent(QDragEnterEvent* event) override;

View File

@ -397,57 +397,42 @@ bool TriggerSTMPowerEvent()
} }
#if defined(HAVE_XRANDR) && HAVE_XRANDR #if defined(HAVE_XRANDR) && HAVE_XRANDR
void EnableScreenSaver(Window win, bool enable) void InhibitScreenSaver(Window win, bool inhibit)
#else #else
void EnableScreenSaver(bool enable) void InhibitScreenSaver(bool inhibit)
#endif #endif
{ {
// Inhibit the screensaver. Depending on the operating system this may also // Inhibit the screensaver. Depending on the operating system this may also
// disable low-power states and/or screen dimming. // disable low-power states and/or screen dimming.
#if defined(HAVE_X11) && HAVE_X11 #if defined(HAVE_X11) && HAVE_X11
if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER)) X11Utils::InhibitScreensaver(win, inhibit);
{
X11Utils::InhibitScreensaver(win, !enable);
}
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
// Prevents Windows from sleeping, turning off the display, or idling // Prevents Windows from sleeping, turning off the display, or idling
if (enable) SetThreadExecutionState(ES_CONTINUOUS |
{ (inhibit ? (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) : 0));
SetThreadExecutionState(ES_CONTINUOUS);
}
else
{
EXECUTION_STATE should_screen_save =
Config::Get(Config::MAIN_DISABLE_SCREENSAVER) ? ES_DISPLAY_REQUIRED : 0;
SetThreadExecutionState(ES_CONTINUOUS | should_screen_save | ES_SYSTEM_REQUIRED);
}
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
static IOPMAssertionID s_power_assertion = kIOPMNullAssertionID; static IOPMAssertionID s_power_assertion = kIOPMNullAssertionID;
if (inhibit)
if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER))
{ {
if (enable) CFStringRef reason_for_activity = CFSTR("Emulation Running");
if (IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep,
kIOPMAssertionLevelOn, reason_for_activity,
&s_power_assertion) != kIOReturnSuccess)
{ {
if (s_power_assertion != kIOPMNullAssertionID) s_power_assertion = kIOPMNullAssertionID;
{
IOPMAssertionRelease(s_power_assertion);
s_power_assertion = kIOPMNullAssertionID;
}
} }
else }
else
{
if (s_power_assertion != kIOPMNullAssertionID)
{ {
CFStringRef reason_for_activity = CFSTR("Emulation Running"); IOPMAssertionRelease(s_power_assertion);
if (IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, s_power_assertion = kIOPMNullAssertionID;
kIOPMAssertionLevelOn, reason_for_activity,
&s_power_assertion) != kIOReturnSuccess)
{
s_power_assertion = kIOPMNullAssertionID;
}
} }
} }
#endif #endif

View File

@ -14,9 +14,9 @@ void Init();
void Shutdown(); void Shutdown();
#if defined(HAVE_XRANDR) && HAVE_XRANDR #if defined(HAVE_XRANDR) && HAVE_XRANDR
void EnableScreenSaver(unsigned long win, bool enable); void InhibitScreenSaver(unsigned long win, bool enable);
#else #else
void EnableScreenSaver(bool enable); void InhibitScreenSaver(bool enable);
#endif #endif
// Calls std::locale::global, selecting a fallback locale if the // Calls std::locale::global, selecting a fallback locale if the