Attempt to fix auto gpu switch

This commit is contained in:
seerge
2023-03-19 21:59:22 +01:00
parent d7f1d1d5fd
commit 4d347df45c
6 changed files with 64 additions and 36 deletions

View File

@@ -20,6 +20,7 @@ public class ASUSWmi
public const uint BatteryLimit = 0x00120057;
public const uint ScreenOverdrive = 0x00050019;
public const uint ScreenMiniled = 0x0005001E;
public const uint DevsCPUFanCurve = 0x00110024;
public const uint DevsGPUFanCurve = 0x00110025;

View File

@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Management;
using System.Management;
using System.Text.Json;
public class AppConfig

View File

@@ -16,7 +16,7 @@
<PlatformTarget>x64</PlatformTarget>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<AssemblyVersion>0.33</AssemblyVersion>
<AssemblyVersion>0.34</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
@@ -51,15 +51,6 @@
<PackageReference Include="WinForms.DataVisualization" Version="1.7.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Management.Infrastructure">
<HintPath>..\..\.nuget\packages\microsoft.management.infrastructure\2.0.0\ref\net451\Microsoft.Management.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Management.Infrastructure.Native">
<HintPath>..\..\.nuget\packages\microsoft.management.infrastructure.runtime.win\2.0.0\runtimes\win10-x64\lib\netstandard1.6\Microsoft.Management.Infrastructure.Native.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Resources\eco.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>

View File

@@ -112,7 +112,7 @@ namespace GHelper
}
public static void SetAutoModes()
public static void SetAutoModes(bool wait = false)
{
if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastAuto) < 1000) return;
@@ -126,6 +126,9 @@ namespace GHelper
settingsForm.AutoPerformance(isPlugged);
// waiting a bit before turning off dGPU
// if (wait && isPlugged != PowerLineStatus.Online) Thread.Sleep(3000);
bool switched = settingsForm.AutoGPUMode(isPlugged);
if (!switched) settingsForm.AutoScreen(isPlugged);
@@ -135,7 +138,7 @@ namespace GHelper
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Logger.WriteLine("Windows - Power Mode Changed");
SetAutoModes();
SetAutoModes(true);
}

View File

@@ -85,6 +85,7 @@ namespace GHelper
buttonKeyboardColor = new Button();
pictureKeyboard = new PictureBox();
labelKeyboard = new Label();
buttonMiniled = new RoundedButton();
panelMatrix.SuspendLayout();
tableLayoutMatrix.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureMatrix).BeginInit();
@@ -686,6 +687,7 @@ namespace GHelper
tableScreen.Controls.Add(buttonScreenAuto, 0, 0);
tableScreen.Controls.Add(button60Hz, 1, 0);
tableScreen.Controls.Add(button120Hz, 2, 0);
tableScreen.Location = new Point(16, 51);
tableScreen.Margin = new Padding(8, 4, 8, 4);
tableScreen.Name = "tableScreen";
@@ -721,7 +723,7 @@ namespace GHelper
button60Hz.FlatAppearance.BorderSize = 0;
button60Hz.FlatStyle = FlatStyle.Flat;
button60Hz.ForeColor = SystemColors.ControlText;
button60Hz.Location = new Point(197, 4);
button60Hz.Location = new Point(390, 4);
button60Hz.Margin = new Padding(4);
button60Hz.Name = "button60Hz";
button60Hz.Size = new Size(185, 72);
@@ -738,7 +740,7 @@ namespace GHelper
button120Hz.FlatAppearance.BorderSize = 0;
button120Hz.FlatStyle = FlatStyle.Flat;
button120Hz.ForeColor = SystemColors.ControlText;
button120Hz.Location = new Point(390, 4);
button120Hz.Location = new Point(583, 4);
button120Hz.Margin = new Padding(4);
button120Hz.Name = "button120Hz";
button120Hz.Size = new Size(185, 72);
@@ -901,6 +903,24 @@ namespace GHelper
labelKeyboard.TabIndex = 32;
labelKeyboard.Text = "Laptop Keyboard";
//
// buttonMiniled
//
buttonMiniled.Activated = false;
buttonMiniled.BackColor = SystemColors.ControlLightLight;
buttonMiniled.BorderColor = Color.Transparent;
buttonMiniled.CausesValidation = false;
buttonMiniled.Dock = DockStyle.Fill;
buttonMiniled.FlatAppearance.BorderSize = 0;
buttonMiniled.FlatStyle = FlatStyle.Flat;
buttonMiniled.ForeColor = SystemColors.ControlText;
buttonMiniled.Location = new Point(197, 4);
buttonMiniled.Margin = new Padding(4);
buttonMiniled.Name = "buttonMiniled";
buttonMiniled.Size = new Size(185, 72);
buttonMiniled.TabIndex = 3;
buttonMiniled.Text = "Miniled";
buttonMiniled.UseVisualStyleBackColor = false;
//
// SettingsForm
//
AutoScaleDimensions = new SizeF(192F, 192F);
@@ -1015,5 +1035,6 @@ namespace GHelper
private RoundedButton buttonOptimized;
private Label labelTipGPU;
private Label labelTipScreen;
private RoundedButton buttonMiniled;
}
}

