My dmenu build
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 13 anos
há 18 anos
há 16 anos
há 18 anos
há 14 anos
há 18 anos
há 18 anos
há 18 anos
há 16 anos
há 13 anos
há 14 anos
há 13 anos
há 14 anos
há 17 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 17 anos
há 14 anos
há 18 anos
há 18 anos
há 14 anos
há 14 anos
há 14 anos
há 17 anos
há 18 anos
há 13 anos
há 14 anos
há 18 anos
há 18 anos
há 18 anos
há 18 anos
há 13 anos
há 13 anos
há 13 anos
há 14 anos
há 18 anos
há 18 anos
há 18 anos
há 18 anos
há 14 anos
há 18 anos
há 18 anos
há 18 anos
há 17 anos
há 13 anos
há 13 anos
há 17 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 17 anos
há 13 anos
há 17 anos
há 13 anos
há 18 anos
há 13 anos
há 13 anos
há 13 anos
há 18 anos
há 13 anos
há 18 anos
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 = 0;
  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 root, 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 == argc-1)
  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. setup();
  99. readstdin();
  100. }
  101. else {
  102. readstdin();
  103. setup();
  104. }
  105. match();
  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, root, True, GrabModeAsync, GrabModeAsync, CurrentTime))
  191. return;
  192. usleep(1000);
  193. }
  194. eprintf("cannot grab keyboard\n");
  195. }
  196. void
  197. insert(const char *s, ssize_t n) {
  198. if(strlen(text) + n > sizeof text - 1)
  199. return;
  200. memmove(text + cursor + n, text + cursor, sizeof text - cursor - MAX(n, 0));
  201. if(n > 0)
  202. memcpy(text + cursor, s, n);
  203. cursor += n;
  204. match();
  205. }
  206. void
  207. keypress(XKeyEvent *ev) {
  208. char buf[32];
  209. size_t len;
  210. KeySym ksym;
  211. len = strlen(text);
  212. XLookupString(ev, buf, sizeof buf, &ksym, NULL);
  213. if(ev->state & ControlMask)
  214. switch(tolower(ksym)) {
  215. default:
  216. return;
  217. case XK_a:
  218. ksym = XK_Home;
  219. break;
  220. case XK_b:
  221. ksym = XK_Left;
  222. break;
  223. case XK_c:
  224. ksym = XK_Escape;
  225. break;
  226. case XK_d:
  227. ksym = XK_Delete;
  228. break;
  229. case XK_e:
  230. ksym = XK_End;
  231. break;
  232. case XK_f:
  233. ksym = XK_Right;
  234. break;
  235. case XK_h:
  236. ksym = XK_BackSpace;
  237. break;
  238. case XK_i:
  239. ksym = XK_Tab;
  240. break;
  241. case XK_j:
  242. ksym = XK_Return;
  243. break;
  244. case XK_k: /* delete right */
  245. text[cursor] = '\0';
  246. match();
  247. break;
  248. case XK_n:
  249. ksym = XK_Down;
  250. break;
  251. case XK_p:
  252. ksym = XK_Up;
  253. break;
  254. case XK_u: /* delete left */
  255. insert(NULL, 0 - cursor);
  256. break;
  257. case XK_w: /* delete word */
  258. while(cursor > 0 && text[nextrune(-1)] == ' ')
  259. insert(NULL, nextrune(-1) - cursor);
  260. while(cursor > 0 && text[nextrune(-1)] != ' ')
  261. insert(NULL, nextrune(-1) - cursor);
  262. break;
  263. case XK_y: /* paste selection */
  264. XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
  265. return;
  266. }
  267. switch(ksym) {
  268. default:
  269. if(!iscntrl(*buf))
  270. insert(buf, strlen(buf));
  271. break;
  272. case XK_Delete:
  273. if(cursor == len)
  274. return;
  275. cursor = nextrune(+1);
  276. case XK_BackSpace:
  277. if(cursor > 0)
  278. insert(NULL, nextrune(-1) - cursor);
  279. break;
  280. case XK_End:
  281. if(cursor < len) {
  282. cursor = len;
  283. break;
  284. }
  285. if(next) {
  286. curr = matchend;
  287. calcoffsets();
  288. curr = prev;
  289. calcoffsets();
  290. while(next && (curr = curr->right))
  291. calcoffsets();
  292. }
  293. sel = matchend;
  294. break;
  295. case XK_Escape:
  296. exit(EXIT_FAILURE);
  297. case XK_Home:
  298. if(sel == matches) {
  299. cursor = 0;
  300. break;
  301. }
  302. sel = curr = matches;
  303. calcoffsets();
  304. break;
  305. case XK_Left:
  306. if(cursor > 0 && (!sel || !sel->left || lines > 0)) {
  307. cursor = nextrune(-1);
  308. break;
  309. }
  310. else if(lines > 0)
  311. return;
  312. case XK_Up:
  313. if(sel && sel->left && (sel = sel->left)->right == curr) {
  314. curr = prev;
  315. calcoffsets();
  316. }
  317. break;
  318. case XK_Next:
  319. if(!next)
  320. return;
  321. sel = curr = next;
  322. calcoffsets();
  323. break;
  324. case XK_Prior:
  325. if(!prev)
  326. return;
  327. sel = curr = prev;
  328. calcoffsets();
  329. break;
  330. case XK_Return:
  331. case XK_KP_Enter:
  332. fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
  333. exit(EXIT_SUCCESS);
  334. case XK_Right:
  335. if(cursor < len) {
  336. cursor = nextrune(+1);
  337. break;
  338. }
  339. else if(lines > 0)
  340. return;
  341. case XK_Down:
  342. if(sel && sel->right && (sel = sel->right) == next) {
  343. curr = next;
  344. calcoffsets();
  345. }
  346. break;
  347. case XK_Tab:
  348. if(!sel)
  349. return;
  350. strncpy(text, sel->text, sizeof text);
  351. cursor = strlen(text);
  352. match();
  353. break;
  354. }
  355. drawmenu();
  356. }
  357. void
  358. match(void) {
  359. size_t len = strlen(text);
  360. Item *item, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
  361. matches = lexact = lprefix = lsubstr = matchend = exactend = prefixend = substrend = NULL;
  362. for(item = items; item && item->text; item++)
  363. if(!fstrncmp(text, item->text, len + 1))
  364. appenditem(item, &lexact, &exactend);
  365. else if(!fstrncmp(text, item->text, len))
  366. appenditem(item, &lprefix, &prefixend);
  367. else if(fstrstr(item->text, text))
  368. appenditem(item, &lsubstr, &substrend);
  369. if(lexact) {
  370. matches = lexact;
  371. matchend = exactend;
  372. }
  373. if(lprefix) {
  374. if(matchend) {
  375. matchend->right = lprefix;
  376. lprefix->left = matchend;
  377. }
  378. else
  379. matches = lprefix;
  380. matchend = prefixend;
  381. }
  382. if(lsubstr) {
  383. if(matchend) {
  384. matchend->right = lsubstr;
  385. lsubstr->left = matchend;
  386. }
  387. else
  388. matches = lsubstr;
  389. matchend = substrend;
  390. }
  391. curr = sel = matches;
  392. calcoffsets();
  393. }
  394. size_t
  395. nextrune(int incr) {
  396. size_t n, len = strlen(text);
  397. for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr);
  398. return n;
  399. }
  400. void
  401. paste(void) {
  402. char *p, *q;
  403. int di;
  404. unsigned long dl;
  405. Atom da;
  406. XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
  407. utf8, &da, &di, &dl, &dl, (unsigned char **)&p);
  408. insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p));
  409. XFree(p);
  410. drawmenu();
  411. }
  412. void
  413. readstdin(void) {
  414. char buf[sizeof text], *p, *maxstr = NULL;
  415. size_t i, max = 0, size = 0;
  416. for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) {
  417. if(i+1 >= size / sizeof *items)
  418. if(!(items = realloc(items, (size += BUFSIZ))))
  419. eprintf("cannot realloc %u bytes:", size);
  420. if((p = strchr(buf, '\n')))
  421. *p = '\0';
  422. if(!(items[i].text = strdup(buf)))
  423. eprintf("cannot strdup %u bytes:", strlen(buf)+1);
  424. if(strlen(items[i].text) > max)
  425. max = strlen(maxstr = items[i].text);
  426. }
  427. if(maxstr)
  428. inputw = textw(dc, maxstr);
  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;
  455. XSetWindowAttributes wa;
  456. #ifdef XINERAMA
  457. int n;
  458. XineramaScreenInfo *info;
  459. #endif
  460. screen = DefaultScreen(dc->dpy);
  461. root = RootWindow(dc->dpy, screen);
  462. utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
  463. normcol[ColBG] = getcolor(dc, normbgcolor);
  464. normcol[ColFG] = getcolor(dc, normfgcolor);
  465. selcol[ColBG] = getcolor(dc, selbgcolor);
  466. selcol[ColFG] = getcolor(dc, selfgcolor);
  467. /* menu geometry */
  468. bh = dc->font.height + 2;
  469. lines = MAX(lines, 0);
  470. mh = (lines + 1) * bh;
  471. #ifdef XINERAMA
  472. if((info = XineramaQueryScreens(dc->dpy, &n))) {
  473. int i, di;
  474. unsigned int du;
  475. Window dw;
  476. XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
  477. for(i = 0; i < n-1; i++)
  478. if((monitor == info[i].screen_number)
  479. || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
  480. break;
  481. x = info[i].x_org;
  482. y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
  483. mw = info[i].width;
  484. XFree(info);
  485. }
  486. else
  487. #endif
  488. {
  489. x = 0;
  490. y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
  491. mw = DisplayWidth(dc->dpy, screen);
  492. }
  493. /* menu window */
  494. wa.override_redirect = True;
  495. wa.background_pixmap = ParentRelative;
  496. wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  497. win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
  498. DefaultDepth(dc->dpy, screen), CopyFromParent,
  499. DefaultVisual(dc->dpy, screen),
  500. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  501. grabkeyboard();
  502. resizedc(dc, mw, mh);
  503. inputw = MIN(inputw, mw/3);
  504. promptw = prompt ? textw(dc, prompt) : 0;
  505. XMapRaised(dc->dpy, win);
  506. }