/
MidiDeviceWatcher.cs
199 lines (182 loc) · 6.98 KB
/
MidiDeviceWatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using SDKTemplate;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.Midi;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace MIDI
{
/// <summary>
/// DeviceWatcher class to monitor adding/removing MIDI devices on the fly
/// </summary>
internal class MidiDeviceWatcher
{
internal DeviceWatcher deviceWatcher = null;
internal DeviceInformationCollection deviceInformationCollection = null;
bool enumerationCompleted = false;
ListBox portList = null;
string midiSelector = string.Empty;
CoreDispatcher coreDispatcher = null;
/// <summary>
/// Constructor: Initialize and hook up Device Watcher events
/// </summary>
/// <param name="midiSelectorString">MIDI Device Selector</param>
/// <param name="dispatcher">CoreDispatcher instance, to update UI thread</param>
/// <param name="portListBox">The UI element to update with list of devices</param>
internal MidiDeviceWatcher(string midiSelectorString, CoreDispatcher dispatcher, ListBox portListBox)
{
this.deviceWatcher = DeviceInformation.CreateWatcher(midiSelectorString);
this.portList = portListBox;
this.midiSelector = midiSelectorString;
this.coreDispatcher = dispatcher;
this.deviceWatcher.Added += DeviceWatcher_Added;
this.deviceWatcher.Removed += DeviceWatcher_Removed;
this.deviceWatcher.Updated += DeviceWatcher_Updated;
this.deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
}
/// <summary>
/// Destructor: Remove Device Watcher events
/// </summary>
~MidiDeviceWatcher()
{
this.deviceWatcher.Added -= DeviceWatcher_Added;
this.deviceWatcher.Removed -= DeviceWatcher_Removed;
this.deviceWatcher.Updated -= DeviceWatcher_Updated;
this.deviceWatcher.EnumerationCompleted -= DeviceWatcher_EnumerationCompleted;
}
/// <summary>
/// Start the Device Watcher
/// </summary>
internal void Start()
{
if(this.deviceWatcher.Status != DeviceWatcherStatus.Started)
{
this.deviceWatcher.Start();
}
}
/// <summary>
/// Stop the Device Watcher
/// </summary>
internal void Stop()
{
if(this.deviceWatcher.Status != DeviceWatcherStatus.Stopped)
{
this.deviceWatcher.Stop();
}
}
/// <summary>
/// Get the DeviceInformationCollection
/// </summary>
/// <returns></returns>
internal DeviceInformationCollection GetDeviceInformationCollection()
{
return this.deviceInformationCollection;
}
/// <summary>
/// Add any connected MIDI devices to the list
/// </summary>
private async void UpdateDevices()
{
// Get a list of all MIDI devices
this.deviceInformationCollection = await DeviceInformation.FindAllAsync(this.midiSelector);
// If no devices are found, update the ListBox
if ((this.deviceInformationCollection == null) || (this.deviceInformationCollection.Count == 0))
{
// Start with a clean list
this.portList.Items.Clear();
this.portList.Items.Add("No MIDI ports found");
this.portList.IsEnabled = false;
}
// If devices are found, enumerate them and add them to the list
else
{
// Start with a clean list
this.portList.Items.Clear();
foreach (var device in deviceInformationCollection)
{
this.portList.Items.Add(device.Name);
}
this.portList.IsEnabled = true;
}
}
/// <summary>
/// Update UI on device added
/// </summary>
/// <param name="sender">The active DeviceWatcher instance</param>
/// <param name="args">Event arguments</param>
private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
// If all devices have been enumerated
if (this.enumerationCompleted)
{
await coreDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
// Update the device list
UpdateDevices();
});
}
}
/// <summary>
/// Update UI on device removed
/// </summary>
/// <param name="sender">The active DeviceWatcher instance</param>
/// <param name="args">Event arguments</param>
private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
// If all devices have been enumerated
if (this.enumerationCompleted)
{
await coreDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
// Update the device list
UpdateDevices();
});
}
}
/// <summary>
/// Update UI on device updated
/// </summary>
/// <param name="sender">The active DeviceWatcher instance</param>
/// <param name="args">Event arguments</param>
private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
// If all devices have been enumerated
if (this.enumerationCompleted)
{
await coreDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
// Update the device list
UpdateDevices();
});
}
}
/// <summary>
/// Update UI on device enumeration completed.
/// </summary>
/// <param name="sender">The active DeviceWatcher instance</param>
/// <param name="args">Event arguments</param>
private async void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
{
this.enumerationCompleted = true;
await coreDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
// Update the device list
UpdateDevices();
});
}
}
}