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.

622 lines
13 KiB

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