-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically detect USB device changes
- Loading branch information
Showing
17 changed files
with
661 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2023 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "DeviceListener.h" | ||
#include <QTimer> | ||
|
||
DeviceListener::DeviceListener(QWidget* parent) | ||
: QObject(parent) | ||
, m_platformImpl(new DEVICELISTENER_IMPL(parent)) | ||
{ | ||
connect(impl(), &DEVICELISTENER_IMPL::devicePlugged, this, [&](bool state, void* ctx, void* device) { | ||
// Wait a few ms to prevent USB device access conflicts | ||
QTimer::singleShot(50, [&] { emit devicePlugged(state, ctx, device); }); | ||
}); | ||
} | ||
|
||
DeviceListener::~DeviceListener() | ||
{ | ||
} | ||
|
||
DEVICELISTENER_IMPL* DeviceListener::impl() | ||
{ | ||
return qobject_cast<DEVICELISTENER_IMPL*>(m_platformImpl.data()); | ||
} | ||
|
||
void DeviceListener::registerHotplugCallback(bool arrived, | ||
bool left, | ||
int vendorId, | ||
int productId, | ||
const QUuid* deviceClass) | ||
{ | ||
impl()->registerHotplugCallback(arrived, left, vendorId, productId, deviceClass); | ||
} | ||
|
||
void DeviceListener::deregisterHotplugCallback() | ||
{ | ||
impl()->deregisterHotplugCallback(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2023 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef DEVICELISTENER_H | ||
#define DEVICELISTENER_H | ||
|
||
#include <QScopedPointer> | ||
#include <QWidget> | ||
|
||
#if defined(Q_OS_WIN) | ||
#include "winutils/DeviceListenerWin.h" | ||
#elif defined(Q_OS_MACOS) | ||
#include "macutils/DeviceListenerMac.h" | ||
#elif defined(Q_OS_UNIX) | ||
#include "nixutils/DeviceListenerLibUsb.h" | ||
#endif | ||
|
||
class QUuid; | ||
|
||
class DeviceListener : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
static constexpr int MATCH_ANY = -1; | ||
|
||
explicit DeviceListener(QWidget* parent); | ||
DeviceListener(const DeviceListener&) = delete; | ||
~DeviceListener() override; | ||
|
||
/** | ||
* Register a hotplug notification callback. | ||
* | ||
* Fires devicePlugged() or deviceUnplugged() when the state of a matching device changes. | ||
* The signals are supplied with the platform-specific context and ID of the firing device. | ||
* Registering a new callback with the same DeviceListener will unregister any previous callbacks. | ||
* | ||
* @param arrived listen for new devices | ||
* @param left listen for device unplug | ||
* @param vendorId vendor ID to listen for or DeviceListener::MATCH_ANY | ||
* @param productId product ID to listen for or DeviceListener::MATCH_ANY | ||
* @param deviceClass device class GUID (Windows only) | ||
* @return callback handle | ||
*/ | ||
void registerHotplugCallback(bool arrived, | ||
bool left, | ||
int vendorId = MATCH_ANY, | ||
int productId = MATCH_ANY, | ||
const QUuid* deviceClass = nullptr); | ||
void deregisterHotplugCallback(); | ||
|
||
signals: | ||
void devicePlugged(bool state, void* ctx, void* device); | ||
|
||
private: | ||
DEVICELISTENER_IMPL* impl(); | ||
QScopedPointer<QObject> m_platformImpl; | ||
}; | ||
|
||
#endif // DEVICELISTENER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (C) 2023 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "DeviceListenerMac.h" | ||
|
||
#include <QPointer> | ||
#include <IOKit/IOKitLib.h> | ||
|
||
DeviceListenerMac::DeviceListenerMac(QObject* parent) | ||
: QObject(parent) | ||
, m_mgr(nullptr) | ||
{ | ||
} | ||
|
||
DeviceListenerMac::~DeviceListenerMac() | ||
{ | ||
if (m_mgr) { | ||
IOHIDManagerUnscheduleFromRunLoop(m_mgr, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | ||
IOHIDManagerClose(m_mgr, kIOHIDOptionsTypeNone); | ||
CFRelease(m_mgr); | ||
} | ||
} | ||
|
||
void DeviceListenerMac::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId, const QUuid*) | ||
{ | ||
if (!m_mgr) { | ||
m_mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone); | ||
if (!m_mgr) { | ||
qWarning("Failed to create IOHIDManager."); | ||
return; | ||
} | ||
IOHIDManagerScheduleWithRunLoop(m_mgr, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | ||
} | ||
|
||
if (vendorId > 0 || productId > 0) { | ||
CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOHIDDeviceKey); | ||
if (vendorId > 0) { | ||
auto vid = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId); | ||
CFDictionaryAddValue(matchingDict, CFSTR(kIOHIDVendorIDKey), vid); | ||
CFRelease(vid); | ||
} | ||
if (productId > 0) { | ||
auto pid = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId); | ||
CFDictionaryAddValue(matchingDict, CFSTR(kIOHIDProductIDKey), pid); | ||
CFRelease(pid); | ||
} | ||
IOHIDManagerSetDeviceMatching(m_mgr, matchingDict); | ||
CFRelease(matchingDict); | ||
} else { | ||
IOHIDManagerSetDeviceMatching(m_mgr, nullptr); | ||
} | ||
|
||
QPointer that = this; | ||
if (arrived) { | ||
IOHIDManagerRegisterDeviceMatchingCallback(m_mgr, [](void* ctx, IOReturn, void*, IOHIDDeviceRef device) { | ||
static_cast<DeviceListenerMac*>(ctx)->onDeviceStateChanged(true, device); | ||
}, that); | ||
} | ||
if (left) { | ||
IOHIDManagerRegisterDeviceRemovalCallback(m_mgr, [](void* ctx, IOReturn, void*, IOHIDDeviceRef device) { | ||
static_cast<DeviceListenerMac*>(ctx)->onDeviceStateChanged(true, device); | ||
}, that); | ||
} | ||
|
||
if (IOHIDManagerOpen(m_mgr, kIOHIDOptionsTypeNone) != kIOReturnSuccess) { | ||
qWarning("Could not open enumerated devices."); | ||
} | ||
} | ||
|
||
void DeviceListenerMac::deregisterHotplugCallback() | ||
{ | ||
if (m_mgr) { | ||
IOHIDManagerRegisterDeviceMatchingCallback(m_mgr, nullptr, this); | ||
IOHIDManagerRegisterDeviceRemovalCallback(m_mgr, nullptr, this); | ||
} | ||
} | ||
|
||
void DeviceListenerMac::onDeviceStateChanged(bool state, void* device) | ||
{ | ||
emit devicePlugged(state, m_mgr, device); | ||
} |
Oops, something went wrong.