forked from elros34/qxcompositor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner.cpp
238 lines (201 loc) · 7.98 KB
/
runner.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
/****************************************************************************
**
** Copyright (C) 2020 Rinigus https://github.com/rinigus
**
** This file is part of Flatpak Runner.
**
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of the copyright holder nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
**
****************************************************************************/
#include <QFileInfo>
#include <QSettings>
#include <QProcessEnvironment>
#include <iostream>
#include <unistd.h>
#include "runner.h"
#include <QDebug>
Runner::Runner(QString program, QStringList flatpak_options, QStringList program_options,
QString wayland_socket, QString dbusaddress, AppSettings &appsettings, QObject *parent):
QObject(parent), m_crashed(false), m_exitCode(0)
{
if (program.isEmpty())
return; // nothing to do
QString fc = "flatpak";
QStringList fo;
QSettings qset;
// run command
fo << "run";
// Wayland
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("WAYLAND_DISPLAY", wayland_socket);
env.insert("FLATPAK_MALIIT_CONTAINER_DBUS", dbusaddress);
if (!env.contains("FLATPAK_GL_DRIVERS"))
env.insert("FLATPAK_GL_DRIVERS", "host");
// drop QMLSCENE_DEVICE="customcontext" as we don't have it in environment
if (env.value("QMLSCENE_DEVICE") == "customcontext")
env.remove("QMLSCENE_DEVICE");
// dpi and scaling factor
fo << QStringLiteral("--env=QT_WAYLAND_FORCE_DPI=%1").arg(appsettings.appDpi(program, true));
{
int scale = appsettings.appScaling(program, true);
if (scale > 1)
fo << QString("--env=QT_SCALE_FACTOR=%1").arg(scale);
}
// filesystems
QStringList filesys;
filesys << QLatin1String("/system")
<< QLatin1String("/vendor")
<< QLatin1String("/odm")
<< QLatin1String("/plat_property_contexts")
<< QLatin1String("/nonplat_property_contexts")
<< QLatin1String("/apex");
for (const auto &fs: filesys)
if (QFileInfo::exists(fs))
fo << QStringLiteral("--filesystem=%1:ro").arg(fs);
// add support for maliit connection via private dbus
fo << QStringLiteral("--filesystem=/run/user/%1/maliit-server").arg(getuid());
// devices
fo << "--device=all";
// keyboard
fo << "--talk-name=org.maliit.server";
// Set environment variables
QMap<QString,QString> fpkenv = appsettings.appEnv(program, true);
for (auto i=fpkenv.constBegin(); i!=fpkenv.constEnd(); ++i)
fo << "--env=" + i.key() + "=" + i.value();
// // libhybris
// fo << "--env=HYBRIS_EGLPLATFORM_DIR=/var/run/host/usr/lib/libhybris"
// << "--env=HYBRIS_LINKER_DIR=/var/run/host/usr/lib/libhybris/linker";
// // check if we have HYBRIS_LD_LIBRARY_PATH defined
// QStringList ldlibs = env.value("HYBRIS_LD_LIBRARY_PATH",
// "/usr/libexec/droid-hybris/system/lib:/vendor/lib:/system/lib").split(':');
// QStringList ldlibs_processed;
// for (QString &s: ldlibs)
// {
// if (s.startsWith("/usr"))
// ldlibs_processed.append("/var/run/host" + s);
// else
// ldlibs_processed.append(s);
// }
// fo << "--env=HYBRIS_LD_LIBRARY_PATH=" + ldlibs_processed.join(':');
// // set LD_LIBRARY_PATH to the used extension
// if (qset.value("main/set_ld_path", 1).toInt() > 0)
// fo << "--env=LD_LIBRARY_PATH=" + qset.value("main/ld_library_path", "/usr/lib/arm-linux-gnueabihf/GL/host/lib").toString();
// libhybris
QLatin1String extPath(
#ifdef AARCH64
"/usr/lib/aarch64-linux-gnu/GL/host"
#else
"/usr/lib/arm-linux-gnueabihf/GL/host"
#endif
);
QLatin1String libStr(
#ifdef AARCH64
"lib64"
#else
"lib"
#endif
);
fo << QStringLiteral("--env=HYBRIS_EGLPLATFORM_DIR=%1/%2/libhybris").arg(extPath).arg(libStr)
<< QStringLiteral("--env=HYBRIS_LINKER_DIR=%1/%2/libhybris/linker").arg(extPath).arg(libStr);
// check if we have HYBRIS_LD_LIBRARY_PATH defined
QStringList ldlibs = env.value("HYBRIS_LD_LIBRARY_PATH",
QStringLiteral("/usr/libexec/droid-hybris") +
QStringLiteral("/system/lib:/vendor/lib:/system/lib").replace(QLatin1String("lib"), libStr)).split(':');
QStringList ldlibs_processed;
ldlibs_processed << (extPath + QLatin1String("/libexec/droid-hybris/system/") + libStr)
<< ldlibs;
fo << "--env=HYBRIS_LD_LIBRARY_PATH=" + ldlibs_processed.join(':');
// set LD_LIBRARY_PATH to the used extension
if (qset.value("main/set_ld_path",
#ifdef AARCH64
0
#else
1
#endif
).toInt() > 0)
fo << "--env=LD_LIBRARY_PATH=" + qset.value("main/ld_library_path",
extPath + QLatin1String("/") + libStr).toString();
// add supplied flatpak options in the end
fo << flatpak_options;
// add program and its options
fo << program << program_options;
// debug
#if 1
std::cout << "WAYLAND_DISPLAY=\"" << wayland_socket.toStdString() << "\" "
<< "FLATPAK_MALIIT_CONTAINER_DBUS=\"" << dbusaddress.toStdString() << "\" "
<< fc.toStdString() << " " << fo.join(' ').toStdString() << "\n";
#endif
// start application
m_process = new QProcess(this);
m_process->setProcessEnvironment(env);
connect(m_process, &QProcess::errorOccurred, this, &Runner::onError);
connect(m_process, &QProcess::readyReadStandardError, this, &Runner::onStdErr);
connect(m_process, &QProcess::readyReadStandardOutput, this, &Runner::onStdOut);
connect(m_process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
this, &Runner::onFinished);
m_command = fc;
m_options = fo;
m_flatpak_program = program;
emit programChanged(m_flatpak_program);
}
void Runner::start()
{
if (!m_process) return;
m_process->start(m_command, m_options);
}
void Runner::onError(QProcess::ProcessError /*error*/)
{
std::cerr << m_process->errorString().toStdString() << "\n";
}
void Runner::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
onStdErr();
onStdOut();
bool c = (exitStatus == QProcess::CrashExit);
if (m_crashed != c)
{
m_crashed = c;
emit crashedChanged(m_crashed);
}
if (m_exitCode != exitCode)
{
m_exitCode = exitCode;
emit exitCodeChanged(m_exitCode);
}
emit exit();
}
void Runner::onStdErr()
{
std::cerr << m_process->readAllStandardError().constData() << std::flush;
}
void Runner::onStdOut()
{
std::cout << m_process->readAllStandardOutput().constData() << std::flush;
}