A mirror of phillbush's xmenu.
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.
 
 
 
 
 

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