From f0b98f959218a08de4eba61c721a663a1c60432b Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Wed, 15 Jan 2025 19:45:44 +1100 Subject: [PATCH] Lots of optimisations + regression fix - Use List instead of array for most YAMDCC config elements - Remove unnecessary string copies from FanCurveConf's Copy method - Combine profile add/delete, config apply/revert, and fan speed change methods - Shorten some config editor method names - Fix config editor not exiting after starting EC-to-config - Fix apply buttons not getting disabled when applying configs using tool strip menu or keyboard shortcut (which triggers the tool strip menu item) - Fix buggy curve editor while adding/removing curve points --- YAMDCC.Common/Dialogs/CrashDialog.cs | 9 +- YAMDCC.Common/Dialogs/ProgressDialog.cs | 8 +- YAMDCC.Common/Utils.cs | 7 +- YAMDCC.Config/FanConf.cs | 3 +- YAMDCC.Config/FanCurveConf.cs | 11 +- YAMDCC.Config/FanModeConf.cs | 3 +- YAMDCC.Config/PerfModeConf.cs | 3 +- YAMDCC.Config/YAMDCC_Config.cs | 35 +- YAMDCC.ConfigEditor/MainWindow.Designer.cs | 58 +- YAMDCC.ConfigEditor/MainWindow.cs | 703 +++++++++------------ YAMDCC.ConfigEditor/MainWindow.resx | 58 +- YAMDCC.ConfigEditor/Program.cs | 61 +- YAMDCC.ConfigEditor/Status.cs | 4 +- YAMDCC.ConfigEditor/Strings.resx | 4 +- YAMDCC.Service/FanControlService.cs | 31 +- 15 files changed, 413 insertions(+), 585 deletions(-) diff --git a/YAMDCC.Common/Dialogs/CrashDialog.cs b/YAMDCC.Common/Dialogs/CrashDialog.cs index a685f5e..cee8628 100644 --- a/YAMDCC.Common/Dialogs/CrashDialog.cs +++ b/YAMDCC.Common/Dialogs/CrashDialog.cs @@ -37,14 +37,7 @@ private void btnReportIssue_Click(object sender, EventArgs e) private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetText(txtReport.Text); - - // should never fail, but better safe than sorry - // (this is the crash handling dialog after all) - if (sender is Button b) - { - // give confirmation that the crash report has been copied - b.Text = "Copied!"; - } + ((Button)sender).Text = "Copied!"; } private void btnExit_Click(object sender, EventArgs e) diff --git a/YAMDCC.Common/Dialogs/ProgressDialog.cs b/YAMDCC.Common/Dialogs/ProgressDialog.cs index ff5acef..95f5969 100644 --- a/YAMDCC.Common/Dialogs/ProgressDialog.cs +++ b/YAMDCC.Common/Dialogs/ProgressDialog.cs @@ -104,11 +104,11 @@ public ProgressDialog( SetTitle(Caption); // event setup - Worker.DoWork += Worker_DoWork; - Worker.RunWorkerCompleted += Worker_RunWorkerCompleted; + Worker.DoWork += new DoWorkEventHandler(Worker_DoWork); + Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted); if (reportsProgress) { - Worker.ProgressChanged += Worker_ProgressChanged; + Worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged); } else { @@ -127,7 +127,7 @@ public ProgressDialog( } DisplayTimer.Interval = 1000; - DisplayTimer.Tick += DisplayTimer_Tick; + DisplayTimer.Tick += new EventHandler(DisplayTimer_Tick); } private void ProgressDialog_Load(object sender, EventArgs e) diff --git a/YAMDCC.Common/Utils.cs b/YAMDCC.Common/Utils.cs index d0e6d86..51435a9 100644 --- a/YAMDCC.Common/Utils.cs +++ b/YAMDCC.Common/Utils.cs @@ -460,15 +460,10 @@ public static string GetPCManufacturer() private static string GetBIOSRegValue(string name) { - RegistryKey biosKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS"); - try + using (RegistryKey biosKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS")) { return (string)biosKey?.GetValue(name, null); } - finally - { - biosKey?.Close(); - } } } diff --git a/YAMDCC.Config/FanConf.cs b/YAMDCC.Config/FanConf.cs index 1a73645..44c5a17 100644 --- a/YAMDCC.Config/FanConf.cs +++ b/YAMDCC.Config/FanConf.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System.Collections.Generic; using System.Xml.Serialization; namespace YAMDCC.Config; @@ -95,5 +96,5 @@ public sealed class FanConf /// otherwise at least one fan curve (the "default" curve) must exist. /// [XmlArray] - public FanCurveConf[] FanCurveConfs { get; set; } + public List FanCurveConfs { get; set; } } diff --git a/YAMDCC.Config/FanCurveConf.cs b/YAMDCC.Config/FanCurveConf.cs index 6a313e5..e882bcc 100644 --- a/YAMDCC.Config/FanCurveConf.cs +++ b/YAMDCC.Config/FanCurveConf.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System.Collections.Generic; using System.Xml.Serialization; namespace YAMDCC.Config; @@ -39,7 +40,7 @@ public sealed class FanCurveConf /// The fan speeds and associated up and down thresholds. /// [XmlArray] - public TempThreshold[] TempThresholds { get; set; } + public List TempThresholds { get; set; } /// /// Creates a deep copy of this . @@ -53,12 +54,10 @@ public FanCurveConf Copy() FanCurveConf newCfg = (FanCurveConf)MemberwiseClone(); // create a copy of everything that didn't get copied by the above - newCfg.Name = string.Copy(Name); - newCfg.Desc = string.Copy(Desc); - newCfg.TempThresholds = new TempThreshold[TempThresholds.Length]; - for (int i = 0; i < newCfg.TempThresholds.Length; i++) + newCfg.TempThresholds = new List(TempThresholds.Count); + for (int i = 0; i < TempThresholds.Count; i++) { - newCfg.TempThresholds[i] = TempThresholds[i].Copy(); + newCfg.TempThresholds.Add(TempThresholds[i].Copy()); } return newCfg; } diff --git a/YAMDCC.Config/FanModeConf.cs b/YAMDCC.Config/FanModeConf.cs index 8a5e36c..4fb59a7 100644 --- a/YAMDCC.Config/FanModeConf.cs +++ b/YAMDCC.Config/FanModeConf.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System.Collections.Generic; using System.Xml.Serialization; namespace YAMDCC.Config; @@ -41,5 +42,5 @@ public sealed class FanModeConf /// An array of possible fan modes for the laptop. /// [XmlArray] - public FanMode[] FanModes { get; set; } + public List FanModes { get; set; } } diff --git a/YAMDCC.Config/PerfModeConf.cs b/YAMDCC.Config/PerfModeConf.cs index f4d28f5..a05d74b 100644 --- a/YAMDCC.Config/PerfModeConf.cs +++ b/YAMDCC.Config/PerfModeConf.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . +using System.Collections.Generic; using System.Xml.Serialization; namespace YAMDCC.Config; @@ -41,5 +42,5 @@ public sealed class PerfModeConf /// An array of possible performance modes for the laptop. /// [XmlArray] - public PerfMode[] PerfModes { get; set; } + public List PerfModes { get; set; } } diff --git a/YAMDCC.Config/YAMDCC_Config.cs b/YAMDCC.Config/YAMDCC_Config.cs index 5975597..9c717c7 100644 --- a/YAMDCC.Config/YAMDCC_Config.cs +++ b/YAMDCC.Config/YAMDCC_Config.cs @@ -15,6 +15,7 @@ // YAMDCC. If not, see . using System; +using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Serialization; @@ -69,7 +70,7 @@ public sealed class YAMDCC_Config /// The list of s associated with the laptop. /// [XmlArray] - public FanConf[] FanConfs { get; set; } + public List FanConfs { get; set; } /// /// The laptop's Full Blast config. @@ -132,7 +133,7 @@ public sealed class YAMDCC_Config /// May be null or empty if not needed. /// [XmlArray] - public RegConf[] RegConfs { get; set; } + public List RegConfs { get; set; } /// /// Parses a YAMDCC config XML and returns a @@ -209,12 +210,12 @@ private bool IsValid() // 1. Check if FanConfigs is not null // 2. Check if there's at least 1 FanConfig - if (FanConfs?.Length < 1) + if (FanConfs?.Count < 1) { return false; } - for (int i = 0; i < FanConfs.Length; i++) + for (int i = 0; i < FanConfs.Count; i++) { FanConf cfg = FanConfs[i]; @@ -232,7 +233,7 @@ private bool IsValid() // the selected fan curve shouldn't be higher than // the number of fan curves in the config. - if (cfg.CurveSel >= FanConfs[i].FanCurveConfs.Length || + if (cfg.CurveSel >= FanConfs[i].FanCurveConfs.Count || cfg.CurveSel < 0) { // if the fan profile selection is out of range, @@ -251,24 +252,24 @@ private bool IsValid() if (cfg.UpThresholdRegs?.Length < 1 || cfg.UpThresholdRegs?.Length != cfg.DownThresholdRegs?.Length || cfg.FanCurveRegs?.Length != cfg.UpThresholdRegs?.Length + 1 || - cfg.FanCurveConfs?.Length < 1) + cfg.FanCurveConfs?.Count < 1) { return false; } - for (int j = 0; j < cfg.FanCurveConfs.Length; j++) + for (int j = 0; j < cfg.FanCurveConfs.Count; j++) { FanCurveConf curveCfg = cfg.FanCurveConfs[j]; if (string.IsNullOrEmpty(curveCfg.Name) || string.IsNullOrEmpty(curveCfg.Desc) || // there should be exactly one temperature threshold // per fan curve register; if there isn't, return false - curveCfg.TempThresholds?.Length != cfg.FanCurveRegs.Length) + curveCfg.TempThresholds?.Count != cfg.FanCurveRegs.Length) { return false; } - for (int k = 0; k < curveCfg.TempThresholds.Length; k++) + for (int k = 0; k < curveCfg.TempThresholds.Count; k++) { if (curveCfg.TempThresholds[k] is null) { @@ -311,14 +312,14 @@ private bool IsValid() if (PerfModeConf is not null) { - if (PerfModeConf.PerfModes?.Length < 1) + if (PerfModeConf.PerfModes?.Count < 1) { return false; } // the selected performance mode shouldn't be higher than // the number of performance modes in the config - if (PerfModeConf.ModeSel >= PerfModeConf.PerfModes.Length || + if (PerfModeConf.ModeSel >= PerfModeConf.PerfModes.Count || PerfModeConf.ModeSel < 0) { // same as fan profile selection, set the performance @@ -326,7 +327,7 @@ private bool IsValid() PerfModeConf.ModeSel = 0; } - for (int i = 0; i < PerfModeConf.PerfModes.Length; i++) + for (int i = 0; i < PerfModeConf.PerfModes.Count; i++) { PerfMode perfMode = PerfModeConf.PerfModes[i]; @@ -340,19 +341,19 @@ private bool IsValid() if (FanModeConf is not null) { - if (FanModeConf.FanModes?.Length < 1) + if (FanModeConf.FanModes?.Count < 1) { return false; } // you know the drill by now - if (FanModeConf.ModeSel >= FanModeConf.FanModes.Length || + if (FanModeConf.ModeSel >= FanModeConf.FanModes.Count || FanModeConf.ModeSel < 0) { FanModeConf.ModeSel = 0; } - for (int i = 0; i < FanModeConf.FanModes.Length; i++) + for (int i = 0; i < FanModeConf.FanModes.Count; i++) { FanMode fanMode = FanModeConf.FanModes[i]; @@ -374,9 +375,9 @@ private bool IsValid() return false; } - if (RegConfs?.Length > 0) + if (RegConfs?.Count > 0) { - for (int i = 0; i < RegConfs.Length; i++) + for (int i = 0; i < RegConfs.Count; i++) { if (string.IsNullOrEmpty(RegConfs[i].Name) || string.IsNullOrEmpty(RegConfs[i].Desc)) diff --git a/YAMDCC.ConfigEditor/MainWindow.Designer.cs b/YAMDCC.ConfigEditor/MainWindow.Designer.cs index 19250b4..db0f44c 100644 --- a/YAMDCC.ConfigEditor/MainWindow.Designer.cs +++ b/YAMDCC.ConfigEditor/MainWindow.Designer.cs @@ -79,7 +79,7 @@ private void InitializeComponent() this.tsiOptions = new System.Windows.Forms.ToolStripMenuItem(); this.tsiProfAdd = new System.Windows.Forms.ToolStripMenuItem(); this.tsiProfEdit = new System.Windows.Forms.ToolStripMenuItem(); - this.tsiProfRename = new System.Windows.Forms.ToolStripMenuItem(); + this.tsiProfRen = new System.Windows.Forms.ToolStripMenuItem(); this.tsiProfChangeDesc = new System.Windows.Forms.ToolStripMenuItem(); this.tsiProfDel = new System.Windows.Forms.ToolStripMenuItem(); this.tsiSwitchAll = new System.Windows.Forms.ToolStripMenuItem(); @@ -226,7 +226,7 @@ private void InitializeComponent() this.tsiApply.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); this.tsiApply.Size = new System.Drawing.Size(207, 22); this.tsiApply.Text = "&Apply changes"; - this.tsiApply.Click += new System.EventHandler(this.tsiApply_Click); + this.tsiApply.Click += new System.EventHandler(this.ApplyConf); // // tsiRevert // @@ -234,7 +234,7 @@ private void InitializeComponent() this.tsiRevert.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.R))); this.tsiRevert.Size = new System.Drawing.Size(207, 22); this.tsiRevert.Text = "&Revert changes"; - this.tsiRevert.Click += new System.EventHandler(this.tsiRevert_Click); + this.tsiRevert.Click += new System.EventHandler(this.RevertConf); // // sep2 // @@ -273,12 +273,12 @@ private void InitializeComponent() this.tsiProfAdd.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N))); this.tsiProfAdd.Size = new System.Drawing.Size(257, 22); this.tsiProfAdd.Text = "&New fan profile..."; - this.tsiProfAdd.Click += new System.EventHandler(this.tsiProfAdd_Click); + this.tsiProfAdd.Click += new System.EventHandler(this.ProfAdd); // // tsiProfEdit // this.tsiProfEdit.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.tsiProfRename, + this.tsiProfRen, this.tsiProfChangeDesc, sep3, this.tsiProfDel}); @@ -288,17 +288,17 @@ private void InitializeComponent() // // tsiProfRename // - this.tsiProfRename.Name = "tsiProfRename"; - this.tsiProfRename.Size = new System.Drawing.Size(180, 22); - this.tsiProfRename.Text = "Change Name"; - this.tsiProfRename.Click += new System.EventHandler(this.tsiProfRename_Click); + this.tsiProfRen.Name = "tsiProfRename"; + this.tsiProfRen.Size = new System.Drawing.Size(180, 22); + this.tsiProfRen.Text = "Change Name"; + this.tsiProfRen.Click += new System.EventHandler(this.ProfRename); // // tsiProfChangeDesc // this.tsiProfChangeDesc.Name = "tsiProfChangeDesc"; this.tsiProfChangeDesc.Size = new System.Drawing.Size(180, 22); this.tsiProfChangeDesc.Text = "Change Description"; - this.tsiProfChangeDesc.Click += new System.EventHandler(this.tsiProfChangeDesc_Click); + this.tsiProfChangeDesc.Click += new System.EventHandler(this.ProfChangeDesc); // // sep3 // @@ -311,21 +311,21 @@ private void InitializeComponent() this.tsiProfDel.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Delete))); this.tsiProfDel.Size = new System.Drawing.Size(180, 22); this.tsiProfDel.Text = "Delete"; - this.tsiProfDel.Click += new System.EventHandler(this.tsiProfDel_Click); + this.tsiProfDel.Click += new System.EventHandler(this.ProfDel); // // tsiSwitchAll // this.tsiSwitchAll.Name = "tsiSwitchAll"; this.tsiSwitchAll.Size = new System.Drawing.Size(257, 22); this.tsiSwitchAll.Text = "Switch all fan profiles"; - this.tsiSwitchAll.Click += new System.EventHandler(this.tsiSwitchAll_Click); + this.tsiSwitchAll.Click += new System.EventHandler(this.SwitchAllToggle); // // tsiECtoConf // this.tsiECtoConf.Name = "tsiECtoConf"; this.tsiECtoConf.Size = new System.Drawing.Size(257, 22); this.tsiECtoConf.Text = "Get &default fan profile from EC..."; - this.tsiECtoConf.Click += new System.EventHandler(this.tsiECtoConf_Click); + this.tsiECtoConf.Click += new System.EventHandler(this.ECtoConf); // // sep4 // @@ -339,14 +339,14 @@ private void InitializeComponent() this.tsiECMon.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.M))); this.tsiECMon.Size = new System.Drawing.Size(257, 22); this.tsiECMon.Text = "Enable EC &monitoring"; - this.tsiECMon.Click += new System.EventHandler(this.tsiECMon_Click); + this.tsiECMon.Click += new System.EventHandler(this.ECMonToggle); // // tsiAdvanced // this.tsiAdvanced.Name = "tsiAdvanced"; this.tsiAdvanced.Size = new System.Drawing.Size(257, 22); this.tsiAdvanced.Text = "Show advanced settings"; - this.tsiAdvanced.Click += new System.EventHandler(this.tsiAdvanced_Click); + this.tsiAdvanced.Click += new System.EventHandler(this.AdvancedToggle); // // sep5 // @@ -548,7 +548,7 @@ private void InitializeComponent() this.cboFanSel.Name = "cboFanSel"; this.cboFanSel.Size = new System.Drawing.Size(119, 23); this.cboFanSel.TabIndex = 1; - this.cboFanSel.SelectedIndexChanged += new System.EventHandler(this.cboFanSel_IndexChanged); + this.cboFanSel.SelectedIndexChanged += new System.EventHandler(this.FanSelChanged); // // lblProfSel // @@ -570,7 +570,7 @@ private void InitializeComponent() this.cboProfSel.Name = "cboProfSel"; this.cboProfSel.Size = new System.Drawing.Size(119, 23); this.cboProfSel.TabIndex = 3; - this.cboProfSel.SelectedIndexChanged += new System.EventHandler(this.cboProfSel_IndexChanged); + this.cboProfSel.SelectedIndexChanged += new System.EventHandler(this.ProfSelChanged); // // btnProfAdd // @@ -581,7 +581,7 @@ private void InitializeComponent() this.btnProfAdd.TabIndex = 4; this.btnProfAdd.Text = "+"; this.btnProfAdd.UseVisualStyleBackColor = true; - this.btnProfAdd.Click += new System.EventHandler(this.btnProfAdd_Click); + this.btnProfAdd.Click += new System.EventHandler(this.ProfAdd); this.btnProfAdd.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.btnProfAdd_KeyPress); // // btnProfDel @@ -593,7 +593,7 @@ private void InitializeComponent() this.btnProfDel.TabIndex = 5; this.btnProfDel.Text = "-"; this.btnProfDel.UseVisualStyleBackColor = true; - this.btnProfDel.Click += new System.EventHandler(this.btnProfDel_Click); + this.btnProfDel.Click += new System.EventHandler(this.ProfDel); // // tabExtra // @@ -643,7 +643,7 @@ private void InitializeComponent() this.cboFanMode.Name = "cboFanMode"; this.cboFanMode.Size = new System.Drawing.Size(150, 23); this.cboFanMode.TabIndex = 9; - this.cboFanMode.SelectedIndexChanged += new System.EventHandler(this.cboFanMode_IndexChanged); + this.cboFanMode.SelectedIndexChanged += new System.EventHandler(this.FanModeChange); // // lblFanMode // @@ -688,7 +688,7 @@ private void InitializeComponent() this.cboPerfMode.Name = "cboPerfMode"; this.cboPerfMode.Size = new System.Drawing.Size(150, 23); this.cboPerfMode.TabIndex = 3; - this.cboPerfMode.SelectedIndexChanged += new System.EventHandler(this.cboPerfMode_IndexChanged); + this.cboPerfMode.SelectedIndexChanged += new System.EventHandler(this.PerfModeChange); // // lblWinFnSwap // @@ -712,7 +712,7 @@ private void InitializeComponent() this.chkWinFnSwap.TabIndex = 5; this.chkWinFnSwap.Text = "Enabled"; this.chkWinFnSwap.UseVisualStyleBackColor = true; - this.chkWinFnSwap.CheckedChanged += new System.EventHandler(this.chkWinFnSwap_Toggled); + this.chkWinFnSwap.CheckedChanged += new System.EventHandler(this.WinFnSwapToggle); // // lblKeyLight // @@ -756,7 +756,7 @@ private void InitializeComponent() this.tbKeyLight.Size = new System.Drawing.Size(150, 45); this.tbKeyLight.TabIndex = 1; this.tbKeyLight.TickStyle = System.Windows.Forms.TickStyle.Both; - this.tbKeyLight.Scroll += new System.EventHandler(this.tbKeyLight_Scroll); + this.tbKeyLight.Scroll += new System.EventHandler(this.KeyLightChange); // // lblKeyLightHigh // @@ -790,7 +790,7 @@ private void InitializeComponent() this.chkChgLim.TabIndex = 0; this.chkChgLim.Text = "Enabled"; this.chkChgLim.UseVisualStyleBackColor = true; - this.chkChgLim.CheckedChanged += new System.EventHandler(this.chkChgLim_CheckedChanged); + this.chkChgLim.CheckedChanged += new System.EventHandler(this.ChgLimToggle); // // numChgLim // @@ -799,7 +799,7 @@ private void InitializeComponent() this.numChgLim.Name = "numChgLim"; this.numChgLim.Size = new System.Drawing.Size(50, 23); this.numChgLim.TabIndex = 1; - this.numChgLim.ValueChanged += new System.EventHandler(this.numChgLim_Changed); + this.numChgLim.ValueChanged += new System.EventHandler(this.ChgLimChange); // // tabInfo // @@ -935,7 +935,7 @@ private void InitializeComponent() this.chkFullBlast.TabIndex = 0; this.chkFullBlast.Text = "Full Blast"; this.chkFullBlast.UseVisualStyleBackColor = true; - this.chkFullBlast.CheckedChanged += new System.EventHandler(this.chkFullBlast_Toggled); + this.chkFullBlast.CheckedChanged += new System.EventHandler(this.FullBlastToggle); // // btnRevert // @@ -947,7 +947,7 @@ private void InitializeComponent() this.btnRevert.TabIndex = 1; this.btnRevert.Text = "Revert"; this.btnRevert.UseVisualStyleBackColor = true; - this.btnRevert.Click += new System.EventHandler(this.btnRevert_Click); + this.btnRevert.Click += new System.EventHandler(this.RevertConf); // // btnApply // @@ -958,7 +958,7 @@ private void InitializeComponent() this.btnApply.TabIndex = 2; this.btnApply.Text = "Apply"; this.btnApply.UseVisualStyleBackColor = true; - this.btnApply.Click += new System.EventHandler(this.btnApply_Click); + this.btnApply.Click += new System.EventHandler(this.ApplyConf); // // flwStats // @@ -1091,7 +1091,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem tsiOptions; private System.Windows.Forms.ToolStripMenuItem tsiProfAdd; private System.Windows.Forms.ToolStripMenuItem tsiProfEdit; - private System.Windows.Forms.ToolStripMenuItem tsiProfRename; + private System.Windows.Forms.ToolStripMenuItem tsiProfRen; private System.Windows.Forms.ToolStripMenuItem tsiProfChangeDesc; private System.Windows.Forms.ToolStripMenuItem tsiProfDel; private System.Windows.Forms.ToolStripMenuItem tsiECtoConf; diff --git a/YAMDCC.ConfigEditor/MainWindow.cs b/YAMDCC.ConfigEditor/MainWindow.cs index e6557af..5c6e4d6 100644 --- a/YAMDCC.ConfigEditor/MainWindow.cs +++ b/YAMDCC.ConfigEditor/MainWindow.cs @@ -74,7 +74,7 @@ public MainWindow() tsiRevert.ToolTipText = Strings.GetString("ttRevert"); tsiExit.ToolTipText = Strings.GetString("ttExit"); tsiProfAdd.ToolTipText = Strings.GetString("ttProfAdd"); - tsiProfRename.ToolTipText = Strings.GetString("ttProfRen"); + tsiProfRen.ToolTipText = Strings.GetString("ttProfRen"); tsiProfChangeDesc.ToolTipText = Strings.GetString("ttProfChangeDesc"); tsiSwitchAll.ToolTipText = Strings.GetString("ttSwitchAll"); tsiECtoConf.ToolTipText = Strings.GetString("ttECtoConf"); @@ -87,8 +87,8 @@ public MainWindow() tsiLogError.ToolTipText = Strings.GetString("ttLogLevel"); tsiLogFatal.ToolTipText = Strings.GetString("ttLogLevel"); tsiLogNone.ToolTipText = Strings.GetString("ttLogLevel"); - tsiStopSvc.ToolTipText = Strings.GetString("ttSvcStop"); - tsiUninstall.ToolTipText = Strings.GetString("ttSvcUninstall"); + tsiStopSvc.ToolTipText = Strings.GetString("ttStopSvc"); + tsiUninstall.ToolTipText = Strings.GetString("ttUninstall"); tsiAbout.ToolTipText = Strings.GetString("ttAbout"); tsiSource.ToolTipText = Strings.GetString("ttSource"); ttMain.SetToolTip(cboFanSel, Strings.GetString("ttFanSel")); @@ -101,19 +101,19 @@ public MainWindow() { Interval = 1000, }; - tmrPoll.Tick += tmrPoll_Tick; + tmrPoll.Tick += new EventHandler(tmrPoll_Tick); tmrStatusReset = new() { Interval = 5000, }; - tmrStatusReset.Tick += tmrStatusReset_Tick; + tmrStatusReset.Tick += new EventHandler(tmrStatusReset_Tick); tmrSvcTimeout = new() { Interval = 10000, }; - tmrSvcTimeout.Tick += tmrSvcTimeout_Tick; + tmrSvcTimeout.Tick += new EventHandler(tmrSvcTimeout_Tick); switch (CommonConfig.GetLogLevel()) { @@ -156,41 +156,34 @@ public MainWindow() try { - string path; - StreamReader sr = new(Paths.LastConf, Encoding.UTF8); - try - { - path = sr.ReadLine(); - CommonConfig.SetLastConf(path); - } - finally + using (StreamReader sr = new(Paths.LastConf, Encoding.UTF8)) { - sr.Close(); + CommonConfig.SetLastConf(sr.ReadLine()); } File.Delete(Paths.LastConf); } // legacy LastConf file doesn't exist catch (FileNotFoundException) { } - + DisableAll(); } #region Events private void MainWindow_Load(object sender, EventArgs e) { - IPCClient.ServerMessage += IPC_MessageReceived; - IPCClient.Error += IPCClient_Error; + IPCClient.ServerMessage += new EventHandler>(IPCMessage); + IPCClient.Error += new EventHandler>(IPCError); IPCClient.Start(); ProgressDialog dlg = new("Connecting to YAMDCC service...", (e) => e.Result = !IPCClient.WaitForConnection(5000)); dlg.ShowDialog(); - if (dlg.Result is bool b && b) + if ((bool)dlg.Result) { throw new TimeoutException(Strings.GetString("exSvcTimeout")); } - AppDomain.CurrentDomain.ProcessExit += OnProcessExit; + AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); LoadConf(Paths.CurrentConf); @@ -202,7 +195,7 @@ private void MainWindow_Load(object sender, EventArgs e) } else { - SendServiceMessage(new ServiceCommand(Command.GetKeyLightBright, string.Empty)); + SendSvcMessage(new ServiceCommand(Command.GetKeyLightBright, string.Empty)); } } @@ -224,7 +217,7 @@ private void MainWindow_Closing(object sender, FormClosingEventArgs e) // Disable Full Blast if it was enabled while the program was running: if (chkFullBlast.Checked) { - SendServiceMessage(new ServiceCommand(Command.FullBlast, "0")); + SendSvcMessage(new ServiceCommand(Command.FullBlast, "0")); } } @@ -236,11 +229,16 @@ private void OnProcessExit(object sender, EventArgs e) IPCClient.Stop(); } - private void IPC_MessageReceived(object sender, PipeMessageEventArgs e) + private void IPCMessage(object sender, PipeMessageEventArgs e) { tmrSvcTimeout.Stop(); string[] args = e.Message.Value.Split(' '); - if (args.Length == 1) + if (args.Length != 1) + { + return; + } + + Invoke(() => { switch (e.Message.Response) { @@ -251,7 +249,24 @@ private void IPC_MessageReceived(object sender, PipeMessageEventArgs e) + private void IPCError(object sender, PipeErrorEventArgs e) { CrashDialog dlg = new(e.Exception); dlg.ShowDialog(); } - private void HandleSuccessResponse(string[] args) - { - if (int.TryParse(args[0], out int value)) - { - Command cmd = (Command)value; - - switch (cmd) - { - case Command.ApplyConfig: - btnApply.Invoke(() => btnApply.Enabled = tsiApply.Enabled = true); - UpdateStatus(StatusCode.ConfApplySuccess); - if (Config.KeyLightConf is not null) - { - SendServiceMessage(new ServiceCommand(Command.GetKeyLightBright, "")); - } - break; - case Command.FullBlast: - UpdateStatus(StatusCode.FullBlastToggleSuccess); - break; - } - } - } - #region Tool strip menu items #region File @@ -382,16 +373,6 @@ private void tsiSaveConf_Click(object sender, EventArgs e) } } - private void tsiApply_Click(object sender, EventArgs e) - { - ApplyConf(); - } - - private void tsiRevert_Click(object sender, EventArgs e) - { - RevertConf(); - } - private void tsiExit_Click(object sender, EventArgs e) { Close(); @@ -399,12 +380,7 @@ private void tsiExit_Click(object sender, EventArgs e) #endregion #region Options - private void tsiProfAdd_Click(object sender, EventArgs e) - { - AddFanProfile(); - } - - private void tsiProfRename_Click(object sender, EventArgs e) + private void ProfRename(object sender, EventArgs e) { FanCurveConf curveCfg = Config.FanConfs[cboFanSel.SelectedIndex] .FanCurveConfs[cboProfSel.SelectedIndex]; @@ -420,10 +396,11 @@ private void tsiProfRename_Click(object sender, EventArgs e) } } - private void tsiProfChangeDesc_Click(object sender, EventArgs e) + private void ProfChangeDesc(object sender, EventArgs e) { FanCurveConf curveCfg = Config.FanConfs[cboFanSel.SelectedIndex] .FanCurveConfs[cboProfSel.SelectedIndex]; + TextInputDialog dlg = new( Strings.GetString("dlgProfChangeDesc"), "Change Profile Description", curveCfg.Desc, true); @@ -436,16 +413,11 @@ private void tsiProfChangeDesc_Click(object sender, EventArgs e) } } - private void tsiProfDel_Click(object sender, EventArgs e) - { - DelFanProfile(); - } - - private void tsiSwitchAll_Click(object sender, EventArgs e) + private void SwitchAllToggle(object sender, EventArgs e) { if (!tsiSwitchAll.Checked) { - if (FansHaveSameProfileCount()) + if (FansHaveSameProfCount()) { tsiSwitchAll.Checked = true; } @@ -461,16 +433,17 @@ private void tsiSwitchAll_Click(object sender, EventArgs e) } - private void tsiECtoConf_Click(object sender, EventArgs e) + private void ECtoConf(object sender, EventArgs e) { if (Utils.ShowInfo(Strings.GetString("dlgECtoConfStart"), "Default fan profile from EC?", MessageBoxButtons.YesNo) == DialogResult.Yes) { + Application.Exit(); CommonConfig.SetECtoConfState(ECtoConfState.PendingReboot); } } - private void tsiECMon_Click(object sender, EventArgs e) + private void ECMonToggle(object sender, EventArgs e) { if (tsiECMon.Checked) { @@ -489,7 +462,7 @@ private void tsiECMon_Click(object sender, EventArgs e) } } - private void tsiAdvanced_Click(object sender, EventArgs e) + private void AdvancedToggle(object sender, EventArgs e) { if (!tsiAdvanced.Checked && Utils.ShowWarning(Strings.GetString("dlgAdvanced"), "Show advanced settings?", MessageBoxDefaultButton.Button2) != DialogResult.Yes) @@ -656,7 +629,7 @@ private void tsiCheckUpdate_Click(object sender, EventArgs e) { try { - Process.Start("Updater", "--checkupdate"); + Process.Start("updater", "--checkupdate"); } catch (FileNotFoundException) { @@ -667,22 +640,22 @@ private void tsiCheckUpdate_Click(object sender, EventArgs e) #endregion - private void cboFanSel_IndexChanged(object sender, EventArgs e) + private void FanSelChanged(object sender, EventArgs e) { if (Config is not null) { - UpdateFanCurveDisplay(); + UpdateCurveEditor(); } } - private void cboProfSel_IndexChanged(object sender, EventArgs e) + private void ProfSelChanged(object sender, EventArgs e) { FanConf cfg = Config.FanConfs[cboFanSel.SelectedIndex]; FanCurveConf curveCfg = cfg.FanCurveConfs[cboProfSel.SelectedIndex]; if (tsiSwitchAll.Checked) { - for (int i = 0; i < Config.FanConfs.Length; i++) + for (int i = 0; i < Config.FanConfs.Count; i++) { Config.FanConfs[i].CurveSel = cboProfSel.SelectedIndex; } @@ -695,43 +668,82 @@ private void cboProfSel_IndexChanged(object sender, EventArgs e) ttMain.SetToolTip(cboProfSel, Strings.GetString( "ttProfSel", cfg.FanCurveConfs[cfg.CurveSel].Desc)); - int numTempThresholds = cfg.UpThresholdRegs.Length; - - // Fan curve for (int i = 0; i < numFanSpds.Length; i++) { - if (i <= numTempThresholds) - { - numFanSpds[i].Value = tbFanSpds[i].Value - = curveCfg.TempThresholds[i].FanSpeed; + // Fan curve + numFanSpds[i].Value = tbFanSpds[i].Value + = curveCfg.TempThresholds[i].FanSpeed; - numFanSpds[i].Enabled = tbFanSpds[i].Enabled = curveCfg.Name != "Default"; - } - } + numFanSpds[i].Enabled = tbFanSpds[i].Enabled = curveCfg.Name != "Default"; - // Temp thresholds - for (int i = 0; i < numUpTs.Length; i++) - { - if (i <= numTempThresholds) + // Temp thresholds + if (i < cfg.UpThresholdRegs.Length) { TempThreshold t = curveCfg.TempThresholds[i + 1]; numUpTs[i].Value = t.UpThreshold; numDownTs[i].Value = t.DownThreshold; - numUpTs[i].Enabled = numDownTs[i].Enabled = curveCfg.Name != "Default"; } - else - { - numUpTs[i].Enabled = numDownTs[i].Enabled = false; - } } btnApply.Enabled = tsiApply.Enabled = true; btnProfDel.Enabled = tsiProfDel.Enabled = curveCfg.Name != "Default"; } - private void btnProfAdd_Click(object sender, EventArgs e) + private void ProfAdd(object sender, EventArgs e) { - AddFanProfile(); + if (tsiSwitchAll.Checked) + { + bool switchAll = tsiSwitchAll.Checked; + tsiSwitchAll.Checked = false; + ProfAdd(0, cboFanSel.Items.Count); + tsiSwitchAll.Checked = switchAll; + } + else + { + ProfAdd(cboFanSel.SelectedIndex, cboFanSel.SelectedIndex + 1); + } + + if (!FansHaveSameProfCount()) + { + tsiSwitchAll.Checked = false; + } + + btnRevert.Enabled = tsiRevert.Enabled = true; + } + + private void ProfAdd(int start, int end) + { + FanConf cfg = Config.FanConfs[cboFanSel.SelectedIndex]; + string oldProfName = cfg.FanCurveConfs[cboProfSel.SelectedIndex].Name; + + TextInputDialog dlg = new( + Strings.GetString("dlgProfAdd"), + "New Profile", $"Copy of {oldProfName}"); + + if (dlg.ShowDialog() == DialogResult.OK) + { + for (int i = start; i < end; i++) + { + cfg = Config.FanConfs[i]; + + // Create a copy of the currently selected fan profile + // and add it to the config's list: + FanCurveConf oldCurveCfg = cfg.FanCurveConfs[cfg.CurveSel]; + cfg.FanCurveConfs.Add(oldCurveCfg.Copy()); + cfg.CurveSel = cfg.FanCurveConfs.Count - 1; + + // Name it according to what the user specified + cfg.FanCurveConfs[cfg.CurveSel].Name = dlg.Result; + cfg.FanCurveConfs[cfg.CurveSel].Desc = $"(Copy of {oldCurveCfg.Name})\n{oldCurveCfg.Desc}"; + + // Add the new fan profile to the UI's profile list and select it: + if (i == cboFanSel.SelectedIndex) + { + cboProfSel.Items.Add(dlg.Result); + cboProfSel.SelectedIndex = cfg.CurveSel; + } + } + } } private void btnProfAdd_KeyPress(object sender, KeyPressEventArgs e) @@ -768,29 +780,63 @@ private void btnProfAdd_KeyPress(object sender, KeyPressEventArgs e) } } - private void btnProfDel_Click(object sender, EventArgs e) + private void ProfDel(object sender, EventArgs e) { - DelFanProfile(); + if (tsiSwitchAll.Checked) + { + ProfDel(0, cboFanSel.Items.Count); + } + else + { + ProfDel(cboFanSel.SelectedIndex, cboFanSel.SelectedIndex + 1); + } + + if (!FansHaveSameProfCount()) + { + tsiSwitchAll.Checked = false; + } + + btnRevert.Enabled = tsiRevert.Enabled = true; } - private void numFanSpd_Changed(object sender, EventArgs e) + private void ProfDel(int start, int end) { - NumericUpDown nud = (NumericUpDown)sender; - int i = (int)nud.Tag; - tbFanSpds[i].Value = (int)numFanSpds[i].Value; + for (int i = start; i < end; i++) + { + FanConf cfg = Config.FanConfs[i]; + FanCurveConf curveCfg = cfg.FanCurveConfs[cfg.CurveSel]; - Config.FanConfs[cboFanSel.SelectedIndex] - .FanCurveConfs[cboProfSel.SelectedIndex] - .TempThresholds[i].FanSpeed = (byte)numFanSpds[i].Value; + if (curveCfg.Name != "Default" && Utils.ShowWarning( + Strings.GetString("dlgProfDel", curveCfg.Name), + $"Delete fan profile? ({cfg.Name})") == DialogResult.Yes) + { + // Remove the fan profile from the config's list + cfg.FanCurveConfs.RemoveAt(cfg.CurveSel); + cfg.CurveSel -= 1; - btnRevert.Enabled = tsiRevert.Enabled = true; + // Remove from the list client-side, and select a different fan profile + if (i == cboFanSel.SelectedIndex) + { + cboProfSel.Items.RemoveAt(cboProfSel.SelectedIndex); + cboProfSel.SelectedIndex = cfg.CurveSel; + } + } + } } - private void tbFanSpd_Scroll(object sender, EventArgs e) + private void FanSpdChange(object sender, EventArgs e) { - TrackBar tb = (TrackBar)sender; - int i = (int)tb.Tag; - numFanSpds[i].Value = tbFanSpds[i].Value; + Control c = (Control)sender; + int i = (int)c.Tag; + + if (c is NumericUpDown) + { + tbFanSpds[i].Value = (int)numFanSpds[i].Value; + } + else if (c is TrackBar) + { + numFanSpds[i].Value = tbFanSpds[i].Value; + } Config.FanConfs[cboFanSel.SelectedIndex] .FanCurveConfs[cboProfSel.SelectedIndex] @@ -799,7 +845,7 @@ private void tbFanSpd_Scroll(object sender, EventArgs e) btnRevert.Enabled = tsiRevert.Enabled = true; } - private void numUpT_Changed(object sender, EventArgs e) + private void UpTChange(object sender, EventArgs e) { NumericUpDown nud = (NumericUpDown)sender; int i = (int)nud.Tag; @@ -816,7 +862,7 @@ private void numUpT_Changed(object sender, EventArgs e) btnRevert.Enabled = tsiRevert.Enabled = true; } - private void numDownT_Changed(object sender, EventArgs e) + private void DownTChange(object sender, EventArgs e) { NumericUpDown nud = (NumericUpDown)sender; int i = (int)nud.Tag; @@ -828,12 +874,12 @@ private void numDownT_Changed(object sender, EventArgs e) btnRevert.Enabled = tsiRevert.Enabled = true; } - private void chkChgLim_CheckedChanged(object sender, EventArgs e) + private void ChgLimToggle(object sender, EventArgs e) { numChgLim.Enabled = chkChgLim.Checked; } - private void numChgLim_Changed(object sender, EventArgs e) + private void ChgLimChange(object sender, EventArgs e) { if (Config is not null) { @@ -841,7 +887,7 @@ private void numChgLim_Changed(object sender, EventArgs e) } } - private void cboPerfMode_IndexChanged(object sender, EventArgs e) + private void PerfModeChange(object sender, EventArgs e) { if (Config is not null) { @@ -853,18 +899,18 @@ private void cboPerfMode_IndexChanged(object sender, EventArgs e) } } - private void chkWinFnSwap_Toggled(object sender, EventArgs e) + private void WinFnSwapToggle(object sender, EventArgs e) { Config.KeySwapConf.Enabled = chkWinFnSwap.Checked; btnRevert.Enabled = tsiRevert.Enabled = true; } - private void tbKeyLight_Scroll(object sender, EventArgs e) + private void KeyLightChange(object sender, EventArgs e) { - SendServiceMessage(new ServiceCommand(Command.SetKeyLightBright, $"{tbKeyLight.Value}")); + SendSvcMessage(new ServiceCommand(Command.SetKeyLightBright, $"{tbKeyLight.Value}")); } - private void cboFanMode_IndexChanged(object sender, EventArgs e) + private void FanModeChange(object sender, EventArgs e) { if (Config is not null) { @@ -905,20 +951,54 @@ private void btnGetModel_Click(object sender, EventArgs e) } } - private void chkFullBlast_Toggled(object sender, EventArgs e) + private void FullBlastToggle(object sender, EventArgs e) { - SendServiceMessage(new ServiceCommand(Command.FullBlast, chkFullBlast.Checked ? "1" : "0")); + SendSvcMessage(new ServiceCommand(Command.FullBlast, chkFullBlast.Checked ? "1" : "0")); } - private void btnRevert_Click(object sender, EventArgs e) + private void RevertConf(object sender, EventArgs e) { - RevertConf(); + if (Utils.ShowWarning(Strings.GetString("dlgRevert"), + "Revert?") == DialogResult.Yes) + { + try + { + YAMDCC_Config tempConf = YAMDCC_Config.Load(CommonConfig.GetLastConf()); + LoadConf(tempConf); + Config = tempConf; + UpdateCurveEditor(); + ApplyConf(sender, e); + btnRevert.Enabled = tsiRevert.Enabled = false; + } + catch (Exception ex) + { + if (ex is FileNotFoundException) + { + Utils.ShowError(Strings.GetString("dlgOldConfMissing")); + } + else if (ex is InvalidConfigException or InvalidOperationException) + { + Utils.ShowError(Strings.GetString("dlgOldConfInvalid")); + } + else + { + throw; + } + } + } } - private void btnApply_Click(object sender, EventArgs e) + private void ApplyConf(object sender, EventArgs e) { btnApply.Enabled = tsiApply.Enabled = false; - ApplyConf(); + + // Save the updated config + Config.ChargeLimitConf.CurVal = (byte)(chkChgLim.Checked + ? numChgLim.Value : 0); + Config.Save(Paths.CurrentConf); + + // Tell the service to reload and apply the updated config + SendSvcMessage(new ServiceCommand(Command.ApplyConfig, null)); } private void tmrPoll_Tick(object sender, EventArgs e) @@ -941,33 +1021,6 @@ private void tmrSvcTimeout_Tick(object sender, EventArgs e) #endregion // Events #region Private methods - private void UpdateFanMon(int value, int i) - { - switch (i) - { - case 0: - lblTemp.Invoke(new Action(delegate - { - lblTemp.Text = $"Temp: {value}°C"; - })); - break; - case 1: - lblFanSpd.Invoke(new Action(delegate - { - lblFanSpd.Text = $"Fan speed: {value}%"; - })); - break; - case 2: - lblFanRPM.Invoke(new Action(delegate - { - lblFanRPM.Text = value == -1 - ? "RPM: 0" - : $"RPM: {value}"; - })); - break; - } - } - private void LoadConf(string confPath) { UpdateStatus(StatusCode.ConfLoading); @@ -994,7 +1047,7 @@ private void LoadConf(string confPath) private void LoadConf(YAMDCC_Config cfg) { DisableAll(); - tsiSwitchAll.Checked = FansHaveSameProfileCount(); + tsiSwitchAll.Checked = FansHaveSameProfCount(); tsiSaveConf.Enabled = true; @@ -1044,7 +1097,7 @@ private void LoadConf(YAMDCC_Config cfg) else { PerfModeConf perfModeConf = cfg.PerfModeConf; - for (int i = 0; i < perfModeConf.PerfModes.Length; i++) + for (int i = 0; i < perfModeConf.PerfModes.Count; i++) { cboPerfMode.Items.Add(perfModeConf.PerfModes[i].Name); } @@ -1063,7 +1116,7 @@ private void LoadConf(YAMDCC_Config cfg) else { FanModeConf fanModeConf = cfg.FanModeConf; - for (int i = 0; i < fanModeConf.FanModes.Length; i++) + for (int i = 0; i < fanModeConf.FanModes.Count; i++) { cboFanMode.Items.Add(fanModeConf.FanModes[i].Name); } @@ -1086,7 +1139,7 @@ private void LoadConf(YAMDCC_Config cfg) } cboFanSel.Items.Clear(); - for (int i = 0; i < cfg.FanConfs.Length; i++) + for (int i = 0; i < cfg.FanConfs.Count; i++) { cboFanSel.Items.Add(cfg.FanConfs[i].Name); } @@ -1101,162 +1154,14 @@ private void LoadConf(YAMDCC_Config cfg) UpdateStatus(StatusCode.None); } - private void ApplyConf() - { - // Save the updated config - Config.ChargeLimitConf.CurVal = (byte)(chkChgLim.Checked - ? numChgLim.Value : 0); - Config.Save(Paths.CurrentConf); - - // Tell the service to reload and apply the updated config - SendServiceMessage(new ServiceCommand(Command.ApplyConfig, null)); - } - - private void RevertConf() - { - if (Utils.ShowWarning(Strings.GetString("dlgRevert"), - "Revert?") == DialogResult.Yes) - { - try - { - YAMDCC_Config tempConf = YAMDCC_Config.Load(CommonConfig.GetLastConf()); - LoadConf(tempConf); - Config = tempConf; - UpdateFanCurveDisplay(); - ApplyConf(); - btnRevert.Enabled = tsiRevert.Enabled = false; - } - catch (Exception ex) - { - if (ex is FileNotFoundException) - { - Utils.ShowError(Strings.GetString("dlgOldConfMissing")); - } - else if (ex is InvalidConfigException or InvalidOperationException) - { - Utils.ShowError(Strings.GetString("dlgOldConfInvalid")); - } - else - { - throw; - } - } - } - } - private void PollEC() { - SendServiceMessage(new ServiceCommand(Command.GetTemp, $"{cboFanSel.SelectedIndex}")); - SendServiceMessage(new ServiceCommand(Command.GetFanSpeed, $"{cboFanSel.SelectedIndex}")); - SendServiceMessage(new ServiceCommand(Command.GetFanRPM, $"{cboFanSel.SelectedIndex}")); - } - - private void AddFanProfile() - { - if (tsiSwitchAll.Checked) - { - bool switchAll = tsiSwitchAll.Checked; - tsiSwitchAll.Checked = false; - AddFanProfImpl(0, cboFanSel.Items.Count); - tsiSwitchAll.Checked = switchAll; - } - else - { - AddFanProfImpl(cboFanSel.SelectedIndex, cboFanSel.SelectedIndex + 1); - } - - if (!FansHaveSameProfileCount()) - { - tsiSwitchAll.Checked = false; - } - - btnRevert.Enabled = tsiRevert.Enabled = true; - } - - private void AddFanProfImpl(int start, int end) - { - FanConf cfg = Config.FanConfs[cboFanSel.SelectedIndex]; - string oldProfName = cfg.FanCurveConfs[cboProfSel.SelectedIndex].Name; - - TextInputDialog dlg = new( - Strings.GetString("dlgProfAdd"), - "New Profile", $"Copy of {oldProfName}"); - - if (dlg.ShowDialog() == DialogResult.OK) - { - for (int i = start; i < end; i++) - { - cfg = Config.FanConfs[i]; - FanCurveConf oldCurveCfg = cfg.FanCurveConfs[cfg.CurveSel]; - - // Create a copy of the currently selected fan profile: - FanCurveConf newCurveCfg = oldCurveCfg.Copy(); - - // Name it according to what the user specified - newCurveCfg.Name = dlg.Result; - newCurveCfg.Desc = $"(Copy of {oldCurveCfg.Name})\n{oldCurveCfg.Desc}"; - - // Add the new fan profile to the config's list - cfg.FanCurveConfs = [.. cfg.FanCurveConfs, newCurveCfg]; - cfg.CurveSel = cfg.FanCurveConfs.Length - 1; - - // Add the new fan profile to the UI's profile list and select it: - if (i == cboFanSel.SelectedIndex) - { - cboProfSel.Items.Add(dlg.Result); - cboProfSel.SelectedIndex = cfg.CurveSel; - } - } - } - } - - private void DelFanProfile() - { - if (tsiSwitchAll.Checked) - { - DelFanProfImpl(0, cboFanSel.Items.Count); - } - else - { - DelFanProfImpl(cboFanSel.SelectedIndex, cboFanSel.SelectedIndex + 1); - } - - if (!FansHaveSameProfileCount()) - { - tsiSwitchAll.Checked = false; - } - - btnRevert.Enabled = tsiRevert.Enabled = true; - } - - private void DelFanProfImpl(int start, int end) - { - for (int i = start; i < end; i++) - { - FanConf cfg = Config.FanConfs[i]; - FanCurveConf curveCfg = cfg.FanCurveConfs[cfg.CurveSel]; - - if (curveCfg.Name != "Default" && Utils.ShowWarning( - Strings.GetString("dlgProfDel", curveCfg.Name), - $"Delete fan profile? ({cfg.Name})") == DialogResult.Yes) - { - // Remove the fan profile from the config's list - List curveCfgList = [.. cfg.FanCurveConfs]; - curveCfgList.RemoveAt(cfg.CurveSel); - cfg.FanCurveConfs = [.. curveCfgList]; - cfg.CurveSel -= 1; - - // Remove from the list client-side, and select a different fan profile - if (i == cboFanSel.SelectedIndex) - { - cboProfSel.Items.RemoveAt(cboProfSel.SelectedIndex); - cboProfSel.SelectedIndex = cfg.CurveSel; - } - } - } + SendSvcMessage(new ServiceCommand(Command.GetTemp, $"{cboFanSel.SelectedIndex}")); + SendSvcMessage(new ServiceCommand(Command.GetFanSpeed, $"{cboFanSel.SelectedIndex}")); + SendSvcMessage(new ServiceCommand(Command.GetFanRPM, $"{cboFanSel.SelectedIndex}")); } - private void UpdateFanCurveDisplay() + private void UpdateCurveEditor() { FanConf cfg = Config.FanConfs[cboFanSel.SelectedIndex]; @@ -1272,6 +1177,7 @@ private void UpdateFanCurveDisplay() numFanSpds.Length != cfg.FanCurveRegs.Length || tbFanSpds.Length != cfg.FanCurveRegs.Length) { + tblCurve.SuspendLayout(); float scale = CurrentAutoScaleDimensions.Height / 72; tblCurve.Controls.Clear(); @@ -1285,16 +1191,16 @@ private void UpdateFanCurveDisplay() // labels on left side tblCurve.ColumnStyles.Add(new ColumnStyle()); - tblCurve.Controls.Add(FanCurveLabel("Speed (%)", scale, ContentAlignment.MiddleRight), 0, 0); - tblCurve.Controls.Add(FanCurveLabel("Up (°C)", scale, ContentAlignment.MiddleRight), 0, 2); - tblCurve.Controls.Add(FanCurveLabel("Down (°C)", scale, ContentAlignment.MiddleRight), 0, 3); + tblCurve.Controls.Add(FanCurveLabel("Speed (%)", scale), 0, 0); + tblCurve.Controls.Add(FanCurveLabel("Up (°C)", scale), 0, 2); + tblCurve.Controls.Add(FanCurveLabel("Down (°C)", scale), 0, 3); for (int i = 0; i < numFanSpds.Length; i++) { tblCurve.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F / numFanSpds.Length)); numFanSpds[i] = FanCurveNUD(i, scale); ttMain.SetToolTip(numFanSpds[i], Strings.GetString("ttFanSpd")); - numFanSpds[i].ValueChanged += numFanSpd_Changed; + numFanSpds[i].ValueChanged += new EventHandler(FanSpdChange); tblCurve.Controls.Add(numFanSpds[i], i + 1, 0); tbFanSpds[i] = new TrackBar() @@ -1308,46 +1214,40 @@ private void UpdateFanCurveDisplay() TickStyle = TickStyle.Both, }; ttMain.SetToolTip(tbFanSpds[i], Strings.GetString("ttFanSpd")); - tbFanSpds[i].ValueChanged += tbFanSpd_Scroll; + tbFanSpds[i].ValueChanged += new EventHandler(FanSpdChange); tblCurve.Controls.Add(tbFanSpds[i], i + 1, 1); if (i != 0) { numUpTs[i - 1] = FanCurveNUD(i - 1, scale); ttMain.SetToolTip(numUpTs[i - 1], Strings.GetString("ttUpT")); - numUpTs[i - 1].ValueChanged += numUpT_Changed; + numUpTs[i - 1].ValueChanged += new EventHandler(UpTChange); tblCurve.Controls.Add(numUpTs[i - 1], i + 1, 2); } else { - tblCurve.Controls.Add(FanCurveLabel("Default", scale), i + 1, 2); + tblCurve.Controls.Add(FanCurveLabel("Default", scale, ContentAlignment.MiddleCenter), i + 1, 2); } if (i != numFanSpds.Length - 1) { numDownTs[i] = FanCurveNUD(i, scale); ttMain.SetToolTip(numDownTs[i], Strings.GetString("ttDownT")); - numDownTs[i].ValueChanged += numDownT_Changed; + numDownTs[i].ValueChanged += new EventHandler(DownTChange); tblCurve.Controls.Add(numDownTs[i], i + 1, 3); } else { - tblCurve.Controls.Add(FanCurveLabel("Max", scale), i + 1, 3); + tblCurve.Controls.Add(FanCurveLabel("Max", scale, ContentAlignment.MiddleCenter), i + 1, 3); } } + tblCurve.ResumeLayout(true); } for (int i = 0; i < numFanSpds.Length; i++) { - if (cfg.FanCurveRegs.Length >= i) - { - numFanSpds[i].Maximum = tbFanSpds[i].Maximum - = Math.Abs(cfg.MaxSpeed - cfg.MinSpeed); - } - else - { - numFanSpds[i].Enabled = tbFanSpds[i].Enabled = false; - } + numFanSpds[i].Maximum = tbFanSpds[i].Maximum = + Math.Abs(cfg.MaxSpeed - cfg.MinSpeed); } cboProfSel.Enabled = true; @@ -1361,7 +1261,7 @@ private void UpdateFanCurveDisplay() } } - private static Label FanCurveLabel(string text, float scale, ContentAlignment textAlign = ContentAlignment.MiddleCenter) + private static Label FanCurveLabel(string text, float scale, ContentAlignment textAlign = ContentAlignment.MiddleRight) { return new Label { @@ -1433,13 +1333,13 @@ private void DisableAll() btnApply.Enabled = false; } - private bool FansHaveSameProfileCount() + private bool FansHaveSameProfCount() { - for (int i = 0; i < Config.FanConfs.Length - 1; i++) + for (int i = 0; i < Config.FanConfs.Count - 1; i++) { - FanConf[] fanCfgs = Config.FanConfs; + List fanCfgs = Config.FanConfs; - if (fanCfgs[i].FanCurveConfs.Length != fanCfgs[i + 1].FanCurveConfs.Length) + if (fanCfgs[i].FanCurveConfs.Count != fanCfgs[i + 1].FanCurveConfs.Count) { return false; } @@ -1449,67 +1349,64 @@ private bool FansHaveSameProfileCount() private void UpdateStatus(StatusCode status, int data = 0) { - lblStatus.Invoke(() => + if (AppStatus.Code == status) { - if (AppStatus.Code == status) - { - AppStatus.Repeats++; - } - else - { - AppStatus.Code = status; - AppStatus.Repeats = 0; - } + AppStatus.Repeats++; + } + else + { + AppStatus.Code = status; + AppStatus.Repeats = 0; + } - // set status text - bool persist = false; - switch (AppStatus.Code) - { - case StatusCode.ServiceCommandFail: - persist = true; - lblStatus.Text = Strings.GetString("statSvcErr", (Command)data); - break; - case StatusCode.ServiceResponseEmpty: - lblStatus.Text = Strings.GetString("statResponseEmpty"); - break; - case StatusCode.ServiceTimeout: - persist = true; - lblStatus.Text = Strings.GetString("statSvcTimeout"); - break; - case StatusCode.NoConfig: - persist = true; - lblStatus.Text = Strings.GetString("statNoConf"); - break; - case StatusCode.ConfLoading: - lblStatus.Text = Strings.GetString("statConfLoading"); - break; - case StatusCode.ConfApplySuccess: - lblStatus.Text = Strings.GetString("statConfApplied"); - break; - case StatusCode.FullBlastToggleSuccess: - lblStatus.Text = Strings.GetString("statFBToggled"); - break; - default: - persist = true; - AppStatus.Repeats = 0; - lblStatus.Text = "Ready"; - break; - } + // set status text + bool persist = false; + switch (AppStatus.Code) + { + case StatusCode.ServiceCommandFail: + persist = true; + lblStatus.Text = Strings.GetString("statSvcErr", (Command)data); + break; + case StatusCode.ServiceResponseEmpty: + lblStatus.Text = Strings.GetString("statResponseEmpty"); + break; + case StatusCode.ServiceTimeout: + persist = true; + lblStatus.Text = Strings.GetString("statSvcTimeout"); + break; + case StatusCode.NoConfig: + persist = true; + lblStatus.Text = Strings.GetString("statNoConf"); + break; + case StatusCode.ConfLoading: + lblStatus.Text = Strings.GetString("statConfLoading"); + break; + case StatusCode.ConfApplied: + lblStatus.Text = Strings.GetString("statConfApplied"); + break; + case StatusCode.FullBlastToggled: + lblStatus.Text = Strings.GetString("statFBToggled"); + break; + default: + persist = true; + AppStatus.Repeats = 0; + lblStatus.Text = "Ready"; + break; + } - if (AppStatus.Repeats > 0) - { - lblStatus.Text += $" (x{AppStatus.Repeats + 1})"; - } + if (AppStatus.Repeats > 0) + { + lblStatus.Text += $" (x{AppStatus.Repeats + 1})"; + } - tmrStatusReset.Stop(); - if (!persist) - { - tmrStatusReset.Start(); - } - }); + tmrStatusReset.Stop(); + if (!persist) + { + tmrStatusReset.Start(); + } } - private void SendServiceMessage(ServiceCommand command) + private void SendSvcMessage(ServiceCommand command) { IPCClient.PushMessage(command); tmrSvcTimeout.Start(); diff --git a/YAMDCC.ConfigEditor/MainWindow.resx b/YAMDCC.ConfigEditor/MainWindow.resx index dacb670..09cf9b5 100644 --- a/YAMDCC.ConfigEditor/MainWindow.resx +++ b/YAMDCC.ConfigEditor/MainWindow.resx @@ -129,24 +129,6 @@ False - - False - - - False - - - False - - - False - - - False - - - False - False @@ -162,37 +144,16 @@ False - - False - False False - - False - - - False - - - False - - - False - - - False - - - False - False - + False @@ -219,25 +180,16 @@ False - - False - - - False - - - False - - + False - + False - + False - + False diff --git a/YAMDCC.ConfigEditor/Program.cs b/YAMDCC.ConfigEditor/Program.cs index d30fcd0..2e43c3f 100644 --- a/YAMDCC.ConfigEditor/Program.cs +++ b/YAMDCC.ConfigEditor/Program.cs @@ -16,7 +16,6 @@ using System; using System.Diagnostics; -using System.Globalization; using System.IO; using System.Runtime.InteropServices; using System.ServiceProcess; @@ -29,8 +28,6 @@ namespace YAMDCC.ConfigEditor; internal static class Program { - private const int SW_RESTORE = 9; - /// /// The main entry point for the application. /// @@ -40,19 +37,8 @@ private static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - #region Global exception handlers - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); - Application.ThreadException += static (sender, e) => - new CrashDialog(e.Exception).ShowDialog(); - - AppDomain.CurrentDomain.UnhandledException += static (sender, e) => - { - if (e.ExceptionObject is Exception ex) - { - new CrashDialog(ex).ShowDialog(); - } - }; - #endregion + Application.ThreadException += new ThreadExceptionEventHandler(ThreadException); + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); if (!Utils.IsAdmin()) { @@ -103,7 +89,7 @@ private static void Main() }); dlg.ShowDialog(); - if (dlg.Result is bool b && b) + if ((bool)dlg.Result) { // Start the program when the service finishes starting: Start(); @@ -119,8 +105,7 @@ private static void Main() } // Check if the service is stopped: - ServiceController yamdccSvc = new("yamdccsvc"); - try + using (ServiceController yamdccSvc = new("yamdccsvc")) { ServiceControllerStatus status = yamdccSvc.Status; if (status == ServiceControllerStatus.Stopped) @@ -130,20 +115,20 @@ private static void Main() MessageBoxButtons.YesNo) == DialogResult.Yes) { ProgressDialog dlg = new(Strings.GetString("dlgSvcStarting"), (e) => + { + if (Utils.StartService("yamdccsvc")) { - if (Utils.StartService("yamdccsvc")) - { - e.Result = false; - } - else - { - Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); - e.Result = true; - } - }); + e.Result = false; + } + else + { + Utils.ShowError(Strings.GetString("dlgSvcStartCrash")); + e.Result = true; + } + }); dlg.ShowDialog(); - if (dlg.Result is bool b && b) + if ((bool)dlg.Result) { return; } @@ -154,10 +139,6 @@ private static void Main() } } } - finally - { - yamdccSvc?.Close(); - } // Start the program when the service finishes starting: Start(); @@ -173,7 +154,7 @@ private static void Main() { if (process.MainWindowHandle != IntPtr.Zero) { - ShowWindow(process.MainWindowHandle, SW_RESTORE); + ShowWindow(process.MainWindowHandle, 9); // SW_RESTORE SetForegroundWindow(process.MainWindowHandle); } break; @@ -206,4 +187,14 @@ private static void Start() [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + + private static void ThreadException(object sender, ThreadExceptionEventArgs e) + { + new CrashDialog(e.Exception).ShowDialog(); + } + + private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + new CrashDialog((Exception)e.ExceptionObject).ShowDialog(); + } } diff --git a/YAMDCC.ConfigEditor/Status.cs b/YAMDCC.ConfigEditor/Status.cs index 41a515e..04835b7 100644 --- a/YAMDCC.ConfigEditor/Status.cs +++ b/YAMDCC.ConfigEditor/Status.cs @@ -36,6 +36,6 @@ internal enum StatusCode ServiceTimeout, ConfLoading, NoConfig, - ConfApplySuccess, - FullBlastToggleSuccess, + ConfApplied, + FullBlastToggled, } diff --git a/YAMDCC.ConfigEditor/Strings.resx b/YAMDCC.ConfigEditor/Strings.resx index 8a0b3e0..ca5ee1a 100644 --- a/YAMDCC.ConfigEditor/Strings.resx +++ b/YAMDCC.ConfigEditor/Strings.resx @@ -245,11 +245,11 @@ using "Load Config", or saved using "Save Config", whichever came last. Click to go to this project's GitHub page. - + Click to stop the YAMDCC service. This will also quit the YAMDCC config editor (this program). - + Click to uninstall the YAMDCC service (and quit the YAMDCC config editor). Only do this if you want to uninstall YAMDCC or move its program files to another location. diff --git a/YAMDCC.Service/FanControlService.cs b/YAMDCC.Service/FanControlService.cs index 7765b00..27b8129 100644 --- a/YAMDCC.Service/FanControlService.cs +++ b/YAMDCC.Service/FanControlService.cs @@ -14,11 +14,9 @@ // You should have received a copy of the GNU General Public License along with // YAMDCC. If not, see . -using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; -using System.Globalization; using System.IO; using System.IO.Pipes; using System.ServiceProcess; @@ -408,12 +406,12 @@ private bool ApplyConf() bool success = true; // Write custom register values, if configured: - if (Config.RegConfs?.Length > 0) + if (Config.RegConfs?.Count > 0) { - for (int i = 0; i < Config.RegConfs.Length; i++) + for (int i = 0; i < Config.RegConfs.Count; i++) { RegConf cfg = Config.RegConfs[i]; - Log.Info(Strings.GetString("svcWriteRegConfs", i + 1, Config.RegConfs.Length)); + Log.Info(Strings.GetString("svcWriteRegConfs", i + 1, Config.RegConfs.Count)); if (!LogECWriteByte(cfg.Reg, cfg.Enabled ? cfg.OnVal : cfg.OffVal)) { success = false; @@ -422,13 +420,13 @@ private bool ApplyConf() } // Write the fan curve to the appropriate registers for each fan: - for (int i = 0; i < Config.FanConfs.Length; i++) + for (int i = 0; i < Config.FanConfs.Count; i++) { FanConf cfg = Config.FanConfs[i]; - Log.Info(Strings.GetString("svcWriteFanConfs", cfg.Name, i + 1, Config.FanConfs.Length)); + Log.Info(Strings.GetString("svcWriteFanConfs", cfg.Name, i + 1, Config.FanConfs.Count)); FanCurveConf curveCfg = cfg.FanCurveConfs[cfg.CurveSel]; - for (int j = 0; j < curveCfg.TempThresholds.Length; j++) + for (int j = 0; j < curveCfg.TempThresholds.Count; j++) { if (!LogECWriteByte(cfg.FanCurveRegs[j], curveCfg.TempThresholds[j].FanSpeed)) { @@ -704,14 +702,15 @@ private bool ECtoConf() Config.Model = pcModel.Trim(); } - for (int i = 0; i < Config.FanConfs.Length; i++) + for (int i = 0; i < Config.FanConfs.Count; i++) { - Log.Info(Strings.GetString("svcReadProfs", i + 1, Config.FanConfs.Length)); + Log.Info(Strings.GetString("svcReadProfs", i + 1, Config.FanConfs.Count)); FanConf cfg = Config.FanConfs[i]; + // look for an already existing Default fan profile FanCurveConf curveCfg = null; - for (int j = 0; j < cfg.FanCurveConfs.Length; j++) + for (int j = 0; j < cfg.FanCurveConfs.Count; j++) { if (cfg.FanCurveConfs[j].Name == "Default") { @@ -725,16 +724,14 @@ private bool ECtoConf() { curveCfg = new() { - TempThresholds = new TempThreshold[cfg.FanCurveRegs.Length] + Name = "Default", + TempThresholds = new List(cfg.FanCurveRegs.Length), }; - List curveCfgList = [.. cfg.FanCurveConfs]; - curveCfgList.Insert(0, curveCfg); - cfg.FanCurveConfs = [.. curveCfgList]; + cfg.FanCurveConfs.Insert(0, curveCfg); cfg.CurveSel++; } - // reset first fan curve config name and description - curveCfg.Name = "Default"; + // reset first fan curve config description curveCfg.Desc = Strings.GetString("DefaultDesc"); for (int j = 0; j < cfg.FanCurveRegs.Length; j++)