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.

548 lines
12 KiB

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