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.

612 lines
13 KiB

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