From a41a38bcc909976e169f5b4bbebd148654713405 Mon Sep 17 00:00:00 2001 From: Jay W Date: Mon, 31 Jan 2022 19:10:10 +0000 Subject: [PATCH 1/3] Add flatpak support to terminatorlib --- terminatorlib/terminal.py | 39 +++++++++++++++++++++++++++++++-------- terminatorlib/util.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/terminatorlib/terminal.py b/terminatorlib/terminal.py index c7babe3c..7bb78a15 100644 --- a/terminatorlib/terminal.py +++ b/terminatorlib/terminal.py @@ -1505,14 +1505,37 @@ class Terminal(Gtk.VBox): dbg('Forking shell: "%s" with args: %s' % (shell, args)) args.insert(0, shell) - result, self.pid = self.vte.spawn_sync(Vte.PtyFlags.DEFAULT, - self.cwd, - args, - envv, - GLib.SpawnFlags.FILE_AND_ARGV_ZERO, - None, - None, - None) + + if util.is_flatpak(): + dbg('Flatpak detected') + args = util.get_flatpak_args(args, envv, self.cwd) + dbg('Forking shell: "%s" with args: %s via flatpak-spawn' % (shell, args)) + + self.pid = self.vte.spawn_async( + Vte.PtyFlags.NO_CTTY, + self.cwd, + args, + envv, + 0, + None, + None, + -1, + None, + None, + None, + ) + else: + result, self.pid = self.vte.spawn_sync( + Vte.PtyFlags.DEFAULT, + self.cwd, + args, + envv, + GLib.SpawnFlags.FILE_AND_ARGV_ZERO, + None, + None, + None + ) + self.command = shell self.titlebar.update() diff --git a/terminatorlib/util.py b/terminatorlib/util.py index 4c4a72d4..c50f9a3e 100644 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -43,6 +43,9 @@ DEBUGCLASSES = [] # list of methods to show debugging for. empty list means show all methods DEBUGMETHODS = [] +def is_flatpak(): + return os.path.exists("/.flatpak-info") + def dbg(log = ""): """Print a message if debugging is enabled""" if DEBUG: @@ -144,6 +147,13 @@ def path_lookup(command): def shell_lookup(): """Find an appropriate shell for the user""" + if is_flatpak(): + getent = subprocess.check_output([ + 'flatpak-spawn', '--host', 'getent', 'passwd', + pwd.getpwuid(os.getuid())[0] + ]).decode(encoding='UTF-8').rstrip('\n') + shell = getent.split(':')[6] + return shell try: usershell = pwd.getpwuid(os.getuid())[6] except KeyError: @@ -394,3 +404,24 @@ def update_config_to_cell_height(filename): except Exception as ex: err('Unable to open ā€˜%sā€™ for reading and/or writting.\n(%s)' % (filename, ex)) + +def get_flatpak_args(args, envv, cwd): + """Contruct args to be executed via flatpak-spawn""" + import json + flatpak_args = None + shell = [args[0]] + env_args = ['--env={}'.format(env) for env in envv] + flatpak_spawn = [ + "flatpak-spawn", "--host", "--watch-bus", "--forward-fd=1", + "--forward-fd=2", "--directory={}".format(cwd) + ] + if len(args) == 2: + flatpak_args = flatpak_spawn + env_args + shell + + # Support -x, -e, custom commands etc. + if len(args) > 2: + flatpak_args = flatpak_spawn + env_args + args[1:] + + dbg('returned flatpak args: %s' % flatpak_args) + + return flatpak_args From 967f039e6023b182998f5eea5a73b0fbf834fef2 Mon Sep 17 00:00:00 2001 From: Jay W Date: Mon, 31 Jan 2022 19:59:34 +0000 Subject: [PATCH 2/3] Simplify flatpak-spawn args --- terminatorlib/util.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/terminatorlib/util.py b/terminatorlib/util.py index c50f9a3e..72eb071e 100644 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -407,20 +407,13 @@ def update_config_to_cell_height(filename): def get_flatpak_args(args, envv, cwd): """Contruct args to be executed via flatpak-spawn""" - import json flatpak_args = None - shell = [args[0]] env_args = ['--env={}'.format(env) for env in envv] flatpak_spawn = [ "flatpak-spawn", "--host", "--watch-bus", "--forward-fd=1", "--forward-fd=2", "--directory={}".format(cwd) ] - if len(args) == 2: - flatpak_args = flatpak_spawn + env_args + shell - - # Support -x, -e, custom commands etc. - if len(args) > 2: - flatpak_args = flatpak_spawn + env_args + args[1:] + flatpak_args = flatpak_spawn + env_args + args dbg('returned flatpak args: %s' % flatpak_args) From 886412fcf1dcf1d0a6cbe4c616ff56ae835fd12d Mon Sep 17 00:00:00 2001 From: Jay W Date: Tue, 1 Feb 2022 19:06:23 +0000 Subject: [PATCH 3/3] Add logic to remove duplicate shell from args --- terminatorlib/util.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/terminatorlib/util.py b/terminatorlib/util.py index 72eb071e..eda14ced 100644 --- a/terminatorlib/util.py +++ b/terminatorlib/util.py @@ -413,6 +413,11 @@ def get_flatpak_args(args, envv, cwd): "flatpak-spawn", "--host", "--watch-bus", "--forward-fd=1", "--forward-fd=2", "--directory={}".format(cwd) ] + # Detect and remove duplicate shell in args + # to work around vte.spawn_sync() requirement. + if len(set([args[0], args[1]])) == 1: + del args[0] + flatpak_args = flatpak_spawn + env_args + args dbg('returned flatpak args: %s' % flatpak_args)