2021-02-19 16:42:37 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
using Dapplo.Windows.Devices;
|
|
|
|
|
using Dapplo.Windows.Devices.Enums;
|
2021-02-19 16:42:37 +00:00
|
|
|
|
using MassiveKnob.Hardware;
|
|
|
|
|
using MassiveKnob.Settings;
|
|
|
|
|
using MassiveKnob.UserControls;
|
|
|
|
|
using Nito.AsyncEx;
|
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Forms
|
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
public partial class SettingsForm : Form, IMassiveKnobHardwareObserver, IObserver<DeviceNotificationEvent>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IAudioDeviceManager audioDeviceManager;
|
|
|
|
|
private readonly IMassiveKnobHardwareFactory massiveKnobHardwareFactory;
|
|
|
|
|
private readonly List<KnobDeviceControl> knobDeviceControls = new List<KnobDeviceControl>();
|
|
|
|
|
|
|
|
|
|
private bool loading = true;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private string lastConnectedPort = null;
|
|
|
|
|
private IDisposable deviceSubscription;
|
2021-02-19 16:42:37 +00:00
|
|
|
|
private IMassiveKnobHardware hardware;
|
|
|
|
|
private IAudioDevice[] devices;
|
|
|
|
|
private Settings.Settings settings;
|
|
|
|
|
|
|
|
|
|
private readonly AsyncLock saveSettingsLock = new AsyncLock();
|
|
|
|
|
private readonly AsyncLock setVolumeLock = new AsyncLock();
|
|
|
|
|
|
|
|
|
|
private bool startupVisibleCalled;
|
|
|
|
|
private bool closing;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SettingsForm(IAudioDeviceManagerFactory audioDeviceManagerFactory, IMassiveKnobHardwareFactory massiveKnobHardwareFactory)
|
|
|
|
|
{
|
|
|
|
|
audioDeviceManager = audioDeviceManagerFactory.Create();
|
|
|
|
|
this.massiveKnobHardwareFactory = massiveKnobHardwareFactory;
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
SerialPortStatusLabel.Text = Strings.StatusNotConnected;
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
// Due to the form not being visible initially (see SetVisibleCore), we can't use the Load event
|
|
|
|
|
AsyncLoad();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 16:42:37 +00:00
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private async void AsyncLoad()
|
|
|
|
|
{
|
|
|
|
|
await LoadSettings();
|
2021-02-19 16:42:37 +00:00
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
LoadSerialPorts(),
|
|
|
|
|
LoadAudioDevices()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
deviceSubscription = DeviceNotification.OnNotification.Subscribe(this);
|
|
|
|
|
|
|
|
|
|
loading = false;
|
|
|
|
|
await Connect();
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
2021-02-20 11:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void RunInUIContext(Action action)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (InvokeRequired)
|
|
|
|
|
Invoke(action);
|
|
|
|
|
else
|
|
|
|
|
action();
|
|
|
|
|
}
|
2021-02-20 11:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 16:42:37 +00:00
|
|
|
|
private Task LoadSerialPorts()
|
|
|
|
|
{
|
|
|
|
|
var portNames = SerialPort.GetPortNames();
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() =>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
SerialPortCombobox.BeginUpdate();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SerialPortCombobox.Items.Clear();
|
|
|
|
|
foreach (var portName in portNames)
|
|
|
|
|
{
|
|
|
|
|
var itemIndex = SerialPortCombobox.Items.Add(portName);
|
|
|
|
|
|
|
|
|
|
if (portName == settings.SerialPort)
|
|
|
|
|
SerialPortCombobox.SelectedIndex = itemIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SerialPortCombobox.EndUpdate();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task LoadSettings()
|
|
|
|
|
{
|
|
|
|
|
var newSettings = await SettingsJsonSerializer.Deserialize();
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() => SetSettings(newSettings));
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private async Task SaveSettings()
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (settings == null)
|
|
|
|
|
return;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
|
|
|
|
|
using (await saveSettingsLock.LockAsync())
|
|
|
|
|
{
|
|
|
|
|
await SettingsJsonSerializer.Serialize(settings);
|
|
|
|
|
}
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task Connect()
|
|
|
|
|
{
|
|
|
|
|
string serialPort = null;
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() =>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
serialPort = (string)SerialPortCombobox.SelectedItem;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(serialPort))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (hardware != null)
|
|
|
|
|
{
|
|
|
|
|
hardware.DetachObserver(this);
|
|
|
|
|
await hardware.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hardware = massiveKnobHardwareFactory.Create(serialPort);
|
|
|
|
|
hardware.AttachObserver(this);
|
|
|
|
|
|
|
|
|
|
await hardware.TryConnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task LoadAudioDevices()
|
|
|
|
|
{
|
|
|
|
|
var newDevices = await audioDeviceManager.GetDevices();
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() => SetDevices(newDevices));
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SetSettings(Settings.Settings value)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SerialPortCombobox.SelectedItem = value.SerialPort;
|
|
|
|
|
|
|
|
|
|
// No need to update the knob device user controls, as they are not loaded yet
|
2021-02-20 11:16:18 +00:00
|
|
|
|
// (guaranteed by the order in AsyncLoad)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
|
|
|
|
|
settings = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
|
2021-02-19 16:42:37 +00:00
|
|
|
|
private void SetDevices(IEnumerable<IAudioDevice> value)
|
|
|
|
|
{
|
|
|
|
|
devices = value.ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var knobDeviceControl in knobDeviceControls)
|
|
|
|
|
knobDeviceControl.SetDevices(devices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SetKnobCount(int count)
|
|
|
|
|
{
|
|
|
|
|
if (count == knobDeviceControls.Count)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SuspendLayout();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DeviceCountUnknownLabel.Visible = count == 0;
|
|
|
|
|
|
|
|
|
|
if (knobDeviceControls.Count > count)
|
|
|
|
|
{
|
|
|
|
|
for (var i = count; i < knobDeviceControls.Count; i++)
|
|
|
|
|
knobDeviceControls[i].Dispose();
|
|
|
|
|
|
|
|
|
|
knobDeviceControls.RemoveRange(count, knobDeviceControls.Count - count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = knobDeviceControls.Count; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var knobDeviceControl = new KnobDeviceControl
|
|
|
|
|
{
|
|
|
|
|
Left = 0,
|
|
|
|
|
Width = DevicesPanel.Width
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
knobDeviceControl.Top = i * knobDeviceControl.Height;
|
|
|
|
|
knobDeviceControl.Parent = DevicesPanel;
|
|
|
|
|
|
|
|
|
|
knobDeviceControl.SetKnobIndex(i);
|
|
|
|
|
knobDeviceControl.SetDevices(devices);
|
|
|
|
|
|
|
|
|
|
if (i < settings.Knobs.Count)
|
|
|
|
|
knobDeviceControl.SetDeviceId(settings.Knobs[i].DeviceId);
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
knobDeviceControl.OnDeviceChanged += async (sender, args) =>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
while (settings.Knobs.Count - 1 < args.KnobIndex)
|
|
|
|
|
settings.Knobs.Add(new Settings.Settings.KnobSettings());
|
|
|
|
|
|
|
|
|
|
settings.Knobs[args.KnobIndex].DeviceId = args.DeviceId;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
await SaveSettings();
|
2021-02-19 16:42:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
knobDeviceControls.Add(knobDeviceControl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var expectedHeight = knobDeviceControls.Count > 0
|
|
|
|
|
? knobDeviceControls[0].Height * count
|
|
|
|
|
: DeviceCountUnknownLabel.Height;
|
|
|
|
|
|
|
|
|
|
if (expectedHeight == DevicesPanel.Height)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var diff = expectedHeight - DevicesPanel.Height;
|
|
|
|
|
Height += diff;
|
|
|
|
|
Top -= diff / 2;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ResumeLayout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override void SetVisibleCore(bool value)
|
|
|
|
|
{
|
|
|
|
|
// Prevent the form from showing at startup
|
|
|
|
|
if (!startupVisibleCalled)
|
2021-02-20 11:16:18 +00:00
|
|
|
|
{
|
2021-02-19 16:42:37 +00:00
|
|
|
|
startupVisibleCalled = true;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
|
|
|
|
|
// Make sure the underlying window is still created, otherwise Close won't work
|
|
|
|
|
if (!IsHandleCreated)
|
|
|
|
|
CreateHandle();
|
|
|
|
|
}
|
2021-02-19 16:42:37 +00:00
|
|
|
|
else
|
|
|
|
|
base.SetVisibleCore(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Settings()
|
|
|
|
|
{
|
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private async Task Quit()
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
Hide();
|
|
|
|
|
|
|
|
|
|
deviceSubscription?.Dispose();
|
|
|
|
|
|
|
|
|
|
foreach (var knobDeviceControl in knobDeviceControls)
|
|
|
|
|
knobDeviceControl.Dispose();
|
|
|
|
|
|
|
|
|
|
knobDeviceControls.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (hardware != null)
|
|
|
|
|
{
|
|
|
|
|
hardware.DetachObserver(this);
|
|
|
|
|
await hardware.Disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audioDeviceManager?.Dispose();
|
|
|
|
|
|
2021-02-19 16:42:37 +00:00
|
|
|
|
closing = true;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
public void Connecting()
|
|
|
|
|
{
|
|
|
|
|
RunInUIContext(() =>
|
|
|
|
|
{
|
|
|
|
|
SerialPortStatusLabel.Text = Strings.StatusConnecting;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-19 16:42:37 +00:00
|
|
|
|
public void Connected(int knobCount)
|
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() =>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
SerialPortStatusLabel.Text = Strings.StatusConnected;
|
|
|
|
|
SetKnobCount(knobCount);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Disconnected()
|
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
RunInUIContext(() =>
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
SerialPortStatusLabel.Text = Strings.StatusNotConnected;
|
2021-02-20 11:16:18 +00:00
|
|
|
|
lastConnectedPort = null;
|
2021-02-19 16:42:37 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
public async void VolumeChanged(int knob, int volume)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (knob >= settings.Knobs.Count)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!settings.Knobs[knob].DeviceId.HasValue)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var deviceId = settings.Knobs[knob].DeviceId.Value;
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
using (await setVolumeLock.LockAsync())
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
var device = await audioDeviceManager.GetDeviceById(deviceId);
|
|
|
|
|
if (device != null)
|
|
|
|
|
await device.SetVolume(volume);
|
|
|
|
|
}
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (closing)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Hide();
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Settings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private async void QuitToolStripMenuItem_Click(object sender, EventArgs e)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
await Quit();
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Settings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void CloseButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
private async void SerialPortCombobox_SelectedIndexChanged(object sender, EventArgs e)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
{
|
2021-02-20 11:16:18 +00:00
|
|
|
|
var newPort = (string) SerialPortCombobox.SelectedItem;
|
|
|
|
|
if (loading || newPort == lastConnectedPort)
|
2021-02-19 16:42:37 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2021-02-20 11:16:18 +00:00
|
|
|
|
lastConnectedPort = newPort;
|
|
|
|
|
if (settings.SerialPort != newPort)
|
|
|
|
|
{
|
|
|
|
|
settings.SerialPort = (string) SerialPortCombobox.SelectedItem;
|
|
|
|
|
await SaveSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async void OnNext(DeviceNotificationEvent value)
|
|
|
|
|
{
|
|
|
|
|
if ((value.EventType == DeviceChangeEvent.DeviceArrival ||
|
|
|
|
|
value.EventType == DeviceChangeEvent.DeviceRemoveComplete) &&
|
|
|
|
|
value.Is(DeviceBroadcastDeviceType.DeviceInterface))
|
|
|
|
|
{
|
|
|
|
|
await LoadSerialPorts();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnError(Exception error)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted()
|
|
|
|
|
{
|
2021-02-19 16:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|