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.
19 #include <X11/cursorfont.h>
20 #include <X11/Xutil.h>
21 #include <X11/keysym.h>
23 typedef struct Item Item;
26 Item *next; /* traverses all items */
27 Item *left, *right; /* traverses items matching current search pattern */
34 static Bool done = False;
36 static Item *allitem = NULL; /* first of all items */
37 static Item *item = NULL; /* first of pattern matching items */
38 static Item *sel = NULL;
39 static Item *nextoff = NULL;
40 static Item *prevoff = NULL;
41 static Item *curroff = NULL;
43 static int screen, mx, my, mw, mh;
44 static char *title = NULL;
45 static char text[4096];
48 static unsigned int cmdw = 0;
49 static unsigned int tw = 0;
50 static unsigned int cw = 0;
51 static const int seek = 30; /* 30px */
53 static Brush brush = {0};
55 static void draw_menu();
56 static void kpress(XKeyEvent * e);
58 static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
63 fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
70 unsigned int tw, w = cmdw + 2 * seek;
75 for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
76 tw = textw(&brush.font, nextoff->text);
79 w += tw + brush.font.height;
85 for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
86 tw = textw(&brush.font, prevoff->left->text);
89 w += tw + brush.font.height;
96 update_items(char *pattern)
98 unsigned int plen = strlen(pattern);
104 if(!title || *pattern)
112 for(i = allitem; i; i=i->next)
113 if(!plen || !strncmp(pattern, i->text, plen)) {
123 for(i = allitem; i; i=i->next)
124 if(plen && strncmp(pattern, i->text, plen)
125 && strstr(i->text, pattern)) {
136 curroff = prevoff = nextoff = sel = item;
141 /* creates brush structs for brush mode drawing */
151 draw(dpy, &brush, False, 0);
154 if(!title || text[0]) {
158 draw(dpy, &brush, False, text);
163 draw(dpy, &brush, False, title);
169 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
172 /* determine maximum items */
173 for(i = curroff; i != nextoff; i=i->right) {
174 brush.border = False;
175 brush.w = textw(&brush.font, i->text);
178 brush.w += brush.font.height;
180 swap((void **)&brush.fg, (void **)&brush.bg);
181 draw(dpy, &brush, True, i->text);
182 swap((void **)&brush.fg, (void **)&brush.bg);
185 draw(dpy, &brush, False, i->text);
191 draw(dpy, &brush, False, nextoff ? ">" : 0);
193 XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
198 kpress(XKeyEvent * e)
203 unsigned int i, len = strlen(text);
206 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
208 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
209 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
210 || IsPrivateKeypadKey(ksym))
213 /* first check if a control mask is omitted */
214 if(e->state & ControlMask) {
246 default: /* ignore other control sequences */
253 if(!(sel && sel->left))
256 if(sel->right == curroff) {
264 strncpy(text, sel->text, sizeof(text));
268 if(!(sel && sel->right))
277 if(e->state & ShiftMask) {
279 fprintf(stdout, "%s", text);
282 fprintf(stdout, "%s", sel->text);
284 fprintf(stdout, "%s", text);
298 } while(i && nitem && prev_nitem == nitem);
303 if(num && !iscntrl((int) buf[0])) {
306 strncat(text, buf, sizeof(text));
308 strncpy(text, buf, sizeof(text));
318 static char *maxname = NULL;
320 unsigned int len = 0, max = 0;
324 while(fgets(buf, sizeof(buf), stdin)) {
326 if (buf[len - 1] == '\n')
334 new = emalloc(sizeof(Item));
335 new->next = new->left = new->right = NULL;
348 main(int argc, char *argv[])
351 XSetWindowAttributes wa;
355 /* command line args */
356 for(i = 1; i < argc; i++) {
357 if (argv[i][0] == '-')
358 switch (argv[i][1]) {
360 fprintf(stdout, "%s", version);
377 dpy = XOpenDisplay(0);
379 error("gridmenu: cannot open dpy\n");
380 screen = DefaultScreen(dpy);
381 root = RootWindow(dpy, screen);
383 maxname = read_allitems();
385 /* grab as early as possible, but after reading all items!!! */
386 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
387 GrabModeAsync, CurrentTime) != GrabSuccess)
391 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
392 loadfont(dpy, &brush.font, FONT);
394 wa.override_redirect = 1;
395 wa.background_pixmap = ParentRelative;
396 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
399 mw = DisplayWidth(dpy, screen);
400 mh = texth(&brush.font);
402 win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
403 DefaultDepth(dpy, screen), CopyFromParent,
404 DefaultVisual(dpy, screen),
405 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
406 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
410 brush.gc = XCreateGC(dpy, root, 0, 0);
411 brush.drawable = XCreatePixmap(dpy, win, mw, mh,
412 DefaultDepth(dpy, screen));
416 cw = textw(&brush.font, maxname) + brush.font.height;
421 tw = textw(&brush.font, title) + brush.font.height;
426 cmdw = title ? tw : cw;
430 XMapRaised(dpy, win);
434 /* main event loop */
435 while(!XNextEvent(dpy, &ev)) {
441 if(ev.xexpose.count == 0) {
452 XUngrabKeyboard(dpy, CurrentTime);
453 XFreePixmap(dpy, brush.drawable);
454 XFreeGC(dpy, brush.gc);
455 XDestroyWindow(dpy, win);