Skip to content

Commit b9ca9fa

Browse files
committed
Run shell on the Flatpak host
When Terminal is running on Flatpak we need to access the host system, otherwise the app is pretty useless. Closes: #36
1 parent 7d7d7a5 commit b9ca9fa

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

src/lib/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ add_library(qmltermwidget STATIC
4343
kptydevice.h
4444
kptyprocess.cpp
4545
kptyprocess.h
46+
utils.cpp
47+
utils.h
4648
)
4749
target_include_directories(qmltermwidget
4850
PUBLIC

src/lib/Session.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "TerminalDisplay.h"
4545
#include "ShellCommand.h"
4646
#include "Vt102Emulation.h"
47+
#include "utils.h"
4748

4849
// QMLTermWidget
4950
#include <QQuickWindow>
@@ -330,13 +331,40 @@ void Session::run()
330331
// the background color is deemed dark or not
331332
QString backgroundColorHint = _hasDarkBackground ? "COLORFGBG=15;0" : "COLORFGBG=0;15";
332333

334+
QStringList environmentVars = _environment;
335+
environmentVars.append(backgroundColorHint);
336+
337+
// If we are running on Flatpak, we should have access to the host
338+
if (isRunningOnFlatpak()) {
339+
QStringList flatpakArgs;
340+
flatpakArgs << QLatin1String("--host") << QLatin1String("--watch-bus");
341+
342+
for (const QString &envLine : environmentVars)
343+
flatpakArgs << QStringLiteral("--env=%1").arg(envLine);
344+
345+
QStringList whitelist;
346+
whitelist
347+
<< QLatin1String("TERM") << QLatin1String("PATH")
348+
<< QLatin1String("EDITOR") << QLatin1String("PS1")
349+
<< QLatin1String("DISPLAY") << QLatin1String("WAYLAND_DISPLAY");
350+
for (const QString &envName : whitelist) {
351+
const QString value = qEnvironmentVariable(qPrintable(envName));
352+
if (!value.isEmpty())
353+
flatpakArgs << QStringLiteral("--env=%1=%2").arg(envName).arg(value);
354+
}
355+
356+
flatpakArgs << exec;
357+
exec = QLatin1String("/usr/bin/flatpak-spawn");
358+
arguments = flatpakArgs;
359+
}
360+
333361
/* if we do all the checking if this shell exists then we use it ;)
334362
* Dont know about the arguments though.. maybe youll need some more checking im not sure
335363
* However this works on Arch and FreeBSD now.
336364
*/
337365
int result = _shellProcess->start(exec,
338366
arguments,
339-
_environment << backgroundColorHint,
367+
environmentVars,
340368
windowId(),
341369
_addToUtmp);
342370

src/lib/kpty.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ extern "C" {
145145
//#include <kdebug.h>
146146
//#include <kstandarddirs.h> // findExe
147147

148+
#include "utils.h"
149+
148150
// not defined on HP-UX for example
149151
#ifndef CTRL
150152
# define CTRL(x) ((x) & 037)
@@ -474,13 +476,15 @@ void KPty::setCTty()
474476
// and get rid of the old controlling terminal.
475477
setsid();
476478

477-
// make our slave pty the new controlling terminal.
479+
if (!isRunningOnFlatpak()) {
480+
// make our slave pty the new controlling terminal.
478481
#ifdef TIOCSCTTY
479-
ioctl(d->slaveFd, TIOCSCTTY, 0);
482+
ioctl(d->slaveFd, TIOCSCTTY, 0);
480483
#else
481-
// __svr4__ hack: the first tty opened after setsid() becomes controlling tty
482-
::close(::open(d->ttyName, O_WRONLY, 0));
484+
// __svr4__ hack: the first tty opened after setsid() becomes controlling tty
485+
::close(::open(d->ttyName, O_WRONLY, 0));
483486
#endif
487+
}
484488

485489
// make our new process group the foreground group on the pty
486490
int pgrp = getpid();

src/lib/utils.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/****************************************************************************
2+
* This file is part of Liri.
3+
*
4+
* Copyright (C) 2019 Pier Luigi Fiorini <[email protected]>
5+
*
6+
* $BEGIN_LICENSE:LGPLv3+$
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
* $END_LICENSE$
22+
***************************************************************************/
23+
24+
#include <QFile>
25+
26+
#include "utils.h"
27+
28+
bool isRunningOnFlatpak()
29+
{
30+
return QFile::exists(QLatin1String("/.flatpak-info"));
31+
}

src/lib/utils.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/****************************************************************************
2+
* This file is part of Liri.
3+
*
4+
* Copyright (C) 2019 Pier Luigi Fiorini <[email protected]>
5+
*
6+
* $BEGIN_LICENSE:LGPLv3+$
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
* $END_LICENSE$
22+
***************************************************************************/
23+
24+
#ifndef UTILS_H
25+
#define UTILS_H
26+
27+
bool isRunningOnFlatpak();
28+
29+
#endif // UTILS_H

0 commit comments

Comments
 (0)