Repository navigation
Expand file tree
/
Copy pathFansPowerForm.cs
More file actions
1196 lines (1093 loc) · 53.4 KB
/
Copy pathFansPowerForm.cs
File metadata and controls
1196 lines (1093 loc) · 53.4 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Windows.Forms;
using MHelper.Hardware;
namespace MHelper
{
public sealed class FansPowerForm : Form
{
private static readonly Color BgWindow = Color.FromArgb(24, 26, 31);
private static readonly Color BgCard = Color.FromArgb(34, 38, 46);
private static readonly Color BgButton = Color.FromArgb(46, 51, 62);
private static readonly Color BgButtonHover = Color.FromArgb(58, 64, 78);
private static readonly Color BorderSubtle = Color.FromArgb(56, 62, 74);
private static readonly Color TextPrimary = Color.FromArgb(242, 244, 248);
private static readonly Color TextMuted = Color.FromArgb(156, 163, 175);
private static readonly Color AccentBlue = Color.FromArgb(59, 130, 246);
private static readonly Color AccentRed = Color.FromArgb(225, 29, 72);
private static readonly Color AccentGreen = Color.FromArgb(16, 185, 129);
private static readonly Color AccentAmber = Color.FromArgb(245, 158, 11);
private readonly MsiAcpi _acpi;
private readonly RyzenSmuController _smu;
private readonly AppSettings _settings;
private readonly UiCategorySettings _cats;
private readonly Action _onChanged;
private readonly float _dpi;
private bool _loading;
private bool _installingPawnIo;
// Ryzen SMU controls (null if ShowRyzenTuning == false)
private Label _lblPawnStatus;
private Button _btnInstallPawn;
private CheckBox _chkCo;
private Label _lblCoVal;
private ModernSlider _sldCo;
private CheckBox _chkTdp;
private Label _lblTdpVal;
private ModernSlider _sldTdp;
private CheckBox _chkTemp;
private Label _lblTempVal;
private ModernSlider _sldTemp;
private CheckBox _chkAutoApply;
private Label _lblRyzenResult;
// Windows Power & Boost (null if ShowWindowsPower == false)
private Button _btnBoostOff, _btnBoostEff, _btnBoostAgg;
private Button _btnPwrEff, _btnPwrBal, _btnPwrPerf;
private Label _lblWinPowerStatus;
// Fan curves & manual sliders (unused if ShowCustomFanCurves == false)
private readonly NumericUpDown[] _cpuNum = new NumericUpDown[7];
private readonly NumericUpDown[] _gpuNum = new NumericUpDown[7];
private bool _fanManualMode = true;
private Button _btnFanTabManual;
private Button _btnFanTabCurve;
private Panel _pnlFanManual;
private Panel _pnlFanCurve;
private ModernSlider _sldFanManual;
private Label _lblFanManualVal;
private Label _lblCpuRpmLive;
private Label _lblGpuRpmLive;
private bool _fanTableOk;
private Button _btnApplyFans;
private Button _btnResetAuto;
private Label _lblFanResult;
private System.Windows.Forms.Timer _rpmTimer;
private int _liveCpuRpm = -1;
private int _liveGpuRpm = -1;
private int _liveDutyPct = -1;
private FanMode _liveFanMode = FanMode.Unknown;
private bool _liveCoolerBoost;
private int _rpmReadInFlight;
private int _fanCardW;
private int _fanCpad;
private readonly ToolTip _toolTip = new ToolTip { AutoPopDelay = 25000, InitialDelay = 120, ReshowDelay = 50, ShowAlways = true };
private int S(int px) { return (int)Math.Round(px * _dpi); }
public FansPowerForm(MsiAcpi acpi, RyzenSmuController smu, AppSettings settings, Action onChanged)
{
_acpi = acpi;
_smu = smu;
_settings = settings;
_cats = settings.GetCategories();
_onChanged = onChanged;
AutoScaleMode = AutoScaleMode.None;
_dpi = UiDpi.GetScale(this);
Text = "M-Helper — Fans + Power";
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
TopMost = true;
BackColor = BgWindow;
ForeColor = TextPrimary;
Font = UiFonts.DialogBody;
DoubleBuffered = true;
KeyPreview = true;
KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape && !_installingPawnIo) Close(); };
ClientSize = new Size(S(540), S(700));
BuildUi();
LoadState();
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
UiChrome.ApplyRoundedDarkBorder(Handle, BorderSubtle);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var pen = new Pen(BorderSubtle, 1))
e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (_installingPawnIo && e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
return;
}
base.OnFormClosing(e);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
if (_rpmTimer != null)
{
_rpmTimer.Stop();
_rpmTimer.Dispose();
_rpmTimer = null;
}
_toolTip.Dispose();
base.OnFormClosed(e);
}
private void BuildUi()
{
int pad = S(14);
int cpad = S(12);
int gap = S(6);
int w = ClientSize.Width - pad * 2;
var subParts = new List<string>();
if (_cats.ShowRyzenTuning) subParts.Add("Ryzen SMU");
if (_cats.ShowWindowsPower) subParts.Add("Windows Power");
if (_cats.ShowCustomFanCurves) subParts.Add("Fan Control");
// --- CUSTOM DRAGGABLE DARK HEADER ---
var hdr = new Panel { Location = new Point(1, 1), Size = new Size(ClientSize.Width - 2, S(40)), BackColor = BgWindow };
var lblHdr = new Label
{
Text = "Fans + Power",
Font = UiFonts.SubWindowTitle,
ForeColor = TextPrimary,
Location = new Point(pad - 1, S(10)),
AutoSize = true,
UseMnemonic = false
};
var lblHdrSub = new Label
{
Text = string.Join(" • ", subParts.ToArray()),
Font = UiFonts.Regular80,
ForeColor = TextMuted,
Location = new Point(pad + S(112), S(14)),
AutoSize = true,
UseMnemonic = false
};
var btnClose = new Button
{
Text = "✕",
FlatStyle = FlatStyle.Flat,
ForeColor = TextMuted,
BackColor = BgWindow,
Size = new Size(S(28), S(26)),
Location = new Point(hdr.Width - pad - S(26), S(7)),
Cursor = Cursors.Hand,
TabStop = false,
UseMnemonic = false
};
btnClose.FlatAppearance.BorderSize = 0;
btnClose.FlatAppearance.MouseOverBackColor = BgCard;
btnClose.Click += delegate { if (!_installingPawnIo) Close(); };
hdr.MouseDown += (s, e) => UiChrome.BeginDragWindow(this, e);
lblHdr.MouseDown += (s, e) => UiChrome.BeginDragWindow(this, e);
lblHdrSub.MouseDown += (s, e) => UiChrome.BeginDragWindow(this, e);
hdr.Controls.AddRange(new Control[] { lblHdr, lblHdrSub, btnClose });
Controls.Add(hdr);
var body = new Panel { Location = new Point(1, hdr.Bottom), Width = ClientSize.Width - 2, BackColor = BgWindow };
int y = S(4);
// --- CARD 1: RYZEN 9 9955HX SMU TUNING ---
if (_cats.ShowRyzenTuning)
{
var cardRyzen = CreateCard(pad - 1, y, w, S(286), "RYZEN CPU TUNING (CURVE OPTIMIZER + TDP LIMITER)");
cardRyzen.Controls.Add(CreateInfoBadge(w - cpad - S(18), S(7),
"• Curve Optimizer (CO): Negative values undervolt all CPU cores for lower temps and higher clocks.\n" +
"• TDP / Temp Limits: Caps sustained package power (W) and max CPU temperature (°C).\n" +
"• 60s Safety Guard: Startup/wake auto-apply automatically disables itself if the system crashes within 60 seconds."));
int cy = S(28);
string pawnVer = RyzenSmuController.GetInstalledPawnIoVersion();
string modStatus;
bool modOk = RyzenSmuController.VerifyModuleIntegrity(out modStatus);
string statusText;
Color statusColor;
if (!_smu.IsCpuSupported)
{
statusText = _smu.CpuGuardReason;
statusColor = AccentAmber;
}
else if (!modOk)
{
statusText = "RyzenSMU.bin " + modStatus + " — SMU writes disabled";
statusColor = AccentAmber;
}
else if (pawnVer != null)
{
statusText = "PawnIO v" + pawnVer + " (WHQL-signed, HVCI safe) — Ready";
statusColor = AccentGreen;
}
else
{
statusText = "PawnIO driver not installed (needed for Ryzen SMU undervolt & TDP)";
statusColor = AccentAmber;
}
_lblPawnStatus = new Label
{
Text = statusText,
Font = UiFonts.Regular82,
ForeColor = statusColor,
Location = new Point(cpad, cy),
Size = new Size(w - S(136), S(18)),
UseMnemonic = false
};
cardRyzen.Controls.Add(_lblPawnStatus);
if (_smu.IsCpuSupported && modOk && pawnVer == null)
{
_btnInstallPawn = CreateButton("Install PawnIO", w - S(118), cy - S(3), S(106), S(24));
_btnInstallPawn.BackColor = AccentBlue;
_btnInstallPawn.FlatAppearance.BorderColor = AccentBlue;
_btnInstallPawn.Click += delegate { RunPawnIoInstall(); };
cardRyzen.Controls.Add(_btnInstallPawn);
}
cy += S(26);
int pw = (w - cpad * 2 - gap * 3) / 4;
var btnP1 = CreateButton("Quiet 35W (-25)", cpad, cy, pw, S(26));
var btnP2 = CreateButton("Game 75C (-25)", cpad + pw + gap, cy, pw, S(26));
var btnP3 = CreateButton("Only -25 CO", cpad + (pw + gap) * 2, cy, pw, S(26));
var btnP4 = CreateButton("0 CO / 115W", cpad + (pw + gap) * 3, cy, pw, S(26));
btnP1.Click += delegate { LoadPreset(true, -25, true, 35, true, 75, null); };
btnP2.Click += delegate { LoadPreset(true, -25, false, 35, true, 75, null); };
btnP3.Click += delegate { LoadPreset(true, -25, false, 115, false, 95, null); };
btnP4.Click += delegate { LoadPreset(true, 0, true, 115, true, 95, "Preset loaded (0 CO, 115W, 95°C) — reboot restores true BIOS fused power defaults."); };
cardRyzen.Controls.AddRange(new Control[] { btnP1, btnP2, btnP3, btnP4 });
cy += S(36);
_chkCo = new CheckBox { Text = "All-Core Undervolt (CO)", ForeColor = TextPrimary, Location = new Point(cpad, cy), AutoSize = true, UseMnemonic = false };
_lblCoVal = new Label { Text = "-25", Font = UiFonts.Semibold90, ForeColor = AccentBlue, TextAlign = ContentAlignment.TopRight, Location = new Point(w - S(84), cy), Size = new Size(S(72), S(18)), UseMnemonic = false };
_sldCo = new ModernSlider(RyzenSmuController.MinCo, RyzenSmuController.MaxCo, -25, 1, AccentBlue)
{
BackColor = BgCard,
Location = new Point(cpad - S(4), cy + S(21)),
Size = new Size(w - (cpad - S(4)) * 2, S(24))
};
_sldCo.ValueChanged += delegate
{
if (_loading) return;
_lblCoVal.Text = _sldCo.Value.ToString();
_chkCo.Checked = true;
};
cardRyzen.Controls.AddRange(new Control[] { _chkCo, _lblCoVal, _sldCo });
cy += S(48);
_chkTdp = new CheckBox { Text = "TDP Watt Limit (STAPM / PPT)", ForeColor = TextPrimary, Location = new Point(cpad, cy), AutoSize = true, UseMnemonic = false };
_lblTdpVal = new Label { Text = "35 W", Font = UiFonts.Semibold90, ForeColor = AccentAmber, TextAlign = ContentAlignment.TopRight, Location = new Point(w - S(84), cy), Size = new Size(S(72), S(18)), UseMnemonic = false };
_sldTdp = new ModernSlider(RyzenSmuController.MinTdpW, RyzenSmuController.MaxTdpW, 35, 5, AccentAmber)
{
BackColor = BgCard,
Location = new Point(cpad - S(4), cy + S(21)),
Size = new Size(w - (cpad - S(4)) * 2, S(24))
};
_sldTdp.ValueChanged += delegate
{
if (_loading) return;
_lblTdpVal.Text = _sldTdp.Value + " W";
_chkTdp.Checked = true;
};
cardRyzen.Controls.AddRange(new Control[] { _chkTdp, _lblTdpVal, _sldTdp });
cy += S(48);
_chkTemp = new CheckBox { Text = "CPU Temp Limit (Tctl / cHTC)", ForeColor = TextPrimary, Location = new Point(cpad, cy), AutoSize = true, UseMnemonic = false };
_lblTempVal = new Label { Text = "75 °C", Font = UiFonts.Semibold90, ForeColor = AccentGreen, TextAlign = ContentAlignment.TopRight, Location = new Point(w - S(84), cy), Size = new Size(S(72), S(18)), UseMnemonic = false };
_sldTemp = new ModernSlider(RyzenSmuController.MinTempC, RyzenSmuController.MaxTempC, 75, 1, AccentGreen)
{
BackColor = BgCard,
Location = new Point(cpad - S(4), cy + S(21)),
Size = new Size(w - (cpad - S(4)) * 2, S(24))
};
_sldTemp.ValueChanged += delegate
{
if (_loading) return;
_lblTempVal.Text = _sldTemp.Value + " °C";
_chkTemp.Checked = true;
};
cardRyzen.Controls.AddRange(new Control[] { _chkTemp, _lblTempVal, _sldTemp });
cy += S(50);
_chkAutoApply = new CheckBox { Text = "Auto-apply on startup / wake (60s guard)", ForeColor = TextMuted, Location = new Point(cpad, cy + S(4)), AutoSize = true, UseMnemonic = false };
var btnApplyRyzen = CreateButton("Apply Ryzen Tuning", w - S(166), cy, S(154), S(28));
btnApplyRyzen.BackColor = AccentRed;
btnApplyRyzen.FlatAppearance.BorderColor = AccentRed;
btnApplyRyzen.Enabled = _smu.IsCpuSupported && modOk;
btnApplyRyzen.Click += delegate { ApplyRyzenTuning(); };
cardRyzen.Controls.AddRange(new Control[] { _chkAutoApply, btnApplyRyzen });
cy += S(30);
_lblRyzenResult = new Label { Text = "", Font = UiFonts.Regular80, ForeColor = TextMuted, Location = new Point(cpad, cy), Size = new Size(w - cpad * 2, S(18)), UseMnemonic = false };
cardRyzen.Controls.Add(_lblRyzenResult);
body.Controls.Add(cardRyzen);
y += cardRyzen.Height + S(10);
}
// --- CARD 2: WINDOWS POWER & CPU BOOST ---
if (_cats.ShowWindowsPower)
{
int winCardH = _cats.ShowRyzenTuning ? S(116) : S(138);
var cardWin = CreateCard(pad - 1, y, w, winCardH, "WINDOWS POWER MODE & CPU TURBO BOOST");
int bw3 = (w - cpad * 2 - gap * 2) / 3;
var lblB = new Label { Text = "CPU Turbo Boost:", ForeColor = TextMuted, Font = UiFonts.Regular83, Location = new Point(cpad, S(27)), AutoSize = true, UseMnemonic = false };
_btnBoostOff = CreateButton("Disabled", cpad, S(46), bw3, S(26));
_btnBoostEff = CreateButton("Efficient", cpad + bw3 + gap, S(46), bw3, S(26));
_btnBoostAgg = CreateButton("Aggressive", cpad + (bw3 + gap) * 2, S(46), bw3, S(26));
_btnBoostOff.Click += delegate { ApplyCpuBoost(CpuBoostMode.Disabled); };
_btnBoostEff.Click += delegate { ApplyCpuBoost(CpuBoostMode.EfficientEnabled); };
_btnBoostAgg.Click += delegate { ApplyCpuBoost(CpuBoostMode.Aggressive); };
_btnPwrEff = CreateButton("Best Efficiency", cpad, S(78), bw3, S(26));
_btnPwrBal = CreateButton("Balanced", cpad + bw3 + gap, S(78), bw3, S(26));
_btnPwrPerf = CreateButton("Best Performance", cpad + (bw3 + gap) * 2, S(78), bw3, S(26));
_btnPwrEff.Click += delegate { ApplyWinPower(WindowsPowerMode.BestPowerEfficiency); };
_btnPwrBal.Click += delegate { ApplyWinPower(WindowsPowerMode.Balanced); };
_btnPwrPerf.Click += delegate { ApplyWinPower(WindowsPowerMode.BestPerformance); };
cardWin.Controls.AddRange(new Control[] { lblB, _btnBoostOff, _btnBoostEff, _btnBoostAgg, _btnPwrEff, _btnPwrBal, _btnPwrPerf });
if (!_cats.ShowRyzenTuning)
{
_lblWinPowerStatus = new Label
{
Text = "",
Font = UiFonts.Regular80,
ForeColor = TextMuted,
Location = new Point(cpad, S(110)),
Size = new Size(w - cpad * 2, S(18)),
UseMnemonic = false
};
cardWin.Controls.Add(_lblWinPowerStatus);
}
body.Controls.Add(cardWin);
y += cardWin.Height + S(10);
}
// --- CARD 3: FAN CONTROL (MANUAL SLIDER & CUSTOM CURVES) ---
if (_cats.ShowCustomFanCurves)
{
_fanCardW = w;
_fanCpad = cpad;
var cardFan = CreateCard(pad - 1, y, w, S(258), "FAN CONTROL (MANUAL SLIDER & CUSTOM CURVES)");
cardFan.Controls.Add(CreateInfoBadge(w - cpad - S(18), S(7),
"• 0–150% Fan Scale: MSI uses 0–100% for normal quiet-to-medium speeds and 101–150% for high-speed overdrive up toward Cooler Boost (~5,000+ RPM).\n" +
"• Linked Fans: On shared-heatpipe MSI laptops (such as Vector A18 HX), firmware couples the GPU fan to match the CPU fan, so one manual slider controls both.\n" +
"• Performance Mode Cap: Balanced mode may limit peak RPM; switch to Extreme (or Cooler Boost) on the main panel for maximum fan speed.\n" +
"• Overheat Protection: Even at low manual speeds, fans still ramp up automatically if temperatures get high."));
FanTables tables = null;
bool fanTableOk = _acpi != null && _acpi.TryReadFanTables(out tables);
_fanTableOk = fanTableOk;
bool canEditFans = fanTableOk && _acpi != null && _acpi.CanWrite;
int initCpuRpm, initGpuRpm, initDuty;
FanMode initMode;
bool initCb;
if (_acpi != null && _acpi.TryReadFanLiveState(out initCpuRpm, out initGpuRpm, out initDuty, out initMode, out initCb))
{
_liveCpuRpm = initCpuRpm;
_liveGpuRpm = initGpuRpm;
_liveDutyPct = initDuty;
_liveFanMode = initMode;
_liveCoolerBoost = initCb;
}
int tabW = (w - cpad * 2 - gap) / 2;
_btnFanTabManual = CreateButton("Manual Speed", cpad, S(28), tabW, S(26));
_btnFanTabCurve = CreateButton("Custom Curve", cpad + tabW + gap, S(28), w - cpad * 2 - tabW - gap, S(26));
_btnFanTabManual.Click += delegate { SetFanTab(true); };
_btnFanTabCurve.Click += delegate { SetFanTab(false); };
_pnlFanManual = new Panel
{
Location = new Point(1, S(60)),
Size = new Size(w - 2, S(120)),
BackColor = BgCard
};
int innerX = cpad - 1;
int innerW = w - cpad * 2;
int initMan = fanTableOk
? Math.Max(0, Math.Min(MsiAcpi.MaxFanSpeedPct, (int)Math.Round(Math.Max(tables.CpuSpeeds[0], tables.GpuSpeeds[0]) / 5.0) * 5))
: 50;
var lblFanMan = new Label
{
Text = "Manual Fan Speed",
ForeColor = AccentBlue,
Font = UiFonts.Semibold85,
Location = new Point(innerX, S(8)),
AutoSize = true,
UseMnemonic = false
};
_lblFanManualVal = new Label
{
Text = "",
ForeColor = AccentBlue,
Font = UiFonts.Semibold90,
TextAlign = ContentAlignment.TopRight,
Location = new Point(w - 2 - innerX - S(160), S(7)),
Size = new Size(S(160), S(18)),
UseMnemonic = false
};
_sldFanManual = new ModernSlider(0, MsiAcpi.MaxFanSpeedPct, initMan, 5, AccentBlue)
{
BackColor = BgCard,
Enabled = canEditFans,
Location = new Point(innerX - S(4), S(32)),
Size = new Size(innerW + S(8), S(26))
};
_sldFanManual.ValueChanged += delegate
{
UpdateManualFanLabels();
if (_sldFanManual.IsDragging && _lblFanResult != null)
{
_lblFanResult.Text = string.Format("Release slider to set fans to {0}%...", _sldFanManual.Value);
_lblFanResult.ForeColor = _sldFanManual.Value > 100 ? AccentAmber : AccentBlue;
}
};
_sldFanManual.ValueCommitted += delegate { ApplyManualFanSpeed(); };
var lblTick0 = new Label
{
Text = "0%",
ForeColor = TextMuted,
Font = UiFonts.Regular80,
Location = new Point(innerX, S(60)),
AutoSize = true,
UseMnemonic = false
};
int midX = innerX + (int)Math.Round(innerW * (100.0 / MsiAcpi.MaxFanSpeedPct)) - S(44);
var lblTick100 = new Label
{
Text = "100% (Normal)",
ForeColor = TextMuted,
Font = UiFonts.Regular80,
TextAlign = ContentAlignment.TopCenter,
Location = new Point(midX, S(60)),
Size = new Size(S(88), S(16)),
UseMnemonic = false
};
var lblTick150 = new Label
{
Text = "150% (Max)",
ForeColor = TextMuted,
Font = UiFonts.Regular80,
TextAlign = ContentAlignment.TopRight,
Location = new Point(w - 2 - innerX - S(72), S(60)),
Size = new Size(S(72), S(16)),
UseMnemonic = false
};
_lblCpuRpmLive = new Label
{
Text = "",
ForeColor = AccentBlue,
Font = UiFonts.Semibold85,
Location = new Point(innerX, S(88)),
Size = new Size(innerW / 2 - S(4), S(18)),
UseMnemonic = false
};
_lblGpuRpmLive = new Label
{
Text = "",
ForeColor = AccentGreen,
Font = UiFonts.Semibold85,
TextAlign = ContentAlignment.TopRight,
Location = new Point(innerX + innerW / 2 + S(4), S(88)),
Size = new Size(innerW / 2 - S(4), S(18)),
UseMnemonic = false
};
UpdateManualFanLabels();
_pnlFanManual.Controls.AddRange(new Control[]
{
lblFanMan, _lblFanManualVal, _sldFanManual,
lblTick0, lblTick100, lblTick150,
_lblCpuRpmLive, _lblGpuRpmLive
});
_pnlFanCurve = new Panel
{
Location = new Point(1, S(60)),
Size = new Size(w - 2, S(120)),
BackColor = BgCard,
Visible = false
};
int leftLabelW = S(64);
int colW = (w - leftLabelW - cpad) / 7;
var lblCpuRow = new Label { Text = "CPU %", ForeColor = AccentBlue, Font = UiFonts.Semibold85, Location = new Point(innerX, S(25)), AutoSize = true, UseMnemonic = false };
var lblGpuRow = new Label { Text = "GPU %", ForeColor = AccentGreen, Font = UiFonts.Semibold85, Location = new Point(innerX, S(83)), AutoSize = true, UseMnemonic = false };
_pnlFanCurve.Controls.AddRange(new Control[] { lblCpuRow, lblGpuRow });
for (int i = 0; i < 7; i++)
{
int cx = leftLabelW - 1 + i * colW;
string cpuHeader = fanTableOk ? (i == 0 ? ("<" + tables.CpuTemps[0] + "°") : (tables.CpuTemps[i - 1] + "°")) : "--°";
string gpuHeader = fanTableOk ? (i == 0 ? ("<" + tables.GpuTemps[0] + "°") : (tables.GpuTemps[i - 1] + "°")) : "--°";
int cpuSpeed = fanTableOk ? tables.CpuSpeeds[i] : MsiAcpi.MinSafeFanFloor[i];
int gpuSpeed = fanTableOk ? tables.GpuSpeeds[i] : MsiAcpi.MinSafeFanFloor[i];
_pnlFanCurve.Controls.Add(new Label { Text = cpuHeader, ForeColor = TextMuted, Font = UiFonts.Regular80, Location = new Point(cx, S(4)), Size = new Size(colW - S(4), S(17)), TextAlign = ContentAlignment.MiddleCenter, UseMnemonic = false });
_cpuNum[i] = CreateFanInput(cx, S(22), colW - S(6), cpuSpeed, canEditFans);
_pnlFanCurve.Controls.Add(_cpuNum[i]);
_pnlFanCurve.Controls.Add(new Label { Text = gpuHeader, ForeColor = TextMuted, Font = UiFonts.Regular80, Location = new Point(cx, S(62)), Size = new Size(colW - S(4), S(17)), TextAlign = ContentAlignment.MiddleCenter, UseMnemonic = false });
_gpuNum[i] = CreateFanInput(cx, S(80), colW - S(6), gpuSpeed, canEditFans);
_pnlFanCurve.Controls.Add(_gpuNum[i]);
}
_btnApplyFans = CreateButton("Apply Custom Curves", cpad, S(186), S(268), S(30));
_btnApplyFans.BackColor = AccentBlue;
_btnApplyFans.FlatAppearance.BorderColor = AccentBlue;
_btnApplyFans.Enabled = canEditFans;
_btnApplyFans.Click += delegate { ApplyFanCurves(); };
_btnResetAuto = CreateButton("Switch Back to Auto", cpad, S(186), w - cpad * 2, S(30));
_btnResetAuto.Enabled = _acpi != null && _acpi.CanWrite;
_btnResetAuto.Click += delegate
{
if (_acpi != null && _acpi.CanWrite)
{
bool autoOk = _acpi.RestoreAutoFanMode(FanMode.Auto);
if (autoOk)
{
FanTables restoredTables;
if (_acpi.TryReadFanTables(out restoredTables))
{
for (int i = 0; i < 7; i++)
{
_cpuNum[i].Value = Math.Max(0, Math.Min(MsiAcpi.MaxFanSpeedPct, restoredTables.CpuSpeeds[i]));
_gpuNum[i].Value = Math.Max(0, Math.Min(MsiAcpi.MaxFanSpeedPct, restoredTables.GpuSpeeds[i]));
}
}
int c, g, d;
FanMode m;
bool cb;
if (_acpi.TryReadFanLiveState(out c, out g, out d, out m, out cb))
{
_liveCpuRpm = c;
_liveGpuRpm = g;
_liveDutyPct = d;
_liveFanMode = m;
_liveCoolerBoost = cb;
}
else
{
_liveFanMode = FanMode.Auto;
_liveCoolerBoost = false;
}
UpdateManualFanLabels();
_lblFanResult.Text = "Automatic fan control restored — fans will slow down over a few seconds.";
_lblFanResult.ForeColor = AccentGreen;
}
else
{
_lblFanResult.Text = "Failed to switch back to automatic fan control.";
_lblFanResult.ForeColor = AccentAmber;
}
if (_onChanged != null) _onChanged();
}
};
_lblFanResult = new Label
{
Text = "",
Font = UiFonts.Regular80,
ForeColor = fanTableOk ? TextMuted : AccentAmber,
Location = new Point(cpad, S(224)),
Size = new Size(w - cpad * 2, S(24)),
UseMnemonic = false
};
cardFan.Controls.AddRange(new Control[] { _btnFanTabManual, _btnFanTabCurve, _pnlFanManual, _pnlFanCurve, _btnApplyFans, _btnResetAuto, _lblFanResult });
SetFanTab(true);
if (_acpi != null)
{
_rpmTimer = new System.Windows.Forms.Timer { Interval = 1500 };
_rpmTimer.Tick += delegate { RefreshFanRpmsAsync(); };
_rpmTimer.Start();
}
body.Controls.Add(cardFan);
y += cardFan.Height + S(10);
}
y += S(2);
Rectangle wa = (Screen.FromPoint(Cursor.Position) ?? Screen.PrimaryScreen).WorkingArea;
int maxTotalH = Math.Max(S(240), wa.Height - S(40));
int desiredTotalH = hdr.Bottom + y + 1;
if (desiredTotalH > maxTotalH)
{
int bodyH = maxTotalH - hdr.Bottom - 1;
body.Size = new Size(ClientSize.Width - 2, bodyH);
body.AutoScroll = true;
Controls.Add(body);
ClientSize = new Size(ClientSize.Width + SystemInformation.VerticalScrollBarWidth, maxTotalH);
hdr.Width = ClientSize.Width - 2;
btnClose.Left = hdr.Width - pad - S(26);
body.Width = ClientSize.Width - 2;
}
else
{
body.Size = new Size(ClientSize.Width - 2, y);
Controls.Add(body);
ClientSize = new Size(ClientSize.Width, desiredTotalH);
}
}
private void SetWinPowerFeedback(string msg, bool ok)
{
Label target = _lblRyzenResult ?? _lblWinPowerStatus;
if (target != null)
{
target.Text = msg;
target.ForeColor = ok ? AccentGreen : AccentAmber;
}
}
private void ApplyCpuBoost(CpuBoostMode mode)
{
bool ok = SystemTuning.SetCpuBoostMode(mode);
SetWinPowerFeedback(ok ? ("CPU Turbo Boost set to " + mode + ".") : "Failed to set CPU Turbo Boost.", ok);
RefreshWinPower();
}
private void ApplyWinPower(WindowsPowerMode mode)
{
bool ok = SystemTuning.SetWindowsPowerMode(mode);
SetWinPowerFeedback(ok ? ("Windows Power Mode set to " + mode + ".") : "Failed to set Windows Power Mode.", ok);
RefreshWinPower();
}
private void LoadState()
{
if (_cats.ShowRyzenTuning && _chkCo != null)
{
RyzenSnapshotSettings s = _settings.GetSnapshot();
_loading = true;
try
{
_chkCo.Checked = s.CoEnabled;
_sldCo.SetValueSilently(s.CoAllCore);
_lblCoVal.Text = _sldCo.Value.ToString();
_chkTdp.Checked = s.TdpEnabled;
_sldTdp.SetValueSilently(s.TdpWatts);
_lblTdpVal.Text = _sldTdp.Value + " W";
_chkTemp.Checked = s.TempEnabled;
_sldTemp.SetValueSilently(s.TempLimitC);
_lblTempVal.Text = _sldTemp.Value + " °C";
_chkAutoApply.Checked = s.AutoApplyOnStartup;
}
finally
{
_loading = false;
}
if (s.CrashGuardTriggered)
{
_lblRyzenResult.Text = "Crash guard skipped auto-apply (previous SMU apply did not pass 60s check).";
_lblRyzenResult.ForeColor = AccentAmber;
}
}
if (_cats.ShowWindowsPower)
{
RefreshWinPower();
}
}
private void LoadPreset(bool coEn, int co, bool tdpEn, int tdp, bool tempEn, int temp, string customNote)
{
if (_chkCo == null) return;
_loading = true;
try
{
_chkCo.Checked = coEn;
_sldCo.SetValueSilently(co);
_lblCoVal.Text = _sldCo.Value.ToString();
_chkTdp.Checked = tdpEn;
_sldTdp.SetValueSilently(tdp);
_lblTdpVal.Text = _sldTdp.Value + " W";
_chkTemp.Checked = tempEn;
_sldTemp.SetValueSilently(temp);
_lblTempVal.Text = _sldTemp.Value + " °C";
}
finally
{
_loading = false;
}
_lblRyzenResult.Text = customNote ?? "Preset loaded — click 'Apply Ryzen Tuning' to send to SMU.";
_lblRyzenResult.ForeColor = AccentBlue;
}
private void RunPawnIoInstall()
{
if (_btnInstallPawn == null || _installingPawnIo) return;
_installingPawnIo = true;
_btnInstallPawn.Enabled = false;
_btnInstallPawn.Text = "Installing...";
_lblPawnStatus.ForeColor = AccentBlue;
_lblPawnStatus.Text = "Downloading pinned PawnIO v2.2.0...";
ThreadPool.QueueUserWorkItem(delegate
{
string err = RyzenSmuController.InstallPawnIo(msg =>
{
if (!IsDisposed && IsHandleCreated)
{
try { BeginInvoke((Action)(() => { if (!IsDisposed && _lblPawnStatus != null) _lblPawnStatus.Text = msg; })); } catch { }
}
});
if (!IsDisposed && IsHandleCreated)
{
try
{
BeginInvoke((Action)(() =>
{
_installingPawnIo = false;
if (IsDisposed || _lblPawnStatus == null) return;
if (err == null)
{
string ver = RyzenSmuController.GetInstalledPawnIoVersion() ?? "installed";
_lblPawnStatus.Text = "PawnIO v" + ver + " (WHQL-signed, HVCI safe) — Ready";
_lblPawnStatus.ForeColor = AccentGreen;
_btnInstallPawn.Visible = false;
_lblRyzenResult.Text = "PawnIO installed & verified! You can apply Ryzen CO & TDP tuning now.";
_lblRyzenResult.ForeColor = AccentGreen;
}
else
{
_lblPawnStatus.Text = "PawnIO install failed";
_lblPawnStatus.ForeColor = AccentAmber;
_lblRyzenResult.Text = err;
_lblRyzenResult.ForeColor = AccentAmber;
_btnInstallPawn.Text = "Retry Install";
_btnInstallPawn.Enabled = true;
}
}));
}
catch
{
_installingPawnIo = false;
}
}
else
{
_installingPawnIo = false;
}
});
}
private void ApplyRyzenTuning()
{
if (_chkCo == null) return;
if (!_chkCo.Checked && !_chkTdp.Checked && !_chkTemp.Checked)
{
if (!_chkAutoApply.Checked)
{
_settings.UpdateRyzenPreferences(false, false, _sldCo.Value, false, _sldTdp.Value, false, _sldTemp.Value);
string disErr;
if (_settings.Save(out disErr))
{
_lblRyzenResult.Text = "Auto-apply disabled; check CO, TDP, or Temp to apply SMU values.";
_lblRyzenResult.ForeColor = TextMuted;
}
else
{
_lblRyzenResult.Text = "Failed to save settings: " + disErr;
_lblRyzenResult.ForeColor = AccentAmber;
}
return;
}
_lblRyzenResult.Text = "Check at least one option (CO, TDP, or Temp) to apply.";
_lblRyzenResult.ForeColor = AccentAmber;
return;
}
RyzenApplyResult res = _settings.ApplyWithCrashGuard(
_smu,
_chkCo.Checked ? (int?)_sldCo.Value : null,
_chkTdp.Checked ? (int?)_sldTdp.Value : null,
_chkTemp.Checked ? (int?)_sldTemp.Value : null);
bool enableAuto = _chkAutoApply.Checked && res.Ok;
if (_chkAutoApply.Checked && !res.Ok)
{
_loading = true;
_chkAutoApply.Checked = false;
_loading = false;
}
_settings.UpdateRyzenPreferences(
enableAuto,
_chkCo.Checked, _sldCo.Value,
_chkTdp.Checked, _sldTdp.Value,
_chkTemp.Checked, _sldTemp.Value);
string saveErr;
bool saved = _settings.Save(out saveErr);
if (!res.Ok)
{
_lblRyzenResult.Text = res.Summary;
_lblRyzenResult.ForeColor = AccentAmber;
}
else if (!saved)
{
_lblRyzenResult.Text = "Applied (" + res.Summary + "), but settings save failed: " + saveErr;
_lblRyzenResult.ForeColor = AccentAmber;
}
else
{
_lblRyzenResult.Text = "Applied: " + res.Summary + (enableAuto ? " (60s crash-guard active)" : "");
_lblRyzenResult.ForeColor = AccentGreen;
}
}
private void RefreshWinPower()
{
if (_btnBoostOff == null) return;
CpuBoostMode b = SystemTuning.GetCpuBoostMode();
StyleButton(_btnBoostOff, b == CpuBoostMode.Disabled, AccentGreen);
StyleButton(_btnBoostEff, b == CpuBoostMode.EfficientEnabled || b == CpuBoostMode.EfficientAggressive, AccentBlue);
StyleButton(_btnBoostAgg, b == CpuBoostMode.Aggressive || b == CpuBoostMode.Enabled, AccentRed);
WindowsPowerMode p = SystemTuning.GetWindowsPowerMode();
StyleButton(_btnPwrEff, p == WindowsPowerMode.BestPowerEfficiency, AccentGreen);
StyleButton(_btnPwrBal, p == WindowsPowerMode.Balanced, AccentBlue);
StyleButton(_btnPwrPerf, p == WindowsPowerMode.BestPerformance, AccentRed);
}
private void UpdateManualFanLabels()
{
if (_lblFanManualVal != null && _sldFanManual != null)
{
if (_sldFanManual.IsDragging)
{
int val = _sldFanManual.Value;
Color accent = val >= 135 ? AccentRed : (val > 100 ? AccentAmber : AccentBlue);
_sldFanManual.SetAccentColor(accent);
_lblFanManualVal.ForeColor = accent;
_lblFanManualVal.Text = val > 100
? string.Format("Manual: {0}% (Boost)", val)
: string.Format("Manual: {0}%", val);
}
else if (_liveCoolerBoost)
{
_sldFanManual.SetValueSilently(MsiAcpi.MaxFanSpeedPct);
_sldFanManual.SetAccentColor(AccentRed);
_lblFanManualVal.ForeColor = AccentRed;
_lblFanManualVal.Text = "Cooler Boost (Max)";
}
else if (_liveFanMode == FanMode.Auto)
{
int shown = _liveDutyPct >= 0
? Math.Max(0, Math.Min(MsiAcpi.MaxFanSpeedPct, _liveDutyPct))
: _sldFanManual.Value;
_sldFanManual.SetValueSilently(shown);
_sldFanManual.SetAccentColor(AccentGreen);
_lblFanManualVal.ForeColor = AccentGreen;
_lblFanManualVal.Text = string.Format("Auto ({0}%)", shown);
}
else if (_liveFanMode == FanMode.Silent)
{
int shown = _liveDutyPct >= 0
? Math.Max(0, Math.Min(MsiAcpi.MaxFanSpeedPct, _liveDutyPct))
: _sldFanManual.Value;
_sldFanManual.SetValueSilently(shown);
_sldFanManual.SetAccentColor(AccentGreen);
_lblFanManualVal.ForeColor = AccentGreen;
_lblFanManualVal.Text = string.Format("Silent ({0}%)", shown);
}
else
{
int val = _sldFanManual.Value;
Color accent = val >= 135 ? AccentRed : (val > 100 ? AccentAmber : AccentBlue);
_sldFanManual.SetAccentColor(accent);
_lblFanManualVal.ForeColor = accent;
_lblFanManualVal.Text = val > 100
? string.Format("Manual: {0}% (Boost)", val)
: string.Format("Manual: {0}%", val);
}
}
if (_btnResetAuto != null)
{
bool autoActive = _liveFanMode == FanMode.Auto && !_liveCoolerBoost;
_btnResetAuto.Text = autoActive ? "Auto Fan Control (Active)" : "Switch Back to Auto";
StyleButton(_btnResetAuto, autoActive, AccentGreen);
}
if (_lblCpuRpmLive != null)
{
_lblCpuRpmLive.Text = "CPU Fan: " + (_liveCpuRpm >= 0 ? (_liveCpuRpm + " RPM") : "-- RPM");
}
if (_lblGpuRpmLive != null)
{
_lblGpuRpmLive.Text = "GPU Fan: " + (_liveGpuRpm >= 0 ? (_liveGpuRpm + " RPM") : "-- RPM");
}
}
private void RefreshFanRpmsAsync()
{
if (_acpi == null || IsDisposed || !IsHandleCreated) return;
if (Interlocked.CompareExchange(ref _rpmReadInFlight, 1, 0) != 0) return;
ThreadPool.QueueUserWorkItem(delegate
{
try
{
int c, g, duty;
FanMode fm;
bool cb;
if (_acpi.TryReadFanLiveState(out c, out g, out duty, out fm, out cb) && !IsDisposed && IsHandleCreated)
{
try
{
BeginInvoke((Action)(() =>
{
if (IsDisposed) return;
_liveCpuRpm = c;
_liveGpuRpm = g;
_liveDutyPct = duty;
_liveFanMode = fm;
_liveCoolerBoost = cb;
UpdateManualFanLabels();
}));
}
catch { }
}
}
finally
{
Interlocked.Exchange(ref _rpmReadInFlight, 0);
}
});
}
internal void SetFanTab(bool manualMode)
{
_fanManualMode = manualMode;
if (_pnlFanManual != null) _pnlFanManual.Visible = manualMode;
if (_pnlFanCurve != null) _pnlFanCurve.Visible = !manualMode;
if (_btnFanTabManual != null) StyleButton(_btnFanTabManual, manualMode, AccentBlue);