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.
 
 
 
 
 

860 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 freemenu(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. int width, height;
  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. width = menu->w + geom.border * 2;
  365. height = menu->h + geom.border * 2;
  366. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  367. if (geom.screenw - geom.cursx >= menu->w)
  368. menu->x = geom.cursx;
  369. else if (geom.cursx > width)
  370. menu->x = geom.cursx - width;
  371. if (geom.screenh - geom.cursy >= height)
  372. menu->y = geom.cursy;
  373. else if (geom.screenh > height)
  374. menu->y = geom.screenh - height;
  375. } else { /* else, calculate in respect to parent menu */
  376. if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
  377. menu->x = menu->parent->x + menu->parent->w + geom.border;
  378. else if (menu->parent->x > menu->w + geom.border)
  379. menu->x = menu->parent->x - menu->w - geom.border;
  380. if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
  381. menu->y = menu->caller->y + menu->parent->y;
  382. else if (geom.screenh - menu->parent->y > height)
  383. menu->y = menu->parent->y;
  384. else if (geom.screenh > height)
  385. menu->y = geom.screenh - height;
  386. }
  387. /* update menu geometry */
  388. changes.height = menu->h;
  389. changes.width = menu->w;
  390. changes.x = menu->x;
  391. changes.y = menu->y;
  392. XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
  393. /* set window manager hints */
  394. sizeh.flags = PMaxSize | PMinSize;
  395. sizeh.min_width = sizeh.max_width = menu->w;
  396. sizeh.min_height = sizeh.max_height = menu->h;
  397. XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
  398. NULL, &classh);
  399. /* create pixmap and XftDraw */
  400. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  401. DefaultDepth(dpy, screen));
  402. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  403. /* calculate positions of submenus */
  404. for (item = menu->list; item != NULL; item = item->next) {
  405. if (item->submenu != NULL)
  406. calcmenu(item->submenu);
  407. }
  408. }
  409. /* try to grab pointer, we may have to wait for another process to ungrab */
  410. static void
  411. grabpointer(void)
  412. {
  413. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  414. int i;
  415. for (i = 0; i < 1000; i++) {
  416. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  417. GrabModeAsync, GrabModeAsync, None,
  418. None, CurrentTime) == GrabSuccess)
  419. return;
  420. nanosleep(&ts, NULL);
  421. }
  422. errx(1, "cannot grab keyboard");
  423. }
  424. /* try to grab keyboard, we may have to wait for another process to ungrab */
  425. static void
  426. grabkeyboard(void)
  427. {
  428. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  429. int i;
  430. for (i = 0; i < 1000; i++) {
  431. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  432. GrabModeAsync, CurrentTime) == GrabSuccess)
  433. return;
  434. nanosleep(&ts, NULL);
  435. }
  436. errx(1, "cannot grab keyboard");
  437. }
  438. /* get menu of given window */
  439. static struct Menu *
  440. getmenu(struct Menu *currmenu, Window win)
  441. {
  442. struct Menu *menu;
  443. for (menu = currmenu; menu != NULL; menu = menu->parent)
  444. if (menu->win == win)
  445. return menu;
  446. return NULL;
  447. }
  448. /* get item of given menu and position */
  449. static struct Item *
  450. getitem(struct Menu *menu, int y)
  451. {
  452. struct Item *item;
  453. if (menu == NULL)
  454. return NULL;
  455. for (item = menu->list; item != NULL; item = item->next)
  456. if (y >= item->y && y <= item->y + item->h)
  457. return item;
  458. return NULL;
  459. }
  460. /* umap previous menus and map current menu and its parents */
  461. static void
  462. mapmenu(struct Menu *currmenu)
  463. {
  464. static struct Menu *prevmenu = NULL;
  465. struct Menu *menu, *menu_;
  466. struct Menu *lcamenu; /* lowest common ancestor menu */
  467. unsigned minlevel; /* level of the closest to root menu */
  468. unsigned maxlevel; /* level of the closest to root menu */
  469. /* do not remap current menu if it wasn't updated*/
  470. if (prevmenu == currmenu)
  471. return;
  472. /* if this is the first time mapping, skip calculations */
  473. if (prevmenu == NULL) {
  474. XMapWindow(dpy, currmenu->win);
  475. prevmenu = currmenu;
  476. return;
  477. }
  478. /* find lowest common ancestor menu */
  479. minlevel = MIN(currmenu->level, prevmenu->level);
  480. maxlevel = MAX(currmenu->level, prevmenu->level);
  481. if (currmenu->level == maxlevel) {
  482. menu = currmenu;
  483. menu_ = prevmenu;
  484. } else {
  485. menu = prevmenu;
  486. menu_ = currmenu;
  487. }
  488. while (menu->level > minlevel)
  489. menu = menu->parent;
  490. while (menu != menu_) {
  491. menu = menu->parent;
  492. menu_ = menu_->parent;
  493. }
  494. lcamenu = menu;
  495. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  496. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  497. menu->selected = NULL;
  498. XUnmapWindow(dpy, menu->win);
  499. }
  500. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  501. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  502. XMapWindow(dpy, menu->win);
  503. }
  504. prevmenu = currmenu;
  505. }
  506. /* draw separator item */
  507. static void
  508. drawseparator(struct Menu *menu, struct Item *item)
  509. {
  510. int linex, liney, linew;
  511. linex = dc.font->height;
  512. liney = item->y + item->h/2;
  513. linew = menu->w - dc.font->height;
  514. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  515. XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
  516. }
  517. /* draw regular item */
  518. static void
  519. drawitem(struct Menu *menu, struct Item *item, XftColor *color)
  520. {
  521. int x, y;
  522. x = dc.font->height;
  523. y = item->y + item->h/2 + dc.font->ascent/2 - 1;
  524. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  525. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  526. x, y, item->label, item->labellen);
  527. /* draw triangle, if item contains a submenu */
  528. if (item->submenu != NULL) {
  529. x = menu->w - dc.font->height/2 - triangle_width/2;
  530. y = item->y + item->h/2 - triangle_height/2 - 1;
  531. XPoint triangle[] = {
  532. {x, y},
  533. {x + triangle_width, y + triangle_height/2},
  534. {x, y + triangle_height},
  535. {x, y}
  536. };
  537. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  538. Convex, CoordModeOrigin);
  539. }
  540. }
  541. /* draw items of the current menu and of its ancestors */
  542. static void
  543. drawmenu(struct Menu *currmenu)
  544. {
  545. struct Menu *menu;
  546. struct Item *item;
  547. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  548. for (item = menu->list; item != NULL; item = item->next) {
  549. XftColor *color;
  550. /* determine item color */
  551. if (item == menu->selected && item->label != NULL)
  552. color = dc.selected;
  553. else
  554. color = dc.normal;
  555. /* draw item box */
  556. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  557. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  558. menu->w, item->h);
  559. if (item->label == NULL) /* item is a separator */
  560. drawseparator(menu, item);
  561. else /* item is a regular item */
  562. drawitem(menu, item, color);
  563. }
  564. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
  565. menu->w, menu->h, 0, 0);
  566. }
  567. }
  568. /* cycle through the items; non-zero direction is next, zero is prev */
  569. static struct Item *
  570. itemcycle(struct Menu *currmenu, int direction)
  571. {
  572. struct Item *item;
  573. struct Item *lastitem;
  574. item = NULL;
  575. if (direction == ITEMNEXT) {
  576. if (currmenu->selected == NULL)
  577. item = currmenu->list;
  578. else if (currmenu->selected->next != NULL)
  579. item = currmenu->selected->next;
  580. while (item != NULL && item->label == NULL)
  581. item = item->next;
  582. if (item == NULL)
  583. item = currmenu->list;
  584. } else {
  585. for (lastitem = currmenu->list;
  586. lastitem != NULL && lastitem->next != NULL;
  587. lastitem = lastitem->next)
  588. ;
  589. if (currmenu->selected == NULL)
  590. item = lastitem;
  591. else if (currmenu->selected->prev != NULL)
  592. item = currmenu->selected->prev;
  593. while (item != NULL && item->label == NULL)
  594. item = item->prev;
  595. if (item == NULL)
  596. item = lastitem;
  597. }
  598. return item;
  599. }
  600. /* run event loop */
  601. static void
  602. run(struct Menu *currmenu)
  603. {
  604. struct Menu *menu;
  605. struct Item *item;
  606. struct Item *previtem = NULL;
  607. KeySym ksym;
  608. XEvent ev;
  609. mapmenu(currmenu);
  610. while (!XNextEvent(dpy, &ev)) {
  611. switch(ev.type) {
  612. case Expose:
  613. if (ev.xexpose.count == 0)
  614. drawmenu(currmenu);
  615. break;
  616. case MotionNotify:
  617. menu = getmenu(currmenu, ev.xbutton.window);
  618. item = getitem(menu, ev.xbutton.y);
  619. if (menu == NULL || item == NULL || previtem == item)
  620. break;
  621. previtem = item;
  622. menu->selected = item;
  623. if (item->submenu != NULL) {
  624. currmenu = item->submenu;
  625. currmenu->selected = NULL;
  626. } else {
  627. currmenu = menu;
  628. }
  629. mapmenu(currmenu);
  630. drawmenu(currmenu);
  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. freemenu(struct Menu *menu)
  694. {
  695. struct Item *item;
  696. struct Item *tmp;
  697. item = menu->list;
  698. while (item != NULL) {
  699. if (item->submenu != NULL)
  700. freemenu(item->submenu);
  701. tmp = item;
  702. item = item->next;
  703. free(tmp);
  704. }
  705. XFreePixmap(dpy, menu->pixmap);
  706. XftDrawDestroy(menu->draw);
  707. XDestroyWindow(dpy, menu->win);
  708. free(menu);
  709. }
  710. /* cleanup and exit */
  711. static void
  712. cleanup(struct Menu *rootmenu)
  713. {
  714. XUngrabPointer(dpy, CurrentTime);
  715. XUngrabKeyboard(dpy, CurrentTime);
  716. freemenu(rootmenu);
  717. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  718. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  719. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  720. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  721. XftColorFree(dpy, visual, colormap, &dc.separator);
  722. XftColorFree(dpy, visual, colormap, &dc.border);
  723. XFreeGC(dpy, dc.gc);
  724. XCloseDisplay(dpy);
  725. }
  726. /* show usage */
  727. static void
  728. usage(void)
  729. {
  730. (void)fprintf(stderr, "usage: xmenu\n");
  731. exit(1);
  732. }