Skip to content

Commit

Permalink
Added screen refresh and display overdrive changes, and fixed crash o…
Browse files Browse the repository at this point in the history
…n 2021 model
  • Loading branch information
seerge committed Feb 17, 2023
1 parent d5416e4 commit 2ba9a0b
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 37 deletions.
131 changes: 129 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Management;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;

public class ASUSWmi
{
Expand All @@ -19,6 +20,7 @@ public class ASUSWmi
public const int GPUMux = 0x00090016;

public const int BatteryLimit = 0x00120057;
public const int ScreenOverdrive = 0x00050019;

public const int PerformanceBalanced = 0;
public const int PerformanceTurbo = 1;
Expand Down Expand Up @@ -49,8 +51,29 @@ private int WMICall(string MethodName, int Device_Id, int Control_status = -1)
ManagementBaseObject outParams = this.mo.InvokeMethod(MethodName, inParams, null);
foreach (PropertyData property in outParams.Properties)
{
if (property.Name == "device_status") return int.Parse(property.Value.ToString()) - 65536;
if (property.Name == "result") return int.Parse(property.Value.ToString());
if (property.Name == "device_status")
{
int status;
try
{
status = int.Parse(property.Value.ToString());
status -= 65536;
return status;
} catch
{
return -1;
}
}
if (property.Name == "result")
{
try
{
return int.Parse(property.Value.ToString());
} catch
{
return -1;
}
}
}

return -1;
Expand Down Expand Up @@ -182,6 +205,103 @@ public void setConfig(string name, int value)



}


public class NativeMethods
{

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;

public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;

public short dmLogPixels;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
};

// PInvoke declaration for EnumDisplaySettings Win32 API
[DllImport("user32.dll")]
public static extern int EnumDisplaySettingsExA(
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 const int ENUM_CURRENT_SETTINGS = -1;


public static DEVMODE CreateDevmode()
{
DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
return dm;
}

public static int GetRefreshRate()
{
DEVMODE dm = CreateDevmode();

int frequency = -1;

if (0 != NativeMethods.EnumDisplaySettingsExA(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
{
Debug.WriteLine(JsonConvert.SerializeObject(dm));
frequency = dm.dmDisplayFrequency;
}

return frequency;
}

public static void SetRefreshRate(int frequency = 120)
{
DEVMODE dm = CreateDevmode();

if (0 != NativeMethods.EnumDisplaySettingsExA(null,NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
{
dm.dmDisplayFrequency = frequency;
int iRet = NativeMethods.ChangeDisplaySettings(ref dm, 0);
}
}

}

namespace GHelper
Expand Down Expand Up @@ -223,6 +343,8 @@ public static void Main()
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));

settingsForm.VisualiseGPUAuto(config.getConfig("gpu_auto"));
settingsForm.VisualiseScreenAuto(config.getConfig("screen_auto"));

settingsForm.SetStartupCheck(scheduler.IsScheduled());

Application.Run();
Expand All @@ -233,6 +355,9 @@ public static void Main()
static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
var collection = (ManagementEventWatcher)sender;

if (e.NewEvent is null) return;

int EventID = int.Parse(e.NewEvent["EventID"].ToString());

Debug.WriteLine(EventID);
Expand All @@ -250,13 +375,15 @@ static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
settingsForm.BeginInvoke(delegate
{
settingsForm.AutoGPUMode(0);
settingsForm.AutoScreen(0);
});
return;
case 88: // Plugged
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));
settingsForm.BeginInvoke(delegate
{
settingsForm.AutoGPUMode(1);
settingsForm.AutoScreen(1);
});
return;

Expand Down
10 changes: 10 additions & 0 deletions Properties/Resources.Designer.cs

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

17 changes: 10 additions & 7 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,25 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="icons8-charging-battery-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-charging-battery-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="eco" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\eco.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="ultimate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ultimate.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="standard" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\standard.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="ultimate" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ultimate.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
<data name="icons8-speed-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-speed-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8-video-card-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-video-card-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8-speed-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-speed-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
<data name="icons8-charging-battery-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-charging-battery-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8-laptop-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-laptop-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Loading

0 comments on commit 2ba9a0b

Please sign in to comment.