| 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 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | namespace QtWaylandClient { |
| 14 | |
| 15 | QWaylandTouchExtension::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 | |
| 26 | QWaylandTouchExtension::~QWaylandTouchExtension() |
| 27 | { |
| 28 | qt_touch_extension_destroy(object()); |
| 29 | } |
| 30 | |
| 31 | void QWaylandTouchExtension::registerDevice(int caps) |
| 32 | { |
| 33 | // TODO number of touchpoints, actual name and ID |
| 34 | mTouchDevice = new QPointingDevice(QLatin1String("some touchscreen" ), 0, |
| 35 | QInputDevice::DeviceType::TouchScreen, QPointingDevice::PointerType::Finger, |
| 36 | QInputDevice::Capabilities(caps), 10, 0); |
| 37 | QWindowSystemInterface::registerInputDevice(device: mTouchDevice); |
| 38 | } |
| 39 | |
| 40 | static inline qreal fromFixed(int f) |
| 41 | { |
| 42 | return f / qreal(10000); |
| 43 | } |
| 44 | |
| 45 | void QWaylandTouchExtension::touch_extension_touch(uint32_t time, |
| 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(msg: "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(msg: "qt_touch_extension: handle_touch: No pointer focus" ); |
| 67 | return; |
| 68 | } |
| 69 | mTargetWindow = win->window(); |
| 70 | |
| 71 | QWindowSystemInterface::TouchPoint tp; |
| 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(caps: flags >> 16); |
| 82 | |
| 83 | tp.area = QRectF(0, 0, fromFixed(f: width), fromFixed(f: height)); |
| 84 | // Got surface-relative coords but need a (virtual) screen position. |
| 85 | QPointF relPos = QPointF(fromFixed(f: x), fromFixed(f: y)); |
| 86 | QPointF delta = relPos - relPos.toPoint(); |
| 87 | tp.area.moveCenter(p: mTargetWindow->mapToGlobal(pos: relPos.toPoint()) + delta); |
| 88 | |
| 89 | tp.normalPosition.setX(fromFixed(f: normalized_x)); |
| 90 | tp.normalPosition.setY(fromFixed(f: normalized_y)); |
| 91 | tp.pressure = pressure / 255.0; |
| 92 | tp.velocity.setX(fromFixed(f: velocity_x)); |
| 93 | tp.velocity.setY(fromFixed(f: 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++; |
| 101 | tp.rawPositions.append(t: QPointF(x, y)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | mTouchPoints.append(t: tp); |
| 106 | mTimestamp = time; |
| 107 | |
| 108 | if (!--mPointsLeft) |
| 109 | sendTouchEvent(); |
| 110 | } |
| 111 | |
| 112 | void 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(i: j).id == prevPoint.id) { |
| 122 | found = true; |
| 123 | break; |
| 124 | } |
| 125 | if (!found) { |
| 126 | QWindowSystemInterface::TouchPoint p = prevPoint; |
| 127 | p.state = QEventPoint::Stationary; |
| 128 | mTouchPoints.append(t: p); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (mTouchPoints.isEmpty()) { |
| 133 | mPrevTouchPoints.clear(); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | QWindowSystemInterface::handleTouchEvent(window: mTargetWindow, timestamp: mTimestamp, device: mTouchDevice, points: 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 |
| 154 | : QEvent::MouseMove; |
| 155 | mLastMouseGlobal = tp.area.center(); |
| 156 | QPoint globalPoint = mLastMouseGlobal.toPoint(); |
| 157 | QPointF delta = mLastMouseGlobal - globalPoint; |
| 158 | mLastMouseLocal = mTargetWindow->mapFromGlobal(pos: globalPoint) + delta; |
| 159 | QWindowSystemInterface::handleMouseEvent(window: mTargetWindow, timestamp: mTimestamp, local: mLastMouseLocal, global: mLastMouseGlobal, |
| 160 | state: buttons, button: Qt::LeftButton, type: eventType); |
| 161 | if (buttons == Qt::NoButton) |
| 162 | mMouseSourceId = -1; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | mPrevTouchPoints = mTouchPoints; |
| 169 | mTouchPoints.clear(); |
| 170 | |
| 171 | if (states == QEventPoint::Released) |
| 172 | mPrevTouchPoints.clear(); |
| 173 | } |
| 174 | |
| 175 | void QWaylandTouchExtension::touchCanceled() |
| 176 | { |
| 177 | mTouchPoints.clear(); |
| 178 | mPrevTouchPoints.clear(); |
| 179 | if (mMouseSourceId != -1) |
| 180 | QWindowSystemInterface::handleMouseEvent(window: mTargetWindow, timestamp: mTimestamp, local: mLastMouseLocal, global: mLastMouseGlobal, state: Qt::NoButton, button: Qt::LeftButton, type: QEvent::MouseButtonRelease); |
| 181 | } |
| 182 | |
| 183 | void QWaylandTouchExtension::touch_extension_configure(uint32_t flags) |
| 184 | { |
| 185 | mFlags = flags; |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | QT_END_NAMESPACE |
| 191 | |