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.

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