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
18 years ago
18 years ago
14 years ago
16 years ago
13 years ago
14 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
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
17 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 copynext 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, *sel;
  55. static Item *prev, *curr, *next;
  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. while(next) {
  287. sel = curr = next;
  288. calcoffsets();
  289. }
  290. while(sel && sel->right)
  291. sel = sel->right;
  292. break;
  293. case XK_Escape:
  294. exit(EXIT_FAILURE);
  295. case XK_Home:
  296. if(sel == matches) {
  297. cursor = 0;
  298. break;
  299. }
  300. sel = curr = matches;
  301. calcoffsets();
  302. break;
  303. case XK_Left:
  304. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  305. cursor = nextrune(-1);
  306. break;
  307. }
  308. else if(lines > 0)
  309. return;
  310. case XK_Up:
  311. if(sel && sel->left && (sel = sel->left)->right == curr) {
  312. curr = prev;
  313. calcoffsets();
  314. }
  315. break;
  316. case XK_Next:
  317. if(!next)
  318. return;
  319. sel = curr = next;
  320. calcoffsets();
  321. break;
  322. case XK_Prior:
  323. if(!prev)
  324. return;
  325. sel = curr = prev;
  326. calcoffsets();
  327. break;
  328. case XK_Return:
  329. case XK_KP_Enter:
  330. fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
  331. exit(EXIT_SUCCESS);
  332. case XK_Right:
  333. if(cursor < len) {
  334. cursor = nextrune(+1);
  335. break;
  336. }
  337. else if(lines > 0)
  338. return;
  339. case XK_Down:
  340. if(sel && sel->right && (sel = sel->right) == next) {
  341. curr = next;
  342. calcoffsets();
  343. }
  344. break;
  345. case XK_Tab:
  346. if(!sel)
  347. return;
  348. strncpy(text, sel->text, sizeof text);
  349. cursor = strlen(text);
  350. match();
  351. break;
  352. }
  353. drawmenu();
  354. }
  355. void
  356. match(void) {
  357. size_t len;
  358. Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  359. len = strlen(text);
  360. matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  361. for(item = items; item && item->text; item++)
  362. if(!fstrncmp(text, item->text, len + 1))
  363. appenditem(item, &lexact, &exactend);
  364. else if(!fstrncmp(text, item->text, len))
  365. appenditem(item, &lprefix, &prefixend);
  366. else if(fstrstr(item->text, text))
  367. appenditem(item, &lsubstr, &substrend);
  368. if(lexact) {
  369. matches = lexact;
  370. itemend = exactend;
  371. }
  372. if(lprefix) {
  373. if(itemend) {
  374. itemend->right = lprefix;
  375. lprefix->left = itemend;
  376. }
  377. else
  378. matches = lprefix;
  379. itemend = prefixend;
  380. }
  381. if(lsubstr) {
  382. if(itemend) {
  383. itemend->right = lsubstr;
  384. lsubstr->left = itemend;
  385. }
  386. else
  387. matches = lsubstr;
  388. }
  389. curr = sel = matches;
  390. calcoffsets();
  391. }
  392. size_t
  393. nextrune(int incr) {
  394. size_t n, len;
  395. len = strlen(text);
  396. for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
  397. return n;
  398. }
  399. void
  400. paste(void) {
  401. char *p, *q;
  402. int di;
  403. unsigned long dl;
  404. Atom da;
  405. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  406. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  407. insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
  408. XFree(p);
  409. drawmenu();
  410. }
  411. void
  412. readstdin(void) {
  413. char buf[sizeof text], *p;
  414. size_t i, size = 0;
  415. for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
  416. if(i+1 == size / sizeof *items || !items)
  417. if(!(items = realloc(items, (size += BUFSIZ))))
  418. eprintf("cannot realloc %u bytes:", size);
  419. if((p = strchr(buf, '\n')))
  420. *p = '\0';
  421. if(!(items[i].text = strdup(buf)))
  422. eprintf("cannot strdup %u bytes:", strlen(buf)+1);
  423. inputw = MAX(inputw, textw(dc, items[i].text));
  424. }
  425. }
  426. void
  427. run(void) {
  428. XEvent ev;
  429. while(!XNextEvent(dc->dpy, &ev))
  430. switch(ev.type) {
  431. case Expose:
  432. if(ev.xexpose.count == 0)
  433. drawmenu();
  434. break;
  435. case KeyPress:
  436. keypress(&ev.xkey);
  437. break;
  438. case SelectionNotify:
  439. if(ev.xselection.property == utf8)
  440. paste();
  441. break;
  442. case VisibilityNotify:
  443. if(ev.xvisibility.state != VisibilityUnobscured)
  444. XRaiseWindow(dc->dpy, win);
  445. break;
  446. }
  447. }
  448. void
  449. setup(void) {
  450. int x, y, screen;
  451. XSetWindowAttributes wa;
  452. #ifdef XINERAMA
  453. int n;
  454. XineramaScreenInfo *info;
  455. #endif
  456. screen = DefaultScreen(dc->dpy);
  457. root = RootWindow(dc->dpy, screen);
  458. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  459. normcol[ColBG] = getcolor(dc, normbgcolor);
  460. normcol[ColFG] = getcolor(dc, normfgcolor);
  461. selcol[ColBG] = getcolor(dc, selbgcolor);
  462. selcol[ColFG] = getcolor(dc, selfgcolor);
  463. /* menu geometry */
  464. bh = dc->font.height + 2;
  465. lines = MAX(lines, 0);
  466. mh = (lines + 1) * bh;
  467. #ifdef XINERAMA
  468. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  469. int i, di;
  470. unsigned int du;
  471. Window dw;
  472. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  473. for(i = 0; i < n-1; i++)
  474. if((monitor == info[i].screen_number)
  475. || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
  476. break;
  477. x = info[i].x_org;
  478. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  479. mw = info[i].width;
  480. XFree(info);
  481. }
  482. else
  483. #endif
  484. {
  485. x = 0;
  486. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  487. mw = DisplayWidth(dc->dpy, screen);
  488. }
  489. /* menu window */
  490. wa.override_redirect = True;
  491. wa.background_pixmap = ParentRelative;
  492. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  493. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  494. DefaultDepth(dc->dpy, screen), CopyFromParent,
  495. DefaultVisual(dc->dpy, screen),
  496. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  497. grabkeyboard();
  498. resizedc(dc, mw, mh);
  499. inputw = MIN(inputw, mw/3);
  500. promptw = prompt ? textw(dc, prompt) : 0;
  501. XMapRaised(dc->dpy, win);
  502. }