From 90ca095954b0314cff5f01c574a915190ae6bb31 Mon Sep 17 00:00:00 2001 From: Vincent Maas Date: Sun, 4 Jul 2021 16:18:59 +0200 Subject: [PATCH] add nvidia/amd presets to steps in LG controller --- ColorControl/AmdService.cs | 4 +- ColorControl/ColorControl.csproj | 2 +- ColorControl/LgDevice.cs | 45 ++++++++-- ColorControl/MainForm.Designer.cs | 26 +++++- ColorControl/MainForm.cs | 112 +++++++++++++++++++++++- ColorControl/NvService.cs | 3 +- ColorControl/Properties/AssemblyInfo.cs | 4 +- ColorControl/ServiceBase.cs | 8 ++ ColorControl/lgtv/LgTvApi.cs | 6 ++ 9 files changed, 188 insertions(+), 22 deletions(-) diff --git a/ColorControl/AmdService.cs b/ColorControl/AmdService.cs index 1d0bb46..026d5a1 100644 --- a/ColorControl/AmdService.cs +++ b/ColorControl/AmdService.cs @@ -13,8 +13,6 @@ class AmdService : GraphicsService private ADLDisplayInfo _currentDisplay; - private AmdPreset _lastAppliedPreset; - public AmdService(string dataPath) : base(dataPath) { LoadPresets(); @@ -191,6 +189,8 @@ public bool ApplyPreset(AmdPreset preset, AppContext appContext) _lastAppliedPreset = preset; + PresetApplied(); + return result; } diff --git a/ColorControl/ColorControl.csproj b/ColorControl/ColorControl.csproj index 02ca566..47b5011 100644 --- a/ColorControl/ColorControl.csproj +++ b/ColorControl/ColorControl.csproj @@ -27,7 +27,7 @@ ColorControl Maassoft 0 - 3.2.2.0 + 3.3.0.0 false true true diff --git a/ColorControl/LgDevice.cs b/ColorControl/LgDevice.cs index 57a44c2..543ef12 100644 --- a/ColorControl/LgDevice.cs +++ b/ColorControl/LgDevice.cs @@ -18,6 +18,7 @@ public class InvokableAction public Type EnumType { get; set; } public decimal MinValue { get; set; } public decimal MaxValue { get; set; } + public string Category { get; set; } } public enum PowerState @@ -37,6 +38,7 @@ public enum PowerOffSource } protected static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + public static Func ExternalServiceHandler; public string Name { get; private set; } public string IpAddress { get; private set; } @@ -61,8 +63,10 @@ public enum PowerOffSource [JsonIgnore] public PowerOffSource PoweredOffBy { get; private set; } + [JsonIgnore] public bool PoweredOffViaApp { get; private set; } - + [JsonIgnore] + private DateTimeOffset _poweredOffViaAppDateTime { get; set; } [JsonIgnore] private List _invokableActions = new List(); @@ -91,6 +95,15 @@ public LgDevice(string name, string ipAddress, string macAddress, bool isCustom AddGenericPictureAction("energySaving", typeof(EnergySaving)); //AddGenericPictureAction("truMotionMode", typeof(TruMotionMode)); AddGenericPictureAction("motionProOLED", typeof(OffToHigh)); + AddGenericPictureAction("uhdDeepColorHDMI1", typeof(OffToOn), category: "other"); + AddGenericPictureAction("uhdDeepColorHDMI2", typeof(OffToOn), category: "other"); + AddGenericPictureAction("uhdDeepColorHDMI3", typeof(OffToOn), category: "other"); + AddGenericPictureAction("uhdDeepColorHDMI4", typeof(OffToOn), category: "other"); + //AddGenericPictureAction("hdmiPcMode", typeof(OffToOn), category: "other"); + AddGenericPictureAction("gameOptimizationHDMI1", typeof(OffToOn), category: "other"); + AddGenericPictureAction("gameOptimizationHDMI2", typeof(OffToOn), category: "other"); + AddGenericPictureAction("gameOptimizationHDMI3", typeof(OffToOn), category: "other"); + AddGenericPictureAction("gameOptimizationHDMI4", typeof(OffToOn), category: "other"); } private void AddInvokableAction(string name, Func, bool> function) @@ -104,7 +117,7 @@ private void AddInvokableAction(string name, Func, bo _invokableActions.Add(action); } - private void AddGenericPictureAction(string name, Type type = null, decimal minValue = 0, decimal maxValue = 0) + private void AddGenericPictureAction(string name, Type type = null, decimal minValue = 0, decimal maxValue = 0, string category = "picture") { var action = new InvokableAction { @@ -112,7 +125,8 @@ private void AddGenericPictureAction(string name, Type type = null, decimal minV Function = new Func, bool>(GenericPictureAction), EnumType = type, MinValue = minValue, - MaxValue = maxValue + MaxValue = maxValue, + Category = category }; _invokableActions.Add(action); @@ -165,7 +179,7 @@ public bool VolumeChanged(dynamic payload) } public bool PowerStateChanged(dynamic payload) { - Logger.Debug($"[{Name}] Power state change: " + JsonConvert.SerializeObject(payload)); + Logger.Debug($"[{Name}] Power state change: {JsonConvert.SerializeObject(payload)}"); var state = ((string)payload.state).Replace(' ', '_'); @@ -176,9 +190,10 @@ public bool PowerStateChanged(dynamic payload) if (CurrentState == PowerState.Active) { - if (payload.processing == null) + if (payload.processing == null && (DateTimeOffset.Now - _poweredOffViaAppDateTime).TotalMilliseconds > 500) { PoweredOffViaApp = false; + _poweredOffViaAppDateTime = DateTimeOffset.MinValue; } } else { @@ -190,6 +205,9 @@ public bool PowerStateChanged(dynamic payload) CurrentState = PowerState.Unknown; Logger.Warn($"Unknown power state: {state}"); } + + Logger.Debug($"PoweredOffBy: {PoweredOffBy}, PoweredOffViaApp: {PoweredOffViaApp}"); + return true; } @@ -307,6 +325,13 @@ private async Task ExecuteSteps(LgTvApi api, LgPreset preset) continue; } + if (ExternalServiceHandler != null && parameters != null) + { + if (ExternalServiceHandler(key, parameters)) + { + continue; + } + } if (keySpec.Length == 2) { @@ -327,7 +352,8 @@ private void ExecuteAction(InvokableAction action, string[] parameters) { var keyValues = new Dictionary { { "name", action.Name }, - { "value", parameters[0] } + { "value", parameters[0] }, + { "category", action.Category } }; function(keyValues); @@ -366,12 +392,13 @@ public async Task> GetApps(bool force = false) internal async Task PowerOff() { - if (!await Connected(true)) + if (!await Connected(true) || CurrentState != PowerState.Active) { return false; } PoweredOffViaApp = true; + _poweredOffViaAppDateTime = DateTimeOffset.Now; await _lgTvApi.TurnOff(); @@ -558,7 +585,9 @@ private bool WakeAction(Dictionary parameters) private bool GenericPictureAction(Dictionary parameters) { var settingName = parameters["name"]; - var task = _lgTvApi.SetSystemSettings(settingName, parameters["value"]); + var value = parameters["value"]; + var category = parameters["category"]; + var task = _lgTvApi.SetSystemSettings(settingName, value, category); Utils.WaitForTask(task); return true; diff --git a/ColorControl/MainForm.Designer.cs b/ColorControl/MainForm.Designer.cs index b8ed7a1..797f63e 100644 --- a/ColorControl/MainForm.Designer.cs +++ b/ColorControl/MainForm.Designer.cs @@ -182,6 +182,8 @@ private void InitializeComponent() this.lbPlugins = new System.Windows.Forms.ListBox(); this.label7 = new System.Windows.Forms.Label(); this.lblInfo = new System.Windows.Forms.Label(); + this.mnuLgNvPresets = new System.Windows.Forms.ToolStripMenuItem(); + this.mnuLgAmdPresets = new System.Windows.Forms.ToolStripMenuItem(); this.tcMain.SuspendLayout(); this.tabNVIDIA.SuspendLayout(); this.mnuNvPresets.SuspendLayout(); @@ -1300,21 +1302,23 @@ private void InitializeComponent() this.mnuLgButtons.ImageScalingSize = new System.Drawing.Size(20, 20); this.mnuLgButtons.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.mnuLgRcButtons, - this.mnuLgActions}); + this.mnuLgActions, + this.mnuLgNvPresets, + this.mnuLgAmdPresets}); this.mnuLgButtons.Name = "mnuLgButtons"; - this.mnuLgButtons.Size = new System.Drawing.Size(116, 48); + this.mnuLgButtons.Size = new System.Drawing.Size(181, 114); this.mnuLgButtons.Opening += new System.ComponentModel.CancelEventHandler(this.mnuLgButtons_Opening); // // mnuLgRcButtons // this.mnuLgRcButtons.Name = "mnuLgRcButtons"; - this.mnuLgRcButtons.Size = new System.Drawing.Size(115, 22); + 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(115, 22); + this.mnuLgActions.Size = new System.Drawing.Size(180, 22); this.mnuLgActions.Text = "Actions"; // // btnCloneLg @@ -1828,6 +1832,18 @@ private void InitializeComponent() this.lblInfo.TabIndex = 1; this.lblInfo.Text = "Info"; // + // mnuLgNvPresets + // + this.mnuLgNvPresets.Name = "mnuLgNvPresets"; + this.mnuLgNvPresets.Size = new System.Drawing.Size(180, 22); + this.mnuLgNvPresets.Text = "NVIDIA presets"; + // + // mnuLgAmdPresets + // + this.mnuLgAmdPresets.Name = "mnuLgAmdPresets"; + this.mnuLgAmdPresets.Size = new System.Drawing.Size(180, 22); + this.mnuLgAmdPresets.Text = "AMD presets"; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -2037,6 +2053,8 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem miLgEnableMotionPro; private System.Windows.Forms.ToolStripMenuItem miLgDisableMotionPro; private System.Windows.Forms.ToolStripSeparator miLgExpertSeparator1; + private System.Windows.Forms.ToolStripMenuItem mnuLgNvPresets; + private System.Windows.Forms.ToolStripMenuItem mnuLgAmdPresets; } } diff --git a/ColorControl/MainForm.cs b/ColorControl/MainForm.cs index 03b8eda..b9c9f36 100644 --- a/ColorControl/MainForm.cs +++ b/ColorControl/MainForm.cs @@ -118,6 +118,8 @@ private void InitNvService() { _nvService = new NvService(_dataDir); FillNvPresets(); + + _nvService.AfterApplyPreset += NvServiceAfterApplyPreset; } catch (Exception) { @@ -127,12 +129,19 @@ private void InitNvService() } } + private void NvServiceAfterApplyPreset(object sender, NvPreset preset) + { + UpdateDisplayInfoItems(); + } + private void InitAmdService() { try { _amdService = new AmdService(_dataDir); FillAmdPresets(); + + _amdService.AfterApplyPreset += AmdServiceAfterApplyPreset; } catch (Exception) { @@ -142,6 +151,11 @@ private void InitAmdService() } } + private void AmdServiceAfterApplyPreset(object sender, AmdPreset preset) + { + UpdateDisplayInfoItemsAmd(); + } + private void InitLgService() { try @@ -169,6 +183,8 @@ private void InitLgService() } _lgService.InstallEventHandlers(); + + LgDevice.ExternalServiceHandler = HandleExternalServiceForLgDevice; } catch (Exception e) { @@ -184,6 +200,25 @@ private void AfterLgServiceRefreshDevices() } } + private bool HandleExternalServiceForLgDevice(string serviceName, string[] parameters) + { + if (string.IsNullOrEmpty(serviceName) || parameters.Length == 0) + { + return false; + } + + if (_nvService != null && serviceName.Equals("NvPreset", StringComparison.OrdinalIgnoreCase)) + { + return _nvService.ApplyPreset(parameters[0]); + } + if (_amdService != null && serviceName.Equals("AmdPreset", StringComparison.OrdinalIgnoreCase)) + { + return _amdService.ApplyPreset(parameters[0]); + } + + return false; + } + private void InitInfo() { var fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.GetFileName(Application.ExecutablePath)); @@ -1154,8 +1189,20 @@ private void btnSetShortcutLg_Click(object sender, EventArgs e) preset.steps.Clear(); if (!clearSteps) { - text = text.Replace(" ", string.Empty); + //text = text.Replace(" ", string.Empty); var steps = text.Split(','); + for (var i = 0; i < steps.Length; i++) + { + var step = steps[i]; + if (step.IndexOf("(") > -1) + { + steps[i] = step.Trim(); + } + else + { + steps[i] = step.Replace(" ", string.Empty); + } + } preset.steps.AddRange(steps); } @@ -1519,7 +1566,6 @@ private bool ApplyNvPreset(NvPreset preset) throw new Exception("Error while applying NVIDIA preset. At least one setting could not be applied. Check the log for details."); } - UpdateDisplayInfoItems(); return true; } catch (Exception e) @@ -1543,7 +1589,6 @@ private bool ApplyAmdPreset(AmdPreset preset) throw new Exception("Error while applying AMD preset. At least one setting could not be applied. Check the log for details."); } - UpdateDisplayInfoItemsAmd(); return true; } catch (Exception e) @@ -1720,6 +1765,26 @@ private void miLgAddAction_Click(object sender, EventArgs e) AddToLgSteps(text); } + private void miLgAddNvPreset_Click(object sender, EventArgs e) + { + var item = sender as ToolStripItem; + var preset = item.Tag as NvPreset; + + var text = $"NvPreset({preset.name})"; + + AddToLgSteps(text); + } + + private void miLgAddAmdPreset_Click(object sender, EventArgs e) + { + var item = sender as ToolStripItem; + var preset = item.Tag as AmdPreset; + + var text = $"AmdPreset({preset.name})"; + + AddToLgSteps(text); + } + private void AddToLgSteps(string step) { var text = edtStepsLg.Text; @@ -2473,7 +2538,7 @@ private void mnuLgButtons_Opening(object sender, CancelEventArgs e) var device = _lgService.GetPresetDevice(preset); var actions = device?.GetInvokableActions(); - foreach (var action in actions) + foreach (var action in actions.Where(a => a.Category == null || !a.Category.Equals("other"))) { var text = action.Name; @@ -2482,6 +2547,45 @@ private void mnuLgButtons_Opening(object sender, CancelEventArgs e) item.Click += miLgAddAction_Click; } + if (_nvService != null) + { + mnuLgNvPresets.DropDownItems.Clear(); + + foreach (var nvPreset in _nvService.GetPresets()) + { + var text = nvPreset.name; + + if (!string.IsNullOrEmpty(text)) + { + var item = mnuLgNvPresets.DropDownItems.Add(text); + item.Tag = nvPreset; + item.Click += miLgAddNvPreset_Click; + } + } + } + + if (_amdService != null) + { + mnuLgAmdPresets.DropDownItems.Clear(); + + foreach (var amdPreset in _amdService.GetPresets()) + { + var text = amdPreset.name; + + if (!string.IsNullOrEmpty(text)) + { + var item = mnuLgAmdPresets.DropDownItems.Add(text); + item.Tag = amdPreset; + item.Click += miLgAddAmdPreset_Click; + } + } + } + + mnuLgNvPresets.Visible = _nvService != null; + mnuLgAmdPresets.Visible = _amdService != null; + + mnuLgNvPresets.Text = mnuLgNvPresets.DropDownItems.Count > 0 ? "NVIDIA presets" : "NVIDIA presets (no named presets found)"; + mnuLgAmdPresets.Text = mnuLgAmdPresets.DropDownItems.Count > 0 ? "AMD presets" : "AMD presets (no named presets found)"; } private void btnLgDeviceConvertToCustom_Click(object sender, EventArgs e) diff --git a/ColorControl/NvService.cs b/ColorControl/NvService.cs index 99e1aa3..6667541 100644 --- a/ColorControl/NvService.cs +++ b/ColorControl/NvService.cs @@ -68,7 +68,6 @@ public struct NV_GPU_DITHER_CONTROL_V1 }; private Display _currentDisplay; - private NvPreset _lastAppliedPreset; public NvService(string dataPath) : base(dataPath) { @@ -254,6 +253,8 @@ public bool ApplyPreset(NvPreset preset, AppContext appContext) _lastAppliedPreset = preset; + PresetApplied(); + return result; } diff --git a/ColorControl/Properties/AssemblyInfo.cs b/ColorControl/Properties/AssemblyInfo.cs index 710f786..f4e9166 100644 --- a/ColorControl/Properties/AssemblyInfo.cs +++ b/ColorControl/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // 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("3.2.2.0")] -[assembly: AssemblyFileVersion("3.2.2.0")] +[assembly: AssemblyVersion("3.3.0.0")] +[assembly: AssemblyFileVersion("3.3.0.0")] diff --git a/ColorControl/ServiceBase.cs b/ColorControl/ServiceBase.cs index 3611cda..354a44f 100644 --- a/ColorControl/ServiceBase.cs +++ b/ColorControl/ServiceBase.cs @@ -7,12 +7,15 @@ namespace ColorControl { abstract class ServiceBase where T : PresetBase { + public event EventHandler AfterApplyPreset; + protected string _dataPath; protected string _presetsFilename; protected JavaScriptSerializer _JsonSerializer; protected JavaScriptSerializer _JsonDeserializer; protected bool _initialized = false; protected List _presets; + protected T _lastAppliedPreset; public ServiceBase(string dataPath) { @@ -54,5 +57,10 @@ protected virtual void Uninitialize() { } + + protected void PresetApplied() + { + AfterApplyPreset?.Invoke(this, _lastAppliedPreset); + } } } diff --git a/ColorControl/lgtv/LgTvApi.cs b/ColorControl/lgtv/LgTvApi.cs index 56a4b91..18b820b 100644 --- a/ColorControl/lgtv/LgTvApi.cs +++ b/ColorControl/lgtv/LgTvApi.cs @@ -34,6 +34,12 @@ public enum OffToHigh high } + public enum OffToOn + { + off, + on + } + public enum OffToAuto { off,