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.

602 lines
12 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dmenu.h"
  6. #include <ctype.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <X11/Xutil.h>
  13. #include <X11/keysym.h>
  14. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  15. typedef struct Item Item;
  16. struct Item {
  17. Item *next; /* traverses all items */
  18. Item *left, *right; /* traverses items matching current search pattern */
  19. char *text;
  20. };
  21. /* static */
  22. static char text[4096];
  23. static char *prompt = NULL;
  24. static int mw, mh;
  25. static int ret = 0;
  26. static int nitem = 0;
  27. static unsigned int cmdw = 0;
  28. static unsigned int promptw = 0;
  29. static unsigned int numlockmask = 0;
  30. static Bool running = True;
  31. static Item *allitems = NULL; /* first of all items */
  32. static Item *item = NULL; /* first of pattern matching items */
  33. static Item *sel = NULL;
  34. static Item *next = NULL;
  35. static Item *prev = NULL;
  36. static Item *curr = NULL;
  37. static Window root;
  38. static Window win;
  39. static unsigned int
  40. textnw(const char *text, unsigned int len) {
  41. XRectangle r;
  42. if(dc.font.set) {
  43. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  44. return r.width;
  45. }
  46. return XTextWidth(dc.font.xfont, text, len);
  47. }
  48. static unsigned int
  49. textw(const char *text) {
  50. return textnw(text, strlen(text)) + dc.font.height;
  51. }
  52. static void
  53. calcoffsets(void) {
  54. unsigned int tw, w;
  55. if(!curr)
  56. return;
  57. w = promptw + cmdw + 2 * SPACE;
  58. for(next = curr; next; next=next->right) {
  59. tw = textw(next->text);
  60. if(tw > mw / 3)
  61. tw = mw / 3;
  62. w += tw;
  63. if(w > mw)
  64. break;
  65. }
  66. w = promptw + cmdw + 2 * SPACE;
  67. for(prev = curr; prev && prev->left; prev=prev->left) {
  68. tw = textw(prev->left->text);
  69. if(tw > mw / 3)
  70. tw = mw / 3;
  71. w += tw;
  72. if(w > mw)
  73. break;
  74. }
  75. }
  76. static void
  77. drawtext(const char *text, unsigned long col[ColLast]) {
  78. int x, y, w, h;
  79. static char buf[256];
  80. unsigned int len, olen;
  81. XGCValues gcv;
  82. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  83. XSetForeground(dpy, dc.gc, col[ColBG]);
  84. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  85. if(!text)
  86. return;
  87. w = 0;
  88. olen = len = strlen(text);
  89. if(len >= sizeof buf)
  90. len = sizeof buf - 1;
  91. memcpy(buf, text, len);
  92. buf[len] = 0;
  93. h = dc.font.ascent + dc.font.descent;
  94. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  95. x = dc.x + (h / 2);
  96. /* shorten text if necessary */
  97. while(len && (w = textnw(buf, len)) > dc.w - h)
  98. buf[--len] = 0;
  99. if(len < olen) {
  100. if(len > 1)
  101. buf[len - 1] = '.';
  102. if(len > 2)
  103. buf[len - 2] = '.';
  104. if(len > 3)
  105. buf[len - 3] = '.';
  106. }
  107. if(w > dc.w)
  108. return; /* too long */
  109. gcv.foreground = col[ColFG];
  110. if(dc.font.set) {
  111. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  112. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc,
  113. x, y, buf, len);
  114. }
  115. else {
  116. gcv.font = dc.font.xfont->fid;
  117. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  118. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  119. }
  120. }
  121. static void
  122. drawmenu(void) {
  123. Item *i;
  124. dc.x = 0;
  125. dc.y = 0;
  126. dc.w = mw;
  127. dc.h = mh;
  128. drawtext(NULL, dc.norm);
  129. /* print prompt? */
  130. if(promptw) {
  131. dc.w = promptw;
  132. drawtext(prompt, dc.sel);
  133. }
  134. dc.x += promptw;
  135. dc.w = mw - promptw;
  136. /* print command */
  137. if(cmdw && item)
  138. dc.w = cmdw;
  139. drawtext(text[0] ? text : NULL, dc.norm);
  140. dc.x += cmdw;
  141. if(curr) {
  142. dc.w = SPACE;
  143. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  144. dc.x += dc.w;
  145. /* determine maximum items */
  146. for(i = curr; i != next; i=i->right) {
  147. dc.w = textw(i->text);
  148. if(dc.w > mw / 3)
  149. dc.w = mw / 3;
  150. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  151. dc.x += dc.w;
  152. }
  153. dc.x = mw - SPACE;
  154. dc.w = SPACE;
  155. drawtext(next ? ">" : NULL, dc.norm);
  156. }
  157. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  158. XFlush(dpy);
  159. }
  160. static unsigned long
  161. getcolor(const char *colstr) {
  162. Colormap cmap = DefaultColormap(dpy, screen);
  163. XColor color;
  164. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  165. eprint("error, cannot allocate color '%s'\n", colstr);
  166. return color.pixel;
  167. }
  168. static void
  169. setfont(const char *fontstr) {
  170. char *def, **missing;
  171. int i, n;
  172. missing = NULL;
  173. if(dc.font.set)
  174. XFreeFontSet(dpy, dc.font.set);
  175. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  176. if(missing)
  177. XFreeStringList(missing);
  178. if(dc.font.set) {
  179. XFontSetExtents *font_extents;
  180. XFontStruct **xfonts;
  181. char **font_names;
  182. dc.font.ascent = dc.font.descent = 0;
  183. font_extents = XExtentsOfFontSet(dc.font.set);
  184. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  185. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  186. if(dc.font.ascent < (*xfonts)->ascent)
  187. dc.font.ascent = (*xfonts)->ascent;
  188. if(dc.font.descent < (*xfonts)->descent)
  189. dc.font.descent = (*xfonts)->descent;
  190. xfonts++;
  191. }
  192. }
  193. else {
  194. if(dc.font.xfont)
  195. XFreeFont(dpy, dc.font.xfont);
  196. dc.font.xfont = NULL;
  197. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  198. eprint("error, cannot load font: '%s'\n", fontstr);
  199. dc.font.ascent = dc.font.xfont->ascent;
  200. dc.font.descent = dc.font.xfont->descent;
  201. }
  202. dc.font.height = dc.font.ascent + dc.font.descent;
  203. }
  204. static void
  205. match(char *pattern) {
  206. unsigned int plen;
  207. Item *i, *j;
  208. if(!pattern)
  209. return;
  210. plen = strlen(pattern);
  211. item = j = NULL;
  212. nitem = 0;
  213. for(i = allitems; i; i=i->next)
  214. if(!plen || !strncmp(pattern, i->text, plen)) {
  215. if(!j)
  216. item = i;
  217. else
  218. j->right = i;
  219. i->left = j;
  220. i->right = NULL;
  221. j = i;
  222. nitem++;
  223. }
  224. for(i = allitems; i; i=i->next)
  225. if(plen && strncmp(pattern, i->text, plen)
  226. && strstr(i->text, pattern)) {
  227. if(!j)
  228. item = i;
  229. else
  230. j->right = i;
  231. i->left = j;
  232. i->right = NULL;
  233. j = i;
  234. nitem++;
  235. }
  236. curr = prev = next = sel = item;
  237. calcoffsets();
  238. }
  239. static void
  240. kpress(XKeyEvent * e) {
  241. char buf[32];
  242. int i, num, prev_nitem;
  243. unsigned int len;
  244. KeySym ksym;
  245. len = strlen(text);
  246. buf[0] = 0;
  247. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  248. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  249. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  250. || IsPrivateKeypadKey(ksym))
  251. return;
  252. /* first check if a control mask is omitted */
  253. if(e->state & ControlMask) {
  254. switch (ksym) {
  255. default: /* ignore other control sequences */
  256. return;
  257. case XK_bracketleft:
  258. ksym = XK_Escape;
  259. break;
  260. case XK_h:
  261. case XK_H:
  262. ksym = XK_BackSpace;
  263. break;
  264. case XK_i:
  265. case XK_I:
  266. ksym = XK_Tab;
  267. break;
  268. case XK_j:
  269. case XK_J:
  270. ksym = XK_Return;
  271. break;
  272. case XK_u:
  273. case XK_U:
  274. text[0] = 0;
  275. match(text);
  276. drawmenu();
  277. return;
  278. case XK_w:
  279. case XK_W:
  280. if(len) {
  281. i = len - 1;
  282. while(i >= 0 && text[i] == ' ')
  283. text[i--] = 0;
  284. while(i >= 0 && text[i] != ' ')
  285. text[i--] = 0;
  286. match(text);
  287. drawmenu();
  288. }
  289. return;
  290. }
  291. }
  292. if(CLEANMASK(e->state) & Mod1Mask) {
  293. switch(ksym) {
  294. default: return;
  295. case XK_h:
  296. ksym = XK_Left;
  297. break;
  298. case XK_l:
  299. ksym = XK_Right;
  300. break;
  301. case XK_j:
  302. ksym = XK_Next;
  303. break;
  304. case XK_k:
  305. ksym = XK_Prior;
  306. break;
  307. case XK_g:
  308. ksym = XK_Home;
  309. break;
  310. case XK_G:
  311. ksym = XK_End;
  312. break;
  313. }
  314. }
  315. switch(ksym) {
  316. default:
  317. if(num && !iscntrl((int) buf[0])) {
  318. buf[num] = 0;
  319. if(len > 0)
  320. strncat(text, buf, sizeof text);
  321. else
  322. strncpy(text, buf, sizeof text);
  323. match(text);
  324. }
  325. break;
  326. case XK_BackSpace:
  327. if((i = len)) {
  328. prev_nitem = nitem;
  329. do {
  330. text[--i] = 0;
  331. match(text);
  332. } while(i && nitem && prev_nitem == nitem);
  333. match(text);
  334. }
  335. break;
  336. case XK_End:
  337. if(!item)
  338. return;
  339. while(next) {
  340. sel = curr = next;
  341. calcoffsets();
  342. }
  343. while(sel && sel->right)
  344. sel = sel->right;
  345. break;
  346. case XK_Escape:
  347. ret = 1;
  348. running = False;
  349. break;
  350. case XK_Home:
  351. if(!item)
  352. return;
  353. sel = curr = item;
  354. calcoffsets();
  355. break;
  356. case XK_Left:
  357. if(!(sel && sel->left))
  358. return;
  359. sel=sel->left;
  360. if(sel->right == curr) {
  361. curr = prev;
  362. calcoffsets();
  363. }
  364. break;
  365. case XK_Next:
  366. if(!next)
  367. return;
  368. sel = curr = next;
  369. calcoffsets();
  370. break;
  371. case XK_Prior:
  372. if(!prev)
  373. return;
  374. sel = curr = prev;
  375. calcoffsets();
  376. break;
  377. case XK_Return:
  378. if((e->state & ShiftMask) && text)
  379. fprintf(stdout, "%s", text);
  380. else if(sel)
  381. fprintf(stdout, "%s", sel->text);
  382. else if(text)
  383. fprintf(stdout, "%s", text);
  384. fflush(stdout);
  385. running = False;
  386. break;
  387. case XK_Right:
  388. if(!(sel && sel->right))
  389. return;
  390. sel=sel->right;
  391. if(sel == next) {
  392. curr = next;
  393. calcoffsets();
  394. }
  395. break;
  396. case XK_Tab:
  397. if(!sel)
  398. return;
  399. strncpy(text, sel->text, sizeof text);
  400. match(text);
  401. break;
  402. }
  403. drawmenu();
  404. }
  405. static char *
  406. readstdin(void) {
  407. static char *maxname = NULL;
  408. char *p, buf[1024];
  409. unsigned int len = 0, max = 0;
  410. Item *i, *new;
  411. i = 0;
  412. while(fgets(buf, sizeof buf, stdin)) {
  413. len = strlen(buf);
  414. if (buf[len - 1] == '\n')
  415. buf[len - 1] = 0;
  416. p = estrdup(buf);
  417. if(max < len) {
  418. maxname = p;
  419. max = len;
  420. }
  421. new = emalloc(sizeof(Item));
  422. new->next = new->left = new->right = NULL;
  423. new->text = p;
  424. if(!i)
  425. allitems = new;
  426. else
  427. i->next = new;
  428. i = new;
  429. }
  430. return maxname;
  431. }
  432. static void
  433. usage(void) {
  434. eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  435. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  436. }
  437. /* extern */
  438. int screen;
  439. Display *dpy;
  440. DC dc = {0};
  441. int
  442. main(int argc, char *argv[]) {
  443. Bool bottom = False;
  444. char *font = FONT;
  445. char *maxname;
  446. char *normbg = NORMBGCOLOR;
  447. char *normfg = NORMFGCOLOR;
  448. char *selbg = SELBGCOLOR;
  449. char *selfg = SELFGCOLOR;
  450. int i, j;
  451. Item *itm;
  452. XEvent ev;
  453. XModifierKeymap *modmap;
  454. XSetWindowAttributes wa;
  455. if(isatty(STDIN_FILENO)) {
  456. fputs("error: dmenu can't run in an interactive shell\n", stdout);
  457. usage();
  458. }
  459. /* command line args */
  460. for(i = 1; i < argc; i++)
  461. if(!strncmp(argv[i], "-b", 3)) {
  462. bottom = True;
  463. }
  464. else if(!strncmp(argv[i], "-fn", 4)) {
  465. if(++i < argc) font = argv[i];
  466. }
  467. else if(!strncmp(argv[i], "-nb", 4)) {
  468. if(++i < argc) normbg = argv[i];
  469. }
  470. else if(!strncmp(argv[i], "-nf", 4)) {
  471. if(++i < argc) normfg = argv[i];
  472. }
  473. else if(!strncmp(argv[i], "-p", 3)) {
  474. if(++i < argc) prompt = argv[i];
  475. }
  476. else if(!strncmp(argv[i], "-sb", 4)) {
  477. if(++i < argc) selbg = argv[i];
  478. }
  479. else if(!strncmp(argv[i], "-sf", 4)) {
  480. if(++i < argc) selfg = argv[i];
  481. }
  482. else if(!strncmp(argv[i], "-v", 3))
  483. eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
  484. else
  485. usage();
  486. setlocale(LC_CTYPE, "");
  487. dpy = XOpenDisplay(0);
  488. if(!dpy)
  489. eprint("dmenu: cannot open display\n");
  490. screen = DefaultScreen(dpy);
  491. root = RootWindow(dpy, screen);
  492. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  493. GrabModeAsync, CurrentTime) != GrabSuccess)
  494. usleep(1000);
  495. maxname = readstdin();
  496. /* init modifier map */
  497. modmap = XGetModifierMapping(dpy);
  498. for (i = 0; i < 8; i++) {
  499. for (j = 0; j < modmap->max_keypermod; j++) {
  500. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  501. == XKeysymToKeycode(dpy, XK_Num_Lock))
  502. numlockmask = (1 << i);
  503. }
  504. }
  505. XFreeModifiermap(modmap);
  506. /* style */
  507. dc.norm[ColBG] = getcolor(normbg);
  508. dc.norm[ColFG] = getcolor(normfg);
  509. dc.sel[ColBG] = getcolor(selbg);
  510. dc.sel[ColFG] = getcolor(selfg);
  511. setfont(font);
  512. /* menu window */
  513. wa.override_redirect = 1;
  514. wa.background_pixmap = ParentRelative;
  515. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  516. mw = DisplayWidth(dpy, screen);
  517. mh = dc.font.height + 2;
  518. win = XCreateWindow(dpy, root, 0,
  519. bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
  520. DefaultDepth(dpy, screen), CopyFromParent,
  521. DefaultVisual(dpy, screen),
  522. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  523. /* pixmap */
  524. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  525. dc.gc = XCreateGC(dpy, root, 0, 0);
  526. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  527. if(maxname)
  528. cmdw = textw(maxname);
  529. if(cmdw > mw / 3)
  530. cmdw = mw / 3;
  531. if(prompt)
  532. promptw = textw(prompt);
  533. if(promptw > mw / 5)
  534. promptw = mw / 5;
  535. text[0] = 0;
  536. match(text);
  537. XMapRaised(dpy, win);
  538. drawmenu();
  539. XSync(dpy, False);
  540. /* main event loop */
  541. while(running && !XNextEvent(dpy, &ev))
  542. switch (ev.type) {
  543. default: /* ignore all crap */
  544. break;
  545. case KeyPress:
  546. kpress(&ev.xkey);
  547. break;
  548. case Expose:
  549. if(ev.xexpose.count == 0)
  550. drawmenu();
  551. break;
  552. }
  553. /* cleanup */
  554. while(allitems) {
  555. itm = allitems->next;
  556. free(allitems->text);
  557. free(allitems);
  558. allitems = itm;
  559. }
  560. if(dc.font.set)
  561. XFreeFontSet(dpy, dc.font.set);
  562. else
  563. XFreeFont(dpy, dc.font.xfont);
  564. XFreePixmap(dpy, dc.drawable);
  565. XFreeGC(dpy, dc.gc);
  566. XDestroyWindow(dpy, win);
  567. XUngrabKeyboard(dpy, CurrentTime);
  568. XCloseDisplay(dpy);
  569. return ret;
  570. }