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.
 
 
 
 
 
 

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