summaryrefslogtreecommitdiff
path: root/cmdtree.c
blob: 142bbb8320ddd8fd3a03ffc744b4b25282c92a08 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include <unistd.h>

#include "util.h"

static Window root, parentwin, win;
static int screen;
static Display *display;
static int mw, mh;
static unsigned int lines = 0;
static XIC xic;

// config
static int topbar = 1;

static void
setup()
{
	int x, y;
	XSetWindowAttributes swa;
	XWindowAttributes wa;
	XIM xim;
	XClassHint ch = {"cmdtree", "cmdtree"};

	if (!XGetWindowAttributes(display, parentwin, &wa))
		die("could not get embedding window attributes: 0x%lx",
		    parentwin);
	x = 0;
	y = topbar ? 0 : wa.height - mh;
	mw = wa.width;
	lines = MAX(lines, 0);
	/* mh = (lines + 1) * bh; */
	mh = 100;

	swa.override_redirect = True;
	/* swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; */
	swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;

	win = XCreateWindow(display, parentwin, x, y, mw, mh, 0,
	                    CopyFromParent, CopyFromParent, CopyFromParent,
	                    CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);

	XSetClassHint(display, win, &ch);

	// what do?
	/* XMapRaised(display, win); */


	xim = XOpenIM(display, NULL, NULL, NULL);
	xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
			XNClientWindow, win, XNFocusWindow, win, NULL);
	XSelectInput(display, win, ExposureMask | KeyPressMask);
	XMapWindow(display, win);


	XSetInputFocus(display, win, RevertToParent, CurrentTime);

	// XXX embed
	/* if (embed) { */
	/* 	XSelectInput(dpy, parentwin, FocusChangeMask); */
	/* 	if (XQueryTree(dpy, parentwin, &dw, &w, &dws, &du) && dws) { */
	/* 		for (i = 0; i < du && dws[i] != win; ++i) */
	/* 			XSelectInput(dpy, dws[i], FocusChangeMask); */
	/* 		XFree(dws); */
	/* 	} */
	/* 	grabfocus(); */
	/* } */

	/* drw_resize(drw, mw, mh); */
	/* drawmenu(); */
}

static void
draw_tree() {
	static const char *msg = "cmdtree is a tree of commands";

	XFillRectangle(display, win, DefaultGC(display, screen),
		       20, 20, 10, 10);

	XDrawString(display, win, DefaultGC(display, screen), 10,
		    50, msg, strlen(msg));
}


static void
run() {
	XEvent e;
	int done = 0;
	char buf[32];
	KeySym ksym = NoSymbol;
	Status status;

	while (!done) {
		XNextEvent(display, &e);
		if (e.type == Expose) {
			draw_tree();
		}

		if (e.type == KeyPress) {
			XmbLookupString(xic, (XKeyEvent*)&e, buf,
					sizeof buf, &ksym, &status);
			if (ksym == XK_q) done = 1;
			if (ksym == XK_Escape) done = 1;
		}

	}

}


int main(void) {

	display = XOpenDisplay(NULL);
	if (display == NULL) {
		fprintf(stderr, "Cannot open display\n");
		exit(1);
	}

	screen = DefaultScreen(display);
	root = RootWindow(display, screen);

	// XXX embed
	/* if (!embed || !(parentwin = strtol(embed, NULL, 0))) */
	/* 	parentwin = root; */

	parentwin = root;

	setup();
	run();

	XCloseDisplay(display);
	return 0;
}