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.

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