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.

721 lines
14 KiB

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