Skip to content

Commit

Permalink
Run shell on the Flatpak host
Browse files Browse the repository at this point in the history
When Terminal is running on Flatpak we need to access the host
system, otherwise the app is pretty useless.

Closes: #36
  • Loading branch information
plfiorini committed Apr 26, 2019
1 parent 7d7d7a5 commit b9ca9fa
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ add_library(qmltermwidget STATIC
kptydevice.h
kptyprocess.cpp
kptyprocess.h
utils.cpp
utils.h
)
target_include_directories(qmltermwidget
PUBLIC
Expand Down
30 changes: 29 additions & 1 deletion src/lib/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "TerminalDisplay.h"
#include "ShellCommand.h"
#include "Vt102Emulation.h"
#include "utils.h"

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

QStringList environmentVars = _environment;
environmentVars.append(backgroundColorHint);

// If we are running on Flatpak, we should have access to the host
if (isRunningOnFlatpak()) {
QStringList flatpakArgs;
flatpakArgs << QLatin1String("--host") << QLatin1String("--watch-bus");

for (const QString &envLine : environmentVars)
flatpakArgs << QStringLiteral("--env=%1").arg(envLine);

QStringList whitelist;
whitelist
<< QLatin1String("TERM") << QLatin1String("PATH")
<< QLatin1String("EDITOR") << QLatin1String("PS1")
<< QLatin1String("DISPLAY") << QLatin1String("WAYLAND_DISPLAY");
for (const QString &envName : whitelist) {
const QString value = qEnvironmentVariable(qPrintable(envName));
if (!value.isEmpty())
flatpakArgs << QStringLiteral("--env=%1=%2").arg(envName).arg(value);
}

flatpakArgs << exec;
exec = QLatin1String("/usr/bin/flatpak-spawn");
arguments = flatpakArgs;
}

/* if we do all the checking if this shell exists then we use it ;)
* Dont know about the arguments though.. maybe youll need some more checking im not sure
* However this works on Arch and FreeBSD now.
*/
int result = _shellProcess->start(exec,
arguments,
_environment << backgroundColorHint,
environmentVars,
windowId(),
_addToUtmp);

Expand Down
12 changes: 8 additions & 4 deletions src/lib/kpty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ extern "C" {
//#include <kdebug.h>
//#include <kstandarddirs.h> // findExe

#include "utils.h"

// not defined on HP-UX for example
#ifndef CTRL
# define CTRL(x) ((x) & 037)
Expand Down Expand Up @@ -474,13 +476,15 @@ void KPty::setCTty()
// and get rid of the old controlling terminal.
setsid();

// make our slave pty the new controlling terminal.
if (!isRunningOnFlatpak()) {
// make our slave pty the new controlling terminal.
#ifdef TIOCSCTTY
ioctl(d->slaveFd, TIOCSCTTY, 0);
ioctl(d->slaveFd, TIOCSCTTY, 0);
#else
// __svr4__ hack: the first tty opened after setsid() becomes controlling tty
::close(::open(d->ttyName, O_WRONLY, 0));
// __svr4__ hack: the first tty opened after setsid() becomes controlling tty
::close(::open(d->ttyName, O_WRONLY, 0));
#endif
}

// make our new process group the foreground group on the pty
int pgrp = getpid();
Expand Down
31 changes: 31 additions & 0 deletions src/lib/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/****************************************************************************
* This file is part of Liri.
*
* Copyright (C) 2019 Pier Luigi Fiorini <[email protected]>
*
* $BEGIN_LICENSE:LGPLv3+$
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
* $END_LICENSE$
***************************************************************************/

#include <QFile>

#include "utils.h"

bool isRunningOnFlatpak()
{
return QFile::exists(QLatin1String("/.flatpak-info"));
}
29 changes: 29 additions & 0 deletions src/lib/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/****************************************************************************
* This file is part of Liri.
*
* Copyright (C) 2019 Pier Luigi Fiorini <[email protected]>
*
* $BEGIN_LICENSE:LGPLv3+$
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
* $END_LICENSE$
***************************************************************************/

#ifndef UTILS_H
#define UTILS_H

bool isRunningOnFlatpak();

#endif // UTILS_H

0 comments on commit b9ca9fa

Please sign in to comment.