总体逻辑,修改发现设备和添加设备;
1、发现设备(qt-everywhere-src-5.15.6\qtbase\src\platformsupport\devicediscovery/qdevicediscovery_static.cpp)
需要修改的逻辑是,当/dev/input/路径下发生了文件的改动,需要重新加载设备列表,然后将设备重新添加使用;
1.1、头文件中(qdevicediscovery_static_p.h)
修改内容为:
- 添加设备名称列表(m_devices);
- 检测热插拔类(m_fileWatcher);
- 检测到文件变动后的槽函数(handleHotPlugWatch);
1.2、修改源文件(qdevicediscovery_static.cpp)
修改内容:
- 构造函数中初始化热插拔监听,关联信号和槽;
- 在浏览设备文件函数中(scanConnectedDevices),将可连接设备添加到设备列表中;
- 槽函数中,判断文件路径是否在/devinput/下,然后再发送信号(deviceRemoved)断开设备,其次是调用浏览设备文件函数(scanConnectedDevices),获取可连接设备列表,最后是将设备文件名称用信号(deviceDetected)发送过去,用于连接;
2、添加设备(qt-everywhere-src-5.15.6\qtbase\src\platformsupport\input\evdevmouse)
该目录下主要是鼠标设备管理(qevdevmousemanager)和鼠标事件处理(qevdevmousehandler)
在设备管理文件中,可以看到调用发现设备类(qdevicediscovery),关联删除设备和添加设备;

在鼠标事件处理文件(qevdevmousehandler)中,主要是在函数(readMouseData),当连接失败后,需要做重连操作;

注:触摸屏可以做相同修改,但需要保存设备文件名称,以便再次打开;
3、源码附录
3.1、qdevicediscovery_static_p.h源码:
#ifndef QDEVICEDISCOVERY_STATIC_H
#define QDEVICEDISCOVERY_STATIC_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "qdevicediscovery_p.h"
#include <QFileSystemWatcher>
#include <QStringList>
QT_BEGIN_NAMESPACE
class QDeviceDiscoveryStatic : public QDeviceDiscovery
{
Q_OBJECT
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
#endif // QDEVICEDISCOVERY_STATIC_H
3.2、qdevicediscovery_static.cpp源码:
#include "qdevicediscovery_static_p.h"
#include <QStringList>
#include <QCoreApplication>
#include <QObject>
#include <QHash>
#include <QDir>
#include <QLoggingCategory>
#include <QtCore/private/qcore_unix_p.h>
#include <linux/input.h>
#include <fcntl.h>
/* android (and perhaps some other linux-derived stuff) don't define everything
* in linux/input.h, so we'll need to do that ourselves.
*/
#ifndef KEY_CNT
#define KEY_CNT (KEY_MAX+1)
#endif
#ifndef REL_CNT
#define REL_CNT (REL_MAX+1)
#endif
#ifndef ABS_CNT
#define ABS_CNT (ABS_MAX+1)
#endif
#ifndef ABS_MT_POSITION_X
#define ABS_MT_POSITION_X 0x35
#endif
#ifndef ABS_MT_POSITION_Y
#define ABS_MT_POSITION_Y 0x36
#endif
#define LONG_BITS (sizeof(long) * 8 )
#define LONG_FIELD_SIZE(bits) ((bits / LONG_BITS) + 1)
static bool testBit(long bit, const long *field)
{
return (field[bit / LONG_BITS] >> bit % LONG_BITS) & 1;
}
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcDD, "qt.qpa.input")
QDeviceDiscovery *QDeviceDiscovery::create(QDeviceTypes types, QObject *parent)
{
return new QDeviceDiscoveryStatic(types, 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, &QFileSystemWatcher::directoryChanged, this, &QDeviceDiscoveryStatic::handleHotPlugWatch);
qCDebug(lcDD) << "static device discovery for type" << types;
}

最低0.47元/天 解锁文章
655

被折叠的 条评论
为什么被折叠?



