ref: 37881fc5c5bd1047b703f6b3ad5f851c7021b808 cmdtree/cmdtree.c -rw-r--r-- 7.2 KiB View raw
                                                                                
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xft/Xft.h>

#include <unistd.h>
#include <assert.h>

#include "drw.h"
#include "util.h"
#include "command.h"

enum {
	SchemeNorm,
	SchemePrefix,
	SchemeLast,
}; /* color schemes */


struct cmdstack {
	struct command *children;
	int nchildren;
};

#define CMDSTACK_SIZE 32
static struct cmdstack cmdstack[CMDSTACK_SIZE];
static int cmdstack_ptr = 0;

static Window root, parentwin, win;
static int screen;
static Display *display;
static int sep_width;
static int bh, mw, mh;
static XIC xic;

#define DEFCMD(b, nme, cmd)			\
	{ .bind = (b),				\
			.name = (nme),		\
			.command = (cmd),	\
			.nchildren = 0,		\
			.children = NULL,	\
			},

#define DEFPREFIX(b, nme, cs)				\
	{ .bind = b,					\
			.name = nme,			\
			.command = 0,			\
			.nchildren = LENGTH(cs),	\
			.children = (cs),		\
			},

#include "cfg.h"

static int
cmdstack_push_(struct command *children, int nchildren) {
	if (cmdstack_ptr + 1 >= CMDSTACK_SIZE)
		return 0;
	cmdstack[cmdstack_ptr].children = children;
	cmdstack[cmdstack_ptr].nchildren = nchildren;
	cmdstack_ptr++;
	return 1;
}

static int
cmdstack_push(struct command *cmd) {
	return cmdstack_push_(cmd->children, cmd->nchildren);
}

struct cmdstack *
cmdstack_top() {
	assert(cmdstack_ptr >= 1);
	return &cmdstack[cmdstack_ptr-1];
}

struct cmdstack *
cmdstack_pop() {
	if (cmdstack_ptr == 1)
		return 0;

	return &cmdstack[cmdstack_ptr--];
}

static void
grabkeyboard(void)
{
	struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000  };
	int i;

	// XXXembed
	/* if (embed) */
	/* 	return; */
	/* try to grab keyboard, we may have to wait for another process to ungrab */
	for (i = 0; i < 1000; i++) {
		if (XGrabKeyboard(display, DefaultRootWindow(display), True,
				  GrabModeAsync, GrabModeAsync, CurrentTime)
		    == GrabSuccess)
			return;
		nanosleep(&ts, NULL);
	}
	die("cannot grab keyboard");
}

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

	bh = drw->fonts->h + 2;
	/* init appearance */
	drw_scm_create(drw, schemes, LENGTH(schemes));

	if (!XGetWindowAttributes(display, parentwin, &wa))
		die("could not get embedding window attributes: 0x%lx",
		    parentwin);
	int vertwidth = 200;
	mw = wa.width;
	mh = 1 * bh;
	switch (position) {
	case POSITION_BOTTOM:
		y = wa.height - mh;
		x = 0;
		break;
	case POSITION_TOP:
		x = 0;
		y = 0;
		break;
	case POSITION_LEFT:
		x = 0;
		y = 0;
		// TODO: calc vertwidth = max(textwidth(nodes)))
		mw = vertwidth;
		mh = wa.height;
		break;
	case POSITION_RIGHT:
		x = wa.width - vertwidth;
		y = 0;
		mw = vertwidth;
		mh = wa.height;
	}
	sep_width = drw_fontset_getwidth(drw, separator);

	swa.override_redirect = True;
	swa.background_pixel = schemes[SchemeNorm].bg_clr.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);
}

static const char *
bind_name(const char *bind) {
	switch (*bind) {
	case ' ': return "SPC";
	default:  return bind;
	}
}

