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.

534 lines
12 KiB

18 years ago
16 years ago
18 years ago
14 years ago
18 years ago
18 years ago
18 years ago
14 years ago
16 years ago
13 years ago
14 years ago
14 years ago
17 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
17 years ago
14 years ago
14 years ago
18 years ago
18 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
14 years ago
18 years ago
14 years ago
13 years ago
14 years ago
18 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
14 years ago
14 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
13 years ago
17 years ago
17 years ago
18 years ago
13 years ago
14 years ago
18 years ago
14 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. #ifdef XINERAMA
  11. #include <X11/extensions/Xinerama.h>
  12. #endif
  13. #include "draw.h"
  14. #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
  15. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  16. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  17. typedef struct Item Item;
  18. struct Item {
  19. char *text;
  20. Item *next; /* traverses all items */
  21. Item *left, *right; /* traverses matching items */
  22. };
  23. static void appenditem(Item *item, Item **list, Item **last);
  24. static void calcoffsets(void);
  25. static void drawmenu(void);
  26. static char *fstrstr(const char *s, const char *sub);
  27. static void grabkeyboard(void);
  28. static void insert(const char *s, ssize_t n);
  29. static void keypress(XKeyEvent *ev);
  30. static void match(void);
  31. static size_t nextrune(int incr);
  32. static void paste(void);
  33. static void readstdin(void);
  34. static void run(void);
  35. static void setup(void);
  36. static char text[BUFSIZ] = "";
  37. static int bh, mw, mh;
  38. static int inputw = 0;
  39. static int lines = 0;
  40. static int monitor = -1;
  41. static int promptw;
  42. static size_t cursor = 0;
  43. static const char *font = NULL;
  44. static const char *prompt = NULL;
  45. static const char *normbgcolor = "#cccccc";
  46. static const char *normfgcolor = "#000000";
  47. static const char *selbgcolor = "#0066ff";
  48. static const char *selfgcolor = "#ffffff";
  49. static unsigned long normcol[ColLast];
  50. static unsigned long selcol[ColLast];
  51. static Atom utf8;
  52. static Bool topbar = True;
  53. static DC *dc;
  54. static Item *items = NULL;
  55. static Item *matches, *sel;
  56. static Item *prev, *curr, *next;
  57. static Window root, win;
  58. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  59. int
  60. main(int argc, char *argv[]) {
  61. int i;
  62. progname = "dmenu";
  63. for(i = 1; i < argc; i++)
  64. /* single flags */
  65. if(!strcmp(argv[i], "-v")) {
  66. fputs("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout);
  67. exit(EXIT_SUCCESS);
  68. }
  69. else if(!strcmp(argv[i], "-b"))
  70. topbar = False;
  71. else if(!strcmp(argv[i], "-i"))
  72. fstrncmp = strncasecmp;
  73. else if(i == argc-1)
  74. goto usage;
  75. /* double flags */
  76. else if(!strcmp(argv[i], "-l"))
  77. lines = atoi(argv[++i]);
  78. else if(!strcmp(argv[i], "-m"))
  79. monitor = atoi(argv[++i]);
  80. else if(!strcmp(argv[i], "-p"))
  81. prompt = argv[++i];
  82. else if(!strcmp(argv[i], "-fn"))
  83. font = argv[++i];
  84. else if(!strcmp(argv[i], "-nb"))
  85. normbgcolor = argv[++i];
  86. else if(!strcmp(argv[i], "-nf"))
  87. normfgcolor = argv[++i];
  88. else if(!strcmp(argv[i], "-sb"))
  89. selbgcolor = argv[++i];
  90. else if(!strcmp(argv[i], "-sf"))
  91. selfgcolor = argv[++i];
  92. else
  93. goto usage;
  94. dc = initdc();
  95. initfont(dc, font);
  96. readstdin();
  97. setup();
  98. run();
  99. return EXIT_FAILURE;
  100. usage:
  101. fputs("usage: dmenu [-b] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
  102. " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
  103. return EXIT_FAILURE;
  104. }
  105. void
  106. appenditem(Item *item, Item **list, Item **last) {
  107. if(!*last)
  108. *list = item;
  109. else
  110. (*last)->right = item;
  111. item->left = *last;
  112. item->right = NULL;
  113. *last = item;
  114. }
  115. void
  116. calcoffsets(void) {
  117. unsigned int i, n;
  118. if(lines > 0)
  119. n = lines * bh;
  120. else
  121. n = mw - (promptw + inputw + textw(dc, "<") + textw(dc, ">"));
  122. for(i = 0, next = curr; next; next = next->right)
  123. if((i += (lines > 0) ? bh : MIN(textw(dc, next->text), n)) > n)
  124. break;
  125. for(i = 0, prev = curr; prev && prev->left; prev = prev->left)
  126. if((i += (lines > 0) ? bh : MIN(textw(dc, prev->left->text), n)) > n)
  127. break;
  128. }
  129. void
  130. drawmenu(void) {
  131. int curpos;
  132. Item *item;
  133. dc->x = 0;
  134. dc->y = 0;
  135. dc->h = bh;
  136. drawrect(dc, 0, 0, mw, mh, True, BG(dc, normcol));
  137. if(prompt) {
  138. dc->w = promptw;
  139. drawtext(dc, prompt, selcol);
  140. dc->x = dc->w;
  141. }
  142. dc->w = (lines > 0 || !matches) ? mw - dc->x : inputw;
  143. drawtext(dc, text, normcol);
  144. if((curpos = textnw(dc, text, cursor) + dc->h/2 - 2) < dc->w)
  145. drawrect(dc, curpos, 2, 1, dc->h - 4, True, FG(dc, normcol));
  146. if(lines > 0) {
  147. dc->w = mw - dc->x;
  148. for(item = curr; item != next; item = item->right) {
  149. dc->y += dc->h;
  150. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  151. }
  152. }
  153. else if(matches) {
  154. dc->x += inputw;
  155. dc->w = textw(dc, "<");
  156. if(curr->left)
  157. drawtext(dc, "<", normcol);
  158. for(item = curr; item != next; item = item->right) {
  159. dc->x += dc->w;
  160. dc->w = MIN(textw(dc, item->text), mw - dc->x - textw(dc, ">"));
  161. drawtext(dc, item->text, (item == sel) ? selcol : normcol);
  162. }
  163. dc->w = textw(dc, ">");
  164. dc->x = mw - dc->w;
  165. if(next)
  166. drawtext(dc, ">", normcol);
  167. }
  168. mapdc(dc, win, mw, mh);
  169. }
  170. char *
  171. fstrstr(const char *s, const char *sub) {
  172. size_t len;
  173. for(len = strlen(sub); *s; s++)
  174. if(!fstrncmp(s, sub, len))
  175. return (char *)s;
  176. return NULL;
  177. }
  178. void
  179. grabkeyboard(void) {
  180. int i;
  181. for(i = 0; i < 1000; i++) {
  182. if(!XGrabKeyboard(dc->dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
  183. return;
  184. usleep(1000);
  185. }
  186. eprintf("cannot grab keyboard\n");
  187. }
  188. void
  189. insert(const char *s, ssize_t n) {
  190. if(strlen(text) + n > sizeof text - 1)
  191. return;
  192. memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
  193. if(n > 0)
  194. memcpy(text + cursor, s, n);
  195. cursor += n;
  196. match();
  197. }
  198. void
  199. keypress(XKeyEvent *ev) {
  200. char buf[32];
  201. size_t len;
  202. KeySym ksym;
  203. len = strlen(text);
  204. XLookupString(ev, buf, sizeof buf, &ksym, NULL);
  205. if(ev->state & ControlMask)
  206. switch(tolower(ksym)) {
  207. default:
  208. return;
  209. case XK_a:
  210. ksym = XK_Home;
  211. break;
  212. case XK_b:
  213. ksym = XK_Left;
  214. break;
  215. case XK_c:
  216. ksym = XK_Escape;
  217. break;
  218. case XK_d:
  219. ksym = XK_Delete;
  220. break;
  221. case XK_e:
  222. ksym = XK_End;
  223. break;
  224. case XK_f:
  225. ksym = XK_Right;
  226. break;
  227. case XK_h:
  228. ksym = XK_BackSpace;
  229. break;
  230. case XK_i:
  231. ksym = XK_Tab;
  232. break;
  233. case XK_j:
  234. ksym = XK_Return;
  235. break;
  236. case XK_k: /* delete right */
  237. text[cursor] = '\0';
  238. match();
  239. break;
  240. case XK_n:
  241. ksym = XK_Down;
  242. break;
  243. case XK_p:
  244. ksym = XK_Up;
  245. break;
  246. case XK_u: /* delete left */
  247. insert(NULL, 0 - cursor);
  248. break;
  249. case XK_w: /* delete word */
  250. while(cursor > 0 && text[nextrune(-1)] == ' ')
  251. insert(NULL, nextrune(-1) - cursor);
  252. while(cursor > 0 && text[nextrune(-1)] != ' ')
  253. insert(NULL, nextrune(-1) - cursor);
  254. break;
  255. case XK_y: /* paste selection */
  256. XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
  257. return;
  258. }
  259. switch(ksym) {
  260. default:
  261. if(!iscntrl(*buf))
  262. insert(buf, strlen(buf));
  263. break;
  264. case XK_Delete:
  265. if(cursor == len)
  266. return;
  267. cursor = nextrune(+1);
  268. case XK_BackSpace:
  269. if(cursor > 0)
  270. insert(NULL, nextrune(-1) - cursor);
  271. break;
  272. case XK_End:
  273. if(cursor < len) {
  274. cursor = len;
  275. break;
  276. }
  277. while(next) {
  278. sel = curr = next;
  279. calcoffsets();
  280. }
  281. while(sel && sel->right)
  282. sel = sel->right;
  283. break;
  284. case XK_Escape:
  285. exit(EXIT_FAILURE);
  286. case XK_Home:
  287. if(sel == matches) {
  288. cursor = 0;
  289. break;
  290. }
  291. sel = curr = matches;
  292. calcoffsets();
  293. break;
  294. case XK_Left:
  295. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  296. cursor = nextrune(-1);
  297. break;
  298. }
  299. else if(lines > 0)
  300. return;
  301. case XK_Up:
  302. if(sel && sel->left && (sel = sel->left)->right == curr) {
  303. curr = prev;
  304. calcoffsets();
  305. }
  306. break;
  307. case XK_Next:
  308. if(!next)
  309. return;
  310. sel = curr = next;
  311. calcoffsets();
  312. break;
  313. case XK_Prior:
  314. if(!prev)
  315. return;
  316. sel = curr = prev;
  317. calcoffsets();
  318. break;
  319. case XK_Return:
  320. case XK_KP_Enter:
  321. fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
  322. exit(EXIT_SUCCESS);
  323. case XK_Right:
  324. if(cursor < len) {
  325. cursor = nextrune(+1);
  326. break;
  327. }
  328. else if(lines > 0)
  329. return;
  330. case XK_Down:
  331. if(sel && sel->right && (sel = sel->right) == next) {
  332. curr = next;
  333. calcoffsets();
  334. }
  335. break;
  336. case XK_Tab:
  337. if(!sel)
  338. return;
  339. strncpy(text, sel->text, sizeof text);
  340. cursor = strlen(text);
  341. match();
  342. break;
  343. }
  344. drawmenu();
  345. }
  346. void
  347. match(void) {
  348. size_t len;
  349. Item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  350. len = strlen(text);
  351. matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
  352. for(item = items; item; item = item->next)
  353. if(!fstrncmp(text, item->text, len + 1))
  354. appenditem(item, &lexact, &exactend);
  355. else if(!fstrncmp(text, item->text, len))
  356. appenditem(item, &lprefix, &prefixend);
  357. else if(fstrstr(item->text, text))
  358. appenditem(item, &lsubstr, &substrend);
  359. if(lexact) {
  360. matches = lexact;
  361. itemend = exactend;
  362. }
  363. if(lprefix) {
  364. if(itemend) {
  365. itemend->right = lprefix;
  366. lprefix->left = itemend;
  367. }
  368. else
  369. matches = lprefix;
  370. itemend = prefixend;
  371. }
  372. if(lsubstr) {
  373. if(itemend) {
  374. itemend->right = lsubstr;
  375. lsubstr->left = itemend;
  376. }
  377. else
  378. matches = lsubstr;
  379. }
  380. curr = sel = matches;
  381. calcoffsets();
  382. }
  383. size_t
  384. nextrune(int incr) {
  385. size_t n, len;
  386. len = strlen(text);
  387. for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
  388. return n;
  389. }
  390. void
  391. paste(void) {
  392. char *p, *q;
  393. int di;
  394. unsigned long dl;
  395. Atom da;
  396. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  397. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  398. insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
  399. XFree(p);
  400. drawmenu();
  401. }
  402. void
  403. readstdin(void) {
  404. char buf[sizeof text], *p;
  405. Item *item, **end;
  406. for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
  407. if((p = strchr(buf, '\n')))
  408. *p = '\0';
  409. if(!(item = calloc(1, sizeof *item)))
  410. eprintf("cannot malloc %u bytes\n", sizeof *item);
  411. if(!(item->text = strdup(buf)))
  412. eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
  413. inputw = MAX(inputw, textw(dc, item->text));
  414. }
  415. }
  416. void
  417. run(void) {
  418. XEvent ev;
  419. while(!XNextEvent(dc->dpy, &ev))
  420. switch(ev.type) {
  421. case Expose:
  422. if(ev.xexpose.count == 0)
  423. drawmenu();
  424. break;
  425. case KeyPress:
  426. keypress(&ev.xkey);
  427. break;
  428. case SelectionNotify:
  429. if(ev.xselection.property == utf8)
  430. paste();
  431. break;
  432. case VisibilityNotify:
  433. if(ev.xvisibility.state != VisibilityUnobscured)
  434. XRaiseWindow(dc->dpy, win);
  435. break;
  436. }
  437. }
  438. void
  439. setup(void) {
  440. int x, y, screen;
  441. XSetWindowAttributes wa;
  442. #ifdef XINERAMA
  443. int n;
  444. XineramaScreenInfo *info;
  445. #endif
  446. screen = DefaultScreen(dc->dpy);
  447. root = RootWindow(dc->dpy, screen);
  448. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  449. normcol[ColBG] = getcolor(dc, normbgcolor);
  450. normcol[ColFG] = getcolor(dc, normfgcolor);
  451. selcol[ColBG] = getcolor(dc, selbgcolor);
  452. selcol[ColFG] = getcolor(dc, selfgcolor);
  453. /* menu geometry */
  454. bh = dc->font.height + 2;
  455. lines = MAX(lines, 0);
  456. mh = (lines + 1) * bh;
  457. #ifdef XINERAMA
  458. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  459. int i, di;
  460. unsigned int du;
  461. Window dw;
  462. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  463. for(i = 0; i < n; i++)
  464. if((monitor == info[i].screen_number)
  465. || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
  466. break;
  467. x = info[i].x_org;
  468. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  469. mw = info[i].width;
  470. XFree(info);
  471. }
  472. else
  473. #endif
  474. {
  475. x = 0;
  476. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  477. mw = DisplayWidth(dc->dpy, screen);
  478. }
  479. /* menu window */
  480. wa.override_redirect = True;
  481. wa.background_pixmap = ParentRelative;
  482. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  483. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  484. DefaultDepth(dc->dpy, screen), CopyFromParent,
  485. DefaultVisual(dc->dpy, screen),
  486. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  487. grabkeyboard();
  488. resizedc(dc, mw, mh);
  489. inputw = MIN(inputw, mw/3);
  490. promptw = prompt ? textw(dc, prompt) : 0;
  491. XMapRaised(dc->dpy, win);
  492. match();
  493. }