Qt 5.4 Linux Raspberry Pi 上使用 Tslib 的触摸屏输入因 LinuxFB QPA 平台插件而失败

Qt 5.4 Linux Touchscreen Input with Tslib on Raspberry Pi failing with LinuxFB QPA Platform Plugin

我买了 Tontec 2.4 英寸触摸屏 ( http://elinux.org/MZTX-PI-EXT ) for my Raspberry Pi. The touchscreen controller requires the "tsc2007.ko" and "tsp_raspi.ko" kernel modules as described in the elinux post. The tsc2007.ko module is in the Raspbian Kernel tree but the tsp_raspi.ko can be found here: https://github.com/osandov/raspi/tree/master/tsc2007.

我已经使用这些模块为 Pi 交叉编译了一个新内核,它们加载正常并在 Raspbian 中创建了一个 /dev/input/event0 设备。如果我 'evtest' 该设备并触摸屏幕,我会得到输出,所以我知道事件正在 Linux:

中传递
pi@raspberry /dev/input $ evtest
Available devices:
/dev/input/event0:  TSC2007 Touchscreen
Select the device event number [0-0]: 0
Input driver version is 1.0.1
Input device ID: bus 0x18 vendor 0x0 product 0x0 version 0x0
Input device name: "TSC2007 Touchscreen"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 330 (BTN_TOUCH)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value   1922
      Min        0
      Max     4095
      Fuzz      64
    Event code 1 (ABS_Y)
      Value   2221
      Min        0
      Max     4095
      Fuzz      64
    Event code 24 (ABS_PRESSURE)
      Value      0
      Min        0
      Max     4095
      Fuzz      64
Properties:
Testing ... (interrupt to exit)
Event: time 1425521704.199489, type 1 (EV_KEY), code 330 (BTN_TOUCH), value 1
Event: time 1425521704.199489, type 3 (EV_ABS), code 1 (ABS_Y), value 2085
Event: time 1425521704.199489, type 3 (EV_ABS), code 24 (ABS_PRESSURE), value 538
Event: time 1425521704.199489, -------------- SYN_REPORT ------------
Event: time 1425521704.209174, type 3 (EV_ABS), code 0 (ABS_X), value 1455
...

我安装了 tslib 和 运行 快速 ts_calibrate。我还确保 ts_test 在触摸屏幕时吐出数据。

我在 /etc/profile 中添加了以下环境变量以支持 Qt5 中的 tslib:

## For Qt5 Touchscreen Support
export QT_DEBUG_PLUGINS=1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/arm-linux-gnueabihf/
export QT_PLUGIN_PATH=/usr/lib/plugins
export QT_QPA_FONTDIR=/usr/lib/fonts
export QT_QPA_PLATFORM_PLUGIN_PATH=/usr/lib/plugins/platforms
export QT_QPA_PLATFORM=linuxfb
export QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event0
export QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS=/dev/input/event0
export TSLIB_TSEVENTTYPE='INPUT'
export TSLIB_CALIBFILE='/etc/pointercal'
export TSLIB_CONFFILE='/etc/ts.conf'
export TSLIB_CONSOLEDEVICE='none'
export TSLIB_FBDEVICE='/dev/fb0'
export TSLIB_PLUGINDIR='/usr/lib/ts'
export TSLIB_TSDEVICE='/dev/input/event0'

我阅读了 Qt5 文档以及如何在我的应用程序中获取触摸事件。我有一个主要的 Widget 并在构造函数中设置了适当的标志:

MainWidget::MainWidget(QLabel *parent)
    : QLabel(parent)
{

    qDebug() << "Setting WA_AcceptTouchEvents on MainWidget...";

    // Accept touch events
    setAttribute(Qt::WA_AcceptTouchEvents);
    setAttribute(Qt::WA_StaticContents);

}

我设置了一个事件过滤器来尝试捕获触摸事件:

bool MainWidget::eventFilter( QObject* target, QEvent* e )
{

    qDebug() << "Event Type: " << e->type();

    return false;

    return QLabel::eventFilter(target, e);
}

我这样启动我的应用程序:

myapp -platform linuxfb:fb=/dev/fb0 -plugin tslib:/dev/input/event0

我还在 Qt 的源代码中为 qtslib.cpp:

取消了对单个 printf 的注释
void QTsLibMouseHandler::readMouseData()
{
    ts_sample sample;

    while (get_sample(m_dev, &sample, m_rawMode)) {
        bool pressed = sample.pressure;
        int x = sample.x;
        int y = sample.y;

        // work around missing coordinates on mouse release
        if (sample.pressure == 0 && sample.x == 0 && sample.y == 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);

        //printf("handleMouseEvent %d %d %d %ld\n", m_x, m_y, pressed, sample.tv.tv_usec);

        QWindowSystemInterface::handleMouseEvent(0, pos, pos, pressed ? Qt::LeftButton : Qt::NoButton);

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

当我启动我的 Qt 应用程序时,我看到插件加载正常(甚至显示正确的 event0 文件)。当我触摸屏幕时,我还看到 qt tslib 插件正在接收触摸事件。问题是永远不会调用事件过滤器!

这是正在启动的应用程序:

Got keys from plugin meta data ("tslib", "tslibraw")
QFactoryLoader::QFactoryLoader() checking directory path "/home/pi/generic" ...
loaded library "/usr/lib/qt/plugins/generic/libqtslibplugin.so"
QTsLibMouseHandler "tslib" ""
QTsLibMouseHandler "tslib" "/dev/input/event0"
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/qt/plugins/styles" ...
QFactoryLoader::QFactoryLoader() checking directory path "/home/pi/styles" ...
Setting WA_AcceptTouchEvents on MainWidget...

-----------------------------------------
Waiting for data now...
-----------------------------------------

handleMouseEvent 0 0 1 751196
handleMouseEvent 0 0 1 751196
handleMouseEvent 1696 1615 1 771075
handleMouseEvent 1696 1615 1 771075
handleMouseEvent 1679 1622 1 781368
handleMouseEvent 1671 1638 1 781368
handleMouseEvent 1679 1622 1 781368
handleMouseEvent 1671 1638 1 781368
...

我发现了一些论坛帖子,其中人们在使用 linuxfb 平台插件进行触摸输入时遇到问题:

我已经尝试了他们所有的建议,但问题仍然存在 - 即使 Qt tslib 插件说它正在接收触摸事件,我的应用程序也没有收到触摸事件。

似乎 tslib 插件在将它接收到的事件注入我的应用程序的事件循环时遇到了问题:

QWindowSystemInterface::handleMouseEvent(0, pos, pos, pressed ? Qt::LeftButton : Qt::NoButton);

我还尝试了 Qt5.4 触摸指纹示例并看到相同的行为 - 没有接收到触摸事件。

我不确定从这里到哪里去。如果能帮助解决这个问题,我将不胜感激。谢谢!

更新:

我更改了我的事件过滤器,现在看起来像这样:

bool MainWidget::eventFilter(QObject *obj, QEvent *event)
{

    qDebug() << "Event received" << obj->metaObject()->className() <<  event->type();
    switch (event->type()) {
        case QEvent::TouchBegin:
            qDebug() << "TouchBegin";
        case QEvent::TouchUpdate:
            qDebug() << "TouchUpdate";
        case QEvent::TouchEnd:
            qDebug() << "TouchEnd";
        {

            //        QTouchEvent *touch = static_cast<QTouchEvent *>(event);
            //        QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
            //        foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) {
            //            switch (touchPoint.state()) {
            //            case Qt::TouchPointStationary:
            //                // don't do anything if this touch point hasn't moved
            //                continue;
            //            default:
            //                {
            //                }
            //                break;
            //            }
            //        }
            //        break;
        }
        //default:
            //return QLabel::event(event);
    }
    //return true;

}

现在,每当我触摸屏幕时,我都能看到 'socket notifier' 事件与 Qt Tslib 插件的打印混合在一起。关于为什么事件类型为 50 而没有触摸事件的任何想法?

Event received QSocketNotifier 50
handleMouseEvent 2702 2618 0 557715
Event received QSocketNotifier 50
handleMouseEvent 2698 2612 1 547758
Event received QSocketNotifier 50
handleMouseEvent 2706 2802 1 759928
Event received QSocketNotifier 50
Event received QSocketNotifier 50

更新#2:

我安装事件过滤器只是为了尝试捕获任何事件。我不确定在 Qt5 中什么 t运行 将事件类型 50 ( QSocketNotifier ) 指定为 QTouch* 或 QMouse* 事件。

这里有更多信息:

通过确保在 RasbperryPi 上安装了 tslib 插件,问题已解决。

TSLIB_PLUGINDIR=/usr/lib/ts

Pi 上不存在目录 /usr/lib/ts。