Commit e057c73a authored by Paulo Queiroz's avatar Paulo Queiroz 📦
Browse files

Save WIP -- I give up for now...

parent bde06899
......@@ -10,6 +10,7 @@ i18n = import('i18n')
subdir('data')
subdir('src')
subdir('toolbox')
subdir('po')
meson.add_install_script('build-aux/meson/postinstall.py')
......@@ -27,6 +27,7 @@ terminal_deps = [
dependency('vte-2.91-gtk4', version: '>= 0.68.0'),
dependency('json-glib-1.0', version: '>= 1.4.4'),
dependency('libpcre2-8'),
meson.get_compiler('vala').find_library('posix'),
]
# https://github.com/elementary/terminal/blob/d9620eb12331a28c658f97ac9a1bdb809aa90089/meson.build
......
......@@ -70,4 +70,50 @@ namespace Terminal {
return arr;
}
public int get_foreground_process (
int terminal_fd,
Cancellable? cancellable = null
) {
if (!is_flatpak ()) {
return Posix.tcgetpgrp (terminal_fd);
}
KeyFile kf = new KeyFile ();
kf.load_from_file ("/.flatpak-info", KeyFileFlags.NONE);
string host_root = kf.get_string ("Instance", "app-path");
string[] argv = {
"flatpak-spawn",
"--host",
"%s/bin/terminal-toolbox".printf (host_root),
// "/app/bin/toolbox/terminal-toolbox",
"tcgetpgrp",
terminal_fd.to_string ()
};
warning ("flatpak-spawn command: %s", string.joinv (" ", argv));
var launcher = new GLib.SubprocessLauncher(
SubprocessFlags.STDOUT_PIPE | SubprocessFlags.STDERR_PIPE
);
launcher.setenv("G_MESSAGES_DEBUG", "false", true);
var sp = launcher.spawnv(argv);
if (sp == null) {
return -1;
}
string? buf = null;
string? err_buf = null;
if (!sp.communicate_utf8(null, cancellable, out buf, out err_buf)) {
return -1;
}
warning ("PID from terminal-toolbox '%s' -- err %s", buf?.strip (), err_buf);
return -1;
}
}
......@@ -40,7 +40,7 @@ public class Terminal.Terminal : Vte.Terminal {
// Properties
public Scheme scheme { get; set; }
public Pid pid { get; protected set; }
public Pid pid { get; protected set; default = -1; }
// Fields
......@@ -350,6 +350,41 @@ public class Terminal.Terminal : Vte.Terminal {
return false;
}
public bool get_can_close (out string command = null) {
if (this.pid < 0 || this.pty == null) {
return true;
}
var fd = this.pty.fd;
if (fd == -1) {
return true;
}
// Get terminal's foreground process
var fgpid = get_foreground_process (fd);
if (fgpid == -1) {
return false;
}
var filename = "/proc/%d/cmdline".printf (fgpid);
string? data = null;
try {
if (!FileUtils.get_contents (filename, out data, null)) {
// There is something running, we just failed to check what it is
return false;
}
command = Filename.to_utf8 (data, data.length, null, null);
warning ("Terminal running command %s", command);
}
catch (Error e) {
warning ("%s", e.message);
}
return true;
}
// private void on_drag_data_received(
// Gdk.DragContext _context,
// int _x,
......
......@@ -214,6 +214,18 @@ public class Terminal.Window : Adw.ApplicationWindow {
return w.tab_view;
});
this.tab_view.close_page.connect ((page) => {
var terminal = (page.get_child () as TerminalTab)?.terminal;
bool can_close = true;
if (terminal != null) {
can_close = terminal.get_can_close ();
}
this.tab_view.close_page_finish (page, can_close);
return true;
});
// Close the window if all tabs were closed
this.tab_view.notify["n-pages"].connect (() => {
if (this.tab_view.n_pages < 1) {
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <stdarg.h>
/**
* This is a simple program meant to be launched with flatpak-spawn --host to
* retrieve host information Flatpak'ed apps don't have access to. The original
* idea for this program came from
* https://github.com/gnunn1/tilix/blob/master/experimental/flatpak/tilix-flatpak-toolbox.c
*/
int die (int code, char *fmt, ...) __attribute__((format(printf,2,0)));
int die (int code, char *fmt, ...) {
va_list list;
va_start (list, fmt);
vfprintf (stderr, fmt, list);
return code;
}
int main (int argc, const char *argv[]) {
if (argc < 2) {
return die (1, "Not enough arguments.\n");
}
if (strcmp (argv[1], "tcgetpgrp") == 0) {
if (argc != 3) {
return die (1, "Missing argument `fd` for tcgetpgrp.\n");
}
unsigned long _fd = (int) strtoul (argv[2], NULL, 10);
if (_fd > __INT_MAX__) {
return die (1, "Invalid fd.\n");
}
int fd = (int) _fd;
pid_t pid = tcgetpgrp (fd);
printf ("pid:%d (fd %d)\n", pid, fd);
return 0;
}
else {
return die (1, "Unknown command '%s'.\n", argv[1]);
}
return 0;
}
toolbox_sources = files(
'main.c',
)
toolbox_deps = []
toolbox_build_args = ['-Werror', '-Wextra', '-Wall']
executable('terminal-toolbox',
toolbox_sources,
c_args: toolbox_build_args,
dependencies: toolbox_deps,
install: true,
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment