From 5c4500315cf4233c25cf7f8ea4143a788d448041 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 12 Nov 2023 22:28:37 +0100 Subject: [PATCH] Migrated Aura & XGM communication to HidSharp library to speed things up (#1594) * Cleanup * Touchpad toggle fix for FA507 https://github.com/seerge/g-helper/issues/1589 * Cleanup --- app/AppConfig.cs | 7 +- app/AsusACPI.cs | 3 +- app/AsusUSB.cs | 681 ---------------------------------- app/Extra.cs | 13 +- app/Fans.cs | 3 +- app/GHelper.csproj | 1 - app/Gpu/GPUModeControl.cs | 11 +- app/Input/InputDispatcher.cs | 33 +- app/Input/KeyboardHook.cs | 8 +- app/Input/KeyboardListener.cs | 20 +- app/Mode/ModeControl.cs | 6 +- app/Settings.cs | 29 +- app/USB/AsusHid.cs | 96 +++++ app/USB/Aura.cs | 475 ++++++++++++++++++++++++ app/USB/XGM.cs | 65 ++++ 15 files changed, 716 insertions(+), 735 deletions(-) delete mode 100644 app/AsusUSB.cs create mode 100644 app/USB/AsusHid.cs create mode 100644 app/USB/Aura.cs create mode 100644 app/USB/XGM.cs diff --git a/app/AppConfig.cs b/app/AppConfig.cs index 41168871..097e6da6 100644 --- a/app/AppConfig.cs +++ b/app/AppConfig.cs @@ -355,7 +355,7 @@ public static class AppConfig public static bool IsStrixLimitedRGB() { - return ContainsModel("G614JV") || ContainsModel("G614JZ") || ContainsModel("G512LI"); + return ContainsModel("G614JV") || ContainsModel("G614JZ") || ContainsModel("G512LI") || ContainsModel("G513RS"); } public static bool IsZ13() @@ -441,4 +441,9 @@ public static class AppConfig return ContainsModel("GV301RA") || ContainsModel("GV302XA") || IsAlly(); } + public static bool IsHardwareTouchpadToggle() + { + return ContainsModel("FA507"); + } + } diff --git a/app/AsusACPI.cs b/app/AsusACPI.cs index f2d1abed..50844515 100644 --- a/app/AsusACPI.cs +++ b/app/AsusACPI.cs @@ -1,4 +1,5 @@ using GHelper; +using GHelper.USB; using System.Management; using System.Runtime.InteropServices; @@ -549,7 +550,7 @@ public class AsusACPI DeviceSet(TUF_KB_BRIGHTNESS, param, "TUF Brightness"); } - public void TUFKeyboardRGB(int mode, Color color, int speed, string? log = "TUF RGB") + public void TUFKeyboardRGB(AuraMode mode, Color color, int speed, string? log = "TUF RGB") { byte[] setting = new byte[6]; diff --git a/app/AsusUSB.cs b/app/AsusUSB.cs deleted file mode 100644 index ba70cf64..00000000 --- a/app/AsusUSB.cs +++ /dev/null @@ -1,681 +0,0 @@ -using GHelper.Gpu; -using GHelper.Helpers; -using System.Text; -using HidLibrary; - -namespace GHelper -{ - - - public class AuraPower - { - public bool BootLogo; - public bool BootKeyb; - public bool AwakeLogo; - public bool AwakeKeyb; - public bool SleepLogo; - public bool SleepKeyb; - public bool ShutdownLogo; - public bool ShutdownKeyb; - - public bool BootBar; - public bool AwakeBar; - public bool SleepBar; - public bool ShutdownBar; - - public bool BootLid; - public bool AwakeLid; - public bool SleepLid; - public bool ShutdownLid; - - public bool BootRear; - public bool AwakeRear; - public bool SleepRear; - public bool ShutdownRear; - } - - - public static class AsusUSB - { - 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_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_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 }; - - private static int mode = 0; - private static int speed = 1; - public static Color Color1 = Color.White; - public static Color Color2 = Color.Black; - - static bool isTuf = AppConfig.IsTUF() || AppConfig.IsVivobook(); - static bool isStrix = AppConfig.IsStrix(); - - static public bool isSingleColor = false; - - static bool isOldHeatmap = AppConfig.Is("old_heatmap"); - - - static System.Timers.Timer timer = new System.Timers.Timer(2000); - static HidDevice? auraDevice = null; - - - static byte[] AuraPowerMessage(AuraPower flags) - { - byte keyb = 0, bar = 0, lid = 0, rear = 0; - - if (flags.BootLogo) keyb |= 1 << 0; - if (flags.BootKeyb) keyb |= 1 << 1; - if (flags.AwakeLogo) keyb |= 1 << 2; - if (flags.AwakeKeyb) keyb |= 1 << 3; - if (flags.SleepLogo) keyb |= 1 << 4; - if (flags.SleepKeyb) keyb |= 1 << 5; - if (flags.ShutdownLogo) keyb |= 1 << 6; - if (flags.ShutdownKeyb) keyb |= 1 << 7; - - if (flags.BootBar) bar |= 1 << 1; - if (flags.AwakeBar) bar |= 1 << 2; - if (flags.SleepBar) bar |= 1 << 3; - if (flags.ShutdownBar) bar |= 1 << 4; - - if (flags.BootLid) lid |= 1 << 0; - if (flags.AwakeLid) lid |= 1 << 1; - if (flags.SleepLid) lid |= 1 << 2; - if (flags.ShutdownLid) lid |= 1 << 3; - - if (flags.BootLid) lid |= 1 << 4; - if (flags.AwakeLid) lid |= 1 << 5; - if (flags.SleepLid) lid |= 1 << 6; - if (flags.ShutdownLid) lid |= 1 << 7; - - if (flags.BootRear) rear |= 1 << 0; - if (flags.AwakeRear) rear |= 1 << 1; - if (flags.SleepRear) rear |= 1 << 2; - if (flags.ShutdownRear) rear |= 1 << 3; - - if (flags.BootRear) rear |= 1 << 4; - if (flags.AwakeRear) rear |= 1 << 5; - if (flags.SleepRear) rear |= 1 << 6; - if (flags.ShutdownRear) rear |= 1 << 7; - - return new byte[] { 0x5d, 0xbd, 0x01, keyb, bar, lid, rear, 0xFF }; - } - - static AsusUSB() - { - 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; - } - - private static void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) - { - SetHeatmap(); - } - - static void SetHeatmap(bool init = false) - { - float cpuTemp = (float)HardwareControl.GetCPUTemp(); - int freeze = 20, cold = 40, warm = 65, hot = 90; - Color color; - - //Debug.WriteLine(cpuTemp); - - if (cpuTemp < cold) color = ColorUtilities.GetWeightedAverage(Color.Blue, Color.Green, ((float)cpuTemp - freeze) / (cold - freeze)); - else if (cpuTemp < warm) color = ColorUtilities.GetWeightedAverage(Color.Green, Color.Yellow, ((float)cpuTemp - cold) / (warm - cold)); - else if (cpuTemp < hot) color = ColorUtilities.GetWeightedAverage(Color.Yellow, Color.Red, ((float)cpuTemp - warm) / (hot - warm)); - else color = Color.Red; - - ApplyColor(color, init); - } - - public static Dictionary GetSpeeds() - { - return new Dictionary - { - { 0, Properties.Strings.AuraSlow }, - { 1, Properties.Strings.AuraNormal }, - { 2, Properties.Strings.AuraFast } - }; - } - - static Dictionary _modesSingleColor = new Dictionary - { - { 0, Properties.Strings.AuraStatic }, - { 1, Properties.Strings.AuraBreathe }, - { 10, Properties.Strings.AuraStrobe }, - }; - - static Dictionary _modes = new Dictionary - { - { 0, Properties.Strings.AuraStatic }, - { 1, Properties.Strings.AuraBreathe }, - { 2, Properties.Strings.AuraColorCycle }, - { 3, Properties.Strings.AuraRainbow }, - { 10, Properties.Strings.AuraStrobe }, - { HEATMAP, "Heatmap"}, - { GPUMODE, "GPU Mode" } - }; - - static Dictionary _modesStrix = new Dictionary - { - { 0, Properties.Strings.AuraStatic }, - { 1, Properties.Strings.AuraBreathe }, - { 2, Properties.Strings.AuraColorCycle }, - { 3, Properties.Strings.AuraRainbow }, - { 4, "Star" }, - { 5, "Rain" }, - { 6, "Highlight" }, - { 7, "Laser" }, - { 8, "Ripple" }, - { 10, Properties.Strings.AuraStrobe}, - { 11, "Comet" }, - { 12, "Flash" }, - { HEATMAP, "Heatmap"} - }; - - - public static Dictionary GetModes() - { - if (isTuf) - { - _modes.Remove(3); - } - - if (isSingleColor) - { - return _modesSingleColor; - } - - if (AppConfig.IsAdvantageEdition()) - { - return _modes; - } - - if (AppConfig.IsStrix() && !AppConfig.IsStrixLimitedRGB()) - { - return _modesStrix; - } - - return _modes; - } - - - public static int Mode - { - get { return mode; } - set - { - if (GetModes().ContainsKey(value)) - mode = value; - else - mode = 0; - } - } - - - public static bool HasSecondColor() - { - return (mode == 1 && !isTuf); - } - - public static int Speed - { - get { return speed; } - set - { - if (GetSpeeds().ContainsKey(value)) - speed = value; - else - speed = 1; - } - - } - - public static void SetColor(int colorCode) - { - Color1 = Color.FromArgb(colorCode); - } - - public static void SetColor2(int colorCode) - { - Color2 = Color.FromArgb(colorCode); - } - - - - 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[1] = 0xb3; - msg[2] = 0x00; // Zone - msg[3] = (byte)mode; // Aura Mode - msg[4] = color.R; // R - msg[5] = mono ? (byte)0 : color.G; // G - 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[10] = color2.R; // R - msg[11] = mono ? (byte)0 : color2.G; // G - msg[12] = mono ? (byte)0 : color2.B; // B - return msg; - } - - public static void Init() - { - 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(); - } - }); - } - - - 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(); - } - } - */ - - }); - - - } - - - public static void ApplyAuraPower() - { - - AuraPower flags = new(); - - // Keyboard - flags.AwakeKeyb = AppConfig.IsNotFalse("keyboard_awake"); - flags.BootKeyb = AppConfig.IsNotFalse("keyboard_boot"); - flags.SleepKeyb = AppConfig.IsNotFalse("keyboard_sleep"); - flags.ShutdownKeyb = AppConfig.IsNotFalse("keyboard_shutdown"); - - // Logo - flags.AwakeLogo = AppConfig.IsNotFalse("keyboard_awake_logo"); - flags.BootLogo = AppConfig.IsNotFalse("keyboard_boot_logo"); - flags.SleepLogo = AppConfig.IsNotFalse("keyboard_sleep_logo"); - flags.ShutdownLogo = AppConfig.IsNotFalse("keyboard_shutdown_logo"); - - // Lightbar - flags.AwakeBar = AppConfig.IsNotFalse("keyboard_awake_bar"); - flags.BootBar = AppConfig.IsNotFalse("keyboard_boot_bar"); - flags.SleepBar = AppConfig.IsNotFalse("keyboard_sleep_bar"); - flags.ShutdownBar = AppConfig.IsNotFalse("keyboard_shutdown_bar"); - - // Lid - flags.AwakeLid = AppConfig.IsNotFalse("keyboard_awake_lid"); - flags.BootLid = AppConfig.IsNotFalse("keyboard_boot_lid"); - flags.SleepLid = AppConfig.IsNotFalse("keyboard_sleep_lid"); - flags.ShutdownLid = AppConfig.IsNotFalse("keyboard_shutdown_lid"); - - // Rear Bar - flags.AwakeRear = AppConfig.IsNotFalse("keyboard_awake_lid"); - flags.BootRear = AppConfig.IsNotFalse("keyboard_boot_lid"); - 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(); - } - - if (isTuf) - Program.acpi.TUFKeyboardPower( - flags.AwakeKeyb, - flags.BootKeyb, - flags.SleepKeyb, - flags.ShutdownKeyb); - - } - - - 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) - { - - if (isTuf) - { - Program.acpi.TUFKeyboardRGB(0, color, 0, null); - return; - } - - if (auraDevice is null || !auraDevice.IsConnected) GetAuraDevice(); - if (auraDevice is null || !auraDevice.IsConnected) return; - - if (isStrix && !isOldHeatmap) - { - byte[] msg = new byte[0x40]; - - byte start = 9; - byte maxLeds = 0x93; - - msg[0] = AURA_HID_ID; - msg[1] = 0xbc; - msg[2] = 0; - msg[3] = 1; - msg[4] = 1; - msg[5] = 1; - msg[6] = 0; - msg[7] = 0x10; - - for (byte i = 0; i < 0x12; i++) - { - msg[start + i * 3] = color.R; // R - msg[start + 1 + i * 3] = color.G; // G - 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 }); - } - - for (byte b = 0; b < maxLeds; b += 0x10) - { - msg[6] = b; - auraDevice.Write(msg); - } - - msg[6] = maxLeds; - auraDevice.Write(msg); - - msg[4] = 4; - msg[5] = 0; - msg[6] = 0; - msg[7] = 0; - auraDevice.Write(msg); - } - - else - { - //Debug.WriteLine(color.ToString()); - auraDevice.Write(AuraMessage(0, color, color, 0)); - auraDevice.Write(MESSAGE_SET); - } - - } - - - public static void ApplyGPUColor() - { - if (AppConfig.Get("aura_mode") != GPUMODE) return; - - Logger.WriteLine(GPUModeControl.gpuMode.ToString()); - - switch (GPUModeControl.gpuMode) - { - case AsusACPI.GPUModeUltimate: - ApplyColor(Color.Red, true); - break; - case AsusACPI.GPUModeEco: - ApplyColor(Color.Green, true); - break; - default: - ApplyColor(Color.Yellow, true); - break; - } - } - - - public static void ApplyAura() - { - - Mode = AppConfig.Get("aura_mode"); - Speed = AppConfig.Get("aura_speed"); - SetColor(AppConfig.Get("aura_color")); - SetColor2(AppConfig.Get("aura_color2")); - - timer.Enabled = false; - - if (Mode == HEATMAP) - { - SetHeatmap(true); - timer.Enabled = true; - return; - } - - if (Mode == GPUMODE) - { - ApplyGPUColor(); - return; - } - - int _speed; - switch (Speed) - { - case 1: - _speed = 0xeb; - break; - case 2: - _speed = 0xf5; - break; - default: - _speed = 0xe1; - break; - } - - 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(); - } - - if (isTuf) - Program.acpi.TUFKeyboardRGB(Mode, Color1, _speed); - - } - - - // 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/Extra.cs b/app/Extra.cs index 5478c5c1..889e3909 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.ApplyPower(); } 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 7d602778..9112b20d 100644 --- a/app/GHelper.csproj +++ b/app/GHelper.csproj @@ -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..8154f6ed 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) @@ -302,10 +303,10 @@ namespace GHelper.Input KeyboardHook.KeyKeyPress(Keys.LWin, Keys.P); break; case Keys.F10: - HandleOptimizationEvent(107); + ToggleTouchpadEvent(true); break; case Keys.F11: - HandleOptimizationEvent(108); + SleepEvent(); break; case Keys.F12: KeyboardHook.KeyKeyPress(Keys.LWin, Keys.A); @@ -476,9 +477,21 @@ namespace GHelper.Input } } + static void ToggleTouchpadEvent(bool hotkey = false) + { + if (hotkey || !AppConfig.IsHardwareTouchpadToggle()) ToggleTouchpad(); + Thread.Sleep(200); + Program.toast.RunToast(GetTouchpadState() ? "On" : "Off", ToastIcon.Touchpad); + } + static void ToggleTouchpad() { - KeyboardHook.KeyKeyKeyPress(Keys.LWin, Keys.LControlKey, Keys.F24); + KeyboardHook.KeyKeyKeyPress(Keys.LWin, Keys.LControlKey, Keys.F24, 50); + } + + static void SleepEvent() + { + Program.acpi.DeviceSet(AsusACPI.UniversalControl, AsusACPI.KB_Sleep, "Sleep"); } public static void ToggleArrowLock() @@ -619,12 +632,10 @@ namespace GHelper.Input Program.acpi.DeviceSet(AsusACPI.UniversalControl, AsusACPI.Brightness_Up, "Brightness"); break; case 107: // FN+F10 - ToggleTouchpad(); - Thread.Sleep(200); - Program.toast.RunToast(GetTouchpadState() ? "On" : "Off", ToastIcon.Touchpad); + ToggleTouchpadEvent(); break; case 108: // FN+F11 - Program.acpi.DeviceSet(AsusACPI.UniversalControl, AsusACPI.KB_Sleep, "Sleep"); + SleepEvent(); break; case 106: // Screenpad button on DUO if (Control.ModifierKeys == Keys.Shift) @@ -654,8 +665,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 +689,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..b5b025eb 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 = (AuraMode)AppConfig.Get("aura_mode"); + Aura.Speed = (AuraSpeed)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.ApplyPower(); + 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..faf9535e --- /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 auraStream; + + 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, string log = "USB") + { + try + { + stream.Write(data); + Logger.WriteLine($"{log} " + stream.Device.ProductID + ": " + BitConverter.ToString(data)); + } + catch (Exception ex) + { + Debug.WriteLine($"Error writing {log} to HID device: {ex.Message} {BitConverter.ToString(data)}"); + } + } + + public static void Write(byte[] data, byte reportId = AURA_ID, string log = "USB") + { + using (var stream = FindHidStream(reportId)) + WriteData(stream, data, log); + } + 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) + { + + if (auraStream == null) auraStream = FindHidStream(AURA_ID); + if (auraStream == null) return; + + try + { + auraStream.Write(data); + } + catch (Exception ex) + { + auraStream.Dispose(); + Debug.WriteLine($"Error writing data to HID device: {ex.Message}"); + } + } + +} + diff --git a/app/USB/Aura.cs b/app/USB/Aura.cs new file mode 100644 index 00000000..3a512518 --- /dev/null +++ b/app/USB/Aura.cs @@ -0,0 +1,475 @@ +using GHelper.Gpu; +using GHelper.Helpers; +using System.Text; + +namespace GHelper.USB +{ + public class AuraPower + { + public bool BootLogo; + public bool BootKeyb; + public bool AwakeLogo; + public bool AwakeKeyb; + public bool SleepLogo; + public bool SleepKeyb; + public bool ShutdownLogo; + public bool ShutdownKeyb; + + public bool BootBar; + public bool AwakeBar; + public bool SleepBar; + public bool ShutdownBar; + + public bool BootLid; + public bool AwakeLid; + public bool SleepLid; + public bool ShutdownLid; + + public bool BootRear; + public bool AwakeRear; + public bool SleepRear; + public bool ShutdownRear; + } + + public enum AuraMode : int + { + AuraStatic = 0, + AuraBreathe = 1, + AuraColorCycle = 2, + AuraRainbow = 3, + Star = 4, + Rain = 5, + Highlight = 6, + Laser = 7, + Ripple = 8, + AuraStrobe = 10, + Comet = 11, + Flash = 12, + HEATMAP = 20, + GPUMODE = 21, + } + + public enum AuraSpeed : int + { + Slow = 0, + Normal = 1, + Fast = 2, + } + + + public static class Aura + { + + static byte[] MESSAGE_APPLY = { AsusHid.AURA_ID, 0xb4 }; + static byte[] MESSAGE_SET = { AsusHid.AURA_ID, 0xb5, 0, 0, 0 }; + + private static AuraMode mode = AuraMode.AuraStatic; + private static AuraSpeed speed = AuraSpeed.Normal; + + public static Color Color1 = Color.White; + public static Color Color2 = Color.Black; + + static bool isACPI = AppConfig.IsTUF() || AppConfig.IsVivobook(); + static bool isStrix = AppConfig.IsStrix(); + + static public bool isSingleColor = false; + + static bool isOldHeatmap = AppConfig.Is("old_heatmap"); + + static System.Timers.Timer timer = new System.Timers.Timer(1000); + + private static Dictionary _modesSingleColor = new Dictionary + { + { AuraMode.AuraStatic, Properties.Strings.AuraStatic }, + { AuraMode.AuraBreathe, Properties.Strings.AuraBreathe }, + { AuraMode.AuraStrobe, Properties.Strings.AuraStrobe }, + }; + + private static Dictionary _modes = new Dictionary + { + { AuraMode.AuraStatic, Properties.Strings.AuraStatic }, + { AuraMode.AuraBreathe, Properties.Strings.AuraBreathe }, + { AuraMode.AuraColorCycle, Properties.Strings.AuraColorCycle }, + { AuraMode.AuraRainbow, Properties.Strings.AuraRainbow }, + { AuraMode.AuraStrobe, Properties.Strings.AuraStrobe }, + { AuraMode.HEATMAP, "Heatmap"}, + { AuraMode.GPUMODE, "GPU Mode" } + }; + + private static Dictionary _modesStrix = new Dictionary + { + { AuraMode.AuraStatic, Properties.Strings.AuraStatic }, + { AuraMode.AuraBreathe, Properties.Strings.AuraBreathe }, + { AuraMode.AuraColorCycle, Properties.Strings.AuraColorCycle }, + { AuraMode.AuraRainbow, Properties.Strings.AuraRainbow }, + { AuraMode.Star, "Star" }, + { AuraMode.Rain, "Rain" }, + { AuraMode.Highlight, "Highlight" }, + { AuraMode.Laser, "Laser" }, + { AuraMode.Ripple, "Ripple" }, + { AuraMode.AuraStrobe, Properties.Strings.AuraStrobe}, + { AuraMode.Comet, "Comet" }, + { AuraMode.Flash, "Flash" }, + { AuraMode.HEATMAP, "Heatmap"}, + }; + + static Aura() + { + timer.Elapsed += Timer_Elapsed; + isSingleColor = AppConfig.IsSingleColor(); // Mono Color + + if (AppConfig.ContainsModel("GA402X") || AppConfig.ContainsModel("GA402N")) + using (var stream = AsusHid.FindHidStream(AsusHid.AURA_ID)) + { + if (stream is null) return; + if (stream.Device.ReleaseNumberBcd == 22 || stream.Device.ReleaseNumberBcd == 23) isSingleColor = true; + stream.Close(); + } + } + + private static void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) + { + SetHeatmap(); + } + + static void SetHeatmap(bool init = false) + { + float cpuTemp = (float)HardwareControl.GetCPUTemp(); + int freeze = 20, cold = 40, warm = 65, hot = 90; + Color color; + + //Debug.WriteLine(cpuTemp); + + if (cpuTemp < cold) color = ColorUtilities.GetWeightedAverage(Color.Blue, Color.Green, ((float)cpuTemp - freeze) / (cold - freeze)); + else if (cpuTemp < warm) color = ColorUtilities.GetWeightedAverage(Color.Green, Color.Yellow, ((float)cpuTemp - cold) / (warm - cold)); + else if (cpuTemp < hot) color = ColorUtilities.GetWeightedAverage(Color.Yellow, Color.Red, ((float)cpuTemp - warm) / (hot - warm)); + else color = Color.Red; + + ApplyColor(color, init); + } + + public static Dictionary GetSpeeds() + { + return new Dictionary + { + { AuraSpeed.Slow, Properties.Strings.AuraSlow }, + { AuraSpeed.Normal, Properties.Strings.AuraNormal }, + { AuraSpeed.Fast, Properties.Strings.AuraFast } + }; + } + + + public static Dictionary GetModes() + { + if (isACPI) + { + _modes.Remove(AuraMode.AuraRainbow); + } + + if (isSingleColor) + { + return _modesSingleColor; + } + + if (AppConfig.IsAdvantageEdition()) + { + return _modes; + } + + if (AppConfig.IsStrix() && !AppConfig.IsStrixLimitedRGB()) + { + return _modesStrix; + } + + return _modes; + } + + public static AuraMode Mode + { + get { return mode; } + set + { + mode = GetModes().ContainsKey(value) ? value : AuraMode.AuraStatic; + } + } + + public static AuraSpeed Speed + { + get { return speed; } + set + { + speed = GetSpeeds().ContainsKey(value) ? value : AuraSpeed.Normal; + } + + } + + public static void SetColor(int colorCode) + { + Color1 = Color.FromArgb(colorCode); + } + + public static void SetColor2(int colorCode) + { + Color2 = Color.FromArgb(colorCode); + } + + public static bool HasSecondColor() + { + return mode == AuraMode.AuraBreathe && !isACPI; + } + + public static byte[] AuraMessage(AuraMode mode, Color color, Color color2, int speed, bool mono = false) + { + + byte[] msg = new byte[17]; + msg[0] = AsusHid.AURA_ID; + msg[1] = 0xb3; + msg[2] = 0x00; // Zone + msg[3] = (byte)mode; // Aura Mode + msg[4] = color.R; // R + msg[5] = mono ? (byte)0 : color.G; // G + 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 == AuraMode.AuraBreathe ? (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 + return msg; + } + + public static void Init() + { + Task.Run(async () => + { + AsusHid.Write(new List { + new byte[] { AsusHid.AURA_ID, 0xb9 }, + Encoding.ASCII.GetBytes("]ASUS Tech.Inc."), + new byte[] { AsusHid.AURA_ID, 0x05, 0x20, 0x31, 0, 0x1a }, + Encoding.ASCII.GetBytes("^ASUS Tech.Inc."), + new byte[] { 0x5e, 0x05, 0x20, 0x31, 0, 0x1a } + }); + }); + } + + + public static void ApplyBrightness(int brightness, string log = "Backlight", bool delay = false) + { + Task.Run(async () => + { + if (delay) await Task.Delay(TimeSpan.FromSeconds(1)); + if (isACPI) Program.acpi.TUFKeyboardBrightness(brightness); + + AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.AURA_ID, log); + if (AppConfig.ContainsModel("GA503")) + AsusHid.Write(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.INPUT_ID, log); + }); + + + } + + static byte[] AuraPowerMessage(AuraPower flags) + { + byte keyb = 0, bar = 0, lid = 0, rear = 0; + + if (flags.BootLogo) keyb |= 1 << 0; + if (flags.BootKeyb) keyb |= 1 << 1; + if (flags.AwakeLogo) keyb |= 1 << 2; + if (flags.AwakeKeyb) keyb |= 1 << 3; + if (flags.SleepLogo) keyb |= 1 << 4; + if (flags.SleepKeyb) keyb |= 1 << 5; + if (flags.ShutdownLogo) keyb |= 1 << 6; + if (flags.ShutdownKeyb) keyb |= 1 << 7; + + if (flags.BootBar) bar |= 1 << 1; + if (flags.AwakeBar) bar |= 1 << 2; + if (flags.SleepBar) bar |= 1 << 3; + if (flags.ShutdownBar) bar |= 1 << 4; + + if (flags.BootLid) lid |= 1 << 0; + if (flags.AwakeLid) lid |= 1 << 1; + if (flags.SleepLid) lid |= 1 << 2; + if (flags.ShutdownLid) lid |= 1 << 3; + + if (flags.BootLid) lid |= 1 << 4; + if (flags.AwakeLid) lid |= 1 << 5; + if (flags.SleepLid) lid |= 1 << 6; + if (flags.ShutdownLid) lid |= 1 << 7; + + if (flags.BootRear) rear |= 1 << 0; + if (flags.AwakeRear) rear |= 1 << 1; + if (flags.SleepRear) rear |= 1 << 2; + if (flags.ShutdownRear) rear |= 1 << 3; + + if (flags.BootRear) rear |= 1 << 4; + if (flags.AwakeRear) rear |= 1 << 5; + if (flags.SleepRear) rear |= 1 << 6; + if (flags.ShutdownRear) rear |= 1 << 7; + + return new byte[] { 0x5d, 0xbd, 0x01, keyb, bar, lid, rear, 0xFF }; + } + + public static void ApplyPower() + { + + AuraPower flags = new(); + + // Keyboard + flags.AwakeKeyb = AppConfig.IsNotFalse("keyboard_awake"); + flags.BootKeyb = AppConfig.IsNotFalse("keyboard_boot"); + flags.SleepKeyb = AppConfig.IsNotFalse("keyboard_sleep"); + flags.ShutdownKeyb = AppConfig.IsNotFalse("keyboard_shutdown"); + + // Logo + flags.AwakeLogo = AppConfig.IsNotFalse("keyboard_awake_logo"); + flags.BootLogo = AppConfig.IsNotFalse("keyboard_boot_logo"); + flags.SleepLogo = AppConfig.IsNotFalse("keyboard_sleep_logo"); + flags.ShutdownLogo = AppConfig.IsNotFalse("keyboard_shutdown_logo"); + + // Lightbar + flags.AwakeBar = AppConfig.IsNotFalse("keyboard_awake_bar"); + flags.BootBar = AppConfig.IsNotFalse("keyboard_boot_bar"); + flags.SleepBar = AppConfig.IsNotFalse("keyboard_sleep_bar"); + flags.ShutdownBar = AppConfig.IsNotFalse("keyboard_shutdown_bar"); + + // Lid + flags.AwakeLid = AppConfig.IsNotFalse("keyboard_awake_lid"); + flags.BootLid = AppConfig.IsNotFalse("keyboard_boot_lid"); + flags.SleepLid = AppConfig.IsNotFalse("keyboard_sleep_lid"); + flags.ShutdownLid = AppConfig.IsNotFalse("keyboard_shutdown_lid"); + + // Rear Bar + flags.AwakeRear = AppConfig.IsNotFalse("keyboard_awake_lid"); + flags.BootRear = AppConfig.IsNotFalse("keyboard_boot_lid"); + flags.SleepRear = AppConfig.IsNotFalse("keyboard_sleep_lid"); + flags.ShutdownRear = AppConfig.IsNotFalse("keyboard_shutdown_lid"); + + AsusHid.Write(AuraPowerMessage(flags)); + + if (isACPI) + Program.acpi.TUFKeyboardPower( + flags.AwakeKeyb, + flags.BootKeyb, + flags.SleepKeyb, + flags.ShutdownKeyb); + + } + + public static void ApplyColor(Color color, bool init = false) + { + + if (isACPI) + { + Program.acpi.TUFKeyboardRGB(0, color, 0, null); + return; + } + + if (isStrix && !isOldHeatmap) + { + byte[] msg = new byte[0x40]; + + byte start = 9; + byte maxLeds = 0x93; + + msg[0] = AsusHid.AURA_ID; + msg[1] = 0xbc; + msg[2] = 0; + msg[3] = 1; + msg[4] = 1; + msg[5] = 1; + msg[6] = 0; + msg[7] = 0x10; + + for (byte i = 0; i < 0x12; i++) + { + msg[start + i * 3] = color.R; // R + msg[start + 1 + i * 3] = color.G; // G + msg[start + 2 + i * 3] = color.B; // B + } + + if (init) + { + Init(); + AsusHid.WriteAura(new byte[] { AsusHid.AURA_ID, 0xbc }); + } + + for (byte b = 0; b < maxLeds; b += 0x10) + { + msg[6] = b; + AsusHid.WriteAura(msg); + } + + msg[6] = maxLeds; + AsusHid.WriteAura(msg); + + msg[4] = 4; + msg[5] = 0; + msg[6] = 0; + msg[7] = 0; + AsusHid.WriteAura(msg); + } + + else + { + AsusHid.WriteAura(AuraMessage(0, color, color, 0)); + AsusHid.WriteAura(MESSAGE_SET); + } + + } + + + public static void ApplyGPUColor() + { + if ((AuraMode)AppConfig.Get("aura_mode") != AuraMode.GPUMODE) return; + + switch (GPUModeControl.gpuMode) + { + case AsusACPI.GPUModeUltimate: + ApplyColor(Color.Red, true); + break; + case AsusACPI.GPUModeEco: + ApplyColor(Color.Green, true); + break; + default: + ApplyColor(Color.Yellow, true); + break; + } + } + + + public static void ApplyAura() + { + + Mode = (AuraMode)AppConfig.Get("aura_mode"); + Speed = (AuraSpeed)AppConfig.Get("aura_speed"); + SetColor(AppConfig.Get("aura_color")); + SetColor2(AppConfig.Get("aura_color2")); + + timer.Enabled = false; + + if (Mode == AuraMode.HEATMAP) + { + SetHeatmap(true); + timer.Enabled = true; + return; + } + + if (Mode == AuraMode.GPUMODE) + { + ApplyGPUColor(); + return; + } + + int _speed = (Speed == AuraSpeed.Normal) ? 0xeb : (Speed == AuraSpeed.Fast) ? 0xf5 : 0xe1; + + AsusHid.Write(new List { AuraMessage(Mode, Color1, Color2, _speed, isSingleColor), MESSAGE_APPLY, MESSAGE_SET }); + + if (isACPI) + Program.acpi.TUFKeyboardRGB(Mode, Color1, _speed); + + } + + + } + +} \ 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); + } + } +}