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.

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