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.

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