2021-02-21 13:04:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Controls;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
using AudioSwitcher.AudioApi;
|
2021-02-24 18:35:01 +00:00
|
|
|
|
using MassiveKnob.Plugin.CoreAudio.OSD;
|
2021-02-28 10:55:23 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 18:35:01 +00:00
|
|
|
|
namespace MassiveKnob.Plugin.CoreAudio.GetVolume
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 18:35:01 +00:00
|
|
|
|
public class DeviceGetVolumeAction : IMassiveKnobAction
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 18:35:01 +00:00
|
|
|
|
public Guid ActionId { get; } = new Guid("6ebf91af-8240-4a75-9729-c6a1eb60dcba");
|
|
|
|
|
public MassiveKnobActionType ActionType { get; } = MassiveKnobActionType.OutputAnalog;
|
|
|
|
|
public string Name { get; } = Strings.GetVolumeName;
|
|
|
|
|
public string Description { get; } = Strings.GetVolumeDescription;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
|
|
|
|
|
2021-02-28 10:55:23 +00:00
|
|
|
|
public IMassiveKnobActionInstance Create(ILogger logger)
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
return new Instance();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 18:35:01 +00:00
|
|
|
|
private class Instance : IMassiveKnobActionInstance
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
2021-02-24 18:35:01 +00:00
|
|
|
|
private DeviceGetVolumeActionSettings settings;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
private IDevice playbackDevice;
|
2021-02-24 18:35:01 +00:00
|
|
|
|
private IDisposable deviceChanged;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobActionContext context)
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
actionContext = context;
|
2021-02-24 18:35:01 +00:00
|
|
|
|
settings = context.GetSettings<DeviceGetVolumeActionSettings>();
|
2021-02-24 08:05:11 +00:00
|
|
|
|
ApplySettings();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-02-24 18:35:01 +00:00
|
|
|
|
deviceChanged?.Dispose();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-03-08 19:18:47 +00: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?.VolumeChanged.Subscribe(VolumeChanged);
|
|
|
|
|
}
|
2021-02-24 18:35:01 +00:00
|
|
|
|
|
2021-02-28 13:40:51 +00:00
|
|
|
|
if (playbackDevice != null)
|
|
|
|
|
actionContext.SetAnalogOutput((byte)playbackDevice.Volume);
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
public UserControl CreateSettingsControl()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 18:35:01 +00:00
|
|
|
|
var viewModel = new DeviceGetVolumeActionSettingsViewModel(settings);
|
2021-02-24 08:05:11 +00:00
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 18:35:01 +00:00
|
|
|
|
return new DeviceGetVolumeActionSettingsView(viewModel);
|
2021-02-24 08:05:11 +00:00
|
|
|
|
}
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 18:35:01 +00:00
|
|
|
|
|
|
|
|
|
private void VolumeChanged(DeviceVolumeChangedArgs args)
|
2021-02-24 08:05:11 +00:00
|
|
|
|
{
|
2021-02-24 18:35:01 +00:00
|
|
|
|
actionContext.SetAnalogOutput((byte)args.Volume);
|
|
|
|
|
|
|
|
|
|
if (settings.OSD)
|
|
|
|
|
OSDManager.Show(args.Device);
|
2021-02-24 08:05:11 +00:00
|
|
|
|
}
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|