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.

710 lines
14 KiB

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