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.

545 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, prev_nitem;
  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((i = len)) {
  276. prev_nitem = nitem;
  277. do {
  278. text[--i] = 0;
  279. match(text);
  280. } while(i && nitem && prev_nitem == nitem);
  281. match(text);
  282. }
  283. break;
  284. case XK_End:
  285. if(!item)
  286. return;
  287. while(next) {
  288. sel = curr = next;
  289. calcoffsets();
  290. }
  291. while(sel && sel->right)
  292. sel = sel->right;
  293. break;
  294. case XK_Escape:
  295. ret = 1;
  296. running = False;
  297. break;
  298. case XK_Home:
  299. if(!item)
  300. return;
  301. sel = curr = item;
  302. calcoffsets();
  303. break;
  304. case XK_Left:
  305. if(!(sel && sel->left))
  306. return;
  307. sel=sel->left;
  308. if(sel->right == curr) {
  309. curr = prev;
  310. calcoffsets();
  311. }
  312. break;
  313. case XK_Next:
  314. if(!next)
  315. return;
  316. sel = curr = next;
  317. calcoffsets();
  318. break;
  319. case XK_Prior:
  320. if(!prev)
  321. return;
  322. sel = curr = prev;
  323. calcoffsets();
  324. break;
  325. case XK_Return:
  326. if((e->state & ShiftMask) && text)
  327. fprintf(stdout, "%s", text);
  328. else if(sel)
  329. fprintf(stdout, "%s", sel->text);
  330. else if(text)
  331. fprintf(stdout, "%s", text);
  332. fflush(stdout);
  333. running = False;
  334. break;
  335. case XK_Right:
  336. if(!(sel && sel->right))
  337. return;
  338. sel=sel->right;
  339. if(sel == next) {
  340. curr = next;
  341. calcoffsets();
  342. }
  343. break;
  344. case XK_Tab:
  345. if(!sel)
  346. return;
  347. strncpy(text, sel->text, sizeof text);
  348. match(text);
  349. break;
  350. }
  351. drawmenu();
  352. }
  353. static char *
  354. readstdin(void) {
  355. static char *maxname = NULL;
  356. char *p, buf[1024];
  357. unsigned int len = 0, max = 0;
  358. Item *i, *new;
  359. i = 0;
  360. while(fgets(buf, sizeof buf, stdin)) {
  361. len = strlen(buf);
  362. if (buf[len - 1] == '\n')
  363. buf[len - 1] = 0;
  364. p = estrdup(buf);
  365. if(max < len) {
  366. maxname = p;
  367. max = len;
  368. }
  369. new = emalloc(sizeof(Item));
  370. new->next = new->left = new->right = NULL;
  371. new->text = p;
  372. if(!i)
  373. allitems = new;
  374. else
  375. i->next = new;
  376. i = new;
  377. }
  378. return maxname;
  379. }
  380. static void
  381. usage(void) {
  382. eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  383. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  384. }
  385. /* extern */
  386. int screen;
  387. Display *dpy;
  388. DC dc = {0};
  389. int
  390. main(int argc, char *argv[]) {
  391. Bool bottom = False;
  392. char *font = FONT;
  393. char *maxname;
  394. char *normbg = NORMBGCOLOR;
  395. char *normfg = NORMFGCOLOR;
  396. char *selbg = SELBGCOLOR;
  397. char *selfg = SELFGCOLOR;
  398. int i, j;
  399. Item *itm;
  400. XEvent ev;
  401. XModifierKeymap *modmap;
  402. XSetWindowAttributes wa;
  403. /* command line args */
  404. for(i = 1; i < argc; i++)
  405. if(!strncmp(argv[i], "-b", 3)) {
  406. bottom = True;
  407. }
  408. else if(!strncmp(argv[i], "-fn", 4)) {
  409. if(++i < argc) font = argv[i];
  410. }
  411. else if(!strncmp(argv[i], "-nb", 4)) {
  412. if(++i < argc) normbg = argv[i];
  413. }
  414. else if(!strncmp(argv[i], "-nf", 4)) {
  415. if(++i < argc) normfg = argv[i];
  416. }
  417. else if(!strncmp(argv[i], "-p", 3)) {
  418. if(++i < argc) prompt = argv[i];
  419. }
  420. else if(!strncmp(argv[i], "-sb", 4)) {
  421. if(++i < argc) selbg = argv[i];
  422. }
  423. else if(!strncmp(argv[i], "-sf", 4)) {
  424. if(++i < argc) selfg = argv[i];
  425. }
  426. else if(!strncmp(argv[i], "-v", 3))
  427. eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
  428. else
  429. usage();
  430. setlocale(LC_CTYPE, "");
  431. dpy = XOpenDisplay(0);
  432. if(!dpy)
  433. eprint("dmenu: cannot open display\n");
  434. screen = DefaultScreen(dpy);
  435. root = RootWindow(dpy, screen);
  436. if(isatty(STDIN_FILENO)) {
  437. maxname = readstdin();
  438. grabkeyboard();
  439. }
  440. else { /* prevent keypress loss */
  441. grabkeyboard();
  442. maxname = readstdin();
  443. }
  444. /* init modifier map */
  445. modmap = XGetModifierMapping(dpy);
  446. for (i = 0; i < 8; i++) {
  447. for (j = 0; j < modmap->max_keypermod; j++) {
  448. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  449. == XKeysymToKeycode(dpy, XK_Num_Lock))
  450. numlockmask = (1 << i);
  451. }
  452. }
  453. XFreeModifiermap(modmap);
  454. /* style */
  455. dc.norm[ColBG] = initcolor(normbg);
  456. dc.norm[ColFG] = initcolor(normfg);
  457. dc.sel[ColBG] = initcolor(selbg);
  458. dc.sel[ColFG] = initcolor(selfg);
  459. initfont(font);
  460. /* menu window */
  461. wa.override_redirect = 1;
  462. wa.background_pixmap = ParentRelative;
  463. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  464. mw = DisplayWidth(dpy, screen);
  465. mh = dc.font.height + 2;
  466. win = XCreateWindow(dpy, root, 0,
  467. bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
  468. DefaultDepth(dpy, screen), CopyFromParent,
  469. DefaultVisual(dpy, screen),
  470. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  471. /* pixmap */
  472. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  473. dc.gc = XCreateGC(dpy, root, 0, 0);
  474. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  475. if(maxname)
  476. cmdw = textw(maxname);
  477. if(cmdw > mw / 3)
  478. cmdw = mw / 3;
  479. if(prompt)
  480. promptw = textw(prompt);
  481. if(promptw > mw / 5)
  482. promptw = mw / 5;
  483. text[0] = 0;
  484. match(text);
  485. XMapRaised(dpy, win);
  486. drawmenu();
  487. XSync(dpy, False);
  488. /* main event loop */
  489. while(running && !XNextEvent(dpy, &ev))
  490. switch (ev.type) {
  491. default: /* ignore all crap */
  492. break;
  493. case KeyPress:
  494. kpress(&ev.xkey);
  495. break;
  496. case Expose:
  497. if(ev.xexpose.count == 0)
  498. drawmenu();
  499. break;
  500. }
  501. /* cleanup */
  502. while(allitems) {
  503. itm = allitems->next;
  504. free(allitems->text);
  505. free(allitems);
  506. allitems = itm;
  507. }
  508. if(dc.font.set)
  509. XFreeFontSet(dpy, dc.font.set);
  510. else
  511. XFreeFont(dpy, dc.font.xfont);
  512. XFreePixmap(dpy, dc.drawable);
  513. XFreeGC(dpy, dc.gc);
  514. XDestroyWindow(dpy, win);
  515. XUngrabKeyboard(dpy, CurrentTime);
  516. XCloseDisplay(dpy);
  517. return ret;
  518. }