-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEventHandlerForDevice.h
220 lines (183 loc) · 7.03 KB
/
EventHandlerForDevice.h
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#include "pch.h"
namespace VBA10
{
//class to represent connected event arguments
public ref class OnDeviceConnectedEventArgs sealed
{
public:
OnDeviceConnectedEventArgs(bool isDeviceSuccessfullyConnected, Windows::Devices::Enumeration::DeviceInformation^ deviceInformation)
{
this->isDeviceSuccessfullyConnected = isDeviceSuccessfullyConnected;
this->deviceInformation = deviceInformation;
}
property bool IsDeviceSuccessfullyConnected
{
bool get(void)
{
return isDeviceSuccessfullyConnected;
}
}
property Windows::Devices::Enumeration::DeviceInformation^ DeviceInformation
{
Windows::Devices::Enumeration::DeviceInformation^ get(void)
{
return deviceInformation;
}
}
private:
bool isDeviceSuccessfullyConnected;
Windows::Devices::Enumeration::DeviceInformation^ deviceInformation;
};
/// <summary>
/// The purpose of this class is to demonstrate what to do to a HidDevice when a specific app event
/// is raised (app suspension and resume) or when the device is disconnected. In addition to handling
/// the HidDevice, the app's state should also be saved upon app suspension (will not be demonstrated here).
///
/// This class will also demonstrate how to handle device watcher events.
///
/// For simplicity, this class will only allow at most one device to be connected at any given time. In order
/// to make this class support multiple devices, make this class a non-singleton and create multiple instances
/// of this class; each instance should watch one connected device.
/// </summary>
[Windows::Foundation::Metadata::WebHostHidden]
public ref class EventHandlerForDevice sealed
{
public:
static property EventHandlerForDevice^ Current
{
EventHandlerForDevice^ get(void);
}
static void CreateNewEventHandlerForDevice(void);
property Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, Windows::Devices::Enumeration::DeviceInformation^>^ OnDeviceClose
{
Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, Windows::Devices::Enumeration::DeviceInformation^>^ get(void)
{
return deviceCloseCallback;
}
void set(Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, Windows::Devices::Enumeration::DeviceInformation^>^ newHandler)
{
deviceCloseCallback = newHandler;
}
}
property Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, OnDeviceConnectedEventArgs^>^ OnDeviceConnected
{
Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, OnDeviceConnectedEventArgs^>^ get(void)
{
return deviceConnectedCallback;
}
void set(Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, OnDeviceConnectedEventArgs^>^ newHandler)
{
deviceConnectedCallback = newHandler;
}
}
property bool IsDeviceConnected
{
bool get(void)
{
return device != nullptr;
}
}
property Windows::Devices::HumanInterfaceDevice::HidDevice^ Device
{
Windows::Devices::HumanInterfaceDevice::HidDevice^ get(void)
{
return device;
}
}
/// <summary>
/// This DeviceInformation represents which device is connected or which device will be reconnected when
/// the device is plugged in again (if IsEnabledAutoReconnect is true);.
/// </summary>
property Windows::Devices::Enumeration::DeviceInformation^ DeviceInformation
{
Windows::Devices::Enumeration::DeviceInformation^ get(void)
{
return deviceInformation;
}
}
/// <summary>
/// Returns DeviceAccessInformation for the device that is currently connected using this EventHandlerForDevice
/// object.
/// </summary>
property Windows::Devices::Enumeration::DeviceAccessInformation^ DeviceAccessInformation
{
Windows::Devices::Enumeration::DeviceAccessInformation^ get(void)
{
return deviceAccessInformation;
}
}
/// <summary>
/// DeviceSelector AQS used to find this device
/// </summary>
property Platform::String^ DeviceSelector
{
Platform::String^ get(void)
{
return deviceSelector;
}
}
/// <summary>
/// True if EventHandlerForDevice will attempt to reconnect to the device once it is plugged into the computer again
/// </summary>
property bool IsEnabledAutoReconnect
{
bool get(void)
{
return isEnabledAutoReconnect;
}
void set(bool value)
{
isEnabledAutoReconnect = value;
}
}
Windows::Foundation::IAsyncOperation<bool>^ OpenDeviceAsync(Windows::Devices::Enumeration::DeviceInformation^ deviceInfo);
void CloseDevice(void);
private:
/// <summary>
/// Allows for singleton EventHandlerForDevice
/// </summary>
static EventHandlerForDevice^ eventHandlerForDevice;
/// <summary>
/// Used to synchronize threads to avoid multiple instantiations of eventHandlerForDevice.
/// </summary>
static SRWLOCK srwSingletonCreationLock;
Windows::Devices::Enumeration::DeviceWatcher^ deviceWatcher;
Platform::String^ deviceSelector;
Windows::Devices::Enumeration::DeviceInformation^ deviceInformation;
Windows::Devices::Enumeration::DeviceAccessInformation^ deviceAccessInformation;
Windows::Devices::HumanInterfaceDevice::HidDevice^ device;
Windows::Foundation::EventRegistrationToken appSuspendEventToken;
Windows::Foundation::EventRegistrationToken appResumeEventToken;
Windows::Foundation::EventRegistrationToken deviceAddedEventToken;
Windows::Foundation::EventRegistrationToken deviceRemovedEventToken;
Windows::Foundation::EventRegistrationToken deviceAccessChangedEventToken;
Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, Windows::Devices::Enumeration::DeviceInformation^>^ deviceCloseCallback;
Windows::Foundation::TypedEventHandler<EventHandlerForDevice^, OnDeviceConnectedEventArgs^>^ deviceConnectedCallback;
bool watcherSuspended;
bool watcherStarted;
bool isEnabledAutoReconnect;
EventHandlerForDevice(void);
~EventHandlerForDevice();
void CloseCurrentlyConnectedDevice(void);
void RegisterForAppEvents(void);
void UnregisterFromAppEvents(void);
void RegisterForDeviceAccessStatusChange();
void UnregisterFromDeviceAccessStatusChange();
void RegisterForDeviceWatcherEvents(void);
void UnregisterFromDeviceWatcherEvents(void);
void StartDeviceWatcher(void);
void StopDeviceWatcher(void);
void OnAppSuspension(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ args);
void OnAppResume(Platform::Object^ sender, Platform::Object^ args);
void OnDeviceRemoved(
Windows::Devices::Enumeration::DeviceWatcher^ sender,
Windows::Devices::Enumeration::DeviceInformationUpdate^ deviceInformationUpdate);
void OnDeviceAdded(
Windows::Devices::Enumeration::DeviceWatcher^ sender,
Windows::Devices::Enumeration::DeviceInformation^ deviceInfo);
void OnDeviceAccessChanged(
Windows::Devices::Enumeration::DeviceAccessInformation^ sender,
Windows::Devices::Enumeration::DeviceAccessChangedEventArgs^ eventArgs);
};
}