Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions VolumeControlPlugin/Commands/InputAdjustment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
44 changes: 41 additions & 3 deletions VolumeControlPlugin/Commands/SetDefaultDeviceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion VolumeControlPlugin/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions VolumeControlPlugin/Helpers/PluginLog.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions VolumeControlPlugin/VolumeControlPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Loupedeck.VolumeControlPlugin.Helpers;
using Loupedeck.VolumeControlPlugin.Services;

namespace Loupedeck.VolumeControlPlugin
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions VolumeControlPlugin/VolumeControlPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Commands\InputAdjustment.cs" />
<Compile Include="Commands\SetDefaultDeviceCommand.cs" />
<Compile Include="Helpers\DeviceHelper.cs" />
<Compile Include="Helpers\PluginLog.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\NotificationService.cs" />
<Compile Include="VolumeControlApplication.cs" />
Expand Down