Fixed resolution switching when laptop screen is not main, added support for > 120hz laptop screens

This commit is contained in:
seerge
2023-02-18 14:38:20 +01:00
parent 7683f2472b
commit 414167f008
4 changed files with 119 additions and 30 deletions

View File

@@ -31,7 +31,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.Management" Version="7.0.0" /> <PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" /> <PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup> </ItemGroup>

View File

@@ -1,11 +1,12 @@
using Microsoft.Win32.TaskScheduler; using Microsoft.Win32.TaskScheduler;
using Newtonsoft.Json;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Management; using System.Management;
using System.Reflection.Metadata.Ecma335; using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
public class ASUSWmi public class ASUSWmi
{ {
private ManagementObject mo; private ManagementObject mo;
@@ -170,9 +171,14 @@ public class AppConfig
if (File.Exists(configFile)) if (File.Exists(configFile))
{ {
string text = File.ReadAllText(configFile); string text = File.ReadAllText(configFile);
config = JsonConvert.DeserializeObject<Dictionary<string, object>>(text); try
if (config is null) {
config = JsonSerializer.Deserialize<Dictionary<string, object>>(text);
}
catch
{
initConfig(); initConfig();
}
} }
else else
{ {
@@ -185,7 +191,7 @@ public class AppConfig
{ {
config = new Dictionary<string, object>(); config = new Dictionary<string, object>();
config["performance_mode"] = 0; config["performance_mode"] = 0;
string jsonString = JsonConvert.SerializeObject(config); string jsonString = JsonSerializer.Serialize(config);
File.WriteAllText(configFile, jsonString); File.WriteAllText(configFile, jsonString);
} }
@@ -199,7 +205,7 @@ public class AppConfig
public void setConfig(string name, int value) public void setConfig(string name, int value)
{ {
config[name] = value; config[name] = value;
string jsonString = JsonConvert.SerializeObject(config); string jsonString = JsonSerializer.Serialize(config);
File.WriteAllText(configFile, jsonString); File.WriteAllText(configFile, jsonString);
} }
@@ -251,21 +257,35 @@ public class NativeMethods
public int dmPanningHeight; 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 // PInvoke declaration for EnumDisplaySettings Win32 API
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern int EnumDisplaySettingsExA( public static extern int EnumDisplaySettingsEx(
string lpszDeviceName, string lpszDeviceName,
int iModeNum, int iModeNum,
ref DEVMODE lpDevMode); ref DEVMODE lpDevMode);
// PInvoke declaration for ChangeDisplaySettings Win32 API // PInvoke declaration for ChangeDisplaySettings Win32 API
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern int ChangeDisplaySettings( public static extern int ChangeDisplaySettingsEx(
ref DEVMODE lpDevMode, string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd,
int dwFlags); DisplaySettingsFlags dwflags, IntPtr lParam);
public const int ENUM_CURRENT_SETTINGS = -1; public const int ENUM_CURRENT_SETTINGS = -1;
public const string laptopScreenName = "\\\\.\\DISPLAY1";
public static DEVMODE CreateDevmode() public static DEVMODE CreateDevmode()
{ {
@@ -276,30 +296,58 @@ public class NativeMethods
return dm; 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() public static int GetRefreshRate()
{ {
DEVMODE dm = CreateDevmode(); DEVMODE dm = CreateDevmode();
Screen laptopScreen = FindLaptopScreen();
int frequency = -1; 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; frequency = dm.dmDisplayFrequency;
} }
return frequency; return frequency;
} }
public static void SetRefreshRate(int frequency = 120) public static int SetRefreshRate(int frequency = 120)
{ {
DEVMODE dm = CreateDevmode(); 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; 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;
} }
} }

3
Settings.Designer.cs generated
View File

@@ -367,6 +367,7 @@
// //
this.button120Hz.BackColor = System.Drawing.SystemColors.ControlLightLight; this.button120Hz.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.button120Hz.Dock = System.Windows.Forms.DockStyle.Fill; this.button120Hz.Dock = System.Windows.Forms.DockStyle.Fill;
this.button120Hz.FlatAppearance.BorderColor = System.Drawing.SystemColors.ActiveBorder;
this.button120Hz.FlatAppearance.BorderSize = 0; this.button120Hz.FlatAppearance.BorderSize = 0;
this.button120Hz.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button120Hz.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button120Hz.Location = new System.Drawing.Point(227, 10); this.button120Hz.Location = new System.Drawing.Point(227, 10);
@@ -382,8 +383,10 @@
this.button60Hz.BackColor = System.Drawing.SystemColors.ControlLightLight; this.button60Hz.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.button60Hz.CausesValidation = false; this.button60Hz.CausesValidation = false;
this.button60Hz.Dock = System.Windows.Forms.DockStyle.Fill; this.button60Hz.Dock = System.Windows.Forms.DockStyle.Fill;
this.button60Hz.FlatAppearance.BorderColor = System.Drawing.SystemColors.ActiveBorder;
this.button60Hz.FlatAppearance.BorderSize = 0; this.button60Hz.FlatAppearance.BorderSize = 0;
this.button60Hz.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 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.Location = new System.Drawing.Point(10, 10);
this.button60Hz.Margin = new System.Windows.Forms.Padding(10); this.button60Hz.Margin = new System.Windows.Forms.Padding(10);
this.button60Hz.Name = "button60Hz"; this.button60Hz.Name = "button60Hz";

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text.Json;
using System.Timers; using System.Timers;
namespace GHelper namespace GHelper
@@ -57,7 +58,7 @@ namespace GHelper
private void Button120Hz_Click(object? sender, EventArgs e) private void Button120Hz_Click(object? sender, EventArgs e)
{ {
SetScreen(120, 1); SetScreen(1000, 1);
} }
private void Button60Hz_Click(object? sender, EventArgs e) private void Button60Hz_Click(object? sender, EventArgs e)
@@ -68,22 +69,51 @@ namespace GHelper
public void SetScreen(int frequency = -1, int overdrive = -1) 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) if (frequency > 0)
NativeMethods.SetRefreshRate(frequency); NativeMethods.SetRefreshRate(frequency);
if (overdrive > 0) if (overdrive > 0)
Program.wmi.DeviceSet(ASUSWmi.ScreenOverdrive, overdrive); 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) int frequency = NativeMethods.GetRefreshRate();
frequency = NativeMethods.GetRefreshRate(); int maxFrequency = Program.config.getConfig("max_frequency");
if (overdrive < 0) if (frequency < 0) {
overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive); 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; button60Hz.FlatAppearance.BorderSize = buttonInactive;
button120Hz.FlatAppearance.BorderSize = buttonInactive; button120Hz.FlatAppearance.BorderSize = buttonInactive;
@@ -91,11 +121,20 @@ namespace GHelper
if (frequency == 60) if (frequency == 60)
{ {
button60Hz.FlatAppearance.BorderSize = buttonActive; button60Hz.FlatAppearance.BorderSize = buttonActive;
} else if (frequency == 120) } else
{ {
if (maxFrequency > 60)
maxFrequency = frequency;
Program.config.setConfig("max_frequency", maxFrequency);
button120Hz.FlatAppearance.BorderSize = buttonActive; button120Hz.FlatAppearance.BorderSize = buttonActive;
} }
if (maxFrequency > 60)
{
button120Hz.Text = maxFrequency.ToString() + "Hz + OD";
}
Program.config.setConfig("frequency", frequency); Program.config.setConfig("frequency", frequency);
Program.config.setConfig("overdrive", overdrive); Program.config.setConfig("overdrive", overdrive);
} }
@@ -210,7 +249,7 @@ namespace GHelper
if (ScreenAuto != 1) return; if (ScreenAuto != 1) return;
if (Plugged == 1) if (Plugged == 1)
SetScreen(120, 1); SetScreen(1000, 1);
else else
SetScreen(60, 0); SetScreen(60, 0);
@@ -233,25 +272,24 @@ namespace GHelper
{ {
if (eco == 1 && Plugged == 1) // Eco going Standard on plugged if (eco == 1 && Plugged == 1) // Eco going Standard on plugged
{ {
Program.wmi.DeviceSet(ASUSWmi.GPUEco, 0);
GPUMode = ASUSWmi.GPUModeStandard; GPUMode = ASUSWmi.GPUModeStandard;
VisualiseGPUMode(GPUMode); VisualiseGPUMode(GPUMode);
Program.wmi.DeviceSet(ASUSWmi.GPUEco, 0);
Program.config.setConfig("gpu_mode", GPUMode); Program.config.setConfig("gpu_mode", GPUMode);
} }
else if (eco == 0 && Plugged == 0) // Standard going Eco on plugged else if (eco == 0 && Plugged == 0) // Standard going Eco on plugged
{ {
Program.wmi.DeviceSet(ASUSWmi.GPUEco, 1);
GPUMode = ASUSWmi.GPUModeEco; GPUMode = ASUSWmi.GPUModeEco;
VisualiseGPUMode(GPUMode); VisualiseGPUMode(GPUMode);
Program.wmi.DeviceSet(ASUSWmi.GPUEco, 1);
Program.config.setConfig("gpu_mode", GPUMode); Program.config.setConfig("gpu_mode", GPUMode);
} }
} }
} }
public int InitGPUMode() public int InitGPUMode()
{ {
@@ -400,6 +438,7 @@ namespace GHelper
public void Disable_Ultimate() public void Disable_Ultimate()
{ {
buttonUltimate.Enabled = false; buttonUltimate.Enabled = false;
buttonUltimate.BackColor = SystemColors.ControlLight;
} }
public void SetStartupCheck(bool status) public void SetStartupCheck(bool status)