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 XRectangle rect;
35 static Bool done = False;
37 static Item *allitem = NULL; /* first of all items */
38 static Item *item = NULL; /* first of pattern matching items */
39 static Item *sel = NULL;
40 static Item *nextoff = NULL;
41 static Item *prevoff = NULL;
42 static Item *curroff = NULL;
45 static char *title = NULL;
46 static char text[4096];
49 static unsigned int cmdw = 0;
50 static unsigned int twidth = 0;
51 static unsigned int cwidth = 0;
52 static const int seek = 30; /* 30px */
54 static Brush brush = {0};
56 static void draw_menu();
57 static void kpress(XKeyEvent * e);
59 static char version[] = "gridmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
64 fprintf(stderr, "%s", "usage: gridmenu [-v] [-t <title>]\n");
71 unsigned int tw, w = cmdw + 2 * seek;
76 for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
77 tw = textwidth(&brush.font, nextoff->text);
78 if(tw > rect.width / 3)
80 w += tw + brush.font.height;
86 for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
87 tw = textwidth(&brush.font, prevoff->left->text);
88 if(tw > rect.width / 3)
90 w += tw + brush.font.height;
97 update_items(char *pattern)
99 unsigned int plen = strlen(pattern);
105 if(!title || *pattern)
113 for(i = allitem; i; i=i->next)
114 if(!plen || !strncmp(pattern, i->text, plen)) {
124 for(i = allitem; i; i=i->next)
125 if(plen && strncmp(pattern, i->text, plen)
126 && strstr(i->text, pattern)) {
137 curroff = prevoff = nextoff = sel = item;
142 /* creates brush structs for brush mode drawing */
146 unsigned int offx = 0;
152 draw(dpy, &brush, False, 0);
155 if(!title || text[0]) {
158 brush.rect.width = cmdw;
159 draw(dpy, &brush, False, text);
163 brush.rect.width = cmdw;
164 draw(dpy, &brush, False, title);
166 offx += brush.rect.width;
170 brush.rect.width = seek;
171 offx += brush.rect.width;
172 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
174 /* determine maximum items */
175 for(i = curroff; i != nextoff; i=i->right) {
176 brush.border = False;
178 brush.rect.width = textwidth(&brush.font, i->text);
179 if(brush.rect.width > rect.width / 3)
180 brush.rect.width = rect.width / 3;
181 brush.rect.width += brush.font.height;
183 swap((void **)&brush.fg, (void **)&brush.bg);
184 draw(dpy, &brush, True, i->text);
185 swap((void **)&brush.fg, (void **)&brush.bg);
188 draw(dpy, &brush, False, i->text);
189 offx += brush.rect.width;
192 brush.rect.x = rect.width - seek;
193 brush.rect.width = seek;
194 draw(dpy, &brush, False, nextoff ? ">" : 0);
196 XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, rect.width,
202 kpress(XKeyEvent * e)
207 unsigned int i, len = strlen(text);
210 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
212 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
213 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
214 || IsPrivateKeypadKey(ksym))
217 /* first check if a control mask is omitted */
218 if(e->state & ControlMask) {
250 default: /* ignore other control sequences */
257 if(!(sel && sel->left))
260 if(sel->right == curroff) {
268 strncpy(text, sel->text, sizeof(text));
272 if(!(sel && sel->right))
281 if(e->state & ShiftMask) {
283 fprintf(stdout, "%s", text);
286 fprintf(stdout, "%s", sel->text);
288 fprintf(stdout, "%s", text);
302 } while(i && nitem && prev_nitem == nitem);
307 if((num == 1) && !iscntrl((int) buf[0])) {
310 strncat(text, buf, sizeof(text));
312 strncpy(text, buf, sizeof(text));
322 static char *maxname = NULL;
324 unsigned int len = 0, max = 0;
328 while(fgets(buf, sizeof(buf), stdin)) {
330 if (buf[len - 1] == '\n')
338 new = emalloc(sizeof(Item));
339 new->next = new->left = new->right = NULL;
352 main(int argc, char *argv[])
355 XSetWindowAttributes wa;
361 fputs(STATUSCMD, stdout);
363 pipe_spawn(buf, sizeof(buf), NULL, STATUSCMD);
368 /* command line args */
369 for(i = 1; i < argc; i++) {
370 if (argv[i][0] == '-')
371 switch (argv[i][1]) {
373 fprintf(stdout, "%s", version);
390 dpy = XOpenDisplay(0);
392 error("gridmenu: cannot open dpy\n");
393 screen = DefaultScreen(dpy);
394 root = RootWindow(dpy, screen);
396 maxname = read_allitems();
398 /* grab as early as possible, but after reading all items!!! */
399 while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
400 GrabModeAsync, CurrentTime) != GrabSuccess)
404 loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
405 loadfont(dpy, &brush.font, FONT);
407 wa.override_redirect = 1;
408 wa.background_pixmap = ParentRelative;
409 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
411 rect.width = DisplayWidth(dpy, screen);
412 rect.height = labelheight(&brush.font);
413 rect.y = DisplayHeight(dpy, screen) - rect.height;
416 win = XCreateWindow(dpy, root, rect.x, rect.y,
417 rect.width, rect.height, 0, DefaultDepth(dpy, screen),
418 CopyFromParent, DefaultVisual(dpy, screen),
419 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
420 XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
424 brush.gc = XCreateGC(dpy, root, 0, 0);
425 brush.drawable = XCreatePixmap(dpy, win, rect.width, rect.height,
426 DefaultDepth(dpy, screen));
430 cwidth = textwidth(&brush.font, maxname) + brush.font.height;
431 if(cwidth > rect.width / 3)
432 cwidth = rect.width / 3;
435 twidth = textwidth(&brush.font, title) + brush.font.height;
436 if(twidth > rect.width / 3)
437 twidth = rect.width / 3;
440 cmdw = title ? twidth : cwidth;
444 XMapRaised(dpy, win);
448 /* main event loop */
449 while(!XNextEvent(dpy, &ev)) {
455 if(ev.xexpose.count == 0) {
466 XUngrabKeyboard(dpy, CurrentTime);
467 XFreePixmap(dpy, brush.drawable);
468 XFreeGC(dpy, brush.gc);
469 XDestroyWindow(dpy, win);