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.

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