1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * See LICENSE file for license details.
12 #include <X11/Xutil.h>
13 #include <X11/keysym.h>
15 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
17 typedef struct Item Item;
19 Item *next; /* traverses all items */
20 Item *left, *right; /* traverses items matching current search pattern */
26 static char text[4096];
27 static char *prompt = NULL;
31 static unsigned int cmdw = 0;
32 static unsigned int promptw = 0;
33 static unsigned int numlockmask = 0;
34 static Bool running = True;
35 static Item *allitems = NULL; /* first of all items */
36 static Item *item = NULL; /* first of pattern matching items */
37 static Item *sel = NULL;
38 static Item *next = NULL;
39 static Item *prev = NULL;
40 static Item *curr = NULL;
50 w = promptw + cmdw + 2 * SPACE;
51 for(next = curr; next; next=next->right) {
52 tw = textw(next->text);
59 w = promptw + cmdw + 2 * SPACE;
60 for(prev = curr; prev && prev->left; prev=prev->left) {
61 tw = textw(prev->left->text);
78 drawtext(NULL, dc.norm);
82 drawtext(prompt, dc.sel);
89 drawtext(text[0] ? text : NULL, dc.norm);
93 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
95 /* determine maximum items */
96 for(i = curr; i != next; i=i->right) {
97 dc.w = textw(i->text);
100 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
105 drawtext(next ? ">" : NULL, dc.norm);
107 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
115 for(len = 1000; len; len--) {
116 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
125 initcolor(const char *colstr) {
126 Colormap cmap = DefaultColormap(dpy, screen);
129 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
130 eprint("error, cannot allocate color '%s'\n", colstr);
135 initfont(const char *fontstr) {
136 char *def, **missing;
141 XFreeFontSet(dpy, dc.font.set);
142 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
144 XFreeStringList(missing);
146 XFontSetExtents *font_extents;
147 XFontStruct **xfonts;
149 dc.font.ascent = dc.font.descent = 0;
150 font_extents = XExtentsOfFontSet(dc.font.set);
151 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
152 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
153 if(dc.font.ascent < (*xfonts)->ascent)
154 dc.font.ascent = (*xfonts)->ascent;
155 if(dc.font.descent < (*xfonts)->descent)
156 dc.font.descent = (*xfonts)->descent;
162 XFreeFont(dpy, dc.font.xfont);
163 dc.font.xfont = NULL;
164 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
165 eprint("error, cannot load font: '%s'\n", fontstr);
166 dc.font.ascent = dc.font.xfont->ascent;
167 dc.font.descent = dc.font.xfont->descent;
169 dc.font.height = dc.font.ascent + dc.font.descent;
173 match(char *pattern) {
179 plen = strlen(pattern);
182 for(i = allitems; i; i=i->next)
183 if(!plen || !strncmp(pattern, i->text, plen)) {
193 for(i = allitems; i; i=i->next)
194 if(plen && strncmp(pattern, i->text, plen)
195 && strstr(i->text, pattern)) {
205 curr = prev = next = sel = item;
210 kpress(XKeyEvent * e) {
218 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
219 if(IsKeypadKey(ksym)) {
220 if(ksym == XK_KP_Enter) {
222 } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
223 ksym = (ksym - XK_KP_0) + XK_0;
226 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
227 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
228 || IsPrivateKeypadKey(ksym))
230 /* first check if a control mask is omitted */
231 if(e->state & ControlMask) {
233 default: /* ignore other control sequences */
260 while(i >= 0 && text[i] == ' ')
262 while(i >= 0 && text[i] != ' ')
270 if(CLEANMASK(e->state) & Mod1Mask) {
295 if(num && !iscntrl((int) buf[0])) {
298 strncat(text, buf, sizeof text);
300 strncpy(text, buf, sizeof text);
317 while(sel && sel->right)
331 if(!(sel && sel->left))
334 if(sel->right == curr) {
352 if((e->state & ShiftMask) && text)
353 fprintf(stdout, "%s", text);
355 fprintf(stdout, "%s", sel->text);
357 fprintf(stdout, "%s", text);
362 if(!(sel && sel->right))
373 strncpy(text, sel->text, sizeof text);
382 static char *maxname = NULL;
384 unsigned int len = 0, max = 0;
388 while(fgets(buf, sizeof buf, stdin)) {
390 if (buf[len - 1] == '\n')
397 new = emalloc(sizeof(Item));
398 new->next = new->left = new->right = NULL;
412 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
413 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
423 main(int argc, char *argv[]) {
427 char *normbg = NORMBGCOLOR;
428 char *normfg = NORMFGCOLOR;
429 char *selbg = SELBGCOLOR;
430 char *selfg = SELFGCOLOR;
434 XModifierKeymap *modmap;
435 XSetWindowAttributes wa;
437 /* command line args */
438 for(i = 1; i < argc; i++)
439 if(!strncmp(argv[i], "-b", 3)) {
442 else if(!strncmp(argv[i], "-fn", 4)) {
443 if(++i < argc) font = argv[i];
445 else if(!strncmp(argv[i], "-nb", 4)) {
446 if(++i < argc) normbg = argv[i];
448 else if(!strncmp(argv[i], "-nf", 4)) {
449 if(++i < argc) normfg = argv[i];
451 else if(!strncmp(argv[i], "-p", 3)) {
452 if(++i < argc) prompt = argv[i];
454 else if(!strncmp(argv[i], "-sb", 4)) {
455 if(++i < argc) selbg = argv[i];
457 else if(!strncmp(argv[i], "-sf", 4)) {
458 if(++i < argc) selfg = argv[i];
460 else if(!strncmp(argv[i], "-v", 3))
461 eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
464 setlocale(LC_CTYPE, "");
465 dpy = XOpenDisplay(0);
467 eprint("dmenu: cannot open display\n");
468 screen = DefaultScreen(dpy);
469 root = RootWindow(dpy, screen);
470 if(isatty(STDIN_FILENO)) {
471 maxname = readstdin();
472 running = grabkeyboard();
474 else { /* prevent keypress loss */
475 running = grabkeyboard();
476 maxname = readstdin();
478 /* init modifier map */
479 modmap = XGetModifierMapping(dpy);
480 for (i = 0; i < 8; i++) {
481 for (j = 0; j < modmap->max_keypermod; j++) {
482 if(modmap->modifiermap[i * modmap->max_keypermod + j]
483 == XKeysymToKeycode(dpy, XK_Num_Lock))
484 numlockmask = (1 << i);
487 XFreeModifiermap(modmap);
489 dc.norm[ColBG] = initcolor(normbg);
490 dc.norm[ColFG] = initcolor(normfg);
491 dc.sel[ColBG] = initcolor(selbg);
492 dc.sel[ColFG] = initcolor(selfg);
495 wa.override_redirect = 1;
496 wa.background_pixmap = ParentRelative;
497 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
498 mw = DisplayWidth(dpy, screen);
499 mh = dc.font.height + 2;
500 win = XCreateWindow(dpy, root, 0,
501 bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
502 DefaultDepth(dpy, screen), CopyFromParent,
503 DefaultVisual(dpy, screen),
504 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
506 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
507 dc.gc = XCreateGC(dpy, root, 0, 0);
508 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
510 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
512 cmdw = textw(maxname);
516 promptw = textw(prompt);
521 XMapRaised(dpy, win);
525 /* main event loop */
526 while(running && !XNextEvent(dpy, &ev))
528 default: /* ignore all crap */
534 if(ev.xexpose.count == 0)
541 itm = allitems->next;
542 free(allitems->text);
547 XFreeFontSet(dpy, dc.font.set);
549 XFreeFont(dpy, dc.font.xfont);
550 XFreePixmap(dpy, dc.drawable);
552 XDestroyWindow(dpy, win);
553 XUngrabKeyboard(dpy, CurrentTime);