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.

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