diff --git a/app/Extra.cs b/app/Extra.cs index 5478c5c1..50f11821 100644 --- a/app/Extra.cs +++ b/app/Extra.cs @@ -4,6 +4,7 @@ using GHelper.Helpers; using GHelper.Input; using GHelper.Mode; using GHelper.UI; +using GHelper.USB; using System.Diagnostics; namespace GHelper @@ -218,10 +219,10 @@ namespace GHelper Shown += Keyboard_Shown; comboKeyboardSpeed.DropDownStyle = ComboBoxStyle.DropDownList; - comboKeyboardSpeed.DataSource = new BindingSource(AsusUSB.GetSpeeds(), null); + comboKeyboardSpeed.DataSource = new BindingSource(Aura.GetSpeeds(), null); comboKeyboardSpeed.DisplayMember = "Value"; comboKeyboardSpeed.ValueMember = "Key"; - comboKeyboardSpeed.SelectedValue = AsusUSB.Speed; + comboKeyboardSpeed.SelectedValue = Aura.Speed; comboKeyboardSpeed.SelectedValueChanged += ComboKeyboardSpeed_SelectedValueChanged; // Keyboard @@ -400,7 +401,7 @@ namespace GHelper else AppConfig.Set("keyboard_brightness", sliderBrightness.Value); - AsusUSB.ApplyBrightness(sliderBrightness.Value, "Slider"); + Aura.ApplyBrightness(sliderBrightness.Value, "Slider"); } private void InitServices() @@ -515,7 +516,7 @@ namespace GHelper private void CheckXMG_CheckedChanged(object? sender, EventArgs e) { AppConfig.Set("xmg_light", (checkXMG.Checked ? 1 : 0)); - AsusUSB.ApplyXGMLight(checkXMG.Checked); + XGM.Light(checkXMG.Checked); } private void CheckUSBC_CheckedChanged(object? sender, EventArgs e) @@ -563,14 +564,14 @@ namespace GHelper AppConfig.Set("keyboard_sleep_logo", (checkSleepLogo.Checked ? 1 : 0)); AppConfig.Set("keyboard_shutdown_logo", (checkShutdownLogo.Checked ? 1 : 0)); - AsusUSB.ApplyAuraPower(); + Aura.ApplyAuraPower(); } private void ComboKeyboardSpeed_SelectedValueChanged(object? sender, EventArgs e) { AppConfig.Set("aura_speed", (int)comboKeyboardSpeed.SelectedValue); - AsusUSB.ApplyAura(); + Aura.ApplyAura(); } diff --git a/app/Fans.cs b/app/Fans.cs index ef113de2..1d1e1f95 100644 --- a/app/Fans.cs +++ b/app/Fans.cs @@ -2,6 +2,7 @@ using GHelper.Gpu.NVidia; using GHelper.Mode; using GHelper.UI; +using GHelper.USB; using Ryzen; using System.Diagnostics; using System.Windows.Forms.DataVisualization.Charting; @@ -1004,7 +1005,7 @@ namespace GHelper InitPowerPlan(); - if (Program.acpi.IsXGConnected()) AsusUSB.ResetXGM(); + if (Program.acpi.IsXGConnected()) XGM.Reset(); if (gpuVisible) diff --git a/app/GHelper.csproj b/app/GHelper.csproj index c0fa3c20..9112b20d 100644 --- a/app/GHelper.csproj +++ b/app/GHelper.csproj @@ -15,7 +15,7 @@ AnyCPU False True - 0.130 + 0.131 @@ -59,7 +59,6 @@ - diff --git a/app/Gpu/GPUModeControl.cs b/app/Gpu/GPUModeControl.cs index 9406542f..831bbf72 100644 --- a/app/Gpu/GPUModeControl.cs +++ b/app/Gpu/GPUModeControl.cs @@ -1,6 +1,7 @@ using GHelper.Display; using GHelper.Gpu.NVidia; using GHelper.Helpers; +using GHelper.USB; using System.Diagnostics; namespace GHelper.Gpu @@ -53,7 +54,7 @@ namespace GHelper.Gpu AppConfig.Set("gpu_mode", gpuMode); settings.VisualiseGPUMode(gpuMode); - AsusUSB.ApplyGPUColor(); + Aura.ApplyGPUColor(); } @@ -284,7 +285,7 @@ namespace GHelper.Gpu if (Program.acpi.IsXGConnected()) { //Program.acpi.DeviceSet(AsusACPI.GPUXGInit, 1, "XG Init"); - AsusUSB.InitXGM(); + XGM.Init(); } } @@ -298,7 +299,7 @@ namespace GHelper.Gpu if (Program.acpi.DeviceGet(AsusACPI.GPUXG) == 1) { - AsusUSB.ResetXGM(); + XGM.Reset(); HardwareControl.KillGPUApps(); DialogResult dialogResult = MessageBox.Show("Did you close all applications running on XG Mobile?", "Disabling XG Mobile", MessageBoxButtons.YesNo); @@ -318,12 +319,12 @@ namespace GHelper.Gpu InitXGM(); - AsusUSB.ApplyXGMLight(AppConfig.Is("xmg_light")); + XGM.Light(AppConfig.Is("xmg_light")); await Task.Delay(TimeSpan.FromSeconds(15)); if (AppConfig.IsMode("auto_apply")) - AsusUSB.SetXGMFan(AppConfig.GetFanConfig(AsusFan.XGM)); + XGM.SetFan(AppConfig.GetFanConfig(AsusFan.XGM)); HardwareControl.RecreateGpuControl(); diff --git a/app/Input/InputDispatcher.cs b/app/Input/InputDispatcher.cs index 0ddf5408..768b5e67 100644 --- a/app/Input/InputDispatcher.cs +++ b/app/Input/InputDispatcher.cs @@ -1,6 +1,7 @@ using GHelper.Display; using GHelper.Helpers; using GHelper.Mode; +using GHelper.USB; using Microsoft.Win32; using System.Diagnostics; using System.Management; @@ -59,7 +60,7 @@ namespace GHelper.Input if (backlightActivity && iddle.TotalSeconds > kb_timeout) { backlightActivity = false; - AsusUSB.ApplyBrightness(0, "Timeout"); + Aura.ApplyBrightness(0, "Timeout"); } if (!backlightActivity && iddle.TotalSeconds < kb_timeout) @@ -478,7 +479,7 @@ namespace GHelper.Input static void ToggleTouchpad() { - KeyboardHook.KeyKeyKeyPress(Keys.LWin, Keys.LControlKey, Keys.F24); + KeyboardHook.KeyKeyKeyPress(Keys.LWin, Keys.LControlKey, Keys.F24, 50); } public static void ToggleArrowLock() @@ -654,8 +655,8 @@ namespace GHelper.Input public static void SetBacklightAuto(bool init = false) { - if (init) AsusUSB.Init(); - AsusUSB.ApplyBrightness(GetBacklight(), "Auto", init); + if (init) Aura.Init(); + Aura.ApplyBrightness(GetBacklight(), "Auto", init); } public static void SetBacklight(int delta, bool force = false) @@ -678,7 +679,7 @@ namespace GHelper.Input if (force || !OptimizationService.IsRunning()) { - AsusUSB.ApplyBrightness(backlight, "HotKey"); + Aura.ApplyBrightness(backlight, "HotKey"); } if (!OptimizationService.IsOSDRunning()) diff --git a/app/Input/KeyboardHook.cs b/app/Input/KeyboardHook.cs index ada85528..3d5c0467 100644 --- a/app/Input/KeyboardHook.cs +++ b/app/Input/KeyboardHook.cs @@ -34,15 +34,17 @@ public sealed class KeyboardHook : IDisposable keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); } - public static void KeyKeyKeyPress(Keys key, Keys key2, Keys key3) + public static void KeyKeyKeyPress(Keys key, Keys key2, Keys key3, int sleep = 0) { keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); keybd_event((byte)key3, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); - keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); - keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); keybd_event((byte)key3, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); + if (sleep > 0) Thread.Sleep(sleep); + keybd_event((byte)key2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); + if (sleep > 0) Thread.Sleep(sleep); + keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); } /// diff --git a/app/Input/KeyboardListener.cs b/app/Input/KeyboardListener.cs index 693e320d..e5828639 100644 --- a/app/Input/KeyboardListener.cs +++ b/app/Input/KeyboardListener.cs @@ -1,4 +1,5 @@ -using HidLibrary; +using HidSharp; +using GHelper.USB; namespace GHelper.Input { @@ -9,14 +10,14 @@ namespace GHelper.Input public KeyboardListener(Action KeyHandler) { - HidDevice? input = AsusUSB.GetDevice(); + HidStream? input = AsusHid.FindHidStream(AsusHid.INPUT_ID); // Fallback if (input == null) { - AsusUSB.Init(); + Aura.Init(); Thread.Sleep(1000); - input = AsusUSB.GetDevice(); + input = input = AsusHid.FindHidStream(AsusHid.INPUT_ID); } if (input == null) @@ -25,7 +26,9 @@ namespace GHelper.Input return; } - Logger.WriteLine($"Input: {input.DevicePath}"); + input.ReadTimeout = int.MaxValue; + + Logger.WriteLine($"Input: {input.Device.DevicePath}"); var task = Task.Run(() => { @@ -35,14 +38,15 @@ namespace GHelper.Input { // Emergency break - if (input == null || !input.IsConnected) + if (input == null || !input.CanRead) { Logger.WriteLine("Listener terminated"); break; } - var data = input.Read().Data; - if (data.Length > 1 && data[0] == AsusUSB.INPUT_HID_ID && data[1] > 0 && data[1] != 236) + + var data = input.Read(); + if (data.Length > 1 && data[0] == AsusHid.INPUT_ID && data[1] > 0 && data[1] != 236) { Logger.WriteLine($"Key: {data[1]}"); KeyHandler(data[1]); diff --git a/app/Mode/ModeControl.cs b/app/Mode/ModeControl.cs index adabb763..94902184 100644 --- a/app/Mode/ModeControl.cs +++ b/app/Mode/ModeControl.cs @@ -1,5 +1,6 @@ using GHelper.Gpu.NVidia; using GHelper.Helpers; +using GHelper.USB; using Ryzen; namespace GHelper.Mode @@ -80,7 +81,7 @@ namespace GHelper.Mode Program.acpi.DeviceSet(AsusACPI.VivoBookMode, vivoMode, "VivoMode"); } - if (AppConfig.Is("xgm_fan") && Program.acpi.IsXGConnected()) AsusUSB.ResetXGM(); + if (AppConfig.Is("xgm_fan") && Program.acpi.IsXGConnected()) XGM.Reset(); if (notify) Program.toast.RunToast(Modes.GetCurrentName(), SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online ? ToastIcon.Charger : ToastIcon.Battery); @@ -133,8 +134,7 @@ namespace GHelper.Mode bool xgmFan = false; if (AppConfig.Is("xgm_fan") && Program.acpi.IsXGConnected()) { - //AsusUSB.InitXGM(); - AsusUSB.SetXGMFan(AppConfig.GetFanConfig(AsusFan.XGM)); + XGM.SetFan(AppConfig.GetFanConfig(AsusFan.XGM)); xgmFan = true; } diff --git a/app/Settings.cs b/app/Settings.cs index 2eaeb247..eac41e79 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -10,6 +10,7 @@ using GHelper.Mode; using GHelper.Peripherals; using GHelper.Peripherals.Mouse; using GHelper.UI; +using GHelper.USB; using System.Diagnostics; using System.Timers; @@ -324,7 +325,7 @@ namespace GHelper { case 0: Logger.WriteLine("Monitor Power Off"); - AsusUSB.ApplyBrightness(0); + Aura.ApplyBrightness(0); break; case 1: Logger.WriteLine("Monitor Power On"); @@ -698,16 +699,16 @@ namespace GHelper public void InitAura() { - AsusUSB.Mode = AppConfig.Get("aura_mode"); - AsusUSB.Speed = AppConfig.Get("aura_speed"); - AsusUSB.SetColor(AppConfig.Get("aura_color")); - AsusUSB.SetColor2(AppConfig.Get("aura_color2")); + Aura.Mode = AppConfig.Get("aura_mode"); + Aura.Speed = AppConfig.Get("aura_speed"); + Aura.SetColor(AppConfig.Get("aura_color")); + Aura.SetColor2(AppConfig.Get("aura_color2")); comboKeyboard.DropDownStyle = ComboBoxStyle.DropDownList; - comboKeyboard.DataSource = new BindingSource(AsusUSB.GetModes(), null); + comboKeyboard.DataSource = new BindingSource(Aura.GetModes(), null); comboKeyboard.DisplayMember = "Value"; comboKeyboard.ValueMember = "Key"; - comboKeyboard.SelectedValue = AsusUSB.Mode; + comboKeyboard.SelectedValue = Aura.Mode; comboKeyboard.SelectedValueChanged += ComboKeyboard_SelectedValueChanged; @@ -729,7 +730,7 @@ namespace GHelper { Task.Run(() => { - AsusUSB.ApplyAura(); + Aura.ApplyAura(); VisualiseAura(); }); } @@ -738,9 +739,9 @@ namespace GHelper { Invoke(delegate { - pictureColor.BackColor = AsusUSB.Color1; - pictureColor2.BackColor = AsusUSB.Color2; - pictureColor2.Visible = AsusUSB.HasSecondColor(); + pictureColor.BackColor = Aura.Color1; + pictureColor2.BackColor = Aura.Color2; + pictureColor2.Visible = Aura.HasSecondColor(); }); } @@ -1044,14 +1045,14 @@ namespace GHelper if (!AppConfig.Is("skip_aura")) { - AsusUSB.ApplyAuraPower(); - AsusUSB.ApplyAura(); + Aura.ApplyAuraPower(); + Aura.ApplyAura(); } InputDispatcher.SetBacklightAuto(true); if (Program.acpi.IsXGConnected()) - AsusUSB.ApplyXGMLight(AppConfig.Is("xmg_light")); + XGM.Light(AppConfig.Is("xmg_light")); if (AppConfig.HasTabletMode()) InputDispatcher.TabletMode(); diff --git a/app/USB/AsusHid.cs b/app/USB/AsusHid.cs new file mode 100644 index 00000000..cc2bae75 --- /dev/null +++ b/app/USB/AsusHid.cs @@ -0,0 +1,96 @@ +using HidSharp; +using HidSharp.Reports; +using System.Diagnostics; + +namespace GHelper.USB; +public static class AsusHid +{ + public const int ASUS_ID = 0x0b05; + + public const byte INPUT_ID = 0x5a; + public const byte AURA_ID = 0x5d; + + static int[] deviceIds = { 0x1a30, 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0, 0x18c6, 0x1abe }; + + static HidStream _stream; + + public static HidStream FindHidStream(byte reportId, int minFeatureLength = 1) + { + HidDeviceLoader loader = new HidDeviceLoader(); + var deviceList = loader.GetDevices(ASUS_ID).Where(device => deviceIds.Contains(device.ProductID)); + + foreach (var device in deviceList) if (device.CanOpen) + { + try + { + var config = new OpenConfiguration(); + config.SetOption(OpenOption.Interruptible, false); + config.SetOption(OpenOption.Exclusive, false); + config.SetOption(OpenOption.Priority, 10); + HidStream hidStream = device.Open(); + + if (device.GetMaxFeatureReportLength() >= minFeatureLength) + { + var reportDescriptor = device.GetReportDescriptor(); + if (reportDescriptor.TryGetReport(ReportType.Feature, reportId, out _)) + { + return hidStream; + } + } + + hidStream.Close(); + hidStream.Dispose(); + } + catch (Exception ex) + { + Debug.WriteLine($"Error accessing HID device: {ex.Message}"); + } + } + + return null; + } + + static void WriteData(HidStream stream, byte[] data) + { + try + { + stream.Write(data); + Logger.WriteLine("USB " + stream.Device.ProductID + ": " + BitConverter.ToString(data)); + } + catch (Exception ex) + { + Debug.WriteLine($"Error writing to HID device: {ex.Message} {BitConverter.ToString(data)}"); + } + } + + public static void Write(byte[] data, byte reportId = AURA_ID) + { + using (var stream = FindHidStream(reportId)) + WriteData(stream, data); + } + public static void Write(List dataList, byte reportId = AURA_ID) + { + using (var stream = FindHidStream(reportId)) + foreach (var data in dataList) + WriteData(stream, data); + } + + public static void WriteAura(byte[] data, byte reportId = AURA_ID) + { + + if (_stream == null) _stream = FindHidStream(reportId); + if (_stream == null) return; + + try + { + _stream.Write(data); + } + catch (Exception ex) + { + _stream.Dispose(); + Debug.WriteLine($"Error writing data to HID device: {ex.Message}"); + } + } + +} + diff --git a/app/AsusUSB.cs b/app/USB/Aura.cs similarity index 58% rename from app/AsusUSB.cs rename to app/USB/Aura.cs index ba70cf64..4c38b290 100644 --- a/app/AsusUSB.cs +++ b/app/USB/Aura.cs @@ -1,12 +1,9 @@ using GHelper.Gpu; using GHelper.Helpers; using System.Text; -using HidLibrary; -namespace GHelper +namespace GHelper.USB { - - public class AuraPower { public bool BootLogo; @@ -35,26 +32,19 @@ namespace GHelper } - public static class AsusUSB + public static class Aura { public const int HEATMAP = 20; public const int GPUMODE = 21; - public const int ASUS_ID = 0x0b05; - - 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_INIT1 = new byte[] { AsusHid.AURA_ID, 0xb9 }; public static readonly byte[] LED_INIT2 = Encoding.ASCII.GetBytes("]ASUS Tech.Inc."); - public static readonly byte[] LED_INIT3 = new byte[] { AURA_HID_ID, 0x05, 0x20, 0x31, 0, 0x1a }; + public static readonly byte[] LED_INIT3 = new byte[] { AsusHid.AURA_ID, 0x05, 0x20, 0x31, 0, 0x1a }; 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, 0x1a }; - static byte[] MESSAGE_APPLY = { AURA_HID_ID, 0xb4 }; - static byte[] MESSAGE_SET = { AURA_HID_ID, 0xb5, 0, 0, 0 }; - - static int[] deviceIds = { 0x1a30, 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0, 0x18c6, 0x1abe }; + static byte[] MESSAGE_APPLY = { AsusHid.AURA_ID, 0xb4 }; + static byte[] MESSAGE_SET = { AsusHid.AURA_ID, 0xb5, 0, 0, 0 }; private static int mode = 0; private static int speed = 1; @@ -68,10 +58,7 @@ namespace GHelper static bool isOldHeatmap = AppConfig.Is("old_heatmap"); - - static System.Timers.Timer timer = new System.Timers.Timer(2000); - static HidDevice? auraDevice = null; - + static System.Timers.Timer timer = new System.Timers.Timer(80); static byte[] AuraPowerMessage(AuraPower flags) { @@ -114,14 +101,18 @@ namespace GHelper return new byte[] { 0x5d, 0xbd, 0x01, keyb, bar, lid, rear, 0xFF }; } - static AsusUSB() + static Aura() { timer.Elapsed += Timer_Elapsed; - isSingleColor = AppConfig.IsSingleColor(); // Mono Color - var device = GetDevice(AURA_HID_ID); - if (device is not null && (device.Attributes.Version == 22 || device.Attributes.Version == 23) && (AppConfig.ContainsModel("GA402X") || AppConfig.ContainsModel("GA402N"))) isSingleColor = true; + using (var stream = AsusHid.FindHidStream(AsusHid.AURA_ID)) + { + if (stream is null) return; + if (stream.Device.ReleaseNumberBcd == 22 || stream.Device.ReleaseNumberBcd == 23 && (AppConfig.ContainsModel("GA402X") || AppConfig.ContainsModel("GA402N"))) isSingleColor = true; + stream.Close(); + } + } private static void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) @@ -232,7 +223,7 @@ namespace GHelper public static bool HasSecondColor() { - return (mode == 1 && !isTuf); + return mode == 1 && !isTuf; } public static int Speed @@ -259,37 +250,11 @@ namespace GHelper } - - private static IEnumerable GetHidDevices(int[] deviceIds, int minFeatures = 1) - { - HidDevice[] HidDeviceList = HidDevices.Enumerate(ASUS_ID, deviceIds).ToArray(); - foreach (HidDevice device in HidDeviceList) - if (device.IsConnected && device.Capabilities.FeatureReportByteLength >= minFeatures) - yield return device; - } - - public static HidDevice? GetDevice(byte reportID = INPUT_HID_ID) - { - HidDevice[] HidDeviceList = HidDevices.Enumerate(ASUS_ID, deviceIds).ToArray(); - HidDevice input = null; - - foreach (HidDevice device in HidDeviceList) - if (device.ReadFeatureData(out byte[] data, reportID)) - { - input = device; - //Logger.WriteLine("HID Device("+ reportID + ")" + + device.Capabilities.FeatureReportByteLength + "|" + device.Capabilities.InputReportByteLength + device.DevicePath); - if (reportID == INPUT_HID_ID && device.Attributes.ProductId == 0x1a30) return input; - } - - return input; - } - - public static byte[] AuraMessage(int mode, Color color, Color color2, int speed, bool mono = false) { byte[] msg = new byte[17]; - msg[0] = AURA_HID_ID; + msg[0] = AsusHid.AURA_ID; msg[1] = 0xb3; msg[2] = 0x00; // Zone msg[3] = (byte)mode; // Aura Mode @@ -298,7 +263,7 @@ namespace GHelper msg[6] = mono ? (byte)0 : color.B; // B msg[7] = (byte)speed; // aura.speed as u8; msg[8] = 0; // aura.direction as u8; - msg[9] = (mode == 1) ? (byte)1 : (byte)0; + msg[9] = mode == 1 ? (byte)1 : (byte)0; msg[10] = color2.R; // R msg[11] = mono ? (byte)0 : color2.G; // G msg[12] = mono ? (byte)0 : color2.B; // B @@ -309,71 +274,20 @@ namespace GHelper { Task.Run(async () => { - var devices = GetHidDevices(deviceIds); - foreach (HidDevice device in devices) - { - device.OpenDevice(); - device.WriteFeatureData(LED_INIT1); - device.WriteFeatureData(LED_INIT2); - device.WriteFeatureData(LED_INIT3); - device.WriteFeatureData(LED_INIT4); - device.WriteFeatureData(LED_INIT5); - device.CloseDevice(); - } + AsusHid.Write(new List { LED_INIT1, LED_INIT2, LED_INIT3, LED_INIT4, LED_INIT5 }); }); } public static void ApplyBrightness(int brightness, string log = "Backlight", bool delay = false) { - - Task.Run(async () => { - if (delay) await Task.Delay(TimeSpan.FromSeconds(1)); - if (isTuf) Program.acpi.TUFKeyboardBrightness(brightness); - byte[] msg = { AURA_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; - byte[] msgBackup = { INPUT_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; - - var devices = GetHidDevices(deviceIds); - foreach (HidDevice device in devices) - { - device.OpenDevice(); - - if (device.ReadFeatureData(out byte[] data, AURA_HID_ID)) - { - device.WriteFeatureData(msg); - Logger.WriteLine(log + ":" + BitConverter.ToString(msg)); - } - - if (AppConfig.ContainsModel("GA503") && device.ReadFeatureData(out byte[] dataBackkup, INPUT_HID_ID)) - { - device.WriteFeatureData(msgBackup); - Logger.WriteLine(log + ":" + BitConverter.ToString(msgBackup)); - } - - device.CloseDevice(); - } - - // Backup payload for old models - /* - if (AppConfig.ContainsModel("GA503RW")) - { - byte[] msgBackup = { INPUT_HID_ID, 0xba, 0xc5, 0xc4, (byte)brightness }; - - var devicesBackup = GetHidDevices(deviceIds); - foreach (HidDevice device in devicesBackup) - { - device.OpenDevice(); - device.WriteFeatureData(msgBackup); - device.CloseDevice(); - } - } - */ - + AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }); + if (AppConfig.ContainsModel("GA503")) AsusHid.Write(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.INPUT_ID); }); @@ -415,19 +329,7 @@ namespace GHelper flags.SleepRear = AppConfig.IsNotFalse("keyboard_sleep_lid"); flags.ShutdownRear = AppConfig.IsNotFalse("keyboard_shutdown_lid"); - var devices = GetHidDevices(deviceIds); - byte[] msg = AuraPowerMessage(flags); - - foreach (HidDevice device in devices) - { - device.OpenDevice(); - if (device.ReadFeatureData(out byte[] data, AURA_HID_ID)) - { - device.WriteFeatureData(msg); - Logger.WriteLine("USB-KB " + device.Attributes.ProductHexId + ":" + BitConverter.ToString(msg)); - } - device.CloseDevice(); - } + AsusHid.Write(AuraPowerMessage(flags)); if (isTuf) Program.acpi.TUFKeyboardPower( @@ -438,26 +340,6 @@ namespace GHelper } - - static void GetAuraDevice() - { - var devices = GetHidDevices(deviceIds); - foreach (HidDevice device in devices) - { - device.OpenDevice(); - if (device.ReadFeatureData(out byte[] data, AURA_HID_ID)) - { - Logger.WriteLine("Aura Device:" + device.DevicePath); - auraDevice = device; - return; - } - else - { - device.CloseDevice(); - } - } - } - public static void ApplyColor(Color color, bool init = false) { @@ -467,9 +349,6 @@ namespace GHelper return; } - if (auraDevice is null || !auraDevice.IsConnected) GetAuraDevice(); - if (auraDevice is null || !auraDevice.IsConnected) return; - if (isStrix && !isOldHeatmap) { byte[] msg = new byte[0x40]; @@ -477,7 +356,7 @@ namespace GHelper byte start = 9; byte maxLeds = 0x93; - msg[0] = AURA_HID_ID; + msg[0] = AsusHid.AURA_ID; msg[1] = 0xbc; msg[2] = 0; msg[3] = 1; @@ -493,38 +372,32 @@ namespace GHelper msg[start + 2 + i * 3] = color.B; // B } - if (init) { - auraDevice.Write(LED_INIT1); - auraDevice.Write(LED_INIT2); - auraDevice.Write(LED_INIT3); - auraDevice.Write(LED_INIT4); - auraDevice.Write(LED_INIT5); - auraDevice.Write(new byte[] { AURA_HID_ID, 0xbc }); + Init(); + AsusHid.WriteAura(new byte[] { AsusHid.AURA_ID, 0xbc }); } for (byte b = 0; b < maxLeds; b += 0x10) { msg[6] = b; - auraDevice.Write(msg); + AsusHid.WriteAura(msg); } msg[6] = maxLeds; - auraDevice.Write(msg); + AsusHid.WriteAura(msg); msg[4] = 4; msg[5] = 0; msg[6] = 0; msg[7] = 0; - auraDevice.Write(msg); + AsusHid.WriteAura(msg); } else { - //Debug.WriteLine(color.ToString()); - auraDevice.Write(AuraMessage(0, color, color, 0)); - auraDevice.Write(MESSAGE_SET); + AsusHid.WriteAura(AuraMessage(0, color, color, 0)); + AsusHid.WriteAura(MESSAGE_SET); } } @@ -534,8 +407,6 @@ namespace GHelper { if (AppConfig.Get("aura_mode") != GPUMODE) return; - Logger.WriteLine(GPUModeControl.gpuMode.ToString()); - switch (GPUModeControl.gpuMode) { case AsusACPI.GPUModeUltimate: @@ -574,36 +445,9 @@ namespace GHelper return; } - int _speed; - switch (Speed) - { - case 1: - _speed = 0xeb; - break; - case 2: - _speed = 0xf5; - break; - default: - _speed = 0xe1; - break; - } + int _speed = (Speed == 1) ? 0xeb : (Speed == 2) ? 0xf5 : 0xe1; - byte[] msg; - var devices = GetHidDevices(deviceIds); - - foreach (HidDevice device in devices) - { - device.OpenDevice(); - if (device.ReadFeatureData(out byte[] data, AURA_HID_ID)) - { - msg = AuraMessage(Mode, Color1, Color2, _speed, isSingleColor); - device.WriteFeatureData(msg); - device.WriteFeatureData(MESSAGE_APPLY); - device.WriteFeatureData(MESSAGE_SET); - Logger.WriteLine("USB-KB " + device.Attributes.Version + device.Description + device.DevicePath + ":" + BitConverter.ToString(msg)); - } - device.CloseDevice(); - } + AsusHid.Write(new List { AuraMessage(Mode, Color1, Color2, _speed, isSingleColor), MESSAGE_APPLY, MESSAGE_SET }); if (isTuf) Program.acpi.TUFKeyboardRGB(Mode, Color1, _speed); @@ -611,71 +455,6 @@ namespace GHelper } - // Reference : thanks to https://github.com/RomanYazvinsky/ for initial discovery of XGM payloads - public static int SetXGM(byte[] msg) - { - - //Logger.WriteLine("XGM Payload :" + BitConverter.ToString(msg)); - - var payload = new byte[300]; - Array.Copy(msg, payload, msg.Length); - - foreach (HidDevice device in GetHidDevices(new int[] { 0x1970 }, 300)) - { - device.OpenDevice(); - Logger.WriteLine("XGM " + device.Attributes.ProductHexId + "|" + device.Capabilities.FeatureReportByteLength + ":" + BitConverter.ToString(msg)); - device.WriteFeatureData(payload); - device.CloseDevice(); - //return 1; - } - - return 0; - } - - public static void InitXGM() - { - byte[] ASUS_INIT = Encoding.ASCII.GetBytes("^ASUS Tech.Inc."); - - SetXGM(ASUS_INIT); - - /* - SetXGM(new byte[] { 0x5e, 0xd0, 0x02 }); - SetXGM(new byte[] { 0x5e, 0xd0, 0x03 }); - SetXGM(ASUS_INIT); - SetXGM(new byte[] { 0x5e, 0xd1, 0x02 }); // reset fan - SetXGM(ASUS_INIT); - SetXGM(new byte[] { 0x5e, 0xce, 0x03 }); - SetXGM(new byte[] { 0x5e, 0xd0, 0x04 }); - SetXGM(new byte[] { 0x5e, 0xd0, 0x01 }); - */ - } - - public static void ApplyXGMLight(bool status) - { - SetXGM(new byte[] { 0x5e, 0xc5, status ? (byte)0x50 : (byte)0 }); - } - - - public static int ResetXGM() - { - return SetXGM(new byte[] { 0x5e, 0xd1, 0x02 }); - } - - public static int SetXGMFan(byte[] curve) - { - - if (AsusACPI.IsInvalidCurve(curve)) return -1; - - //InitXGM(); - - byte[] msg = new byte[19]; - Array.Copy(new byte[] { 0x5e, 0xd1, 0x01 }, msg, 3); - Array.Copy(curve, 0, msg, 3, curve.Length); - - return SetXGM(msg); - } - - } } \ No newline at end of file diff --git a/app/USB/XGM.cs b/app/USB/XGM.cs new file mode 100644 index 00000000..8aac792f --- /dev/null +++ b/app/USB/XGM.cs @@ -0,0 +1,65 @@ +// Reference : thanks to https://github.com/RomanYazvinsky/ for initial discovery of XGM payloads + +using HidSharp; +using System.Diagnostics; +using System.Text; + +namespace GHelper.USB +{ + public static class XGM + { + const int XGM_ID = 0x1970; + public const int ASUS_ID = 0x0b05; + public static void Write(byte[] data) + { + HidDeviceLoader loader = new HidDeviceLoader(); + HidDevice device = loader.GetDevices(ASUS_ID, XGM_ID).Where(device => device.GetMaxFeatureReportLength() >= 300).FirstOrDefault(); + if (device is null) return; + + try + { + using (HidStream hidStream = device.Open()) + { + var payload = new byte[300]; + Array.Copy(data, payload, data.Length); + + hidStream.Write(payload); + Logger.WriteLine("XGM " + device.ProductID + "|" + device.GetMaxFeatureReportLength() + ":" + BitConverter.ToString(data)); + + hidStream.Close(); + } + } + catch (Exception ex) + { + Debug.WriteLine($"Error accessing HID device: {ex.Message}"); + } + } + + public static void Init() + { + Write(Encoding.ASCII.GetBytes("^ASUS Tech.Inc.")); + } + + public static void Light(bool status) + { + Write(new byte[] { 0x5e, 0xc5, status ? (byte)0x50 : (byte)0 }); + } + + + public static void Reset() + { + Write(new byte[] { 0x5e, 0xd1, 0x02 }); + } + + public static void SetFan(byte[] curve) + { + if (AsusACPI.IsInvalidCurve(curve)) return; + + byte[] msg = new byte[19]; + Array.Copy(new byte[] { 0x5e, 0xd1, 0x01 }, msg, 3); + Array.Copy(curve, 0, msg, 3, curve.Length); + + Write(msg); + } + } +}