diff --git a/app/AsusUSB.cs b/app/AsusUSB.cs index cdf9f57a..5f1492f1 100644 --- a/app/AsusUSB.cs +++ b/app/AsusUSB.cs @@ -53,16 +53,19 @@ namespace GHelper public static class AsusUSB { - public const byte HID_ID = 0x5a; + public const int ASUS_ID = 0x0b05; - public static readonly byte[] LED_INIT1 = new byte[] { 0x5d, 0xb9 }; + public const byte INPUT_HID_ID = 0x5a; + public const byte AURA_HID_ID = 0x5d; + + public static readonly byte[] LED_INIT1 = new byte[] { AURA_HID_ID, 0xb9 }; public static readonly byte[] LED_INIT2 = Encoding.ASCII.GetBytes("]ASUS Tech.Inc."); - public static readonly byte[] LED_INIT3 = new byte[] { 0x5d, 0x05, 0x20, 0x31, 0, 0x08 }; + public static readonly byte[] LED_INIT3 = new byte[] { AURA_HID_ID, 0x05, 0x20, 0x31, 0, 0x08 }; public static readonly byte[] LED_INIT4 = Encoding.ASCII.GetBytes("^ASUS Tech.Inc."); public static readonly byte[] LED_INIT5 = new byte[] { 0x5e, 0x05, 0x20, 0x31, 0, 0x08 }; - static byte[] MESSAGE_SET = { 0x5d, 0xb5, 0, 0, 0 }; - static byte[] MESSAGE_APPLY = { 0x5d, 0xb4 }; + static byte[] MESSAGE_SET = { AURA_HID_ID, 0xb5, 0, 0, 0 }; + static byte[] MESSAGE_APPLY = { AURA_HID_ID, 0xb4 }; static int[] deviceIds = { 0x1a30, 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0, 0x18c6 }; @@ -174,72 +177,42 @@ namespace GHelper private static IEnumerable GetHidDevices(int[] deviceIds, int minInput = 18) { - HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); + HidDevice[] HidDeviceList = HidDevices.Enumerate(ASUS_ID, deviceIds).ToArray(); foreach (HidDevice device in HidDeviceList) if (device.IsConnected && device.Capabilities.FeatureReportByteLength > 0 - && device.Capabilities.InputReportByteLength >= minInput) // + && device.Capabilities.InputReportByteLength >= minInput) yield return device; } - private static HidDevice? GetInputDevice() + public static HidDevice? GetDevice(byte reportID = INPUT_HID_ID) { - HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); + HidDevice[] HidDeviceList = HidDevices.Enumerate(ASUS_ID, deviceIds).ToArray(); HidDevice input = null; foreach (HidDevice device in HidDeviceList) - if (device.ReadFeatureData(out byte[] data, HID_ID)) + if (device.ReadFeatureData(out byte[] data, reportID)) { input = device; - Logger.WriteLine("Input Events" + device.Capabilities.FeatureReportByteLength + "|" + device.Capabilities.InputReportByteLength + device.Description + device.DevicePath); + //Logger.WriteLine("HID Device("+ reportID + ")" + + device.Capabilities.FeatureReportByteLength + "|" + device.Capabilities.InputReportByteLength + device.DevicePath); } - if (input is null) Logger.WriteLine("Input device not found"); - return input; } public static bool TouchpadToggle() { - HidDevice? input = GetInputDevice(); - if (input != null) return input.WriteFeatureData(new byte[] { HID_ID, 0xf4, 0x6b }); + HidDevice? input = GetDevice(); + if (input != null) return input.WriteFeatureData(new byte[] { INPUT_HID_ID, 0xf4, 0x6b }); return false; } - public static void RunListener(Action KeyHandler) - { - HidDevice? input = GetInputDevice(); - if (input == null) return; - - //Logger.WriteLine("Input Events " + input.DevicePath); - - var task = Task.Run(() => - { - try - { - while (true) - { - var data = input.Read().Data; - if (data.Length > 1 && data[0] == HID_ID && data[1] > 0) - { - Logger.WriteLine("Key:" + data[1]); - KeyHandler(data[1]); - } - } - } - catch (Exception ex) - { - Logger.WriteLine(ex.ToString()); - } - }); - - } public static byte[] AuraMessage(int mode, Color color, Color color2, int speed) { byte[] msg = new byte[17]; - msg[0] = 0x5d; + msg[0] = AURA_HID_ID; msg[1] = 0xb3; msg[2] = 0x00; // Zone msg[3] = (byte)mode; // Aura Mode @@ -277,14 +250,15 @@ namespace GHelper { Task.Run(() => { - byte[] msg = { 0x5d, 0xba, 0xc5, 0xc4, (byte)brightness }; - var devices = GetHidDevices(deviceIds); + byte[] msg = { INPUT_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; + Logger.WriteLine("KB Backlight:" + BitConverter.ToString(msg)); + + var devices = GetHidDevices(deviceIds, 0); foreach (HidDevice device in devices) { device.OpenDevice(); device.WriteFeatureData(msg); - Logger.WriteLine("USB-KB " + device.Capabilities.FeatureReportByteLength + ":" + BitConverter.ToString(msg)); device.CloseDevice(); } diff --git a/app/InputDispatcher.cs b/app/InputDispatcher.cs index 247e2e03..30ee5325 100644 --- a/app/InputDispatcher.cs +++ b/app/InputDispatcher.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using HidLibrary; +using Microsoft.Win32; using NAudio.CoreAudioApi; using System.Diagnostics; using System.Management; @@ -6,6 +7,58 @@ using Tools; namespace GHelper { + public class KeyboardListener + { + + CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + + public KeyboardListener(Action KeyHandler) + { + HidDevice? input = AsusUSB.GetDevice(); + + if (input == null) + { + Logger.WriteLine("Input device not found"); + return; + } + else + { + Logger.WriteLine("Listener input " + input.DevicePath); + } + + + var task = Task.Run(() => + { + try + { + while (!cancellationTokenSource.Token.IsCancellationRequested) + { + var data = input.Read().Data; + if (data.Length > 1 && data[0] == AsusUSB.INPUT_HID_ID && data[1] > 0) + { + Logger.WriteLine("Key:" + data[1]); + KeyHandler(data[1]); + } + } + Logger.WriteLine("Listener stopped"); + + } + catch (Exception ex) + { + Logger.WriteLine(ex.ToString()); + } + }); + + + } + + public void Dispose() + { + cancellationTokenSource?.Cancel(); + } + } + + public class InputDispatcher { @@ -14,6 +67,8 @@ namespace GHelper public static Keys keyProfile = Keys.F5; + KeyboardListener listener; + KeyHandler m1, m2, togggle; public InputDispatcher(nint handle) @@ -23,8 +78,6 @@ namespace GHelper Program.acpi.SubscribeToEvents(WatcherEventArrived); - if (!isOptimizationRunning) AsusUSB.RunListener(HandleEvent); - // CTRL + SHIFT + F5 to cycle profiles if (AppConfig.getConfig("keybind_profile") != -1) keyProfile = (Keys)AppConfig.getConfig("keybind_profile"); @@ -33,8 +86,19 @@ namespace GHelper m2 = new KeyHandler(KeyHandler.NOMOD, Keys.VolumeUp, windowHandle); RegisterKeys(); + } + public void InitListener() + { + if (listener is not null) listener.Dispose(); + + if (!OptimizationService.IsRunning()) + listener = new KeyboardListener(HandleEvent); + } + + + public void RegisterKeys() { diff --git a/app/Program.cs b/app/Program.cs index ae4c956b..808e982f 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -1,11 +1,8 @@ using Microsoft.Win32; -using NAudio.CoreAudioApi; using System.Diagnostics; using System.Globalization; -using System.Management; using System.Reflection; using System.Security.Principal; -using Tools; using static NativeMethods; namespace GHelper @@ -43,11 +40,11 @@ namespace GHelper string language = AppConfig.getConfigString("language"); - if (language != null && language.Length > 0) + if (language != null && language.Length > 0) Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language); - else + else Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture; - + Debug.WriteLine(CultureInfo.CurrentUICulture); //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr"); @@ -86,7 +83,7 @@ namespace GHelper settingsForm.InitMatrix(); settingsForm.SetStartupCheck(Startup.IsScheduled()); - + SetAutoModes(); // Subscribing for system power change events @@ -103,7 +100,7 @@ namespace GHelper SettingsToggle(action); } - + Application.Run(); @@ -147,6 +144,8 @@ namespace GHelper isPlugged = SystemInformation.PowerStatus.PowerLineStatus; Logger.WriteLine("AutoSetting for " + isPlugged.ToString()); + inputDispatcher.InitListener(); + settingsForm.SetBatteryChargeLimit(AppConfig.getConfig("charge_limit")); settingsForm.AutoPerformance(); diff --git a/app/Properties/Strings.fr.resx b/app/Properties/Strings.fr.resx index 2780df4c..e9e382ed 100644 --- a/app/Properties/Strings.fr.resx +++ b/app/Properties/Strings.fr.resx @@ -187,7 +187,7 @@ Limite à 60 Hz pour éco. batterie, valeur normale sur secteur - Éveillé + Allumé Délai (secondes) avant extinction rétroéclairage @@ -199,7 +199,7 @@ Limite de charge - Lors du démarrage + Au démarrage Luminosité @@ -361,7 +361,7 @@ Désactiver le micro - Ouvrir la fenêtre G-Helper + Ouvrir G-Helper Optimisé diff --git a/app/Settings.cs b/app/Settings.cs index 5fdc2212..380f839c 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -93,8 +93,6 @@ namespace GHelper comboMatrix.DropDownClosed += ComboMatrix_SelectedValueChanged; comboMatrixRunning.DropDownClosed += ComboMatrixRunning_SelectedValueChanged; - checkMatrix.CheckedChanged += CheckMatrix_CheckedChanged; ; - buttonMatrix.Click += ButtonMatrix_Click; checkStartup.CheckedChanged += CheckStartup_CheckedChanged; @@ -735,7 +733,7 @@ namespace GHelper comboMatrixRunning.SelectedIndex = (running != -1) ? Math.Min(running, comboMatrixRunning.Items.Count - 1) : 0; checkMatrix.Checked = (AppConfig.getConfig("matrix_auto") == 1); - + checkMatrix.CheckedChanged += CheckMatrix_CheckedChanged; } @@ -1267,13 +1265,12 @@ namespace GHelper { AsusUSB.Init(); - if (AppConfig.getConfig("keyboard_auto") != 1) return; + int backlight = AppConfig.getConfig("keyboard_brightness"); - if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online) - AsusUSB.ApplyBrightness(3); - else + if (AppConfig.isConfig("keyboard_auto") && SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Online) AsusUSB.ApplyBrightness(0); - + else if (backlight >= 0) + AsusUSB.ApplyBrightness(backlight); }