GCC Code Coverage Report


Directory: ./
File: server/wayland/resource.h
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 45 47 95.7%
Branches: 6 14 42.9%

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 #pragma once
21
22 #include "client.h"
23 #include "display.h"
24 #include "send.h"
25
26 #include <QObject>
27 #include <cstdint>
28 #include <functional>
29 #include <tuple>
30 #include <wayland-server.h>
31
32 struct wl_client;
33 struct wl_interface;
34 struct wl_resource;
35
36 namespace Wrapland::Server::Wayland
37 {
38
39 class Client;
40
41 template<typename Handle>
42 class Resource
43 {
44 public:
45 Resource(Resource* parent,
46 uint32_t id,
47 wl_interface const* interface,
48 void const* impl,
49 Handle* handle)
50 : Resource(parent->client(), parent->version(), id, interface, impl, handle)
51 {
52 }
53
54 1294 Resource(Server::Client* client,
55 uint32_t version,
56 uint32_t id,
57 wl_interface const* interface,
58 void const* impl,
59 Handle* handle)
60 1294 : Resource(Client::cast_client(client), version, id, interface, impl, handle)
61 {
62 1294 }
63
64 1515 Resource(Client* client,
65 uint32_t version,
66 uint32_t id,
67 wl_interface const* interface,
68 void const* impl,
69 Handle* handle)
70 1515 : client{client}
71 1515 , version{version}
72 1515 , handle{handle}
73 1515 , resource{client->createResource(interface, version, id)}
74 1515 {
75 1515 wl_resource_set_user_data(resource, this);
76 1515 wl_resource_set_implementation(resource, impl, this, destroy);
77 1515 }
78
79 Resource(Resource&) = delete;
80 Resource& operator=(Resource) = delete;
81 Resource(Resource&&) noexcept = delete;
82 Resource& operator=(Resource&&) noexcept = delete;
83
84 1515 virtual ~Resource() = default;
85
86 uint32_t id() const
87 {
88 return resource ? wl_resource_get_id(resource) : 0;
89 }
90
91 19 void flush()
92 {
93 19 client->flush();
94 19 }
95
96 2795 static Handle* get_handle(wl_resource* resource)
97 {
98 2795 return self(resource)->handle;
99 }
100
101 template<auto sender, uint32_t minVersion = 0, typename... Args>
102 2486 void send(Args&&... args)
103 {
104 2486 Wayland::send<sender, minVersion>(resource, version, args...);
105 2486 }
106
107 // We only support std::tuple but since it's just internal API that should be well enough.
108 template<auto sender, uint32_t minVersion = 0, typename... Args>
109 void send(std::tuple<Args...>&& tuple)
110 {
111 Wayland::send_tuple<sender, minVersion>(
112 resource, version, std::forward<decltype(tuple)>(tuple));
113 }
114
115 17 void postError(uint32_t code, char const* msg, ...)
116 {
117 va_list args;
118 17 va_start(args, msg);
119 17 wl_resource_post_error(resource, code, msg, args);
120 17 va_end(args);
121 17 }
122
123 226 static void destroyCallback([[maybe_unused]] wl_client* client, wl_resource* wlResource)
124 {
125 226 auto res = self(wlResource);
126 226 wl_resource_destroy(res->resource);
127 226 }
128
129 4 void serverSideDestroy()
130 {
131 4 wl_resource_set_destructor(resource, nullptr);
132 4 wl_resource_destroy(resource);
133 4 }
134
135 Client* client;
136 uint32_t version;
137 Handle* handle;
138 wl_resource* resource;
139
140 private:
141 4532 static auto self(wl_resource* resource)
142 {
143 4532 return static_cast<Resource<Handle>*>(wl_resource_get_user_data(resource));
144 }
145
146 1511 static void destroy(wl_resource* wlResource)
147 {
148 1511 auto resource = self(wlResource);
149
150 1511 resource->onDestroy();
151
3/6
✓ Branch 0 taken 1461 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
1511 delete resource;
152 1511 }
153
154 1511 void onDestroy()
155 {
156 1511 Q_EMIT handle->resourceDestroyed();
157
3/6
✓ Branch 0 taken 1461 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
1511 delete handle;
158 1511 }
159 };
160
161 }
162