diff --git a/GHelper.csproj b/GHelper.csproj index f5399a20..b8df8c75 100644 --- a/GHelper.csproj +++ b/GHelper.csproj @@ -31,7 +31,6 @@ - diff --git a/Program.cs b/Program.cs index 32396f22..9f798fcd 100644 --- a/Program.cs +++ b/Program.cs @@ -1,11 +1,12 @@ using Microsoft.Win32.TaskScheduler; -using Newtonsoft.Json; using System.Collections.Generic; using System.Diagnostics; using System.Management; using System.Reflection.Metadata.Ecma335; using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.Json.Serialization; public class ASUSWmi { private ManagementObject mo; @@ -170,9 +171,14 @@ public class AppConfig if (File.Exists(configFile)) { string text = File.ReadAllText(configFile); - config = JsonConvert.DeserializeObject>(text); - if (config is null) + try + { + config = JsonSerializer.Deserialize>(text); + } + catch + { initConfig(); + } } else { @@ -185,7 +191,7 @@ public class AppConfig { config = new Dictionary(); config["performance_mode"] = 0; - string jsonString = JsonConvert.SerializeObject(config); + string jsonString = JsonSerializer.Serialize(config); File.WriteAllText(configFile, jsonString); } @@ -199,7 +205,7 @@ public class AppConfig public void setConfig(string name, int value) { config[name] = value; - string jsonString = JsonConvert.SerializeObject(config); + string jsonString = JsonSerializer.Serialize(config); File.WriteAllText(configFile, jsonString); } @@ -251,21 +257,35 @@ public class NativeMethods public int dmPanningHeight; }; + [Flags()] + public enum DisplaySettingsFlags : int + { + CDS_UPDATEREGISTRY = 1, + CDS_TEST = 2, + CDS_FULLSCREEN = 4, + CDS_GLOBAL = 8, + CDS_SET_PRIMARY = 0x10, + CDS_RESET = 0x40000000, + CDS_NORESET = 0x10000000 + } + // PInvoke declaration for EnumDisplaySettings Win32 API [DllImport("user32.dll")] - public static extern int EnumDisplaySettingsExA( + public static extern int EnumDisplaySettingsEx( string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); // PInvoke declaration for ChangeDisplaySettings Win32 API [DllImport("user32.dll")] - public static extern int ChangeDisplaySettings( - ref DEVMODE lpDevMode, - int dwFlags); + public static extern int ChangeDisplaySettingsEx( + string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, + DisplaySettingsFlags dwflags, IntPtr lParam); + public const int ENUM_CURRENT_SETTINGS = -1; + public const string laptopScreenName = "\\\\.\\DISPLAY1"; public static DEVMODE CreateDevmode() { @@ -276,30 +296,58 @@ public class NativeMethods return dm; } + public static Screen FindLaptopScreen() + { + var screens = Screen.AllScreens; + Screen laptopScreen = null; + + foreach (var screen in screens) + { + if (screen.DeviceName == laptopScreenName) + { + laptopScreen = screen; + } + } + + if (laptopScreen is null) return null; + else return laptopScreen; + } + public static int GetRefreshRate() { DEVMODE dm = CreateDevmode(); + Screen laptopScreen = FindLaptopScreen(); int frequency = -1; - if (0 != NativeMethods.EnumDisplaySettingsExA(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) + if (laptopScreen is null) + return -1; + + if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) { - Debug.WriteLine(JsonConvert.SerializeObject(dm)); frequency = dm.dmDisplayFrequency; } return frequency; } - public static void SetRefreshRate(int frequency = 120) + public static int SetRefreshRate(int frequency = 120) { DEVMODE dm = CreateDevmode(); + Screen laptopScreen = FindLaptopScreen(); - if (0 != NativeMethods.EnumDisplaySettingsExA(null,NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) + if (laptopScreen is null) + return -1; + + if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) { dm.dmDisplayFrequency = frequency; - int iRet = NativeMethods.ChangeDisplaySettings(ref dm, 0); + int iRet = NativeMethods.ChangeDisplaySettingsEx(laptopScreen.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero); + return iRet; } + + return 0; + } } diff --git a/Settings.Designer.cs b/Settings.Designer.cs index d6351e3a..96835b8e 100644 --- a/Settings.Designer.cs +++ b/Settings.Designer.cs @@ -367,6 +367,7 @@ // this.button120Hz.BackColor = System.Drawing.SystemColors.ControlLightLight; this.button120Hz.Dock = System.Windows.Forms.DockStyle.Fill; + this.button120Hz.FlatAppearance.BorderColor = System.Drawing.SystemColors.ActiveBorder; this.button120Hz.FlatAppearance.BorderSize = 0; this.button120Hz.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button120Hz.Location = new System.Drawing.Point(227, 10); @@ -382,8 +383,10 @@ this.button60Hz.BackColor = System.Drawing.SystemColors.ControlLightLight; this.button60Hz.CausesValidation = false; this.button60Hz.Dock = System.Windows.Forms.DockStyle.Fill; + this.button60Hz.FlatAppearance.BorderColor = System.Drawing.SystemColors.ActiveBorder; this.button60Hz.FlatAppearance.BorderSize = 0; this.button60Hz.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.button60Hz.ForeColor = System.Drawing.SystemColors.ControlText; this.button60Hz.Location = new System.Drawing.Point(10, 10); this.button60Hz.Margin = new System.Windows.Forms.Padding(10); this.button60Hz.Name = "button60Hz"; diff --git a/Settings.cs b/Settings.cs index 349964d9..5966a107 100644 --- a/Settings.cs +++ b/Settings.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Text.Json; using System.Timers; namespace GHelper @@ -57,7 +58,7 @@ namespace GHelper private void Button120Hz_Click(object? sender, EventArgs e) { - SetScreen(120, 1); + SetScreen(1000, 1); } private void Button60Hz_Click(object? sender, EventArgs e) @@ -68,22 +69,51 @@ namespace GHelper public void SetScreen(int frequency = -1, int overdrive = -1) { + + int currentFrequency = NativeMethods.GetRefreshRate(); + if (currentFrequency < 0) // Laptop screen not detected or has unknown refresh rate + return; + + if (frequency >= 1000) + { + frequency = Program.config.getConfig("max_frequency"); + if (frequency <= 60) + frequency = 120; + } + if (frequency > 0) NativeMethods.SetRefreshRate(frequency); + if (overdrive > 0) Program.wmi.DeviceSet(ASUSWmi.ScreenOverdrive, overdrive); - InitScreen(frequency, overdrive); + + InitScreen(); } - public void InitScreen(int frequency = -1, int overdrive = -1) + public void InitScreen() { - if (frequency < 0) - frequency = NativeMethods.GetRefreshRate(); + int frequency = NativeMethods.GetRefreshRate(); + int maxFrequency = Program.config.getConfig("max_frequency"); - if (overdrive < 0) - overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive); + if (frequency < 0) { + button60Hz.Enabled= false; + button120Hz.Enabled = false; + labelSreen.Text = "Latop Screen: Turned off"; + button60Hz.BackColor = SystemColors.ControlLight; + button120Hz.BackColor = SystemColors.ControlLight; + } + else + { + button60Hz.Enabled = true; + button120Hz.Enabled = true; + button60Hz.BackColor = SystemColors.ControlLightLight; + button120Hz.BackColor = SystemColors.ControlLightLight; + labelSreen.Text = "Latop Screen"; + } + + int overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive); button60Hz.FlatAppearance.BorderSize = buttonInactive; button120Hz.FlatAppearance.BorderSize = buttonInactive; @@ -91,10 +121,19 @@ namespace GHelper if (frequency == 60) { button60Hz.FlatAppearance.BorderSize = buttonActive; - } else if (frequency == 120) + } else { + if (maxFrequency > 60) + maxFrequency = frequency; + + Program.config.setConfig("max_frequency", maxFrequency); button120Hz.FlatAppearance.BorderSize = buttonActive; - } + } + + if (maxFrequency > 60) + { + button120Hz.Text = maxFrequency.ToString() + "Hz + OD"; + } Program.config.setConfig("frequency", frequency); Program.config.setConfig("overdrive", overdrive); @@ -210,7 +249,7 @@ namespace GHelper if (ScreenAuto != 1) return; if (Plugged == 1) - SetScreen(120, 1); + SetScreen(1000, 1); else SetScreen(60, 0); @@ -233,25 +272,24 @@ namespace GHelper { if (eco == 1 && Plugged == 1) // Eco going Standard on plugged { + Program.wmi.DeviceSet(ASUSWmi.GPUEco, 0); + GPUMode = ASUSWmi.GPUModeStandard; VisualiseGPUMode(GPUMode); - Program.wmi.DeviceSet(ASUSWmi.GPUEco, 0); Program.config.setConfig("gpu_mode", GPUMode); } else if (eco == 0 && Plugged == 0) // Standard going Eco on plugged { + Program.wmi.DeviceSet(ASUSWmi.GPUEco, 1); + GPUMode = ASUSWmi.GPUModeEco; VisualiseGPUMode(GPUMode); - Program.wmi.DeviceSet(ASUSWmi.GPUEco, 1); Program.config.setConfig("gpu_mode", GPUMode); - } } } - - public int InitGPUMode() { @@ -400,6 +438,7 @@ namespace GHelper public void Disable_Ultimate() { buttonUltimate.Enabled = false; + buttonUltimate.BackColor = SystemColors.ControlLight; } public void SetStartupCheck(bool status)