File indexing completed on 2022-09-27 16:29:37

0001 /*
0002     SPDX-FileCopyrightText: 2016 ROSA
0003     SPDX-License-Identifier: GPL-3.0-or-later
0004 */
0005 
0006 ////////////////////////////////////////////////////////////////////////////////
0007 // Windows implementation of UsbDeviceMonitor
0008 
0009 #include "usbdevicemonitor_win_p.h"
0010 #include "usbdevicemonitor.h"
0011 
0012 #include <QApplication>
0013 
0014 
0015 // Private class implementation
0016 
0017 UsbDeviceMonitorPrivate::UsbDeviceMonitorPrivate()
0018 {
0019 }
0020 
0021 UsbDeviceMonitorPrivate::~UsbDeviceMonitorPrivate()
0022 {
0023 }
0024 
0025 
0026 // Main class implementation
0027 
0028 UsbDeviceMonitor::UsbDeviceMonitor(QObject *parent) :
0029     QObject(parent),
0030     d_ptr(new UsbDeviceMonitorPrivate())
0031 {
0032 }
0033 
0034 UsbDeviceMonitor::~UsbDeviceMonitor()
0035 {
0036     cleanup();
0037     delete d_ptr;
0038 }
0039 
0040 // Closes handles and frees resources
0041 void UsbDeviceMonitor::cleanup()
0042 {
0043 }
0044 
0045 // Implements QAbstractNativeEventFilter interface for processing WM_DEVICECHANGE messages (Windows)
0046 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0047 bool UsbDeviceMonitor::nativeEventFilter(const QByteArray& eventType, void* message, long* result)
0048 #else
0049 bool UsbDeviceMonitor::nativeEventFilter(const QByteArray& eventType, void* message, qintptr* result)
0050 #endif
0051 {
0052     Q_UNUSED(eventType);
0053 
0054     MSG* msg = static_cast<MSG*>(message);
0055     if ((msg->message == WM_DEVICECHANGE) &&
0056         ((msg->wParam == DBT_DEVICEARRIVAL) || (msg->wParam == DBT_DEVICEREMOVECOMPLETE)))
0057     {
0058         // If the event was caused by adding or removing a device, mark the WinAPI message as processed
0059         // and emit the notification signal
0060         *result = TRUE;
0061         emit deviceChanged();
0062         return true;
0063     }
0064     return false;
0065 }
0066 
0067 bool UsbDeviceMonitor::startMonitoring()
0068 {
0069     // In Windows we use QAbstractNativeEventFilter interface implementation and process native Windows messages
0070     qApp->installNativeEventFilter(this);
0071     return true;
0072 }