From 7f6175f8ef4d64c843c7df0021c1ad957819ef05 Mon Sep 17 00:00:00 2001 From: Vincent Maas Date: Sun, 14 Feb 2021 15:17:30 +0100 Subject: [PATCH] add WOL to steps, fix saving configs --- ColorControl/ColorControl.csproj | 2 +- ColorControl/LgService.cs | 93 ++++++++++++++------- ColorControl/MainForm.Designer.cs | 46 ++++++++--- ColorControl/MainForm.cs | 104 +++++++++++++++++------- ColorControl/Properties/AssemblyInfo.cs | 4 +- 5 files changed, 175 insertions(+), 74 deletions(-) diff --git a/ColorControl/ColorControl.csproj b/ColorControl/ColorControl.csproj index 9e0ff03..45a84c9 100644 --- a/ColorControl/ColorControl.csproj +++ b/ColorControl/ColorControl.csproj @@ -27,7 +27,7 @@ ColorControl Maassoft 0 - 1.5.5.0 + 1.6.0.0 false true true diff --git a/ColorControl/LgService.cs b/ColorControl/LgService.cs index 54d217a..240e2b4 100644 --- a/ColorControl/LgService.cs +++ b/ColorControl/LgService.cs @@ -42,10 +42,15 @@ namespace ColorControl private List _remoteControlButtons; private JavaScriptSerializer _JsonDeserializer = new JavaScriptSerializer(); + private Dictionary _invokableActions = new Dictionary(); + public LgService(string dataDir, bool allowPowerOn) { _dataDir = dataDir; _allowPowerOn = allowPowerOn; + + _invokableActions.Add("WOL", new Action(WakeSelectedDevice)); + LoadConfig(); LoadRemoteControlButtons(); @@ -62,6 +67,11 @@ namespace ColorControl } ~LgService() + { + GlobalSave(); + } + + public void GlobalSave() { SaveConfig(); SaveRemoteControlButtons(); @@ -105,6 +115,11 @@ namespace ColorControl return _remoteControlButtons; } + public Dictionary GetInvokableActions() + { + return _invokableActions; + } + private List GenerateDefaultRemoteControlButtons() { var list = new List(); @@ -151,13 +166,13 @@ namespace ColorControl return preset; } - public void SaveConfig() + private void SaveConfig() { var json = JsonConvert.SerializeObject(Config); File.WriteAllText(_configFilename, json); } - public void SaveRemoteControlButtons() + private void SaveRemoteControlButtons() { var json = JsonConvert.SerializeObject(_remoteControlButtons); File.WriteAllText(_rcButtonsFilename, json); @@ -244,6 +259,17 @@ namespace ColorControl { var hasApp = !string.IsNullOrEmpty(preset.appId); + var hasWOL = preset.steps.Any(s => s.Equals("WOL", StringComparison.OrdinalIgnoreCase)); + + if (hasWOL) + { + var connected = await WakeAndConnectToSelectedDevice(0); + if (!connected) + { + return false; + } + } + for (var tries = 0; tries <= 1; tries++) { if (!await Connected(reconnect || tries == 1)) @@ -317,13 +343,19 @@ namespace ColorControl foreach (var step in preset.steps) { var keySpec = step.Split(':'); + var key = keySpec[0].ToUpper(); + if (_invokableActions.ContainsKey(key)) + { + continue; + } + if (keySpec.Length == 2) { - SendKey(mouse, keySpec[0], int.Parse(keySpec[1])); + SendKey(mouse, key, int.Parse(keySpec[1])); } else { - SendKey(mouse, keySpec[0]); + SendKey(mouse, key); } } } @@ -383,9 +415,14 @@ namespace ColorControl return true; } - internal void WakeSelectedDevice(string macAddress = null) + internal void WakeSelectedDevice() { - macAddress = macAddress == null ? SelectedDevice?.MacAddress : macAddress; + WakeSelectedDevice(SelectedDevice?.MacAddress ?? Config.PreferredMacAddress); + } + + internal void WakeSelectedDevice(string macAddress) + { + macAddress = string.IsNullOrEmpty(macAddress) ? SelectedDevice?.MacAddress : macAddress; if (macAddress != null) { WOL.WakeFunction(macAddress, !Config.UseAlternateWol); @@ -393,7 +430,7 @@ namespace ColorControl } else { - Logger.Debug("Cannot wake device: no device has been selected"); + Logger.Debug("Cannot wake device: no device has been selected or there is no preferred MAC-address stored in the configuration"); } } @@ -405,31 +442,29 @@ namespace ColorControl } } - private void WakeAndConnectToSelectedDevice(int wakeDelay = 5000, int connectDelay = 1000) + private async Task WakeAndConnectToSelectedDevice(int wakeDelay = 5000, int connectDelay = 1000) { - Task.Run(async () => + try { - try - { - //if (wakeDelay == 0) - //{ - // if (await ConnectToSelectedDevice()) - // { - // Logger.Debug("Already connected, no wake needed?"); - // return; - // }; - //} + //if (wakeDelay == 0) + //{ + // if (await ConnectToSelectedDevice()) + // { + // Logger.Debug("Already connected, no wake needed?"); + // return; + // }; + //} - await Task.Delay(wakeDelay); - WakeSelectedDevice(); - await Task.Delay(connectDelay); - await ConnectToSelectedDevice(); - } - catch (Exception e) - { - Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString()); - } - }); + await Task.Delay(wakeDelay); + WakeSelectedDevice(); + await Task.Delay(connectDelay); + return await ConnectToSelectedDevice(); + } + catch (Exception e) + { + Logger.Error("WakeAndConnectToSelectedDevice: " + e.ToLogString()); + return false; + } } } } diff --git a/ColorControl/MainForm.Designer.cs b/ColorControl/MainForm.Designer.cs index 4d27965..af1296b 100644 --- a/ColorControl/MainForm.Designer.cs +++ b/ColorControl/MainForm.Designer.cs @@ -112,6 +112,7 @@ this.label10 = new System.Windows.Forms.Label(); this.chkFixChromeFonts = new System.Windows.Forms.CheckBox(); this.grpLGOptions = new System.Windows.Forms.GroupBox(); + this.chkLgAlternateWolMechanism = new System.Windows.Forms.CheckBox(); this.btnLGTestPower = new System.Windows.Forms.Button(); this.label9 = new System.Windows.Forms.Label(); this.edtLgPowerOnAfterResumeDelay = new System.Windows.Forms.NumericUpDown(); @@ -134,12 +135,14 @@ this.lbPlugins = new System.Windows.Forms.ListBox(); this.label7 = new System.Windows.Forms.Label(); this.lblInfo = new System.Windows.Forms.Label(); - this.chkLgAlternateWolMechanism = new System.Windows.Forms.CheckBox(); + this.mnuLgRcButtons = new System.Windows.Forms.ToolStripMenuItem(); + this.mnuLgActions = new System.Windows.Forms.ToolStripMenuItem(); this.tcMain.SuspendLayout(); this.tabNVIDIA.SuspendLayout(); this.mnuNvPresets.SuspendLayout(); this.tabAMD.SuspendLayout(); this.tabLG.SuspendLayout(); + this.mnuLgButtons.SuspendLayout(); this.tabOptions.SuspendLayout(); this.grpNvidiaOptions.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pbGradient)).BeginInit(); @@ -644,15 +647,18 @@ this.btnLgAddButton.Name = "btnLgAddButton"; this.btnLgAddButton.Size = new System.Drawing.Size(75, 23); this.btnLgAddButton.TabIndex = 32; - this.btnLgAddButton.Text = "Add button"; + this.btnLgAddButton.Text = "Add step"; this.btnLgAddButton.UseVisualStyleBackColor = true; this.btnLgAddButton.Click += new System.EventHandler(this.btnLgAddButton_Click); // // mnuLgButtons // this.mnuLgButtons.ImageScalingSize = new System.Drawing.Size(20, 20); + this.mnuLgButtons.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.mnuLgRcButtons, + this.mnuLgActions}); this.mnuLgButtons.Name = "mnuLgButtons"; - this.mnuLgButtons.Size = new System.Drawing.Size(61, 4); + this.mnuLgButtons.Size = new System.Drawing.Size(181, 70); // // label3 // @@ -1044,6 +1050,17 @@ this.grpLGOptions.TabStop = false; this.grpLGOptions.Text = "LG controller"; // + // chkLgAlternateWolMechanism + // + this.chkLgAlternateWolMechanism.AutoSize = true; + this.chkLgAlternateWolMechanism.Location = new System.Drawing.Point(158, 72); + this.chkLgAlternateWolMechanism.Name = "chkLgAlternateWolMechanism"; + this.chkLgAlternateWolMechanism.Size = new System.Drawing.Size(173, 17); + this.chkLgAlternateWolMechanism.TabIndex = 7; + this.chkLgAlternateWolMechanism.Text = "Use alternate WOL mechanism"; + this.chkLgAlternateWolMechanism.UseVisualStyleBackColor = true; + this.chkLgAlternateWolMechanism.CheckedChanged += new System.EventHandler(this.chkLgAlternateWolMechanism_CheckedChanged); + // // btnLGTestPower // this.btnLGTestPower.Location = new System.Drawing.Point(9, 68); @@ -1297,16 +1314,17 @@ this.lblInfo.TabIndex = 1; this.lblInfo.Text = "Info"; // - // chkLgAlternateWolMechanism + // mnuLgRcButtons // - this.chkLgAlternateWolMechanism.AutoSize = true; - this.chkLgAlternateWolMechanism.Location = new System.Drawing.Point(158, 72); - this.chkLgAlternateWolMechanism.Name = "chkLgAlternateWolMechanism"; - this.chkLgAlternateWolMechanism.Size = new System.Drawing.Size(173, 17); - this.chkLgAlternateWolMechanism.TabIndex = 7; - this.chkLgAlternateWolMechanism.Text = "Use alternate WOL mechanism"; - this.chkLgAlternateWolMechanism.UseVisualStyleBackColor = true; - this.chkLgAlternateWolMechanism.CheckedChanged += new System.EventHandler(this.chkLgAlternateWolMechanism_CheckedChanged); + this.mnuLgRcButtons.Name = "mnuLgRcButtons"; + this.mnuLgRcButtons.Size = new System.Drawing.Size(180, 22); + this.mnuLgRcButtons.Text = "Buttons"; + // + // mnuLgActions + // + this.mnuLgActions.Name = "mnuLgActions"; + this.mnuLgActions.Size = new System.Drawing.Size(180, 22); + this.mnuLgActions.Text = "Actions"; // // MainForm // @@ -1320,6 +1338,7 @@ this.Name = "MainForm"; this.Text = "ColorControl"; this.Activated += new System.EventHandler(this.MainForm_Activated); + this.Deactivate += new System.EventHandler(this.MainForm_Deactivate); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed); this.Shown += new System.EventHandler(this.MainForm_Shown); @@ -1332,6 +1351,7 @@ this.tabAMD.PerformLayout(); this.tabLG.ResumeLayout(false); this.tabLG.PerformLayout(); + this.mnuLgButtons.ResumeLayout(false); this.tabOptions.ResumeLayout(false); this.grpNvidiaOptions.ResumeLayout(false); this.grpNvidiaOptions.PerformLayout(); @@ -1463,6 +1483,8 @@ private System.Windows.Forms.Button btnLGRemoteControl; private System.Windows.Forms.ToolStripMenuItem miNvPresetApplyOnStartup; private System.Windows.Forms.CheckBox chkLgAlternateWolMechanism; + private System.Windows.Forms.ToolStripMenuItem mnuLgRcButtons; + private System.Windows.Forms.ToolStripMenuItem mnuLgActions; } } diff --git a/ColorControl/MainForm.cs b/ColorControl/MainForm.cs index 41d7503..7bde64d 100644 --- a/ColorControl/MainForm.cs +++ b/ColorControl/MainForm.cs @@ -176,6 +176,32 @@ namespace ColorControl edtLgPowerOnAfterResumeDelay.Value = _lgService.Config.PowerOnDelayAfterResume; edtLgDeviceFilter.Text = _lgService.Config.DeviceSearchKey; chkLgAlternateWolMechanism.Checked = _lgService.Config.UseAlternateWol; + + var values = Enum.GetValues(typeof(ButtonType)); + foreach (var button in values) + { + var text = button.ToString(); + if (text[0] == '_') + { + text = text.Substring(1); + } + + var item = mnuLgRcButtons.DropDownItems.Add(text); + item.Click += miLgAddButton_Click; + } + + var actions = _lgService.GetInvokableActions(); + foreach (var keyValuePair in actions) + { + var text = keyValuePair.Key; + + var item = mnuLgActions.DropDownItems.Add(text); + item.Click += miLgAddAction_Click; + } + + + SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged); + //SystemEvents.SessionEnded += new SessionEndedEventHandler(SessionEnded); } catch (Exception e) { @@ -184,24 +210,6 @@ namespace ColorControl InitInfo(); - var values = Enum.GetValues(typeof(ButtonType)); - foreach (var button in values) - { - var text = button.ToString(); - if (text[0] == '_') - { - text = text.Substring(1); - } - var item = mnuLgButtons.Items.Add(text); - item.Click += miLgAddButton_Click; - } - - if (_lgService != null) - { - //SystemEvents.SessionEnded += new SessionEndedEventHandler(SessionEnded); - SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged); - } - _restartDetector = new RestartDetector(); //Scale(new SizeF(1.25F, 1.25F)); @@ -531,11 +539,8 @@ namespace ColorControl UserExit = false; return; } - - SaveNvPresets(); - SaveLgPresets(); - SaveConfig(); + GlobalSave(); if (SystemShutdown && _lgService.Config.PowerOffOnShutdown) { @@ -560,16 +565,40 @@ namespace ColorControl } } + private void GlobalSave() + { + SaveNvPresets(); + SaveLgPresets(); + + _lgService?.GlobalSave(); + + SaveConfig(); + } + private void SaveNvPresets() { - var json = _JsonSerializer.Serialize(_presets); - File.WriteAllText(_nvPresetsFilename, json); + try + { + var json = _JsonSerializer.Serialize(_presets); + File.WriteAllText(_nvPresetsFilename, json); + } + catch (Exception e) + { + Logger.Error(e.ToLogString()); + } } private void SaveLgPresets() { - var json = _JsonSerializer.Serialize(_lgPresets); - File.WriteAllText(_lgPresetsFilename, json); + try + { + var json = _JsonSerializer.Serialize(_lgPresets); + File.WriteAllText(_lgPresetsFilename, json); + } + catch (Exception e) + { + Logger.Error(e.ToLogString()); + } } private void lvNvPresets_SelectedIndexChanged(object sender, EventArgs e) @@ -1812,12 +1841,22 @@ namespace ColorControl { var item = sender as ToolStripItem; - var button = item.Text; + AddToLgSteps(item.Text); + } + private void miLgAddAction_Click(object sender, EventArgs e) + { + var item = sender as ToolStripItem; + + AddToLgSteps(item.Text); + } + + private void AddToLgSteps(string step) + { var text = edtStepsLg.Text; if (string.IsNullOrWhiteSpace(text)) { - text = button; + text = step; } else { @@ -1828,11 +1867,11 @@ namespace ColorControl } if (pos == text.Length) { - text += ", " + button; + text += ", " + step; } else { - text = text.Substring(0, pos + 1) + " " + button + ", " + text.Substring(pos + 1).Trim(); + text = text.Substring(0, pos + 1) + " " + step + ", " + text.Substring(pos + 1).Trim(); } } edtStepsLg.Text = text.Trim(); @@ -2080,5 +2119,10 @@ Do you want to continue?"; { _lgService.Config.UseAlternateWol = chkLgAlternateWolMechanism.Checked; } + + private void MainForm_Deactivate(object sender, EventArgs e) + { + GlobalSave(); + } } } \ No newline at end of file diff --git a/ColorControl/Properties/AssemblyInfo.cs b/ColorControl/Properties/AssemblyInfo.cs index 9621319..11801a9 100644 --- a/ColorControl/Properties/AssemblyInfo.cs +++ b/ColorControl/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.5.0")] -[assembly: AssemblyFileVersion("1.5.5.0")] +[assembly: AssemblyVersion("1.6.0.0")] +[assembly: AssemblyFileVersion("1.6.0.0")]