From c13a6e9ded3a59d4e6226a5dd667eb8de295e1d2 Mon Sep 17 00:00:00 2001 From: seerge Date: Sun, 9 Apr 2023 12:14:47 +0200 Subject: [PATCH] Custom keybinds for performance modes --- app/ASUSWmi.cs | 4 +-- app/HardwareMonitor.cs | 2 ++ app/NativeMethods.cs | 48 +++++++++++++++++++++++++++++++++--- app/Program.cs | 6 ++++- app/Settings.cs | 55 +++++++++++++++++++++++++++--------------- 5 files changed, 89 insertions(+), 26 deletions(-) diff --git a/app/ASUSWmi.cs b/app/ASUSWmi.cs index bbe9eefa..f8ae770d 100644 --- a/app/ASUSWmi.cs +++ b/app/ASUSWmi.cs @@ -60,11 +60,11 @@ public class ASUSWmi public const int GPUModeUltimate = 2; - public const int MaxTotal = 180; + public const int MaxTotal = 200; public const int MinTotal = 5; public const int DefaultTotal = 125; - public const int MaxCPU = 90; + public const int MaxCPU = 130; public const int MinCPU = 5; public const int DefaultCPU = 80; diff --git a/app/HardwareMonitor.cs b/app/HardwareMonitor.cs index a7cf85fd..53aea845 100644 --- a/app/HardwareMonitor.cs +++ b/app/HardwareMonitor.cs @@ -1,6 +1,7 @@ using GHelper; using GHelper.Gpu; using System.Diagnostics; +using System.Management; public static class HardwareMonitor { @@ -62,6 +63,7 @@ public static class HardwareMonitor return 0; } + public static void ReadSensors() { batteryDischarge = -1; diff --git a/app/NativeMethods.cs b/app/NativeMethods.cs index 3abc8aec..84235b29 100644 --- a/app/NativeMethods.cs +++ b/app/NativeMethods.cs @@ -1,12 +1,53 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; -using System.Security.Cryptography; -using System.Xml.Linq; using static Tools.ScreenInterrogatory; namespace Tools { + + public class KeyHandler + { + + public const int NOMOD = 0x0000; + public const int ALT = 0x0001; + public const int CTRL = 0x0002; + public const int SHIFT = 0x0004; + public const int WIN = 0x0008; + + public const int WM_HOTKEY_MSG_ID = 0x0312; + + + [DllImport("user32.dll")] + private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); + [DllImport("user32.dll")] + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + private int modifier; + private int key; + private IntPtr hWnd; + private int id; + public KeyHandler(int modifier, Keys key, nint handle) + { + this.modifier = modifier; + this.key = (int)key; + this.hWnd = handle; + id = this.GetHashCode(); + } + public override int GetHashCode() + { + return modifier ^ key ^ hWnd.ToInt32(); + } + public bool Register() + { + return RegisterHotKey(hWnd, id, modifier, key); + } + public bool Unregiser() + { + return UnregisterHotKey(hWnd, id); + } + } + public static class ScreenInterrogatory { public const int ERROR_SUCCESS = 0; @@ -608,7 +649,8 @@ public class NativeMethods if (dm.dmDisplayFrequency > frequency) frequency = dm.dmDisplayFrequency; i++; } - } else + } + else { if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) { diff --git a/app/Program.cs b/app/Program.cs index b80e584c..50c109f9 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Win32; using System.Diagnostics; using System.Management; +using Tools; namespace GHelper { @@ -57,7 +58,7 @@ namespace GHelper } Logger.WriteLine("------------"); - Logger.WriteLine("App launched"); + Logger.WriteLine("App launched: " + config.GetModel()); Application.EnableVisualStyles(); @@ -84,6 +85,9 @@ namespace GHelper var settingGuid = new NativeMethods.PowerSettingGuid(); unRegPowerNotify = NativeMethods.RegisterPowerSettingNotification(ds, settingGuid.ConsoleDisplayState, NativeMethods.DEVICE_NOTIFY_WINDOW_HANDLE); + // CTRL + SHIFT + F5 to cycle profiles + var ghk = new KeyHandler(KeyHandler.SHIFT | KeyHandler.CTRL, Keys.F5, ds); + ghk.Register(); if (Environment.CurrentDirectory.Trim('\\') == Application.StartupPath.Trim('\\')) { diff --git a/app/Settings.cs b/app/Settings.cs index 2227f27a..fb8b6a43 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -5,6 +5,7 @@ using System.Drawing.Imaging; using System.Reflection; using System.Text.Json; using System.Timers; +using Tools; namespace GHelper { @@ -27,6 +28,9 @@ namespace GHelper static AnimeMatrixDevice mat; static long lastRefresh; + private bool customFans = false; + private int customPower = 0; + public SettingsForm() { InitializeComponent(); @@ -185,7 +189,7 @@ namespace GHelper private static void TrayIcon_MouseMove(object? sender, MouseEventArgs e) { - Program.settingsForm.RefreshSensors(); + Program.settingsForm.RefreshSensors(); } @@ -231,7 +235,7 @@ namespace GHelper private void ButtonOptimized_MouseHover(object? sender, EventArgs e) { - labelTipGPU.Text = "Switch to Eco on battery and to Standard when plugged"; + labelTipGPU.Text = "Sptch to Eco on battery and to Standard when plugged"; } private void ButtonGPU_MouseLeave(object? sender, EventArgs e) @@ -280,6 +284,9 @@ namespace GHelper } m.Result = (IntPtr)1; break; + case KeyHandler.WM_HOTKEY_MSG_ID: + CyclePerformanceMode(); + break; } base.WndProc(ref m); } @@ -854,13 +861,17 @@ namespace GHelper } } + + private void SetPerformanceLabel() + { + labelPerf.Text = "Performance Mode" + (customFans?"+":"") + ((customPower > 0) ? " "+customPower+"W" : ""); + } + public void SetPower() { int limit_total = Program.config.getConfigPerf("limit_total"); int limit_cpu = Program.config.getConfigPerf("limit_cpu"); - string limitLabel = null; - if (limit_total > ASUSWmi.MaxTotal) return; if (limit_total < ASUSWmi.MinTotal) return; @@ -870,25 +881,23 @@ namespace GHelper if (Program.wmi.DeviceGet(ASUSWmi.PPT_TotalA0) >= 0) { Program.wmi.DeviceSet(ASUSWmi.PPT_TotalA0, limit_total, "PowerLimit A"); - limitLabel = limit_total + "W"; + customPower = limit_total; } if (Program.wmi.DeviceGet(ASUSWmi.PPT_CPUB0) >= 0) { Program.wmi.DeviceSet(ASUSWmi.PPT_CPUB0, limit_cpu, "PowerLimit B"); - limitLabel = limit_cpu + "W"; + customPower = limit_cpu; } - Program.settingsForm.BeginInvoke(delegate - { - labelPerf.Text = "Performance Mode+ " + limitLabel; - }); + Program.settingsForm.BeginInvoke(SetPerformanceLabel); } public void AutoFans() { + customFans = false; if (Program.config.getConfigPerf("auto_apply") == 1) { @@ -904,16 +913,18 @@ namespace GHelper Logger.WriteLine("Driver rejected fan curve, resetting mode to " + mode); Program.wmi.DeviceSet(ASUSWmi.PerformanceMode, mode, "PerformanceMode"); } - else - labelPerf.Text = "Performance Mode+"; + else customFans = true; } - else - labelPerf.Text = "Performance Mode"; + + Program.settingsForm.BeginInvoke(SetPerformanceLabel); } public void AutoPower(int delay = 0) { + + customPower = 0; + if (Program.config.getConfigPerf("auto_apply_power") == 1) { if (delay > 0) @@ -1003,15 +1014,19 @@ namespace GHelper fans.InitPower(); fans.InitBoost(); } - - - } public void CyclePerformanceMode() { - SetPerformanceMode(Program.config.getConfig("performance_mode") + 1, true); + int mode = Program.config.getConfig("performance_mode"); + + if (Control.ModifierKeys == Keys.Shift) + mode = (mode == 0) ? 2 : mode - 1; + else + mode++; + + SetPerformanceMode(mode, true); } @@ -1021,10 +1036,10 @@ namespace GHelper if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online) Aura.ApplyBrightness(3); - //Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Up); + //Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Up); else Aura.ApplyBrightness(0); - //Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Down); + //Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Down); }