diff --git a/app/AppConfig.cs b/app/AppConfig.cs index 548c1275..c6f64cc9 100644 --- a/app/AppConfig.cs +++ b/app/AppConfig.cs @@ -71,11 +71,11 @@ public static class AppConfig File.WriteAllText(configFile, jsonString); } - public static int getConfig(string name) + public static int getConfig(string name, int empty = -1) { if (config.ContainsKey(name)) return int.Parse(config[name].ToString()); - else return -1; + else return empty; } public static bool isConfig(string name) @@ -83,11 +83,11 @@ public static class AppConfig return getConfig(name) == 1; } - public static string getConfigString(string name) + public static string getConfigString(string name, string empty = null) { if (config.ContainsKey(name)) return config[name].ToString(); - else return null; + else return empty; } public static void setConfig(string name, int value) diff --git a/app/AsusUSB.cs b/app/AsusUSB.cs index a7f95467..9c801da3 100644 --- a/app/AsusUSB.cs +++ b/app/AsusUSB.cs @@ -248,23 +248,31 @@ namespace GHelper public static void ApplyBrightness(int brightness) { - Task.Run(async () => + if (AppConfig.ContainsModel("TUF")) + Program.acpi.TUFKeyboardBrightness(brightness); + + byte[] msg = { AURA_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; + + var devices = GetHidDevices(deviceIds); + foreach (HidDevice device in devices) { - - byte[] msg = { INPUT_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; + device.OpenDevice(); + device.WriteFeatureData(msg); Logger.WriteLine("KB Backlight:" + BitConverter.ToString(msg)); + device.CloseDevice(); + } - var devices = GetHidDevices(deviceIds, 0); - foreach (HidDevice device in devices) - { - device.OpenDevice(); - device.WriteFeatureData(msg); - device.CloseDevice(); - } - if (AppConfig.ContainsModel("TUF")) - Program.acpi.TUFKeyboardBrightness(brightness); - }); + // Backup payload for old models + byte[] msgBackup = { INPUT_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; + + var devicesBackup = GetHidDevices(deviceIds, 0); + foreach (HidDevice device in devicesBackup) + { + device.OpenDevice(); + device.WriteFeatureData(msgBackup); + device.CloseDevice(); + } } @@ -381,23 +389,6 @@ namespace GHelper } - public static void SetBacklightOffDelay(int value = 60) - { - try - { - RegistryKey myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ASUS\ASUS System Control Interface\AsusOptimization\ASUS Keyboard Hotkeys", true); - if (myKey != null) - { - myKey.SetValue("TurnOffKeybdLight", value, RegistryValueKind.DWord); - myKey.Close(); - } - } - catch (Exception ex) - { - Logger.WriteLine(ex.Message); - } - } - } diff --git a/app/Extra.cs b/app/Extra.cs index a9708f28..9348781a 100644 --- a/app/Extra.cs +++ b/app/Extra.cs @@ -209,7 +209,7 @@ namespace GHelper checkXMG.Checked = !(AppConfig.getConfig("xmg_light") == 0); checkXMG.CheckedChanged += CheckXMG_CheckedChanged; - int kb_timeout = AppConfig.getConfig("keyboard_light_tiomeout"); + int kb_timeout = AppConfig.getConfig("keyboard_timeout"); numericBacklightTime.Value = (kb_timeout >= 0) ? kb_timeout : 60; numericBacklightTime.ValueChanged += NumericBacklightTime_ValueChanged; @@ -218,9 +218,7 @@ namespace GHelper private void NumericBacklightTime_ValueChanged(object? sender, EventArgs e) { - Program.RunAsAdmin("extra"); - AppConfig.setConfig("keyboard_light_tiomeout", (int)numericBacklightTime.Value); - AsusUSB.SetBacklightOffDelay((int)numericBacklightTime.Value); + AppConfig.setConfig("keyboard_timeout", (int)numericBacklightTime.Value); } private void CheckXMG_CheckedChanged(object? sender, EventArgs e) diff --git a/app/GHelper.csproj b/app/GHelper.csproj index f0a9aedc..9cd7a1c5 100644 --- a/app/GHelper.csproj +++ b/app/GHelper.csproj @@ -16,7 +16,7 @@ AnyCPU False True - 0.68 + 0.69 diff --git a/app/InputDispatcher.cs b/app/InputDispatcher.cs index 92cd466c..a24e09c1 100644 --- a/app/InputDispatcher.cs +++ b/app/InputDispatcher.cs @@ -54,6 +54,8 @@ namespace GHelper public class InputDispatcher { + System.Timers.Timer timer = new System.Timers.Timer(1000); + public bool backlight = true; private static nint windowHandle; @@ -84,15 +86,43 @@ namespace GHelper RegisterKeys(); + timer.Elapsed += Timer_Elapsed; + } - public void InitListener() + private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) + { + TimeSpan iddle = NativeMethods.GetIdleTime(); + int kb_timeout = AppConfig.getConfig("keyboard_timeout", 60); + + if (kb_timeout == 0) return; + + if (backlight && iddle.TotalSeconds > kb_timeout) + { + backlight = false; + AsusUSB.ApplyBrightness(0); + } + + if (!backlight && iddle.TotalSeconds < kb_timeout) + { + backlight = true; + AsusUSB.ApplyBrightness(AppConfig.getConfig("keyboard_brightness")); + } + + //Debug.WriteLine(iddle.TotalSeconds); + } + + public void Init() { if (listener is not null) listener.Dispose(); + Program.acpi.DeviceInit(); + if (!OptimizationService.IsRunning()) listener = new KeyboardListener(HandleEvent); + timer.Enabled = (AppConfig.getConfig("keyboard_timeout") > 0 && SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Online); + } diff --git a/app/NativeMethods.cs b/app/NativeMethods.cs index a4e211b5..8845eacd 100644 --- a/app/NativeMethods.cs +++ b/app/NativeMethods.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar; using static Tools.ScreenInterrogatory; namespace Tools @@ -351,6 +352,25 @@ namespace Tools public class NativeMethods { + internal struct LASTINPUTINFO + { + public uint cbSize; + public uint dwTime; + } + + [DllImport("User32.dll")] + private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + public static TimeSpan GetIdleTime() + { + LASTINPUTINFO lastInPut = new LASTINPUTINFO(); + lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut); + GetLastInputInfo(ref lastInPut); + return TimeSpan.FromMilliseconds((uint)Environment.TickCount - lastInPut.dwTime); + + } + + private const int WM_SYSCOMMAND = 0x0112; private const int SC_MONITORPOWER = 0xF170; private const int MONITOR_OFF = 2; diff --git a/app/OptimizationService.cs b/app/OptimizationService.cs index c54ab72f..c032002e 100644 --- a/app/OptimizationService.cs +++ b/app/OptimizationService.cs @@ -1,6 +1,7 @@ using System.Text.RegularExpressions; using System.Text; using System.Diagnostics; +using Microsoft.Win32; namespace GHelper { @@ -40,6 +41,25 @@ namespace GHelper return (Process.GetProcessesByName("AsusOptimization").Count() > 0); } + + public static void SetBacklightOffDelay(int value = 60) + { + try + { + RegistryKey myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ASUS\ASUS System Control Interface\AsusOptimization\ASUS Keyboard Hotkeys", true); + if (myKey != null) + { + myKey.SetValue("TurnOffKeybdLight", value, RegistryValueKind.DWord); + myKey.Close(); + } + } + catch (Exception ex) + { + Logger.WriteLine(ex.Message); + } + } + + } } \ No newline at end of file diff --git a/app/Program.cs b/app/Program.cs index 808e982f..ce622060 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -29,7 +29,7 @@ namespace GHelper public static InputDispatcher inputDispatcher; - private static PowerLineStatus isPlugged = PowerLineStatus.Unknown; + private static PowerLineStatus isPlugged = SystemInformation.PowerStatus.PowerLineStatus; // The main entry point for the application public static void Main(string[] args) @@ -144,7 +144,7 @@ namespace GHelper isPlugged = SystemInformation.PowerStatus.PowerLineStatus; Logger.WriteLine("AutoSetting for " + isPlugged.ToString()); - inputDispatcher.InitListener(); + inputDispatcher.Init(); settingsForm.SetBatteryChargeLimit(AppConfig.getConfig("charge_limit")); settingsForm.AutoPerformance(); @@ -163,9 +163,10 @@ namespace GHelper private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { + if (SystemInformation.PowerStatus.PowerLineStatus == isPlugged) return; - Logger.WriteLine("Windows - Power Mode Changed"); + Logger.WriteLine("Power Mode Changed"); SetAutoModes(); } diff --git a/app/Settings.cs b/app/Settings.cs index 0e6c65e9..fe615df5 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -153,6 +153,55 @@ namespace GHelper } + protected override void WndProc(ref Message m) + { + + switch (m.Msg) + { + case NativeMethods.WM_POWERBROADCAST: + if (m.WParam == (IntPtr)NativeMethods.PBT_POWERSETTINGCHANGE) + { + var settings = (NativeMethods.POWERBROADCAST_SETTING)m.GetLParam(typeof(NativeMethods.POWERBROADCAST_SETTING)); + switch (settings.Data) + { + case 0: + Logger.WriteLine("Monitor Power Off"); + AsusUSB.ApplyBrightness(0); + break; + case 1: + Logger.WriteLine("Monitor Power On"); + Program.SetAutoModes(); + break; + case 2: + Logger.WriteLine("Monitor Dimmed"); + break; + } + } + m.Result = (IntPtr)1; + break; + + case KeyHandler.WM_HOTKEY_MSG_ID: + + Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); + + switch (key) + { + case Keys.VolumeDown: + InputDispatcher.KeyProcess("m1"); + break; + case Keys.VolumeUp: + InputDispatcher.KeyProcess("m2"); + break; + default: + if (key == InputDispatcher.keyProfile) CyclePerformanceMode(); + break; + } + + break; + } + base.WndProc(ref m); + } + public void RunToast(string text, ToastIcon? icon = null) { toast.RunToast(text, icon); @@ -493,59 +542,6 @@ namespace GHelper AutoScreen(); } - protected override void WndProc(ref Message m) - { - - switch (m.Msg) - { - case NativeMethods.WM_POWERBROADCAST: - if (m.WParam == (IntPtr)NativeMethods.PBT_POWERSETTINGCHANGE) - { - var settings = (NativeMethods.POWERBROADCAST_SETTING)m.GetLParam(typeof(NativeMethods.POWERBROADCAST_SETTING)); - switch (settings.Data) - { - case 0: - Logger.WriteLine("Monitor Power Off"); - AsusUSB.ApplyBrightness(0); - SetBatteryChargeLimit(AppConfig.getConfig("charge_limit")); - break; - case 1: - Logger.WriteLine("Monitor Power On"); - Program.SetAutoModes(); - break; - case 2: - Logger.WriteLine("Monitor Dimmed"); - break; - } - } - m.Result = (IntPtr)1; - break; - - case KeyHandler.WM_HOTKEY_MSG_ID: - - Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); - - switch (key) - { - case Keys.VolumeDown: - InputDispatcher.KeyProcess("m1"); - break; - case Keys.VolumeUp: - InputDispatcher.KeyProcess("m2"); - break; - default: - if (key == InputDispatcher.keyProfile) CyclePerformanceMode(); - break; - } - - break; - } - base.WndProc(ref m); - } - - - - private void CheckStartup_CheckedChanged(object? sender, EventArgs e) {