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.
 
 
 
 
 

859 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. GC gc;
  27. XftFont *font;
  28. };
  29. /* menu geometry structure */
  30. struct Geometry {
  31. int border; /* window border width */
  32. int separator; /* menu separator width */
  33. int itemw, itemh; /* item width and height */
  34. int cursx, cursy; /* cursor position */
  35. int screenw, screenh; /* screen width and height */
  36. };
  37. /* menu item structure */
  38. struct Item {
  39. char *label; /* string to be drawed on menu */
  40. char *output; /* string to be outputed when item is clicked */
  41. int y; /* item y position relative to menu */
  42. int h; /* item height */
  43. size_t labellen; /* strlen(label) */
  44. struct Item *prev; /* previous item */
  45. struct Item *next; /* next item */
  46. struct Menu *submenu; /* submenu spawned by clicking on item */
  47. };
  48. /* menu structure */
  49. struct Menu {
  50. struct Menu *parent; /* parent menu */
  51. struct Item *caller; /* item that spawned the menu */
  52. struct Item *list; /* list of items contained by the menu */
  53. struct Item *selected; /* item currently selected in the menu */
  54. int x, y, w, h; /* menu geometry */
  55. unsigned level; /* menu level relative to root */
  56. Drawable pixmap; /* pixmap to draw the menu on */
  57. XftDraw *draw;
  58. Window win; /* menu window to map on the screen */
  59. };
  60. /* functions declarations */
  61. static void getresources(void);
  62. static void getcolor(const char *s, XftColor *color);
  63. static void setupdc(void);
  64. static void calcgeom(void);
  65. static struct Item *allocitem(const char *label, const char *output);
  66. static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
  67. static struct Menu *parsestdin(void);
  68. static void calcmenu(struct Menu *menu);
  69. static void grabpointer(void);
  70. static void grabkeyboard(void);
  71. static struct Menu *getmenu(struct Menu *currmenu, Window win);
  72. static struct Item *getitem(struct Menu *menu, int y);
  73. static void mapmenu(struct Menu *currmenu);
  74. static void drawseparator(struct Menu *menu, struct Item *item);
  75. static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
  76. static void drawmenu(struct Menu *currmenu);
  77. static struct Item *itemcycle(struct Menu *currmenu, int direction);
  78. static void run(struct Menu *currmenu);
  79. static void freemenu(struct Menu *menu);
  80. static void cleanup(struct Menu *rootmenu);
  81. static void usage(void);
  82. /* global variables (X stuff and geometries) */
  83. static Display *dpy;
  84. static int screen;
  85. static Visual *visual;
  86. static Window rootwin;
  87. static Colormap colormap;
  88. static struct DC dc;
  89. static struct Geometry geom;
  90. #include "config.h"
  91. int
  92. main(int argc, char *argv[])
  93. {
  94. struct Menu *rootmenu;
  95. int ch;
  96. while ((ch = getopt(argc, argv, "")) != -1) {
  97. switch (ch) {
  98. default:
  99. usage();
  100. break;
  101. }
  102. }
  103. argc -= optind;
  104. argv += optind;
  105. if (argc != 0)
  106. usage();
  107. /* open connection to server and set X variables */
  108. if ((dpy = XOpenDisplay(NULL)) == NULL)
  109. errx(1, "cannot open display");
  110. screen = DefaultScreen(dpy);
  111. visual = DefaultVisual(dpy, screen);
  112. rootwin = RootWindow(dpy, screen);
  113. colormap = DefaultColormap(dpy, screen);
  114. /* setup */
  115. getresources();
  116. setupdc();
  117. calcgeom();
  118. /* generate menus and recalculate them */
  119. rootmenu = parsestdin();
  120. if (rootmenu == NULL)
  121. errx(1, "no menu generated");
  122. calcmenu(rootmenu);
  123. /* grab mouse and keyboard */
  124. grabpointer();
  125. grabkeyboard();
  126. /* run event loop */
  127. run(rootmenu);
  128. cleanup(rootmenu);
  129. return 0;
  130. }
  131. /* read xrdb for configuration options */
  132. static void
  133. getresources(void)
  134. {
  135. char *xrm;
  136. long n;
  137. char *type;
  138. XrmDatabase xdb;
  139. XrmValue xval;
  140. XrmInitialize();
  141. if ((xrm = XResourceManagerString(dpy)) == NULL)
  142. return;
  143. xdb = XrmGetStringDatabase(xrm);
  144. if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
  145. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  146. border_pixels = n;
  147. if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
  148. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  149. separator_pixels = n;
  150. if (XrmGetResource(xdb, "xmenu.padding", "*", &type, &xval) == True)
  151. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  152. padding_pixels = n;
  153. if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
  154. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  155. width_pixels = n;
  156. if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
  157. background_color = strdup(xval.addr);
  158. if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
  159. foreground_color = strdup(xval.addr);
  160. if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
  161. selbackground_color = strdup(xval.addr);
  162. if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
  163. selforeground_color = strdup(xval.addr);
  164. if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
  165. separator_color = strdup(xval.addr);
  166. if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
  167. border_color = strdup(xval.addr);
  168. if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
  169. font = strdup(xval.addr);
  170. XrmDestroyDatabase(xdb);
  171. }
  172. /* get color from color string */
  173. static void
  174. getcolor(const char *s, XftColor *color)
  175. {
  176. if(!XftColorAllocName(dpy, visual, colormap, s, color))
  177. errx(1, "cannot allocate color: %s", s);
  178. }
  179. /* init draw context */
  180. static void
  181. setupdc(void)
  182. {
  183. /* get color pixels */
  184. getcolor(background_color, &dc.normal[ColorBG]);
  185. getcolor(foreground_color, &dc.normal[ColorFG]);
  186. getcolor(selbackground_color, &dc.selected[ColorBG]);
  187. getcolor(selforeground_color, &dc.selected[ColorFG]);
  188. getcolor(separator_color, &dc.separator);
  189. getcolor(border_color, &dc.border);
  190. /* try to get font */
  191. if ((dc.font = XftFontOpenName(dpy, screen, font)) == NULL)
  192. errx(1, "cannot load font");
  193. /* create common GC */
  194. dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
  195. }
  196. /* calculate menu and screen geometry */
  197. static void
  198. calcgeom(void)
  199. {
  200. Window w1, w2; /* unused variables */
  201. int a, b; /* unused variables */
  202. unsigned mask; /* unused variable */
  203. XQueryPointer(dpy, rootwin, &w1, &w2, &geom.cursx, &geom.cursy, &a, &b, &mask);
  204. geom.screenw = DisplayWidth(dpy, screen);
  205. geom.screenh = DisplayHeight(dpy, screen);
  206. geom.itemh = dc.font->height + padding_pixels * 2;
  207. geom.itemw = width_pixels;
  208. geom.border = border_pixels;
  209. geom.separator = separator_pixels;
  210. }
  211. /* allocate an item */
  212. static struct Item *
  213. allocitem(const char *label, const char *output)
  214. {
  215. struct Item *item;
  216. if ((item = malloc(sizeof *item)) == NULL)
  217. err(1, "malloc");
  218. if (*label == '\0') {
  219. item->label = NULL;
  220. item->output = NULL;
  221. } else {
  222. if ((item->label = strdup(label)) == NULL)
  223. err(1, "strdup");
  224. if ((item->output = strdup(output)) == NULL)
  225. err(1, "strdup");
  226. }
  227. item->y = 0;
  228. item->h = item->label ? geom.itemh : geom.separator;
  229. if (item->label == NULL)
  230. item->labellen = 0;
  231. else
  232. item->labellen = strlen(item->label);
  233. item->next = NULL;
  234. item->submenu = NULL;
  235. return item;
  236. }
  237. /* allocate a menu */
  238. static struct Menu *
  239. allocmenu(struct Menu *parent, struct Item *list, unsigned level)
  240. {
  241. XSetWindowAttributes swa;
  242. struct Menu *menu;
  243. if ((menu = malloc(sizeof *menu)) == NULL)
  244. err(1, "malloc");
  245. menu->parent = parent;
  246. menu->list = list;
  247. menu->caller = NULL;
  248. menu->selected = NULL;
  249. menu->w = geom.itemw;
  250. menu->h = 0; /* calculated by calcmenu() */
  251. menu->x = 0; /* calculated by calcmenu() */
  252. menu->y = 0; /* calculated by calcmenu() */
  253. menu->level = level;
  254. swa.override_redirect = True;
  255. swa.background_pixel = dc.normal[ColorBG].pixel;
  256. swa.border_pixel = dc.border.pixel;
  257. swa.save_under = True; /* pop-up windows should save_under*/
  258. swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
  259. | PointerMotionMask | LeaveWindowMask;
  260. menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
  261. CopyFromParent, CopyFromParent, CopyFromParent,
  262. CWOverrideRedirect | CWBackPixel |
  263. CWBorderPixel | CWEventMask | CWSaveUnder,
  264. &swa);
  265. return menu;
  266. }
  267. /* create menus and items from the stdin */
  268. static struct Menu *
  269. parsestdin(void)
  270. {
  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 loops */
  278. struct Menu *menu; /* dummy menu for loops */
  279. struct Menu *rootmenu; /* menu to be returned */
  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. curritem->prev = NULL;
  305. curritem->next = NULL;
  306. } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
  307. for (menu = prevmenu, i = level;
  308. menu != NULL && i < prevmenu->level;
  309. menu = menu->parent, i++)
  310. ;
  311. if (menu == NULL)
  312. errx(1, "reached NULL menu");
  313. for (item = menu->list; item->next != NULL; item = item->next)
  314. ;
  315. item->next = curritem;
  316. curritem->prev = item;
  317. curritem->next = NULL;
  318. prevmenu = menu;
  319. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  320. for (item = prevmenu->list; item->next != NULL; item = item->next)
  321. ;
  322. item->next = curritem;
  323. curritem->prev = item;
  324. curritem->next = NULL;
  325. } else if (level > prevmenu->level) { /* item begins a new menu */
  326. menu = allocmenu(prevmenu, curritem, level);
  327. for (item = prevmenu->list; item->next != NULL; item = item->next)
  328. ;
  329. item->submenu = menu;
  330. menu->caller = item;
  331. curritem->prev = NULL;
  332. curritem->next = NULL;
  333. prevmenu = menu;
  334. }
  335. }
  336. return rootmenu;
  337. }
  338. /* recursivelly calculate menu geometry and set window hints */
  339. static void
  340. calcmenu(struct Menu *menu)
  341. {
  342. static XClassHint classh = {PROGNAME, PROGNAME};
  343. XWindowChanges changes;
  344. XSizeHints sizeh;
  345. XGlyphInfo ext;
  346. struct Item *item;
  347. int labelwidth;
  348. int width, height;
  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. width = menu->w + geom.border * 2;
  364. height = menu->h + geom.border * 2;
  365. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  366. if (geom.screenw - geom.cursx >= menu->w)
  367. menu->x = geom.cursx;
  368. else if (geom.cursx > width)
  369. menu->x = geom.cursx - width;
  370. if (geom.screenh - geom.cursy >= height)
  371. menu->y = geom.cursy;
  372. else if (geom.screenh > height)
  373. menu->y = geom.screenh - height;
  374. } else { /* else, calculate in respect to parent menu */
  375. if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
  376. menu->x = menu->parent->x + menu->parent->w + geom.border;
  377. else if (menu->parent->x > menu->w + geom.border)
  378. menu->x = menu->parent->x - menu->w - geom.border;
  379. if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
  380. menu->y = menu->caller->y + menu->parent->y;
  381. else if (geom.screenh - menu->parent->y > height)
  382. menu->y = menu->parent->y;
  383. else if (geom.screenh > height)
  384. menu->y = geom.screenh - height;
  385. }
  386. /* update menu geometry */
  387. changes.height = menu->h;
  388. changes.width = menu->w;
  389. changes.x = menu->x;
  390. changes.y = menu->y;
  391. XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
  392. /* set window manager hints */
  393. sizeh.flags = PMaxSize | PMinSize;
  394. sizeh.min_width = sizeh.max_width = menu->w;
  395. sizeh.min_height = sizeh.max_height = menu->h;
  396. XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
  397. NULL, &classh);
  398. /* create pixmap and XftDraw */
  399. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  400. DefaultDepth(dpy, screen));
  401. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  402. /* calculate positions of submenus */
  403. for (item = menu->list; item != NULL; item = item->next) {
  404. if (item->submenu != NULL)
  405. calcmenu(item->submenu);
  406. }
  407. }
  408. /* try to grab pointer, we may have to wait for another process to ungrab */
  409. static void
  410. grabpointer(void)
  411. {
  412. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  413. int i;
  414. for (i = 0; i < 1000; i++) {
  415. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  416. GrabModeAsync, GrabModeAsync, None,
  417. None, CurrentTime) == GrabSuccess)
  418. return;
  419. nanosleep(&ts, NULL);
  420. }
  421. errx(1, "cannot grab keyboard");
  422. }
  423. /* try to grab keyboard, we may have to wait for another process to ungrab */
  424. static void
  425. grabkeyboard(void)
  426. {
  427. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  428. int i;
  429. for (i = 0; i < 1000; i++) {
  430. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  431. GrabModeAsync, CurrentTime) == GrabSuccess)
  432. return;
  433. nanosleep(&ts, NULL);
  434. }
  435. errx(1, "cannot grab keyboard");
  436. }
  437. /* get menu of given window */
  438. static struct Menu *
  439. getmenu(struct Menu *currmenu, Window win)
  440. {
  441. struct Menu *menu;
  442. for (menu = currmenu; menu != NULL; menu = menu->parent)
  443. if (menu->win == win)
  444. return menu;
  445. return NULL;
  446. }
  447. /* get item of given menu and position */
  448. static struct Item *
  449. getitem(struct Menu *menu, int y)
  450. {
  451. struct Item *item;
  452. if (menu == NULL)
  453. return NULL;
  454. for (item = menu->list; item != NULL; item = item->next)
  455. if (y >= item->y && y <= item->y + item->h)
  456. return item;
  457. return NULL;
  458. }
  459. /* umap previous menus and map current menu and its parents */
  460. static void
  461. mapmenu(struct Menu *currmenu)
  462. {
  463. static struct Menu *prevmenu = NULL;
  464. struct Menu *menu, *menu_;
  465. struct Menu *lcamenu; /* lowest common ancestor menu */
  466. unsigned minlevel; /* level of the closest to root menu */
  467. unsigned maxlevel; /* level of the closest to root menu */
  468. /* do not remap current menu if it wasn't updated*/
  469. if (prevmenu == currmenu)
  470. return;
  471. /* if this is the first time mapping, skip calculations */
  472. if (prevmenu == NULL) {
  473. XMapWindow(dpy, currmenu->win);
  474. prevmenu = currmenu;
  475. return;
  476. }
  477. /* find lowest common ancestor menu */
  478. minlevel = MIN(currmenu->level, prevmenu->level);
  479. maxlevel = MAX(currmenu->level, prevmenu->level);
  480. if (currmenu->level == maxlevel) {
  481. menu = currmenu;
  482. menu_ = prevmenu;
  483. } else {
  484. menu = prevmenu;
  485. menu_ = currmenu;
  486. }
  487. while (menu->level > minlevel)
  488. menu = menu->parent;
  489. while (menu != menu_) {
  490. menu = menu->parent;
  491. menu_ = menu_->parent;
  492. }
  493. lcamenu = menu;
  494. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  495. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  496. menu->selected = NULL;
  497. XUnmapWindow(dpy, menu->win);
  498. }
  499. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  500. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  501. XMapWindow(dpy, menu->win);
  502. }
  503. prevmenu = currmenu;
  504. }
  505. /* draw separator item */
  506. static void
  507. drawseparator(struct Menu *menu, struct Item *item)
  508. {
  509. int y;
  510. y = item->y + item->h/2;
  511. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  512. XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
  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. freemenu(struct Menu *menu)
  691. {
  692. struct Item *item;
  693. struct Item *tmp;
  694. item = menu->list;
  695. while (item != NULL) {
  696. if (item->submenu != NULL)
  697. freemenu(item->submenu);
  698. tmp = item;
  699. item = item->next;
  700. free(tmp->label);
  701. free(tmp->output);
  702. free(tmp);
  703. }
  704. XFreePixmap(dpy, menu->pixmap);
  705. XftDrawDestroy(menu->draw);
  706. XDestroyWindow(dpy, menu->win);
  707. free(menu);
  708. }
  709. /* cleanup and exit */
  710. static void
  711. cleanup(struct Menu *rootmenu)
  712. {
  713. XUngrabPointer(dpy, CurrentTime);
  714. XUngrabKeyboard(dpy, CurrentTime);
  715. freemenu(rootmenu);
  716. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  717. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  718. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  719. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  720. XftColorFree(dpy, visual, colormap, &dc.separator);
  721. XftColorFree(dpy, visual, colormap, &dc.border);
  722. XFreeGC(dpy, dc.gc);
  723. XCloseDisplay(dpy);
  724. }
  725. /* show usage */
  726. static void
  727. usage(void)
  728. {
  729. (void)fprintf(stderr, "usage: xmenu\n");
  730. exit(1);
  731. }