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 "qwaylandtouch_p.h"
5#include "qwaylandinputdevice_p.h"
6#include "qwaylanddisplay_p.h"
7#include "qwaylandsurface_p.h"
8
9#include <QtGui/QPointingDevice>
10
11QT_BEGIN_NAMESPACE
12
13namespace QtWaylandClient {
14
15QWaylandTouchExtension::QWaylandTouchExtension(QWaylandDisplay *display, uint32_t id)
16 : QtWayland::qt_touch_extension(display->wl_registry(), id, 1),
17 mDisplay(display),
18 mTouchDevice(nullptr),
19 mPointsLeft(0),
20 mFlags(0),
21 mMouseSourceId(-1),
22 mInputDevice(nullptr)
23{
24}
25
26void QWaylandTouchExtension::registerDevice(int caps)
27{
28 // TODO number of touchpoints, actual name and ID
29 mTouchDevice = new QPointingDevice(QLatin1String("some touchscreen"), 0,
30 QInputDevice::DeviceType::TouchScreen, QPointingDevice::PointerType::Finger,
31 QInputDevice::Capabilities(caps), 10, 0);
32 QWindowSystemInterface::registerInputDevice(device: mTouchDevice);
33}
34
35static inline qreal fromFixed(int f)
36{
37 return f / qreal(10000);
38}
39
40void QWaylandTouchExtension::touch_extension_touch(uint32_t time,
41 uint32_t id, uint32_t state, int32_t x, int32_t y,
42 int32_t normalized_x, int32_t normalized_y,
43 int32_t width, int32_t height, uint32_t pressure,
44 int32_t velocity_x, int32_t velocity_y,
45 uint32_t flags, wl_array *rawdata)
46{
47 if (!mInputDevice) {
48 QList<QWaylandInputDevice *> inputDevices = mDisplay->inputDevices();
49 if (inputDevices.isEmpty()) {
50 qWarning(msg: "qt_touch_extension: handle_touch: No input devices");
51 return;
52 }
53 mInputDevice = inputDevices.first();
54 }
55 QWaylandWindow *win = mInputDevice->touchFocus();
56 if (!win)
57 win = mInputDevice->pointerFocus();
58 if (!win)
59 win = mInputDevice->keyboardFocus();
60 if (!win || !win->window()) {
61 qWarning(msg: "qt_touch_extension: handle_touch: No pointer focus");
62 return;
63 }
64 mTargetWindow = win->window();
65
66 QWindowSystemInterface::TouchPoint tp;
67 tp.id = id;
68 tp.state = QEventPoint::State(int(state & 0xFFFF));
69 int sentPointCount = state >> 16;
70 if (!mPointsLeft) {
71 Q_ASSERT(sentPointCount > 0);
72 mPointsLeft = sentPointCount;
73 }
74
75 if (!mTouchDevice)
76 registerDevice(caps: flags >> 16);
77
78 tp.area = QRectF(0, 0, fromFixed(f: width), fromFixed(f: height));
79 // Got surface-relative coords but need a (virtual) screen position.
80 QPointF relPos = QPointF(fromFixed(f: x), fromFixed(f: y));
81 QPointF delta = relPos - relPos.toPoint();
82 tp.area.moveCenter(p: mTargetWindow->mapToGlobal(pos: relPos.toPoint()) + delta);
83
84 tp.normalPosition.setX(fromFixed(f: normalized_x));
85 tp.normalPosition.setY(fromFixed(f: normalized_y));
86 tp.pressure = pressure / 255.0;
87 tp.velocity.setX(fromFixed(f: velocity_x));
88 tp.velocity.setY(fromFixed(f: velocity_y));
89
90 if (rawdata) {
91 const int rawPosCount = rawdata->size / sizeof(float) / 2;
92 float *p = static_cast<float *>(rawdata->data);
93 for (int i = 0; i < rawPosCount; ++i) {
94 float x = *p++;
95 float y = *p++;
96 tp.rawPositions.append(t: QPointF(x, y));
97 }
98 }
99
100 mTouchPoints.append(t: tp);
101 mTimestamp = time;
102
103 if (!--mPointsLeft)
104 sendTouchEvent();
105}
106
107void QWaylandTouchExtension::sendTouchEvent()
108{
109 // Copy all points, that are in the previous but not in the current list, as stationary.
110 for (int i = 0; i < mPrevTouchPoints.size(); ++i) {
111 const QWindowSystemInterface::TouchPoint &prevPoint(mPrevTouchPoints.at(i));
112 if (prevPoint.state == QEventPoint::Released)
113 continue;
114 bool found = false;
115 for (int j = 0; j < mTouchPoints.size(); ++j)
116 if (mTouchPoints.at(i: j).id == prevPoint.id) {
117 found = true;
118 break;
119 }
120 if (!found) {
121 QWindowSystemInterface::TouchPoint p = prevPoint;
122 p.state = QEventPoint::Stationary;
123 mTouchPoints.append(t: p);
124 }
125 }
126
127 if (mTouchPoints.isEmpty()) {
128 mPrevTouchPoints.clear();
129 return;
130 }
131
132 QWindowSystemInterface::handleTouchEvent(window: mTargetWindow, timestamp: mTimestamp, device: mTouchDevice, points: mTouchPoints);
133
134 QEventPoint::States states = {};
135 for (int i = 0; i < mTouchPoints.size(); ++i)
136 states |= mTouchPoints.at(i).state;
137
138 if (mFlags & QT_TOUCH_EXTENSION_FLAGS_MOUSE_FROM_TOUCH) {
139 const bool firstPress = states == QEventPoint::Pressed;
140 if (firstPress)
141 mMouseSourceId = mTouchPoints.first().id;
142 for (int i = 0; i < mTouchPoints.size(); ++i) {
143 const QWindowSystemInterface::TouchPoint &tp(mTouchPoints.at(i));
144 if (tp.id == mMouseSourceId) {
145 const bool released = tp.state == QEventPoint::Released;
146 Qt::MouseButtons buttons = released ? Qt::NoButton : Qt::LeftButton;
147 QEvent::Type eventType = firstPress ? QEvent::MouseButtonPress
148 : released ? QEvent::MouseButtonRelease
149 : QEvent::MouseMove;
150 mLastMouseGlobal = tp.area.center();
151 QPoint globalPoint = mLastMouseGlobal.toPoint();
152 QPointF delta = mLastMouseGlobal - globalPoint;
153 mLastMouseLocal = mTargetWindow->mapFromGlobal(pos: globalPoint) + delta;
154 QWindowSystemInterface::handleMouseEvent(window: mTargetWindow, timestamp: mTimestamp, local: mLastMouseLocal, global: mLastMouseGlobal,
155 state: buttons, button: Qt::LeftButton, type: eventType);
156 if (buttons == Qt::NoButton)
157 mMouseSourceId = -1;
158 break;
159 }
160 }
161 }
162
163 mPrevTouchPoints = mTouchPoints;
164 mTouchPoints.clear();
165
166 if (states == QEventPoint::Released)
167 mPrevTouchPoints.clear();
168}
169
170void QWaylandTouchExtension::touchCanceled()
171{
172 mTouchPoints.clear();
173 mPrevTouchPoints.clear();
174 if (mMouseSourceId != -1)
175 QWindowSystemInterface::handleMouseEvent(window: mTargetWindow, timestamp: mTimestamp, local: mLastMouseLocal, global: mLastMouseGlobal, state: Qt::NoButton, button: Qt::LeftButton, type: QEvent::MouseButtonRelease);
176}
177
178void QWaylandTouchExtension::touch_extension_configure(uint32_t flags)
179{
180 mFlags = flags;
181}
182
183}
184
185QT_END_NAMESPACE
186

source code of qtwayland/src/client/qwaylandtouch.cpp