dmenu for lunch applications in dwm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

544 lines
12 KiB

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