A mirror of phillbush's xmenu.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

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