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.
 
 
 
 
 

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