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.

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