-
Notifications
You must be signed in to change notification settings - Fork 1
/
usbdevicemonitor_win.cpp
63 lines (49 loc) · 1.52 KB
/
usbdevicemonitor_win.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
////////////////////////////////////////////////////////////////////////////////
// Windows implementation of UsbDeviceMonitor
#include <QApplication>
#include "usbdevicemonitor.h"
#include "usbdevicemonitor_win_p.h"
// Private class implementation
UsbDeviceMonitorPrivate::UsbDeviceMonitorPrivate()
{
}
UsbDeviceMonitorPrivate::~UsbDeviceMonitorPrivate()
{
}
// Main class implementation
UsbDeviceMonitor::UsbDeviceMonitor(QObject *parent) :
QObject(parent),
d_ptr(new UsbDeviceMonitorPrivate())
{
}
UsbDeviceMonitor::~UsbDeviceMonitor()
{
cleanup();
delete d_ptr;
}
// Closes handles and frees resources
void UsbDeviceMonitor::cleanup()
{
}
// Implements QAbstractNativeEventFilter interface for processing WM_DEVICECHANGE messages (Windows)
bool UsbDeviceMonitor::nativeEventFilter(const QByteArray& eventType, void* message, qintptr* result)
{
Q_UNUSED(eventType);
MSG* msg = static_cast<MSG*>(message);
if ((msg->message == WM_DEVICECHANGE) &&
((msg->wParam == DBT_DEVICEARRIVAL) || (msg->wParam == DBT_DEVICEREMOVECOMPLETE)))
{
// If the event was caused by adding or remiving a device, mark the WinAPI message as processed
// and emit the notification signal
*result = TRUE;
emit deviceChanged();
return true;
}
return false;
}
bool UsbDeviceMonitor::startMonitoring()
{
// In Windows we use QAbstractNativeEventFilter interface implementation and process native Windows messages
qApp->installNativeEventFilter(this);
return true;
}