-
Notifications
You must be signed in to change notification settings - Fork 3
/
EventMonitor.cpp
269 lines (227 loc) · 8 KB
/
EventMonitor.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "nilPnP.h"
#include "nilUtil.h"
namespace Nil {
const wchar_t* cEventMonitorClass = L"NIL_MONITOR";
EventMonitor::EventMonitor( HINSTANCE instance, const Cooperation coop ):
mInstance( instance ), mClass( 0 ), mWindow( 0 ), mNotifications( 0 ),
mInputBuffer( nullptr ), mCooperation( coop ),
mInputBufferSize( 10240 ) // 10KB default
{
WNDCLASSEXW wx = { 0 };
wx.cbSize = sizeof( WNDCLASSEXW );
wx.lpfnWndProc = wndProc;
wx.hInstance = mInstance;
wx.lpszClassName = cEventMonitorClass;
mClass = RegisterClassExW( &wx );
if ( !mClass )
NIL_EXCEPT_WINAPI( "Window class registration failed" );
mWindow = CreateWindowExW(
0, (LPCWSTR)mClass, nullptr, 0, 0, 0, 0, 0, 0, 0, mInstance, this ); //-V542
if ( !mWindow )
NIL_EXCEPT_WINAPI( "Window creation failed" );
mInputBuffer = malloc( (size_t)mInputBufferSize );
if ( !mInputBuffer )
NIL_EXCEPT( "Couldn't allocate input read buffer" );
registerNotifications();
}
void EventMonitor::registerPnPListener( PnPListener* listener )
{
mPnPListeners.push_back( listener );
}
void EventMonitor::unregisterPnPListener( PnPListener* listener )
{
mPnPListeners.remove( listener );
}
void EventMonitor::registerRawListener( RawListener* listener )
{
mRawListeners.push_back( listener );
}
void EventMonitor::unregisterRawListener( RawListener* listener )
{
mRawListeners.remove( listener );
}
void EventMonitor::handleInterfaceArrival( const GUID& deviceClass,
const wideString& devicePath )
{
for ( auto listener : mPnPListeners )
listener->onPnPPlug( deviceClass, devicePath );
}
void EventMonitor::handleInterfaceRemoval( const GUID& deviceClass,
const wideString& devicePath )
{
for ( auto listener : mPnPListeners )
listener->onPnPUnplug( deviceClass, devicePath );
}
void EventMonitor::handleRawArrival( HANDLE handle )
{
for ( auto listener : mRawListeners )
listener->onRawArrival( handle );
}
void EventMonitor::handleRawInput( HRAWINPUT input, const bool sinked )
{
unsigned int dataSize;
// TODO:LOW We could probably get away with just one GetRawInputData call?
if ( GetRawInputData( input, RID_INPUT, NULL, &dataSize, sizeof( RAWINPUTHEADER ) ) == (UINT)-1 )
return;
if ( !dataSize )
return;
// Resize our input buffer if packet size exceeds previous cap
if ( dataSize > mInputBufferSize )
{
mInputBufferSize = dataSize;
mInputBuffer = realloc( mInputBuffer, (size_t)dataSize );
if ( !mInputBuffer )
NIL_EXCEPT( "Couldn't reallocate input read buffer" );
}
if ( GetRawInputData( input, RID_INPUT, mInputBuffer, &dataSize, sizeof( RAWINPUTHEADER ) ) == (UINT)-1 )
return;
auto raw = (const RAWINPUT*)mInputBuffer;
// Ping our listeners
if ( raw->header.dwType == RIM_TYPEMOUSE )
{
for ( auto listener : mRawListeners )
listener->onRawMouseInput( raw->header.hDevice, raw->data.mouse, sinked );
}
else if ( raw->header.dwType == RIM_TYPEKEYBOARD )
{
for ( auto listener : mRawListeners )
listener->onRawKeyboardInput( raw->header.hDevice, raw->data.keyboard, sinked );
}
}
void EventMonitor::handleRawRemoval( HANDLE handle )
{
for ( auto listener : mRawListeners )
listener->onRawRemoval( handle );
}
void EventMonitor::registerNotifications()
{
DEV_BROADCAST_DEVICEINTERFACE filter;
ZeroMemory( &filter, sizeof( filter ) );
filter.dbcc_size = sizeof( filter );
filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_classguid = g_HIDInterfaceGUID;
mNotifications = RegisterDeviceNotificationW( mWindow, &filter,
DEVICE_NOTIFY_WINDOW_HANDLE );
if ( !mNotifications )
NIL_EXCEPT_WINAPI( "Couldn't register for device interface notifications" );
RAWINPUTDEVICE rawDevices[2];
rawDevices[0].dwFlags =
( mCooperation == Cooperation_Background )
? RIDEV_DEVNOTIFY | RIDEV_INPUTSINK
: RIDEV_DEVNOTIFY;
rawDevices[0].hwndTarget = mWindow;
rawDevices[0].usUsagePage = USBUsagePage_Desktop;
rawDevices[0].usUsage = USBDesktopUsage_Mice;
rawDevices[1].dwFlags =
( mCooperation == Cooperation_Background )
? RIDEV_DEVNOTIFY | RIDEV_INPUTSINK
: RIDEV_DEVNOTIFY;
rawDevices[1].hwndTarget = mWindow;
rawDevices[1].usUsagePage = USBUsagePage_Desktop;
rawDevices[1].usUsage = USBDesktopUsage_Keyboards;
if ( !RegisterRawInputDevices( rawDevices, 2, sizeof( RAWINPUTDEVICE ) ) )
NIL_EXCEPT_WINAPI( "Couldn't register for raw input notifications" );
}
LRESULT CALLBACK EventMonitor::wndProc( HWND window, UINT message,
WPARAM wParam, LPARAM lParam )
{
PDEV_BROADCAST_DEVICEINTERFACE_W broadcast;
if ( message == WM_CREATE )
{
auto createstruct = (LPCREATESTRUCTW)lParam;
auto me = (EventMonitor*)createstruct->lpCreateParams;
SetWindowLongPtrW( window, GWLP_USERDATA, (DWORD_PTR)me );
return 1;
}
auto ptr = static_cast<DWORD_PTR>( GetWindowLongPtrW( window, GWLP_USERDATA ) );
auto me = reinterpret_cast<EventMonitor*>( ptr );
if ( !me )
return DefWindowProcW( window, message, wParam, lParam );
switch ( message )
{
case WM_DEVICECHANGE:
broadcast = (PDEV_BROADCAST_DEVICEINTERFACE_W)lParam;
if ( broadcast && broadcast->dbcc_devicetype == DBT_DEVTYP_DEVICEINTERFACE )
{
if ( wParam == DBT_DEVICEARRIVAL )
{
me->handleInterfaceArrival( broadcast->dbcc_classguid, broadcast->dbcc_name );
}
else if ( wParam == DBT_DEVICEREMOVECOMPLETE )
{
me->handleInterfaceRemoval( broadcast->dbcc_classguid, broadcast->dbcc_name );
}
}
return TRUE;
break;
case WM_INPUT_DEVICE_CHANGE:
if ( wParam == GIDC_ARRIVAL )
{
me->handleRawArrival( (HANDLE)lParam );
}
else if ( wParam == GIDC_REMOVAL )
{
me->handleRawRemoval( (HANDLE)lParam );
}
return 0;
break;
case WM_INPUT:
if ( GET_RAWINPUT_CODE_WPARAM( wParam ) == RIM_INPUT )
{
me->handleRawInput( (HRAWINPUT)lParam, false );
}
else if ( GET_RAWINPUT_CODE_WPARAM( wParam ) == RIM_INPUTSINK )
{
me->handleRawInput( (HRAWINPUT)lParam, true );
}
return DefWindowProcW( window, message, wParam, lParam );
break;
case WM_CLOSE:
return 0;
break;
case WM_DESTROY:
me->unregisterNotifications();
return 0;
break;
default:
return DefWindowProcW( window, message, wParam, lParam );
break;
}
}
void EventMonitor::unregisterNotifications()
{
RAWINPUTDEVICE rawDevices[2];
rawDevices[0].dwFlags = RIDEV_REMOVE;
rawDevices[0].hwndTarget = 0;
rawDevices[0].usUsagePage = USBUsagePage_Desktop;
rawDevices[0].usUsage = USBDesktopUsage_Mice;
rawDevices[0].dwFlags = RIDEV_REMOVE;
rawDevices[0].hwndTarget = 0;
rawDevices[0].usUsagePage = USBUsagePage_Desktop;
rawDevices[0].usUsage = USBDesktopUsage_Keyboards;
RegisterRawInputDevices( rawDevices, 2, sizeof( RAWINPUTDEVICE ) );
if ( mNotifications )
{
UnregisterDeviceNotification( mNotifications );
mNotifications = 0;
}
}
void EventMonitor::update()
{
MSG msg;
// Run all queued messages
while ( PeekMessageW( &msg, mWindow, 0, 0, PM_REMOVE ) > 0 )
{
DispatchMessageW( &msg );
}
}
EventMonitor::~EventMonitor()
{
unregisterNotifications();
free( mInputBuffer );
if ( mWindow )
DestroyWindow( mWindow );
if ( mClass )
UnregisterClassW( (LPCWSTR)mClass, mInstance ); //-V542
}
}