Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
27{
28 qt_touch_extension_destroy(object());
29}
30
31void QWaylandTouchExtension::registerDevice(int caps)
32{
33 // TODO number of touchpoints, actual name and ID
34 mTouchDevice = new QPointingDevice(QLatin1String("some touchscreen"), 0,
36 QInputDevice::Capabilities(caps), 10, 0);
38}
39
40static inline qreal fromFixed(int f)
41{
42 return f / qreal(10000);
43}
44
46 uint32_t id, uint32_t state, int32_t x, int32_t y,
47 int32_t normalized_x, int32_t normalized_y,
48 int32_t width, int32_t height, uint32_t pressure,
49 int32_t velocity_x, int32_t velocity_y,
50 uint32_t flags, wl_array *rawdata)
51{
52 if (!mInputDevice) {
53 QList<QWaylandInputDevice *> inputDevices = mDisplay->inputDevices();
54 if (inputDevices.isEmpty()) {
55 qWarning("qt_touch_extension: handle_touch: No input devices");
56 return;
57 }
58 mInputDevice = inputDevices.first();
59 }
60 QWaylandWindow *win = mInputDevice->touchFocus();
61 if (!win)
62 win = mInputDevice->pointerFocus();
63 if (!win)
64 win = mInputDevice->keyboardFocus();
65 if (!win || !win->window()) {
66 qWarning("qt_touch_extension: handle_touch: No pointer focus");
67 return;
68 }
69 mTargetWindow = win->window();
70
72 tp.id = id;
73 tp.state = QEventPoint::State(int(state & 0xFFFF));
74 int sentPointCount = state >> 16;
75 if (!mPointsLeft) {
76 Q_ASSERT(sentPointCount > 0);
77 mPointsLeft = sentPointCount;
78 }
79
80 if (!mTouchDevice)
81 registerDevice(flags >> 16);
82
84 // Got surface-relative coords but need a (virtual) screen position.
85 QPointF relPos = QPointF(fromFixed(x), fromFixed(y));
86 QPointF delta = relPos - relPos.toPoint();
87 tp.area.moveCenter(mTargetWindow->mapToGlobal(relPos.toPoint()) + delta);
88
89 tp.normalPosition.setX(fromFixed(normalized_x));
90 tp.normalPosition.setY(fromFixed(normalized_y));
91 tp.pressure = pressure / 255.0;
92 tp.velocity.setX(fromFixed(velocity_x));
93 tp.velocity.setY(fromFixed(velocity_y));
94
95 if (rawdata) {
96 const int rawPosCount = rawdata->size / sizeof(float) / 2;
97 float *p = static_cast<float *>(rawdata->data);
98 for (int i = 0; i < rawPosCount; ++i) {
99 float x = *p++;
100 float y = *p++;
102 }
103 }
104
105 mTouchPoints.append(tp);
106 mTimestamp = time;
107
108 if (!--mPointsLeft)
109 sendTouchEvent();
110}
111
112void QWaylandTouchExtension::sendTouchEvent()
113{
114 // Copy all points, that are in the previous but not in the current list, as stationary.
115 for (int i = 0; i < mPrevTouchPoints.size(); ++i) {
116 const QWindowSystemInterface::TouchPoint &prevPoint(mPrevTouchPoints.at(i));
117 if (prevPoint.state == QEventPoint::Released)
118 continue;
119 bool found = false;
120 for (int j = 0; j < mTouchPoints.size(); ++j)
121 if (mTouchPoints.at(j).id == prevPoint.id) {
122 found = true;
123 break;
124 }
125 if (!found) {
128 mTouchPoints.append(p);
129 }
130 }
131
132 if (mTouchPoints.isEmpty()) {
133 mPrevTouchPoints.clear();
134 return;
135 }
136
137 QWindowSystemInterface::handleTouchEvent(mTargetWindow, mTimestamp, mTouchDevice, mTouchPoints);
138
139 QEventPoint::States states = {};
140 for (int i = 0; i < mTouchPoints.size(); ++i)
141 states |= mTouchPoints.at(i).state;
142
143 if (mFlags & QT_TOUCH_EXTENSION_FLAGS_MOUSE_FROM_TOUCH) {
144 const bool firstPress = states == QEventPoint::Pressed;
145 if (firstPress)
146 mMouseSourceId = mTouchPoints.first().id;
147 for (int i = 0; i < mTouchPoints.size(); ++i) {
148 const QWindowSystemInterface::TouchPoint &tp(mTouchPoints.at(i));
149 if (tp.id == mMouseSourceId) {
150 const bool released = tp.state == QEventPoint::Released;
151 Qt::MouseButtons buttons = released ? Qt::NoButton : Qt::LeftButton;
152 QEvent::Type eventType = firstPress ? QEvent::MouseButtonPress
153 : released ? QEvent::MouseButtonRelease
155 mLastMouseGlobal = tp.area.center();
156 QPoint globalPoint = mLastMouseGlobal.toPoint();
157 QPointF delta = mLastMouseGlobal - globalPoint;
158 mLastMouseLocal = mTargetWindow->mapFromGlobal(globalPoint) + delta;
159 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal,
160 buttons, Qt::LeftButton, eventType);
161 if (buttons == Qt::NoButton)
162 mMouseSourceId = -1;
163 break;
164 }
165 }
166 }
167
168 mPrevTouchPoints = mTouchPoints;
169 mTouchPoints.clear();
170
172 mPrevTouchPoints.clear();
173}
174
176{
177 mTouchPoints.clear();
178 mPrevTouchPoints.clear();
179 if (mMouseSourceId != -1)
180 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal, Qt::NoButton, Qt::LeftButton, QEvent::MouseButtonRelease);
181}
182
184{
185 mFlags = flags;
186}
187
188}
189
State
Specifies the state of this event point.
Definition qeventpoint.h:48
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
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
T & first()
Definition qlist.h:645
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:358
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:404
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:353
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr void moveCenter(const QPointF &p) noexcept
Moves the rectangle, leaving the center point at the given position.
Definition qrect.h:726
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:4313
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:58
@ NoButton
Definition qnamespace.h:57
#define qWarning
Definition qlogging.h:166
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:187
QWidget * win
Definition settings.cpp:6
QObject::connect nullptr