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.
 
 
 
 
 

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