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.

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