1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qevdevtablethandler_p.h"
41
42#include <QStringList>
43#include <QSocketNotifier>
44#include <QGuiApplication>
45#include <QLoggingCategory>
46#include <QtCore/private/qcore_unix_p.h>
47#include <qpa/qwindowsysteminterface.h>
48#ifdef Q_OS_FREEBSD
49#include <dev/evdev/input.h>
50#else
51#include <linux/input.h>
52#endif
53
54QT_BEGIN_NAMESPACE
55
56Q_LOGGING_CATEGORY(qLcEvdevTablet, "qt.qpa.input")
57
58class QEvdevTabletData
59{
60public:
61 QEvdevTabletData(QEvdevTabletHandler *q_ptr);
62
63 void processInputEvent(input_event *ev);
64 void report();
65
66 QEvdevTabletHandler *q;
67 int lastEventType;
68 QString devName;
69 struct {
70 int x, y, p, d;
71 } minValues, maxValues;
72 struct {
73 int x, y, p, d;
74 bool down, lastReportDown;
75 int tool, lastReportTool;
76 QPointF lastReportPos;
77 } state;
78};
79
80QEvdevTabletData::QEvdevTabletData(QEvdevTabletHandler *q_ptr)
81 : q(q_ptr), lastEventType(0)
82{
83 memset(s: &minValues, c: 0, n: sizeof(minValues));
84 memset(s: &maxValues, c: 0, n: sizeof(maxValues));
85 memset(s: static_cast<void *>(&state), c: 0, n: sizeof(state));
86}
87
88void QEvdevTabletData::processInputEvent(input_event *ev)
89{
90 if (ev->type == EV_ABS) {
91 switch (ev->code) {
92 case ABS_X:
93 state.x = ev->value;
94 break;
95 case ABS_Y:
96 state.y = ev->value;
97 break;
98 case ABS_PRESSURE:
99 state.p = ev->value;
100 break;
101 case ABS_DISTANCE:
102 state.d = ev->value;
103 break;
104 default:
105 break;
106 }
107 } else if (ev->type == EV_KEY) {
108 // code BTN_TOOL_* value 1 -> proximity enter
109 // code BTN_TOOL_* value 0 -> proximity leave
110 // code BTN_TOUCH value 1 -> contact with screen
111 // code BTN_TOUCH value 0 -> no contact
112 switch (ev->code) {
113 case BTN_TOUCH:
114 state.down = ev->value != 0;
115 break;
116 case BTN_TOOL_PEN:
117 state.tool = ev->value ? QTabletEvent::Pen : 0;
118 break;
119 case BTN_TOOL_RUBBER:
120 state.tool = ev->value ? QTabletEvent::Eraser : 0;
121 break;
122 default:
123 break;
124 }
125 } else if (ev->type == EV_SYN && ev->code == SYN_REPORT && lastEventType != ev->type) {
126 report();
127 }
128 lastEventType = ev->type;
129}
130
131void QEvdevTabletData::report()
132{
133 if (!state.lastReportTool && state.tool)
134 QWindowSystemInterface::handleTabletEnterProximityEvent(device: QTabletEvent::Stylus, pointerType: state.tool, uid: q->deviceId());
135
136 qreal nx = (state.x - minValues.x) / qreal(maxValues.x - minValues.x);
137 qreal ny = (state.y - minValues.y) / qreal(maxValues.y - minValues.y);
138
139 QRect winRect = QGuiApplication::primaryScreen()->geometry();
140 QPointF globalPos(nx * winRect.width(), ny * winRect.height());
141 int pointer = state.tool;
142 // Prevent sending confusing values of 0 when moving the pen outside the active area.
143 if (!state.down && state.lastReportDown) {
144 globalPos = state.lastReportPos;
145 pointer = state.lastReportTool;
146 }
147
148 int pressureRange = maxValues.p - minValues.p;
149 qreal pressure = pressureRange ? (state.p - minValues.p) / qreal(pressureRange) : qreal(1);
150
151 if (state.down || state.lastReportDown) {
152 QWindowSystemInterface::handleTabletEvent(window: 0, local: QPointF(), global: globalPos,
153 device: QTabletEvent::Stylus, pointerType: pointer,
154 buttons: state.down ? Qt::LeftButton : Qt::NoButton,
155 pressure, xTilt: 0, yTilt: 0, tangentialPressure: 0, rotation: 0, z: 0, uid: q->deviceId(),
156 qGuiApp->keyboardModifiers());
157 }
158
159 if (state.lastReportTool && !state.tool)
160 QWindowSystemInterface::handleTabletLeaveProximityEvent(device: QTabletEvent::Stylus, pointerType: state.tool, uid: q->deviceId());
161
162 state.lastReportDown = state.down;
163 state.lastReportTool = state.tool;
164 state.lastReportPos = globalPos;
165}
166
167
168QEvdevTabletHandler::QEvdevTabletHandler(const QString &device, const QString &spec, QObject *parent)
169 : QObject(parent), m_fd(-1), m_device(device), m_notifier(0), d(0)
170{
171 Q_UNUSED(spec)
172
173 setObjectName(QLatin1String("Evdev Tablet Handler"));
174
175 qCDebug(qLcEvdevTablet, "evdevtablet: using %ls", qUtf16Printable(device));
176
177 m_fd = QT_OPEN(pathname: device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, mode: 0);
178 if (m_fd < 0) {
179 qErrnoWarning(msg: "evdevtablet: Cannot open input device %ls", qUtf16Printable(device));
180 return;
181 }
182
183 bool grabSuccess = !ioctl(fd: m_fd, EVIOCGRAB, (void *) 1);
184 if (grabSuccess)
185 ioctl(fd: m_fd, EVIOCGRAB, (void *) 0);
186 else
187 qWarning(msg: "evdevtablet: %ls: The device is grabbed by another process. No events will be read.", qUtf16Printable(device));
188
189 d = new QEvdevTabletData(this);
190 if (!queryLimits())
191 qWarning(msg: "evdevtablet: %ls: Unset or invalid ABS limits. Behavior will be unspecified.", qUtf16Printable(device));
192
193 m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
194 connect(sender: m_notifier, signal: &QSocketNotifier::activated, receiver: this, slot: &QEvdevTabletHandler::readData);
195}
196
197QEvdevTabletHandler::~QEvdevTabletHandler()
198{
199 if (m_fd >= 0)
200 QT_CLOSE(fd: m_fd);
201
202 delete d;
203}
204
205qint64 QEvdevTabletHandler::deviceId() const
206{
207 return m_fd;
208}
209
210bool QEvdevTabletHandler::queryLimits()
211{
212 bool ok = true;
213 input_absinfo absInfo;
214 memset(s: &absInfo, c: 0, n: sizeof(input_absinfo));
215 ok &= ioctl(fd: m_fd, EVIOCGABS(ABS_X), &absInfo) >= 0;
216 if (ok) {
217 d->minValues.x = absInfo.minimum;
218 d->maxValues.x = absInfo.maximum;
219 qCDebug(qLcEvdevTablet, "evdevtablet: %ls: min X: %d max X: %d", qUtf16Printable(m_device),
220 d->minValues.x, d->maxValues.x);
221 }
222 ok &= ioctl(fd: m_fd, EVIOCGABS(ABS_Y), &absInfo) >= 0;
223 if (ok) {
224 d->minValues.y = absInfo.minimum;
225 d->maxValues.y = absInfo.maximum;
226 qCDebug(qLcEvdevTablet, "evdevtablet: %ls: min Y: %d max Y: %d", qUtf16Printable(m_device),
227 d->minValues.y, d->maxValues.y);
228 }
229 if (ioctl(fd: m_fd, EVIOCGABS(ABS_PRESSURE), &absInfo) >= 0) {
230 d->minValues.p = absInfo.minimum;
231 d->maxValues.p = absInfo.maximum;
232 qCDebug(qLcEvdevTablet, "evdevtablet: %ls: min pressure: %d max pressure: %d", qUtf16Printable(m_device),
233 d->minValues.p, d->maxValues.p);
234 }
235 if (ioctl(fd: m_fd, EVIOCGABS(ABS_DISTANCE), &absInfo) >= 0) {
236 d->minValues.d = absInfo.minimum;
237 d->maxValues.d = absInfo.maximum;
238 qCDebug(qLcEvdevTablet, "evdevtablet: %ls: min distance: %d max distance: %d", qUtf16Printable(m_device),
239 d->minValues.d, d->maxValues.d);
240 }
241 char name[128];
242 if (ioctl(fd: m_fd, EVIOCGNAME(sizeof(name) - 1), name) >= 0) {
243 d->devName = QString::fromLocal8Bit(str: name);
244 qCDebug(qLcEvdevTablet, "evdevtablet: %ls: device name: %s", qUtf16Printable(m_device), name);
245 }
246 return ok;
247}
248
249void QEvdevTabletHandler::readData()
250{
251 input_event buffer[32];
252 int n = 0;
253 for (; ;) {
254 int result = QT_READ(fd: m_fd, data: reinterpret_cast<char*>(buffer) + n, maxlen: sizeof(buffer) - n);
255 if (!result) {
256 qWarning(msg: "evdevtablet: %ls: Got EOF from input device", qUtf16Printable(m_device));
257 return;
258 } else if (result < 0) {
259 if (errno != EINTR && errno != EAGAIN) {
260 qErrnoWarning(msg: "evdevtablet: %ls: Could not read from input device", qUtf16Printable(m_device));
261 if (errno == ENODEV) { // device got disconnected -> stop reading
262 delete m_notifier;
263 m_notifier = 0;
264 QT_CLOSE(fd: m_fd);
265 m_fd = -1;
266 }
267 return;
268 }
269 } else {
270 n += result;
271 if (n % sizeof(input_event) == 0)
272 break;
273 }
274 }
275
276 n /= sizeof(input_event);
277
278 for (int i = 0; i < n; ++i)
279 d->processInputEvent(ev: &buffer[i]);
280}
281
282
283QEvdevTabletHandlerThread::QEvdevTabletHandlerThread(const QString &device, const QString &spec, QObject *parent)
284 : QDaemonThread(parent), m_device(device), m_spec(spec), m_handler(0)
285{
286 start();
287}
288
289QEvdevTabletHandlerThread::~QEvdevTabletHandlerThread()
290{
291 quit();
292 wait();
293}
294
295void QEvdevTabletHandlerThread::run()
296{
297 m_handler = new QEvdevTabletHandler(m_device, m_spec);
298 exec();
299 delete m_handler;
300 m_handler = 0;
301}
302
303
304QT_END_NAMESPACE
305

source code of qtbase/src/platformsupport/input/evdevtablet/qevdevtablethandler.cpp