2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
4 * See LICENSE file for license details.
16 #include <X11/cursorfont.h>
17 #include <X11/Xutil.h>
18 #include <X11/keysym.h>
20 typedef struct Item Item;
23 Item *next; /* traverses all items */
24 Item *left, *right; /* traverses items matching current search pattern */
31 static Bool done = False;
33 static Item *allitem = NULL; /* first of all items */
34 static Item *item = NULL; /* first of pattern matching items */
35 static Item *sel = NULL;
36 static Item *nextoff = NULL;
37 static Item *prevoff = NULL;
38 static Item *curroff = NULL;
40 static int screen, mx, my, mw, mh;
41 static char *title = NULL;
42 static char text[4096];
45 static unsigned int cmdw = 0;
46 static unsigned int tw = 0;
47 static unsigned int cw = 0;
48 static const int seek = 30; /* 30px */
50 static Brush brush = {0};
52 static void draw_menu();
53 static void kpress(XKeyEvent * e);
55 static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
58 usage() { error("usage: gridmenu [-v] [-t <title>]\n"); }
63 unsigned int tw, w = cmdw + 2 * seek;
68 for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
69 tw = textw(&brush.font, nextoff->text);
72 w += tw + brush.font.height;
78 for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
79 tw = textw(&brush.font, prevoff->left->text);
82 w += tw + brush.font.height;
89 update_items(char *pattern)
91 unsigned int plen = strlen(pattern);
97 if(!title || *pattern)
105 for(i = allitem; i; i=i->next)
106 if(!plen || !strncmp(pattern, i->text, plen)) {
116 for(i = allitem; i; i=i->next)
117 if(plen && strncmp(pattern, i->text, plen)
118 && strstr(i->text, pattern)) {
129 curroff = prevoff = nextoff = sel = item;
134 /* creates brush structs for brush mode drawing */
144 draw(dpy, &brush, False, 0);
147 if(!title || text[0]) {
151 draw(dpy, &brush, False, text);
156 draw(dpy, &brush, False, title);
162 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
165 /* determine maximum items */
166 for(i = curroff; i != nextoff; i=i->right) {
167 brush.border = False;
168 brush.w = textw(&brush.font, i->text);
171 brush.w += brush.font.height;
173 swap((void **)&brush.fg, (void **)&brush.bg);
174 draw(dpy, &brush, True, i->text);
175 swap((void **)&brush.fg, (void **)&brush.bg);
178 draw(dpy, &brush, False, i->text);
184 draw(dpy, &brush, False, nextoff ? ">" : 0);
186 XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
191 kpress(XKeyEvent * e)
196 unsigned int i, len = strlen(text);
199 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
201 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
202 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
203 || IsPrivateKeypadKey(ksym))
206 /* first check if a control mask is omitted */
207 if(e->state & ControlMask) {
209 default: /* ignore other control sequences */
229 if(!(sel && sel->left))
232 if(sel->right == curroff) {
240 strncpy(text, sel->text, sizeof(text));
244 if(!(sel && sel->right))
253 if(e->state & ShiftMask) {
255 fprintf(stdout, "%s", text);
258 fprintf(stdout, "%s", sel->text);
260 fprintf(stdout, "%s", text);
274 } while(i && nitem && prev_nitem == nitem);
279 if(num && !iscntrl((int) buf[0])) {
282 strncat(text, buf, sizeof(text));
284 strncpy(text, buf, sizeof(text));
294 static char *maxname = NULL;
296 unsigned int len = 0, max = 0;
300 while(fgets(buf, sizeof(buf), stdin)) {
302 if (buf[len - 1] == '\n')
310 new = emalloc(sizeof(Item));
311 new->next = new->left = new->right = NULL;
324 main(int argc, char *argv[])
327 XSetWindowAttributes wa;
331 /* command line args */
332 for(i = 1; i < argc; i++) {
333 if (argv[i][0] == '-')
334 switch (argv[i][1]) {
336 fprintf(stdout, "%s", version);
353 dpy = XOpenDisplay(0);
355 error("gridmenu: cannot open dpy\n");
356 screen = DefaultScreen(dpy);
357 root = RootWindow(dpy, screen);
359 maxname = read_allitems();
361 /* grab as early as possible, but after reading all items!!! */
362 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
363 GrabModeAsync, CurrentTime) != GrabSuccess)
367 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
368 loadfont(dpy, &brush.font, FONT);
370 wa.override_redirect = 1;
371 wa.background_pixmap = ParentRelative;
372 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
375 mw = DisplayWidth(dpy, screen);
376 mh = texth(&brush.font);
378 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
379 DefaultDepth(dpy, screen), CopyFromParent,
380 DefaultVisual(dpy, screen),
381 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
382 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
386 brush.gc = XCreateGC(dpy, root, 0, 0);
387 brush.drawable = XCreatePixmap(dpy, win, mw, mh,
388 DefaultDepth(dpy, screen));
392 cw = textw(&brush.font, maxname) + brush.font.height;
397 tw = textw(&brush.font, title) + brush.font.height;
402 cmdw = title ? tw : cw;
406 XMapRaised(dpy, win);
410 /* main event loop */
411 while(!done && !XNextEvent(dpy, &ev)) {
417 if(ev.xexpose.count == 0)
425 XUngrabKeyboard(dpy, CurrentTime);
426 XFreePixmap(dpy, brush.drawable);
427 XFreeGC(dpy, brush.gc);
428 XDestroyWindow(dpy, win);