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.
 
 
 
 
 
 

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