summaryrefslogtreecommitdiff
path: root/tterm.c
blob: 626d414292c71e802ae45b82ff12bb5a0dff5f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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>

#define die(...) do{fprintf(stderr,__VA_ARGS__);exit(1);}while(0)

Atom pidatom;
Display *display;
Window root;

unsigned char *pidprop = 0;

Window
findwindow(unsigned int pid, Window w)
{
	Atom type;
	int format;
	unsigned int i, nchildren;
	unsigned long nitems, bytes_after;
	Window *children, _root, parent;

	pidprop = 0;
	if (XGetWindowProperty(display, w, pidatom, 0, 1, False, XA_CARDINAL,
		&type, &format, &nitems, &bytes_after, &pidprop) == Success
	&& pidprop && pid == *((unsigned long *)pidprop))
		return w;

	if (XQueryTree(display, w, &_root, &parent, &children, &nchildren))
		for (i = 0; i < nchildren; i++) {
			w = findwindow(pid, children[i]);
			if (w) return w;
		}
	return 0;
}

int
main(int argc, char *argv[])
{
	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");

	root = XDefaultRootWindow(display);
	pidatom = XInternAtom(display, "_NET_WM_PID", 1);
	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");

	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);
		}
	}

	unlink(writefile);
	XCloseDisplay(display);
}