From 6adb2e2fcf31290b601b20d9bb5052928b6e1124 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sat, 20 May 2023 20:50:58 +0200 Subject: [PATCH] Keyboard Reading --- app/AsusUSB.cs | 50 ++++++++++++++++++++++++- app/OSDBase.cs | 2 +- app/OptimizationService.cs | 6 +++ app/Program.cs | 76 +++++++++++++++++++++++++++++++------- app/ScreenBrightness.cs | 46 +++++++++++++++++++++++ app/Settings.cs | 16 ++++---- app/ToastForm.cs | 22 ++++++----- 7 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 app/ScreenBrightness.cs diff --git a/app/AsusUSB.cs b/app/AsusUSB.cs index 1fae4e8a..dab7bcee 100644 --- a/app/AsusUSB.cs +++ b/app/AsusUSB.cs @@ -52,6 +52,8 @@ namespace GHelper public static class AsusUSB { + public const byte HID_ID = 0x5a; + static byte[] MESSAGE_SET = { 0x5d, 0xb5, 0, 0, 0 }; static byte[] MESSAGE_APPLY = { 0x5d, 0xb4 }; @@ -172,6 +174,51 @@ namespace GHelper yield return device; } + private static HidDevice? GetInputDevice() + { + HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); + + foreach (HidDevice device in HidDeviceList) + if (device.ReadFeatureData(out byte[] data, HID_ID)) + return device; + return null; + } + + public static void TouchpadToggle() + { + HidDevice? input = GetInputDevice(); + if (input != null) input.WriteFeatureData(new byte[] { HID_ID,0xf4,0x6b}); + } + + public static void RunListener(Action KeyHandler) + { + HidDevice? input = GetInputDevice(); + if (input == null) return; + + Logger.WriteLine("Input Events " + input.DevicePath); + + 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) { @@ -300,10 +347,11 @@ namespace GHelper byte[] msg = AuraMessage(Mode, Color1, Color2, _speed); var devices = GetHidDevices(deviceIds); + if (devices.Count() == 0) { Logger.WriteLine("USB-KB : not found"); - GetHidDevices(deviceIds, 0); + devices = GetHidDevices(deviceIds, 1); } foreach (HidDevice device in devices) diff --git a/app/OSDBase.cs b/app/OSDBase.cs index f1895ee1..45f99142 100644 --- a/app/OSDBase.cs +++ b/app/OSDBase.cs @@ -4,7 +4,7 @@ using System.Runtime.InteropServices; namespace OSD { - class OSDNativeForm : NativeWindow, IDisposable + public class OSDNativeForm : NativeWindow, IDisposable { private bool _disposed = false; diff --git a/app/OptimizationService.cs b/app/OptimizationService.cs index 04b0ce2b..6f5973ac 100644 --- a/app/OptimizationService.cs +++ b/app/OptimizationService.cs @@ -1,5 +1,6 @@ using System.Text.RegularExpressions; using System.Text; +using System.Diagnostics; namespace GHelper { @@ -33,6 +34,11 @@ namespace GHelper File.WriteAllText(path, fileContents, Encoding.Unicode); } } + + public static bool IsRunning() + { + return (Process.GetProcessesByName("AsusOptimization").Count() > 0); + } } } \ No newline at end of file diff --git a/app/Program.cs b/app/Program.cs index 5645cc85..69673564 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -1,4 +1,6 @@ using Microsoft.Win32; +using NAudio.CoreAudioApi; +using OSD; using System.Diagnostics; using System.Globalization; using System.Management; @@ -22,7 +24,6 @@ namespace GHelper public static AppConfig config = new AppConfig(); public static SettingsForm settingsForm = new SettingsForm(); - public static ToastForm toast = new ToastForm(); public static IntPtr unRegPowerNotify; @@ -30,6 +31,8 @@ namespace GHelper private static long lastTheme; private static long lastAdmin; + private static bool isOptimizationRunning = OptimizationService.IsRunning(); + private static PowerLineStatus isPlugged = PowerLineStatus.Unknown; // The main entry point for the application @@ -103,7 +106,7 @@ namespace GHelper SettingsToggle(action); } - //Task.Run(wmi.RunListener); + if (!isOptimizationRunning) AsusUSB.RunListener(HandleEvent); Application.Run(); @@ -220,6 +223,8 @@ namespace GHelper action = "ghelper"; if (name == "fnf4") action = "aura"; + if (name == "m3" && !isOptimizationRunning) + action = "micmute"; } switch (action) @@ -254,6 +259,16 @@ namespace GHelper case "custom": CustomKey(name); break; + case "micmute": + using (var enumerator = new MMDeviceEnumerator()) + { + var commDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications); + bool muteStatus = !commDevice.AudioEndpointVolume.Mute; + commDevice.AudioEndpointVolume.Mute = muteStatus; + settingsForm.BeginInvoke(settingsForm.RunToast, muteStatus ? "Mic Muted" : "Mic Unmuted"); + } + break; + default: break; } @@ -272,21 +287,12 @@ namespace GHelper Logger.WriteLine("Tablet: " + tabletState + " Touchpad: " + touchpadState); - if ((tabletState && touchpadState) || (!tabletState && !touchpadState)) - acpi.DeviceSet(AsusACPI.UniversalControl, AsusACPI.Touchpad_Toggle, "Touchpad"); + if ((tabletState && touchpadState) || (!tabletState && !touchpadState)) AsusUSB.TouchpadToggle(); } - static void WatcherEventArrived(object sender, EventArrivedEventArgs e) + static void HandleEvent(int EventID) { - var collection = (ManagementEventWatcher)sender; - - if (e.NewEvent is null) return; - - int EventID = int.Parse(e.NewEvent["EventID"].ToString()); - - Logger.WriteLine("WMI event " + EventID); - switch (EventID) { case 124: // M3 @@ -304,10 +310,52 @@ namespace GHelper case 189: // Tablet mode TabletMode(); return; - } + if (isOptimizationRunning) return; + // Asus Optimization service Events + + int brightness = config.getConfig("keyboard_brightness"); + + switch (EventID) + { + case 197: // FN+F2 + brightness = Math.Max(0, brightness - 1); + config.setConfig("keyboard_brightness", brightness); + AsusUSB.ApplyBrightness(brightness); + settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight -"); + break; + case 196: // FN+F3 + brightness = Math.Min(3, brightness + 1); + config.setConfig("keyboard_brightness", brightness); + AsusUSB.ApplyBrightness(brightness); + settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight +"); + break; + case 16: // FN+F7 + ScreenBrightness.Adjust(-10); + settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness -"); + break; + case 32: // FN+F8 + ScreenBrightness.Adjust(+10); + settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness +"); + break; + case 107: // FN+F10 + AsusUSB.TouchpadToggle(); + settingsForm.BeginInvoke(settingsForm.RunToast, "Touchpad"); + break; + case 108: // FN+F11 + Application.SetSuspendState(PowerState.Suspend, true, true); + break; + } + } + + static void WatcherEventArrived(object sender, EventArrivedEventArgs e) + { + if (e.NewEvent is null) return; + int EventID = int.Parse(e.NewEvent["EventID"].ToString()); + Logger.WriteLine("WMI event " + EventID); + HandleEvent(EventID); } static void SettingsToggle(string action = "") diff --git a/app/ScreenBrightness.cs b/app/ScreenBrightness.cs new file mode 100644 index 00000000..b96203fe --- /dev/null +++ b/app/ScreenBrightness.cs @@ -0,0 +1,46 @@ +namespace GHelper +{ + using System; + using System.Diagnostics; + using System.Management; + + public static class ScreenBrightness + { + public static int Get() + { + using var mclass = new ManagementClass("WmiMonitorBrightness") + { + Scope = new ManagementScope(@"\\.\root\wmi") + }; + using var instances = mclass.GetInstances(); + foreach (ManagementObject instance in instances) + { + return (byte)instance.GetPropertyValue("CurrentBrightness"); + } + return 0; + } + + public static void Set(int brightness) + { + using var mclass = new ManagementClass("WmiMonitorBrightnessMethods") + { + Scope = new ManagementScope(@"\\.\root\wmi") + }; + using var instances = mclass.GetInstances(); + var args = new object[] { 1, brightness }; + foreach (ManagementObject instance in instances) + { + instance.InvokeMethod("WmiSetBrightness", args); + } + } + + public static void Adjust(int delta) + { + int brightness = Get(); + Debug.WriteLine(brightness); + brightness = Math.Min(100, Math.Max(0, brightness + delta)); + Set(brightness); + } + + } +} diff --git a/app/Settings.cs b/app/Settings.cs index 813e9919..595f24bf 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -1,12 +1,7 @@ using CustomControls; using GHelper.AnimeMatrix; using GHelper.Gpu; -using NAudio.CoreAudioApi; -using NAudio.Wave; -using Starlight.AnimeMatrix; using System.Diagnostics; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.Net; using System.Reflection; using System.Text.Json; @@ -22,6 +17,8 @@ namespace GHelper private ContextMenuStrip contextMenuStrip = new CustomContextMenu(); private ToolStripMenuItem menuSilent, menuBalanced, menuTurbo, menuEco, menuStandard, menuUltimate, menuOptimized; + public ToastForm toast = new ToastForm(); + public static System.Timers.Timer aTimer = default!; public static Point trayPoint; @@ -30,7 +27,6 @@ namespace GHelper public string perfName = "Balanced"; public AniMatrix matrix; - public Fans fans; public Extra keyb; @@ -42,7 +38,6 @@ namespace GHelper public SettingsForm() { - InitializeComponent(); InitTheme(true); @@ -157,6 +152,11 @@ namespace GHelper } + public void RunToast(string text) + { + toast.RunToast(text); + } + public void SetContextMenu() { @@ -1145,7 +1145,7 @@ namespace GHelper { try { - Program.toast.RunToast(perfName); + toast.RunToast(perfName); } catch { diff --git a/app/ToastForm.cs b/app/ToastForm.cs index d3ea8367..170a013a 100644 --- a/app/ToastForm.cs +++ b/app/ToastForm.cs @@ -1,4 +1,5 @@ -using System.Drawing.Drawing2D; +using System.Diagnostics; +using System.Drawing.Drawing2D; using OSD; @@ -41,12 +42,19 @@ namespace GHelper } } - class ToastForm : OSDNativeForm + public class ToastForm : OSDNativeForm { protected static string toastText = "Balanced"; protected static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); + public ToastForm() + { + timer.Tick += timer_Tick; + timer.Enabled = false; + timer.Interval = 2000; + } + protected override void PerformPaint(PaintEventArgs e) { Brush brush = new SolidBrush(Color.FromArgb(150,Color.Black)); @@ -65,6 +73,7 @@ namespace GHelper public void RunToast(string text) { + Hide(); toastText = text; Screen screen1 = Screen.FromHandle(base.Handle); @@ -76,18 +85,13 @@ namespace GHelper Show(); - timer.Stop(); - timer.Tick -= timer_Tick; - - timer.Tick += timer_Tick; timer.Enabled = true; - timer.Interval = 2000; - timer.Start(); } private void timer_Tick(object? sender, EventArgs e) { - timer.Stop(); + Debug.WriteLine("Toast end"); + timer.Enabled = false; Hide(); } }