GCC Code Coverage Report


Directory: ./
File: server/compositor.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 26 31 83.9%
Branches: 5 14 35.7%

Line Branch Exec Source
1 /********************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
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 "compositor.h"
21
22 #include "client.h"
23 #include "display.h"
24 #include "region.h"
25 #include "surface_p.h"
26
27 #include "wayland/global.h"
28
29 #include <algorithm>
30 #include <vector>
31
32 namespace Wrapland::Server
33 {
34
35 constexpr uint32_t CompositorVersion = 4;
36 using CompositorGlobal = Wayland::Global<Compositor, CompositorVersion>;
37 using CompositorBind = Wayland::Bind<CompositorGlobal>;
38
39 class Compositor::Private : public CompositorGlobal
40 {
41 public:
42 Private(Compositor* q_ptr, Display* display);
43
44 std::vector<Surface*> surfaces;
45
46 private:
47 static void createSurfaceCallback(CompositorBind* bind, uint32_t id);
48 static void createRegionCallback(CompositorBind* bind, uint32_t id);
49
50 static const struct wl_compositor_interface s_interface;
51 };
52
53 934 Compositor::Private::Private(Compositor* q_ptr, Display* display)
54 467 : CompositorGlobal(q_ptr, display, &wl_compositor_interface, &s_interface)
55 467 {
56 467 display->globals.compositor = q_ptr;
57 467 }
58
59 const struct wl_compositor_interface Compositor::Private::s_interface = {
60 cb<createSurfaceCallback>,
61 cb<createRegionCallback>,
62 };
63
64 467 Compositor::Compositor(Display* display)
65
2/4
✓ Branch 0 taken 467 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 467 times.
✗ Branch 3 not taken.
467 : d_ptr(new Private(this, display))
66 467 {
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 467 times.
467 d_ptr->create();
68 467 }
69
70 934 Compositor::~Compositor() = default;
71
72 515 void Compositor::Private::createSurfaceCallback(CompositorBind* bind, uint32_t id)
73 {
74 515 auto priv = bind->global()->handle->d_ptr.get();
75
76
1/2
✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
515 auto surface = new Surface(bind->client->handle, bind->version, id);
77 // TODO(romangg): error handling (when resource not created)
78
79 515 priv->surfaces.push_back(surface);
80 1030 connect(surface, &Surface::resourceDestroyed, priv->handle, [priv, surface] {
81 1030 priv->surfaces.erase(std::remove(priv->surfaces.begin(), priv->surfaces.end(), surface),
82 515 priv->surfaces.end());
83 515 });
84
85 515 Q_EMIT priv->handle->surfaceCreated(surface);
86 515 }
87
88 20 void Compositor::Private::createRegionCallback(CompositorBind* bind, uint32_t id)
89 {
90 20 auto compositor = bind->global()->handle;
91
92
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 auto region = new Region(bind->client->handle, bind->version, id);
93 // TODO(romangg): error handling (when resource not created)
94
95 20 Q_EMIT compositor->regionCreated(region);
96 20 }
97
98 Surface* Compositor::getSurface(uint32_t id, Client* client)
99 {
100 auto it = std::find_if(
101 d_ptr->surfaces.cbegin(), d_ptr->surfaces.cend(), [id, client](Surface* surface) {
102 return surface->d_ptr->client->handle == client && surface->d_ptr->id() == id;
103 });
104 return it != d_ptr->surfaces.cend() ? *it : nullptr;
105 }
106
107 }
108