-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplay.cpp
265 lines (213 loc) · 6.48 KB
/
display.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/********************************************************************
Copyright © 2020 Roman Gilg <subdiff@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include <QObject>
#include <QAbstractEventDispatcher>
#include <QSocketNotifier>
#include <QThread>
#include <QTimer>
#include "logging.h"
#include "display.h"
#include "buffer_manager.h"
#include "client.h"
#include "nucleus.h"
#include "utils.h"
#include "../client_p.h"
#include "../display.h"
#include <algorithm>
#include <exception>
#include <wayland-server.h>
namespace Wrapland::Server::Wayland
{
Display* Display::backendCast(Server::Display* display)
{
return display->d_ptr.get();
}
Display::Display(Server::Display* handle)
: handle{handle}
, m_bufferManager{std::make_unique<BufferManager>()}
{
}
Display::~Display()
{
for (auto stale_global : m_stale_globals) {
delete stale_global;
}
terminate();
if (m_display) {
wl_display_destroy(m_display);
}
}
wl_display* Display::native() const
{
return m_display;
}
bool Display::running() const
{
return m_running;
}
void Display::setRunning(bool running)
{
Q_ASSERT(m_running != running);
m_running = running;
}
void Display::addGlobal(BasicNucleus* nucleus)
{
m_globals.push_back(nucleus);
}
void Display::removeGlobal(BasicNucleus* nucleus)
{
m_globals.erase(std::remove(m_globals.begin(), m_globals.end(), nucleus), m_globals.end());
m_stale_globals.push_back(nucleus);
// A single-shot QTimer is cleaned up by Qt internally when the receiver (here handle) goes
// away what clang-tidy does not understand.
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
QTimer::singleShot(s_global_stale_time, handle, [this, nucleus] {
delete nucleus;
m_stale_globals.erase(std::remove(m_stale_globals.begin(), m_stale_globals.end(), nucleus),
m_stale_globals.end());
});
}
void Display::addSocket()
{
if (!socket_name.empty()) {
if (wl_display_add_socket(m_display, socket_name.c_str()) != 0) {
throw std::bad_exception();
}
return;
}
socket_name = wl_display_add_socket_auto(m_display);
if (socket_name.empty()) {
throw std::bad_exception();
}
}
void Display::start()
{
Q_ASSERT(!m_running);
if (!m_display) {
m_display = wl_display_create();
}
try {
addSocket();
} catch (std::bad_exception&) {
qCWarning(WRAPLAND_SERVER, "Failed to create Wayland socket");
// TODO(romangg): Shall we rethrow?
return;
}
m_loop = wl_display_get_event_loop(m_display);
installSocketNotifier(handle);
}
void Display::startLoop()
{
Q_ASSERT(!running());
Q_ASSERT(native());
installSocketNotifier(handle);
}
void Display::terminate()
{
if (!m_running) {
return;
}
// That call is not really necessary because we run our own Qt-embedded event loop and do not
// call wl_display_run() in the beginning. That being said leave it in here as a reminder that
// there is this possibility.
wl_display_terminate(m_display);
// Then we destroy all remaining clients. There might be clients that have established
// a connection but not yet interacted with us in any way.
wl_display_destroy_clients(m_display);
for (auto nucleus : m_globals) {
nucleus->release();
}
wl_display_destroy(m_display);
m_display = nullptr;
m_loop = nullptr;
setRunning(false);
}
void Display::installSocketNotifier(QObject* parent)
{
if (!QThread::currentThread()) {
return;
}
auto const fd = wl_event_loop_get_fd(m_loop);
if (fd == -1) {
qCWarning(WRAPLAND_SERVER, "Did not get the file descriptor for the event loop");
return;
}
auto* m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, parent);
QObject::connect(m_notifier, &QSocketNotifier::activated, parent, [this] { dispatch(); });
QObject::connect(QThread::currentThread()->eventDispatcher(),
&QAbstractEventDispatcher::aboutToBlock,
parent,
[this] { flush(); });
setRunning(true);
}
void Display::flush()
{
if (!m_display || !m_loop) {
return;
}
wl_display_flush_clients(m_display);
}
void Display::dispatchEvents(int msecTimeout)
{
Q_ASSERT(m_display);
if (m_running) {
dispatch();
} else if (m_loop) {
wl_event_loop_dispatch(m_loop, msecTimeout);
wl_display_flush_clients(m_display);
}
}
void Display::dispatch()
{
if (!m_display || !m_loop) {
return;
}
if (wl_event_loop_dispatch(m_loop, 0) != 0) {
qCWarning(WRAPLAND_SERVER, "Error on dispatching Wayland event loop");
}
}
Client* Display::getClient(wl_client* wlClient)
{
Q_ASSERT(wlClient);
auto it = std::find_if(m_clients.cbegin(), m_clients.cend(), [wlClient](Client* client) {
return client->native == wlClient;
});
if (it != m_clients.cend()) {
return *it;
}
return nullptr;
}
Server::Client* Display::createClientHandle(wl_client* wlClient)
{
auto priv_cl = Client::create_client(wlClient, this);
m_clients.push_back(priv_cl);
QObject::connect(priv_cl->handle, &Server::Client::disconnected, handle, [this](auto client) {
remove_all_if(m_clients,
[client](auto&& candidate) { return candidate->handle == client; });
Q_EMIT handle->clientDisconnected(client);
});
return priv_cl->handle;
}
std::vector<Client*> const& Display::clients() const
{
return m_clients;
}
BufferManager* Display::bufferManager() const
{
return m_bufferManager.get();
}
}