Skip to content

Commit

Permalink
Add Windows Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious committed Dec 12, 2023
1 parent 6e76467 commit 37d333d
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ if(WITH_XC_YUBIKEY)
elseif(UNIX)
set(keepassx_SOURCES ${keepassx_SOURCES} gui/osutils/nixutils/DeviceListenerLibUsb.cpp)
elseif(WIN32)
# set(keepassx_SOURCES ${keepassx_SOURCES} gui/osutils/nixutils/DeviceListenerWin.cpp.cpp)
set(keepassx_SOURCES ${keepassx_SOURCES} gui/osutils/winutils/DeviceListenerWin.cpp)
endif()
endif()

Expand Down
11 changes: 8 additions & 3 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
: DialogyWidget(parent)
, m_ui(new Ui::DatabaseOpenWidget())
, m_db(nullptr)
, m_deviceListener(new DeviceListener(this))
{
m_ui->setupUi(this);

Expand Down Expand Up @@ -90,7 +91,7 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));

#ifdef WITH_XC_YUBIKEY
connect(&m_deviceListener, SIGNAL(devicePlugged(bool, void*, void*)), this, SLOT(pollHardwareKey()));
connect(m_deviceListener, SIGNAL(devicePlugged(bool, void*, void*)), this, SLOT(pollHardwareKey()));
#endif

m_ui->hardwareKeyLabelHelp->setIcon(icons()->icon("system-help").pixmap(QSize(12, 12)));
Expand Down Expand Up @@ -147,7 +148,11 @@ void DatabaseOpenWidget::showEvent(QShowEvent* event)

#ifdef WITH_XC_YUBIKEY
constexpr int vid = 0x1050; // Yubico vendor ID
m_deviceListener.registerHotplugCallback(true, true, vid);
#ifdef Q_OS_WIN
m_deviceListener->registerHotplugCallback(true, true, vid, DeviceListener::MATCH_ANY, &DeviceListenerWin::DEV_CLS_CCID);
#else
m_deviceListener->registerHotplugCallback(true, true, vid);
#endif
#endif
}

Expand All @@ -161,7 +166,7 @@ void DatabaseOpenWidget::hideEvent(QHideEvent* event)
}

#ifdef WITH_XC_YUBIKEY
m_deviceListener.deregisterHotplugCallback();
m_deviceListener->deregisterHotplugCallback();
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/DatabaseOpenWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef KEEPASSX_DATABASEOPENWIDGET_H
#define KEEPASSX_DATABASEOPENWIDGET_H

#include <QPointer>
#include <QScopedPointer>
#include <QTimer>

Expand Down Expand Up @@ -83,7 +84,7 @@ private slots:

private:
#ifdef WITH_XC_YUBIKEY
DeviceListener m_deviceListener;
QPointer<DeviceListener> m_deviceListener;
#endif
bool m_pollingHardwareKey = false;
bool m_blockQuickUnlock = false;
Expand Down
9 changes: 4 additions & 5 deletions src/gui/osutils/DeviceListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
*/

#include "DeviceListener.h"

#include <QTimer>

DeviceListener::DeviceListener(QObject* parent)
DeviceListener::DeviceListener(QWidget* parent)
: QObject(parent)
, m_platformImpl(new DEVICELISTENER_IMPL())
, 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
Expand All @@ -38,9 +37,9 @@ DEVICELISTENER_IMPL* DeviceListener::impl()
return qobject_cast<DEVICELISTENER_IMPL*>(m_platformImpl.data());
}

void DeviceListener::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId)
void DeviceListener::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId, const QUuid* deviceClass)
{
impl()->registerHotplugCallback(arrived, left, vendorId, productId);
impl()->registerHotplugCallback(arrived, left, vendorId, productId, deviceClass);
}

void DeviceListener::deregisterHotplugCallback()
Expand Down
11 changes: 7 additions & 4 deletions src/gui/osutils/DeviceListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@
#ifndef DEVICELISTENER_H
#define DEVICELISTENER_H

#include <QObject>
#include <QScopedPointer>
#include <QWidget>

#if defined(Q_OS_WIN)
// TODO
#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(QObject* parent = nullptr);
explicit DeviceListener(QWidget* parent);
DeviceListener(const DeviceListener&) = delete;
~DeviceListener() override;

Expand All @@ -51,9 +53,10 @@ class DeviceListener : public QObject
* @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);
void registerHotplugCallback(bool arrived, bool left, int vendorId = MATCH_ANY, int productId = MATCH_ANY, const QUuid* deviceClass = nullptr);
void deregisterHotplugCallback();

signals:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/osutils/ScreenLockListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "ScreenLockListener.h"
#include "ScreenLockListenerPrivate.h"

