A mirror of phillbush's xmenu.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

860 satır
22 KiB

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