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.

606 lines
13 KiB

18 years ago
18 years ago
16 years ago
18 years ago
16 years ago
18 years ago
18 years ago
18 years ago
18 years ago
16 years ago
16 years ago
14 years ago
14 years ago
16 years ago
14 years ago
18 years ago
17 years ago
16 years ago
14 years ago
14 years ago
16 years ago
14 years ago
16 years ago
17 years ago
17 years ago
14 years ago
18 years ago
14 years ago
14 years ago
14 years ago
14 years ago
18 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
17 years ago
17 years ago
17 years ago
17 years ago
14 years ago
18 years ago
14 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
14 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
17 years ago
14 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
14 years ago
18 years ago
17 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
17 years ago
17 years ago
18 years ago
14 years ago
14 years ago
17 years ago
18 years ago
14 years ago
14 years ago
17 years ago
14 years ago
18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xlib.h>
  10. #include <X11/Xutil.h>
  11. #ifdef XINERAMA
  12. #include <X11/extensions/Xinerama.h>
  13. #endif
  14. #include <draw.h>
  15. /* macros */
  16. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  17. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  18. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  19. #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
  20. typedef struct Item Item;
  21. struct Item {
  22. char *text;
  23. Item *next; /* traverses all items */
  24. Item *left, *right; /* traverses items matching current search pattern */
  25. };
  26. /* forward declarations */
  27. static void appenditem(Item *i, Item **list, Item **last);
  28. static void calcoffsetsh(void);
  29. static void calcoffsetsv(void);
  30. static char *cistrstr(const char *s, const char *sub);
  31. static void cleanup(void);
  32. static void dinput(void);
  33. static void drawmenu(void);
  34. static void drawmenuh(void);
  35. static void drawmenuv(void);
  36. static void grabkeyboard(void);
  37. static void kpress(XKeyEvent *e);
  38. static void match(char *pattern);
  39. static void readstdin(void);
  40. static void run(void);
  41. static void setup(void);
  42. #include "config.h"
  43. /* variables */
  44. static char **argp = NULL;
  45. static char *maxname = NULL;
  46. static char *prompt = NULL;
  47. static char text[4096];
  48. static int cmdw = 0;
  49. static int promptw = 0;
  50. static int screen;
  51. static unsigned int lines = 0;
  52. static unsigned int numlockmask = 0;
  53. static unsigned int mw, mh;
  54. static unsigned long normcol[ColLast];
  55. static unsigned long selcol[ColLast];
  56. static Bool topbar = True;
  57. static DC dc;
  58. static Display *dpy;
  59. static Item *allitems = NULL; /* first of all items */
  60. static Item *item = NULL; /* first of pattern matching items */
  61. static Item *sel = NULL;
  62. static Item *next = NULL;
  63. static Item *prev = NULL;
  64. static Item *curr = NULL;
  65. static Window win, root;
  66. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  67. static char *(*fstrstr)(const char *, const char *) = strstr;
  68. static void (*calcoffsets)(void) = calcoffsetsh;
  69. void
  70. appenditem(Item *i, Item **list, Item **last) {
  71. if(!(*last))
  72. *list = i;
  73. else
  74. (*last)->right = i;
  75. i->left = *last;
  76. i->right = NULL;
  77. *last = i;
  78. }
  79. void
  80. calcoffsetsh(void) {
  81. unsigned int x;
  82. x = promptw + cmdw + (2 * spaceitem);
  83. for(next = curr; next; next = next->right)
  84. if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
  85. break;
  86. x = promptw + cmdw + (2 * spaceitem);
  87. for(prev = curr; prev && prev->left; prev = prev->left)
  88. if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
  89. break;
  90. }
  91. void
  92. calcoffsetsv(void) {
  93. unsigned int i;
  94. next = prev = curr;
  95. for(i = 0; i < lines && next; i++)
  96. next = next->right;
  97. mh = (dc.font.height + 2) * (i + 1);
  98. for(i = 0; i < lines && prev && prev->left; i++)
  99. prev = prev->left;
  100. }
  101. char *
  102. cistrstr(const char *s, const char *sub) {
  103. int c, csub;
  104. unsigned int len;
  105. if(!sub)
  106. return (char *)s;
  107. if((c = tolower(*sub++)) != '\0') {
  108. len = strlen(sub);
  109. do {
  110. do {
  111. if((csub = *s++) == '\0')
  112. return NULL;
  113. }
  114. while(tolower(csub) != c);
  115. }
  116. while(strncasecmp(s, sub, len) != 0);
  117. s--;
  118. }
  119. return (char *)s;
  120. }
  121. void
  122. cleanup(void) {
  123. Item *itm;
  124. while(allitems) {
  125. itm = allitems->next;
  126. free(allitems->text);
  127. free(allitems);
  128. allitems = itm;
  129. }
  130. cleanupdraw(&dc);
  131. XDestroyWindow(dpy, win);
  132. XUngrabKeyboard(dpy, CurrentTime);
  133. XCloseDisplay(dpy);
  134. }
  135. void
  136. dinput(void) {
  137. cleanup();
  138. argp[0] = "dinput";
  139. argp[1] = text;
  140. execvp("dinput", argp);
  141. eprint("cannot exec dinput\n");
  142. }
  143. void
  144. drawmenu(void) {
  145. dc.x = 0;
  146. dc.y = 0;
  147. dc.w = mw;
  148. dc.h = mh;
  149. drawtext(&dc, NULL, normcol);
  150. dc.h = dc.font.height + 2;
  151. dc.y = topbar ? 0 : mh - dc.h;
  152. /* print prompt? */
  153. if(prompt) {
  154. dc.w = promptw;
  155. drawtext(&dc, prompt, selcol);
  156. dc.x += dc.w;
  157. }
  158. dc.w = mw - dc.x;
  159. /* print command */
  160. if(cmdw && item && lines == 0)
  161. dc.w = cmdw;
  162. drawtext(&dc, text, normcol);
  163. if(lines > 0)
  164. drawmenuv();
  165. else if(curr)
  166. drawmenuh();
  167. commitdraw(&dc, win);
  168. }
  169. void
  170. drawmenuh(void) {
  171. Item *i;
  172. dc.x += cmdw;
  173. dc.w = spaceitem;
  174. drawtext(&dc, curr->left ? "<" : NULL, normcol);
  175. dc.x += dc.w;
  176. for(i = curr; i != next; i = i->right) {
  177. dc.w = MIN(textw(&dc, i->text), mw / 3);
  178. drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
  179. dc.x += dc.w;
  180. }
  181. dc.w = spaceitem;
  182. dc.x = mw - dc.w;
  183. drawtext(&dc, next ? ">" : NULL, normcol);
  184. }
  185. void
  186. drawmenuv(void) {
  187. Item *i;
  188. XWindowAttributes wa;
  189. dc.y = topbar ? dc.h : 0;
  190. dc.w = mw - dc.x;
  191. for(i = curr; i != next; i = i->right) {
  192. drawtext(&dc, i->text, (sel == i) ? selcol : normcol);
  193. dc.y += dc.h;
  194. }
  195. if(!XGetWindowAttributes(dpy, win, &wa))
  196. eprint("cannot get window attributes");
  197. XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
  198. }
  199. void
  200. grabkeyboard(void) {
  201. unsigned int len;
  202. for(len = 1000; len; len--) {
  203. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  204. == GrabSuccess)
  205. return;
  206. usleep(1000);
  207. }
  208. exit(EXIT_FAILURE);
  209. }
  210. void
  211. kpress(XKeyEvent *e) {
  212. char buf[sizeof text];
  213. int num;
  214. unsigned int i, len;
  215. KeySym ksym;
  216. len = strlen(text);
  217. num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
  218. if(ksym == XK_KP_Enter)
  219. ksym = XK_Return;
  220. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  221. ksym = (ksym - XK_KP_0) + XK_0;
  222. else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  223. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  224. || IsPrivateKeypadKey(ksym))
  225. return;
  226. /* first check if a control mask is omitted */
  227. if(e->state & ControlMask) {
  228. switch(tolower(ksym)) {
  229. default:
  230. return;
  231. case XK_a:
  232. ksym = XK_Home;
  233. break;
  234. case XK_b:
  235. ksym = XK_Left;
  236. break;
  237. case XK_c:
  238. ksym = XK_Escape;
  239. break;
  240. case XK_e:
  241. ksym = XK_End;
  242. break;
  243. case XK_f:
  244. ksym = XK_Right;
  245. break;
  246. case XK_h:
  247. ksym = XK_BackSpace;
  248. break;
  249. case XK_i:
  250. ksym = XK_Tab;
  251. break;
  252. case XK_j:
  253. case XK_m:
  254. ksym = XK_Return;
  255. break;
  256. case XK_n:
  257. ksym = XK_Down;
  258. break;
  259. case XK_p:
  260. ksym = XK_Up;
  261. break;
  262. case XK_u:
  263. text[0] = '\0';
  264. match(text);
  265. break;
  266. case XK_w:
  267. if(len == 0)
  268. return;
  269. i = len;
  270. while(i-- > 0 && text[i] == ' ');
  271. while(i-- > 0 && text[i] != ' ');
  272. text[++i] = '\0';
  273. match(text);
  274. break;
  275. }
  276. }
  277. switch(ksym) {
  278. default:
  279. num = MIN(num, sizeof text);
  280. if(num && !iscntrl((int) buf[0])) {
  281. memcpy(text + len, buf, num + 1);
  282. len += num;
  283. match(text);
  284. }
  285. break;
  286. case XK_BackSpace:
  287. if(len == 0)
  288. return;
  289. for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
  290. len -= i;
  291. text[len] = '\0';
  292. match(text);
  293. break;
  294. case XK_End:
  295. while(next) {
  296. sel = curr = next;
  297. calcoffsets();
  298. }
  299. while(sel && sel->right)
  300. sel = sel->right;
  301. break;
  302. case XK_Escape:
  303. exit(EXIT_FAILURE);
  304. case XK_Home:
  305. sel = curr = item;
  306. calcoffsets();
  307. break;
  308. case XK_Left:
  309. case XK_Up:
  310. if(!sel || !sel->left)
  311. return;
  312. sel = sel->left;
  313. if(sel->right == curr) {
  314. curr = prev;
  315. calcoffsets();
  316. }
  317. break;
  318. case XK_Next:
  319. if(!next)
  320. return;
  321. sel = curr = next;
  322. calcoffsets();
  323. break;
  324. case XK_Prior:
  325. if(!prev)
  326. return;
  327. sel = curr = prev;
  328. calcoffsets();
  329. break;
  330. case XK_Return:
  331. if(e->state & ShiftMask)
  332. dinput();
  333. fprintf(stdout, "%s", sel ? sel->text : text);
  334. fflush(stdout);
  335. exit(EXIT_SUCCESS);
  336. case XK_Right:
  337. case XK_Down:
  338. if(!sel || !sel->right)
  339. return;
  340. sel = sel->right;
  341. if(sel == next) {
  342. curr = next;
  343. calcoffsets();
  344. }
  345. break;
  346. case XK_Tab:
  347. if(sel)
  348. strncpy(text, sel->text, sizeof text);
  349. dinput();
  350. break;
  351. }
  352. drawmenu();
  353. }
  354. void
  355. match(char *pattern) {
  356. unsigned int plen;
  357. Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  358. if(!pattern)
  359. return;
  360. plen = strlen(pattern);
  361. item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  362. for(i = allitems; i; i = i->next)
  363. if(!fstrncmp(pattern, i->text, plen + 1))
  364. appenditem(i, &lexact, &exactend);
  365. else if(!fstrncmp(pattern, i->text, plen))
  366. appenditem(i, &lprefix, &prefixend);
  367. else if(fstrstr(i->text, pattern))
  368. appenditem(i, &lsubstr, &substrend);
  369. if(lexact) {
  370. item = lexact;
  371. itemend = exactend;
  372. }
  373. if(lprefix) {
  374. if(itemend) {
  375. itemend->right = lprefix;
  376. lprefix->left = itemend;
  377. }
  378. else
  379. item = lprefix;
  380. itemend = prefixend;
  381. }
  382. if(lsubstr) {
  383. if(itemend) {
  384. itemend->right = lsubstr;
  385. lsubstr->left = itemend;
  386. }
  387. else
  388. item = lsubstr;
  389. }
  390. curr = prev = next = sel = item;
  391. calcoffsets();
  392. }
  393. void
  394. readstdin(void) {
  395. char *p, buf[sizeof text];
  396. unsigned int len = 0, max = 0;
  397. Item *i, *new;
  398. i = NULL;
  399. while(fgets(buf, sizeof buf, stdin)) {
  400. len = strlen(buf);
  401. if(buf[len-1] == '\n')
  402. buf[--len] = '\0';
  403. if(!(p = strdup(buf)))
  404. eprint("cannot strdup %u bytes\n", len);
  405. if((max = MAX(max, len)) == len)
  406. maxname = p;
  407. if(!(new = malloc(sizeof *new)))
  408. eprint("cannot malloc %u bytes\n", sizeof *new);
  409. new->next = new->left = new->right = NULL;
  410. new->text = p;
  411. if(!i)
  412. allitems = new;
  413. else
  414. i->next = new;
  415. i = new;
  416. }
  417. }
  418. void
  419. run(void) {
  420. XEvent ev;
  421. /* main event loop */
  422. XSync(dpy, False);
  423. while(!XNextEvent(dpy, &ev))
  424. switch(ev.type) {
  425. case KeyPress:
  426. kpress(&ev.xkey);
  427. break;
  428. case Expose:
  429. if(ev.xexpose.count == 0)
  430. drawmenu();
  431. break;
  432. case VisibilityNotify:
  433. if(ev.xvisibility.state != VisibilityUnobscured)
  434. XRaiseWindow(dpy, win);
  435. break;
  436. }
  437. exit(EXIT_FAILURE);
  438. }
  439. void
  440. setup(void) {
  441. int i, j, x, y;
  442. #if XINERAMA
  443. int n;
  444. XineramaScreenInfo *info = NULL;
  445. #endif
  446. XModifierKeymap *modmap;
  447. XSetWindowAttributes wa;
  448. /* init modifier map */
  449. modmap = XGetModifierMapping(dpy);
  450. for(i = 0; i < 8; i++)
  451. for(j = 0; j < modmap->max_keypermod; j++) {
  452. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  453. == XKeysymToKeycode(dpy, XK_Num_Lock))
  454. numlockmask = (1 << i);
  455. }
  456. XFreeModifiermap(modmap);
  457. dc.dpy = dpy;
  458. normcol[ColBG] = getcolor(&dc, normbgcolor);
  459. normcol[ColFG] = getcolor(&dc, normfgcolor);
  460. selcol[ColBG] = getcolor(&dc, selbgcolor);
  461. selcol[ColFG] = getcolor(&dc, selfgcolor);
  462. initfont(&dc, font);
  463. /* menu window */
  464. wa.override_redirect = True;
  465. wa.background_pixmap = ParentRelative;
  466. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  467. /* menu window geometry */
  468. mh = (dc.font.height + 2) * (lines + 1);
  469. #if XINERAMA
  470. if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
  471. i = 0;
  472. if(n > 1) {
  473. int di;
  474. unsigned int dui;
  475. Window dummy;
  476. if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui))
  477. for(i = 0; i < n; i++)
  478. if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
  479. break;
  480. }
  481. x = info[i].x_org;
  482. y = topbar ? info[i].y_org : info[i].y_org + info[i].height - mh;
  483. mw = info[i].width;
  484. XFree(info);
  485. }
  486. else
  487. #endif
  488. {
  489. x = 0;
  490. y = topbar ? 0 : mh - DisplayHeight(dpy, screen);
  491. mw = DisplayWidth(dpy, screen);
  492. }
  493. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  494. DefaultDepth(dpy, screen), CopyFromParent,
  495. DefaultVisual(dpy, screen),
  496. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  497. setupdraw(&dc, win);
  498. if(maxname)
  499. cmdw = MIN(textw(&dc, maxname), mw / 3);
  500. if(prompt)
  501. promptw = MIN(textw(&dc, prompt), mw / 5);
  502. text[0] = '\0';
  503. match(text);
  504. XMapRaised(dpy, win);
  505. }
  506. int
  507. main(int argc, char *argv[]) {
  508. unsigned int i;
  509. /* command line args */
  510. progname = "dmenu";
  511. for(i = 1; i < argc; i++)
  512. if(!strcmp(argv[i], "-i")) {
  513. fstrncmp = strncasecmp;
  514. fstrstr = cistrstr;
  515. }
  516. else if(!strcmp(argv[i], "-b"))
  517. topbar = False;
  518. else if(!strcmp(argv[i], "-l")) {
  519. if(++i < argc) lines = atoi(argv[i]);
  520. if(lines > 0)
  521. calcoffsets = calcoffsetsv;
  522. }
  523. else if(!strcmp(argv[i], "-fn")) {
  524. if(++i < argc) font = argv[i];
  525. }
  526. else if(!strcmp(argv[i], "-nb")) {
  527. if(++i < argc) normbgcolor = argv[i];
  528. }
  529. else if(!strcmp(argv[i], "-nf")) {
  530. if(++i < argc) normfgcolor = argv[i];
  531. }
  532. else if(!strcmp(argv[i], "-p")) {
  533. if(++i < argc) prompt = argv[i];
  534. }
  535. else if(!strcmp(argv[i], "-sb")) {
  536. if(++i < argc) selbgcolor = argv[i];
  537. }
  538. else if(!strcmp(argv[i], "-sf")) {
  539. if(++i < argc) selfgcolor = argv[i];
  540. }
  541. else if(!strcmp(argv[i], "-v")) {
  542. printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
  543. exit(EXIT_SUCCESS);
  544. }
  545. else {
  546. fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
  547. " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
  548. exit(EXIT_FAILURE);
  549. }
  550. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  551. fprintf(stderr, "dmenu: warning: no locale support\n");
  552. if(!(dpy = XOpenDisplay(NULL)))
  553. eprint("cannot open display\n");
  554. if(atexit(&cleanup) != 0)
  555. eprint("cannot register cleanup\n");
  556. screen = DefaultScreen(dpy);
  557. root = RootWindow(dpy, screen);
  558. if(!(argp = malloc(sizeof *argp * (argc+2))))
  559. eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
  560. memcpy(argp + 2, argv + 1, sizeof *argp * argc);
  561. readstdin();
  562. grabkeyboard();
  563. setup();
  564. run();
  565. return 0;
  566. }