diff --git a/app/ASUSWmi.cs b/app/ASUSWmi.cs index 6d979da8..4a118df0 100644 --- a/app/ASUSWmi.cs +++ b/app/ASUSWmi.cs @@ -1,6 +1,4 @@ -using GHelper; -using System.Diagnostics; -using System.Management; +using System.Management; using System.Runtime.InteropServices; public class ASUSWmi @@ -12,6 +10,10 @@ public class ASUSWmi const uint DSTS = 0x53545344; const uint DEVS = 0x53564544; + public const uint UniversalControl = 0x00100021; + public const int KB_Light_Up = 0xc4; + public const int KB_Light_Down = 0xc5; + public const uint CPU_Fan = 0x00110013; public const uint GPU_Fan = 0x00110014; public const uint Mid_Fan = 0x00110031; @@ -231,7 +233,7 @@ public class ASUSWmi return DeviceGetBuffer(DevsGPUFanCurve, fan_mode); case 2: return DeviceGetBuffer(DevsMidFanCurve, fan_mode); - default: + default: return DeviceGetBuffer(DevsCPUFanCurve, fan_mode); } diff --git a/app/AnimeMatrix/AnimeMatrixDevice.cs b/app/AnimeMatrix/AnimeMatrixDevice.cs index 5cb076a1..2fca7539 100644 --- a/app/AnimeMatrix/AnimeMatrixDevice.cs +++ b/app/AnimeMatrix/AnimeMatrixDevice.cs @@ -359,7 +359,7 @@ namespace Starlight.AnimeMatrix - public void PresentText(string text, float fontSize = 8F) + public void PresentText(string text, float fontSize = 8.5F) { using (Bitmap bmp = new Bitmap(MaxColumns * 3, MaxRows)) { @@ -378,21 +378,22 @@ namespace Starlight.AnimeMatrix g.DrawString(text, font, Brushes.White, -textSize.Width/2, -textSize.Height / 2); */ - g.DrawString(text, font, Brushes.White, 12, -2); + g.DrawString(text, font, Brushes.White, 5, -2); } } - GenerateFrame(bmp); + GenerateFrame(bmp, System.Drawing.Drawing2D.InterpolationMode.Bicubic); Present(); } } - public void GenerateFrame(Image image) + public void GenerateFrame(Image image, System.Drawing.Drawing2D.InterpolationMode interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic) { int width = MaxColumns * 3; int height = MaxRows; + int targetWidth = MaxColumns * 2; float scale; @@ -403,14 +404,14 @@ namespace Starlight.AnimeMatrix using (var graph = Graphics.FromImage(bmp)) { - var scaleWidth = (int)(image.Width * scale); - var scaleHeight = (int)(image.Height * scale); + var scaleWidth = (float)(image.Width * scale); + var scaleHeight = (float)(image.Height * scale); - graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; + graph.InterpolationMode = interpolation; graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; - graph.DrawImage(image, (int)Math.Round(targetWidth - scaleWidth/1.5), 0, (int)Math.Round(scaleWidth/1.5), scaleHeight); + graph.DrawImage(image, (float)Math.Round(targetWidth - scaleWidth * targetWidth / width), 0, (float)Math.Round(scaleWidth * targetWidth / width), scaleHeight); } @@ -420,12 +421,12 @@ namespace Starlight.AnimeMatrix if (x % 2 == y % 2) { var pixel = bmp.GetPixel(x, y); - SetLedPlanar(x / 2, y, (byte)((pixel.R + pixel.G + pixel.B) / 3)); + var color = (pixel.R + pixel.G + pixel.B) / 3; + if (color < 10) color = 0; + SetLedPlanar(x / 2, y, (byte)color); } } } - - } private void EnsureRowInRange(int row) diff --git a/app/Aura.cs b/app/Aura.cs index 816b41de..6ffb29ad 100644 --- a/app/Aura.cs +++ b/app/Aura.cs @@ -1,7 +1,5 @@ using HidLibrary; -using Microsoft.VisualBasic.ApplicationServices; using System.Diagnostics; -using static Starlight.AnimeMatrix.BuiltInAnimation; namespace GHelper { @@ -36,7 +34,7 @@ namespace GHelper { a |= (uint)n; } - return new byte[] {0x5d, 0xbd, 0x01, (byte)(a & 0xff), (byte)((a & 0xff00) >> 8), (byte)((a & 0xff0000) >> 16) }; + return new byte[] { 0x5d, 0xbd, 0x01, (byte)(a & 0xff), (byte)((a & 0xff00) >> 8), (byte)((a & 0xff0000) >> 16) }; } public static ushort BitOr(this AuraDev19b6 self, AuraDev19b6 rhs) @@ -56,6 +54,7 @@ namespace GHelper static byte[] MESSAGE_SET = { 0x5d, 0xb5, 0, 0, 0 }; static byte[] MESSAGE_APPLY = { 0x5d, 0xb4 }; + static int[] deviceIds = { 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0 }; private static int mode = 0; private static int speed = 1; @@ -92,7 +91,7 @@ namespace GHelper { if (GetModes().ContainsKey(value)) mode = value; - else + else mode = 0; } } @@ -145,6 +144,22 @@ namespace GHelper } + public static void ApplyBrightness(int brightness) + { + HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); + + byte[] msg = { 0x5a, 0xba, 0xc5, 0xc4, (byte)brightness }; + + foreach (HidDevice device in HidDeviceList) + if (device.IsConnected && device.Description.Contains("HID")) + { + device.OpenDevice(); + device.Write(msg); + device.CloseDevice(); + } + + } + public static void ApplyAuraPower(bool awake = true, bool boot = false, bool sleep = false, bool shutdown = false) { @@ -158,7 +173,7 @@ namespace GHelper if (shutdown) flags.Add(AuraDev19b6.ShutdownKeyb); byte[] msg = AuraDev19b6Extensions.ToBytes(flags.ToArray()); - + Debug.WriteLine(BitConverter.ToString(msg)); foreach (HidDevice device in HidDeviceList) @@ -176,10 +191,7 @@ namespace GHelper public static void ApplyAura() { - HidDevice[] HidDeviceList; - int[] deviceIds = { 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0 }; - - HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); + HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray(); int _speed; diff --git a/app/Keyboard.Designer.cs b/app/Keyboard.Designer.cs index 9bc3d0fe..e48598ff 100644 --- a/app/Keyboard.Designer.cs +++ b/app/Keyboard.Designer.cs @@ -41,14 +41,18 @@ namespace GHelper comboM3 = new RComboBox(); labelM3 = new Label(); groupLight = new GroupBox(); - checkAwake = new CheckBox(); - checkBoot = new CheckBox(); - checkSleep = new CheckBox(); - checkShutdown = new CheckBox(); - comboKeyboardSpeed = new RComboBox(); labelSpeed = new Label(); + comboKeyboardSpeed = new RComboBox(); + checkShutdown = new CheckBox(); + checkSleep = new CheckBox(); + checkBoot = new CheckBox(); + checkAwake = new CheckBox(); + groupOther = new GroupBox(); + checkKeyboardAuto = new CheckBox(); + checkTopmost = new CheckBox(); groupBox1.SuspendLayout(); groupLight.SuspendLayout(); + groupOther.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -65,7 +69,7 @@ namespace GHelper groupBox1.Dock = DockStyle.Top; groupBox1.Location = new Point(10, 10); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(751, 242); + groupBox1.Size = new Size(756, 242); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Key Bindings"; @@ -158,50 +162,19 @@ namespace GHelper groupLight.Dock = DockStyle.Top; groupLight.Location = new Point(10, 252); groupLight.Name = "groupLight"; - groupLight.Size = new Size(751, 304); + groupLight.Size = new Size(756, 304); groupLight.TabIndex = 1; groupLight.TabStop = false; groupLight.Text = "Keyboard Backlight"; // - // checkAwake + // labelSpeed // - checkAwake.AutoSize = true; - checkAwake.Location = new Point(25, 59); - checkAwake.Name = "checkAwake"; - checkAwake.Size = new Size(115, 36); - checkAwake.TabIndex = 0; - checkAwake.Text = "Awake"; - checkAwake.UseVisualStyleBackColor = true; - // - // checkBoot - // - checkBoot.AutoSize = true; - checkBoot.Location = new Point(25, 101); - checkBoot.Name = "checkBoot"; - checkBoot.Size = new Size(96, 36); - checkBoot.TabIndex = 1; - checkBoot.Text = "Boot"; - checkBoot.UseVisualStyleBackColor = true; - // - // checkSleep - // - checkSleep.AutoSize = true; - checkSleep.Location = new Point(25, 143); - checkSleep.Name = "checkSleep"; - checkSleep.Size = new Size(105, 36); - checkSleep.TabIndex = 2; - checkSleep.Text = "Sleep"; - checkSleep.UseVisualStyleBackColor = true; - // - // checkShutdown - // - checkShutdown.AutoSize = true; - checkShutdown.Location = new Point(25, 185); - checkShutdown.Name = "checkShutdown"; - checkShutdown.Size = new Size(154, 36); - checkShutdown.TabIndex = 3; - checkShutdown.Text = "Shutdown"; - checkShutdown.UseVisualStyleBackColor = true; + labelSpeed.AutoSize = true; + labelSpeed.Location = new Point(25, 237); + labelSpeed.Name = "labelSpeed"; + labelSpeed.Size = new Size(198, 32); + labelSpeed.TabIndex = 40; + labelSpeed.Text = "Animation Speed"; // // comboKeyboardSpeed // @@ -219,20 +192,84 @@ namespace GHelper comboKeyboardSpeed.TabIndex = 39; comboKeyboardSpeed.TabStop = false; // - // labelSpeed + // checkShutdown // - labelSpeed.AutoSize = true; - labelSpeed.Location = new Point(25, 237); - labelSpeed.Name = "labelSpeed"; - labelSpeed.Size = new Size(198, 32); - labelSpeed.TabIndex = 40; - labelSpeed.Text = "Animation Speed"; + checkShutdown.AutoSize = true; + checkShutdown.Location = new Point(25, 185); + checkShutdown.Name = "checkShutdown"; + checkShutdown.Size = new Size(154, 36); + checkShutdown.TabIndex = 3; + checkShutdown.Text = "Shutdown"; + checkShutdown.UseVisualStyleBackColor = true; + // + // checkSleep + // + checkSleep.AutoSize = true; + checkSleep.Location = new Point(25, 143); + checkSleep.Name = "checkSleep"; + checkSleep.Size = new Size(105, 36); + checkSleep.TabIndex = 2; + checkSleep.Text = "Sleep"; + checkSleep.UseVisualStyleBackColor = true; + // + // checkBoot + // + checkBoot.AutoSize = true; + checkBoot.Location = new Point(25, 101); + checkBoot.Name = "checkBoot"; + checkBoot.Size = new Size(96, 36); + checkBoot.TabIndex = 1; + checkBoot.Text = "Boot"; + checkBoot.UseVisualStyleBackColor = true; + // + // checkAwake + // + checkAwake.AutoSize = true; + checkAwake.Location = new Point(25, 59); + checkAwake.Name = "checkAwake"; + checkAwake.Size = new Size(115, 36); + checkAwake.TabIndex = 0; + checkAwake.Text = "Awake"; + checkAwake.UseVisualStyleBackColor = true; + // + // groupOther + // + groupOther.Controls.Add(checkKeyboardAuto); + groupOther.Controls.Add(checkTopmost); + groupOther.Dock = DockStyle.Top; + groupOther.Location = new Point(10, 556); + groupOther.Name = "groupOther"; + groupOther.Size = new Size(756, 167); + groupOther.TabIndex = 2; + groupOther.TabStop = false; + groupOther.Text = "Other"; + // + // checkKeyboardAuto + // + checkKeyboardAuto.AutoSize = true; + checkKeyboardAuto.Location = new Point(25, 51); + checkKeyboardAuto.Name = "checkKeyboardAuto"; + checkKeyboardAuto.Size = new Size(712, 36); + checkKeyboardAuto.TabIndex = 2; + checkKeyboardAuto.Text = "Lower backlight brightness on battery and back when plugged"; + checkKeyboardAuto.UseVisualStyleBackColor = true; + // + // checkTopmost + // + checkTopmost.AutoSize = true; + checkTopmost.Location = new Point(25, 104); + checkTopmost.Name = "checkTopmost"; + checkTopmost.Size = new Size(390, 36); + checkTopmost.TabIndex = 1; + checkTopmost.Text = "Keep app window always on top"; + checkTopmost.UseVisualStyleBackColor = true; // // Keyboard // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(771, 858); + ClientSize = new Size(776, 858); + Controls.Add(groupOther); Controls.Add(groupLight); Controls.Add(groupBox1); FormBorderStyle = FormBorderStyle.FixedSingle; @@ -243,11 +280,13 @@ namespace GHelper Padding = new Padding(10); ShowIcon = false; ShowInTaskbar = false; - Text = "Keyboard"; + Text = "Extra Settings"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupLight.ResumeLayout(false); groupLight.PerformLayout(); + groupOther.ResumeLayout(false); + groupOther.PerformLayout(); ResumeLayout(false); } @@ -270,5 +309,8 @@ namespace GHelper private CheckBox checkShutdown; private Label labelSpeed; private RComboBox comboKeyboardSpeed; + private GroupBox groupOther; + private CheckBox checkTopmost; + private CheckBox checkKeyboardAuto; } } \ No newline at end of file diff --git a/app/Keyboard.cs b/app/Keyboard.cs index b4f44510..e8060e6a 100644 --- a/app/Keyboard.cs +++ b/app/Keyboard.cs @@ -78,8 +78,25 @@ namespace GHelper checkSleep.CheckedChanged += CheckPower_CheckedChanged; checkShutdown.CheckedChanged += CheckPower_CheckedChanged; + checkTopmost.Checked = (Program.config.getConfig("topmost") == 1); + checkTopmost.CheckedChanged += CheckTopmost_CheckedChanged; ; + + checkKeyboardAuto.Checked = (Program.config.getConfig("keyboard_auto") == 1); + checkKeyboardAuto.CheckedChanged += CheckKeyboardAuto_CheckedChanged; + + } + private void CheckKeyboardAuto_CheckedChanged(object? sender, EventArgs e) + { + Program.config.setConfig("keyboard_auto", (checkKeyboardAuto.Checked ? 1 : 0)); + } + + private void CheckTopmost_CheckedChanged(object? sender, EventArgs e) + { + Program.config.setConfig("topmost", (checkTopmost.Checked ? 1 : 0)); + Program.settingsForm.TopMost = checkTopmost.Checked; + } private void CheckPower_CheckedChanged(object? sender, EventArgs e) { diff --git a/app/NativeMethods.cs b/app/NativeMethods.cs index 01c2c9cb..bc860d73 100644 --- a/app/NativeMethods.cs +++ b/app/NativeMethods.cs @@ -588,7 +588,7 @@ public class NativeMethods return laptopScreen; } - public static int GetRefreshRate() + public static int GetRefreshRate(bool max = false) { DEVMODE dm = CreateDevmode(); @@ -598,11 +598,23 @@ public class NativeMethods if (laptopScreen is null) return -1; - if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) + if (max) { - frequency = dm.dmDisplayFrequency; + int i = 0; + while (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen, i, ref dm)) + { + if (dm.dmDisplayFrequency > frequency) frequency = dm.dmDisplayFrequency; + i++; + } + } else + { + if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm)) + { + frequency = dm.dmDisplayFrequency; + } } + return frequency; } diff --git a/app/Program.cs b/app/Program.cs index 14a9b99b..37709b48 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -148,6 +148,7 @@ namespace GHelper if (SystemInformation.PowerStatus.PowerLineStatus == isPlugged) return; Logger.WriteLine("Windows - Power Mode Changed"); + settingsForm.AutoKeyboard(SystemInformation.PowerStatus.PowerLineStatus); SetAutoModes(true); } diff --git a/app/Settings.cs b/app/Settings.cs index 50ebff98..d028a84f 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -2,6 +2,7 @@ using Starlight.AnimeMatrix; using System.Diagnostics; using System.Drawing.Imaging; +using System.Globalization; using System.Reflection; using System.Text.Json; using System.Timers; @@ -133,6 +134,8 @@ namespace GHelper labelModel.Text = model; + this.TopMost = Program.config.getConfig("topmost") == 1; + Task.Run(async () => { await Task.Delay(TimeSpan.FromSeconds(5)); @@ -687,8 +690,7 @@ namespace GHelper if (frequency >= 1000) { - frequency = Program.config.getConfig("max_frequency"); - if (frequency <= 60) frequency = 120; + frequency = NativeMethods.GetRefreshRate(true); } if (frequency > 0) @@ -714,7 +716,7 @@ namespace GHelper { int frequency = NativeMethods.GetRefreshRate(); - int maxFrequency = Program.config.getConfig("max_frequency"); + int maxFrequency = NativeMethods.GetRefreshRate(true); bool screenAuto = (Program.config.getConfig("screen_auto") == 1); @@ -744,12 +746,8 @@ namespace GHelper { button60Hz.Activated = true; } - else + else if (frequency > 60) { - if (frequency > 60) - maxFrequency = frequency; - - Program.config.setConfig("max_frequency", maxFrequency); button120Hz.Activated = true; } @@ -999,6 +997,19 @@ namespace GHelper SetPerformanceMode(Program.config.getConfig("performance_mode") + 1, true); } + + public void AutoKeyboard(PowerLineStatus Plugged = PowerLineStatus.Online) + { + if (Program.config.getConfig("keyboard_auto") != 1) return; + + if (Plugged == PowerLineStatus.Online) + Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Up); + else + Program.wmi.DeviceSet(ASUSWmi.UniversalControl, ASUSWmi.KB_Light_Down); + + + } + public void AutoPerformance(PowerLineStatus Plugged = PowerLineStatus.Online) { int mode = Program.config.getConfig("performance_" + (int)Plugged);