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.

dmenu.c 12 KiB

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