2021-02-24 09:05:11 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using AudioSwitcher.AudioApi;
|
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
namespace MassiveKnob.Plugin.CoreAudio.Base
|
2021-02-24 09:05:11 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
public class BaseDeviceSettingsViewModel<T> : BaseDeviceSettingsViewModel where T : BaseDeviceSettings
|
|
|
|
|
{
|
|
|
|
|
protected new T Settings => (T)base.Settings;
|
|
|
|
|
|
|
|
|
|
public BaseDeviceSettingsViewModel(T settings) : base(settings)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
public class BaseDeviceSettingsViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
protected readonly BaseDeviceSettings Settings;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
private IList<PlaybackDeviceViewModel> playbackDevices;
|
|
|
|
|
private PlaybackDeviceViewModel selectedDevice;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable UnusedMember.Global - used by WPF Binding
|
|
|
|
|
public IList<PlaybackDeviceViewModel> PlaybackDevices
|
|
|
|
|
{
|
|
|
|
|
get => playbackDevices;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
playbackDevices = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlaybackDeviceViewModel SelectedDevice
|
|
|
|
|
{
|
|
|
|
|
get => selectedDevice;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == selectedDevice)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
selectedDevice = value;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
Settings.DeviceId = selectedDevice?.Id;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool OSD
|
|
|
|
|
{
|
|
|
|
|
get => Settings.OSD;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == Settings.OSD)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Settings.OSD = value;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ReSharper restore UnusedMember.Global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseDeviceSettingsViewModel(BaseDeviceSettings settings)
|
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
Settings = settings;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
var coreAudioController = CoreAudioControllerInstance.Acquire();
|
|
|
|
|
var devices = await coreAudioController.GetPlaybackDevicesAsync();
|
|
|
|
|
var deviceViewModels = devices
|
|
|
|
|
.OrderBy(d => d.FullName)
|
|
|
|
|
.Select(PlaybackDeviceViewModel.FromDevice)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2021-03-08 20:18:47 +01:00
|
|
|
|
Application.Current?.Dispatcher.Invoke(() =>
|
2021-02-24 09:05:11 +01:00
|
|
|
|
{
|
|
|
|
|
PlaybackDevices = deviceViewModels;
|
|
|
|
|
SelectedDevice = deviceViewModels.SingleOrDefault(d => d.Id == settings.DeviceId);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool IsSettingsProperty(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
return propertyName != nameof(PlaybackDevices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PlaybackDeviceViewModel
|
|
|
|
|
{
|
|
|
|
|
public Guid Id { get; private set; }
|
|
|
|
|
public string DisplayName { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static PlaybackDeviceViewModel FromDevice(IDevice device)
|
|
|
|
|
{
|
|
|
|
|
string displayFormat;
|
|
|
|
|
|
|
|
|
|
if ((device.State & DeviceState.Disabled) != 0)
|
|
|
|
|
displayFormat = Strings.DeviceDisplayNameDisabled;
|
|
|
|
|
else if ((device.State & DeviceState.Unplugged) != 0)
|
|
|
|
|
displayFormat = Strings.DeviceDisplayNameUnplugged;
|
|
|
|
|
else if ((device.State & DeviceState.NotPresent) != 0)
|
|
|
|
|
displayFormat = Strings.DeviceDisplayNameNotPresent;
|
|
|
|
|
else if ((device.State & DeviceState.Active) == 0)
|
|
|
|
|
displayFormat = Strings.DeviceDisplayNameInactive;
|
|
|
|
|
else
|
|
|
|
|
displayFormat = Strings.DeviceDisplayNameActive;
|
|
|
|
|
|
|
|
|
|
return new PlaybackDeviceViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = device.Id,
|
|
|
|
|
DisplayName = string.Format(displayFormat, device.FullName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|