static int
draw_command(Drw *drw, int x, int y, struct command *cmd) {
	char buf[256];
	int lpad = 0;
	int pad = 0;
	unsigned int w;
	int invert = 0;
	int isprefix = 0;
	char *name = cmd->name;
	struct scheme *scheme = NULL;

	//drw_fontset_getwidth(drw, binding) + 1;

	drw_setscheme(drw, &schemes[SchemeNorm].bind_clr,
		      &schemes[SchemeNorm].bg_clr);

	const char *bindname = bind_name(cmd->bind);

	w = drw_fontset_getwidth(drw, bindname);
	x = drw_text(drw, x+pad, y, w, bh, lpad, bindname, invert);

	w = sep_width;
	drw_setscheme(drw, &schemes[SchemeNorm].arrow_clr,
		      &schemes[SchemeNorm].bg_clr);
	x = drw_text(drw, x+pad, y, w, bh, lpad, separator, invert);

	isprefix = command_is_prefix(cmd);

	if (isprefix)
		scheme = &schemes[SchemePrefix];
	else
		scheme = &schemes[SchemeNorm];

	drw_setscheme(drw, &scheme->name_clr, &scheme->bg_clr);

	if (scheme->prefix && strlen(scheme->prefix) != 0) {
		snprintf(buf, 256, "%s%s", scheme->prefix, cmd->name);
		name = buf;
	}

	w = drw_fontset_getwidth(drw, name);
	x = drw_text(drw, x+pad, y, w, bh, lpad, name, invert);

	return x;
}


/* static void */
/* calc_tree_exts(struct node *nodes, int num_nodes, int *rows, int *cols) { */
/* } */
static void
draw_tree_vertical(Drw *drw, struct command *cmds, int ncmds, int x, int y, int w, int h) {
	int i;
	int colw = 0;

	x += xpad;
	y += ypad;

	drw_setscheme(drw, &schemes[SchemeNorm].bg_clr,
		      &schemes[SchemeNorm].bg_clr);
	drw_rect(drw, 0, 0, w, h, 1, 1);

	int c = '0';

	for (i = 0; i < ncmds; ++i, ++c, y += bh) {
		struct command *cmd = &cmds[i];
		if (y >= mh) {
			x = colw;
			y = 0;
			colw = 0;
		}
		colw = MAX(draw_command(drw, x, y, cmd) + 20, colw);
	}
}


static void
draw_tree(Drw *drw, int x, int y, int w, int h) {
	if (!drw)
		return;

	struct cmdstack *cmdstack = cmdstack_top();

	draw_tree_vertical(drw, cmdstack->children, cmdstack->nchildren, x, y,
			   w, h);

	XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
	XSync(drw->dpy, False);
}


static void
cleanup(Drw *drw, int code) {
	drw_free(drw);
	exit(code);
}

static void
run(Drw *drw) {
	XEvent e;
	int done = 0;
	char buf[32];
	KeySym ksym = NoSymbol;
	Status status;
	int code = 0;
	struct cmdstack *cmdstack = NULL;
	struct command *cmd = NULL;

	while (!done) {
		cmd = NULL;
		XNextEvent(display, &e);
		switch (e.type) {
		case Expose:
			draw_tree(drw, 0, 0, mw, mh);
			break;
		case KeyPress:
			XmbLookupString(xic, (XKeyEvent*)&e, buf, sizeof buf,
					&ksym, &status);

			if (ksym == XK_Escape) {
				code = 1;
				done = 1;
				break;
			}
			else if (ksym == XK_BackSpace) {
				if (cmdstack_pop())
					draw_tree(drw, 0, 0, mw, mh);
			}

			cmdstack = cmdstack_top();
			cmd = command_lookup(cmdstack->children,
					     cmdstack->nchildren, buf);

			if (cmd) {
				if (command_is_prefix(cmd)) {
					cmdstack_push(cmd);
					draw_tree(drw, 0, 0, mw, mh);
				}
				else {
					command_exec(cmd);
				}
			}

			break;
		}
	}

	cleanup(drw, code);
}


int main(void) {
	XWindowAttributes wa;
	Drw *drw;

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

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

	parentwin = root;

	int res = cmdstack_push_(commands, LENGTH(commands));
	assert(res);

	if (!XGetWindowAttributes(display, parentwin, &wa))
		die("could not get embedding window attributes: 0x%lx",
		    parentwin);

	drw = drw_create(display, screen, root, wa.width, wa.height);

	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
		die("no fonts could be loaded.");

	grabkeyboard();
	setup(drw);
	run(drw);

	XCloseDisplay(display);
	return 0;
}