Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtslib.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtslib_p.h"
5
6#include <QSocketNotifier>
7#include <QStringList>
8#include <QPoint>
9#include <QLoggingCategory>
10
11#include <qpa/qwindowsysteminterface.h>
12
13#include <errno.h>
14#include <tslib.h>
15
16QT_BEGIN_NAMESPACE
17
18using namespace Qt::StringLiterals;
19
20Q_STATIC_LOGGING_CATEGORY(qLcTsLib, "qt.qpa.input")
21
22QTsLibMouseHandler::QTsLibMouseHandler(const QString &key,
23 const QString &specification,
24 QObject *parent)
25 : QObject(parent),
26 m_rawMode(!key.compare("TslibRaw"_L1, Qt::CaseInsensitive))
27{
28 qCDebug(qLcTsLib) << "Initializing tslib plugin" << key << specification;
29 setObjectName("TSLib Mouse Handler"_L1);
30
31 m_dev = ts_setup(nullptr, 1);
32 if (!m_dev) {
33 qErrnoWarning(errno, "ts_setup() failed");
34 return;
35 }
36
37#ifdef TSLIB_VERSION_EVENTPATH /* also introduced in 1.15 */
38 qCDebug(qLcTsLib) << "tslib device is" << ts_get_eventpath(m_dev);
39#endif
40 m_notify = new QSocketNotifier(ts_fd(m_dev), QSocketNotifier::Read, this);
41 connect(m_notify, &QSocketNotifier::activated, this, &QTsLibMouseHandler::readMouseData);
42}
43
45{
46 if (m_dev)
47 ts_close(m_dev);
48}
49
50static bool get_sample(struct tsdev *dev, struct ts_sample *sample, bool rawMode)
51{
52 if (rawMode)
53 return (ts_read_raw(dev, sample, 1) == 1);
54 else
55 return (ts_read(dev, sample, 1) == 1);
56}
57
58void QTsLibMouseHandler::readMouseData()
59{
60 ts_sample sample;
61
62 while (get_sample(m_dev, &sample, m_rawMode)) {
63 bool pressed = sample.pressure;
64 int x = sample.x;
65 int y = sample.y;
66
67 // coordinates on release events can contain arbitrary values, just ignore them
68 if (sample.pressure == 0) {
69 x = m_x;
70 y = m_y;
71 }
72
73 if (!m_rawMode) {
74 //filtering: ignore movements of 2 pixels or less
75 int dx = x - m_x;
76 int dy = y - m_y;
77 if (dx*dx <= 4 && dy*dy <= 4 && pressed == m_pressed)
78 continue;
79 }
80 QPoint pos(x, y);
81
82 Qt::MouseButton button = pressed ^ m_pressed ? Qt::LeftButton : Qt::NoButton;
83 Qt::MouseButtons state = pressed ? Qt::LeftButton : Qt::NoButton;
84 QEvent::Type type = pressed ? (m_pressed ? QEvent::MouseMove : QEvent::MouseButtonPress)
85 : QEvent::MouseButtonRelease;
86
87 QWindowSystemInterface::handleMouseEvent(nullptr, pos, pos, state, button, type);
88
89 m_x = x;
90 m_y = y;
91 m_pressed = pressed;
92 }
93}
94
95QT_END_NAMESPACE
96
97#include "moc_qtslib_p.cpp"
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")
static bool get_sample(struct tsdev *dev, struct ts_sample *sample, bool rawMode)
Definition qtslib.cpp:50