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