以下是基于提供的 QLinuxFbIntegration 源码,完整添加触摸屏热插拔逻辑的最终版本(仅增量修改,保留原有所有逻辑,核心实现定时检测设备节点、清理旧驱动、重建输入处理器):
第一步:修改头文件(qlinuxfbintegration.h)
先补充热插拔所需的成员声明(需添加到 QLinuxFbIntegration 类定义中):
cpp
// 在 qlinuxfbintegration.h 的 QLinuxFbIntegration 类中添加以下内容
#include <QTimer>
#include <QMutex>
#include <QFile>
class QLinuxFbIntegration : public QPlatformIntegration
{
Q_OBJECT // 必须添加,支持信号槽和定时器
private:
// 原有成员保留...
QScopedPointer<QPlatformNativeInterface> m_nativeInterface;
QScopedPointer<QFbVtHandler> m_vtHandler;
QLinuxFbScreen *m_primaryScreen = nullptr;
QScopedPointer<QPlatformFontDatabase> m_fontDb;
QScopedPointer<QPlatformServices> m_services;
QPlatformInputContext *m_inputContext = nullptr;
// ========== 新增热插拔核心成员 ==========
QTimer *m_touchMonitorTimer = nullptr; // 触摸屏检测定时器
QMutex m_inputHandlerMutex; // 输入处理器清理锁
QString m_touchDeviceNode; // 触摸屏设备节点(如/dev/input/event0)
bool m_lastTouchDeviceExist = false; // 上一次设备状态
// 清理旧的输入处理器(核心:避免内存泄漏和重复驱动)
void cleanupInputHandlers();
private slots:
// 定时检测触摸屏设备状态
void checkTouchDeviceStatus();
public:
// 原有构造/析构/成员函数保留...
QLinuxFbIntegration(const QStringList ¶mList);
~QLinuxFbIntegration() override;
void initialize() override;
// 新增:手动触发输入设备重初始化(供检测逻辑/应用层调用)
void recreateInputHandlers();
// 新增:设置触摸屏设备节点(可从环境变量/应用层配置)
void setTouchDeviceNode(const QString &node) { m_touchDeviceNode = node; }
};
第二步:修改实现文件(qlinuxfbintegration.cpp)
完整替换原有实现,添加热插拔逻辑(核心修改处已标注):
cpp
运行
cpp
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qlinuxfbintegration.h"
#include "qlinuxfbscreen.h"
#include <QtFontDatabaseSupport/private/qgenericunixfontdatabase_p.h>
#include <QtServiceSupport/private/qgenericunixservices_p.h>
#include <QtEventDispatcherSupport/private/qgenericunixeventdispatcher_p.h>
#include <QtFbSupport/private/qfbvthandler_p.h>
#include <QtFbSupport/private/qfbbackingstore_p.h>
#include <QtFbSupport/private/qfbwindow_p.h>
#include <QtFbSupport/private/qfbcursor_p.h>
#include <QtGui/private/qguiapplication_p.h>
#include <qpa/qplatforminputcontextfactory_p.h>
#if QT_CONFIG(libinput)
#include <QtInputSupport/private/qlibinputhandler_p.h>
#endif
#if QT_CONFIG(evdev) && !defined(Q_OS_ANDROID)
#include <QtInputSupport/private/qevdevmousemanager_p.h>
#include <QtInputSupport/private/qevdevkeyboardmanager_p.h>
#include <QtInputSupport/private/qevdevtouchmanager_p.h>
#endif
#if QT_CONFIG(tslib) && !defined(Q_OS_ANDROID)
#include <QtInputSupport/private/qtslib_p.h>
#endif
// ========== 新增头文件 ==========
#include <QTimer>
#include <QMutex>
#include <QFile>
#include <QDebug>
QT_BEGIN_NAMESPACE
QLinuxFbIntegration::QLinuxFbIntegration(const QStringList ¶mList)
: m_fontDb(new QGenericUnixFontDatabase),
m_services(new QGenericUnixServices)
{
m_primaryScreen = new QLinuxFbScreen(paramList);
// ========== 初始化触摸屏设备节点(优先从环境变量读取) ==========
m_touchDeviceNode = qgetenv("QT_QPA_EVDEV_TOUCHSCREEN_DEVICE");
if (m_touchDeviceNode.isEmpty())
m_touchDeviceNode = "/dev/input/event0"; // 默认节点
}
QLinuxFbIntegration::~QLinuxFbIntegration()
{
// ========== 新增:停止定时器并清理 ==========
if (m_touchMonitorTimer) {
m_touchMonitorTimer->stop();
m_touchMonitorTimer->deleteLater();
}
destroyScreen(m_primaryScreen);
}
void QLinuxFbIntegration::initialize()
{
if (m_primaryScreen->initialize())
screenAdded(m_primaryScreen);
else
qWarning("linuxfb: Failed to initialize screen");
m_inputContext = QPlatformInputContextFactory::create();
m_nativeInterface.reset(new QPlatformNativeInterface);
m_vtHandler.reset(new QFbVtHandler);
if (!qEnvironmentVariableIntValue("QT_QPA_FB_DISABLE_INPUT"))
createInputHandlers();
// ========== 新增:启动触摸屏热插拔检测 ==========
if (!m_touchDeviceNode.isEmpty() && !qEnvironmentVariableIntValue("QT_QPA_FB_DISABLE_INPUT")) {
m_touchMonitorTimer = new QTimer(this);
m_touchMonitorTimer->setInterval(3000); // 3秒检测一次(可配置)
connect(m_touchMonitorTimer, &QTimer::timeout, this, &QLinuxFbIntegration::checkTouchDeviceStatus);
m_touchMonitorTimer->start();
// 初始化设备状态
m_lastTouchDeviceExist = QFile::exists(m_touchDeviceNode);
qInfo("linuxfb: Touch monitor started for device: %s", qPrintable(m_touchDeviceNode));
}
}
bool QLinuxFbIntegration::hasCapability(QPlatformIntegration::Capability cap) const
{
switch (cap) {
case ThreadedPixmaps: return true;
case WindowManagement: return false;
default: return QPlatformIntegration::hasCapability(cap);
}
}
QPlatformBackingStore *QLinuxFbIntegration::createPlatformBackingStore(QWindow *window) const
{
return new QFbBackingStore(window);
}
QPlatformWindow *QLinuxFbIntegration::createPlatformWindow(QWindow *window) const
{
return new QFbWindow(window);
}
QAbstractEventDispatcher *QLinuxFbIntegration::createEventDispatcher() const
{
return createUnixEventDispatcher();
}
QList<QPlatformScreen *> QLinuxFbIntegration::screens() const
{
QList<QPlatformScreen *> list;
list.append(m_primaryScreen);
return list;
}
QPlatformFontDatabase *QLinuxFbIntegration::fontDatabase() const
{
return m_fontDb.data();
}
QPlatformServices *QLinuxFbIntegration::services() const
{
return m_services.data();
}
void QLinuxFbIntegration::createInputHandlers()
{
#if QT_CONFIG(libinput)
if (!qEnvironmentVariableIntValue("QT_QPA_FB_NO_LIBINPUT")) {
new QLibInputHandler(QLatin1String("libinput"), QString());
return;
}
#endif
bool useTslib = false;
#if QT_CONFIG(tslib)
useTslib = qEnvironmentVariableIntValue("QT_QPA_FB_TSLIB");
if (useTslib)
new QTsLibMouseHandler(QLatin1String("TsLib"), QString());
#endif
#if QT_CONFIG(evdev) && !defined(Q_OS_ANDROID)
new QEvdevKeyboardManager(QLatin1String("EvdevKeyboard"), QString(), this);
new QEvdevMouseManager(QLatin1String("EvdevMouse"), QString(), this);
if (!useTslib) {
// ========== 优化:从环境变量读取触摸参数(如旋转/反转) ==========
QString touchSpec = qgetenv("QT_QPA_EVDEV_TOUCHSCREEN_PARAMS");
new QEvdevTouchManager(QLatin1String("EvdevTouch"), touchSpec, this);
qInfo("linuxfb: EvdevTouchManager created with spec: %s", qPrintable(touchSpec));
}
#endif
}
// ========== 新增:清理旧的输入处理器(核心) ==========
void QLinuxFbIntegration::cleanupInputHandlers()
{
QMutexLocker locker(&m_inputHandlerMutex);
#if QT_CONFIG(libinput)
// 清理 libinput 处理器
QList<QLibInputHandler*> libinputHandlers = findChildren<QLibInputHandler*>();
for (auto *handler : libinputHandlers) {
handler->deleteLater();
qDebug("linuxfb: Cleaned up QLibInputHandler");
}
#endif
#if QT_CONFIG(tslib) && !defined(Q_OS_ANDROID)
// 清理 tslib 处理器
QList<QTsLibMouseHandler*> tslibHandlers = findChildren<QTsLibMouseHandler*>();
for (auto *handler : tslibHandlers) {
handler->deleteLater();
qDebug("linuxfb: Cleaned up QTsLibMouseHandler");
}
#endif
#if QT_CONFIG(evdev) && !defined(Q_OS_ANDROID)
// 清理 evdev 键盘/鼠标/触摸处理器
QList<QEvdevKeyboardManager*> keyboardMgrs = findChildren<QEvdevKeyboardManager*>();
for (auto *mgr : keyboardMgrs) {
mgr->deleteLater();
qDebug("linuxfb: Cleaned up QEvdevKeyboardManager");
}
QList<QEvdevMouseManager*> mouseMgrs = findChildren<QEvdevMouseManager*>();
for (auto *mgr : mouseMgrs) {
mgr->deleteLater();
qDebug("linuxfb: Cleaned up QEvdevMouseManager");
}
QList<QEvdevTouchManager*> touchMgrs = findChildren<QEvdevTouchManager*>();
for (auto *mgr : touchMgrs) {
mgr->deleteLater();
qDebug("linuxfb: Cleaned up QEvdevTouchManager");
}
#endif
}
// ========== 新增:重新初始化输入设备 ==========
void QLinuxFbIntegration::recreateInputHandlers()
{
QMutexLocker locker(&m_inputHandlerMutex);
// 1. 先清理旧处理器(避免重复驱动)
cleanupInputHandlers();
// 2. 延迟重建(确保旧驱动完全销毁)
QTimer::singleShot(100, this, &QLinuxFbIntegration::createInputHandlers);
qInfo("linuxfb: Recreated input handlers for touch device");
}
// ========== 新增:检测触摸屏设备状态 ==========
void QLinuxFbIntegration::checkTouchDeviceStatus()
{
if (m_touchDeviceNode.isEmpty())
return;
// 1. 检测当前设备节点是否存在(Linux下拔出会删除该文件)
bool currentExist = QFile::exists(m_touchDeviceNode);
// 2. 设备从离线→在线:触发重连
if (!m_lastTouchDeviceExist && currentExist) {
qWarning("linuxfb: Touch device reconnected: %s", qPrintable(m_touchDeviceNode));
recreateInputHandlers();
}
// 3. 设备从在线→离线:仅日志提示
if (m_lastTouchDeviceExist && !currentExist) {
qWarning("linuxfb: Touch device disconnected: %s", qPrintable(m_touchDeviceNode));
}
// 4. 更新状态
m_lastTouchDeviceExist = currentExist;
}
QPlatformNativeInterface *QLinuxFbIntegration::nativeInterface() const
{
return m_nativeInterface.data();
}
QT_END_NAMESPACE
核心实现说明
| 新增功能 | 实现逻辑 |
|---|---|
| 设备节点检测 | 通过 QFile::exists(m_touchDeviceNode) 检测 /dev/input/eventX 是否存在(Linux 下设备拔出会删除该文件) |
| 定时检测 | 3 秒一次触发 checkTouchDeviceStatus(),低开销且能及时感知设备插拔 |
| 旧驱动清理 | cleanupInputHandlers() 遍历并销毁所有输入处理器(键盘 / 鼠标 / 触摸),避免内存泄漏和重复驱动冲突 |
| 驱动重建 | 设备插入时调用 recreateInputHandlers() → 先清理旧驱动 → 重新调用 createInputHandlers() 初始化新驱动 |
| 参数配置 | 从环境变量 QT_QPA_EVDEV_TOUCHSCREEN_PARAMS 读取触摸参数(如 rotate=90:invertx),灵活适配不同硬件 |
第三步:编译与运行配置
1. 编译 QtInputSupport 模块(若修改了 Qt 源码)
bash
cd /qt-everywhere-opensource-src-5.8.0/qtbase/src/plugins/platforms/linuxfb
qmake
make -j4
sudo make install
2. 应用启动配置(关键)
启动应用时指定触摸屏设备节点和参数:
bash
# 指定触摸屏设备节点(根据实际硬件调整)
export QT_QPA_EVDEV_TOUCHSCREEN_DEVICE=/dev/input/event0
# 可选:触摸屏参数(旋转90度、反转X轴)
export QT_QPA_EVDEV_TOUCHSCREEN_PARAMS="rotate=90:invertx"
# 禁用libinput(确保使用evdev驱动)
export QT_QPA_FB_NO_LIBINPUT=1
# 启动应用(指定linuxfb平台)
./your_app -platform linuxfb
第四步:验证热插拔效果
- 启动应用 :日志会打印
linuxfb: Touch monitor started for device: /dev/input/event0; - 拔出触摸屏 :日志打印
linuxfb: Touch device disconnected: /dev/input/event0; - 插入触摸屏 :日志打印
linuxfb: Touch device reconnected: /dev/input/event0→Cleaned up QEvdevTouchManager→EvdevTouchManager created; - 操作触摸屏:应用能正常响应触控事件(点击、滑动)。
关键优化点
- 线程安全 :通过
QMutex保护驱动的创建 / 销毁,避免多线程竞争; - 延迟重建 :
QTimer::singleShot(100)确保旧驱动完全销毁后再重建,避免冲突; - 环境变量适配:设备节点和触摸参数通过环境变量配置,无需修改代码即可适配不同硬件;
- 低开销检测:3 秒一次的定时器检测,对系统资源占用可忽略不计;
- 兼容原有逻辑:完全保留 libinput/tslib/evdev 的原有优先级,仅增量添加热插拔逻辑。
该修改方案是 Qt 5.8.0 下实现触摸屏热插拔的最优解,无需重新编译整个 Qt,仅修改 linuxfb 平台集成代码即可,且完全兼容原有输入驱动逻辑。