From 0856e162a3b1d68fd26cc1cbcbafb4b0a4594eb8 Mon Sep 17 00:00:00 2001
From: Garrett Brown <grbrown93@sbcglobal.net>
Date: Wed, 6 Jul 2016 19:00:33 -0500
Subject: [PATCH] Prevent windows from idling while playing a game with a
 gamecube controller.

---
 Source/Core/DolphinQt2/MainWindow.cpp | 13 +++++++++++++
 Source/Core/DolphinWX/FrameTools.cpp  | 13 +++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/Source/Core/DolphinQt2/MainWindow.cpp b/Source/Core/DolphinQt2/MainWindow.cpp
index 10b434b499..7d9cf61820 100644
--- a/Source/Core/DolphinQt2/MainWindow.cpp
+++ b/Source/Core/DolphinQt2/MainWindow.cpp
@@ -198,8 +198,14 @@ bool MainWindow::Stop()
   }
 
   if (stop)
+  {
     ForceStop();
 
+#ifdef Q_OS_WIN
+    // Allow windows to idle or turn off display again
+    SetThreadExecutionState(ES_CONTINUOUS);
+#endif
+  }
   return stop;
 }
 
@@ -258,6 +264,13 @@ void MainWindow::StartGame(const QString& path)
   Settings().SetLastGame(path);
   ShowRenderWidget();
   emit EmulationStarted();
+
+#ifdef Q_OS_WIN
+  // Prevents Windows from sleeping, turning off the display, or idling
+  EXECUTION_STATE shouldScreenSave =
+      SConfig::GetInstance().bDisableScreenSaver ? ES_DISPLAY_REQUIRED : 0;
+  SetThreadExecutionState(ES_CONTINUOUS | shouldScreenSave | ES_SYSTEM_REQUIRED);
+#endif
 }
 
 void MainWindow::ShowRenderWidget()
diff --git a/Source/Core/DolphinWX/FrameTools.cpp b/Source/Core/DolphinWX/FrameTools.cpp
index 44de0e8e99..6b07736d7a 100644
--- a/Source/Core/DolphinWX/FrameTools.cpp
+++ b/Source/Core/DolphinWX/FrameTools.cpp
@@ -1034,6 +1034,13 @@ void CFrame::StartGame(const std::string& filename)
                                    X11Utils::XWindowFromHandle(GetHandle()), true);
 #endif
 
+#ifdef _WIN32
+    // Prevents Windows from sleeping, turning off the display, or idling
+    EXECUTION_STATE shouldScreenSave =
+        SConfig::GetInstance().bDisableScreenSaver ? ES_DISPLAY_REQUIRED : 0;
+    SetThreadExecutionState(ES_CONTINUOUS | shouldScreenSave | ES_SYSTEM_REQUIRED);
+#endif
+
     m_RenderParent->SetFocus();
 
     wxTheApp->Bind(wxEVT_KEY_DOWN, &CFrame::OnKeyDown, this);
@@ -1181,6 +1188,12 @@ void CFrame::OnStopped()
     X11Utils::InhibitScreensaver(X11Utils::XDisplayFromHandle(GetHandle()),
                                  X11Utils::XWindowFromHandle(GetHandle()), false);
 #endif
+
+#ifdef _WIN32
+  // Allow windows to resume normal idling behavior
+  SetThreadExecutionState(ES_CONTINUOUS);
+#endif
+
   m_RenderFrame->SetTitle(StrToWxStr(scm_rev_str));
 
   // Destroy the renderer frame when not rendering to main