From 55f69c77dd3d9853a4faacc9e8a8a8b1dac69599 Mon Sep 17 00:00:00 2001 From: Lesueur Benjamin Date: Thu, 11 Feb 2021 08:56:27 +0100 Subject: [PATCH] improved Power Profile application - improved game detection check. - implemented a handle check and restore to make sure you're not pulled out of your game on power profile application. --- DockerForm/Form1.cs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/DockerForm/Form1.cs b/DockerForm/Form1.cs index 3bccae1..b097b16 100644 --- a/DockerForm/Form1.cs +++ b/DockerForm/Form1.cs @@ -15,6 +15,7 @@ using System.Xml.Serialization; using Microsoft.Win32.TaskScheduler; using Task = Microsoft.Win32.TaskScheduler.Task; +using System.Runtime.InteropServices; namespace DockerForm { @@ -31,7 +32,6 @@ public partial class Form1 : Form public static bool PowerStatus; public static bool IsPowerNew = false; public static bool IsRunning = true; - public static bool GameProfileApplied = false; // Configurable vars public static bool MinimizeOnStartup = false; @@ -62,11 +62,17 @@ public partial class Form1 : Form // PowerProfile vars public static Dictionary ProfileDB = new Dictionary(); + public static PowerProfile CurrentProfile; // TaskManager vars private static TaskService ts; private const string taskName = "ThunderboltSwitch"; + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + private const int WM_DEVICECHANGE = 0x0219; protected override void WndProc(ref Message m) { @@ -113,12 +119,13 @@ private static void CheckPowerProfiles() private static void SetPowerProfile(PowerProfile profile, DockerGame game = null) { - // is this a supported platform (Intel) + // skip if unsupported platform if (MCHBAR == null) return; - // is this a game-associated Power Profile - GameProfileApplied = game != null; + // skip if call isn't needed + if (profile == CurrentProfile) + return; string command = "/Min /Nologo /Stdout /command=\""; @@ -153,6 +160,9 @@ private static void SetPowerProfile(PowerProfile profile, DockerGame game = null command += "rwexit\""; + // get current handle + IntPtr curHnd = GetForegroundWindow(); + ProcessStartInfo RWInfo = new ProcessStartInfo { CreateNoWindow = true, @@ -164,6 +174,12 @@ private static void SetPowerProfile(PowerProfile profile, DockerGame game = null }; Process.Start(RWInfo); + // restore handle + SetForegroundWindow(curHnd); + + // update current profile + CurrentProfile = profile; + SendNotification("Power Profile [" + profile.GetName() + "] applied.", true); LogManager.UpdateLog("Power Profile [" + profile.GetName() + "] applied." + profile.ToString()); } @@ -264,8 +280,7 @@ static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e) DatabaseManager.UpdateFilesAndRegistries(game, GetCurrentState(), GetCurrentState(), true, false, true, GetCurrentState()); - if(GameProfileApplied) - CheckPowerProfiles(); + CheckPowerProfiles(); } } catch (Exception ex) { }