Xinqi Bao's Git

fixed config.mk dep
[dmenu.git] / dmenu.c
1 /* See LICENSE file for copyright and license details. */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <X11/Xlib.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xutil.h>
10 #ifdef XINERAMA
11 #include <X11/extensions/Xinerama.h>
12 #endif
13 #include "draw.h"
14
15 #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
16 #define MIN(a,b) ((a) < (b) ? (a) : (b))
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
18
19 typedef struct Item Item;
20 struct Item {
21 char *text;
22 Item *next; /* traverses all items */
23 Item *left, *right; /* traverses matching items */
24 };
25
26 static void appenditem(Item *item, Item **list, Item **last);
27 static void calcoffsets(void);
28 static void drawmenu(void);
29 static char *fstrstr(const char *s, const char *sub);
30 static void grabkeyboard(void);
31 static void insert(const char *s, ssize_t n);
32 static void keypress(XKeyEvent *ev);
33 static void match(void);
34 static size_t nextrune(int incr);
35 static void paste(void);
36 static void readstdin(void);
37 static void run(void);
38 static void setup(void);
39 static void usage(void);
40
41 static char text[BUFSIZ];
42 static int bh, mw, mh;
43 static int inputw = 0;
44 static int lines = 0;
45 static int monitor = -1;
46 static int promptw;
47 static size_t cursor = 0;
48 static const char *font = NULL;
49 static const char *prompt = NULL;
50 static const char *normbgcolor = "#cccccc";
51 static const char *normfgcolor = "#000000";
52 static const char *selbgcolor = "#0066ff";
53 static const char *selfgcolor = "#ffffff";
54 static unsigned long normcol[ColLast];
55 static unsigned long selcol[ColLast];
56 static Atom utf8;
57 static Bool topbar = True;
58 static DC *dc;
59 static Item *items = NULL;
60 static Item *matches, *sel;
61 static Item *prev, *curr, *next;
62 static Window root, win;
63
64 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
65
66 void
67 appenditem(Item *item, Item **list, Item **last) {
68 if(!*last)
69 *list = item;
70 else
71 (*last)->right = item;
72 item->left = *last;
73 item->right = NULL;
74 *last = item;
75 }
76
77 void
78 calcoffsets(void) {
79 unsigned int i, n;
80
81 if(lines > 0)
82 n = lines * bh;
83 else
84 n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
85
86 for(i = 0, next = curr; next; next = next->right)
87 if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
88 break;
89 for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
90 if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
91 break;
92 }
93
94 void
95 drawmenu(void) {
96 int curpos;
97 Item *item;
98
99 dc->x = 0;
100 dc->y = 0;
101 dc->h = bh;
102 drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
103
104 if(prompt) {
105 dc->w = promptw;
106 drawtext(dc, prompt, selcol);
107 dc->x = dc->w;
108 }
109 dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
110 drawtext(dc, text, normcol);
111 if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
112 drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
113
114 if(lines > 0) {
115 dc->w = mw - dc->x;
116 for(item = curr; item != next; item = item->right) {
117 dc->y += dc->h;
118 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
119 }
120 }
121 else if(matches) {
122 dc->x += inputw;
123 dc->w = textw(dc, "<");
124 if(curr->left)
125 drawtext(dc, "<", normcol);
126 for(item = curr; item != next; item = item->right) {
127 dc->x += dc->w;
128 dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
129 drawtext(dc, item->text, (item == sel) ? selcol : normcol);
130 }
131 dc->w = textw(dc, ">");
132 dc->x = mw - dc->w;
133 if(next)
134 drawtext(dc, ">", normcol);
135 }
136 mapdc(dc, win, mw, mh);
137 }
138
139 char *
140 fstrstr(const char *s, const char *sub) {
141 size_t len;
142
143 for(len = strlen(sub); *s; s++)
144 if(!fstrncmp(s, sub, len))
145 return (char *)s;
146 return NULL;
147 }
148
149 void
150 grabkeyboard(void) {
151 int i;
152
153 for(i = 0; i < 1000; i++) {
154 if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
155 return;
156 usleep(1000);
157 }
158 eprintf("cannot grab keyboard\n");
159 }
160
161 void
162 insert(const char *s, ssize_t n) {
163 if(strlen(text) + n > sizeof text - 1)
164 return;
165 memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
166 if(n > 0)
167 memcpy(text + cursor, s, n);
168 cursor += n;
169 match();
170 }
171
172 void
173 keypress(XKeyEvent *ev) {
174 char buf[32];
175 size_t len;
176 KeySym ksym;
177
178 len = strlen(text);
179 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
180 if(ev->state & ControlMask) {
181 switch(tolower(ksym)) {
182 default:
183 return;
184 case XK_a:
185 ksym = XK_Home;
186 break;
187 case XK_b:
188 ksym = XK_Left;
189 break;
190 case XK_c:
191 ksym = XK_Escape;
192 break;
193 case XK_d:
194 ksym = XK_Delete;
195 break;
196 case XK_e:
197 ksym = XK_End;
198 break;
199 case XK_f:
200 ksym = XK_Right;
201 break;
202 case XK_h:
203 ksym = XK_BackSpace;
204 break;
205 case XK_i:
206 ksym = XK_Tab;
207 break;
208 case XK_j:
209 ksym = XK_Return;
210 break;
211 case XK_k: /* delete right */
212 text[cursor] = '\0';
213 match();
214 break;
215 case XK_n:
216 ksym = XK_Down;
217 break;
218 case XK_p:
219 ksym = XK_Up;
220 break;
221 case XK_u: /* delete left */
222 insert(NULL, 0 - cursor);
223 break;
224 case XK_w: /* delete word */
225 while(cursor > 0 && text[nextrune(-1)] == ' ')
226 insert(NULL, nextrune(-1) - cursor);
227 while(cursor > 0 && text[nextrune(-1)] != ' ')
228 insert(NULL, nextrune(-1) - cursor);
229 break;
230 case XK_y: /* paste selection */
231 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
232 return;
233 }
234 }
235 switch(ksym) {
236 default:
237 if(!iscntrl(*buf))
238 insert(buf, strlen(buf));
239 break;
240 case XK_Delete:
241 if(cursor == len)
242 return;
243 cursor = nextrune(+1);
244 case XK_BackSpace:
245 if(cursor > 0)
246 insert(NULL, nextrune(-1) - cursor);
247 break;
248 case XK_End:
249 if(cursor < len) {
250 cursor = len;
251 break;
252 }
253 while(next) {
254 sel = curr = next;
255 calcoffsets();
256 }
257 while(sel && sel->right)
258 sel = sel->right;
259 break;
260 case XK_Escape:
261 exit(EXIT_FAILURE);
262 case XK_Home:
263 if(sel == matches) {
264 cursor = 0;
265 break;
266 }
267 sel = curr = matches;
268 calcoffsets();
269 break;
270 case XK_Left:
271 if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
272 cursor = nextrune(-1);
273 break;
274 }
275 else if(lines > 0)
276 return;
277 case XK_Up:
278 if(sel && sel->left && (sel = sel->left)->right == curr) {
279 curr = prev;
280 calcoffsets();
281 }
282 break;
283 case XK_Next:
284 if(!next)
285 return;
286 sel = curr = next;
287 calcoffsets();
288 break;
289 case XK_Prior:
290 if(!prev)
291 return;
292 sel = curr = prev;
293 calcoffsets();
294 break;
295 case XK_Return:
296 case XK_KP_Enter:
297 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
298 fflush(stdout);
299 exit(EXIT_SUCCESS);
300 case XK_Right:
301 if(cursor < len) {
302 cursor = nextrune(+1);
303 break;
304 }
305 else if(lines > 0)
306 return;
307 case XK_Down:
308 if(sel && sel->right && (sel = sel->right) == next) {
309 curr = next;
310 calcoffsets();
311 }
312 break;
313 case XK_Tab:
314 if(!sel)
315 return;
316 strncpy(text, sel->text, sizeof text);
317 cursor = strlen(text);
318 match();
319 break;
320 }
321 drawmenu();
322 }
323
324 void
325 match(void) {
326 size_t len;
327 Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
328
329 len = strlen(text);
330 matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
331 for(item = items; item; item = item->next)
332 if(!fstrncmp(text, item->text, len + 1))
333 appenditem(item, &lexact, &exactend);
334 else if(!fstrncmp(text, item->text, len))
335 appenditem(item, &lprefix, &prefixend);
336 else if(fstrstr(item->text, text))
337 appenditem(item, &lsubstr, &substrend);
338
339 if(lexact) {
340 matches = lexact;
341 itemend = exactend;
342 }
343 if(lprefix) {
344 if(itemend) {
345 itemend->right = lprefix;
346 lprefix->left = itemend;
347 }
348 else
349 matches = lprefix;
350 itemend = prefixend;
351 }
352 if(lsubstr) {
353 if(itemend) {
354 itemend->right = lsubstr;
355 lsubstr->left = itemend;
356 }
357 else
358 matches = lsubstr;
359 }
360 curr = prev = next = sel = matches;
361 calcoffsets();
362 }
363
364 size_t
365 nextrune(int incr) {
366 size_t n, len;
367
368 len = strlen(text);
369 for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
370 return n;
371 }
372
373 void
374 paste(void) {
375 char *p, *q;
376 int di;
377 unsigned long dl;
378 Atom da;
379
380 XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
381 utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
382 insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
383 XFree(p);
384 drawmenu();
385 }
386
387 void
388 readstdin(void) {
389 char buf[sizeof text], *p;
390 Item *item, **end;
391
392 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
393 if((p = strchr(buf, '\n')))
394 *p = '\0';
395 if(!(item = malloc(sizeof *item)))
396 eprintf("cannot malloc %u bytes\n", sizeof *item);
397 if(!(item->text = strdup(buf)))
398 eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
399 item->next = item->left = item->right = NULL;
400 inputw = MAX(inputw, textw(dc, item->text));
401 }
402 }
403
404 void
405 run(void) {
406 XEvent ev;
407
408 while(!XNextEvent(dc->dpy, &ev))
409 switch(ev.type) {
410 case Expose:
411 if(ev.xexpose.count == 0)
412 drawmenu();
413 break;
414 case KeyPress:
415 keypress(&ev.xkey);
416 break;
417 case SelectionNotify:
418 if(ev.xselection.property == utf8)
419 paste();
420 break;
421 case VisibilityNotify:
422 if(ev.xvisibility.state != VisibilityUnobscured)
423 XRaiseWindow(dc->dpy, win);
424 break;
425 }
426 }
427
428 void
429 setup(void) {
430 int x, y, screen;
431 XSetWindowAttributes wa;
432 #ifdef XINERAMA
433 int n;
434 XineramaScreenInfo *info;
435 #endif
436
437 screen = DefaultScreen(dc->dpy);
438 root = RootWindow(dc->dpy, screen);
439 utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
440
441 normcol[ColBG] = getcolor(dc, normbgcolor);
442 normcol[ColFG] = getcolor(dc, normfgcolor);
443 selcol[ColBG] = getcolor(dc, selbgcolor);
444 selcol[ColFG] = getcolor(dc, selfgcolor);
445
446 /* menu geometry */
447 bh = dc->font.height + 2;
448 lines = MAX(lines, 0);
449 mh = (lines + 1) * bh;
450 #ifdef XINERAMA
451 if((info = XineramaQueryScreens(dc->dpy, &n))) {
452 int i, di;
453 unsigned int du;
454 Window dw;
455
456 XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
457 for(i = 0; i < n; i++)
458 if((monitor == info[i].screen_number)
459 || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
460 break;
461 x = info[i].x_org;
462 y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
463 mw = info[i].width;
464 XFree(info);
465 }
466 else
467 #endif
468 {
469 x = 0;
470 y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
471 mw = DisplayWidth(dc->dpy, screen);
472 }
473 /* menu window */
474 wa.override_redirect = True;
475 wa.background_pixmap = ParentRelative;
476 wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
477 win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
478 DefaultDepth(dc->dpy, screen), CopyFromParent,
479 DefaultVisual(dc->dpy, screen),
480 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
481
482 grabkeyboard();
483 resizedc(dc, mw, mh);
484 inputw = MIN(inputw, mw/3);
485 promptw = prompt ? textw(dc, prompt) : 0;
486 XMapRaised(dc->dpy, win);
487 text[0] = '\0';
488 match();
489 }
490
491 void
492 usage(void) {
493 fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
494 " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
495 exit(EXIT_FAILURE);
496 }
497
498 int
499 main(int argc, char *argv[]) {
500 int i;
501
502 progname = "dmenu";
503 for(i = 1; i < argc; i++)
504 /* single flags */
505 if(!strcmp(argv[i], "-v")) {
506 fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
507 exit(EXIT_SUCCESS);
508 }
509 else if(!strcmp(argv[i], "-b"))
510 topbar = False;
511 else if(!strcmp(argv[i], "-i"))
512 fstrncmp = strncasecmp;
513 else if(i == argc-1)
514 usage();
515 /* double flags */
516 else if(!strcmp(argv[i], "-l"))
517 lines = atoi(argv[++i]);
518 else if(!strcmp(argv[i], "-m"))
519 monitor = atoi(argv[++i]);
520 else if(!strcmp(argv[i], "-p"))
521 prompt = argv[++i];
522 else if(!strcmp(argv[i], "-fn"))
523 font = argv[++i];
524 else if(!strcmp(argv[i], "-nb"))
525 normbgcolor = argv[++i];
526 else if(!strcmp(argv[i], "-nf"))
527 normfgcolor = argv[++i];
528 else if(!strcmp(argv[i], "-sb"))
529 selbgcolor = argv[++i];
530 else if(!strcmp(argv[i], "-sf"))
531 selfgcolor = argv[++i];
532 else
533 usage();
534
535 dc = initdc();
536 initfont(dc, font);
537 readstdin();
538 setup();
539 run();
540
541 return EXIT_FAILURE; /* should not reach */
542 }