1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
13 typedef struct Item Item;
16 Item *next; /* traverses all items */
17 Item *left, *right; /* traverses items matching current search pattern */
20 /* forward declarations */
21 static void appenditem(Item *i, Item **list, Item **last);
22 static void calcoffsetsh(void);
23 static void calcoffsetsv(void);
24 static char *cistrstr(const char *s, const char *sub);
25 static void cleanup(void);
26 static void dinput(void);
27 static void drawitem(char *s, unsigned long col[ColLast]);
28 static void drawmenuh(void);
29 static void drawmenuv(void);
30 static void match(void);
31 static void readstdin(void);
34 static char **argp = NULL;
35 static char *maxname = NULL;
36 static unsigned int cmdw = 0;
37 static unsigned int lines = 0;
38 static Item *allitems = NULL; /* first of all items */
39 static Item *item = NULL; /* first of pattern matching items */
40 static Item *sel = NULL;
41 static Item *next = NULL;
42 static Item *prev = NULL;
43 static Item *curr = NULL;
44 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
45 static char *(*fstrstr)(const char *, const char *) = strstr;
46 static void (*calcoffsets)(void) = calcoffsetsh;
49 appenditem(Item *i, Item **list, Item **last) {
63 w = promptw + cmdw + textw(&dc, "<") + textw(&dc, ">");
64 for(x = w, next = curr; next; next = next->right)
65 if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
67 for(x = w, prev = curr; prev && prev->left; prev = prev->left)
68 if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
77 for(i = 0; i < lines && next; i++)
79 mh = (dc.font.height + 2) * (i + 1);
80 for(i = 0; i < lines && prev && prev->left; i++)
85 cistrstr(const char *s, const char *sub) {
91 if((c = tolower(*sub++)) != '\0') {
95 if((csub = *s++) == '\0')
98 while(tolower(csub) != c);
100 while(strncasecmp(s, sub, len) != 0);
111 itm = allitems->next;
112 free(allitems->text);
117 XDestroyWindow(dpy, win);
118 XUngrabKeyboard(dpy, CurrentTime);
127 execvp("dinput", argp);
128 eprint("cannot exec dinput\n");
137 drawbox(&dc, normcol);
138 dc.h = dc.font.height + 2;
139 dc.y = topbar ? 0 : mh - dc.h;
143 drawtext(&dc, prompt, selcol);
148 if(cmdw && item && lines == 0)
150 drawtext(&dc, text, normcol);
155 commitdraw(&dc, win);
159 drawitem(char *s, unsigned long col[ColLast]) {
161 drawtext(&dc, s, col);
169 dc.w = textw(&dc, "<");
170 drawtext(&dc, curr->left ? "<" : NULL, normcol);
172 for(i = curr; i != next; i = i->right) {
173 dc.w = MIN(textw(&dc, i->text), mw / 3);
174 drawitem(i->text, (sel == i) ? selcol : normcol);
177 dc.w = textw(&dc, ">");
179 drawtext(&dc, next ? ">" : NULL, normcol);
185 XWindowAttributes wa;
187 dc.y = topbar ? dc.h : 0;
189 for(i = curr; i != next; i = i->right) {
190 drawitem(i->text, (sel == i) ? selcol : normcol);
193 if(!XGetWindowAttributes(dpy, win, &wa))
194 eprint("cannot get window attributes");
195 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
199 kpress(XKeyEvent *e) {
200 char buf[sizeof text];
206 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
207 if(ksym == XK_KP_Enter)
209 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
210 ksym = (ksym - XK_KP_0) + XK_0;
211 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
212 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
213 || IsPrivateKeypadKey(ksym))
215 /* first check if a control mask is omitted */
216 if(e->state & ControlMask) {
217 switch(tolower(ksym)) {
259 while(i-- > 0 && text[i] == ' ');
260 while(i-- > 0 && text[i] != ' ');
268 num = MIN(num, sizeof text);
269 if(num && !iscntrl((int) buf[0])) {
270 memcpy(text + len, buf, num + 1);
278 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
288 while(sel && sel->right)
299 if(!sel || !sel->left)
302 if(sel->right == curr) {
320 if(e->state & ShiftMask)
322 fprintf(stdout, "%s", sel ? sel->text : text);
327 if(!sel || !sel->right)
337 strncpy(text, sel->text, sizeof text);
347 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
350 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
351 for(i = allitems; i; i = i->next)
352 if(!fstrncmp(text, i->text, len + 1))
353 appenditem(i, &lexact, &exactend);
354 else if(!fstrncmp(text, i->text, len))
355 appenditem(i, &lprefix, &prefixend);
356 else if(fstrstr(i->text, text))
357 appenditem(i, &lsubstr, &substrend);
364 itemend->right = lprefix;
365 lprefix->left = itemend;
373 itemend->right = lsubstr;
374 lsubstr->left = itemend;
379 curr = prev = next = sel = item;
385 char *p, buf[sizeof text];
386 unsigned int len = 0, max = 0;
390 while(fgets(buf, sizeof buf, stdin)) {
392 if(buf[len-1] == '\n')
394 if(!(p = strdup(buf)))
395 eprint("cannot strdup %u bytes\n", len);
396 if((max = MAX(max, len)) == len)
398 if(!(new = malloc(sizeof *new)))
399 eprint("cannot malloc %u bytes\n", sizeof *new);
400 new->next = new->left = new->right = NULL;
411 main(int argc, char *argv[]) {
414 /* command line args */
416 for(i = 1; i < argc; i++)
417 if(!strcmp(argv[i], "-i")) {
418 fstrncmp = strncasecmp;
421 else if(!strcmp(argv[i], "-b"))
423 else if(!strcmp(argv[i], "-l")) {
424 if(++i < argc) lines = atoi(argv[i]);
426 calcoffsets = calcoffsetsv;
428 else if(!strcmp(argv[i], "-fn")) {
429 if(++i < argc) font = argv[i];
431 else if(!strcmp(argv[i], "-nb")) {
432 if(++i < argc) normbgcolor = argv[i];
434 else if(!strcmp(argv[i], "-nf")) {
435 if(++i < argc) normfgcolor = argv[i];
437 else if(!strcmp(argv[i], "-p")) {
438 if(++i < argc) prompt = argv[i];
440 else if(!strcmp(argv[i], "-sb")) {
441 if(++i < argc) selbgcolor = argv[i];
443 else if(!strcmp(argv[i], "-sf")) {
444 if(++i < argc) selfgcolor = argv[i];
446 else if(!strcmp(argv[i], "-v")) {
447 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
451 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
452 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
455 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
456 fprintf(stderr, "dmenu: warning: no locale support\n");
457 if(!(dpy = XOpenDisplay(NULL)))
458 eprint("cannot open display\n");
459 if(atexit(&cleanup) != 0)
460 eprint("cannot register cleanup\n");
461 screen = DefaultScreen(dpy);
462 root = RootWindow(dpy, screen);
463 if(!(argp = malloc(sizeof *argp * (argc+2))))
464 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
465 memcpy(argp + 2, argv + 1, sizeof *argp * argc);
471 cmdw = MIN(textw(&dc, maxname), mw / 3);