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

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