From 9c0803098cc2b8aee555ce8389f7793e7374e3c6 Mon Sep 17 00:00:00 2001
From: jonlee <jonleea@163.com>
Date: Tue, 28 Dec 2021 16:48:14 +0000
Subject:[PATCH 1/2] QT supports input devices hot plugin
---
.../devicediscovery/qdevicediscovery_static.cpp | 50 ++++++++++++++++------
.../devicediscovery/qdevicediscovery_static_p.h | 9 ++++
2 files changed, 45 insertions(+), 14 deletions(-)
diff --git a/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp b/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
index a157567..0beeafb 100644
--- a/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
+++ b/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static.cpp
@@ -47,11 +47,7 @@
#include <QLoggingCategory>
#include <QtCore/private/qcore_unix_p.h>
-#ifdef Q_OS_FREEBSD
-#include <dev/evdev/input.h>
-#else
#include <linux/input.h>
-#endif
#include <fcntl.h>
/* android (and perhaps some other linux-derived stuff) don't define everything
@@ -93,40 +89,42 @@ QDeviceDiscovery *QDeviceDiscovery::create(QDeviceTypes types, QObject *parent)
QDeviceDiscoveryStatic::QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent)
: QDeviceDiscovery(types, parent)
{
+ // 初始化文件监听器
+ m_fileWatcher = new QFileSystemWatcher(this);
+ m_fileWatcher->addPath(QString::fromLatin1(QT_EVDEV_DEVICE_PATH));// "dev/input/"
+ connect(m_fileWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(handleHotPlugWatch(QString)));
qCDebug(lcDD) << "static device discovery for type" << types;
}
QStringList QDeviceDiscoveryStatic::scanConnectedDevices()
{
- QStringList devices;
+ m_devices.clear();
QDir dir;
dir.setFilter(QDir::System);
// check for input devices
if (m_types & Device_InputMask) {
dir.setPath(QString::fromLatin1(QT_EVDEV_DEVICE_PATH));
- const auto deviceFiles = dir.entryList();
- for (const QString &deviceFile : deviceFiles) {
+ foreach (const QString &deviceFile, dir.entryList()) {
QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
if (checkDeviceType(absoluteFilePath))
- devices << absoluteFilePath;
+ m_devices << absoluteFilePath;
}
}
// check for drm devices
if (m_types & Device_VideoMask) {
dir.setPath(QString::fromLatin1(QT_DRM_DEVICE_PATH));
- const auto deviceFiles = dir.entryList();
- for (const QString &deviceFile : deviceFiles) {
+ foreach (const QString &deviceFile, dir.entryList()) {
QString absoluteFilePath = dir.absolutePath() + QLatin1Char('/') + deviceFile;
if (checkDeviceType(absoluteFilePath))
- devices << absoluteFilePath;
+ m_devices << absoluteFilePath;
}
}
- qCDebug(lcDD) << "Found matching devices" << devices;
+ qCDebug(lcDD) << "Found matching devices" << m_devices;
- return devices;
+ return m_devices;
}
bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
@@ -139,7 +137,7 @@ bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
qCDebug(lcDD) << "doing static device discovery for " << device;
- if ((m_types & Device_DRM) && device.contains(QLatin1String(QT_DRM_DEVICE_PREFIX))) {
+ if ((m_types & Device_DRM) && device.contains(QString::fromLatin1(QT_DRM_DEVICE_PREFIX))) {
QT_CLOSE(fd);
return true;
}
@@ -200,4 +198,28 @@ bool QDeviceDiscoveryStatic::checkDeviceType(const QString &device)
return false;
}
+
+void QDeviceDiscoveryStatic::handleHotPlugWatch(const QString &path)
+{
+ if(path.compare(QString::fromLatin1(QT_EVDEV_DEVICE_PATH)))
+ {
+ return;
+ }
+
+ QStringList devices;
+
+ // 先移除原来的设备
+ foreach (const QString &device, m_devices)
+ emit deviceRemoved(device);
+
+ // 获取现在的设备
+ // 注,这里获取的设备已经经过过滤,原因是在对该类进行实例化的时候
+ // 已经传进了筛选参数,如:QDeviceDiscovery::Device_Keyboard
+ devices = this->scanConnectedDevices();
+
+ // 重新添加设备
+ foreach (const QString &device, devices)
+ emit deviceDetected(device);
+}
+
QT_END_NAMESPACE
diff --git a/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static_p.h b/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static_p.h
index ab5a9c6..70600de 100644
--- a/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static_p.h
+++ b/qtbase/src/platformsupport/devicediscovery/qdevicediscovery_static_p.h
@@ -52,6 +52,8 @@
//
#include "qdevicediscovery_p.h"
+#include <QFileSystemWatcher>
+#include <QStringList>
QT_BEGIN_NAMESPACE
@@ -63,8 +65,15 @@ public:
QDeviceDiscoveryStatic(QDeviceTypes types, QObject *parent = 0);
QStringList scanConnectedDevices() Q_DECL_OVERRIDE;
+private slots:
+ void handleHotPlugWatch(const QString &path);
private:
bool checkDeviceType(const QString &device);
+ // 用于检测鼠标键盘热插拔
+ QFileSystemWatcher *m_fileWatcher;
+
+ // 原有的设备列表
+ QStringList m_devices;
};
QT_END_NAMESPACE
--
2.7.4