View File

@@ -45,6 +45,7 @@ namespace GHelper
button60Hz.BorderColor = SystemColors.ActiveBorder;
button120Hz.BorderColor = SystemColors.ActiveBorder;
buttonScreenAuto.BorderColor = SystemColors.ActiveBorder;
buttonMiniled.BorderColor = colorTurbo;
buttonOptimized.Click += ButtonOptimized_Click;
buttonSilent.Click += ButtonSilent_Click;
@@ -62,6 +63,7 @@ namespace GHelper
button60Hz.Click += Button60Hz_Click;
button120Hz.Click += Button120Hz_Click;
buttonScreenAuto.Click += ButtonScreenAuto_Click;
buttonMiniled.Click += ButtonMiniled_Click;
buttonQuit.Click += ButtonQuit_Click;
@@ -614,13 +616,17 @@ namespace GHelper
SetScreen(60, 0);
}
private void ButtonMiniled_Click(object? sender, EventArgs e)
{
int miniled = (Program.config.getConfig("miniled") == 1) ? 0 : 1;
Program.config.setConfig("miniled", miniled);
SetScreen(-1, -1, miniled);
}
public void SetScreen(int frequency = -1, int overdrive = -1)
public void SetScreen(int frequency = -1, int overdrive = -1, int miniled = -1)
{
int currentFrequency = NativeMethods.GetRefreshRate();
if (currentFrequency < 0) // Laptop screen not detected or has unknown refresh rate
if (NativeMethods.GetRefreshRate() < 0) // Laptop screen not detected or has unknown refresh rate
{
InitScreen();
return;
@@ -629,19 +635,25 @@ namespace GHelper
if (frequency >= 1000)
{
frequency = Program.config.getConfig("max_frequency");
if (frequency <= 60)
frequency = 120;
if (frequency <= 60) frequency = 120;
}
if (frequency <= 0) return;
if (frequency > 0)
{
NativeMethods.SetRefreshRate(frequency);
Logger.WriteLine("Screen " + frequency.ToString() + "Hz");
}
if (overdrive >= 0)
Program.wmi.DeviceSet(ASUSWmi.ScreenOverdrive, overdrive);
InitScreen();
if (miniled >= 0)
{
Program.wmi.DeviceSet(ASUSWmi.ScreenMiniled, miniled);
Debug.WriteLine("Miniled " + miniled);
}
Logger.WriteLine("Screen " + frequency.ToString() + "Hz");
InitScreen();
}
@@ -653,15 +665,8 @@ namespace GHelper
bool screenAuto = (Program.config.getConfig("screen_auto") == 1);
int overdrive = 0;
try
{
overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive);
}
catch
{
Logger.WriteLine("Screen Overdrive not supported");
}
int overdrive = Program.wmi.DeviceGet(ASUSWmi.ScreenOverdrive);
int miniled = Program.wmi.DeviceGet(ASUSWmi.ScreenMiniled);
if (frequency < 0)
{
@@ -711,6 +716,13 @@ namespace GHelper
button120Hz.Text = maxFrequency.ToString() + "Hz + OD";
}
if (miniled >= 0)
{
tableScreen.Controls.Add(buttonMiniled, 3, 0);
buttonMiniled.Activated = (miniled == 1);
Program.config.setConfig("miniled", miniled);
}
Program.config.setConfig("frequency", frequency);
Program.config.setConfig("overdrive", overdrive);
}
@@ -1031,12 +1043,13 @@ namespace GHelper
}
Program.config.setConfig("gpu_mode", GpuMode);
ButtonEnabled(buttonOptimized, true);
ButtonEnabled(buttonEco, true);
ButtonEnabled(buttonStandard, true);
ButtonEnabled(buttonUltimate, true);
Program.config.setConfig("gpu_mode", GpuMode);
VisualiseGPUMode(GpuMode);
return GpuMode;