GCC Code Coverage Report


Directory: ./
File: src/client/pointer.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 113 122 92.6%
Branches: 23 42 54.8%

Line Branch Exec Source
1 /********************************************************************
2 Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "pointer.h"
21 #include "surface.h"
22 #include "wayland_pointer_p.h"
23 // Qt
24 #include <QPointF>
25 #include <QPointer>
26 // wayland
27 #include <wayland-client-protocol.h>
28
29 namespace Wrapland
30 {
31 namespace Client
32 {
33
34 9 static Pointer::Axis wlAxisToPointerAxis(uint32_t axis)
35 {
36
2/3
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
9 switch (axis) {
37 case WL_POINTER_AXIS_VERTICAL_SCROLL:
38 4 return Pointer::Axis::Vertical;
39 case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
40 5 return Pointer::Axis::Horizontal;
41 }
42
43 Q_UNREACHABLE();
44 9 }
45
46 class Q_DECL_HIDDEN Pointer::Private
47 {
48 public:
49 Private(Pointer* q);
50 void setup(wl_pointer* p);
51
52 WaylandPointer<wl_pointer, wl_pointer_release> pointer;
53 QPointer<Surface> enteredSurface;
54 65 quint32 enteredSerial = 0;
55
56 private:
57 void enter(uint32_t serial, wl_surface* surface, QPointF const& relativeToSurface);
58 void leave(uint32_t serial);
59 static void enterCallback(void* data,
60 wl_pointer* pointer,
61 uint32_t serial,
62 wl_surface* surface,
63 wl_fixed_t sx,
64 wl_fixed_t sy);
65 static void
66 leaveCallback(void* data, wl_pointer* pointer, uint32_t serial, wl_surface* surface);
67 static void
68 motionCallback(void* data, wl_pointer* pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy);
69 static void buttonCallback(void* data,
70 wl_pointer* pointer,
71 uint32_t serial,
72 uint32_t time,
73 uint32_t button,
74 uint32_t state);
75 static void
76 axisCallback(void* data, wl_pointer* pointer, uint32_t time, uint32_t axis, wl_fixed_t value);
77 static void frameCallback(void* data, wl_pointer* pointer);
78 static void axisSourceCallback(void* data, wl_pointer* pointer, uint32_t axis_source);
79 static void axisStopCallback(void* data, wl_pointer* pointer, uint32_t time, uint32_t axis);
80 static void
81 axisDiscreteCallback(void* data, wl_pointer* pointer, uint32_t axis, int32_t discrete);
82
83 Pointer* q;
84 static const wl_pointer_listener s_listener;
85 };
86
87 65 Pointer::Private::Private(Pointer* q)
88 65 : q(q)
89 {
90 65 }
91
92 65 void Pointer::Private::setup(wl_pointer* p)
93 {
94
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 Q_ASSERT(p);
95
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 Q_ASSERT(!pointer);
96 65 pointer.setup(p);
97 65 wl_pointer_add_listener(pointer, &s_listener, this);
98 65 }
99
100 const wl_pointer_listener Pointer::Private::s_listener = {
101 enterCallback,
102 leaveCallback,
103 motionCallback,
104 buttonCallback,
105 axisCallback,
106 frameCallback,
107 axisSourceCallback,
108 axisStopCallback,
109 axisDiscreteCallback,
110 };
111
112 65 Pointer::Pointer(QObject* parent)
113 65 : QObject(parent)
114
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
65 , d(new Private(this))
115 65 {
116 65 }
117
118 130 Pointer::~Pointer()
119 130 {
120
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 release();
121 130 }
122
123 90 void Pointer::release()
124 {
125 90 d->pointer.release();
126 90 }
127
128 65 void Pointer::setup(wl_pointer* pointer)
129 {
130 65 d->setup(pointer);
131 65 }
132
133 75 void Pointer::Private::enterCallback(void* data,
134 wl_pointer* pointer,
135 uint32_t serial,
136 wl_surface* surface,
137 wl_fixed_t sx,
138 wl_fixed_t sy)
139 {
140 75 auto p = reinterpret_cast<Pointer::Private*>(data);
141
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 Q_ASSERT(p->pointer == pointer);
142 75 p->enter(serial, surface, QPointF(wl_fixed_to_double(sx), wl_fixed_to_double(sy)));
143 75 }
144
145 75 void Pointer::Private::enter(uint32_t serial, wl_surface* surface, QPointF const& relativeToSurface)
146 {
147 75 enteredSurface = QPointer<Surface>(Surface::get(surface));
148 75 enteredSerial = serial;
149 75 Q_EMIT q->entered(serial, relativeToSurface);
150 75 }
151
152 36 void Pointer::Private::leaveCallback(void* data,
153 wl_pointer* pointer,
154 uint32_t serial,
155 wl_surface* surface)
156 {
157 36 auto p = reinterpret_cast<Pointer::Private*>(data);
158
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 Q_ASSERT(p->pointer == pointer);
159 Q_UNUSED(surface)
160 36 p->leave(serial);
161 36 }
162
163 36 void Pointer::Private::leave(uint32_t serial)
164 {
165 36 enteredSurface.clear();
166 36 Q_EMIT q->left(serial);
167 36 }
168
169 9 void Pointer::Private::motionCallback(void* data,
170 wl_pointer* pointer,
171 uint32_t time,
172 wl_fixed_t sx,
173 wl_fixed_t sy)
174 {
175 9 auto p = reinterpret_cast<Pointer::Private*>(data);
176
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Q_ASSERT(p->pointer == pointer);
177 9 Q_EMIT p->q->motion(QPointF(wl_fixed_to_double(sx), wl_fixed_to_double(sy)), time);
178 9 }
179
180 52 void Pointer::Private::buttonCallback(void* data,
181 wl_pointer* pointer,
182 uint32_t serial,
183 uint32_t time,
184 uint32_t button,
185 uint32_t state)
186 {
187 52 auto p = reinterpret_cast<Pointer::Private*>(data);
188
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 Q_ASSERT(p->pointer == pointer);
189 104 auto toState = [state] {
190
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 28 times.
52 if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
191 24 return ButtonState::Released;
192 } else {
193 28 return ButtonState::Pressed;
194 }
195 52 };
196 52 Q_EMIT p->q->buttonStateChanged(serial, time, button, toState());
197 52 }
198
199 6 void Pointer::Private::axisCallback(void* data,
200 wl_pointer* pointer,
201 uint32_t time,
202 uint32_t axis,
203 wl_fixed_t value)
204 {
205 6 auto p = reinterpret_cast<Pointer::Private*>(data);
206
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Q_ASSERT(p->pointer == pointer);
207 6 Q_EMIT p->q->axisChanged(time, wlAxisToPointerAxis(axis), wl_fixed_to_double(value));
208 6 }
209
210 101 void Pointer::Private::frameCallback(void* data, wl_pointer* pointer)
211 {
212 101 auto p = reinterpret_cast<Pointer::Private*>(data);
213
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 Q_ASSERT(p->pointer == pointer);
214 101 Q_EMIT p->q->frame();
215 101 }
216
217 3 void Pointer::Private::axisSourceCallback(void* data, wl_pointer* pointer, uint32_t axis_source)
218 {
219 3 auto p = reinterpret_cast<Pointer::Private*>(data);
220
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Q_ASSERT(p->pointer == pointer);
221 AxisSource source;
222
2/5
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 switch (axis_source) {
223 case WL_POINTER_AXIS_SOURCE_WHEEL:
224 1 source = AxisSource::Wheel;
225 1 break;
226 case WL_POINTER_AXIS_SOURCE_FINGER:
227 2 source = AxisSource::Finger;
228 2 break;
229 case WL_POINTER_AXIS_SOURCE_CONTINUOUS:
230 source = AxisSource::Continuous;
231 break;
232 case WL_POINTER_AXIS_SOURCE_WHEEL_TILT:
233 source = AxisSource::WheelTilt;
234 break;
235 default:
236 Q_UNREACHABLE();
237 break;
238 }
239 3 Q_EMIT p->q->axisSourceChanged(source);
240 3 }
241
242 1 void Pointer::Private::axisStopCallback(void* data,
243 wl_pointer* pointer,
244 uint32_t time,
245 uint32_t axis)
246 {
247 1 auto p = reinterpret_cast<Pointer::Private*>(data);
248
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 Q_ASSERT(p->pointer == pointer);
249 1 Q_EMIT p->q->axisStopped(time, wlAxisToPointerAxis(axis));
250 1 }
251
252 2 void Pointer::Private::axisDiscreteCallback(void* data,
253 wl_pointer* pointer,
254 uint32_t axis,
255 int32_t discrete)
256 {
257 2 auto p = reinterpret_cast<Pointer::Private*>(data);
258
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Q_ASSERT(p->pointer == pointer);
259 2 Q_EMIT p->q->axisDiscreteChanged(wlAxisToPointerAxis(axis), discrete);
260 2 }
261
262 5 void Pointer::setCursor(Surface* surface, QPoint const& hotspot)
263 {
264
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Q_ASSERT(isValid());
265 5 wl_surface* s = nullptr;
266
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (surface) {
267 2 s = *surface;
268 2 }
269 5 wl_pointer_set_cursor(d->pointer, d->enteredSerial, s, hotspot.x(), hotspot.y());
270 5 }
271
272 1 void Pointer::hideCursor()
273 {
274 1 setCursor(nullptr);
275 1 }
276
277 18 Surface* Pointer::enteredSurface()
278 {
279 18 return d->enteredSurface.data();
280 }
281
282 16 Surface* Pointer::enteredSurface() const
283 {
284 16 return d->enteredSurface.data();
285 }
286
287 47 bool Pointer::isValid() const
288 {
289 47 return d->pointer.isValid();
290 }
291
292 Pointer::operator wl_pointer*() const
293 {
294 return d->pointer;
295 }
296
297 19 Pointer::operator wl_pointer*()
298 {
299 19 return d->pointer;
300 }
301
302 }
303 }
304