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.
 
 
 
 
 

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