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.

537 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
14 years ago
14 years ago
14 years ago
14 years ago
17 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
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
18 years ago
18 years ago
18 years ago
14 years ago
18 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
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 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 *next; /* traverses all items */
  21. Item *left, *right; /* traverses matching items */
  22. };
  23. static void appenditem(Item *item, Item **list, Item **last);
  24. static void calcoffsets(void);
  25. static void drawmenu(void);
  26. static char *fstrstr(const char *s, const char *sub);
  27. static void grabkeyboard(void);
  28. static void insert(const char *s, ssize_t n);
  29. static void keypress(XKeyEvent *ev);
  30. static void match(void);
  31. static size_t nextrune(int incr);
  32. static void paste(void);
  33. static void readstdin(void);
  34. static void run(void);
  35. static void setup(void);
  36. static void usage(void);
  37. static char text[BUFSIZ];
  38. static size_t cursor = 0;
  39. static const char *font = NULL;
  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 bh, mw, mh;
  46. static unsigned int inputw = 0;
  47. static unsigned int lines = 0;
  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 *items = NULL;
  55. static Item *matches, *sel;
  56. static Item *prev, *curr, *next;
  57. static Window root, win;
  58. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  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 * bh;
  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) ? bh : 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) ? bh : MIN(textw(dc, prev->left->text), mw/3)) > n)
  81. break;
  82. }
  83. void
  84. drawmenu(void) {
  85. int curpos;
  86. Item *item;
  87. dc->x = 0;
  88. dc->y = 0;
  89. dc->h = bh;
  90. drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
  91. if(prompt) {
  92. dc->w = promptw;
  93. drawtext(dc, prompt, selcol);
  94. dc->x = dc->w;
  95. }
  96. dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
  97. drawtext(dc, text, normcol);
  98. if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
  99. drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
  100. if(lines > 0) {
  101. dc->w = mw - dc->x;
  102. for(item = curr; item != next; item = item->right) {
  103. dc->y += dc->h;
  104. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  105. }
  106. }
  107. else if(matches) {
  108. dc->x += inputw;
  109. dc->w = textw(dc, "<");
  110. if(curr->left)
  111. drawtext(dc, "<", normcol);
  112. for(item = curr; item != next; item = item->right) {
  113. dc->x += dc->w;
  114. dc->w = MIN(textw(dc, item->text), mw/3);
  115. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  116. }
  117. dc->w = textw(dc, ">");
  118. dc->x = mw - dc->w;
  119. if(next)
  120. drawtext(dc, ">", normcol);
  121. }
  122. commitdraw(dc, win, mw, mh);
  123. }
  124. char *
  125. fstrstr(const char *s, const char *sub) {
  126. size_t len;
  127. for(len = strlen(sub); *s; s++)
  128. if(!fstrncmp(s, sub, len))
  129. return (char *)s;
  130. return NULL;
  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. if(strlen(text) + n > sizeof text - 1)
  145. return;
  146. memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
  147. if(n > 0)
  148. memcpy(text + cursor, s, n);
  149. cursor += n;
  150. match();
  151. }
  152. void
  153. keypress(XKeyEvent *ev) {
  154. char buf[32];
  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, 0 - cursor);
  202. break;
  203. case XK_w: /* delete word */
  204. while(cursor > 0 && text[nextrune(-1)] == ' ')
  205. insert(NULL, nextrune(-1) - cursor);
  206. while(cursor > 0 && text[nextrune(-1)] != ' ')
  207. insert(NULL, nextrune(-1) - cursor);
  208. break;
  209. case XK_y: /* paste selection */
  210. XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
  211. return;
  212. }
  213. }
  214. switch(ksym) {
  215. default:
  216. if(isprint(*buf))
  217. insert(buf, strlen(buf));
  218. break;
  219. case XK_Delete:
  220. if(cursor == len)
  221. return;
  222. cursor = nextrune(+1);
  223. case XK_BackSpace:
  224. if(cursor > 0)
  225. insert(NULL, nextrune(-1) - cursor);
  226. break;
  227. case XK_End:
  228. if(cursor < len) {
  229. cursor = len;
  230. break;
  231. }
  232. while(next) {
  233. sel = curr = next;
  234. calcoffsets();
  235. }
  236. while(sel && sel->right)
  237. sel = sel->right;
  238. break;
  239. case XK_Escape:
  240. exit(EXIT_FAILURE);
  241. case XK_Home:
  242. if(sel == matches) {
  243. cursor = 0;
  244. break;
  245. }
  246. sel = curr = matches;
  247. calcoffsets();
  248. break;
  249. case XK_Left:
  250. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  251. cursor = nextrune(-1);
  252. break;
  253. }
  254. else if(lines > 0)
  255. return;
  256. case XK_Up:
  257. if(sel && sel->left && (sel = sel->left)->right == curr) {
  258. curr = prev;
  259. calcoffsets();
  260. }
  261. break;
  262. case XK_Next:
  263. if(!next)
  264. return;
  265. sel = curr = next;
  266. calcoffsets();
  267. break;
  268. case XK_Prior:
  269. if(!prev)
  270. return;
  271. sel = curr = prev;
  272. calcoffsets();
  273. break;
  274. case XK_Return:
  275. case XK_KP_Enter:
  276. fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
  277. fflush(stdout);
  278. exit(EXIT_SUCCESS);
  279. case XK_Right:
  280. if(cursor < len) {
  281. cursor = nextrune(+1);
  282. break;
  283. }
  284. else if(lines > 0)
  285. return;
  286. case XK_Down:
  287. if(sel && sel->right && (sel = sel->right) == next) {
  288. curr = next;
  289. calcoffsets();
  290. }
  291. break;
  292. case XK_Tab:
  293. if(!sel)
  294. return;
  295. strncpy(text, sel->text, sizeof text);
  296. cursor = strlen(text);
  297. match();
  298. break;
  299. }
  300. drawmenu();
  301. }
  302. void
  303. match(void) {
  304. size_t len;
  305. Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  306. len = strlen(text);
  307. matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  308. for(item = items; item; item = item->next)
  309. if(!fstrncmp(text, item->text, len + 1))
  310. appenditem(item, &lexact, &exactend);
  311. else if(!fstrncmp(text, item->text, len))
  312. appenditem(item, &lprefix, &prefixend);
  313. else if(fstrstr(item->text, text))
  314. appenditem(item, &lsubstr, &substrend);
  315. if(lexact) {
  316. matches = lexact;
  317. itemend = exactend;
  318. }
  319. if(lprefix) {
  320. if(itemend) {
  321. itemend->right = lprefix;
  322. lprefix->left = itemend;
  323. }
  324. else
  325. matches = lprefix;
  326. itemend = prefixend;
  327. }
  328. if(lsubstr) {
  329. if(itemend) {
  330. itemend->right = lsubstr;
  331. lsubstr->left = itemend;
  332. }
  333. else
  334. matches = lsubstr;
  335. }
  336. curr = prev = next = sel = matches;
  337. calcoffsets();
  338. }
  339. size_t
  340. nextrune(int incr) {
  341. size_t n, len;
  342. len = strlen(text);
  343. for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
  344. return n;
  345. }
  346. void
  347. paste(void) {
  348. char *p, *q;
  349. int di;
  350. unsigned long dl;
  351. Atom da;
  352. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  353. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  354. insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
  355. XFree(p);
  356. drawmenu();
  357. }
  358. void
  359. readstdin(void) {
  360. char buf[sizeof text], *p;
  361. Item *item, **end;
  362. for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
  363. if((p = strchr(buf, '\n')))
  364. *p = '\0';
  365. if(!(item = malloc(sizeof *item)))
  366. eprintf("cannot malloc %u bytes\n", sizeof *item);
  367. if(!(item->text = strdup(buf)))
  368. eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
  369. item->next = item->left = item->right = NULL;
  370. inputw = MAX(inputw, textw(dc, item->text));
  371. }
  372. }
  373. void
  374. run(void) {
  375. XEvent ev;
  376. while(!XNextEvent(dc->dpy, &ev))
  377. switch(ev.type) {
  378. case Expose:
  379. if(ev.xexpose.count == 0)
  380. drawmenu();
  381. break;
  382. case KeyPress:
  383. keypress(&ev.xkey);
  384. break;
  385. case SelectionNotify:
  386. if(ev.xselection.property == utf8)
  387. paste();
  388. break;
  389. case VisibilityNotify:
  390. if(ev.xvisibility.state != VisibilityUnobscured)
  391. XRaiseWindow(dc->dpy, win);
  392. break;
  393. }
  394. }
  395. void
  396. setup(void) {
  397. int x, y, screen;
  398. XSetWindowAttributes wa;
  399. #ifdef XINERAMA
  400. int n;
  401. XineramaScreenInfo *info;
  402. #endif
  403. screen = DefaultScreen(dc->dpy);
  404. root = RootWindow(dc->dpy, screen);
  405. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  406. normcol[ColBG] = getcolor(dc, normbgcolor);
  407. normcol[ColFG] = getcolor(dc, normfgcolor);
  408. selcol[ColBG] = getcolor(dc, selbgcolor);
  409. selcol[ColFG] = getcolor(dc, selfgcolor);
  410. /* menu geometry */
  411. bh = dc->font.height + 2;
  412. mh = (lines + 1) * bh;
  413. #ifdef XINERAMA
  414. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  415. int i, di;
  416. unsigned int du;
  417. Window dw;
  418. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  419. for(i = 0; i < n; i++)
  420. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  421. break;
  422. x = info[i].x_org;
  423. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  424. mw = info[i].width;
  425. XFree(info);
  426. }
  427. else
  428. #endif
  429. {
  430. x = 0;
  431. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  432. mw = DisplayWidth(dc->dpy, screen);
  433. }
  434. /* menu window */
  435. wa.override_redirect = True;
  436. wa.background_pixmap = ParentRelative;
  437. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  438. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  439. DefaultDepth(dc->dpy, screen), CopyFromParent,
  440. DefaultVisual(dc->dpy, screen),
  441. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  442. grabkeyboard();
  443. setcanvas(dc, mw, mh);
  444. inputw = MIN(inputw, mw/3);
  445. promptw = prompt ? MIN(textw(dc, prompt), mw/5) : 0;
  446. XMapRaised(dc->dpy, win);
  447. text[0] = '\0';
  448. match();
  449. }
  450. void
  451. usage(void) {
  452. fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
  453. " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
  454. exit(EXIT_FAILURE);
  455. }
  456. int
  457. main(int argc, char *argv[]) {
  458. int i;
  459. progname = "dmenu";
  460. for(i = 1; i < argc; i++)
  461. /* single flags */
  462. if(!strcmp(argv[i], "-v")) {
  463. fputs("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n", stdout);
  464. exit(EXIT_SUCCESS);
  465. }
  466. else if(!strcmp(argv[i], "-b"))
  467. topbar = False;
  468. else if(!strcmp(argv[i], "-i"))
  469. fstrncmp = strncasecmp;
  470. else if(i == argc-1)
  471. usage();
  472. /* double flags */
  473. else if(!strcmp(argv[i], "-l"))
  474. lines = atoi(argv[++i]);
  475. else if(!strcmp(argv[i], "-p"))
  476. prompt = argv[++i];
  477. else if(!strcmp(argv[i], "-fn"))
  478. font = argv[++i];
  479. else if(!strcmp(argv[i], "-nb"))
  480. normbgcolor = argv[++i];
  481. else if(!strcmp(argv[i], "-nf"))
  482. normfgcolor = argv[++i];
  483. else if(!strcmp(argv[i], "-sb"))
  484. selbgcolor = argv[++i];
  485. else if(!strcmp(argv[i], "-sf"))
  486. selfgcolor = argv[++i];
  487. else
  488. usage();
  489. dc = initdraw();
  490. initfont(dc, font);
  491. readstdin();
  492. setup();
  493. run();
  494. return EXIT_FAILURE; /* should not reach */
  495. }