Qt5触摸屏支持热插拔

2 篇文章 0 订阅
测试的Qt5版本:5.12.8 ,5.9.1
基于Linuxfb和Tslib的触摸交互,当触摸屏热插拔后触摸无法恢复,需要修改Qt的tslib插件库源码。


QT支持热插拔的前提是Linux内核需要支持热插拔,event设备可以即插即用

源码路径:

qtbase/src/platformsupport/input/tslib/qtslib.cpp

static QString mKey;
static QString mSpecification;
static QByteArray mDevice;

QTsLibMouseHandler::QTsLibMouseHandler(const QString &key,
                                       const QString &specification,
                                       QObject *parent)
: QObject(parent),
m_notify(0), m_x(0), m_y(0), m_pressed(0), m_rawMode(false)
{
    qCDebug(qLcTsLib) << "Initializing tslib plugin" << key << specification;
    setObjectName(QLatin1String("TSLib Mouse Handler"));

    mKey=key;
    mSpecification=specification;

    QByteArray device = qgetenv("TSLIB_TSDEVICE");

    if (specification.startsWith(QLatin1String("/dev/")))
    device = specification.toLocal8Bit();

    if (device.isEmpty())
    device = QByteArrayLiteral("/dev/input/event1");

    m_dev = ts_open(device.constData(), 1);
    if (!m_dev) {
        qErrnoWarning(errno, "ts_open() failed");
        return;
    }

    if (ts_config(m_dev))
    qErrnoWarning(errno, "ts_config() failed");

    m_rawMode = !key.compare(QLatin1String("TslibRaw"), Qt::CaseInsensitive);

    int fd = ts_fd(m_dev);
    if (fd >= 0) {
        qCDebug(qLcTsLib) << "tslib device is" << device;
        m_notify = new QSocketNotifier(fd, QSocketNotifier::Read, this);
        connect(m_notify, &QSocketNotifier::activated, this, &QTsLibMouseHandler::readMouseData);
    } else {
        qErrnoWarning(errno, "tslib: Cannot open input device %s", device.constData());
    }
    
    mDevice=device;
    printf("mKey=%s  mSpecification=%s m_rawMode=%d mDevice=%s\n",mKey.toLatin1().constData(), mSpecification.toLatin1().constData(),m_rawMode,mDevice.constData());
}

void QTsLibMouseHandler::readMouseData()
{   
    ts_sample sample;
    bool ret;

      //ret = get_sample(m_dev,&sample,m_rawMode);
      //printf("get_sample_ret=%d\n",ret);
    while(get_sample(m_dev, &sample, m_rawMode)) {
//    if(ret {
        bool pressed = sample.pressure;
        int x = sample.x;
        int y = sample.y;

        // coordinates on release events can contain arbitrary values, just ignore them
        if (sample.pressure == 0) {
            x = m_x;
            y = m_y;
        }

        if (!m_rawMode) {
            //filtering: ignore movements of 2 pixels or less
            int dx = x - m_x;                
            int dy = y - m_y;
            if (dx*dx <= 4 && dy*dy <= 4 && pressed == m_pressed)
            continue;
        }
        QPoint pos(x, y);

        QWindowSystemInterface::handleMouseEvent(nullptr, pos, pos,
                                                 pressed ? Qt::LeftButton : Qt::NoButton,
                                                 Qt::NoButton, QEvent::None);

        m_x = x;
        m_y = y;
        m_pressed = pressed;
    }

//    printf("QtTslib readMouseData End\n");
#if 1
    if(access(mDevice.constData(),F_OK))
    {
        ts_close(m_dev);
        disconnect(m_notify);
        delete m_notify;

        while(1){
            printf("Please Input TouchScreen Device...\n");
            sleep(1);

            m_dev = ts_open(mDevice.constData(), 1);
            if (!m_dev) {
                continue;
            }

            if (ts_config(m_dev)){
                printf("QtLib: ts_config failed!\n");
            }

            m_rawMode = !mKey.compare(QLatin1String("TslibRaw"), Qt::CaseInsensitive);

            int fd = ts_fd(m_dev);
            if (fd >= 0) {
                qCDebug(qLcTsLib) << "tslib device is" <<mDevice;
                m_notify = new QSocketNotifier(fd, QSocketNotifier::Read, this);
                connect(m_notify, &QSocketNotifier::activated, this, &QTsLibMouseHandler::readMouseData);
                break;
            } else {
                qErrnoWarning(errno, "tslib: Cannot open input device %s", mDevice.constData());
            }

        }
    }
#endif 
}

将编译好的qtbase/plugins/platforms/libqlinuxfb.so安装到目标系统里

QT5.12.8环境变量

export QT_ROOT=/usr/lib
export QT_QPA_PLATFORM_PLUGIN_PATH=$QT_ROOT/plugins 
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0
export QT_QPA_FONTDIR=$QT_ROOT/fonts 
export QT_QPA_GENERIC_PLUGINS=Tslib:/dev/input/touchscreen0 
export QT_QPA_EVDEV_KEYBOARD_PARAMETERS=/dev/input/event0
export QML_IMPORT_PATH=$QT_ROOT/qml
export QML2_IMPORT_PATH=$QT_ROOT/qml
export QT_QPA_FB_TSLIB=1


export TSLIB_TSDEVICE=/dev/input/touchscreen0
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_PLUGINDIR=/usr/lib/ts
export TSLIB_FBDEVICE=/dev/fb0
# export TSLIB_CONSOLEDEVICE=none #视情况而定
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值