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