A mirror of phillbush's xmenu.
Você não pode selecionar mais de 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.
 
 
 
 
 

833 linhas
21 KiB

  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <X11/Xlib.h>
  7. #include <X11/Xutil.h>
  8. #include <X11/Xresource.h>
  9. #include <X11/XKBlib.h>
  10. #include <X11/Xft/Xft.h>
  11. #define PROGNAME "xmenu"
  12. #define ITEMPREV 0
  13. #define ITEMNEXT 1
  14. /* macros */
  15. #define LEN(x) (sizeof (x) / sizeof (x[0]))
  16. #define MAX(x,y) ((x)>(y)?(x):(y))
  17. #define MIN(x,y) ((x)<(y)?(x):(y))
  18. /* color enum */
  19. enum {ColorFG, ColorBG, ColorLast};
  20. /* draw context structure */
  21. struct DC {
  22. XftColor normal[ColorLast];
  23. XftColor selected[ColorLast];
  24. XftColor decoration[ColorLast];
  25. Drawable d;
  26. GC gc;
  27. XftFont *font;
  28. };
  29. /* menu geometry structure */
  30. struct Geometry {
  31. int itemb; /* item border */
  32. int itemw; /* item width */
  33. int itemh; /* item height */
  34. int border; /* window border width */
  35. int separator; /* menu separator width */
  36. };
  37. /* screen geometry structure */
  38. struct ScreenGeometry {
  39. int cursx, cursy; /* cursor position */
  40. int screenw, screenh; /* screen width and height */
  41. };
  42. /* menu item structure */
  43. struct Item {
  44. char *label; /* string to be drawed on menu */
  45. char *output; /* string to be outputed when item is clicked */
  46. int y; /* item y position relative to menu */
  47. int h; /* item height */
  48. size_t labellen; /* strlen(label) */
  49. struct Item *prev; /* previous item */
  50. struct Item *next; /* next item */
  51. struct Menu *submenu; /* submenu spawned by clicking on item */
  52. };
  53. /* menu structure */
  54. struct Menu {
  55. struct Menu *parent; /* parent menu */
  56. struct Item *caller; /* item that spawned the menu */
  57. struct Item *list; /* list of items contained by the menu */
  58. struct Item *selected; /* item currently selected in the menu */
  59. int x, y, w, h; /* menu geometry */
  60. unsigned level; /* menu level relative to root */
  61. Drawable pixmap; /* pixmap to draw the menu on */
  62. XftDraw *draw;
  63. Window win; /* menu window to map on the screen */
  64. };
  65. /* function declarations */
  66. static void getcolor(const char *s, XftColor *color);
  67. static void getresources(void);
  68. static void setupdc(void);
  69. static void setupgeom(void);
  70. static void setupgrab(void);
  71. static struct Item *allocitem(const char *label, const char *output);
  72. static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
  73. static void getmenuitem(Window win, int y, struct Menu **menu_ret, struct Item **item_ret);
  74. static void drawmenu(void);
  75. static void calcscreengeom(void);
  76. static void calcmenu(struct Menu *menu);
  77. static void setcurrmenu(struct Menu *currmenu_new);
  78. static void parsestdin(void);
  79. static void run(void);
  80. static void freewindow(struct Menu *menu);
  81. static void cleanup(void);
  82. static void usage(void);
  83. /* X variables */
  84. static Colormap colormap;
  85. static Display *dpy;
  86. static Visual *visual;
  87. static Window rootwin;
  88. static int screen;
  89. static struct DC dc;
  90. /* menu variables */
  91. static struct Menu *rootmenu = NULL;
  92. static struct Menu *currmenu = NULL;
  93. static char **menutitle;
  94. static int menutitlecount;
  95. /* geometry variables */
  96. static struct Geometry geom;
  97. static struct ScreenGeometry screengeom;
  98. /* flag variables */
  99. static Bool override_redirect = True;
  100. #include "config.h"
  101. int
  102. main(int argc, char *argv[])
  103. {
  104. int ch;
  105. while ((ch = getopt(argc, argv, "w")) != -1) {
  106. switch (ch) {
  107. case 'w':
  108. override_redirect = False;
  109. break;
  110. default:
  111. usage();
  112. break;
  113. }
  114. }
  115. argc -= optind;
  116. argv += optind;
  117. menutitle = argv;
  118. menutitlecount = argc;
  119. /* open connection to server and set X variables */
  120. if ((dpy = XOpenDisplay(NULL)) == NULL)
  121. errx(1, "cannot open display");
  122. screen = DefaultScreen(dpy);
  123. visual = DefaultVisual(dpy, screen);
  124. rootwin = RootWindow(dpy, screen);
  125. colormap = DefaultColormap(dpy, screen);
  126. /* setup */
  127. getresources();
  128. setupdc();
  129. setupgeom();
  130. /* generate menus and recalculate them */
  131. parsestdin();
  132. if (rootmenu == NULL)
  133. errx(1, "no menu generated");
  134. calcscreengeom();
  135. calcmenu(rootmenu);
  136. /* grab mouse and keyboard */
  137. if (override_redirect)
  138. setupgrab();
  139. /* map root menu */
  140. currmenu = rootmenu;
  141. XMapWindow(dpy, rootmenu->win);
  142. /* run event loop */
  143. run();
  144. cleanup();
  145. return 0;
  146. }
  147. /* read xrdb for configuration options */
  148. static void
  149. getresources(void)
  150. {
  151. char *xrm;
  152. long n;
  153. XrmInitialize();
  154. if ((xrm = XResourceManagerString(dpy))) {
  155. char *type;
  156. XrmDatabase xdb;
  157. XrmValue xval;
  158. xdb = XrmGetStringDatabase(xrm);
  159. if (XrmGetResource(xdb, "xmenu.menuborder", "*", &type, &xval) == True)
  160. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  161. menuborder = n;
  162. if (XrmGetResource(xdb, "xmenu.separatorsize", "*", &type, &xval) == True)
  163. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  164. separatorsize = n;
  165. if (XrmGetResource(xdb, "xmenu.itemborder", "*", &type, &xval) == True)
  166. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  167. itemborder = n;
  168. if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
  169. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  170. width = n;
  171. if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
  172. background = strdup(xval.addr);
  173. if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
  174. foreground = strdup(xval.addr);
  175. if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
  176. selbackground = strdup(xval.addr);
  177. if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
  178. selforeground = strdup(xval.addr);
  179. if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
  180. separator = strdup(xval.addr);
  181. if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
  182. border = strdup(xval.addr);
  183. if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
  184. font = strdup(xval.addr);
  185. XrmDestroyDatabase(xdb);
  186. }
  187. }
  188. /* get color from color string */
  189. static void
  190. getcolor(const char *s, XftColor *color)
  191. {
  192. if(!XftColorAllocName(dpy, visual, colormap, s, color))
  193. errx(1, "cannot allocate color: %s", s);
  194. }
  195. /* init draw context */
  196. static void
  197. setupdc(void)
  198. {
  199. /* get color pixels */
  200. getcolor(background, &dc.normal[ColorBG]);
  201. getcolor(foreground, &dc.normal[ColorFG]);
  202. getcolor(selbackground, &dc.selected[ColorBG]);
  203. getcolor(selforeground, &dc.selected[ColorFG]);
  204. getcolor(separator, &dc.decoration[ColorBG]);
  205. getcolor(border, &dc.decoration[ColorFG]);
  206. /* try to get font */
  207. if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
  208. errx(1, "cannot load font");
  209. /* create GC */
  210. dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
  211. }
  212. /* init menu geometry values */
  213. static void
  214. setupgeom(void)
  215. {
  216. geom.itemb = itemborder;
  217. geom.itemh = dc.font->height + itemborder * 2;
  218. geom.itemw = width;
  219. geom.border = menuborder;
  220. geom.separator = separatorsize;
  221. }
  222. /* grab pointer */
  223. static void
  224. setupgrab(void)
  225. {
  226. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  227. GrabModeAsync, GrabModeAsync, None,
  228. None, CurrentTime) != GrabSuccess)
  229. errx(1, "cannot grab pointer");
  230. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  231. GrabModeAsync, CurrentTime) != GrabSuccess)
  232. errx(1, "cannot grab keyboard");
  233. }
  234. /* allocate an item */
  235. static struct Item *
  236. allocitem(const char *label, const char *output)
  237. {
  238. struct Item *item;
  239. if ((item = malloc(sizeof *item)) == NULL)
  240. err(1, "malloc");
  241. if (*label == '\0') {
  242. item->label = NULL;
  243. item->output = NULL;
  244. } else {
  245. if ((item->label = strdup(label)) == NULL)
  246. err(1, "strdup");
  247. if ((item->output = strdup(output)) == NULL)
  248. err(1, "strdup");
  249. }
  250. item->y = 0;
  251. item->h = item->label ? geom.itemh : geom.separator;
  252. if (item->label == NULL)
  253. item->labellen = 0;
  254. else
  255. item->labellen = strlen(item->label);
  256. item->next = NULL;
  257. item->submenu = NULL;
  258. return item;
  259. }
  260. /* allocate a menu */
  261. static struct Menu *
  262. allocmenu(struct Menu *parent, struct Item *list, unsigned level)
  263. {
  264. XSetWindowAttributes swa;
  265. struct Menu *menu;
  266. if ((menu = malloc(sizeof *menu)) == NULL)
  267. err(1, "malloc");
  268. menu->parent = parent;
  269. menu->list = list;
  270. menu->caller = NULL;
  271. menu->selected = NULL;
  272. menu->w = geom.itemw;
  273. menu->h = 0; /* calculated by calcmenu() */
  274. menu->x = 0; /* calculated by calcmenu() */
  275. menu->y = 0; /* calculated by calcmenu() */
  276. menu->level = level;
  277. swa.override_redirect = override_redirect;
  278. swa.background_pixel = dc.decoration[ColorBG].pixel;
  279. swa.border_pixel = dc.decoration[ColorFG].pixel;
  280. swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
  281. | PointerMotionMask | LeaveWindowMask;
  282. menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
  283. CopyFromParent, CopyFromParent, CopyFromParent,
  284. CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
  285. &swa);
  286. return menu;
  287. }
  288. /* create menus and items from the stdin */
  289. static void
  290. parsestdin(void)
  291. {
  292. char *s, buf[BUFSIZ];
  293. char *label, *output;
  294. unsigned level = 0;
  295. unsigned i;
  296. struct Item *curritem = NULL; /* item currently being read */
  297. struct Menu *prevmenu = NULL; /* menu the previous item was added to */
  298. struct Item *item; /* dummy item for for loops */
  299. struct Menu *menu; /* dummy menu for for loops */
  300. size_t count = 0; /* number of items in the current menu */
  301. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  302. level = 0;
  303. s = buf;
  304. while (*s == '\t') {
  305. level++;
  306. s++;
  307. }
  308. label = output = s;
  309. while (*s != '\0' && *s != '\t' && *s != '\n')
  310. s++;
  311. while (*s == '\t')
  312. *s++ = '\0';
  313. if (*s != '\0' && *s != '\n')
  314. output = s;
  315. while (*s != '\0' && *s != '\n')
  316. s++;
  317. if (*s == '\n')
  318. *s = '\0';
  319. curritem = allocitem(label, output);
  320. if (prevmenu == NULL) { /* there is no menu yet */
  321. menu = allocmenu(NULL, curritem, level);
  322. rootmenu = menu;
  323. prevmenu = menu;
  324. count = 1;
  325. curritem->prev = NULL;
  326. curritem->next = NULL;
  327. } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
  328. for (menu = prevmenu, i = level;
  329. menu != NULL && i < prevmenu->level;
  330. menu = menu->parent, i++)
  331. ;
  332. if (menu == NULL)
  333. errx(1, "reached NULL menu");
  334. for (item = menu->list; item->next != NULL; item = item->next)
  335. ;
  336. item->next = curritem;
  337. curritem->prev = item;
  338. curritem->next = NULL;
  339. prevmenu = menu;
  340. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  341. for (item = prevmenu->list; item->next != NULL; item = item->next)
  342. ;
  343. item->next = curritem;
  344. curritem->prev = item;
  345. curritem->next = NULL;
  346. } else if (level > prevmenu->level) { /* item begins a new menu */
  347. menu = allocmenu(prevmenu, curritem, level);
  348. for (item = prevmenu->list; item->next != NULL; item = item->next)
  349. ;
  350. item->submenu = menu;
  351. menu->caller = item;
  352. curritem->prev = NULL;
  353. curritem->next = NULL;
  354. prevmenu = menu;
  355. }
  356. count++;
  357. }
  358. }
  359. /* calculate screen geometry */
  360. static void
  361. calcscreengeom(void)
  362. {
  363. Window w1, w2; /* unused variables */
  364. int a, b; /* unused variables */
  365. unsigned mask; /* unused variable */
  366. XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
  367. screengeom.screenw = DisplayWidth(dpy, screen);
  368. screengeom.screenh = DisplayHeight(dpy, screen);
  369. }
  370. /* recursivelly calculate height and position of the menus */
  371. static void
  372. calcmenu(struct Menu *menu)
  373. {
  374. static XClassHint classh = {PROGNAME, PROGNAME};
  375. XWindowChanges changes;
  376. XTextProperty textprop;
  377. XSizeHints sizeh;
  378. XGlyphInfo ext;
  379. struct Item *item;
  380. int labelwidth;
  381. /* calculate items positions and menu width and height */
  382. menu->w = geom.itemw;
  383. for (item = menu->list; item != NULL; item = item->next) {
  384. item->y = menu->h;
  385. if (item->label == NULL) /* height for separator item */
  386. menu->h += geom.separator;
  387. else
  388. menu->h += geom.itemh;
  389. XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
  390. item->labellen, &ext);
  391. labelwidth = ext.xOff + dc.font->height * 2;
  392. menu->w = MAX(menu->w, labelwidth);
  393. }
  394. /* calculate menu's x and y positions */
  395. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  396. if (screengeom.screenw - screengeom.cursx >= menu->w)
  397. menu->x = screengeom.cursx;
  398. else if (screengeom.cursx > menu->w)
  399. menu->x = screengeom.cursx - menu->w;
  400. if (screengeom.screenh - screengeom.cursy >= menu->h)
  401. menu->y = screengeom.cursy;
  402. else if (screengeom.screenh > menu->h)
  403. menu->y = screengeom.screenh - menu->h;
  404. XStringListToTextProperty(menutitle, menutitlecount, &textprop);
  405. } else { /* else, calculate in respect to parent menu */
  406. /* search for the item in parent menu that generates this menu */
  407. for (item = menu->parent->list; item->submenu != menu; item = item->next)
  408. ;
  409. if (screengeom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= menu->w)
  410. menu->x = menu->parent->x + menu->parent->w + geom.border;
  411. else if (menu->parent->x > menu->w + geom.border)
  412. menu->x = menu->parent->x - menu->w - geom.border;
  413. if (screengeom.screenh - (item->y + menu->parent->y) > menu->h)
  414. menu->y = item->y + menu->parent->y;
  415. else if (screengeom.screenh - menu->parent->y > menu->h)
  416. menu->y = menu->parent->y;
  417. else if (screengeom.screenh > menu->h)
  418. menu->y = screengeom.screenh - menu->h;
  419. XStringListToTextProperty(&(menu->caller->output), 1, &textprop);
  420. }
  421. /* update menu geometry */
  422. changes.height = menu->h;
  423. changes.width = menu->w;
  424. changes.x = menu->x;
  425. changes.y = menu->y;
  426. XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
  427. /* set window manager hints */
  428. sizeh.flags = PMaxSize | PMinSize;
  429. sizeh.min_width = sizeh.max_width = menu->w;
  430. sizeh.min_height = sizeh.max_height = menu->h;
  431. XSetWMProperties(dpy, menu->win, &textprop, NULL, NULL, 0, &sizeh,
  432. NULL, &classh);
  433. /* create pixmap and XftDraw */
  434. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  435. DefaultDepth(dpy, screen));
  436. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  437. /* calculate positions of submenus */
  438. for (item = menu->list; item != NULL; item = item->next) {
  439. if (item->submenu != NULL)
  440. calcmenu(item->submenu);
  441. }
  442. }
  443. /* get menu and item of given window and position */
  444. static void
  445. getmenuitem(Window win, int y,
  446. struct Menu **menu_ret, struct Item **item_ret)
  447. {
  448. struct Menu *menu = NULL;
  449. struct Item *item = NULL;
  450. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  451. if (menu->win == win) {
  452. for (item = menu->list; item != NULL; item = item->next) {
  453. if (y >= item->y && y <= item->y + item->h) {
  454. goto done;
  455. }
  456. }
  457. }
  458. }
  459. done:
  460. *menu_ret = menu;
  461. *item_ret = item;
  462. }
  463. /* set currentmenu to menu, umap previous menus and map current menu and its parents */
  464. static void
  465. setcurrmenu(struct Menu *currmenu_new)
  466. {
  467. struct Menu *menu, *menu_;
  468. struct Item *item;
  469. struct Menu *lcamenu; /* lowest common ancestor menu */
  470. unsigned minlevel; /* level of the closest to root menu */
  471. unsigned maxlevel; /* level of the closest to root menu */
  472. if (currmenu_new == currmenu)
  473. return;
  474. /* find lowest common ancestor menu */
  475. lcamenu = rootmenu;
  476. if (currmenu != NULL) {
  477. minlevel = MIN(currmenu_new->level, currmenu->level);
  478. maxlevel = MAX(currmenu_new->level, currmenu->level);
  479. if (currmenu_new->level == maxlevel) {
  480. menu = currmenu_new;
  481. menu_ = currmenu;
  482. } else {
  483. menu = currmenu;
  484. menu_ = currmenu_new;
  485. }
  486. while (menu->level > minlevel)
  487. menu = menu->parent;
  488. while (menu != menu_) {
  489. menu = menu->parent;
  490. menu_ = menu_->parent;
  491. }
  492. lcamenu = menu;
  493. }
  494. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  495. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  496. XUnmapWindow(dpy, menu->win);
  497. }
  498. currmenu = currmenu_new;
  499. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  500. item = NULL;
  501. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  502. XMapWindow(dpy, menu->win);
  503. if (item != NULL)
  504. menu->selected = item;
  505. item = menu->caller;
  506. }
  507. }
  508. /* draw items of the current menu and of its ancestors */
  509. static void
  510. drawmenu(void)
  511. {
  512. struct Menu *menu;
  513. struct Item *item;
  514. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  515. for (item = menu->list; item != NULL; item = item->next) {
  516. XftColor *color;
  517. int labelx, labely;
  518. /* determine item color */
  519. if (item == menu->selected)
  520. color = dc.selected;
  521. else
  522. color = dc.normal;
  523. /* continue if item is a separator */
  524. if (item->label == NULL)
  525. continue;
  526. /* draw item box */
  527. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  528. XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  529. menu->w, item->h);
  530. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  531. menu->w, item->h);
  532. /* draw item label */
  533. labelx = 0 + dc.font->height;
  534. labely = item->y + dc.font->height + geom.itemb / 2;
  535. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  536. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  537. labelx, labely, item->label,
  538. item->labellen);
  539. /* draw triangle, if item contains a submenu */
  540. if (item->submenu != NULL) {
  541. int trianglex = menu->w - dc.font->height + geom.itemb - 1;
  542. int triangley = item->y + (3 * item->h)/8 -1;
  543. XPoint triangle[] = {
  544. {trianglex, triangley},
  545. {trianglex + item->h/8 + 1, item->y + item->h/2},
  546. {trianglex, triangley + item->h/4 + 2},
  547. {trianglex, triangley}
  548. };
  549. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  550. Convex, CoordModeOrigin);
  551. }
  552. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y,
  553. menu->w, item->h, 0, item->y);
  554. }
  555. }
  556. }
  557. /* cycle through the items; non-zero direction is next, zero is prev */
  558. static struct Item *
  559. itemcycle(int direction)
  560. {
  561. struct Item *item;
  562. struct Item *lastitem;
  563. item = NULL;
  564. if (direction == ITEMNEXT) {
  565. if (currmenu->selected == NULL)
  566. item = currmenu->list;
  567. else if (currmenu->selected->next != NULL)
  568. item = currmenu->selected->next;
  569. while (item != NULL && item->label == NULL)
  570. item = item->next;
  571. if (item == NULL)
  572. item = currmenu->list;
  573. } else {
  574. for (lastitem = currmenu->list;
  575. lastitem != NULL && lastitem->next != NULL;
  576. lastitem = lastitem->next)
  577. ;
  578. if (currmenu->selected == NULL)
  579. item = lastitem;
  580. else if (currmenu->selected->prev != NULL)
  581. item = currmenu->selected->prev;
  582. while (item != NULL && item->label == NULL)
  583. item = item->prev;
  584. if (item == NULL)
  585. item = lastitem;
  586. }
  587. return item;
  588. }
  589. /* run event loop */
  590. static void
  591. run(void)
  592. {
  593. struct Menu *menu;
  594. struct Item *item;
  595. struct Item *previtem = NULL;
  596. KeySym ksym;
  597. XEvent ev;
  598. while (!XNextEvent(dpy, &ev)) {
  599. switch(ev.type) {
  600. case Expose:
  601. if (ev.xexpose.count == 0)
  602. drawmenu();
  603. break;
  604. case MotionNotify:
  605. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  606. if (menu != NULL && item != NULL) {
  607. if (previtem != item) {
  608. if (item->submenu != NULL)
  609. setcurrmenu(item->submenu);
  610. else
  611. setcurrmenu(menu);
  612. previtem = item;
  613. drawmenu();
  614. } else if (menu->selected != item) {
  615. menu->selected = item;
  616. drawmenu();
  617. }
  618. }
  619. break;
  620. case ButtonRelease:
  621. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  622. if (menu != NULL && item != NULL) {
  623. selectitem:
  624. if (item->label == NULL)
  625. break; /* ignore separators */
  626. if (item->submenu != NULL) {
  627. setcurrmenu(item->submenu);
  628. } else {
  629. printf("%s\n", item->output);
  630. return;
  631. }
  632. currmenu->selected = currmenu->list;
  633. drawmenu();
  634. break;
  635. } else {
  636. return;
  637. }
  638. case ButtonPress:
  639. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  640. if (menu == NULL || item == NULL)
  641. return;
  642. break;
  643. case KeyPress:
  644. ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
  645. if (ksym == XK_Escape && currmenu == rootmenu)
  646. return;
  647. /* Shift-Tab = ISO_Left_Tab */
  648. if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
  649. ksym = XK_ISO_Left_Tab;
  650. /* cycle through menu */
  651. item = NULL;
  652. if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
  653. item = itemcycle(ITEMPREV);
  654. } else if (ksym == XK_Tab || ksym == XK_Down) {
  655. item = itemcycle(ITEMNEXT);
  656. } else if ((ksym == XK_Return || ksym == XK_Right) &&
  657. currmenu->selected != NULL) {
  658. item = currmenu->selected;
  659. goto selectitem;
  660. } else if ((ksym == XK_Escape || ksym == XK_Left) &&
  661. currmenu->parent != NULL) {
  662. item = currmenu->parent->selected;
  663. setcurrmenu(currmenu->parent);
  664. } else
  665. break;
  666. currmenu->selected = item;
  667. drawmenu();
  668. break;
  669. case LeaveNotify:
  670. currmenu->selected = NULL;
  671. drawmenu();
  672. break;
  673. }
  674. }
  675. }
  676. /* recursivelly free a pixmap */
  677. static void
  678. freewindow(struct Menu *menu)
  679. {
  680. struct Item *item;
  681. for (item = menu->list; item != NULL; item = item->next)
  682. if (item->submenu != NULL)
  683. freewindow(item->submenu);
  684. XFreePixmap(dpy, menu->pixmap);
  685. XftDrawDestroy(menu->draw);
  686. XDestroyWindow(dpy, menu->win);
  687. }
  688. /* cleanup and exit */
  689. static void
  690. cleanup(void)
  691. {
  692. freewindow(rootmenu);
  693. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  694. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  695. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  696. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  697. XftColorFree(dpy, visual, colormap, &dc.decoration[ColorBG]);
  698. XftColorFree(dpy, visual, colormap, &dc.decoration[ColorFG]);
  699. XFreeGC(dpy, dc.gc);
  700. XCloseDisplay(dpy);
  701. }
  702. /* show usage */
  703. static void
  704. usage(void)
  705. {
  706. (void)fprintf(stderr, "usage: xmenu [-w]\n");
  707. exit(1);
  708. }