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.

578 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
17 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. if(!(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  151. eprint("error, cannot load font: '%s'\n", fontstr);
  152. }
  153. dc.font.ascent = dc.font.xfont->ascent;
  154. dc.font.descent = dc.font.xfont->descent;
  155. }
  156. dc.font.height = dc.font.ascent + dc.font.descent;
  157. }
  158. static int
  159. strido(const char *text, const char *pattern) {
  160. for(; *text && *pattern; text++)
  161. if (*text == *pattern)
  162. pattern++;
  163. return !*pattern;
  164. }
  165. static void
  166. match(char *pattern) {
  167. unsigned int plen;
  168. Item *i, *j;
  169. if(!pattern)
  170. return;
  171. plen = strlen(pattern);
  172. item = j = NULL;
  173. nitem = 0;
  174. for(i = allitems; i; i=i->next)
  175. if(!plen || !strncmp(pattern, i->text, plen)) {
  176. if(!j)
  177. item = i;
  178. else
  179. j->right = i;
  180. i->left = j;
  181. i->right = NULL;
  182. j = i;
  183. nitem++;
  184. }
  185. for(i = allitems; i; i=i->next)
  186. if(plen && strncmp(pattern, i->text, plen)
  187. && strstr(i->text, pattern)) {
  188. if(!j)
  189. item = i;
  190. else
  191. j->right = i;
  192. i->left = j;
  193. i->right = NULL;
  194. j = i;
  195. nitem++;
  196. }
  197. for(i = allitems; i; i=i->next)
  198. if(plen && strncmp(pattern, i->text, plen)
  199. && !strstr(i->text, pattern)
  200. && strido(i->text,pattern)) {
  201. if(!j)
  202. item = i;
  203. else
  204. j->right = i;
  205. i->left = j;
  206. i->right = NULL;
  207. j = i;
  208. nitem++;
  209. }
  210. curr = prev = next = sel = item;
  211. calcoffsets();
  212. }
  213. static void
  214. kpress(XKeyEvent * e) {
  215. char buf[32];
  216. int i, num;
  217. unsigned int len;
  218. KeySym ksym;
  219. len = strlen(text);
  220. buf[0] = 0;
  221. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  222. if(IsKeypadKey(ksym)) {
  223. if(ksym == XK_KP_Enter) {
  224. ksym = XK_Return;
  225. } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
  226. ksym = (ksym - XK_KP_0) + XK_0;
  227. }
  228. }
  229. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  230. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  231. || IsPrivateKeypadKey(ksym))
  232. return;
  233. /* first check if a control mask is omitted */
  234. if(e->state & ControlMask) {
  235. switch (ksym) {
  236. default: /* ignore other control sequences */
  237. return;
  238. case XK_bracketleft:
  239. ksym = XK_Escape;
  240. break;
  241. case XK_h:
  242. case XK_H:
  243. ksym = XK_BackSpace;
  244. break;
  245. case XK_i:
  246. case XK_I:
  247. ksym = XK_Tab;
  248. break;
  249. case XK_j:
  250. case XK_J:
  251. ksym = XK_Return;
  252. break;
  253. case XK_u:
  254. case XK_U:
  255. text[0] = 0;
  256. match(text);
  257. drawmenu();
  258. return;
  259. case XK_w:
  260. case XK_W:
  261. if(len) {
  262. i = len - 1;
  263. while(i >= 0 && text[i] == ' ')
  264. text[i--] = 0;
  265. while(i >= 0 && text[i] != ' ')
  266. text[i--] = 0;
  267. match(text);
  268. drawmenu();
  269. }
  270. return;
  271. }
  272. }
  273. if(CLEANMASK(e->state) & Mod1Mask) {
  274. switch(ksym) {
  275. default: return;
  276. case XK_h:
  277. ksym = XK_Left;
  278. break;
  279. case XK_l:
  280. ksym = XK_Right;
  281. break;
  282. case XK_j:
  283. ksym = XK_Next;
  284. break;
  285. case XK_k:
  286. ksym = XK_Prior;
  287. break;
  288. case XK_g:
  289. ksym = XK_Home;
  290. break;
  291. case XK_G:
  292. ksym = XK_End;
  293. break;
  294. }
  295. }
  296. switch(ksym) {
  297. default:
  298. if(num && !iscntrl((int) buf[0])) {
  299. buf[num] = 0;
  300. if(len > 0)
  301. strncat(text, buf, sizeof text);
  302. else
  303. strncpy(text, buf, sizeof text);
  304. match(text);
  305. }
  306. break;
  307. case XK_BackSpace:
  308. if(len) {
  309. text[--len] = 0;
  310. match(text);
  311. }
  312. break;
  313. case XK_End:
  314. if(!item)
  315. return;
  316. while(next) {
  317. sel = curr = next;
  318. calcoffsets();
  319. }
  320. while(sel && sel->right)
  321. sel = sel->right;
  322. break;
  323. case XK_Escape:
  324. ret = 1;
  325. running = False;
  326. break;
  327. case XK_Home:
  328. if(!item)
  329. return;
  330. sel = curr = item;
  331. calcoffsets();
  332. break;
  333. case XK_Left:
  334. if(!(sel && sel->left))
  335. return;
  336. sel=sel->left;
  337. if(sel->right == curr) {
  338. curr = prev;
  339. calcoffsets();
  340. }
  341. break;
  342. case XK_Next:
  343. if(!next)
  344. return;
  345. sel = curr = next;
  346. calcoffsets();
  347. break;
  348. case XK_Prior:
  349. if(!prev)
  350. return;
  351. sel = curr = prev;
  352. calcoffsets();
  353. break;
  354. case XK_Return:
  355. if((e->state & ShiftMask) && text)
  356. fprintf(stdout, "%s", text);
  357. else if(sel)
  358. fprintf(stdout, "%s", sel->text);
  359. else if(text)
  360. fprintf(stdout, "%s", text);
  361. fflush(stdout);
  362. running = False;
  363. break;
  364. case XK_Right:
  365. if(!(sel && sel->right))
  366. return;
  367. sel=sel->right;
  368. if(sel == next) {
  369. curr = next;
  370. calcoffsets();
  371. }
  372. break;
  373. case XK_Tab:
  374. if(!sel)
  375. return;
  376. strncpy(text, sel->text, sizeof text);
  377. match(text);
  378. break;
  379. }
  380. drawmenu();
  381. }
  382. static char *
  383. readstdin(void) {
  384. static char *maxname = NULL;
  385. char *p, buf[1024];
  386. unsigned int len = 0, max = 0;
  387. Item *i, *new;
  388. i = 0;
  389. while(fgets(buf, sizeof buf, stdin)) {
  390. len = strlen(buf);
  391. if (buf[len - 1] == '\n')
  392. buf[len - 1] = 0;
  393. p = estrdup(buf);
  394. if(max < len) {
  395. maxname = p;
  396. max = len;
  397. }
  398. new = emalloc(sizeof(Item));
  399. new->next = new->left = new->right = NULL;
  400. new->text = p;
  401. if(!i)
  402. allitems = new;
  403. else
  404. i->next = new;
  405. i = new;
  406. }
  407. return maxname;
  408. }
  409. static void
  410. usage(void) {
  411. eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  412. " [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
  413. }
  414. /* extern */
  415. int screen;
  416. Display *dpy;
  417. DC dc = {0};
  418. int
  419. main(int argc, char *argv[]) {
  420. Bool bottom = False;
  421. char *font = FONT;
  422. char *maxname;
  423. char *normbg = NORMBGCOLOR;
  424. char *normfg = NORMFGCOLOR;
  425. char *selbg = SELBGCOLOR;
  426. char *selfg = SELFGCOLOR;
  427. int i, j;
  428. Item *itm;
  429. XEvent ev;
  430. XModifierKeymap *modmap;
  431. XSetWindowAttributes wa;
  432. /* command line args */
  433. for(i = 1; i < argc; i++)
  434. if(!strcmp(argv[i], "-b")) {
  435. bottom = True;
  436. }
  437. else if(!strcmp(argv[i], "-fn")) {
  438. if(++i < argc) font = argv[i];
  439. }
  440. else if(!strcmp(argv[i], "-nb")) {
  441. if(++i < argc) normbg = argv[i];
  442. }
  443. else if(!strcmp(argv[i], "-nf")) {
  444. if(++i < argc) normfg = argv[i];
  445. }
  446. else if(!strcmp(argv[i], "-p")) {
  447. if(++i < argc) prompt = argv[i];
  448. }
  449. else if(!strcmp(argv[i], "-sb")) {
  450. if(++i < argc) selbg = argv[i];
  451. }
  452. else if(!strcmp(argv[i], "-sf")) {
  453. if(++i < argc) selfg = argv[i];
  454. }
  455. else if(!strcmp(argv[i], "-v"))
  456. eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
  457. else
  458. usage();
  459. setlocale(LC_CTYPE, "");
  460. dpy = XOpenDisplay(0);
  461. if(!dpy)
  462. eprint("dmenu: cannot open display\n");
  463. screen = DefaultScreen(dpy);
  464. root = RootWindow(dpy, screen);
  465. if(isatty(STDIN_FILENO)) {
  466. maxname = readstdin();
  467. running = grabkeyboard();
  468. }
  469. else { /* prevent keypress loss */
  470. running = grabkeyboard();
  471. maxname = readstdin();
  472. }
  473. /* init modifier map */
  474. modmap = XGetModifierMapping(dpy);
  475. for (i = 0; i < 8; i++) {
  476. for (j = 0; j < modmap->max_keypermod; j++) {
  477. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  478. == XKeysymToKeycode(dpy, XK_Num_Lock))
  479. numlockmask = (1 << i);
  480. }
  481. }
  482. XFreeModifiermap(modmap);
  483. /* style */
  484. dc.norm[ColBG] = initcolor(normbg);
  485. dc.norm[ColFG] = initcolor(normfg);
  486. dc.sel[ColBG] = initcolor(selbg);
  487. dc.sel[ColFG] = initcolor(selfg);
  488. initfont(font);
  489. /* menu window */
  490. wa.override_redirect = 1;
  491. wa.background_pixmap = ParentRelative;
  492. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  493. mw = DisplayWidth(dpy, screen);
  494. mh = dc.font.height + 2;
  495. win = XCreateWindow(dpy, root, 0,
  496. bottom ? DisplayHeight(dpy, screen) - mh : 0, mw, mh, 0,
  497. DefaultDepth(dpy, screen), CopyFromParent,
  498. DefaultVisual(dpy, screen),
  499. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  500. /* pixmap */
  501. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  502. dc.gc = XCreateGC(dpy, root, 0, 0);
  503. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  504. if(!dc.font.set)
  505. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  506. if(maxname)
  507. cmdw = textw(maxname);
  508. if(cmdw > mw / 3)
  509. cmdw = mw / 3;
  510. if(prompt)
  511. promptw = textw(prompt);
  512. if(promptw > mw / 5)
  513. promptw = mw / 5;
  514. text[0] = 0;
  515. match(text);
  516. XMapRaised(dpy, win);
  517. drawmenu();
  518. XSync(dpy, False);
  519. /* main event loop */
  520. while(running && !XNextEvent(dpy, &ev))
  521. switch (ev.type) {
  522. default: /* ignore all crap */
  523. break;
  524. case KeyPress:
  525. kpress(&ev.xkey);
  526. break;
  527. case Expose:
  528. if(ev.xexpose.count == 0)
  529. drawmenu();
  530. break;
  531. }
  532. /* cleanup */
  533. while(allitems) {
  534. itm = allitems->next;
  535. free(allitems->text);
  536. free(allitems);
  537. allitems = itm;
  538. }
  539. if(dc.font.set)
  540. XFreeFontSet(dpy, dc.font.set);
  541. else
  542. XFreeFont(dpy, dc.font.xfont);
  543. XFreePixmap(dpy, dc.drawable);
  544. XFreeGC(dpy, dc.gc);
  545. XDestroyWindow(dpy, win);
  546. XUngrabKeyboard(dpy, CurrentTime);
  547. XCloseDisplay(dpy);
  548. return ret;
  549. }