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