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 drawmenuh(void);
28 static void drawmenuv(void);
29 static void match(void);
30 static void readstdin(void);
33 static char **argp = NULL;
34 static char *maxname = NULL;
35 static unsigned int cmdw = 0;
36 static unsigned int lines = 0;
37 static Item *allitems = NULL; /* first of all items */
38 static Item *item = NULL; /* first of pattern matching items */
39 static Item *sel = NULL;
40 static Item *next = NULL;
41 static Item *prev = NULL;
42 static Item *curr = NULL;
43 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
44 static char *(*fstrstr)(const char *, const char *) = strstr;
45 static void (*calcoffsets)(void) = calcoffsetsh;
48 appenditem(Item *i, Item **list, Item **last) {
62 w = promptw + cmdw + textw(&dc, "<") + textw(&dc, ">");
63 for(x = w, next = curr; next; next = next->right)
64 if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
66 for(x = w, prev = curr; prev && prev->left; prev = prev->left)
67 if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
76 for(i = 0; i < lines && next; i++)
78 mh = (dc.font.height + 2) * (i + 1);
79 for(i = 0; i < lines && prev && prev->left; i++)
84 cistrstr(const char *s, const char *sub) {
90 if((c = tolower(*sub++)) != '\0') {
94 if((csub = *s++) == '\0')
97 while(tolower(csub) != c);
99 while(strncasecmp(s, sub, len) != 0);
110 itm = allitems->next;
111 free(allitems->text);
116 XDestroyWindow(dpy, win);
117 XUngrabKeyboard(dpy, CurrentTime);
126 execvp("dinput", argp);
127 eprint("cannot exec dinput\n");
136 drawtext(&dc, NULL, normcol);
137 dc.h = dc.font.height + 2;
138 dc.y = topbar ? 0 : mh - dc.h;
142 drawtext(&dc, prompt, selcol);
147 if(cmdw && item && lines == 0)
149 drawtext(&dc, text, normcol);
154 commitdraw(&dc, win);
162 dc.w = textw(&dc, "<");
163 drawtext(&dc, curr->left ? "<" : NULL, normcol);
165 for(i = curr; i != next; i = i->right) {
166 dc.w = MIN(textw(&dc, i->text), mw / 3);
167 drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
170 dc.w = textw(&dc, ">");
172 drawtext(&dc, next ? ">" : NULL, normcol);
178 XWindowAttributes wa;
180 dc.y = topbar ? dc.h : 0;
182 for(i = curr; i != next; i = i->right) {
183 drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
186 if(!XGetWindowAttributes(dpy, win, &wa))
187 eprint("cannot get window attributes");
188 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
192 kpress(XKeyEvent *e) {
193 char buf[sizeof text];
199 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
200 if(ksym == XK_KP_Enter)
202 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
203 ksym = (ksym - XK_KP_0) + XK_0;
204 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
205 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
206 || IsPrivateKeypadKey(ksym))
208 /* first check if a control mask is omitted */
209 if(e->state & ControlMask) {
210 switch(tolower(ksym)) {
252 while(i-- > 0 && text[i] == ' ');
253 while(i-- > 0 && text[i] != ' ');
261 num = MIN(num, sizeof text);
262 if(num && !iscntrl((int) buf[0])) {
263 memcpy(text + len, buf, num + 1);
271 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
281 while(sel && sel->right)
292 if(!sel || !sel->left)
295 if(sel->right == curr) {
313 if(e->state & ShiftMask)
315 fprintf(stdout, "%s", sel ? sel->text : text);
320 if(!sel || !sel->right)
330 strncpy(text, sel->text, sizeof text);
340 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
343 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
344 for(i = allitems; i; i = i->next)
345 if(!fstrncmp(text, i->text, len + 1))
346 appenditem(i, &lexact, &exactend);
347 else if(!fstrncmp(text, i->text, len))
348 appenditem(i, &lprefix, &prefixend);
349 else if(fstrstr(i->text, text))
350 appenditem(i, &lsubstr, &substrend);
357 itemend->right = lprefix;
358 lprefix->left = itemend;
366 itemend->right = lsubstr;
367 lsubstr->left = itemend;
372 curr = prev = next = sel = item;
378 char *p, buf[sizeof text];
379 unsigned int len = 0, max = 0;
383 while(fgets(buf, sizeof buf, stdin)) {
385 if(buf[len-1] == '\n')
387 if(!(p = strdup(buf)))
388 eprint("cannot strdup %u bytes\n", len);
389 if((max = MAX(max, len)) == len)
391 if(!(new = malloc(sizeof *new)))
392 eprint("cannot malloc %u bytes\n", sizeof *new);
393 new->next = new->left = new->right = NULL;
404 main(int argc, char *argv[]) {
407 /* command line args */
409 for(i = 1; i < argc; i++)
410 if(!strcmp(argv[i], "-i")) {
411 fstrncmp = strncasecmp;
414 else if(!strcmp(argv[i], "-b"))
416 else if(!strcmp(argv[i], "-l")) {
417 if(++i < argc) lines = atoi(argv[i]);
419 calcoffsets = calcoffsetsv;
421 else if(!strcmp(argv[i], "-fn")) {
422 if(++i < argc) font = argv[i];
424 else if(!strcmp(argv[i], "-nb")) {
425 if(++i < argc) normbgcolor = argv[i];
427 else if(!strcmp(argv[i], "-nf")) {
428 if(++i < argc) normfgcolor = argv[i];
430 else if(!strcmp(argv[i], "-p")) {
431 if(++i < argc) prompt = argv[i];
433 else if(!strcmp(argv[i], "-sb")) {
434 if(++i < argc) selbgcolor = argv[i];
436 else if(!strcmp(argv[i], "-sf")) {
437 if(++i < argc) selfgcolor = argv[i];
439 else if(!strcmp(argv[i], "-v")) {
440 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
444 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
445 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
448 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
449 fprintf(stderr, "dmenu: warning: no locale support\n");
450 if(!(dpy = XOpenDisplay(NULL)))
451 eprint("cannot open display\n");
452 if(atexit(&cleanup) != 0)
453 eprint("cannot register cleanup\n");
454 screen = DefaultScreen(dpy);
455 root = RootWindow(dpy, screen);
456 if(!(argp = malloc(sizeof *argp * (argc+2))))
457 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
458 memcpy(argp + 2, argv + 1, sizeof *argp * argc);
464 cmdw = MIN(textw(&dc, maxname), mw / 3);