A mirror of phillbush's xmenu.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

865 行
22 KiB

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