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.
 
 
 
 
 

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