2021-02-21 14:04:24 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Controls;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
using AudioSwitcher.AudioApi;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
using MassiveKnob.Plugin.CoreAudio.OSD;
|
2021-02-28 11:55:23 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
namespace MassiveKnob.Plugin.CoreAudio.GetMuted
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
public class DeviceGetMutedAction : IMassiveKnobAction
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
public Guid ActionId { get; } = new Guid("86646ca7-f472-4c5a-8d0f-7e5d2d162ab9");
|
|
|
|
|
public MassiveKnobActionType ActionType { get; } = MassiveKnobActionType.OutputDigital;
|
|
|
|
|
public string Name { get; } = Strings.GetMutedName;
|
|
|
|
|
public string Description { get; } = Strings.GetMutedDescription;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
|
|
|
|
|
2021-02-28 11:55:23 +01:00
|
|
|
|
public IMassiveKnobActionInstance Create(ILogger logger)
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
return new Instance();
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
private class Instance : IMassiveKnobActionInstance
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
private DeviceGetMutedActionSettings settings;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
private IDevice playbackDevice;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
private IDisposable deviceChanged;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobActionContext context)
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
actionContext = context;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
settings = context.GetSettings<DeviceGetMutedActionSettings>();
|
2021-02-24 09:05:11 +01:00
|
|
|
|
ApplySettings();
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
deviceChanged?.Dispose();
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-03-08 20:18:47 +01:00
|
|
|
|
if (playbackDevice == null || playbackDevice.Id != settings.DeviceId)
|
|
|
|
|
{
|
|
|
|
|
var coreAudioController = CoreAudioControllerInstance.Acquire();
|
|
|
|
|
playbackDevice = settings.DeviceId.HasValue
|
|
|
|
|
? coreAudioController.GetDevice(settings.DeviceId.Value)
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
deviceChanged?.Dispose();
|
|
|
|
|
deviceChanged = playbackDevice?.MuteChanged.Subscribe(MuteChanged);
|
|
|
|
|
}
|
2021-02-24 19:35:01 +01:00
|
|
|
|
|
2021-02-28 14:40:51 +01:00
|
|
|
|
if (playbackDevice != null)
|
|
|
|
|
actionContext.SetDigitalOutput(settings.Inverted ? !playbackDevice.IsMuted : playbackDevice.IsMuted);
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
public UserControl CreateSettingsControl()
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
var viewModel = new DeviceGetMutedActionSettingsViewModel(settings);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
return new DeviceGetMutedActionSettingsView(viewModel);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
|
|
|
|
|
private void MuteChanged(DeviceMuteChangedArgs args)
|
2021-02-24 09:05:11 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
actionContext.SetDigitalOutput(settings.Inverted ? !args.IsMuted : args.IsMuted);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
if (settings.OSD)
|
|
|
|
|
OSDManager.Show(args.Device);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|