diff options
author | Malfurious <m@lfurio.us> | 2021-10-03 02:16:10 -0400 |
---|---|---|
committer | Malfurious <m@lfurio.us> | 2024-08-12 13:15:35 -0400 |
commit | 09774ed8d6f014e63cdcd558ff0503acc73283d4 (patch) | |
tree | 68698e2e780123e822cf5c9db5d9d9a1932b5e74 | |
parent | b3b90931f13ee65c4713963327f82778d7c75495 (diff) | |
download | st-09774ed8d6f014e63cdcd558ff0503acc73283d4.tar.gz st-09774ed8d6f014e63cdcd558ff0503acc73283d4.zip |
patch: newterm (orphan version)
Ctrl-Shift-Return now creates a new st terminal, whose CWD is the same
as the parent st's CWD.
This version of the patch does a double fork, a technique commonly used
by daemons to spawn orphan processes.
This solution is specific to the swallow patch for dwm which traverses
the process tree to determine if the new window is a decendant of a
terminal window, in which case the new window should take the place of
the terminal window.
The way the original newterm patch worked the new st terminal would be a
direct decendant of the parent st terminal process, which could lead to
the wrong terminal window being swallowed.
The double fork method avoids this by leaving all new st terminals as
orphans, i.e. they will have no parent process.
-rw-r--r-- | config.def.h | 1 | ||||
-rw-r--r-- | st.c | 32 | ||||
-rw-r--r-- | st.h | 1 |
3 files changed, 34 insertions, 0 deletions
diff --git a/config.def.h b/config.def.h index e7f5a0b..6326287 100644 --- a/config.def.h +++ b/config.def.h @@ -204,6 +204,7 @@ static Shortcut shortcuts[] = { { TERMMOD, XK_Y, selpaste, {.i = 0} }, { ShiftMask, XK_Insert, selpaste, {.i = 0} }, { TERMMOD, XK_Num_Lock, numlock, {.i = 0} }, + { TERMMOD, XK_Return, newterm, {.i = 0} }, }; /* @@ -153,6 +153,7 @@ typedef struct { } STREscape; static void execsh(char *, char **); +static char *getcwd_by_pid(pid_t pid); static void stty(char **); static void sigchld(int); static void ttywriteraw(const char *, size_t); @@ -1056,6 +1057,37 @@ tswapscreen(void) } void +newterm(const Arg* a) +{ + int res; + switch (fork()) { + case -1: + die("fork failed: %s\n", strerror(errno)); + break; + case 0: + switch (fork()) { + case -1: + die("fork failed: %s\n", strerror(errno)); + break; + case 0: + res = chdir(getcwd_by_pid(pid)); + execlp("st", "./st", NULL); + break; + default: + exit(0); + } + default: + wait(NULL); + } +} + +static char *getcwd_by_pid(pid_t pid) { + char buf[32]; + snprintf(buf, sizeof buf, "/proc/%d/cwd", pid); + return realpath(buf, NULL); +} + +void tscrolldown(int orig, int n) { int i; @@ -81,6 +81,7 @@ void die(const char *, ...); void redraw(void); void draw(void); +void newterm(const Arg *); void printscreen(const Arg *); void printsel(const Arg *); void sendbreak(const Arg *); |