dmenu for lunch applications in dwm
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.

734 lines
15 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
17 years ago
17 years ago
17 years ago
18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <ctype.h>
  3. #include <locale.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <X11/Xlib.h>
  10. #include <X11/Xutil.h>
  11. #include <X11/keysym.h>
  12. /* macros */
  13. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  14. /* enums */
  15. enum { ColFG, ColBG, ColLast };
  16. /* typedefs */
  17. typedef struct {
  18. int x, y, w, h;
  19. unsigned long norm[ColLast];
  20. unsigned long sel[ColLast];
  21. Drawable drawable;
  22. GC gc;
  23. struct {
  24. XFontStruct *xfont;
  25. XFontSet set;
  26. int ascent;
  27. int descent;
  28. int height;
  29. } font;
  30. } DC; /* draw context */
  31. typedef struct Item Item;
  32. struct Item {
  33. char *text;
  34. Bool matched;
  35. Item *next; /* traverses all items */
  36. Item *left, *right; /* traverses items matching current search pattern */
  37. };
  38. /* forward declarations */
  39. Item *appenditem(Item *i, Item *last);
  40. void calcoffsets(void);
  41. char *cistrstr(const char *s, const char *sub);
  42. void cleanup(void);
  43. void drawmenu(void);
  44. void drawtext(const char *text, unsigned long col[ColLast]);
  45. void *emalloc(unsigned int size);
  46. void eprint(const char *errstr, ...);
  47. char *estrdup(const char *str);
  48. unsigned long getcolor(const char *colstr);
  49. Bool grabkeyboard(void);
  50. void initfont(const char *fontstr);
  51. void kpress(XKeyEvent * e);
  52. void match(char *pattern);
  53. void readstdin(void);
  54. void run(void);
  55. void setup(int x, int y, int w);
  56. unsigned int textnw(const char *text, unsigned int len);
  57. unsigned int textw(const char *text);
  58. #include "config.h"
  59. /* variables */
  60. char *font = FONT;
  61. char *maxname = NULL;
  62. char *normbg = NORMBGCOLOR;
  63. char *normfg = NORMFGCOLOR;
  64. char *prompt = NULL;
  65. char *selbg = SELBGCOLOR;
  66. char *selfg = SELFGCOLOR;
  67. char text[4096];
  68. int screen;
  69. int ret = 0;
  70. unsigned int cmdw = 0;
  71. unsigned int mw, mh;
  72. unsigned int promptw = 0;
  73. unsigned int nitem = 0;
  74. unsigned int numlockmask = 0;
  75. Bool running = True;
  76. Display *dpy;
  77. DC dc = {0};
  78. Item *allitems = NULL; /* first of all items */
  79. Item *item = NULL; /* first of pattern matching items */
  80. Item *sel = NULL;
  81. Item *next = NULL;
  82. Item *prev = NULL;
  83. Item *curr = NULL;
  84. Window root, win;
  85. int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
  86. char *(*fstrstr)(const char *, const char *) = strstr;
  87. Item *
  88. appenditem(Item *i, Item *last) {
  89. if(!last)
  90. item = i;
  91. else
  92. last->right = i;
  93. i->left = last;
  94. i->right = NULL;
  95. last = i;
  96. nitem++;
  97. return last;
  98. }
  99. void
  100. calcoffsets(void) {
  101. unsigned int tw, w;
  102. if(!curr)
  103. return;
  104. w = promptw + cmdw + 2 * SPACE;
  105. for(next = curr; next; next=next->right) {
  106. tw = textw(next->text);
  107. if(tw > mw / 3)
  108. tw = mw / 3;
  109. w += tw;
  110. if(w > mw)
  111. break;
  112. }
  113. w = promptw + cmdw + 2 * SPACE;
  114. for(prev = curr; prev && prev->left; prev=prev->left) {
  115. tw = textw(prev->left->text);
  116. if(tw > mw / 3)
  117. tw = mw / 3;
  118. w += tw;
  119. if(w > mw)
  120. break;
  121. }
  122. }
  123. char *
  124. cistrstr(const char *s, const char *sub) {
  125. int c, csub;
  126. unsigned int len;
  127. if(!sub)
  128. return (char *)s;
  129. if((c = *sub++) != 0) {
  130. c = tolower(c);
  131. len = strlen(sub);
  132. do {
  133. do {
  134. if((csub = *s++) == 0)
  135. return (NULL);
  136. }
  137. while(tolower(csub) != c);
  138. }
  139. while(strncasecmp(s, sub, len) != 0);
  140. s--;
  141. }
  142. return (char *)s;
  143. }
  144. void
  145. cleanup(void) {
  146. Item *itm;
  147. while(allitems) {
  148. itm = allitems->next;
  149. free(allitems->text);
  150. free(allitems);
  151. allitems = itm;
  152. }
  153. if(dc.font.set)
  154. XFreeFontSet(dpy, dc.font.set);
  155. else
  156. XFreeFont(dpy, dc.font.xfont);
  157. XFreePixmap(dpy, dc.drawable);
  158. XFreeGC(dpy, dc.gc);
  159. XDestroyWindow(dpy, win);
  160. XUngrabKeyboard(dpy, CurrentTime);
  161. }
  162. void
  163. drawmenu(void) {
  164. Item *i;
  165. dc.x = 0;
  166. dc.y = 0;
  167. dc.w = mw;
  168. dc.h = mh;
  169. drawtext(NULL, dc.norm);
  170. /* print prompt? */
  171. if(promptw) {
  172. dc.w = promptw;
  173. drawtext(prompt, dc.sel);
  174. }
  175. dc.x += promptw;
  176. dc.w = mw - promptw;
  177. /* print command */
  178. if(cmdw && item)
  179. dc.w = cmdw;
  180. drawtext(text[0] ? text : NULL, dc.norm);
  181. dc.x += cmdw;
  182. if(curr) {
  183. dc.w = SPACE;
  184. drawtext((curr && curr->left) ? "<" : NULL, dc.norm);
  185. dc.x += dc.w;
  186. /* determine maximum items */
  187. for(i = curr; i != next; i=i->right) {
  188. dc.w = textw(i->text);
  189. if(dc.w > mw / 3)
  190. dc.w = mw / 3;
  191. drawtext(i->text, (sel == i) ? dc.sel : dc.norm);
  192. dc.x += dc.w;
  193. }
  194. dc.x = mw - SPACE;
  195. dc.w = SPACE;
  196. drawtext(next ? ">" : NULL, dc.norm);
  197. }
  198. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
  199. XFlush(dpy);
  200. }
  201. void
  202. drawtext(const char *text, unsigned long col[ColLast]) {
  203. int x, y, w, h;
  204. static char buf[256];
  205. unsigned int len, olen;
  206. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  207. XSetForeground(dpy, dc.gc, col[ColBG]);
  208. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  209. if(!text)
  210. return;
  211. w = 0;
  212. olen = len = strlen(text);
  213. if(len >= sizeof buf)
  214. len = sizeof buf - 1;
  215. memcpy(buf, text, len);
  216. buf[len] = 0;
  217. h = dc.font.ascent + dc.font.descent;
  218. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  219. x = dc.x + (h / 2);
  220. /* shorten text if necessary */
  221. while(len && (w = textnw(buf, len)) > dc.w - h)
  222. buf[--len] = 0;
  223. if(len < olen) {
  224. if(len > 1)
  225. buf[len - 1] = '.';
  226. if(len > 2)
  227. buf[len - 2] = '.';
  228. if(len > 3)
  229. buf[len - 3] = '.';
  230. }
  231. if(w > dc.w)
  232. return; /* too long */
  233. XSetForeground(dpy, dc.gc, col[ColFG]);
  234. if(dc.font.set)
  235. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  236. else
  237. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  238. }
  239. void *
  240. emalloc(unsigned int size) {
  241. void *res = malloc(size);
  242. if(!res)
  243. eprint("fatal: could not malloc() %u bytes\n", size);
  244. return res;
  245. }
  246. void
  247. eprint(const char *errstr, ...) {
  248. va_list ap;
  249. va_start(ap, errstr);
  250. vfprintf(stderr, errstr, ap);
  251. va_end(ap);
  252. exit(EXIT_FAILURE);
  253. }
  254. char *
  255. estrdup(const char *str) {
  256. void *res = strdup(str);
  257. if(!res)
  258. eprint("fatal: could not malloc() %u bytes\n", strlen(str));
  259. return res;
  260. }
  261. unsigned long
  262. getcolor(const char *colstr) {
  263. Colormap cmap = DefaultColormap(dpy, screen);
  264. XColor color;
  265. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  266. eprint("error, cannot allocate color '%s'\n", colstr);
  267. return color.pixel;
  268. }
  269. Bool
  270. grabkeyboard(void) {
  271. unsigned int len;
  272. for(len = 1000; len; len--) {
  273. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  274. == GrabSuccess)
  275. break;
  276. usleep(1000);
  277. }
  278. return len > 0;
  279. }
  280. void
  281. initfont(const char *fontstr) {
  282. char *def, **missing;
  283. int i, n;
  284. if(!fontstr || fontstr[0] == '\0')
  285. eprint("error, cannot load font: '%s'\n", fontstr);
  286. missing = NULL;
  287. if(dc.font.set)
  288. XFreeFontSet(dpy, dc.font.set);
  289. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  290. if(missing)
  291. XFreeStringList(missing);
  292. if(dc.font.set) {
  293. XFontSetExtents *font_extents;
  294. XFontStruct **xfonts;
  295. char **font_names;
  296. dc.font.ascent = dc.font.descent = 0;
  297. font_extents = XExtentsOfFontSet(dc.font.set);
  298. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  299. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  300. if(dc.font.ascent < (*xfonts)->ascent)
  301. dc.font.ascent = (*xfonts)->ascent;
  302. if(dc.font.descent < (*xfonts)->descent)
  303. dc.font.descent = (*xfonts)->descent;
  304. xfonts++;
  305. }
  306. }
  307. else {
  308. if(dc.font.xfont)
  309. XFreeFont(dpy, dc.font.xfont);
  310. dc.font.xfont = NULL;
  311. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  312. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  313. eprint("error, cannot load font: '%s'\n", fontstr);
  314. dc.font.ascent = dc.font.xfont->ascent;
  315. dc.font.descent = dc.font.xfont->descent;
  316. }
  317. dc.font.height = dc.font.ascent + dc.font.descent;
  318. }
  319. void
  320. kpress(XKeyEvent * e) {
  321. char buf[32];
  322. int i, num;
  323. unsigned int len;
  324. KeySym ksym;
  325. len = strlen(text);
  326. buf[0] = 0;
  327. num = XLookupString(e, buf, sizeof buf, &ksym, 0);
  328. if(IsKeypadKey(ksym)) {
  329. if(ksym == XK_KP_Enter) {
  330. ksym = XK_Return;
  331. } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
  332. ksym = (ksym - XK_KP_0) + XK_0;
  333. }
  334. }
  335. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  336. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  337. || IsPrivateKeypadKey(ksym))
  338. return;
  339. /* first check if a control mask is omitted */
  340. if(e->state & ControlMask) {
  341. switch (ksym) {
  342. default: /* ignore other control sequences */
  343. return;
  344. case XK_bracketleft:
  345. ksym = XK_Escape;
  346. break;
  347. case XK_h:
  348. case XK_H:
  349. ksym = XK_BackSpace;
  350. break;
  351. case XK_i:
  352. case XK_I:
  353. ksym = XK_Tab;
  354. break;
  355. case XK_j:
  356. case XK_J:
  357. ksym = XK_Return;
  358. break;
  359. case XK_u:
  360. case XK_U:
  361. text[0] = 0;
  362. match(text);
  363. drawmenu();
  364. return;
  365. case XK_w:
  366. case XK_W:
  367. if(len) {
  368. i = len - 1;
  369. while(i >= 0 && text[i] == ' ')
  370. text[i--] = 0;
  371. while(i >= 0 && text[i] != ' ')
  372. text[i--] = 0;
  373. match(text);
  374. drawmenu();
  375. }
  376. return;
  377. }
  378. }
  379. if(CLEANMASK(e->state) & Mod1Mask) {
  380. switch(ksym) {
  381. default: return;
  382. case XK_h:
  383. ksym = XK_Left;
  384. break;
  385. case XK_l:
  386. ksym = XK_Right;
  387. break;
  388. case XK_j:
  389. ksym = XK_Next;
  390. break;
  391. case XK_k:
  392. ksym = XK_Prior;
  393. break;
  394. case XK_g:
  395. ksym = XK_Home;
  396. break;
  397. case XK_G:
  398. ksym = XK_End;
  399. break;
  400. }
  401. }
  402. switch(ksym) {
  403. default:
  404. if(num && !iscntrl((int) buf[0])) {
  405. buf[num] = 0;
  406. if(len > 0)
  407. strncat(text, buf, sizeof text);
  408. else
  409. strncpy(text, buf, sizeof text);
  410. match(text);
  411. }
  412. break;
  413. case XK_BackSpace:
  414. if(len) {
  415. text[--len] = 0;
  416. match(text);
  417. }
  418. break;
  419. case XK_End:
  420. if(!item)
  421. return;
  422. while(next) {
  423. sel = curr = next;
  424. calcoffsets();
  425. }
  426. while(sel && sel->right)
  427. sel = sel->right;
  428. break;
  429. case XK_Escape:
  430. ret = 1;
  431. running = False;
  432. break;
  433. case XK_Home:
  434. if(!item)
  435. return;
  436. sel = curr = item;
  437. calcoffsets();
  438. break;
  439. case XK_Left:
  440. if(!(sel && sel->left))
  441. return;
  442. sel=sel->left;
  443. if(sel->right == curr) {
  444. curr = prev;
  445. calcoffsets();
  446. }
  447. break;
  448. case XK_Next:
  449. if(!next)
  450. return;
  451. sel = curr = next;
  452. calcoffsets();
  453. break;
  454. case XK_Prior:
  455. if(!prev)
  456. return;
  457. sel = curr = prev;
  458. calcoffsets();
  459. break;
  460. case XK_Return:
  461. if((e->state & ShiftMask) && text)
  462. fprintf(stdout, "%s", text);
  463. else if(sel)
  464. fprintf(stdout, "%s", sel->text);
  465. else if(text)
  466. fprintf(stdout, "%s", text);
  467. fflush(stdout);
  468. running = False;
  469. break;
  470. case XK_Right:
  471. if(!(sel && sel->right))
  472. return;
  473. sel=sel->right;
  474. if(sel == next) {
  475. curr = next;
  476. calcoffsets();
  477. }
  478. break;
  479. case XK_Tab:
  480. if(!sel)
  481. return;
  482. strncpy(text, sel->text, sizeof text);
  483. match(text);
  484. break;
  485. }
  486. drawmenu();
  487. }
  488. void
  489. match(char *pattern) {
  490. unsigned int plen;
  491. Item *i, *j;
  492. if(!pattern)
  493. return;
  494. plen = strlen(pattern);
  495. item = j = NULL;
  496. nitem = 0;
  497. for(i = allitems; i; i = i->next)
  498. if((i->matched = !fstrncmp(pattern, i->text, plen)))
  499. j = appenditem(i, j);
  500. for(i = allitems; i; i = i->next)
  501. if(!i->matched && fstrstr(i->text, pattern))
  502. j = appenditem(i, j);
  503. curr = prev = next = sel = item;
  504. calcoffsets();
  505. }
  506. void
  507. readstdin(void) {
  508. char *p, buf[1024];
  509. unsigned int len = 0, max = 0;
  510. Item *i, *new;
  511. i = 0;
  512. while(fgets(buf, sizeof buf, stdin)) {
  513. len = strlen(buf);
  514. if (buf[len - 1] == '\n')
  515. buf[len - 1] = 0;
  516. p = estrdup(buf);
  517. if(max < len) {
  518. maxname = p;
  519. max = len;
  520. }
  521. new = emalloc(sizeof(Item));
  522. new->next = new->left = new->right = NULL;
  523. new->text = p;
  524. if(!i)
  525. allitems = new;
  526. else
  527. i->next = new;
  528. i = new;
  529. }
  530. }
  531. void
  532. run(void) {
  533. XEvent ev;
  534. /* main event loop */
  535. while(running && !XNextEvent(dpy, &ev))
  536. switch (ev.type) {
  537. default: /* ignore all crap */
  538. break;
  539. case KeyPress:
  540. kpress(&ev.xkey);
  541. break;
  542. case Expose:
  543. if(ev.xexpose.count == 0)
  544. drawmenu();
  545. break;
  546. }
  547. }
  548. void
  549. setup(int x, int y, int w) {
  550. unsigned int i, j;
  551. XModifierKeymap *modmap;
  552. XSetWindowAttributes wa;
  553. /* init modifier map */
  554. modmap = XGetModifierMapping(dpy);
  555. for(i = 0; i < 8; i++)
  556. for(j = 0; j < modmap->max_keypermod; j++) {
  557. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  558. == XKeysymToKeycode(dpy, XK_Num_Lock))
  559. numlockmask = (1 << i);
  560. }
  561. XFreeModifiermap(modmap);
  562. /* style */
  563. dc.norm[ColBG] = getcolor(normbg);
  564. dc.norm[ColFG] = getcolor(normfg);
  565. dc.sel[ColBG] = getcolor(selbg);
  566. dc.sel[ColFG] = getcolor(selfg);
  567. initfont(font);
  568. /* menu window */
  569. wa.override_redirect = 1;
  570. wa.background_pixmap = ParentRelative;
  571. wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
  572. mw = w ? w : DisplayWidth(dpy, screen);
  573. mh = dc.font.height + 2;
  574. if(y < 0) {
  575. if(y == (int)(unsigned int)-1)
  576. y = DisplayHeight(dpy, screen) - mh;
  577. else
  578. y = (-1 * y) - mh;
  579. }
  580. win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
  581. DefaultDepth(dpy, screen), CopyFromParent,
  582. DefaultVisual(dpy, screen),
  583. CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
  584. /* pixmap */
  585. dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
  586. dc.gc = XCreateGC(dpy, root, 0, 0);
  587. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  588. if(!dc.font.set)
  589. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  590. if(maxname)
  591. cmdw = textw(maxname);
  592. if(cmdw > mw / 3)
  593. cmdw = mw / 3;
  594. if(prompt)
  595. promptw = textw(prompt);
  596. if(promptw > mw / 5)
  597. promptw = mw / 5;
  598. text[0] = 0;
  599. match(text);
  600. XMapRaised(dpy, win);
  601. }
  602. unsigned int
  603. textnw(const char *text, unsigned int len) {
  604. XRectangle r;
  605. if(dc.font.set) {
  606. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  607. return r.width;
  608. }
  609. return XTextWidth(dc.font.xfont, text, len);
  610. }
  611. unsigned int
  612. textw(const char *text) {
  613. return textnw(text, strlen(text)) + dc.font.height;
  614. }
  615. int
  616. main(int argc, char *argv[]) {
  617. int x = 0, y = 0, w = 0;
  618. unsigned int i;
  619. /* command line args */
  620. for(i = 1; i < argc; i++)
  621. if(!strcmp(argv[i], "-i")) {
  622. fstrncmp = strncasecmp;
  623. fstrstr = cistrstr;
  624. }
  625. else if(!strcmp(argv[i], "-fn")) {
  626. if(++i < argc) font = argv[i];
  627. }
  628. else if(!strcmp(argv[i], "-nb")) {
  629. if(++i < argc) normbg = argv[i];
  630. }
  631. else if(!strcmp(argv[i], "-nf")) {
  632. if(++i < argc) normfg = argv[i];
  633. }
  634. else if(!strcmp(argv[i], "-p")) {
  635. if(++i < argc) prompt = argv[i];
  636. }
  637. else if(!strcmp(argv[i], "-sb")) {
  638. if(++i < argc) selbg = argv[i];
  639. }
  640. else if(!strcmp(argv[i], "-sf")) {
  641. if(++i < argc) selfg = argv[i];
  642. }
  643. else if(!strcmp(argv[i], "-x")) {
  644. if(++i < argc) x = atoi(argv[i]);
  645. }
  646. else if(!strcmp(argv[i], "-y")) {
  647. if(++i < argc) {
  648. if(!strcmp(argv[i], "-0"))
  649. y = (int)(unsigned int)-1;
  650. else
  651. y = atoi(argv[i]);
  652. }
  653. }
  654. else if(!strcmp(argv[i], "-w")) {
  655. if(++i < argc) w = atoi(argv[i]);
  656. }
  657. else if(!strcmp(argv[i], "-v"))
  658. eprint("dmenu-"VERSION", © 2006-2008 dmenu engineers, see LICENSE for details\n");
  659. else
  660. eprint("usage: dmenu [-i] [-fn <font>] [-nb <color>] [-nf <color>]\n"
  661. " [-p <prompt>] [-sb <color>] [-sf <color>]\n"
  662. " [-x <x>] [-y <y>] [-w <w>] [-v]\n");
  663. setlocale(LC_CTYPE, "");
  664. dpy = XOpenDisplay(0);
  665. if(!dpy)
  666. eprint("dmenu: cannot open display\n");
  667. screen = DefaultScreen(dpy);
  668. root = RootWindow(dpy, screen);
  669. if(isatty(STDIN_FILENO)) {
  670. readstdin();
  671. running = grabkeyboard();
  672. }
  673. else { /* prevent keypress loss */
  674. running = grabkeyboard();
  675. readstdin();
  676. }
  677. setup(x, y, w);
  678. drawmenu();
  679. XSync(dpy, False);
  680. run();
  681. cleanup();
  682. XCloseDisplay(dpy);
  683. return ret;
  684. }