1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
14 typedef struct Item Item;
16 Item *next; /* traverses all items */
17 Item *left, *right; /* traverses items matching current search pattern */
23 static char text[4096];
24 static char *prompt = NULL;
28 static unsigned int cmdw = 0;
29 static unsigned int promptw = 0;
30 static unsigned int numlockmask = 0;
31 static Bool running = True;
32 static Item *allitems = NULL; /* first of all items */
33 static Item *item = NULL; /* first of pattern matching items */
34 static Item *sel = NULL;
35 static Item *next = NULL;
36 static Item *prev = NULL;
37 static Item *curr = NULL;
47 w = promptw + cmdw + 2 * SPACE;
48 for(next = curr; next; next=next->right) {
49 tw = textw(next->text);
56 w = promptw + cmdw + 2 * SPACE;
57 for(prev = curr; prev && prev->left; prev=prev->left) {
58 tw = textw(prev->left->text);
75 drawtext(NULL, dc.norm);
79 drawtext(prompt, dc.sel);
86 drawtext(text[0] ? text : NULL, dc.norm);
90 drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
92 /* determine maximum items */
93 for(i = curr; i != next; i=i->right) {
94 dc.w = textw(i->text);
97 drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
102 drawtext(next ? ">" : NULL, dc.norm);
104 XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
112 for(len = 1000; len; len--) {
113 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
122 initcolor(const char *colstr) {
123 Colormap cmap = DefaultColormap(dpy, screen);
126 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
127 eprint("error, cannot allocate color '%s'\n", colstr);
132 initfont(const char *fontstr) {
133 char *def, **missing;
136 if(!fontstr || fontstr[0] == '\0')
137 eprint("error, cannot load font: '%s'\n", fontstr);
140 XFreeFontSet(dpy, dc.font.set);
141 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
143 XFreeStringList(missing);
145 XFontSetExtents *font_extents;
146 XFontStruct **xfonts;
148 dc.font.ascent = dc.font.descent = 0;
149 font_extents = XExtentsOfFontSet(dc.font.set);
150 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
151 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
152 if(dc.font.ascent < (*xfonts)->ascent)
153 dc.font.ascent = (*xfonts)->ascent;
154 if(dc.font.descent < (*xfonts)->descent)
155 dc.font.descent = (*xfonts)->descent;
161 XFreeFont(dpy, dc.font.xfont);
162 dc.font.xfont = NULL;
163 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
164 eprint("error, cannot load font: '%s'\n", fontstr);
165 dc.font.ascent = dc.font.xfont->ascent;
166 dc.font.descent = dc.font.xfont->descent;
168 dc.font.height = dc.font.ascent + dc.font.descent;
172 strido(const char *text, const char *pattern) {
173 for(; *text && *pattern; text++)
174 if (*text == *pattern)
180 match(char *pattern) {
186 plen = strlen(pattern);
189 for(i = allitems; i; i=i->next)
190 if(!plen || !strncmp(pattern, i->text, plen)) {
200 for(i = allitems; i; i=i->next)
201 if(plen && strncmp(pattern, i->text, plen)
202 && strstr(i->text, pattern)) {
212 for(i = allitems; i; i=i->next)
213 if(plen && strncmp(pattern, i->text, plen)
214 && !strstr(i->text, pattern)
215 && strido(i->text,pattern)) {
225 curr = prev = next = sel = item;
230 kpress(XKeyEvent * e) {
238 num = XLookupString(e, buf, sizeof buf, &ksym, 0);
239 if(IsKeypadKey(ksym)) {
240 if(ksym == XK_KP_Enter) {
242 } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
243 ksym = (ksym - XK_KP_0) + XK_0;
246 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
247 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
248 || IsPrivateKeypadKey(ksym))
250 /* first check if a control mask is omitted */
251 if(e->state & ControlMask) {
253 default: /* ignore other control sequences */
280 while(i >= 0 && text[i] == ' ')
282 while(i >= 0 && text[i] != ' ')
290 if(CLEANMASK(e->state) & Mod1Mask) {
315 if(num && !iscntrl((int) buf[0])) {
318 strncat(text, buf, sizeof text);
320 strncpy(text, buf, sizeof text);
337 while(sel && sel->right)
351 if(!(sel && sel->left))
354 if(sel->right == curr) {
372 if((e->state & ShiftMask) && text)
373 fprintf(stdout, "%s", text);
375 fprintf(stdout, "%s", sel->text);
377 fprintf(stdout, "%s", text);
382 if(!(sel && sel->right))
393 strncpy(text, sel->text, sizeof text);
402 static char *maxname = NULL;
404 unsigned int len = 0, max = 0;
408 while(fgets(buf, sizeof buf, stdin)) {
410 if (buf[len - 1] == '\n')
417 new = emalloc(sizeof(Item));
418 new->next = new->left = new->right = NULL;
432 eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
433 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
443 main(int argc, char *argv[]) {
447 char *normbg = NORMBGCOLOR;
448 char *normfg = NORMFGCOLOR;
449 char *selbg = SELBGCOLOR;
450 char *selfg = SELFGCOLOR;
454 XModifierKeymap *modmap;
455 XSetWindowAttributes wa;
457 /* command line args */
458 for(i = 1; i < argc; i++)
459 if(!strcmp(argv[i], "-b")) {
462 else if(!strcmp(argv[i], "-fn")) {
463 if(++i < argc) font = argv[i];
465 else if(!strcmp(argv[i], "-nb")) {
466 if(++i < argc) normbg = argv[i];
468 else if(!strcmp(argv[i], "-nf")) {
469 if(++i < argc) normfg = argv[i];
471 else if(!strcmp(argv[i], "-p")) {
472 if(++i < argc) prompt = argv[i];
474 else if(!strcmp(argv[i], "-sb")) {
475 if(++i < argc) selbg = argv[i];
477 else if(!strcmp(argv[i], "-sf")) {
478 if(++i < argc) selfg = argv[i];
480 else if(!strcmp(argv[i], "-v"))
481 eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
484 setlocale(LC_CTYPE, "");
485 dpy = XOpenDisplay(0);
487 eprint("dmenu: cannot open display\n");
488 screen = DefaultScreen(dpy);
489 root = RootWindow(dpy, screen);
490 if(isatty(STDIN_FILENO)) {
491 maxname = readstdin();
492 running = grabkeyboard();
494 else { /* prevent keypress loss */
495 running = grabkeyboard();
496 maxname = readstdin();
498 /* init modifier map */
499 modmap = XGetModifierMapping(dpy);
500 for (i = 0; i < 8; i++) {
501 for (j = 0; j < modmap->max_keypermod; j++) {
502 if(modmap->modifiermap[i * modmap->max_keypermod + j]
503 == XKeysymToKeycode(dpy, XK_Num_Lock))
504 numlockmask = (1 << i);
507 XFreeModifiermap(modmap);
509 dc.norm[ColBG] = initcolor(normbg);
510 dc.norm[ColFG] = initcolor(normfg);
511 dc.sel[ColBG] = initcolor(selbg);
512 dc.sel[ColFG] = initcolor(selfg);
515 wa.override_redirect = 1;
516 wa.background_pixmap = ParentRelative;
517 wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
518 mw = DisplayWidth(dpy, screen);
519 mh = dc.font.height + 2;
520 win = XCreateWindow(dpy, root, 0,
521 bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
522 DefaultDepth(dpy, screen), CopyFromParent,
523 DefaultVisual(dpy, screen),
524 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
526 dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
527 dc.gc = XCreateGC(dpy, root, 0, 0);
528 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
530 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
532 cmdw = textw(maxname);
536 promptw = textw(prompt);
541 XMapRaised(dpy, win);
545 /* main event loop */
546 while(running && !XNextEvent(dpy, &ev))
548 default: /* ignore all crap */
554 if(ev.xexpose.count == 0)
561 itm = allitems->next;
562 free(allitems->text);
567 XFreeFontSet(dpy, dc.font.set);
569 XFreeFont(dpy, dc.font.xfont);
570 XFreePixmap(dpy, dc.drawable);
572 XDestroyWindow(dpy, win);
573 XUngrabKeyboard(dpy, CurrentTime);