From 9bb5e41a5dbdfc2f9e21fcaeb4ca44e25a2a91f4 Mon Sep 17 00:00:00 2001 From: seerge Date: Tue, 14 Mar 2023 15:39:56 +0100 Subject: [PATCH] Fixed auto resolution switching in some exotic cases --- NativeMethods.cs | 22 +++++++++++----------- Program.cs | 18 +++++++++++++----- Settings.cs | 14 +++++++------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/NativeMethods.cs b/NativeMethods.cs index c63ef9da5..64390e3a7 100644 --- a/NativeMethods.cs +++ b/NativeMethods.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using static Tools.ScreenInterrogatory; +using System.Windows.Forms; namespace Tools { @@ -545,22 +546,19 @@ public static string FindLaptopScreen() foreach (var device in devices) { - if (device.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL) + if (device.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL || + device.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED) { displayNum = count; } count++; - //Debug.WriteLine(device.outputTechnology); - //Debug.WriteLine(device.monitorFriendlyDeviceName); + //Logger.WriteLine(device.outputTechnology.ToString()); + //Logger.WriteLine(device.monitorFriendlyDeviceName); } var screens = Screen.AllScreens; - if (screens.Length != count) - { - Debug.WriteLine("Mismatch between enumerated and available screens"); - return null; - } + if (screens.Length != count) return null; count = 0; foreach (var screen in screens) @@ -569,13 +567,15 @@ public static string FindLaptopScreen() { laptopScreen = screen.DeviceName; } - //Debug.WriteLine(screen.DeviceName); + //Logger.WriteLine(screen.DeviceName); count++; } } - catch + catch (Exception ex) { - Debug.WriteLine("Can't find internal screen"); + Logger.WriteLine(ex.ToString()); + Logger.WriteLine("Can't detect internal screen"); + //laptopScreen = Screen.PrimaryScreen.DeviceName; } diff --git a/Program.cs b/Program.cs index 3d60860a6..9b0d34a2c 100644 --- a/Program.cs +++ b/Program.cs @@ -14,13 +14,19 @@ public static void WriteLine(string logMessage) Debug.WriteLine(logMessage); if (!Directory.Exists(appPath)) Directory.CreateDirectory(appPath); - using (StreamWriter w = File.AppendText(logFile)) + try { - w.WriteLine($"{DateTime.Now}: {logMessage}"); + using (StreamWriter w = File.AppendText(logFile)) + { + w.WriteLine($"{DateTime.Now}: {logMessage}"); + w.Close(); + } + } catch + { + } - var rand = new Random(); - if (rand.Next(50) == 1) Cleanup(); + if (new Random().Next(100) == 1) Cleanup(); } @@ -214,7 +220,9 @@ public static void SetAutoModes() settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit")); settingsForm.AutoPerformance(isPlugged); - settingsForm.AutoGPUMode(isPlugged); + + bool switched = settingsForm.AutoGPUMode(isPlugged); + if (!switched) settingsForm.AutoScreen(isPlugged); settingsForm.SetMatrix(isPlugged); } diff --git a/Settings.cs b/Settings.cs index c12e49c77..f811fce70 100644 --- a/Settings.cs +++ b/Settings.cs @@ -858,33 +858,33 @@ public void AutoScreen(PowerLineStatus Plugged = PowerLineStatus.Online) } - public void AutoGPUMode(PowerLineStatus Plugged = PowerLineStatus.Online) + public bool AutoGPUMode(PowerLineStatus Plugged = PowerLineStatus.Online) { int GpuAuto = Program.config.getConfig("gpu_auto"); - if (GpuAuto != 1) return; + if (GpuAuto != 1) return false; int eco = Program.wmi.DeviceGet(ASUSWmi.GPUEco); int mux = Program.wmi.DeviceGet(ASUSWmi.GPUMux); if (mux == 0) // GPU in Ultimate, ignore - return; + return false; else { if (eco == 1 && Plugged == PowerLineStatus.Online) // Eco going Standard on plugged { SetEcoGPU(0); + return true; } else if (eco == 0 && Plugged != PowerLineStatus.Online) // Standard going Eco on plugged { SetEcoGPU(1); - } - else - { - AutoScreen(Plugged); + return true; } } + return false; + } public int InitGPUMode()