mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Improved Screen detection
This commit is contained in:
@@ -4,12 +4,15 @@ namespace GHelper.Display
|
|||||||
{
|
{
|
||||||
public class ScreenControl
|
public class ScreenControl
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public const int MAX_REFRESH = 1000;
|
||||||
|
|
||||||
public void AutoScreen(bool force = false)
|
public void AutoScreen(bool force = false)
|
||||||
{
|
{
|
||||||
if (force || AppConfig.Is("screen_auto"))
|
if (force || AppConfig.Is("screen_auto"))
|
||||||
{
|
{
|
||||||
if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online)
|
if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online)
|
||||||
SetScreen(1000, 1);
|
SetScreen(MAX_REFRESH, 1);
|
||||||
else
|
else
|
||||||
SetScreen(60, 0);
|
SetScreen(60, 0);
|
||||||
}
|
}
|
||||||
@@ -22,20 +25,19 @@ namespace GHelper.Display
|
|||||||
public void SetScreen(int frequency = -1, int overdrive = -1, int miniled = -1)
|
public void SetScreen(int frequency = -1, int overdrive = -1, int miniled = -1)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (ScreenNative.GetRefreshRate() < 0) // Laptop screen not detected or has unknown refresh rate
|
var laptopScreen = ScreenNative.FindLaptopScreen(true);
|
||||||
{
|
if (laptopScreen is null) return;
|
||||||
InitScreen();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (frequency >= 1000)
|
if (ScreenNative.GetRefreshRate(laptopScreen) < 0) return;
|
||||||
|
|
||||||
|
if (frequency >= MAX_REFRESH)
|
||||||
{
|
{
|
||||||
frequency = ScreenNative.GetRefreshRate(true);
|
frequency = ScreenNative.GetMaxRefreshRate(laptopScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frequency > 0)
|
if (frequency > 0)
|
||||||
{
|
{
|
||||||
ScreenNative.SetRefreshRate(frequency);
|
ScreenNative.SetRefreshRate(laptopScreen, frequency);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overdrive >= 0)
|
if (overdrive >= 0)
|
||||||
@@ -64,8 +66,11 @@ namespace GHelper.Display
|
|||||||
|
|
||||||
public void InitScreen()
|
public void InitScreen()
|
||||||
{
|
{
|
||||||
int frequency = ScreenNative.GetRefreshRate();
|
|
||||||
int maxFrequency = ScreenNative.GetRefreshRate(true);
|
var laptopScreen = ScreenNative.FindLaptopScreen();
|
||||||
|
|
||||||
|
int frequency = ScreenNative.GetRefreshRate(laptopScreen);
|
||||||
|
int maxFrequency = ScreenNative.GetMaxRefreshRate(laptopScreen);
|
||||||
|
|
||||||
bool screenAuto = AppConfig.Is("screen_auto");
|
bool screenAuto = AppConfig.Is("screen_auto");
|
||||||
bool overdriveSetting = !AppConfig.Is("no_overdrive");
|
bool overdriveSetting = !AppConfig.Is("no_overdrive");
|
||||||
|
|||||||
@@ -116,15 +116,17 @@ namespace GHelper.Display
|
|||||||
|
|
||||||
|
|
||||||
public const int ENUM_CURRENT_SETTINGS = -1;
|
public const int ENUM_CURRENT_SETTINGS = -1;
|
||||||
public const string defaultDevice = "\\\\.\\DISPLAY1";
|
public const string defaultDevice = @"\\.\DISPLAY1";
|
||||||
|
|
||||||
public static string? FindLaptopScreen()
|
public static string? FindLaptopScreen(bool log = false)
|
||||||
{
|
{
|
||||||
string? laptopScreen = null;
|
string? laptopScreen = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var devices = GetAllDevices().ToArray();
|
var devices = GetAllDevices().ToArray();
|
||||||
|
var screens = Screen.AllScreens;
|
||||||
|
|
||||||
int count = 0, displayNum = -1;
|
int count = 0, displayNum = -1;
|
||||||
|
|
||||||
string internalName = AppConfig.GetString("internal_display");
|
string internalName = AppConfig.GetString("internal_display");
|
||||||
@@ -138,14 +140,10 @@ namespace GHelper.Display
|
|||||||
displayNum = count;
|
displayNum = count;
|
||||||
AppConfig.Set("internal_display", device.monitorFriendlyDeviceName);
|
AppConfig.Set("internal_display", device.monitorFriendlyDeviceName);
|
||||||
}
|
}
|
||||||
|
if (log) Logger.WriteLine(device.monitorFriendlyDeviceName + ":" + device.outputTechnology.ToString() + ": " + ((count < screens.Length) ? screens[count].DeviceName : ""));
|
||||||
count++;
|
count++;
|
||||||
//Logger.WriteLine(device.monitorFriendlyDeviceName + ":" + device.outputTechnology.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var screens = Screen.AllScreens;
|
|
||||||
|
|
||||||
if (screens.Length != count) return null;
|
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
foreach (var screen in screens)
|
foreach (var screen in screens)
|
||||||
{
|
{
|
||||||
@@ -153,7 +151,7 @@ namespace GHelper.Display
|
|||||||
{
|
{
|
||||||
laptopScreen = screen.DeviceName;
|
laptopScreen = screen.DeviceName;
|
||||||
}
|
}
|
||||||
//Logger.WriteLine(screen.DeviceName);
|
//if (log) Logger.WriteLine(screen.DeviceName);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,44 +168,48 @@ namespace GHelper.Display
|
|||||||
return laptopScreen;
|
return laptopScreen;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int GetRefreshRate(bool max = false)
|
|
||||||
{
|
|
||||||
DEVMODE dm = CreateDevmode();
|
|
||||||
|
|
||||||
string? laptopScreen = FindLaptopScreen();
|
public static int GetMaxRefreshRate(string? laptopScreen)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (laptopScreen is null) return -1;
|
||||||
|
|
||||||
|
DEVMODE dm = CreateDevmode();
|
||||||
int frequency = -1;
|
int frequency = -1;
|
||||||
|
|
||||||
if (laptopScreen is null)
|
int i = 0;
|
||||||
return -1;
|
while (0 != EnumDisplaySettingsEx(laptopScreen, i, ref dm))
|
||||||
|
|
||||||
if (max)
|
|
||||||
{
|
{
|
||||||
int i = 0;
|
if (dm.dmDisplayFrequency > frequency) frequency = dm.dmDisplayFrequency;
|
||||||
while (0 != EnumDisplaySettingsEx(laptopScreen, i, ref dm))
|
i++;
|
||||||
{
|
|
||||||
if (dm.dmDisplayFrequency > frequency) frequency = dm.dmDisplayFrequency;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (0 != EnumDisplaySettingsEx(laptopScreen, ENUM_CURRENT_SETTINGS, ref dm))
|
|
||||||
{
|
|
||||||
frequency = dm.dmDisplayFrequency;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (frequency > 0) AppConfig.Set("screen_max", frequency);
|
||||||
|
else frequency = AppConfig.Get("screen_max");
|
||||||
|
|
||||||
|
return frequency;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetRefreshRate(string? laptopScreen)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (laptopScreen is null) return -1;
|
||||||
|
|
||||||
|
DEVMODE dm = CreateDevmode();
|
||||||
|
int frequency = -1;
|
||||||
|
|
||||||
|
if (0 != EnumDisplaySettingsEx(laptopScreen, ENUM_CURRENT_SETTINGS, ref dm))
|
||||||
|
{
|
||||||
|
frequency = dm.dmDisplayFrequency;
|
||||||
|
}
|
||||||
|
|
||||||
return frequency;
|
return frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int SetRefreshRate(int frequency = 120)
|
public static int SetRefreshRate(string laptopScreen, int frequency = 120)
|
||||||
{
|
{
|
||||||
DEVMODE dm = CreateDevmode();
|
DEVMODE dm = CreateDevmode();
|
||||||
string? laptopScreen = FindLaptopScreen();
|
|
||||||
|
|
||||||
if (laptopScreen is null)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (0 != EnumDisplaySettingsEx(laptopScreen, ENUM_CURRENT_SETTINGS, ref dm))
|
if (0 != EnumDisplaySettingsEx(laptopScreen, ENUM_CURRENT_SETTINGS, ref dm))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -639,7 +639,7 @@ namespace GHelper
|
|||||||
private void Button120Hz_Click(object? sender, EventArgs e)
|
private void Button120Hz_Click(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
AppConfig.Set("screen_auto", 0);
|
AppConfig.Set("screen_auto", 0);
|
||||||
screenControl.SetScreen(1000, 1);
|
screenControl.SetScreen(ScreenControl.MAX_REFRESH, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Button60Hz_Click(object? sender, EventArgs e)
|
private void Button60Hz_Click(object? sender, EventArgs e)
|
||||||
|
|||||||
Reference in New Issue
Block a user