summaryrefslogtreecommitdiffstats
path: root/src/platformsupport/input/evdevtouch
diff options
context:
space:
mode:
Diffstat (limited to 'src/platformsupport/input/evdevtouch')
-rw-r--r--src/platformsupport/input/evdevtouch/qevdevtouchfilter_p.h175
-rw-r--r--src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp272
-rw-r--r--src/platformsupport/input/evdevtouch/qevdevtouchhandler_p.h30
3 files changed, 440 insertions, 37 deletions
diff --git a/src/platformsupport/input/evdevtouch/qevdevtouchfilter_p.h b/src/platformsupport/input/evdevtouch/qevdevtouchfilter_p.h
new file mode 100644
index 0000000000..ff6085d725
--- /dev/null
+++ b/src/platformsupport/input/evdevtouch/qevdevtouchfilter_p.h
@@ -0,0 +1,175 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the plugins module 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 <qglobal.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.
+//
+
+QT_BEGIN_NAMESPACE
+
+struct QEvdevTouchFilter
+{
+ QEvdevTouchFilter();
+
+ void initialize(float pos, float velocity);
+ void update(float pos, float velocity, float timeDelta);
+
+ float position() const { return x.x; }
+ float velocity() const { return x.y; }
+
+private:
+ struct vec2 {
+ vec2(float x = 0.0f, float y = 0.0f) : x(x), y(y) { }
+ float x, y;
+
+ vec2 operator-(vec2 v) {
+ return vec2(x - v.x, y - v.y);
+ }
+
+ vec2 operator+(vec2 v) {
+ return vec2(x + v.x, y + v.y);
+ }
+ };
+
+ struct mat2 {
+ float a, b, c, d;
+ mat2(float a = 1.0f, float b = 0.0f, float c = 0.0f, float d = 1.0f)
+ : a(a)
+ , b(b)
+ , c(c)
+ , d(d)
+ {
+ }
+
+ mat2 transposed() const {
+ return mat2(a, c,
+ b, d);
+ }
+
+ mat2 inverted() const {
+ float det = 1.0f / (a * d - b * c);
+ return mat2( d * det, -b * det,
+ -c * det, a * det);
+ }
+
+ mat2 operator+(mat2 m) const {
+ return mat2(a + m.a, b + m.b,
+ c + m.c, d + m.d);
+ }
+
+ mat2 operator-(mat2 m) const {
+ return mat2(a - m.a, b - m.b,
+ c - m.c, d - m.d);
+ }
+
+ vec2 operator*(vec2 v) const {
+ return vec2(a * v.x + b * v.y,
+ c * v.x + d * v.y);
+ }
+
+ mat2 operator*(mat2 M) const {
+ return mat2(a * M.a + b * M.c,
+ a * M.b + b * M.d,
+ c * M.a + d * M.c,
+ c * M.b + d * M.d);
+ }
+ };
+
+ vec2 x;
+ mat2 A;
+ mat2 P;
+ mat2 Q;
+ mat2 R;
+ mat2 H;
+};
+
+inline QEvdevTouchFilter::QEvdevTouchFilter()
+{
+}
+
+inline void QEvdevTouchFilter::initialize(float pos, float velocity)
+{
+ x = vec2(pos, velocity);
+
+ P = mat2(0.0f, 0.0f,
+ 0.0f, 0.0f);
+
+ Q = mat2(0.0f, 0.0f,
+ 0.0f, 0.1f);
+ R = mat2(0.1f, 0.0f,
+ 0.0f, 0.1f);
+}
+
+inline void QEvdevTouchFilter::update(float pos, float velocity, float dT)
+{
+ A.b = dT;
+
+ // Prediction setp
+ x = A * x;
+ P = A * P * A.transposed() + Q;
+
+ // Correction step (complete with H)
+ // mat2 S = H * P * H.transposed() + R;
+ // mat2 K = P * H.transposed() * S.inverted();
+ // vec2 m(pos, velocity);
+ // vec2 y = m - H * x;
+ // x = x + K * y;
+ // P = (mat2() - K * H) * P;
+
+ // Correction step (without H as H is currently set to I, so we can ignore
+ // it in the calculations...)
+ mat2 S = P + R;
+ mat2 K = P * S.inverted();
+ vec2 m(pos, velocity);
+ vec2 y = m - x;
+ x = x + K * y;
+ P = (mat2() - K) * P;
+
+}
+
+QT_END_NAMESPACE
diff --git a/src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp b/src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp
index d53a317fc5..11f7311bb7 100644
--- a/src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp
+++ b/src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins module of the Qt Toolkit.
@@ -48,7 +49,11 @@
#include <QtCore/private/qcore_unix_p.h>
#include <QtGui/private/qhighdpiscaling_p.h>
#include <QtGui/private/qguiapplication_p.h>
+#ifdef Q_OS_FREEBSD
+#include <dev/evdev/input.h>
+#else
#include <linux/input.h>
+#endif
#if QT_CONFIG(mtdev)
extern "C" {
@@ -96,6 +101,7 @@ public:
QEvdevTouchScreenHandler *q;
int m_lastEventType;
QList<QWindowSystemInterface::TouchPoint> m_touchPoints;
+ QList<QWindowSystemInterface::TouchPoint> m_lastTouchPoints;
struct Contact {
int trackingId;
@@ -114,11 +120,16 @@ public:
Contact m_currentData;
int m_currentSlot;
+ double m_timeStamp;
+ double m_lastTimeStamp;
+
int findClosestContact(const QHash<int, Contact> &contacts, int x, int y, int *dist);
void addTouchPoint(const Contact &contact, Qt::TouchPointStates *combinedStates);
void reportPoints();
void loadMultiScreenMappings();
+ QRect screenGeometry() const;
+
int hw_range_x_min;
int hw_range_x_max;
int hw_range_y_min;
@@ -132,19 +143,40 @@ public:
QTransform m_rotate;
bool m_singleTouch;
QString m_screenName;
- QPointer<QScreen> m_screen;
+ mutable QPointer<QScreen> m_screen;
+
+ // Touch filtering and prediction are part of the same thing. The default
+ // prediction is 0ms, but sensible results can be acheived by setting it
+ // to, for instance, 16ms.
+ // For filtering to work well, the QPA plugin should provide a dead-steady
+ // implementation of QPlatformWindow::requestUpdate().
+ bool m_filtered;
+ int m_prediction;
+
+ // When filtering is enabled, protect the access to current and last
+ // timeStamp and touchPoints, as these are being read on the gui thread.
+ QMutex m_mutex;
};
QEvdevTouchScreenData::QEvdevTouchScreenData(QEvdevTouchScreenHandler *q_ptr, const QStringList &args)
: q(q_ptr),
m_lastEventType(-1),
m_currentSlot(0),
+ m_timeStamp(0), m_lastTimeStamp(0),
hw_range_x_min(0), hw_range_x_max(0),
hw_range_y_min(0), hw_range_y_max(0),
hw_pressure_min(0), hw_pressure_max(0),
- m_typeB(false), m_singleTouch(false)
+ m_forceToActiveWindow(false), m_typeB(false), m_singleTouch(false),
+ m_filtered(false), m_prediction(0)
{
- m_forceToActiveWindow = args.contains(QLatin1String("force_window"));
+ for (const QString &arg : args) {
+ if (arg == QStringLiteral("force_window"))
+ m_forceToActiveWindow = true;
+ else if (arg == QStringLiteral("filtered"))
+ m_filtered = true;
+ else if (arg.startsWith(QStringLiteral("prediction=")))
+ m_prediction = arg.mid(11).toInt();
+ }
}
#define LONG_BITS (sizeof(long) << 3)
@@ -228,9 +260,14 @@ QEvdevTouchScreenHandler::QEvdevTouchScreenHandler(const QString &device, const
#endif
d->deviceNode = device;
-
- qCDebug(qLcEvdevTouch, "evdevtouch: %s: Protocol type %c %s (%s)", qPrintable(d->deviceNode),
- d->m_typeB ? 'B' : 'A', mtdevStr, d->m_singleTouch ? "single" : "multi");
+ qCDebug(qLcEvdevTouch,
+ "evdevtouch: %s: Protocol type %c %s (%s), filtered=%s",
+ qPrintable(d->deviceNode),
+ d->m_typeB ? 'B' : 'A', mtdevStr,
+ d->m_singleTouch ? "single" : "multi",
+ d->m_filtered ? "yes" : "no");
+ if (d->m_filtered)
+ qCDebug(qLcEvdevTouch, " - prediction=%d", d->m_prediction);
input_absinfo absInfo;
memset(&absInfo, 0, sizeof(input_absinfo));
@@ -327,6 +364,11 @@ QEvdevTouchScreenHandler::~QEvdevTouchScreenHandler()
unregisterTouchDevice();
}
+bool QEvdevTouchScreenHandler::isFiltered() const
+{
+ return d->m_filtered;
+}
+
QTouchDevice *QEvdevTouchScreenHandler::touchDevice() const
{
return m_device;
@@ -516,6 +558,14 @@ void QEvdevTouchScreenData::processInputEvent(input_event *data)
if (!m_contacts.isEmpty() && m_contacts.constBegin().value().trackingId == -1)
assignIds();
+ if (m_filtered)
+ m_mutex.lock();
+
+ // update timestamps
+ m_lastTimeStamp = m_timeStamp;
+ m_timeStamp = data->time.tv_sec + data->time.tv_usec / 1000000.0;
+
+ m_lastTouchPoints = m_touchPoints;
m_touchPoints.clear();
Qt::TouchPointStates combinedStates;
@@ -593,8 +643,12 @@ void QEvdevTouchScreenData::processInputEvent(input_event *data)
if (!m_typeB && !m_singleTouch)
m_contacts.clear();
+
if (!m_touchPoints.isEmpty() && combinedStates != Qt::TouchPointStationary)
reportPoints();
+
+ if (m_filtered)
+ m_mutex.unlock();
}
m_lastEventType = data->type;
@@ -653,41 +707,45 @@ void QEvdevTouchScreenData::assignIds()
m_contacts = newContacts;
}
-void QEvdevTouchScreenData::reportPoints()
+QRect QEvdevTouchScreenData::screenGeometry() const
{
- QRect winRect;
if (m_forceToActiveWindow) {
QWindow *win = QGuiApplication::focusWindow();
- if (!win)
- return;
- winRect = QHighDpi::toNativePixels(win->geometry(), win);
- } else {
- // Now it becomes tricky. Traditionally we picked the primaryScreen()
- // and were done with it. But then, enter multiple screens, and
- // suddenly it was all broken.
- //
- // For now we only support the display configuration of the KMS/DRM
- // backends of eglfs. See QTouchOutputMapping.
- //
- // The good news it that once winRect refers to the correct screen
- // geometry in the full virtual desktop space, there is nothing else
- // left to do since qguiapp will handle the rest.
- QScreen *screen = QGuiApplication::primaryScreen();
- if (!m_screenName.isEmpty()) {
- if (!m_screen) {
- const QList<QScreen *> screens = QGuiApplication::screens();
- for (QScreen *s : screens) {
- if (s->name() == m_screenName) {
- m_screen = s;
- break;
- }
+ return win ? QHighDpi::toNativePixels(win->geometry(), win) : QRect();
+ }
+
+ // Now it becomes tricky. Traditionally we picked the primaryScreen()
+ // and were done with it. But then, enter multiple screens, and
+ // suddenly it was all broken.
+ //
+ // For now we only support the display configuration of the KMS/DRM
+ // backends of eglfs. See QTouchOutputMapping.
+ //
+ // The good news it that once winRect refers to the correct screen
+ // geometry in the full virtual desktop space, there is nothing else
+ // left to do since qguiapp will handle the rest.
+ QScreen *screen = QGuiApplication::primaryScreen();
+ if (!m_screenName.isEmpty()) {
+ if (!m_screen) {
+ const QList<QScreen *> screens = QGuiApplication::screens();
+ for (QScreen *s : screens) {
+ if (s->name() == m_screenName) {
+ m_screen = s;
+ break;
}
}
- if (m_screen)
- screen = m_screen;
}
- winRect = QHighDpi::toNativePixels(screen->geometry(), screen);
+ if (m_screen)
+ screen = m_screen;
}
+ return QHighDpi::toNativePixels(screen->geometry(), screen);
+}
+
+void QEvdevTouchScreenData::reportPoints()
+{
+ QRect winRect = screenGeometry();
+ if (winRect.isNull())
+ return;
const int hw_w = hw_range_x_max - hw_range_x_min;
const int hw_h = hw_range_y_max - hw_range_y_min;
@@ -718,13 +776,17 @@ void QEvdevTouchScreenData::reportPoints()
}
// Let qguiapp pick the target window.
- QWindowSystemInterface::handleTouchEvent(Q_NULLPTR, q->touchDevice(), m_touchPoints);
+ if (m_filtered)
+ emit q->touchPointsUpdated();
+ else
+ QWindowSystemInterface::handleTouchEvent(Q_NULLPTR, q->touchDevice(), m_touchPoints);
}
-
-
QEvdevTouchScreenHandlerThread::QEvdevTouchScreenHandlerThread(const QString &device, const QString &spec, QObject *parent)
: QDaemonThread(parent), m_device(device), m_spec(spec), m_handler(Q_NULLPTR), m_touchDeviceRegistered(false)
+ , m_touchUpdatePending(false)
+ , m_filterWindow(Q_NULLPTR)
+ , m_touchRate(-1)
{
start();
}
@@ -738,6 +800,10 @@ QEvdevTouchScreenHandlerThread::~QEvdevTouchScreenHandlerThread()
void QEvdevTouchScreenHandlerThread::run()
{
m_handler = new QEvdevTouchScreenHandler(m_device, m_spec);
+
+ if (m_handler->isFiltered())
+ connect(m_handler, &QEvdevTouchScreenHandler::touchPointsUpdated, this, &QEvdevTouchScreenHandlerThread::scheduleTouchPointUpdate);
+
// Report the registration to the parent thread by invoking the method asynchronously
QMetaObject::invokeMethod(this, "notifyTouchDeviceRegistered", Qt::QueuedConnection);
@@ -758,5 +824,137 @@ void QEvdevTouchScreenHandlerThread::notifyTouchDeviceRegistered()
emit touchDeviceRegistered();
}
+void QEvdevTouchScreenHandlerThread::scheduleTouchPointUpdate()
+{
+ QWindow *window = QGuiApplication::focusWindow();
+ if (window != m_filterWindow) {
+ if (m_filterWindow)
+ m_filterWindow->removeEventFilter(this);
+ m_filterWindow = window;
+ if (m_filterWindow)
+ m_filterWindow->installEventFilter(this);
+ }
+ if (m_filterWindow) {
+ m_touchUpdatePending = true;
+ m_filterWindow->requestUpdate();
+ }
+}
+
+bool QEvdevTouchScreenHandlerThread::eventFilter(QObject *object, QEvent *event)
+{
+ if (m_touchUpdatePending && object == m_filterWindow && event->type() == QEvent::UpdateRequest) {
+ m_touchUpdatePending = false;
+ filterAndSendTouchPoints();
+ }
+ return false;
+}
+
+void QEvdevTouchScreenHandlerThread::filterAndSendTouchPoints()
+{
+ QRect winRect = m_handler->d->screenGeometry();
+ if (winRect.isNull())
+ return;
+
+ float vsyncDelta = 1.0f / QGuiApplication::primaryScreen()->refreshRate();
+
+ QHash<int, FilteredTouchPoint> filteredPoints;
+
+ m_handler->d->m_mutex.lock();
+
+ double time = m_handler->d->m_timeStamp;
+ double lastTime = m_handler->d->m_lastTimeStamp;
+ double touchDelta = time - lastTime;
+ if (m_touchRate < 0 || touchDelta > vsyncDelta) {
+ // We're at the very start, with nothing to go on, so make a guess
+ // that the touch rate will be somewhere in the range of half a vsync.
+ // This doesn't have to be accurate as we will calibrate it over time,
+ // but it gives us a better starting point so calibration will be
+ // slightly quicker. If, on the other hand, we already have an
+ // estimate, we'll leave it as is and keep it.
+ if (m_touchRate < 0)
+ m_touchRate = (1.0 / QGuiApplication::primaryScreen()->refreshRate()) / 2.0;
+
+ } else {
+ // Update our estimate for the touch rate. We're making the assumption
+ // that this value will be mostly accurate with the occational bump,
+ // so we're weighting the existing value high compared to the update.
+ const double ratio = 0.9;
+ m_touchRate = sqrt(m_touchRate * m_touchRate * ratio + touchDelta * touchDelta * (1.0 - ratio));
+ }
+
+ QList<QWindowSystemInterface::TouchPoint> points = m_handler->d->m_touchPoints;
+ const QList<QWindowSystemInterface::TouchPoint> &lastPoints = m_handler->d->m_lastTouchPoints;
+
+ m_handler->d->m_mutex.unlock();
+
+ for (int i=0; i<points.size(); ++i) {
+ QWindowSystemInterface::TouchPoint &tp = points[i];
+ QPointF pos = tp.normalPosition;
+ FilteredTouchPoint f;
+
+ QWindowSystemInterface::TouchPoint ltp;
+ ltp.id = -1;
+ for (int j=0; j<lastPoints.size(); ++j) {
+ if (lastPoints.at(j).id == tp.id) {
+ ltp = lastPoints.at(j);
+ break;
+ }
+ }
+
+ QPointF velocity;
+ if (lastTime != 0 && ltp.id >= 0)
+ velocity = (pos - ltp.normalPosition) / m_touchRate;
+ if (m_filteredPoints.contains(tp.id)) {
+ f = m_filteredPoints.take(tp.id);
+ f.x.update(pos.x(), velocity.x(), vsyncDelta);
+ f.y.update(pos.y(), velocity.y(), vsyncDelta);
+ pos = QPointF(f.x.position(), f.y.position());
+ } else {
+ f.x.initialize(pos.x(), velocity.x());
+ f.y.initialize(pos.y(), velocity.y());
+ // Make sure the first instance of a touch point we send has the
+ // 'pressed' state.
+ if (tp.state != Qt::TouchPointPressed)
+ tp.state = Qt::TouchPointPressed;
+ }
+
+ tp.velocity = QVector2D(f.x.velocity() * winRect.width(), f.y.velocity() * winRect.height());
+
+ qreal filteredNormalizedX = f.x.position() + f.x.velocity() * m_handler->d->m_prediction / 1000.0;
+ qreal filteredNormalizedY = f.y.position() + f.y.velocity() * m_handler->d->m_prediction / 1000.0;
+
+ // Clamp to the screen
+ tp.normalPosition = QPointF(qBound<qreal>(0, filteredNormalizedX, 1),
+ qBound<qreal>(0, filteredNormalizedY, 1));
+
+ qreal x = winRect.x() + (tp.normalPosition.x() * (winRect.width() - 1));
+ qreal y = winRect.y() + (tp.normalPosition.y() * (winRect.height() - 1));
+
+ tp.area.moveCenter(QPointF(x, y));
+
+ // Store the touch point for later so we can release it if we've
+ // missed the actual release between our last update and this.
+ f.touchPoint = tp;
+
+ // Don't store the point for future reference if it is a release.
+ if (tp.state != Qt::TouchPointReleased)
+ filteredPoints[tp.id] = f;
+ }
+
+ for (QHash<int, FilteredTouchPoint>::const_iterator it = m_filteredPoints.constBegin(), end = m_filteredPoints.constEnd(); it != end; ++it) {
+ const FilteredTouchPoint &f = it.value();
+ QWindowSystemInterface::TouchPoint tp = f.touchPoint;
+ tp.state = Qt::TouchPointReleased;
+ tp.velocity = QVector2D();
+ points.append(tp);
+ }
+
+ m_filteredPoints = filteredPoints;
+
+ QWindowSystemInterface::handleTouchEvent(Q_NULLPTR,
+ m_handler->touchDevice(),
+ points);
+}
+
QT_END_NAMESPACE
diff --git a/src/platformsupport/input/evdevtouch/qevdevtouchhandler_p.h b/src/platformsupport/input/evdevtouch/qevdevtouchhandler_p.h
index 6554d4998c..d22aca3266 100644
--- a/src/platformsupport/input/evdevtouch/qevdevtouchhandler_p.h
+++ b/src/platformsupport/input/evdevtouch/qevdevtouchhandler_p.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins module of the Qt Toolkit.
@@ -58,6 +59,7 @@
#include <QThread>
#include <QtCore/private/qthread_p.h>
#include <qpa/qwindowsysteminterface.h>
+#include "qevdevtouchfilter_p.h"
#if QT_CONFIG(mtdev)
struct mtdev;
@@ -78,10 +80,18 @@ public:
QTouchDevice *touchDevice() const;
+ bool isFiltered() const;
+
private slots:
void readData();
+signals:
+ void touchPointsUpdated();
+
private:
+ friend class QEvdevTouchScreenData;
+ friend class QEvdevTouchScreenHandlerThread;
+
void registerTouchDevice();
void unregisterTouchDevice();
@@ -104,16 +114,36 @@ public:
bool isTouchDeviceRegistered() const;
+ bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE;
+
+public slots:
+ void scheduleTouchPointUpdate();
+
signals:
void touchDeviceRegistered();
private:
Q_INVOKABLE void notifyTouchDeviceRegistered();
+ void filterAndSendTouchPoints();
+ QRect targetScreenGeometry() const;
+
QString m_device;
QString m_spec;
QEvdevTouchScreenHandler *m_handler;
bool m_touchDeviceRegistered;
+
+ bool m_touchUpdatePending;
+ QWindow *m_filterWindow;
+
+ struct FilteredTouchPoint {
+ QEvdevTouchFilter x;
+ QEvdevTouchFilter y;
+ QWindowSystemInterface::TouchPoint touchPoint;
+ };
+ QHash<int, FilteredTouchPoint> m_filteredPoints;
+
+ float m_touchRate;
};
QT_END_NAMESPACE