From 13c849eb11e069e6ee1b7683c3c7d3a9050e8fb3 Mon Sep 17 00:00:00 2001 From: Priit Liivak Date: Sun, 4 Feb 2024 17:23:12 +0200 Subject: [PATCH] multiple states for commands. Logging capability --- .../Commands/InputAdjustment.cs | 9 ++++ .../Commands/SetDefaultDeviceCommand.cs | 44 +++++++++++++++++-- VolumeControlPlugin/Helpers/DeviceHelper.cs | 11 ++++- VolumeControlPlugin/Helpers/PluginLog.cs | 33 ++++++++++++++ VolumeControlPlugin/VolumeControlPlugin.cs | 7 +++ .../VolumeControlPlugin.csproj | 1 + 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 VolumeControlPlugin/Helpers/PluginLog.cs diff --git a/VolumeControlPlugin/Commands/InputAdjustment.cs b/VolumeControlPlugin/Commands/InputAdjustment.cs index 8a21cc3..d9b592a 100644 --- a/VolumeControlPlugin/Commands/InputAdjustment.cs +++ b/VolumeControlPlugin/Commands/InputAdjustment.cs @@ -47,7 +47,11 @@ protected override void RunCommand(string actionParameter) var device = _deviceHelper.GetDevice(actionParameter); if (_deviceHelper.IsDisabled(device)) +{ + //Update image in case the device was disconnected/disabled + base.ActionImageChanged(actionParameter); return; +} Task.Run(() => device.SetMuteAsync(!device.IsMuted)).GetAwaiter().GetResult(); base.ActionImageChanged(actionParameter); @@ -60,7 +64,12 @@ protected override void ApplyAdjustment(string actionParameter, int ticks) var device = _deviceHelper.GetDevice(actionParameter); if (_deviceHelper.IsDisabled(device)) +{ + //Update image in case the device was disconnected/disabled + base.ActionImageChanged(actionParameter); return; +} + var volume = device.Volume + ticks; if (volume > 100) diff --git a/VolumeControlPlugin/Commands/SetDefaultDeviceCommand.cs b/VolumeControlPlugin/Commands/SetDefaultDeviceCommand.cs index d6d7033..d4938ac 100644 --- a/VolumeControlPlugin/Commands/SetDefaultDeviceCommand.cs +++ b/VolumeControlPlugin/Commands/SetDefaultDeviceCommand.cs @@ -3,10 +3,12 @@ using CoreDeviceType = AudioSwitcher.AudioApi.DeviceType; using Loupedeck.VolumeControlPlugin.Helpers; +using AudioSwitcher.AudioApi.CoreAudio; +using System; namespace Loupedeck.VolumeControlPlugin.Commands { - abstract class SetDefaultDeviceCommand : PluginDynamicCommand + abstract class SetDefaultDeviceCommand : PluginMultistateDynamicCommand { private readonly bool _isCommunication; private readonly CoreDeviceType _deviceType; @@ -18,6 +20,12 @@ abstract class SetDefaultDeviceCommand : PluginDynamicCommand protected SetDefaultDeviceCommand(bool isCommunication, CoreDeviceType deviceType, string groupName) { + PluginLog.Info("Initializing SetDefaultDeviceCommand: "+ groupName); + + this.AddState("Not default device", "Not selected as default"); + this.AddState("Default device", "Selected as default"); + this.AddState("Device disabled", "Not found or disabled"); // Displayed also, when device is disconnected + _isCommunication = isCommunication; _deviceType = deviceType; _groupName = groupName; @@ -42,6 +50,7 @@ protected override bool OnLoad() foreach (var device in devices) { this.AddParameter(device.Key, device.Value.FullName, _groupName); + this.SetCurrentState(device.Key, this.GetDeviceCommandStateIndex(device.Value)); // Set initial state } return true; @@ -56,11 +65,40 @@ protected override void RunCommand(string actionParameter) if (_deviceHelper.IsDisabled(device)) return; + PluginLog.Info("Setting as default: " + device.FullName); if (_isCommunication) - Task.Run(() => device.SetAsDefaultCommunicationsAsync()).GetAwaiter().GetResult(); + Task.Run(() => device.SetAsDefaultCommunicationsAsync()).GetAwaiter().OnCompleted(this.setDefaultCompleted); else - Task.Run(() => device.SetAsDefaultAsync()).GetAwaiter().GetResult(); + Task.Run(() => device.SetAsDefaultAsync()).GetAwaiter().OnCompleted(this.setDefaultCompleted); + } + + internal void setDefaultCompleted() + { + foreach (var deviceKey in this.GetParameters()) + { + var deviceKeyValue = deviceKey.Value; + + var device = _deviceHelper.GetDevice(deviceKeyValue); + this.SetCurrentState(deviceKeyValue, this.GetDeviceCommandStateIndex(device)); + } + } + + internal Int32 GetDeviceCommandStateIndex(CoreAudioDevice device) + { + + if (device is null || _deviceHelper.IsDisabled(device)) + return 2; // Disabled + + + if (_deviceHelper.IsActive(device, _isCommunication)) + return 1; // Selected/default device + + + return 0; // Not default (available) device + + } + } class SetDefaultPlaybackDevice : SetDefaultDeviceCommand diff --git a/VolumeControlPlugin/Helpers/DeviceHelper.cs b/VolumeControlPlugin/Helpers/DeviceHelper.cs index 91e8cbf..d22d93c 100644 --- a/VolumeControlPlugin/Helpers/DeviceHelper.cs +++ b/VolumeControlPlugin/Helpers/DeviceHelper.cs @@ -20,7 +20,16 @@ internal bool IsDisabled(CoreAudioDevice device) return device.State != DeviceState.Active; } - internal CoreAudioDevice GetDevice( string actionParameter) + internal bool IsActive(CoreAudioDevice device, bool isCommunication) + { + if (device is null) + return false; + + return isCommunication ? device.IsDefaultCommunicationsDevice : device.IsDefaultDevice; + + } + + internal CoreAudioDevice GetDevice(string actionParameter) { if (string.IsNullOrWhiteSpace(actionParameter)) return null; diff --git a/VolumeControlPlugin/Helpers/PluginLog.cs b/VolumeControlPlugin/Helpers/PluginLog.cs new file mode 100644 index 0000000..d91b81f --- /dev/null +++ b/VolumeControlPlugin/Helpers/PluginLog.cs @@ -0,0 +1,33 @@ +namespace Loupedeck.VolumeControlPlugin.Helpers +{ + using System; + + // A helper class that enables logging from the plugin code. + + internal static class PluginLog + { + private static PluginLogFile _pluginLogFile; + + public static void Init(PluginLogFile pluginLogFile) + { + pluginLogFile.CheckNullArgument(nameof(pluginLogFile)); + PluginLog._pluginLogFile = pluginLogFile; + } + + public static void Verbose(String text) => PluginLog._pluginLogFile?.Verbose(text); + + public static void Verbose(Exception ex, String text) => PluginLog._pluginLogFile?.Verbose(ex, text); + + public static void Info(String text) => PluginLog._pluginLogFile?.Info(text); + + public static void Info(Exception ex, String text) => PluginLog._pluginLogFile?.Info(ex, text); + + public static void Warning(String text) => PluginLog._pluginLogFile?.Warning(text); + + public static void Warning(Exception ex, String text) => PluginLog._pluginLogFile?.Warning(ex, text); + + public static void Error(String text) => PluginLog._pluginLogFile?.Error(text); + + public static void Error(Exception ex, String text) => PluginLog._pluginLogFile?.Error(ex, text); + } +} diff --git a/VolumeControlPlugin/VolumeControlPlugin.cs b/VolumeControlPlugin/VolumeControlPlugin.cs index 037b9b3..a70023e 100644 --- a/VolumeControlPlugin/VolumeControlPlugin.cs +++ b/VolumeControlPlugin/VolumeControlPlugin.cs @@ -1,3 +1,4 @@ +using Loupedeck.VolumeControlPlugin.Helpers; using Loupedeck.VolumeControlPlugin.Services; namespace Loupedeck.VolumeControlPlugin @@ -9,6 +10,12 @@ public class VolumeControlPlugin : Plugin public override bool HasNoApplication => true; public override bool UsesApplicationApiOnly => true; + public VolumeControlPlugin() + { + // Initialize the plugin log. + PluginLog.Init(this.Log); + } + public override void Load() { LoadPluginIcons(); diff --git a/VolumeControlPlugin/VolumeControlPlugin.csproj b/VolumeControlPlugin/VolumeControlPlugin.csproj index 2b29c9d..77ee618 100644 --- a/VolumeControlPlugin/VolumeControlPlugin.csproj +++ b/VolumeControlPlugin/VolumeControlPlugin.csproj @@ -60,6 +60,7 @@ +