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.
 
 
 
 
 
 

543 lines
12 KiB

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