1// Copyright (C) 2021 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 "qwaylandpointergestures_p.h"
5#include "qwaylanddisplay_p.h"
6#include "qwaylandinputdevice_p.h"
7
8QT_BEGIN_NAMESPACE
9
10namespace QtWaylandClient {
11
12QWaylandPointerGestures::QWaylandPointerGestures(QWaylandDisplay *display, uint id, uint version)
13 : zwp_pointer_gestures_v1(display->wl_registry(), id, qMin(version, uint(1)))
14{
15}
16
17QWaylandPointerGestureSwipe *
18 QWaylandPointerGestures::createPointerGestureSwipe(QWaylandInputDevice *device)
19{
20 return new QWaylandPointerGestureSwipe(device);
21}
22
23QWaylandPointerGesturePinch *
24 QWaylandPointerGestures::createPointerGesturePinch(QWaylandInputDevice *device)
25{
26 return new QWaylandPointerGesturePinch(device);
27}
28
29QWaylandPointerGestureSwipe::QWaylandPointerGestureSwipe(QWaylandInputDevice *p)
30 : mParent(p)
31{
32}
33
34QWaylandPointerGestureSwipe::~QWaylandPointerGestureSwipe()
35{
36 destroy();
37}
38
39void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_begin(uint32_t serial, uint32_t time,
40 struct ::wl_surface *surface,
41 uint32_t fingers)
42{
43#ifndef QT_NO_GESTURES
44 mFocus = QWaylandWindow::fromWlSurface(surface);
45 if (!mFocus) {
46 return;
47 }
48 mParent->mSerial = serial;
49 mFingers = fingers;
50
51 const auto* pointer = mParent->pointer();
52
53 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_begin @ "
54 << pointer->mSurfacePos << "fingers" << fingers;
55
56 auto e = QWaylandPointerGestureSwipeEvent(mFocus, Qt::GestureStarted, time,
57 pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
58 QPointF());
59
60 mFocus->handleSwipeGesture(inputDevice: mParent, e);
61#endif
62}
63
64void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_update(uint32_t time,
65 wl_fixed_t dx, wl_fixed_t dy)
66{
67#ifndef QT_NO_GESTURES
68 if (!mFocus) {
69 return;
70 }
71 const auto* pointer = mParent->pointer();
72
73 const QPointF delta = QPointF(wl_fixed_to_double(dx), wl_fixed_to_double(dy));
74 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_update @ "
75 << pointer->mSurfacePos << "delta" << delta;
76
77 auto e = QWaylandPointerGestureSwipeEvent(mFocus, Qt::GestureUpdated, time,
78 pointer->mSurfacePos, pointer->mGlobalPos, mFingers, delta);
79
80 mFocus->handleSwipeGesture(inputDevice: mParent, e: e);
81#endif
82}
83
84void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_end(uint32_t serial, uint32_t time,
85 int32_t cancelled)
86{
87#ifndef QT_NO_GESTURES
88 if (!mFocus) {
89 return;
90 }
91 mParent->mSerial = serial;
92 const auto* pointer = mParent->pointer();
93
94 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_end @ "
95 << pointer->mSurfacePos << (cancelled ? "CANCELED" : "");
96
97 auto gestureType = cancelled ? Qt::GestureFinished : Qt::GestureCanceled;
98
99 auto e = QWaylandPointerGestureSwipeEvent(mFocus, gestureType, time,
100 pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
101 QPointF());
102
103 mFocus->handleSwipeGesture(inputDevice: mParent, e);
104
105 mFocus.clear();
106 mFingers = 0;
107#endif
108}
109
110QWaylandPointerGesturePinch::QWaylandPointerGesturePinch(QWaylandInputDevice *p)
111 : mParent(p)
112{
113}
114
115QWaylandPointerGesturePinch::~QWaylandPointerGesturePinch()
116{
117 destroy();
118}
119
120void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_begin(uint32_t serial, uint32_t time,
121 struct ::wl_surface *surface,
122 uint32_t fingers)
123{
124#ifndef QT_NO_GESTURES
125 mFocus = QWaylandWindow::fromWlSurface(surface);
126 if (!mFocus) {
127 return;
128 }
129 mParent->mSerial = serial;
130 mFingers = fingers;
131 mLastScale = 1;
132 const auto* pointer = mParent->pointer();
133
134 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_pinch_v1_begin @ "
135 << pointer->mSurfacePos << "fingers" << fingers;
136
137 auto e = QWaylandPointerGesturePinchEvent(mFocus, Qt::GestureStarted, time,
138 pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
139 QPointF(), 0, 0);
140
141 mFocus->handlePinchGesture(inputDevice: mParent, e);
142#endif
143}
144
145void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_update(uint32_t time,
146 wl_fixed_t dx, wl_fixed_t dy,
147 wl_fixed_t scale,
148 wl_fixed_t rotation)
149{
150#ifndef QT_NO_GESTURES
151 if (!mFocus) {
152 return;
153 }
154 const auto* pointer = mParent->pointer();
155
156 const qreal rscale = wl_fixed_to_double(scale);
157 const qreal rot = wl_fixed_to_double(rotation);
158 const QPointF delta = QPointF(wl_fixed_to_double(dx), wl_fixed_to_double(dy));
159 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_pinch_v1_update @ "
160 << pointer->mSurfacePos << "delta" << delta
161 << "scale" << mLastScale << "->" << rscale
162 << "delta" << rscale - mLastScale << "rot" << rot;
163
164 auto e = QWaylandPointerGesturePinchEvent(mFocus, Qt::GestureUpdated, time,
165 pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
166 delta, rscale - mLastScale, rot);
167
168 mFocus->handlePinchGesture(inputDevice: mParent, e: e);
169
170 mLastScale = rscale;
171#endif
172}
173
174void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_end(uint32_t serial, uint32_t time,
175 int32_t cancelled)
176{
177#ifndef QT_NO_GESTURES
178 if (!mFocus) {
179 return;
180 }
181 mParent->mSerial = serial;
182 const auto* pointer = mParent->pointer();
183
184 qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_end @ "
185 << pointer->mSurfacePos << (cancelled ? "CANCELED" : "");
186
187 auto gestureType = cancelled ? Qt::GestureFinished : Qt::GestureCanceled;
188
189 auto e = QWaylandPointerGesturePinchEvent(mFocus, gestureType, time,
190 pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
191 QPointF(), 0, 0);
192
193 mFocus->handlePinchGesture(inputDevice: mParent, e);
194
195 mFocus.clear();
196 mFingers = 0;
197 mLastScale = 1;
198#endif
199}
200
201} // namespace QtWaylandClient
202
203QT_END_NAMESPACE
204

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