A mirror of phillbush's xmenu.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

862 строки
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 * 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. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  333. /* get the indentation level */
  334. level = strspn(buf, "\t");
  335. /* get the label */
  336. s = level + buf;
  337. label = strtok(s, "\t\n");
  338. /* get the output */
  339. output = strtok(NULL, "\n");
  340. if (output == NULL) {
  341. output = label;
  342. } else {
  343. while (*output == '\t')
  344. output++;
  345. }
  346. rootmenu = buildmenutree(level, label, output);
  347. }
  348. return rootmenu;
  349. }
  350. /* recursivelly calculate menu geometry and set window hints */
  351. static void
  352. calcmenu(struct Menu *menu)
  353. {
  354. static XClassHint classh = {PROGNAME, PROGNAME};
  355. XWindowChanges changes;
  356. XSizeHints sizeh;
  357. XGlyphInfo ext;
  358. struct Item *item;
  359. int labelwidth;
  360. int width, height;
  361. /* calculate items positions and menu width and height */
  362. menu->w = geom.itemw;
  363. for (item = menu->list; item != NULL; item = item->next) {
  364. item->y = menu->h;
  365. if (item->label == NULL) /* height for separator item */
  366. menu->h += geom.separator;
  367. else
  368. menu->h += geom.itemh;
  369. XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
  370. item->labellen, &ext);
  371. labelwidth = ext.xOff + dc.font->height * 2;
  372. menu->w = MAX(menu->w, labelwidth);
  373. }
  374. /* calculate menu's x and y positions */
  375. width = menu->w + geom.border * 2;
  376. height = menu->h + geom.border * 2;
  377. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  378. if (geom.screenw - geom.cursx >= menu->w)
  379. menu->x = geom.cursx;
  380. else if (geom.cursx > width)
  381. menu->x = geom.cursx - width;
  382. if (geom.screenh - geom.cursy >= height)
  383. menu->y = geom.cursy;
  384. else if (geom.screenh > height)
  385. menu->y = geom.screenh - height;
  386. } else { /* else, calculate in respect to parent menu */
  387. if (geom.screenw - (menu->parent->x + menu->parent->w + geom.border) >= width)
  388. menu->x = menu->parent->x + menu->parent->w + geom.border;
  389. else if (menu->parent->x > menu->w + geom.border)
  390. menu->x = menu->parent->x - menu->w - geom.border;
  391. if (geom.screenh - (menu->caller->y + menu->parent->y) > height)
  392. menu->y = menu->caller->y + menu->parent->y;
  393. else if (geom.screenh - menu->parent->y > height)
  394. menu->y = menu->parent->y;
  395. else if (geom.screenh > height)
  396. menu->y = geom.screenh - height;
  397. }
  398. /* update menu geometry */
  399. changes.height = menu->h;
  400. changes.width = menu->w;
  401. changes.x = menu->x;
  402. changes.y = menu->y;
  403. XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
  404. /* set window manager hints */
  405. sizeh.flags = PMaxSize | PMinSize;
  406. sizeh.min_width = sizeh.max_width = menu->w;
  407. sizeh.min_height = sizeh.max_height = menu->h;
  408. XSetWMProperties(dpy, menu->win, NULL, NULL, NULL, 0, &sizeh,
  409. NULL, &classh);
  410. /* create pixmap and XftDraw */
  411. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  412. DefaultDepth(dpy, screen));
  413. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  414. /* calculate positions of submenus */
  415. for (item = menu->list; item != NULL; item = item->next) {
  416. if (item->submenu != NULL)
  417. calcmenu(item->submenu);
  418. }
  419. }
  420. /* try to grab pointer, we may have to wait for another process to ungrab */
  421. static void
  422. grabpointer(void)
  423. {
  424. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  425. int i;
  426. for (i = 0; i < 1000; i++) {
  427. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  428. GrabModeAsync, GrabModeAsync, None,
  429. None, CurrentTime) == GrabSuccess)
  430. return;
  431. nanosleep(&ts, NULL);
  432. }
  433. errx(1, "cannot grab keyboard");
  434. }
  435. /* try to grab keyboard, we may have to wait for another process to ungrab */
  436. static void
  437. grabkeyboard(void)
  438. {
  439. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  440. int i;
  441. for (i = 0; i < 1000; i++) {
  442. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  443. GrabModeAsync, CurrentTime) == GrabSuccess)
  444. return;
  445. nanosleep(&ts, NULL);
  446. }
  447. errx(1, "cannot grab keyboard");
  448. }
  449. /* get menu of given window */
  450. static struct Menu *
  451. getmenu(struct Menu *currmenu, Window win)
  452. {
  453. struct Menu *menu;
  454. for (menu = currmenu; menu != NULL; menu = menu->parent)
  455. if (menu->win == win)
  456. return menu;
  457. return NULL;
  458. }
  459. /* get item of given menu and position */
  460. static struct Item *
  461. getitem(struct Menu *menu, int y)
  462. {
  463. struct Item *item;
  464. if (menu == NULL)
  465. return NULL;
  466. for (item = menu->list; item != NULL; item = item->next)
  467. if (y >= item->y && y <= item->y + item->h)
  468. return item;
  469. return NULL;
  470. }
  471. /* umap previous menus and map current menu and its parents */
  472. static void
  473. mapmenu(struct Menu *currmenu)
  474. {
  475. static struct Menu *prevmenu = NULL;
  476. struct Menu *menu, *menu_;
  477. struct Menu *lcamenu; /* lowest common ancestor menu */
  478. unsigned minlevel; /* level of the closest to root menu */
  479. unsigned maxlevel; /* level of the closest to root menu */
  480. /* do not remap current menu if it wasn't updated*/
  481. if (prevmenu == currmenu)
  482. return;
  483. /* if this is the first time mapping, skip calculations */
  484. if (prevmenu == NULL) {
  485. XMapWindow(dpy, currmenu->win);
  486. prevmenu = currmenu;
  487. return;
  488. }
  489. /* find lowest common ancestor menu */
  490. minlevel = MIN(currmenu->level, prevmenu->level);
  491. maxlevel = MAX(currmenu->level, prevmenu->level);
  492. if (currmenu->level == maxlevel) {
  493. menu = currmenu;
  494. menu_ = prevmenu;
  495. } else {
  496. menu = prevmenu;
  497. menu_ = currmenu;
  498. }
  499. while (menu->level > minlevel)
  500. menu = menu->parent;
  501. while (menu != menu_) {
  502. menu = menu->parent;
  503. menu_ = menu_->parent;
  504. }
  505. lcamenu = menu;
  506. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  507. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  508. menu->selected = NULL;
  509. XUnmapWindow(dpy, menu->win);
  510. }
  511. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  512. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  513. XMapWindow(dpy, menu->win);
  514. }
  515. prevmenu = currmenu;
  516. }
  517. /* draw separator item */
  518. static void
  519. drawseparator(struct Menu *menu, struct Item *item)
  520. {
  521. int y;
  522. y = item->y + item->h/2;
  523. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  524. XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
  525. }
  526. /* draw regular item */
  527. static void
  528. drawitem(struct Menu *menu, struct Item *item, XftColor *color)
  529. {
  530. int x, y;
  531. x = dc.font->height;
  532. y = item->y + item->h/2 + dc.font->ascent/2 - 1;
  533. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  534. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  535. x, y, item->label, item->labellen);
  536. /* draw triangle, if item contains a submenu */
  537. if (item->submenu != NULL) {
  538. x = menu->w - dc.font->height/2 - triangle_width/2;
  539. y = item->y + item->h/2 - triangle_height/2 - 1;
  540. XPoint triangle[] = {
  541. {x, y},
  542. {x + triangle_width, y + triangle_height/2},
  543. {x, y + triangle_height},
  544. {x, y}
  545. };
  546. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  547. Convex, CoordModeOrigin);
  548. }
  549. }
  550. /* draw items of the current menu and of its ancestors */
  551. static void
  552. drawmenu(struct Menu *currmenu)
  553. {
  554. struct Menu *menu;
  555. struct Item *item;
  556. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  557. for (item = menu->list; item != NULL; item = item->next) {
  558. XftColor *color;
  559. /* determine item color */
  560. if (item == menu->selected && item->label != NULL)
  561. color = dc.selected;
  562. else
  563. color = dc.normal;
  564. /* draw item box */
  565. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  566. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  567. menu->w, item->h);
  568. if (item->label == NULL) /* item is a separator */
  569. drawseparator(menu, item);
  570. else /* item is a regular item */
  571. drawitem(menu, item, color);
  572. }
  573. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
  574. menu->w, menu->h, 0, 0);
  575. }
  576. }
  577. /* cycle through the items; non-zero direction is next, zero is prev */
  578. static struct Item *
  579. itemcycle(struct Menu *currmenu, int direction)
  580. {
  581. struct Item *item;
  582. struct Item *lastitem;
  583. item = NULL;
  584. if (direction == ITEMNEXT) {
  585. if (currmenu->selected == NULL)
  586. item = currmenu->list;
  587. else if (currmenu->selected->next != NULL)
  588. item = currmenu->selected->next;
  589. while (item != NULL && item->label == NULL)
  590. item = item->next;
  591. if (item == NULL)
  592. item = currmenu->list;
  593. } else {
  594. for (lastitem = currmenu->list;
  595. lastitem != NULL && lastitem->next != NULL;
  596. lastitem = lastitem->next)
  597. ;
  598. if (currmenu->selected == NULL)
  599. item = lastitem;
  600. else if (currmenu->selected->prev != NULL)
  601. item = currmenu->selected->prev;
  602. while (item != NULL && item->label == NULL)
  603. item = item->prev;
  604. if (item == NULL)
  605. item = lastitem;
  606. }
  607. return item;
  608. }
  609. /* run event loop */
  610. static void
  611. run(struct Menu *currmenu)
  612. {
  613. struct Menu *menu;
  614. struct Item *item;
  615. struct Item *previtem = NULL;
  616. KeySym ksym;
  617. XEvent ev;
  618. mapmenu(currmenu);
  619. while (!XNextEvent(dpy, &ev)) {
  620. switch(ev.type) {
  621. case Expose:
  622. if (ev.xexpose.count == 0)
  623. drawmenu(currmenu);
  624. break;
  625. case MotionNotify:
  626. menu = getmenu(currmenu, ev.xbutton.window);
  627. item = getitem(menu, ev.xbutton.y);
  628. if (menu == NULL || item == NULL || previtem == item)
  629. break;
  630. previtem = item;
  631. menu->selected = item;
  632. if (item->submenu != NULL) {
  633. currmenu = item->submenu;
  634. currmenu->selected = NULL;
  635. } else {
  636. currmenu = menu;
  637. }
  638. mapmenu(currmenu);
  639. drawmenu(currmenu);
  640. break;
  641. case ButtonRelease:
  642. menu = getmenu(currmenu, ev.xbutton.window);
  643. item = getitem(menu, ev.xbutton.y);
  644. if (menu == NULL || item == NULL)
  645. break;
  646. selectitem:
  647. if (item->label == NULL)
  648. break; /* ignore separators */
  649. if (item->submenu != NULL) {
  650. currmenu = item->submenu;
  651. } else {
  652. printf("%s\n", item->output);
  653. return;
  654. }
  655. mapmenu(currmenu);
  656. currmenu->selected = currmenu->list;
  657. drawmenu(currmenu);
  658. break;
  659. case ButtonPress:
  660. menu = getmenu(currmenu, ev.xbutton.window);
  661. if (menu == NULL)
  662. return;
  663. break;
  664. case KeyPress:
  665. ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
  666. /* esc closes xmenu when current menu is the root menu */
  667. if (ksym == XK_Escape && currmenu->parent == NULL)
  668. return;
  669. /* Shift-Tab = ISO_Left_Tab */
  670. if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
  671. ksym = XK_ISO_Left_Tab;
  672. /* cycle through menu */
  673. item = NULL;
  674. if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
  675. item = itemcycle(currmenu, ITEMPREV);
  676. } else if (ksym == XK_Tab || ksym == XK_Down) {
  677. item = itemcycle(currmenu, ITEMNEXT);
  678. } else if ((ksym == XK_Return || ksym == XK_Right) &&
  679. currmenu->selected != NULL) {
  680. item = currmenu->selected;
  681. goto selectitem;
  682. } else if ((ksym == XK_Escape || ksym == XK_Left) &&
  683. currmenu->parent != NULL) {
  684. item = currmenu->parent->selected;
  685. currmenu = currmenu->parent;
  686. mapmenu(currmenu);
  687. } else
  688. break;
  689. currmenu->selected = item;
  690. drawmenu(currmenu);
  691. break;
  692. case LeaveNotify:
  693. previtem = NULL;
  694. currmenu->selected = NULL;
  695. drawmenu(currmenu);
  696. break;
  697. }
  698. }
  699. }
  700. /* recursivelly free pixmaps and destroy windows */
  701. static void
  702. freemenu(struct Menu *menu)
  703. {
  704. struct Item *item;
  705. struct Item *tmp;
  706. item = menu->list;
  707. while (item != NULL) {
  708. if (item->submenu != NULL)
  709. freemenu(item->submenu);
  710. tmp = item;
  711. item = item->next;
  712. if (tmp->label != tmp->output)
  713. free(tmp->label);
  714. free(tmp->output);
  715. free(tmp);
  716. }
  717. XFreePixmap(dpy, menu->pixmap);
  718. XftDrawDestroy(menu->draw);
  719. XDestroyWindow(dpy, menu->win);
  720. free(menu);
  721. }
  722. /* cleanup and exit */
  723. static void
  724. cleanup(void)
  725. {
  726. XUngrabPointer(dpy, CurrentTime);
  727. XUngrabKeyboard(dpy, CurrentTime);
  728. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  729. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  730. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  731. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  732. XftColorFree(dpy, visual, colormap, &dc.separator);
  733. XftColorFree(dpy, visual, colormap, &dc.border);
  734. XFreeGC(dpy, dc.gc);
  735. XCloseDisplay(dpy);
  736. }
  737. /* show usage */
  738. static void
  739. usage(void)
  740. {
  741. (void)fprintf(stderr, "usage: xmenu\n");
  742. exit(1);
  743. }