ScreenLockListener::ScreenLockListener(QObject* parent)
ScreenLockListener::ScreenLockListener(QWidget* parent)
: QObject(parent)
{
m_listener = ScreenLockListenerPrivate::instance(parent);
Expand Down
4 changes: 2 additions & 2 deletions src/gui/osutils/ScreenLockListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#ifndef SCREENLOCKLISTENER_H
#define SCREENLOCKLISTENER_H
#include <QObject>
#include <QWidget>

class ScreenLockListenerPrivate;

Expand All @@ -26,7 +26,7 @@ class ScreenLockListener : public QObject
Q_OBJECT

public:
explicit ScreenLockListener(QObject* parent = nullptr);
explicit ScreenLockListener(QWidget* parent);
~ScreenLockListener() override;

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/gui/osutils/ScreenLockListenerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
#include "winutils/ScreenLockListenerWin.h"
#endif

ScreenLockListenerPrivate::ScreenLockListenerPrivate(QObject* parent)
ScreenLockListenerPrivate::ScreenLockListenerPrivate(QWidget* parent)
: QObject(parent)
{
}

ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QObject* parent)
ScreenLockListenerPrivate* ScreenLockListenerPrivate::instance(QWidget* parent)
{
#if defined(Q_OS_MACOS)
Q_UNUSED(parent);
Expand Down
6 changes: 3 additions & 3 deletions src/gui/osutils/ScreenLockListenerPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

#ifndef SCREENLOCKLISTENERPRIVATE_H
#define SCREENLOCKLISTENERPRIVATE_H
#include <QObject>
#include <QWidget>

class ScreenLockListenerPrivate : public QObject
{
Q_OBJECT
public:
static ScreenLockListenerPrivate* instance(QObject* parent = nullptr);
static ScreenLockListenerPrivate* instance(QWidget* parent = nullptr);

protected:
explicit ScreenLockListenerPrivate(QObject* parent = nullptr);
explicit ScreenLockListenerPrivate(QWidget* parent = nullptr);

signals:
void screenLocked();
Expand Down
3 changes: 2 additions & 1 deletion src/gui/osutils/macutils/DeviceListenerMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <QPointer>
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDManager.h>

DeviceListenerMac::DeviceListenerMac(QObject* parent)
: QObject(parent)
Expand All @@ -35,7 +36,7 @@ DeviceListenerMac::~DeviceListenerMac()
}
}

void DeviceListenerMac::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId)
void DeviceListenerMac::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId, const QUuid*)
{
if (!m_mgr) {
m_mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone);
Expand Down
8 changes: 4 additions & 4 deletions src/gui/osutils/macutils/DeviceListenerMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#define DEVICELISTENER_MAC_H

#include <QObject>
#include <QPointer>

#include <IOKit/hid/IOHIDManager.h>
class IOHIDManagerRef;
class QUuid;

#define DEVICELISTENER_IMPL DeviceListenerMac

Expand All @@ -30,14 +30,14 @@ class DeviceListenerMac : public QObject
Q_OBJECT

public:
explicit DeviceListenerMac(QObject* parent = nullptr);
explicit DeviceListenerMac(QObject* parent);
DeviceListenerMac(const DeviceListenerMac&) = delete;
~DeviceListenerMac() override;

void registerHotplugCallback(bool arrived,
bool left,
int vendorId = -1,
int productId = -1);
int productId = -1, const QUuid* = nullptr);
void deregisterHotplugCallback();

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/gui/osutils/nixutils/DeviceListenerLibUsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <QtConcurrent>
#include <libusb.h>

DeviceListenerLibUsb::DeviceListenerLibUsb(QObject* parent)
DeviceListenerLibUsb::DeviceListenerLibUsb(QWidget* parent)
: QObject(parent)
, m_ctx(nullptr)
, m_callbackRef(0)
Expand Down Expand Up @@ -50,7 +50,7 @@ namespace
}
} // namespace

void DeviceListenerLibUsb::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId)
void DeviceListenerLibUsb::registerHotplugCallback(bool arrived, bool left, int vendorId, int productId, const QUuid*)
{
if (!m_ctx) {
if (libusb_init(reinterpret_cast<libusb_context**>(&m_ctx)) != LIBUSB_SUCCESS) {
Expand Down
6 changes: 4 additions & 2 deletions src/gui/osutils/nixutils/DeviceListenerLibUsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
#include <QFuture>
#include <QObject>

class QUuid;

#define DEVICELISTENER_IMPL DeviceListenerLibUsb

class DeviceListenerLibUsb : public QObject
{
Q_OBJECT

public:
explicit DeviceListenerLibUsb(QObject* parent = nullptr);
explicit DeviceListenerLibUsb(QWidget* parent);
DeviceListenerLibUsb(const DeviceListenerLibUsb&) = delete;
~DeviceListenerLibUsb() override;

void registerHotplugCallback(bool arrived, bool left, int vendorId = -1, int productId = -1);
void registerHotplugCallback(bool arrived, bool left, int vendorId = -1, int productId = -1, const QUuid* = nullptr);
void deregisterHotplugCallback();

signals:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/osutils/nixutils/ScreenLockListenerDBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <QDebug>
#include <QProcessEnvironment>

ScreenLockListenerDBus::ScreenLockListenerDBus(QObject* parent)
ScreenLockListenerDBus::ScreenLockListenerDBus(QWidget* parent)
: ScreenLockListenerPrivate(parent)
{
QDBusConnection sessionBus = QDBusConnection::sessionBus();
Expand Down
3 changes: 2 additions & 1 deletion src/gui/osutils/nixutils/ScreenLockListenerDBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

#ifndef SCREENLOCKLISTENERDBUS_H
#define SCREENLOCKLISTENERDBUS_H

#include "gui/osutils/ScreenLockListenerPrivate.h"
#include <QDBusMessage>

class ScreenLockListenerDBus : public ScreenLockListenerPrivate
{
Q_OBJECT
public:
explicit ScreenLockListenerDBus(QObject* parent = nullptr);
explicit ScreenLockListenerDBus(QWidget* parent);

private slots:
void gnomeSessionStatusChanged(uint status);
Expand Down
Loading

0 comments on commit 37d333d

Please sign in to comment.