Repository navigation
Expand file tree
/
Copy pathVictusControl.cs
More file actions
1409 lines (1245 loc) · 46.1 KB
/
Copy pathVictusControl.cs
File metadata and controls
1409 lines (1245 loc) · 46.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// VictusControl - Lightweight Tray & Omen-Flyout Utility for HP Victus Laptops
// Direct HP BIOS WMI communication (root\wmi, hpqBIntM). Zero background services.
// Compatible with the built-in Windows C# compiler (C# 5, .NET Framework 4.0+).
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
enum EffectMode
{
Static = 0,
Breathing = 1,
Rainbow = 2
}
static class Hp
{
const string Ns = @"root\wmi";
static readonly object Sync = new object();
static ManagementScope _scope;
static ManagementObject _inst;
static ManagementClass _inCls;
static byte[] _templateData;
static void Connect()
{
if (_inst != null) return;
_scope = new ManagementScope(Ns);
_scope.Connect();
using (ManagementClass cls = new ManagementClass(_scope, new ManagementPath("hpqBIntM"), null))
{
_inCls = new ManagementClass(_scope, new ManagementPath("hpqBDataIn"), null);
foreach (ManagementObject o in cls.GetInstances()) { _inst = o; break; }
if (_inst == null) throw new Exception("hpqBIntM WMI class not found");
}
}
public static void ResetConnection()
{
lock (Sync)
{
try { if (_inst != null) _inst.Dispose(); } catch { }
try { if (_inCls != null) _inCls.Dispose(); } catch { }
_inst = null;
_inCls = null;
_scope = null;
_templateData = null;
}
}
static ManagementBaseObject InvokeInternal(uint command, uint type, byte[] data)
{
Connect();
using (ManagementObject inObj = _inCls.CreateInstance())
{
inObj["Sign"] = new byte[] { 0x53, 0x45, 0x43, 0x55 }; // "SECU"
inObj["Command"] = command;
inObj["CommandType"] = type;
inObj["Size"] = (uint)128;
inObj["hpqBData"] = data;
using (ManagementBaseObject inParams = _inst.GetMethodParameters("hpqBIOSInt128"))
{
inParams["InData"] = inObj;
using (ManagementBaseObject outParams = _inst.InvokeMethod("hpqBIOSInt128", inParams, null))
{
return (ManagementBaseObject)outParams["OutData"];
}
}
}
}
static byte[] Pad(byte[] src)
{
byte[] b = new byte[128];
if (src != null) Array.Copy(src, b, Math.Min(128, src.Length));
return b;
}
public static string SetColor(byte r, byte g, byte b)
{
lock (Sync)
{
for (int attempt = 0; attempt < 2; attempt++)
{
try
{
if (_templateData == null)
{
ManagementBaseObject o = InvokeInternal(0x20009, 2, new byte[128]);
uint rcRead = Convert.ToUInt32(o["rwReturnCode"]);
if (rcRead != 0) return "Failed to read color (Code " + rcRead + ")";
_templateData = Pad((byte[])o["Data"]);
}
_templateData[25] = r;
_templateData[26] = g;
_templateData[27] = b;
ManagementBaseObject w = InvokeInternal(0x20009, 3, _templateData);
uint rc = Convert.ToUInt32(w["rwReturnCode"]);
if (rc != 0) return "Failed to write color (Code " + rc + ")";
return null;
}
catch (Exception ex)
{
ResetConnection();
if (attempt == 1) return ex.Message;
Thread.Sleep(200);
}
}
return "WMI error";
}
}
}
static class DisplayHelper
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szPhysicalMonitorDescription;
}
public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
[StructLayout(LayoutKind.Sequential)]
public struct RECT { public int left, top, right, bottom; }
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, out uint pdwNumberOfPhysicalMonitors);
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool SetMonitorBrightness(IntPtr hMonitor, uint dwNewBrightness);
[DllImport("dxva2.dll", SetLastError = true)]
public static extern bool SetVCPFeature(IntPtr hMonitor, byte bVCPCode, uint dwNewValue);
static DimmerOverlay _overlay;
public static bool SetExternalBrightness(int brightnessPercent, out string modeUsed)
{
bool ddcWorked = false;
try
{
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, delegate(IntPtr hMon, IntPtr hdc, ref RECT rc, IntPtr data)
{
// The primary monitor always sits at (0,0). On most Victus laptops that is the
// built-in panel, which must not be changed. Heuristic: see README.
if (rc.left == 0 && rc.top == 0) return true;
uint count = 0;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMon, out count) && count > 0)
{
PHYSICAL_MONITOR[] arr = new PHYSICAL_MONITOR[count];
if (GetPhysicalMonitorsFromHMONITOR(hMon, count, arr))
{
for (int i = 0; i < arr.Length; i++)
{
IntPtr hPhys = arr[i].hPhysicalMonitor;
if (SetMonitorBrightness(hPhys, (uint)brightnessPercent))
{
ddcWorked = true;
}
else if (SetVCPFeature(hPhys, 0x10, (uint)brightnessPercent))
{
ddcWorked = true;
}
}
DestroyPhysicalMonitors(count, arr);
}
}
return true;
}, IntPtr.Zero);
}
catch { }
if (ddcWorked)
{
if (_overlay != null && _overlay.Visible) _overlay.Hide();
modeUsed = "Hardware (DDC/CI)";
return true;
}
// Fallback: Click-through software dimmer overlay
ApplySoftwareDimmer(brightnessPercent);
modeUsed = "Software Dimmer (DDC/CI not responding)";
return false;
}
static void ApplySoftwareDimmer(int brightnessPercent)
{
Screen extScreen = null;
foreach (Screen s in Screen.AllScreens)
{
if (!s.Primary) { extScreen = s; break; }
}
if (extScreen == null)
{
if (_overlay != null && _overlay.Visible) _overlay.Hide();
return;
}
if (brightnessPercent >= 100)
{
if (_overlay != null && _overlay.Visible) _overlay.Hide();
return;
}
if (_overlay == null || _overlay.IsDisposed)
{
_overlay = new DimmerOverlay();
}
_overlay.Bounds = extScreen.Bounds;
double opacity = ((100.0 - brightnessPercent) / 90.0) * 0.82;
_overlay.Opacity = Math.Max(0.04, Math.Min(0.85, opacity));
if (!_overlay.Visible)
{
_overlay.Show();
}
}
}
class DimmerOverlay : Form
{
const int WS_EX_LAYERED = 0x80000;
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_TOOLWINDOW = 0x80;
const int WS_EX_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE;
return cp;
}
}
public DimmerOverlay()
{
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
ShowInTaskbar = false;
TopMost = true;
BackColor = Color.Black;
}
protected override bool ShowWithoutActivation
{
get { return true; }
}
}
class DarkSlider : Control
{
int _value = 100;
bool _isDragging = false;
public int Value
{
get { return _value; }
set
{
int snapped = (int)(Math.Round(Math.Max(10, Math.Min(100, value)) / 10.0, MidpointRounding.AwayFromZero) * 10);
if (_value != snapped)
{
_value = snapped;
Invalidate();
if (ValueChanged != null) ValueChanged(this, EventArgs.Empty);
}
}
}
public event EventHandler ValueChanged;
public DarkSlider()
{
DoubleBuffered = true;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
Cursor = Cursors.Hand;
Height = 22;
}
void UpdateFromMouse(int mouseX)
{
int pad = 10;
int trackWidth = Width - 2 * pad;
if (trackWidth <= 0) return;
double pct = (double)(mouseX - pad) / trackWidth;
pct = Math.Max(0.0, Math.Min(1.0, pct));
int val = (int)(Math.Round((10.0 + pct * 90.0) / 10.0, MidpointRounding.AwayFromZero) * 10);
Value = val;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
_isDragging = true;
UpdateFromMouse(e.X);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_isDragging && e.Button == MouseButtons.Left)
{
UpdateFromMouse(e.X);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
_isDragging = false;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int pad = 10;
int trackWidth = Width - 2 * pad;
int cy = Height / 2;
// Background Track
using (Pen pTrack = new Pen(Color.FromArgb(48, 48, 54), 4f))
{
pTrack.StartCap = LineCap.Round;
pTrack.EndCap = LineCap.Round;
e.Graphics.DrawLine(pTrack, pad, cy, pad + trackWidth, cy);
}
double pct = (_value - 10) / 90.0;
int thumbX = pad + (int)Math.Round(pct * trackWidth);
// Filled Active Track (Accent Blue)
if (thumbX > pad)
{
using (Pen pActive = new Pen(Color.FromArgb(0, 120, 215), 4f))
{
pActive.StartCap = LineCap.Round;
pActive.EndCap = LineCap.Round;
e.Graphics.DrawLine(pActive, pad, cy, thumbX, cy);
}
}
// Thumb Circle
int thumbRadius = 7;
using (Brush brThumb = new SolidBrush(Color.White))
{
e.Graphics.FillEllipse(brThumb, thumbX - thumbRadius, cy - thumbRadius, thumbRadius * 2, thumbRadius * 2);
}
using (Pen pThumb = new Pen(Color.FromArgb(0, 120, 215), 2f))
{
e.Graphics.DrawEllipse(pThumb, thumbX - thumbRadius, cy - thumbRadius, thumbRadius * 2, thumbRadius * 2);
}
}
}
class FlyoutForm : Form
{
readonly TrayApp _app;
Button _btnPower;
Button[] _colorBtns;
Button _btnCustomColor;
Label _lblBrightVal;
DarkSlider _brightSlider;
Button[] _brightBtns;
Button[] _effectBtns;
Button[] _timeoutBtns;
Label _lblExtBrightVal;
DarkSlider _extBrightSlider;
Label _lblExtStatus;
static readonly int[] QuickBrightness = { 20, 50, 80, 100 };
static readonly Font FontToggleNormal = new Font("Segoe UI", 8.5f, FontStyle.Regular);
static readonly Font FontToggleBold = new Font("Segoe UI", 8.5f, FontStyle.Bold);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= 0x00020000; // CS_DROPSHADOW
return cp;
}
}
public FlyoutForm(TrayApp app)
{
_app = app;
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
ShowInTaskbar = false;
TopMost = true;
DoubleBuffered = true;
BackColor = Color.FromArgb(24, 24, 26);
ForeColor = Color.White;
Font = new Font("Segoe UI", 9f);
Size = new Size(360, 470);
BuildUi();
}
void BuildUi()
{
int y = 14;
// Header
Label lblTitle = new Label();
lblTitle.Text = "Victus Control";
lblTitle.Font = new Font("Segoe UI", 12f, FontStyle.Bold);
lblTitle.ForeColor = Color.White;
lblTitle.Location = new Point(16, y);
lblTitle.AutoSize = true;
Controls.Add(lblTitle);
_btnPower = CreatePillButton("● Light ON", 236, y - 2, 108, 28);
_btnPower.Click += delegate
{
_app.TogglePower();
RefreshState();
};
Controls.Add(_btnPower);
y += 40;
Controls.Add(CreateSeparator(16, y, 328));
y += 10;
// Section: COLOR
Controls.Add(CreateHeaderLabel("COLOR", 16, y));
y += 22;
int cx = 16;
_colorBtns = new Button[TrayApp.PresetColors.Length];
for (int i = 0; i < TrayApp.PresetColors.Length; i++)
{
Color c = TrayApp.PresetColors[i];
Button b = new Button();
b.Size = new Size(24, 24);
b.Location = new Point(cx, y);
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Cursor = Cursors.Hand;
b.Paint += delegate(object s, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (Brush br = new SolidBrush(c)) e.Graphics.FillEllipse(br, 1, 1, 22, 22);
bool sel = (_app.CurrentEffect != EffectMode.Rainbow &&
_app.CurrentColor.R == c.R && _app.CurrentColor.G == c.G && _app.CurrentColor.B == c.B);
if (sel)
{
using (Pen p = new Pen(Color.White, 2f)) e.Graphics.DrawEllipse(p, 2, 2, 20, 20);
}
};
b.Click += delegate
{
_app.SetColor(c);
RefreshState();
};
_colorBtns[i] = b;
Controls.Add(b);
cx += 28;
}
_btnCustomColor = new Button();
_btnCustomColor.Text = "+";
_btnCustomColor.Font = new Font("Segoe UI", 11f, FontStyle.Bold);
_btnCustomColor.ForeColor = Color.LightGray;
_btnCustomColor.BackColor = Color.FromArgb(42, 42, 46);
_btnCustomColor.Size = new Size(24, 24);
_btnCustomColor.Location = new Point(cx + 4, y);
_btnCustomColor.FlatStyle = FlatStyle.Flat;
_btnCustomColor.FlatAppearance.BorderSize = 0;
_btnCustomColor.Cursor = Cursors.Hand;
_btnCustomColor.Click += delegate
{
using (ColorDialog dlg = new ColorDialog())
{
dlg.Color = _app.CurrentColor;
dlg.FullOpen = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
_app.SetColor(dlg.Color);
RefreshState();
}
}
};
Controls.Add(_btnCustomColor);
y += 36;
// Section: KEYBOARD BRIGHTNESS
Controls.Add(CreateHeaderLabel("KEYBOARD BRIGHTNESS (10% STEPS)", 16, y));
_lblBrightVal = new Label();
_lblBrightVal.Text = _app.CurrentBrightness + " %";
_lblBrightVal.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
_lblBrightVal.ForeColor = Color.FromArgb(0, 150, 255);
_lblBrightVal.Location = new Point(280, y - 2);
_lblBrightVal.Size = new Size(64, 18);
_lblBrightVal.TextAlign = ContentAlignment.MiddleRight;
Controls.Add(_lblBrightVal);
y += 20;
_brightSlider = new DarkSlider();
_brightSlider.Location = new Point(16, y);
_brightSlider.Size = new Size(328, 22);
_brightSlider.Value = _app.CurrentBrightness;
_brightSlider.ValueChanged += delegate
{
int val = _brightSlider.Value;
_lblBrightVal.Text = val + " %";
_app.SetBrightness(val);
RefreshQuickBrightBtns();
};
Controls.Add(_brightSlider);
y += 26;
_brightBtns = new Button[QuickBrightness.Length];
int bw = 76;
for (int i = 0; i < QuickBrightness.Length; i++)
{
int lvl = QuickBrightness[i];
Button b = CreatePillButton(lvl + " %", 16 + i * (bw + 8), y, bw, 24);
b.Click += delegate
{
_brightSlider.Value = lvl;
_app.SetBrightness(lvl);
RefreshState();
};
_brightBtns[i] = b;
Controls.Add(b);
}
y += 34;
// Section: EFFECT
Controls.Add(CreateHeaderLabel("EFFECT", 16, y));
y += 22;
string[] effNames = { "Static", "Breathing", "Rainbow" };
EffectMode[] effModes = { EffectMode.Static, EffectMode.Breathing, EffectMode.Rainbow };
_effectBtns = new Button[effNames.Length];
int ew = 104;
for (int i = 0; i < effNames.Length; i++)
{
EffectMode em = effModes[i];
Button b = CreatePillButton(effNames[i], 16 + i * (ew + 8), y, ew, 26);
b.Click += delegate
{
_app.SetEffect(em);
RefreshState();
};
_effectBtns[i] = b;
Controls.Add(b);
}
y += 36;
// Section: TIMEOUT (IDLE)
Controls.Add(CreateHeaderLabel("TIMEOUT (IDLE)", 16, y));
y += 22;
int[] toSecs = { 0, 30, 60, 120, 300, 600 };
string[] toLabels = { "Off", "30s", "1m", "2m", "5m", "10m" };
_timeoutBtns = new Button[toSecs.Length];
int tw = 48;
for (int i = 0; i < toSecs.Length; i++)
{
int sec = toSecs[i];
Button b = CreatePillButton(toLabels[i], 16 + i * (tw + 6), y, tw, 26);
b.Click += delegate
{
_app.SetTimeout(sec);
RefreshState();
};
_timeoutBtns[i] = b;
Controls.Add(b);
}
y += 36;
Controls.Add(CreateSeparator(16, y, 328));
y += 10;
// Section: EXTERNAL DISPLAY (TEST)
Controls.Add(CreateHeaderLabel("EXTERNAL DISPLAY (HDMI / TYPE-C)", 16, y));
_lblExtBrightVal = new Label();
_lblExtBrightVal.Text = _app.CurrentExtDisplayBrightness + " %";
_lblExtBrightVal.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
_lblExtBrightVal.ForeColor = Color.FromArgb(0, 210, 140);
_lblExtBrightVal.Location = new Point(280, y - 2);
_lblExtBrightVal.Size = new Size(64, 18);
_lblExtBrightVal.TextAlign = ContentAlignment.MiddleRight;
Controls.Add(_lblExtBrightVal);
y += 20;
_extBrightSlider = new DarkSlider();
_extBrightSlider.Location = new Point(16, y);
_extBrightSlider.Size = new Size(328, 22);
_extBrightSlider.Value = _app.CurrentExtDisplayBrightness;
_extBrightSlider.ValueChanged += delegate
{
int val = _extBrightSlider.Value;
_lblExtBrightVal.Text = val + " %";
string mode;
DisplayHelper.SetExternalBrightness(val, out mode);
_app.SetExtDisplayBrightness(val);
if (_lblExtStatus != null) _lblExtStatus.Text = "Mode: " + mode;
};
Controls.Add(_extBrightSlider);
y += 24;
_lblExtStatus = new Label();
_lblExtStatus.Text = "Mode: Auto (DDC/CI or Overlay)";
_lblExtStatus.Font = new Font("Segoe UI", 7.5f);
_lblExtStatus.ForeColor = Color.FromArgb(120, 120, 130);
_lblExtStatus.Location = new Point(16, y);
_lblExtStatus.Size = new Size(328, 16);
Controls.Add(_lblExtStatus);
}
Label CreateHeaderLabel(string text, int x, int y)
{
Label lbl = new Label();
lbl.Text = text;
lbl.Font = new Font("Segoe UI", 7.5f, FontStyle.Bold);
lbl.ForeColor = Color.FromArgb(140, 140, 148);
lbl.Location = new Point(x, y);
lbl.AutoSize = true;
return lbl;
}
Control CreateSeparator(int x, int y, int width)
{
Panel p = new Panel();
p.Location = new Point(x, y);
p.Size = new Size(width, 1);
p.BackColor = Color.FromArgb(45, 45, 50);
return p;
}
Button CreatePillButton(string text, int x, int y, int width, int height)
{
Button b = new Button();
b.Text = text;
b.Location = new Point(x, y);
b.Size = new Size(width, height);
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Font = new Font("Segoe UI", 8.5f);
b.Cursor = Cursors.Hand;
return b;
}
void RefreshQuickBrightBtns()
{
for (int i = 0; i < QuickBrightness.Length; i++)
{
bool sel = (_app.CurrentBrightness == QuickBrightness[i]);
StyleTogglePill(_brightBtns[i], sel);
}
}
public void RefreshState()
{
// Power Button
if (_app.CurrentPowerOn)
{
_btnPower.Text = "● Light ON";
_btnPower.BackColor = Color.FromArgb(32, 110, 56);
_btnPower.ForeColor = Color.White;
}
else
{
_btnPower.Text = "○ Light OFF";
_btnPower.BackColor = Color.FromArgb(60, 60, 65);
_btnPower.ForeColor = Color.FromArgb(200, 100, 100);
}
// Color circles refresh
foreach (Button b in _colorBtns) b.Invalidate();
// Keyboard Brightness Slider & Value Label
if (_brightSlider != null)
{
_brightSlider.Value = _app.CurrentBrightness;
if (_lblBrightVal != null) _lblBrightVal.Text = _app.CurrentBrightness + " %";
}
RefreshQuickBrightBtns();
// External Display Slider
if (_extBrightSlider != null)
{
_extBrightSlider.Value = _app.CurrentExtDisplayBrightness;
if (_lblExtBrightVal != null) _lblExtBrightVal.Text = _app.CurrentExtDisplayBrightness + " %";
}
// Effect Buttons
EffectMode[] effModes = { EffectMode.Static, EffectMode.Breathing, EffectMode.Rainbow };
for (int i = 0; i < effModes.Length; i++)
{
bool sel = (_app.CurrentEffect == effModes[i]);
StyleTogglePill(_effectBtns[i], sel);
}
// Timeout Buttons
int[] toSecs = { 0, 30, 60, 120, 300, 600 };
for (int i = 0; i < toSecs.Length; i++)
{
bool sel = (_app.CurrentTimeoutSec == toSecs[i]);
StyleTogglePill(_timeoutBtns[i], sel);
}
}
void StyleTogglePill(Button b, bool active)
{
if (active)
{
b.BackColor = Color.FromArgb(0, 120, 215); // Windows 11 Accent Blue
b.ForeColor = Color.White;
b.Font = FontToggleBold;
}
else
{
b.BackColor = Color.FromArgb(38, 38, 42);
b.ForeColor = Color.FromArgb(210, 210, 215);
b.Font = FontToggleNormal;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Pen p = new Pen(Color.FromArgb(50, 50, 56), 1))
{
e.Graphics.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}
}
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
Hide();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
Hide();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
public void ToggleFlyout()
{
if (Visible)
{
Hide();
}
else
{
RefreshState();
Rectangle wa = Screen.PrimaryScreen.WorkingArea;
Left = wa.Right - Width - 16;
Top = wa.Bottom - Height - 16;
Show();
Activate();
BringToFront();
}
}
}
class TrayApp : ApplicationContext
{
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("user32.dll")]
static extern bool DestroyIcon(IntPtr h);
readonly NotifyIcon tray = new NotifyIcon();
readonly object stateLock = new object();
readonly Control ui = new Control();
readonly string cfgPath;
readonly string legacyCfgPath;
Color currentColor = Color.White;
int currentBrightness = 100;
int currentTimeoutSec = 60;
EffectMode currentEffect = EffectMode.Static;
bool currentPowerOn = true;
int currentExtDisplayBrightness = 100;
int omenEventCode = -1; // -1 = any hpqBEvnt toggles the flyout
public Color CurrentColor { get { return currentColor; } }
public int CurrentBrightness { get { return currentBrightness; } }
public int CurrentTimeoutSec { get { return currentTimeoutSec; } }
public EffectMode CurrentEffect { get { return currentEffect; } }
public bool CurrentPowerOn { get { return currentPowerOn; } }
public int CurrentExtDisplayBrightness { get { return currentExtDisplayBrightness; } }
volatile bool isExiting = false;
volatile bool forceUpdate = true;
Thread workerThread;
FlyoutForm flyout;
ManagementEventWatcher omenWatcher;
public static readonly string[] Names = { "White", "Red", "Orange", "Yellow", "Green", "Cyan", "Blue", "Purple", "Pink" };
public static readonly Color[] PresetColors = {
Color.FromArgb(255,255,255), Color.FromArgb(255,0,0), Color.FromArgb(255,100,0),
Color.FromArgb(255,220,0), Color.FromArgb(0,255,0), Color.FromArgb(0,255,255),
Color.FromArgb(0,80,255), Color.FromArgb(160,0,255), Color.FromArgb(255,0,150)
};
ToolStripMenuItem[] colorItems;
ToolStripMenuItem effectStaticItem;
ToolStripMenuItem effectBreatheItem;
ToolStripMenuItem effectRainbowItem;
ToolStripMenuItem[] brightnessItems;
ToolStripMenuItem[] timeoutItems;
ToolStripMenuItem powerItem;
public static readonly int[] BrightnessLevels = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
public static readonly int[] TimeoutSecondsList = { 0, 30, 60, 120, 300, 600 };
public static readonly string[] TimeoutLabels = { "Disabled (Never)", "30 Seconds", "1 Minute", "2 Minutes", "5 Minutes", "10 Minutes" };
public TrayApp()
{
IntPtr h = ui.Handle;
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string dir = Path.Combine(appData, "VictusControl");
Directory.CreateDirectory(dir);
cfgPath = Path.Combine(dir, "settings.txt");
legacyCfgPath = Path.Combine(Path.Combine(appData, "VictusKbd"), "settings.txt");
LoadSettings();
flyout = new FlyoutForm(this);
ContextMenuStrip menu = CreateMenu();
tray.ContextMenuStrip = menu;
tray.Text = "Victus Control (Omen Key or Click)";
UpdateTrayIcon();
tray.Visible = true;
// Left click on tray toggles Flyout
tray.MouseClick += delegate(object s, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ToggleFlyout();
}
};
// Omen key is the only keyboard trigger: WMI event hpqBEvnt (one event per press)
SetupWmiOmenWatcher();
SystemEvents.PowerModeChanged += delegate(object s, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume)
{
Hp.ResetConnection();
forceUpdate = true;
}
};
SystemEvents.SessionSwitch += delegate(object s, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionUnlock)
{
Hp.ResetConnection();
forceUpdate = true;
}
};
workerThread = new Thread(WorkerLoop);
workerThread.IsBackground = true;
workerThread.Start();
}
static int WmiEventCode(ManagementBaseObject ev)
{
try
{
object v = ev["EventData"];
if (v != null) return Convert.ToInt32(v);
}
catch { }
return int.MinValue;
}
void LogWmiEvent(ManagementBaseObject ev)
{
try
{
string line = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ";
foreach (PropertyData p in ev.Properties)
{
line += p.Name + "=" + (p.Value == null ? "null" : p.Value.ToString()) + " ";
}
File.AppendAllText(Path.Combine(Path.GetDirectoryName(cfgPath), "events.log"), line + "\r\n");
}
catch { }
}
void SetupWmiOmenWatcher()
{
try
{
ManagementScope scope = new ManagementScope(@"root\wmi");
scope.Connect();
EventQuery q = new EventQuery("SELECT * FROM hpqBEvnt");
omenWatcher = new ManagementEventWatcher(scope, q);
omenWatcher.EventArrived += delegate(object sender, EventArrivedEventArgs ea)
{
// Log every hpqBEvnt with all its fields, so the Omen key and other keys can be told apart
LogWmiEvent(ea.NewEvent);
// Optional filter: if omenevent=<code> is set in settings, only that code toggles the flyout
if (omenEventCode >= 0 && WmiEventCode(ea.NewEvent) != omenEventCode) return;
ui.BeginInvoke((MethodInvoker)delegate { ToggleFlyout(); });
};
omenWatcher.Start();
}
catch { }
}
public void ToggleFlyout()
{
if (flyout != null) flyout.ToggleFlyout();
}
ContextMenuStrip CreateMenu()
{
ContextMenuStrip menu = new ContextMenuStrip();
menu.Opening += delegate { UpdateMenuChecks(); };
ToolStripMenuItem quickItem = new ToolStripMenuItem("Quick Control (Omen Key)...");
quickItem.Font = new Font(menu.Font, FontStyle.Bold);
quickItem.Click += delegate { ToggleFlyout(); };
menu.Items.Add(quickItem);
menu.Items.Add(new ToolStripSeparator());
colorItems = new ToolStripMenuItem[Names.Length];
for (int i = 0; i < Names.Length; i++)
{
Color c = PresetColors[i];
ToolStripMenuItem item = new ToolStripMenuItem(Names[i]);
item.Click += delegate { SetColor(c); };
colorItems[i] = item;
menu.Items.Add(item);
}
ToolStripMenuItem custom = new ToolStripMenuItem("Custom Color...");
custom.Click += delegate
{
using (ColorDialog dlg = new ColorDialog())
{
dlg.Color = currentColor;