From 4c697d37558538a8577262e0b74bd18a1bb9f239 Mon Sep 17 00:00:00 2001 From: lisa-wolfgang <43426138+lisa-wolfgang@users.noreply.github.com> Date: Fri, 24 Feb 2023 15:10:07 -0600 Subject: [PATCH] Add cross-platform "disable screen saver" setting (#497) --- .../HW/Latte/Core/LattePerformanceMonitor.cpp | 5 --- src/config/CemuConfig.cpp | 4 ++- src/config/CemuConfig.h | 1 + src/gui/GeneralSettings2.cpp | 16 ++++++++- src/gui/GeneralSettings2.h | 1 + src/gui/MainWindow.cpp | 9 +++++ src/util/ScreenSaver/ScreenSaver.h | 36 +++++++++++++++++++ 7 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/util/ScreenSaver/ScreenSaver.h diff --git a/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp b/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp index 714758e9..5d6a020b 100644 --- a/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp +++ b/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp @@ -112,11 +112,6 @@ void LattePerformanceMonitor_frameEnd() LatteOverlay_updateStats(fps, drawCallCounter / elapsedFrames); gui_updateWindowTitles(false, false, fps); } - - // prevent hibernation and screen saver/monitor off - #if BOOST_OS_WINDOWS - SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED); - #endif } LatteOverlay_updateStatsPerFrame(); } diff --git a/src/config/CemuConfig.cpp b/src/config/CemuConfig.cpp index cb287bf1..ac24f4e9 100644 --- a/src/config/CemuConfig.cpp +++ b/src/config/CemuConfig.cpp @@ -60,6 +60,7 @@ void CemuConfig::Load(XMLConfigParser& parser) did_show_macos_disclaimer = parser.get("macos_disclaimer", did_show_macos_disclaimer); fullscreen = parser.get("fullscreen", fullscreen); proxy_server = parser.get("proxy_server", ""); + disable_screensaver = parser.get("disable_screensaver", true); // cpu_mode = parser.get("cpu_mode", cpu_mode.GetInitValue()); //console_region = parser.get("console_region", console_region.GetInitValue()); @@ -363,7 +364,8 @@ void CemuConfig::Save(XMLConfigParser& parser) config.set("macos_disclaimer", did_show_macos_disclaimer); config.set("fullscreen", fullscreen); config.set("proxy_server", proxy_server.GetValue().c_str()); - + config.set("disable_screensaver", disable_screensaver); + // config.set("cpu_mode", cpu_mode.GetValue()); //config.set("console_region", console_region.GetValue()); config.set("console_language", console_language.GetValue()); diff --git a/src/config/CemuConfig.h b/src/config/CemuConfig.h index 6d198548..76f1c64c 100644 --- a/src/config/CemuConfig.h +++ b/src/config/CemuConfig.h @@ -369,6 +369,7 @@ struct CemuConfig ConfigValue fullscreen_menubar{ false }; ConfigValue fullscreen{ false }; ConfigValue proxy_server{}; + ConfigValue disable_screensaver{true}; std::vector game_paths; std::mutex game_cache_entries_mutex; diff --git a/src/gui/GeneralSettings2.cpp b/src/gui/GeneralSettings2.cpp index 9a110332..cfce5f89 100644 --- a/src/gui/GeneralSettings2.cpp +++ b/src/gui/GeneralSettings2.cpp @@ -49,6 +49,8 @@ #include "Cafe/TitleList/TitleList.h" #include "wxHelper.h" +#include "util/ScreenSaver/ScreenSaver.h" + const wxString kDirectSound(wxT("DirectSound")); const wxString kXAudio27(wxT("XAudio2.7")); const wxString kXAudio2(wxT("XAudio2")); @@ -172,6 +174,10 @@ wxPanel* GeneralSettings2::AddGeneralPage(wxNotebook* notebook) m_permanent_storage = new wxCheckBox(box, wxID_ANY, _("Use permanent storage")); m_permanent_storage->SetToolTip(_("Cemu will remember your custom mlc path in %LOCALAPPDATA%/Cemu for new installations.")); second_row->Add(m_permanent_storage, 0, botflag, 5); + second_row->AddSpacer(10); + m_disable_screensaver = new wxCheckBox(box, wxID_ANY, _("Disable screen saver")); + m_disable_screensaver->SetToolTip(_("Prevents the system from activating the screen saver or going to sleep while running a game.")); + second_row->Add(m_disable_screensaver, 0, botflag, 5); box_sizer->Add(second_row, 0, wxEXPAND, 5); } @@ -882,6 +888,13 @@ void GeneralSettings2::StoreConfig() config.permanent_storage = use_ps; } + config.disable_screensaver = m_disable_screensaver->IsChecked(); + // Toggle while a game is running + if (CafeSystem::IsTitleRunning()) + { + ScreenSaver::SetInhibit(config.disable_screensaver); + } + if (!LaunchSettings::GetMLCPath().has_value()) config.SetMLCPath(wxHelper::MakeFSPath(m_mlc_path->GetValue()), false); @@ -1494,7 +1507,8 @@ void GeneralSettings2::ApplyConfig() m_save_screenshot->SetValue(config.save_screenshot); m_permanent_storage->SetValue(config.permanent_storage); - + m_disable_screensaver->SetValue(config.disable_screensaver); + for (auto& path : config.game_paths) { m_game_paths->Append(path); diff --git a/src/gui/GeneralSettings2.h b/src/gui/GeneralSettings2.h index e519078c..1bc086cc 100644 --- a/src/gui/GeneralSettings2.h +++ b/src/gui/GeneralSettings2.h @@ -42,6 +42,7 @@ private: wxCheckBox* m_discord_presence, *m_fullscreen_menubar; wxCheckBox* m_auto_update, *m_save_screenshot; wxCheckBox* m_permanent_storage; + wxCheckBox* m_disable_screensaver; wxListBox* m_game_paths; wxTextCtrl* m_mlc_path; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 8af36b18..c99771f5 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -20,6 +20,7 @@ #include "util/helpers/helpers.h" #include "config/CemuConfig.h" #include "Cemu/DiscordPresence/DiscordPresence.h" +#include "util/ScreenSaver/ScreenSaver.h" #include "gui/GeneralSettings2.h" #include "gui/GraphicPacksWindow2.h" #include "gui/GameProfileWindow.h" @@ -565,6 +566,14 @@ bool MainWindow::FileLoad(std::wstring fileName, wxLaunchGameEvent::INITIATED_BY m_discord->UpdatePresence(DiscordPresence::Playing, m_launched_game_name); #endif + if (GetConfig().disable_screensaver) + { + ScreenSaver::SetInhibit(true); + // TODO: disable when only the game, not Cemu, is closed (a feature not yet implemented) + // currently unnecessary because this will happen automatically when Cemu closes + // ScreenSaver::SetInhibit(false); + } + if (ActiveSettings::FullscreenEnabled()) SetFullScreen(true); diff --git a/src/util/ScreenSaver/ScreenSaver.h b/src/util/ScreenSaver/ScreenSaver.h new file mode 100644 index 00000000..33daf24a --- /dev/null +++ b/src/util/ScreenSaver/ScreenSaver.h @@ -0,0 +1,36 @@ +#include "Cemu/Logging/CemuLogging.h" +#include + +class ScreenSaver +{ +public: + static void SetInhibit(bool inhibit) + { + // Initialize video subsystem if necessary + if (SDL_WasInit(SDL_INIT_VIDEO) == 0) + { + int initErr = SDL_InitSubSystem(SDL_INIT_VIDEO); + if (initErr) + { + cemuLog_force("Could not disable screen saver (SDL video subsystem initialization error)"); + } + } + // Toggle SDL's screen saver inhibition + if (inhibit) + { + SDL_DisableScreenSaver(); + if (SDL_IsScreenSaverEnabled() == SDL_TRUE) + { + cemuLog_force("Could not verify if screen saver was disabled (`SDL_IsScreenSaverEnabled()` returned SDL_TRUE)"); + } + } + else + { + SDL_EnableScreenSaver(); + if (SDL_IsScreenSaverEnabled() == SDL_FALSE) + { + cemuLog_force("Could not verify if screen saver was re-enabled (`SDL_IsScreenSaverEnabled()` returned SDL_FALSE)"); + } + } + }; +};