-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_wayland.cpp
341 lines (287 loc) · 12.7 KB
/
main_wayland.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
SPDX-FileCopyrightText: 2023 Roman Gilg <subdiff@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "main.h"
#include <como/base/wayland/app_singleton.h>
#include <como/base/wayland/xwl_platform.h>
#include <como/desktop/kde/platform.h>
#include <como/desktop/kde/screen_locker.h>
#include <como/render/shortcuts_init.h>
#include <como/script/platform.h>
#include <como/win/shortcuts_init.h>
#include <KShell>
#include <KSignalHandler>
#include <KUpdateLaunchEnvironmentJob>
#include <QApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QProcess>
#include <sys/resource.h>
namespace theseus_ship
{
template<typename Base>
struct input_mod {
using platform_t = como::input::wayland::platform<Base, input_mod>;
std::unique_ptr<como::input::dbus::device_manager<platform_t>> dbus;
};
struct space_mod {
std::unique_ptr<como::desktop::platform> desktop;
};
struct base_mod {
using platform_t = como::base::wayland::xwl_platform<base_mod>;
using render_t = como::render::wayland::xwl_platform<platform_t>;
using input_t = como::input::wayland::platform<platform_t, input_mod<platform_t>>;
using space_t = como::win::wayland::xwl_space<platform_t, space_mod>;
std::unique_ptr<render_t> render;
std::unique_ptr<input_t> input;
std::unique_ptr<space_t> space;
std::unique_ptr<como::xwl::xwayland<space_t>> xwayland;
std::unique_ptr<como::scripting::platform<space_t>> script;
};
}
static rlimit originalNofileLimit = {
.rlim_cur = 0,
.rlim_max = 0,
};
namespace
{
static void restoreNofileLimit()
{
if (setrlimit(RLIMIT_NOFILE, &originalNofileLimit) == -1) {
std::cerr << "Failed to restore RLIMIT_NOFILE limit, legacy apps might be broken"
<< std::endl;
}
}
static void bumpNofileLimit()
{
// It's easy to exceed the file descriptor limit because many things are backed using fds
// nowadays, e.g. dmabufs, shm buffers, etc. Bump the RLIMIT_NOFILE limit to handle that.
// Some apps may still use select(), so we reset the limit to its original value in fork().
if (getrlimit(RLIMIT_NOFILE, &originalNofileLimit) == -1) {
std::cerr << "Failed to bump RLIMIT_NOFILE limit, getrlimit() failed: " << strerror(errno)
<< std::endl;
return;
}
rlimit limit = originalNofileLimit;
limit.rlim_cur = limit.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
std::cerr << "Failed to bump RLIMIT_NOFILE limit, setrlimit() failed: " << strerror(errno)
<< std::endl;
return;
}
pthread_atfork(nullptr, nullptr, restoreNofileLimit);
}
struct exit_process_t {
exit_process_t(QApplication& app)
: app{&app}
{
}
~exit_process_t()
{
if (process && process->state() != QProcess::NotRunning) {
QObject::disconnect(process, nullptr, app, nullptr);
process->terminate();
process->waitForFinished(5000);
}
}
QApplication* app;
QProcess* process{nullptr};
};
}
int main(int argc, char* argv[])
{
using namespace theseus_ship;
// Redirect stderr output. This is useful as a workaround for missing logs in systemd journal
// when launching a full Plasma session.
if (auto log_path = getenv("KWIN_LOG_PATH")) {
if (!freopen(log_path, "w", stderr)) {
std::cerr << "Failed to open '" << log_path << "' for writing stderr." << std::endl;
return 1;
}
}
if (getuid() == 0) {
std::cerr << "kwin_wayland does not support running as root." << std::endl;
return 1;
}
KLocalizedString::setApplicationDomain("kwin");
bumpNofileLimit();
signal(SIGPIPE, SIG_IGN);
// ensure that no thread takes SIGUSR
sigset_t userSignals;
sigemptyset(&userSignals);
sigaddset(&userSignals, SIGUSR1);
sigaddset(&userSignals, SIGUSR2);
pthread_sigmask(SIG_BLOCK, &userSignals, nullptr);
struct {
QCommandLineOption xwl = {
QStringLiteral("xwayland"),
i18n("Start a rootless Xwayland server."),
};
QCommandLineOption socket = {
QStringList{QStringLiteral("s"), QStringLiteral("socket")},
i18n("Name of the Wayland socket to listen on. If not set \"wayland-0\" is used."),
QStringLiteral("socket"),
};
QCommandLineOption lockscreen = {
QStringLiteral("lockscreen"),
i18n("Starts the session in locked mode."),
};
QCommandLineOption no_lockscreen = {
QStringLiteral("no-lockscreen"),
i18n("Starts the session without lock screen support."),
};
QCommandLineOption no_global_shortcuts = {
QStringLiteral("no-global-shortcuts"),
i18n("Starts the session without global shortcuts support."),
};
QCommandLineOption exit_with_session = {
QStringLiteral("exit-with-session"),
i18n("Exit after the session application, which is started by KWin, closed."),
QStringLiteral("/path/to/session"),
};
} options;
QCommandLineParser parser;
parser.setApplicationDescription(i18n("KWinFT Wayland Window Manager"));
KAboutData::applicationData().setupCommandLine(&parser);
parser.addOption(options.xwl);
parser.addOption(options.socket);
parser.addOption(options.no_lockscreen);
parser.addOption(options.no_global_shortcuts);
parser.addOption(options.lockscreen);
parser.addOption(options.exit_with_session);
parser.addPositionalArgument(QStringLiteral("applications"),
i18n("Applications to start once server is started"),
QStringLiteral("[/path/to/application...]"));
como::base::wayland::app_singleton app(argc, argv);
if (!como::Perf::Ftrace::setEnabled(qEnvironmentVariableIsSet("KWIN_PERF_FTRACE"))) {
qWarning() << "Can't enable Ftrace via environment variable.";
}
KSignalHandler::self()->watchSignal(SIGTERM);
KSignalHandler::self()->watchSignal(SIGINT);
KSignalHandler::self()->watchSignal(SIGHUP);
QObject::connect(KSignalHandler::self(),
&KSignalHandler::signalReceived,
app.qapp.get(),
&QCoreApplication::exit);
app_create_about_data();
parser.process(*app.qapp);
KAboutData::applicationData().processCommandLine(&parser);
auto flags = como::base::wayland::start_options::none;
if (parser.isSet(options.lockscreen)) {
flags = como::base::wayland::start_options::lock_screen;
} else if (!parser.isSet(options.no_lockscreen)) {
flags = como::base::wayland::start_options::lock_screen_integration;
}
if (parser.isSet(options.no_global_shortcuts)) {
flags |= como::base::wayland::start_options::no_global_shortcuts;
}
qDebug("Starting Theseus' Ship (Wayland) %s", "0.0.0");
exit_process_t exit_process(*app.qapp);
using base_t = como::base::wayland::xwl_platform<base_mod>;
base_t base({
.config = como::base::config(KConfig::OpenFlag::FullConfig, "kwinrc"),
.socket_name = parser.value(options.socket).toStdString(),
.flags = flags,
.mode = parser.isSet(options.xwl) ? como::base::operation_mode::xwayland
: como::base::operation_mode::wayland,
});
base.mod.render = std::make_unique<base_t::render_t>(base);
base.mod.input
= std::make_unique<base_t::input_t>(base, como::input::config(KConfig::NoGlobals));
base.mod.input->mod.dbus
= std::make_unique<como::input::dbus::device_manager<base_t::input_t>>(*base.mod.input);
base.mod.space = std::make_unique<base_t::space_t>(*base.mod.render, *base.mod.input);
base.mod.space->mod.desktop
= std::make_unique<como::desktop::kde::platform<base_t::space_t>>(*base.mod.space);
como::win::init_shortcuts(*base.mod.space);
como::render::init_shortcuts(*base.mod.render);
base.mod.script = std::make_unique<como::scripting::platform<base_t::space_t>>(*base.mod.space);
como::base::wayland::platform_start(base);
base.process_environment = QProcessEnvironment::systemEnvironment();
if (auto const& name = base.server->display->socket_name(); !name.empty()) {
base.process_environment.insert(QStringLiteral("WAYLAND_DISPLAY"), name.c_str());
}
base.mod.space->mod.desktop->screen_locker
= std::make_unique<como::desktop::kde::screen_locker>(
*base.server, base.process_environment, parser.isSet(options.lockscreen));
if (base.operation_mode == como::base::operation_mode::xwayland) {
try {
base.mod.xwayland
= std::make_unique<como::xwl::xwayland<base_t::space_t>>(*base.mod.space);
} catch (std::system_error const& exc) {
std::cerr << "FATAL ERROR creating Xwayland: " << exc.what() << std::endl;
exit(exc.code().value());
} catch (std::exception const& exc) {
std::cerr << "FATAL ERROR creating Xwayland: " << exc.what() << std::endl;
exit(1);
}
}
auto process_environment = base.process_environment;
// Enforce Wayland platform for started Qt apps. They otherwise for some reason prefer X11.
process_environment.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("wayland"));
// start session
if (parser.isSet(options.exit_with_session) /*&& !m_sessionArgument.isEmpty()*/) {
auto arguments = KShell::splitArgs(parser.value(options.exit_with_session));
if (!arguments.isEmpty()) {
QString program = arguments.takeFirst();
auto p = new QProcess(app.qapp.get());
p->setProcessChannelMode(QProcess::ForwardedErrorChannel);
p->setProcessEnvironment(process_environment);
QObject::connect(p,
qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
app.qapp.get(),
[&exit_process, p](auto code, auto status) {
exit_process.process = {};
p->deleteLater();
if (status == QProcess::CrashExit) {
qWarning() << "Session process has crashed";
QCoreApplication::exit(-1);
return;
}
if (code) {
qWarning() << "Session process exited with code" << code;
}
QCoreApplication::exit(code);
});
p->setProgram(program);
p->setArguments(arguments);
p->start();
exit_process.process = p;
} else {
qWarning("Failed to launch the session process: %s is an invalid command",
qPrintable(parser.value(options.exit_with_session)));
}
}
// start the applications passed to us as command line arguments
if (auto apps = parser.positionalArguments(); !apps.isEmpty()) {
for (auto const& app_name : qAsConst(apps)) {
auto arguments = KShell::splitArgs(app_name);
if (arguments.isEmpty()) {
qWarning("Failed to launch application: %s is an invalid command",
qPrintable(app_name));
continue;
}
QString program = arguments.takeFirst();
// note: this will kill the started process when we exit
// this is going to happen anyway as we are the wayland and X server the app connects to
auto p = new QProcess(app.qapp.get());
p->setProcessChannelMode(QProcess::ForwardedErrorChannel);
p->setProcessEnvironment(process_environment);
p->setProgram(program);
p->setArguments(arguments);
p->startDetached();
p->deleteLater();
}
}
// Need to create a launch environment job for Plasma components to catch up in a systemd boot.
// This implies we're running in a full Plasma session i.e. when we use the wrapper (that's
// there the service name comes from), but we can also do it in a plain setup without session.
// Registering the service names indicates that we're live and all env vars are exported.
auto env_sync_job = new KUpdateLaunchEnvironmentJob(process_environment);
QObject::connect(env_sync_job, &KUpdateLaunchEnvironmentJob::finished, app.qapp.get(), []() {
QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.KWinWrapper"));
});
return app.qapp->exec();
}