My dmenu build
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.
 
 
 
 
 
 

538 lines
12 KiB

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