| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qwaylandtouch.h" |
| 5 | #include "qwaylandtouch_p.h" |
| 6 | |
| 7 | #include <QtWaylandCompositor/QWaylandCompositor> |
| 8 | #include <QtWaylandCompositor/QWaylandSeat> |
| 9 | #include <QtWaylandCompositor/QWaylandView> |
| 10 | #include <QtWaylandCompositor/QWaylandClient> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QWaylandTouchPrivate::QWaylandTouchPrivate(QWaylandTouch *touch, QWaylandSeat *seat) |
| 15 | : seat(seat) |
| 16 | { |
| 17 | Q_UNUSED(touch); |
| 18 | } |
| 19 | |
| 20 | void QWaylandTouchPrivate::touch_release(Resource *resource) |
| 21 | { |
| 22 | wl_resource_destroy(resource->handle); |
| 23 | } |
| 24 | |
| 25 | uint QWaylandTouchPrivate::sendDown(QWaylandSurface *surface, uint32_t time, int touch_id, const QPointF &position) |
| 26 | { |
| 27 | Q_Q(QWaylandTouch); |
| 28 | auto focusResource = resourceMap().value(surface->client()->client()); |
| 29 | if (!focusResource) |
| 30 | return 0; |
| 31 | |
| 32 | uint32_t serial = q->compositor()->nextSerial(); |
| 33 | |
| 34 | wl_touch_send_down(focusResource->handle, serial, time, surface->resource(), touch_id, |
| 35 | wl_fixed_from_double(position.x()), wl_fixed_from_double(position.y())); |
| 36 | return serial; |
| 37 | } |
| 38 | |
| 39 | uint QWaylandTouchPrivate::sendUp(QWaylandClient *client, uint32_t time, int touch_id) |
| 40 | { |
| 41 | auto focusResource = resourceMap().value(client->client()); |
| 42 | |
| 43 | if (!focusResource) |
| 44 | return 0; |
| 45 | |
| 46 | uint32_t serial = compositor()->nextSerial(); |
| 47 | |
| 48 | wl_touch_send_up(focusResource->handle, serial, time, touch_id); |
| 49 | return serial; |
| 50 | } |
| 51 | |
| 52 | void QWaylandTouchPrivate::sendMotion(QWaylandClient *client, uint32_t time, int touch_id, const QPointF &position) |
| 53 | { |
| 54 | auto focusResource = resourceMap().value(client->client()); |
| 55 | |
| 56 | if (!focusResource) |
| 57 | return; |
| 58 | |
| 59 | wl_touch_send_motion(focusResource->handle, time, touch_id, |
| 60 | wl_fixed_from_double(position.x()), wl_fixed_from_double(position.y())); |
| 61 | } |
| 62 | |
| 63 | int QWaylandTouchPrivate::toSequentialWaylandId(int touchId) |
| 64 | { |
| 65 | const int waylandId = ids.indexOf(touchId); |
| 66 | if (waylandId != -1) |
| 67 | return waylandId; |
| 68 | const int availableId = ids.indexOf(-1); |
| 69 | if (availableId != -1) { |
| 70 | ids[availableId] = touchId; |
| 71 | return availableId; |
| 72 | } |
| 73 | ids.append(touchId); |
| 74 | return ids.size() - 1; |
| 75 | } |
| 76 | |
| 77 | /*! |
| 78 | * \class QWaylandTouch |
| 79 | * \inmodule QtWaylandCompositor |
| 80 | * \since 5.8 |
| 81 | * \brief The QWaylandTouch class provides access to a touch device. |
| 82 | * |
| 83 | * This class provides access to the touch device in a QWaylandSeat. It corresponds to |
| 84 | * the Wayland interface wl_touch. |
| 85 | */ |
| 86 | |
| 87 | /*! |
| 88 | * Constructs a QWaylandTouch for the \a seat and with the given \a parent. |
| 89 | */ |
| 90 | QWaylandTouch::QWaylandTouch(QWaylandSeat *seat, QObject *parent) |
| 91 | : QWaylandObject(*new QWaylandTouchPrivate(this, seat), parent) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | /*! |
| 96 | * Returns the input device for this QWaylandTouch. |
| 97 | */ |
| 98 | QWaylandSeat *QWaylandTouch::seat() const |
| 99 | { |
| 100 | Q_D(const QWaylandTouch); |
| 101 | return d->seat; |
| 102 | } |
| 103 | |
| 104 | /*! |
| 105 | * Returns the compositor for this QWaylandTouch. |
| 106 | */ |
| 107 | QWaylandCompositor *QWaylandTouch::compositor() const |
| 108 | { |
| 109 | Q_D(const QWaylandTouch); |
| 110 | return d->compositor(); |
| 111 | } |
| 112 | |
| 113 | /*! |
| 114 | * Sends a touch point event to the touch device of \a surface with the given \a id, |
| 115 | * \a position, and \a state. |
| 116 | * |
| 117 | * Returns the serial of the down or up event if sent, otherwise 0. |
| 118 | */ |
| 119 | uint QWaylandTouch::sendTouchPointEvent(QWaylandSurface *surface, int id, const QPointF &position, Qt::TouchPointState state) |
| 120 | { |
| 121 | Q_D(QWaylandTouch); |
| 122 | uint32_t time = compositor()->currentTimeMsecs(); |
| 123 | uint serial = 0; |
| 124 | switch (state) { |
| 125 | case Qt::TouchPointPressed: |
| 126 | serial = d->sendDown(surface, time, touch_id: id, position); |
| 127 | break; |
| 128 | case Qt::TouchPointMoved: |
| 129 | d->sendMotion(client: surface->client(), time, touch_id: id, position); |
| 130 | break; |
| 131 | case Qt::TouchPointReleased: |
| 132 | serial = d->sendUp(client: surface->client(), time, touch_id: id); |
| 133 | break; |
| 134 | case Qt::TouchPointStationary: |
| 135 | // stationary points are not sent through wayland, the client must cache them |
| 136 | break; |
| 137 | case Qt::TouchPointUnknownState: |
| 138 | // Ignored |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | return serial; |
| 143 | } |
| 144 | |
| 145 | /*! |
| 146 | * Sends a touch frame event to the touch device of a \a client. This indicates the end of a |
| 147 | * contact point list. |
| 148 | */ |
| 149 | void QWaylandTouch::sendFrameEvent(QWaylandClient *client) |
| 150 | { |
| 151 | Q_D(QWaylandTouch); |
| 152 | auto focusResource = d->resourceMap().value(client->client()); |
| 153 | if (focusResource) |
| 154 | d->send_frame(focusResource->handle); |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | * Sends a touch cancel event to the touch device of a \a client. |
| 159 | */ |
| 160 | void QWaylandTouch::sendCancelEvent(QWaylandClient *client) |
| 161 | { |
| 162 | Q_D(QWaylandTouch); |
| 163 | auto focusResource = d->resourceMap().value(client->client()); |
| 164 | if (focusResource) |
| 165 | d->send_cancel(focusResource->handle); |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | * Sends all touch points in \a event to the specified \a surface, |
| 170 | * followed by a touch frame event. |
| 171 | * |
| 172 | * \sa sendTouchPointEvent(), sendFrameEvent() |
| 173 | */ |
| 174 | void QWaylandTouch::sendFullTouchEvent(QWaylandSurface *surface, QTouchEvent *event) |
| 175 | { |
| 176 | Q_D(QWaylandTouch); |
| 177 | if (event->type() == QEvent::TouchCancel) { |
| 178 | sendCancelEvent(client: surface->client()); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | const QList<QTouchEvent::TouchPoint> points = event->points(); |
| 183 | if (points.isEmpty()) |
| 184 | return; |
| 185 | |
| 186 | const int pointCount = points.size(); |
| 187 | for (int i = 0; i < pointCount; ++i) { |
| 188 | const QTouchEvent::TouchPoint &tp(points.at(i)); |
| 189 | // Convert the local pos in the compositor window to surface-relative. |
| 190 | const int id = d->toSequentialWaylandId(touchId: tp.id()); |
| 191 | sendTouchPointEvent(surface, id, position: tp.position(), state: Qt::TouchPointState(tp.state())); |
| 192 | if (tp.state() == QEventPoint::Released) |
| 193 | d->ids[id] = -1; |
| 194 | } |
| 195 | sendFrameEvent(client: surface->client()); |
| 196 | } |
| 197 | |
| 198 | /*! |
| 199 | * \internal |
| 200 | */ |
| 201 | void QWaylandTouch::addClient(QWaylandClient *client, uint32_t id, uint32_t version) |
| 202 | { |
| 203 | Q_D(QWaylandTouch); |
| 204 | d->add(client->client(), id, qMin<uint32_t>(QtWaylandServer::wl_touch::interfaceVersion(), version)); |
| 205 | } |
| 206 | |
| 207 | QT_END_NAMESPACE |
| 208 | |
| 209 | #include "moc_qwaylandtouch.cpp" |
| 210 | |