Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwaylandtouch.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 "qwaylandtouch_p.h"
6#include "qwaylanddisplay_p.h"
7#include "qwaylandsurface_p.h"
8
9#include <QtGui/QPointingDevice>
10
12
13namespace QtWaylandClient {
14
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,
31 QInputDevice::Capabilities(caps), 10, 0);
33}
34
35static inline qreal fromFixed(int f)
36{
37 return f / qreal(10000);
38}
39
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("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("qt_touch_extension: handle_touch: No pointer focus");
62 return;
63 }
64 mTargetWindow = win->window();
65
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(flags >> 16);
77
79 // Got surface-relative coords but need a (virtual) screen position.
80 QPointF relPos = QPointF(fromFixed(x), fromFixed(y));
81 QPointF delta = relPos - relPos.toPoint();
82 tp.area.moveCenter(mTargetWindow->mapToGlobal(relPos.toPoint()) + delta);
83
84 tp.normalPosition.setX(fromFixed(normalized_x));
85 tp.normalPosition.setY(fromFixed(normalized_y));
86 tp.pressure = pressure / 255.0;
87 tp.velocity.setX(fromFixed(velocity_x));
88 tp.velocity.setY(fromFixed(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++;
97 }
98 }
99
100 mTouchPoints.append(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(j).id == prevPoint.id) {
117 found = true;
118 break;
119 }
120 if (!found) {
123 mTouchPoints.append(p);
124 }
125 }
126
127 if (mTouchPoints.isEmpty()) {
128 mPrevTouchPoints.clear();
129 return;
130 }
131
132 QWindowSystemInterface::handleTouchEvent(mTargetWindow, mTimestamp, mTouchDevice, 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
150 mLastMouseGlobal = tp.area.center();
151 QPoint globalPoint = mLastMouseGlobal.toPoint();
152 QPointF delta = mLastMouseGlobal - globalPoint;
153 mLastMouseLocal = mTargetWindow->mapFromGlobal(globalPoint) + delta;
154 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal,
155 buttons, Qt::LeftButton, eventType);
156 if (buttons == Qt::NoButton)
157 mMouseSourceId = -1;
158 break;
159 }
160 }
161 }
162
163 mPrevTouchPoints = mTouchPoints;
164 mTouchPoints.clear();
165
167 mPrevTouchPoints.clear();
168}
169
171{
172 mTouchPoints.clear();
173 mPrevTouchPoints.clear();
174 if (mMouseSourceId != -1)
175 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal, Qt::NoButton, Qt::LeftButton, QEvent::MouseButtonRelease);
176}
177
179{
180 mFlags = flags;
181}
182
183}
184
State
Specifies the state of this event point.
Definition qeventpoint.h:49
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
@ MouseMove
Definition qcoreevent.h:63
@ MouseButtonPress
Definition qcoreevent.h:60
@ MouseButtonRelease
Definition qcoreevent.h:61
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
T & first()
Definition qlist.h:628
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:348
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:394
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:343
\inmodule QtCore\reentrant
Definition qpoint.h:23
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr void moveCenter(const QPointF &p) noexcept
Moves the rectangle, leaving the center point at the given position.
Definition qrect.h:712
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qvectornd.h:505
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qvectornd.h:504
QWidget * window() const
Returns the window for this widget, i.e.
Definition qwidget.cpp:4320
static bool handleTouchEvent(QWindow *window, const QPointingDevice *device, const QList< struct TouchPoint > &points, Qt::KeyboardModifiers mods=Qt::NoModifier)
static bool handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global, Qt::MouseButtons state, Qt::MouseButton button, QEvent::Type type, Qt::KeyboardModifiers mods=Qt::NoModifier, Qt::MouseEventSource source=Qt::MouseEventNotSynthesized)
static void registerInputDevice(const QInputDevice *device)
QList< QWaylandInputDevice * > inputDevices() const
void touch_extension_touch(uint32_t time, uint32_t id, uint32_t state, int32_t x, int32_t y, int32_t normalized_x, int32_t normalized_y, int32_t width, int32_t height, uint32_t pressure, int32_t velocity_x, int32_t velocity_y, uint32_t flags, struct wl_array *rawdata) override
void touch_extension_configure(uint32_t flags) override
QWaylandTouchExtension(QWaylandDisplay *display, uint32_t id)
else opt state
[0]
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
static qreal fromFixed(int f)
@ LeftButton
Definition qnamespace.h:57
@ NoButton
Definition qnamespace.h:56
#define qWarning
Definition qlogging.h:162
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLenum GLuint id
[7]
GLfloat GLfloat f
GLint GLsizei width
GLbitfield flags
GLint y
GLfloat GLfloat p
[1]
GLuint * states
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
double qreal
Definition qtypes.h:92
QWidget * win
Definition settings.cpp:6
QObject::connect nullptr