aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/interface-gtk/interface.c
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-29 12:01:38 +0100
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-30 02:42:34 +0300
commita7202a1fb911f72c309380b42c0ff995c05ba94c (patch)
treeb808fc297e4cf4dd719778f57dd54c5928986460 /src/interface-gtk/interface.c
parentd48439b1e81cc0d0835c63a8bac14562b4f4b5c7 (diff)
GTK: implemented --detach|-d option for detaching from controlling terminalmaster-fmsbw-ci
This is useful to launch from a terminal without "blocking" this terminal. There are tools like nohup and daemonize (BSD) to do the same, but having it builtin is shorter to write.
Diffstat (limited to 'src/interface-gtk/interface.c')
-rw-r--r--src/interface-gtk/interface.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/interface-gtk/interface.c b/src/interface-gtk/interface.c
index a86e3fd..32370d9 100644
--- a/src/interface-gtk/interface.c
+++ b/src/interface-gtk/interface.c
@@ -19,6 +19,7 @@
#include "config.h"
#endif
+#include <stdlib.h>
#include <string.h>
#include <signal.h>
@@ -27,6 +28,7 @@
#include <glib/gstdio.h>
#ifdef G_OS_UNIX
+#include <unistd.h>
#include <glib-unix.h>
#endif
@@ -105,7 +107,7 @@ static struct {
/* current document's name or empty string for "(Unnamed)" buffer */
teco_string_t info_current;
- gboolean no_csd;
+ gboolean no_csd, detach;
gint xembed_id;
GtkWidget *info_bar_widget;
@@ -132,6 +134,26 @@ static struct {
void
teco_interface_init(void)
{
+#ifdef G_OS_UNIX
+ if (teco_interface.detach) {
+ /*
+ * NOTE: There is also daemon() on BSD/Linux,
+ * but the following should be more portable.
+ */
+ pid_t pid = fork();
+ g_assert(pid >= 0);
+ if (pid != 0)
+ /* parent process */
+ exit(EXIT_SUCCESS);
+
+ setsid();
+
+ g_freopen("/dev/null", "r", stdin);
+ g_freopen("/dev/null", "a+", stdout);
+ g_freopen("/dev/null", "a+", stderr);
+ }
+#endif
+
/*
* gtk_init() is not necessary when using gtk_get_option_group(),
* but this will open the default display.
@@ -341,6 +363,11 @@ teco_interface_get_options(void)
G_OPTION_ARG_INT, &teco_interface.xembed_id,
"Embed into an existing X11 Window.", "ID"},
#endif
+#ifdef G_OS_UNIX
+ {"detach", 'd', G_OPTION_FLAG_IN_MAIN,
+ G_OPTION_ARG_NONE, &teco_interface.detach,
+ "Detach from controlling terminal (daemonize).", NULL},
+#endif
{NULL}
};