Skip to content

Commit

Permalink
Fixed resolution switching when laptop screen is not main, added supp…
Browse files Browse the repository at this point in the history
…ort for > 120hz laptop screens
  • Loading branch information
seerge committed Feb 18, 2023
1 parent 7683f24 commit 414167f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 30 deletions.
1 change: 0 additions & 1 deletion GHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>
Expand Down
76 changes: 62 additions & 14 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -170,9 +171,14 @@ public AppConfig()
if (File.Exists(configFile))
{
string text = File.ReadAllText(configFile);
config = JsonConvert.DeserializeObject<Dictionary<string, object>>(text);
if (config is null)
try
{
config = JsonSerializer.Deserialize<Dictionary<string, object>>(text);
}
catch
{
initConfig();
}
}
else
{
Expand All @@ -185,7 +191,7 @@ private void initConfig()
{
config = new Dictionary<string, object>();
config["performance_mode"] = 0;
string jsonString = JsonConvert.SerializeObject(config);
string jsonString = JsonSerializer.Serialize(config);
File.WriteAllText(configFile, jsonString);
}

Expand All @@ -199,7 +205,7 @@ public int getConfig(string name)
public void setConfig(string name, int value)
{
config[name] = value;
string jsonString = JsonConvert.SerializeObject(config);
string jsonString = JsonSerializer.Serialize(config);
File.WriteAllText(configFile, jsonString);
}

Expand Down Expand Up @@ -251,21 +257,35 @@ public struct DEVMODE
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()
{
Expand All @@ -276,30 +296,58 @@ public static DEVMODE CreateDevmode()
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 (laptopScreen is null)
return -1;

if (0 != NativeMethods.EnumDisplaySettingsExA(null,NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
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;

}

}
Expand Down
3 changes: 3 additions & 0 deletions Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 54 additions & 15 deletions Settings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using System.Timers;

namespace GHelper
Expand Down Expand Up @@ -57,7 +58,7 @@ public SettingsForm()

private void Button120Hz_Click(object? sender, EventArgs e)
{
SetScreen(120, 1);
SetScreen(1000, 1);
}

private void Button60Hz_Click(object? sender, EventArgs e)
Expand All @@ -68,33 +69,71 @@ private void Button60Hz_Click(object? sender, EventArgs e)

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 (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";
}

if (overdrive < 0)
overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive);
int overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive);

button60Hz.FlatAppearance.BorderSize = buttonInactive;
button120Hz.FlatAppearance.BorderSize = buttonInactive;

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);
Expand Down Expand Up @@ -210,7 +249,7 @@ public void AutoScreen (int Plugged = 1)
if (ScreenAuto != 1) return;

if (Plugged == 1)
SetScreen(120, 1);
SetScreen(1000, 1);
else
SetScreen(60, 0);

Expand All @@ -233,25 +272,24 @@ public void AutoGPUMode(int Plugged = 1)
{
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()
{

Expand Down Expand Up @@ -400,6 +438,7 @@ private void Settings_Load(object sender, EventArgs e)
public void Disable_Ultimate()
{
buttonUltimate.Enabled = false;
buttonUltimate.BackColor = SystemColors.ControlLight;
}

public void SetStartupCheck(bool status)
Expand Down

0 comments on commit 414167f

Please sign in to comment.