GCC Code Coverage Report


Directory: ./
File: server/viewporter.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 64 70 91.4%
Branches: 26 48 54.2%

Line Branch Exec Source
1 /********************************************************************
2 Copyright © 2020 Adrien Faveraux <ad1rie3@hotmail.fr>
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
21 #include "display.h"
22 #include "wayland/client.h"
23 #include "wayland/global.h"
24 #include "wayland/resource.h"
25
26 #include "surface.h"
27 #include "surface_p.h"
28 #include "viewporter_p.h"
29
30 #include <wayland-server.h>
31 #include <wayland-viewporter-server-protocol.h>
32
33 namespace Wrapland::Server
34 {
35
36 const struct wp_viewporter_interface Viewporter::Private::s_interface = {
37 resourceDestroyCallback,
38 cb<getViewportCallback>,
39 };
40
41 13 Viewporter::Private::Private(Display* display, Viewporter* q_ptr)
42 13 : ViewporterGlobal(q_ptr, display, &wp_viewporter_interface, &s_interface)
43 13 {
44
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 create();
45 13 }
46
47 14 void Viewporter::Private::getViewportCallback(ViewporterBind* bind,
48 uint32_t id,
49 wl_resource* wlSurface)
50 {
51 14 auto priv = get_handle(bind->resource)->d_ptr.get();
52 14 priv->getViewport(bind, id, wlSurface);
53 14 }
54
55 14 void Viewporter::Private::getViewport(ViewporterBind* bind, uint32_t id, wl_resource* wlSurface)
56 {
57
58 14 auto surface = Wayland::Resource<Surface>::get_handle(wlSurface);
59
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (!surface) {
60 // TODO(romangg): send error msg?
61 return;
62 }
63
64
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (surface->d_ptr->viewport) {
65 // Surface already has a viewport. That's a protocol error.
66 1 bind->post_error(WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS, "Surface already has viewport");
67 1 return;
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 auto viewport = new Viewport(bind->client->handle, bind->version, id, surface);
71
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (!viewport->d_ptr->resource) {
72 bind->post_no_memory();
73 delete viewport;
74 return;
75 }
76
77 13 surface->d_ptr->installViewport(viewport);
78 13 Q_EMIT handle->viewportCreated(viewport);
79 14 }
80
81 13 Viewporter::Viewporter(Display* display)
82
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 : d_ptr(new Private(display, this))
83 13 {
84 13 }
85
86 26 Viewporter::~Viewporter() = default;
87
88 const struct wp_viewport_interface Viewport::Private::s_interface = {
89 destroyCallback,
90 setSourceCallback,
91 setDestinationCallback,
92 };
93
94 13 Viewport::Private::Private(Client* client,
95 uint32_t version,
96 uint32_t id,
97 Surface* _surface,
98 Viewport* q_ptr)
99 13 : Wayland::Resource<Viewport>(client, version, id, &wp_viewport_interface, &s_interface, q_ptr)
100 13 , surface(_surface)
101 13 {
102 13 }
103
104 13 Viewport::Viewport(Client* client, uint32_t version, uint32_t id, Surface* surface, QObject* parent)
105 13 : QObject(parent)
106
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 , d_ptr(new Private(client, version, id, surface, this))
107 13 {
108
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
23 connect(surface, &Surface::resourceDestroyed, this, [this] { d_ptr->surface = nullptr; });
109 13 }
110
111 10 void Viewport::Private::setSourceCallback([[maybe_unused]] wl_client* wlClient,
112 wl_resource* wlResource,
113 wl_fixed_t pos_x,
114 wl_fixed_t pos_y,
115 wl_fixed_t width,
116 wl_fixed_t height)
117 {
118 10 auto priv = get_handle(wlResource)->d_ptr;
119 20 priv->setSource(wl_fixed_to_double(pos_x),
120 10 wl_fixed_to_double(pos_y),
121 10 wl_fixed_to_double(width),
122 10 wl_fixed_to_double(height));
123 10 }
124
125 10 void Viewport::Private::setSource(double pos_x, double pos_y, double width, double height)
126 {
127
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!surface) {
128 postError(WP_VIEWPORT_ERROR_NO_SURFACE, "Viewport without surface");
129 return;
130 }
131
5/8
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 9 times.
10 if (pos_x < 0 || pos_y < 0 || width <= 0 || height <= 0) {
132 2 auto cmp = [](double number) { return !qFuzzyCompare(number, -1.); };
133
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if (cmp(pos_x) || cmp(pos_y) || cmp(width) || cmp(height)) {
134 1 postError(WP_VIEWPORT_ERROR_BAD_VALUE, "Source rectangle not well defined");
135 1 return;
136 }
137 }
138
139 9 Q_EMIT handle->sourceRectangleSet(QRectF(pos_x, pos_y, width, height));
140 10 }
141
142 14 void Viewport::Private::setDestinationCallback([[maybe_unused]] wl_client* wlClient,
143 wl_resource* wlResource,
144 int32_t width,
145 int32_t height)
146 {
147 14 get_handle(wlResource)->d_ptr->setDestination(width, height);
148 14 }
149
150 24 void Viewport::Private::setDestination(int width, int height)
151 {
152
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
24 if (!surface) {
153 1 postError(WP_VIEWPORT_ERROR_NO_SURFACE, "Viewport without surface");
154 1 return;
155 }
156
6/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 11 times.
23 if ((width <= 0 && width != -1) || (height <= 0 && height != -1)) {
157 12 postError(WP_VIEWPORT_ERROR_BAD_VALUE, "Destination size not well defined");
158 12 return;
159 }
160
161 11 Q_EMIT handle->destinationSizeSet(QSize(width, height));
162 14 }
163
164 }
165