summaryrefslogtreecommitdiff
path: root/src/client/qwaylandpointergestures.cpp
blob: df43c31e907a7a5a53748a6d359b6bd632563b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qwaylandpointergestures_p.h"
#include "qwaylanddisplay_p.h"
#include "qwaylandinputdevice_p.h"

QT_BEGIN_NAMESPACE

namespace QtWaylandClient {

QWaylandPointerGestures::QWaylandPointerGestures(QWaylandDisplay *display, uint id, uint version)
    : zwp_pointer_gestures_v1(display->wl_registry(), id, qMin(version, uint(1)))
{
}

QWaylandPointerGestureSwipe *
    QWaylandPointerGestures::createPointerGestureSwipe(QWaylandInputDevice *device)
{
    return new QWaylandPointerGestureSwipe(device);
}

QWaylandPointerGesturePinch *
    QWaylandPointerGestures::createPointerGesturePinch(QWaylandInputDevice *device)
{
    return new QWaylandPointerGesturePinch(device);
}

QWaylandPointerGestureSwipe::QWaylandPointerGestureSwipe(QWaylandInputDevice *p)
    : mParent(p)
{
}

QWaylandPointerGestureSwipe::~QWaylandPointerGestureSwipe()
{
    destroy();
}

void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_begin(uint32_t serial, uint32_t time,
                                                                     struct ::wl_surface *surface,
                                                                     uint32_t fingers)
{
#ifndef QT_NO_GESTURES
    mParent->mSerial = serial;
    mFocus = QWaylandWindow::fromWlSurface(surface);
    mFingers = fingers;

    const auto* pointer = mParent->pointer();

    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_begin @ "
                               << pointer->mSurfacePos << "fingers" << fingers;

    auto e = QWaylandPointerGestureSwipeEvent(mFocus, Qt::GestureStarted, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
                                              QPointF());

    mFocus->handleSwipeGesture(mParent, e);
#endif
}

void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_update(uint32_t time,
                                                                      wl_fixed_t dx, wl_fixed_t dy)
{
#ifndef QT_NO_GESTURES
    const auto* pointer = mParent->pointer();

    const QPointF delta = QPointF(wl_fixed_to_double(dx), wl_fixed_to_double(dy));
    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_update @ "
                               << pointer->mSurfacePos << "delta" << delta;

    auto e = QWaylandPointerGestureSwipeEvent(mFocus, Qt::GestureUpdated, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers, delta);

    mFocus->handleSwipeGesture(mParent, e);
#endif
}

void QWaylandPointerGestureSwipe::zwp_pointer_gesture_swipe_v1_end(uint32_t serial, uint32_t time,
                                                                   int32_t cancelled)
{
#ifndef QT_NO_GESTURES
    mParent->mSerial = serial;
    const auto* pointer = mParent->pointer();

    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_end @ "
                               << pointer->mSurfacePos << (cancelled ? "CANCELED" : "");

    auto gestureType = cancelled ? Qt::GestureFinished : Qt::GestureCanceled;

    auto e = QWaylandPointerGestureSwipeEvent(mFocus, gestureType, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
                                              QPointF());

    mFocus->handleSwipeGesture(mParent, e);

    mFocus.clear();
    mFingers = 0;
#endif
}

QWaylandPointerGesturePinch::QWaylandPointerGesturePinch(QWaylandInputDevice *p)
    : mParent(p)
{
}

QWaylandPointerGesturePinch::~QWaylandPointerGesturePinch()
{
    destroy();
}

void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_begin(uint32_t serial, uint32_t time,
                                                                     struct ::wl_surface *surface,
                                                                     uint32_t fingers)
{
#ifndef QT_NO_GESTURES
    mParent->mSerial = serial;
    mFocus = QWaylandWindow::fromWlSurface(surface);
    mFingers = fingers;
    mLastScale = 1;

    const auto* pointer = mParent->pointer();

    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_pinch_v1_begin @ "
                               << pointer->mSurfacePos << "fingers" << fingers;

    auto e = QWaylandPointerGesturePinchEvent(mFocus, Qt::GestureStarted, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
                                              QPointF(), 0, 0);

    mFocus->handlePinchGesture(mParent, e);
#endif
}

void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_update(uint32_t time,
                                                                      wl_fixed_t dx, wl_fixed_t dy,
                                                                      wl_fixed_t scale,
                                                                      wl_fixed_t rotation)
{
#ifndef QT_NO_GESTURES
    const auto* pointer = mParent->pointer();

    const qreal rscale = wl_fixed_to_double(scale);
    const qreal rot = wl_fixed_to_double(rotation);
    const QPointF delta = QPointF(wl_fixed_to_double(dx), wl_fixed_to_double(dy));
    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_pinch_v1_update @ "
                               << pointer->mSurfacePos << "delta" << delta
                               << "scale" << mLastScale << "->" << rscale
                               << "delta" << rscale - mLastScale << "rot" << rot;

    auto e = QWaylandPointerGesturePinchEvent(mFocus, Qt::GestureUpdated, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
                                              delta, rscale - mLastScale, rot);

    mFocus->handlePinchGesture(mParent, e);

    mLastScale = rscale;
#endif
}

void QWaylandPointerGesturePinch::zwp_pointer_gesture_pinch_v1_end(uint32_t serial, uint32_t time,
                                                                   int32_t cancelled)
{
#ifndef QT_NO_GESTURES
    mParent->mSerial = serial;
    const auto* pointer = mParent->pointer();

    qCDebug(lcQpaWaylandInput) << "zwp_pointer_gesture_swipe_v1_end @ "
                               << pointer->mSurfacePos << (cancelled ? "CANCELED" : "");

    auto gestureType = cancelled ? Qt::GestureFinished : Qt::GestureCanceled;

    auto e = QWaylandPointerGesturePinchEvent(mFocus, gestureType, time,
                                              pointer->mSurfacePos, pointer->mGlobalPos, mFingers,
                                              QPointF(), 0, 0);

    mFocus->handlePinchGesture(mParent, e);

    mFocus.clear();
    mFingers = 0;
    mLastScale = 1;
#endif
}

} // namespace QtWaylandClient

QT_END_NAMESPACE