diff options
author | John Ankarström <john@ankarstrom.se> | 2021-06-07 19:41:00 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-06-07 19:41:00 +0200 |
commit | ef1729c0f2f85ed66b9b5accbc0eb9a62af36ef2 (patch) | |
tree | f07d0908063c88d18ffebc5ce518353666ef4085 | |
parent | 81b09f0e5cd153bdca77782c3438cd28ffefeace (diff) | |
download | tterm-ef1729c0f2f85ed66b9b5accbc0eb9a62af36ef2.tar.gz |
Get it working
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | tterm.c | 69 |
2 files changed, 61 insertions, 10 deletions
@@ -1,6 +1,6 @@ CFLAGS != pkg-config --cflags x11 LDFLAGS != pkg-config --libs x11 -CFLAGS += -O2 -pedantic -Wall -Wextra +CFLAGS += -O2 tterm: tterm.c @@ -1,8 +1,10 @@ #include <err.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/param.h> +#include <sys/stat.h> #include <unistd.h> #include <X11/Xatom.h> #include <X11/Xlib.h> @@ -41,22 +43,71 @@ findwindow(unsigned int pid, Window w) int main(int argc, char *argv[]) { - char *ap; + char *cwd, *cmd, *line, *writefile; + FILE *writefp; + int i; + pid_t child; + size_t size; + ssize_t len; Window w; + /* create fifo */ + + writefile = malloc(40*sizeof(char)); + if (writefile == NULL) err(1, "malloc"); + + sprintf(writefile, "/var/tmp/tterm/%d.w", getpid()); + if (mkdir("/var/tmp/tterm", 0700) == -1 && errno != EEXIST) + err(1, "mkdir"); + + unlink(writefile); + if (mkfifo(writefile, 0600) == -1) err(1, "mkfifo"); + + /* start terminal */ + if ((child = fork()) == 0) { + execlp("xterm", "xterm", "-e", "/usr/local/bin/ksh", + "-w", writefile, NULL); + err(1, "execvp"); + } + + /* find new window */ + display = XOpenDisplay(0); - if (display == NULL) - die("could not open display\n"); + if (display == NULL) die("could not open display\n"); root = XDefaultRootWindow(display); pidatom = XInternAtom(display, "_NET_WM_PID", 1); - if (pidatom == None) - die("no _NET_WM_PID atom found\n"); + if (pidatom == None) die("no _NET_WM_PID atom found\n"); + + for (i = 0; !w && i++ < 1000; w = findwindow(child, root)) ; + if (!w) die("could not find window\n"); + + /* watch fifo and update title */ + writefp = fopen(writefile, "r"); + if (writefp == NULL) err(1, "fopen"); - w = findwindow(2400, root); - if (!w) - die("could not find window\n"); + cmd = malloc(1000*sizeof(char)); + if (cmd == NULL) err(1, "malloc"); + + line = NULL; + size = 0; + while ((len = getline(&line, &size, writefp)) != -1) { + if (strncmp(line, "cwd", 3) == 0) { + line += 3; line[len-4] = 0; /* chomp */ + cwd = strdup(line); + XStoreName(display, w, cwd); + XFlush(display); + } else if (strncmp(line, "cmd", 3) == 0) { + line += 3; line[len-4] = 0; /* chomp */ + snprintf(cmd, 1000, "%s (%s)", line, cwd); + XStoreName(display, w, cmd); + XFlush(display); + } else { + XStoreName(display, w, cwd); + XFlush(display); + } + } - //XStoreName(display, w, ap); + unlink(writefile); XCloseDisplay(display); } |