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.

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