A mirror of phillbush's xmenu.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1012 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. #define IMGPADDING 8
  16. /* macros */
  17. #define LEN(x) (sizeof (x) / sizeof (x[0]))
  18. #define MAX(x,y) ((x)>(y)?(x):(y))
  19. #define MIN(x,y) ((x)<(y)?(x):(y))
  20. /* color enum */
  21. enum {ColorFG, ColorBG, ColorLast};
  22. /* draw context structure */
  23. struct DC {
  24. XftColor normal[ColorLast];
  25. XftColor selected[ColorLast];
  26. XftColor border;
  27. XftColor separator;
  28. GC gc;
  29. XftFont *font;
  30. };
  31. /* menu geometry structure */
  32. struct Geometry {
  33. int border; /* window border width */
  34. int separator; /* menu separator width */
  35. int itemw, itemh; /* item width and height */
  36. int cursx, cursy; /* cursor position */
  37. int screenw, screenh; /* screen width and height */
  38. };
  39. /* menu item structure */
  40. struct Item {
  41. char *label; /* string to be drawed on menu */
  42. char *output; /* string to be outputed when item is clicked */
  43. char *file; /* filename of the icon */
  44. int y; /* item y position relative to menu */
  45. int h; /* item height */
  46. size_t labellen; /* strlen(label) */
  47. struct Item *prev; /* previous item */
  48. struct Item *next; /* next item */
  49. struct Menu *submenu; /* submenu spawned by clicking on item */
  50. Imlib_Image icon;
  51. };
  52. /* menu structure */
  53. struct Menu {
  54. struct Menu *parent; /* parent menu */
  55. struct Item *caller; /* item that spawned the menu */
  56. struct Item *list; /* list of items contained by the menu */
  57. struct Item *selected; /* item currently selected in the menu */
  58. int x, y, w, h; /* menu geometry */
  59. unsigned level; /* menu level relative to root */
  60. Drawable pixmap; /* pixmap to draw the menu on */
  61. XftDraw *draw;
  62. Window win; /* menu window to map on the screen */
  63. };
  64. /* functions declarations */
  65. static void getresources(void);
  66. static void getcolor(const char *s, XftColor *color);
  67. static void setupdc(void);
  68. static void calcgeom(struct Geometry *geom);
  69. static struct Item *allocitem(const char *label, const char *output, char *file);
  70. static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
  71. static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file);
  72. static struct Menu *parsestdin(void);
  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.padding", "*", &type, &xval) == True)
  183. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  184. padding_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 = dc.font->height + padding_pixels * 2;
  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. labelwidth = ext.xOff + dc.font->height * 2 + IMGPADDING * 2;
  433. menu->w = MAX(menu->w, labelwidth);
  434. /* create icon */
  435. if (item->file != NULL)
  436. item->icon = loadicon(item->file, dc.font->height);
  437. }
  438. }
  439. /* setup the position of a menu */
  440. static void
  441. setupmenupos(struct Geometry *g, struct Menu *menu)
  442. {
  443. static struct Geometry *geom = NULL;
  444. int width, height;
  445. /*
  446. * Save the geometry so functions can call setupmenupos() without
  447. * having to know the geometry.
  448. */
  449. if (g != NULL)
  450. geom = g;
  451. width = menu->w + geom->border * 2;
  452. height = menu->h + geom->border * 2;
  453. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  454. if (geom->screenw - geom->cursx >= menu->w)
  455. menu->x = geom->cursx;
  456. else if (geom->cursx > width)
  457. menu->x = geom->cursx - width;
  458. if (geom->screenh - geom->cursy >= height)
  459. menu->y = geom->cursy;
  460. else if (geom->screenh > height)
  461. menu->y = geom->screenh - height;
  462. } else { /* else, calculate in respect to parent menu */
  463. if (geom->screenw - (menu->parent->x + menu->parent->w + geom->border) >= width)
  464. menu->x = menu->parent->x + menu->parent->w + geom->border;
  465. else if (menu->parent->x > menu->w + geom->border)
  466. menu->x = menu->parent->x - menu->w - geom->border;
  467. if (geom->screenh - (menu->caller->y + menu->parent->y) > height)
  468. menu->y = menu->caller->y + menu->parent->y;
  469. else if (geom->screenh - menu->parent->y > height)
  470. menu->y = menu->parent->y;
  471. else if (geom->screenh > height)
  472. menu->y = geom->screenh - height;
  473. }
  474. }
  475. /* recursivelly setup menu configuration and its pixmap */
  476. static void
  477. setupmenu(struct Geometry *geom, struct Menu *menu, XClassHint *classh)
  478. {
  479. struct Item *item;
  480. XWindowChanges changes;
  481. XSizeHints sizeh;
  482. XTextProperty wintitle;
  483. /* setup size and position of menus */
  484. setupmenusize(geom, menu);
  485. setupmenupos(geom, menu);
  486. /* update menu geometry */
  487. changes.border_width = geom->border;
  488. changes.height = menu->h;
  489. changes.width = menu->w;
  490. changes.x = menu->x;
  491. changes.y = menu->y;
  492. XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
  493. /* set window title (used if wflag is on) */
  494. if (menu->parent == NULL) {
  495. XStringListToTextProperty(&classh->res_name, 1, &wintitle);
  496. } else {
  497. XStringListToTextProperty(&menu->caller->output, 1, &wintitle);
  498. }
  499. /* set window manager hints */
  500. sizeh.flags = PMaxSize | PMinSize;
  501. sizeh.min_width = sizeh.max_width = menu->w;
  502. sizeh.min_height = sizeh.max_height = menu->h;
  503. XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh);
  504. /* create pixmap and XftDraw */
  505. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  506. DefaultDepth(dpy, screen));
  507. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  508. /* calculate positions of submenus */
  509. for (item = menu->list; item != NULL; item = item->next) {
  510. if (item->submenu != NULL)
  511. setupmenu(geom, item->submenu, classh);
  512. }
  513. }
  514. /* try to grab pointer, we may have to wait for another process to ungrab */
  515. static void
  516. grabpointer(void)
  517. {
  518. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  519. int i;
  520. for (i = 0; i < 1000; i++) {
  521. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  522. GrabModeAsync, GrabModeAsync, None,
  523. None, CurrentTime) == GrabSuccess)
  524. return;
  525. nanosleep(&ts, NULL);
  526. }
  527. errx(1, "cannot grab keyboard");
  528. }
  529. /* try to grab keyboard, we may have to wait for another process to ungrab */
  530. static void
  531. grabkeyboard(void)
  532. {
  533. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  534. int i;
  535. for (i = 0; i < 1000; i++) {
  536. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  537. GrabModeAsync, CurrentTime) == GrabSuccess)
  538. return;
  539. nanosleep(&ts, NULL);
  540. }
  541. errx(1, "cannot grab keyboard");
  542. }
  543. /* get menu of given window */
  544. static struct Menu *
  545. getmenu(struct Menu *currmenu, Window win)
  546. {
  547. struct Menu *menu;
  548. for (menu = currmenu; menu != NULL; menu = menu->parent)
  549. if (menu->win == win)
  550. return menu;
  551. return NULL;
  552. }
  553. /* get item of given menu and position */
  554. static struct Item *
  555. getitem(struct Menu *menu, int y)
  556. {
  557. struct Item *item;
  558. if (menu == NULL)
  559. return NULL;
  560. for (item = menu->list; item != NULL; item = item->next)
  561. if (y >= item->y && y <= item->y + item->h)
  562. return item;
  563. return NULL;
  564. }
  565. /* umap previous menus and map current menu and its parents */
  566. static void
  567. mapmenu(struct Menu *currmenu)
  568. {
  569. static struct Menu *prevmenu = NULL;
  570. struct Menu *menu, *menu_;
  571. struct Menu *lcamenu; /* lowest common ancestor menu */
  572. unsigned minlevel; /* level of the closest to root menu */
  573. unsigned maxlevel; /* level of the closest to root menu */
  574. /* do not remap current menu if it wasn't updated*/
  575. if (prevmenu == currmenu)
  576. return;
  577. /* if this is the first time mapping, skip calculations */
  578. if (prevmenu == NULL) {
  579. XMapWindow(dpy, currmenu->win);
  580. prevmenu = currmenu;
  581. return;
  582. }
  583. /* find lowest common ancestor menu */
  584. minlevel = MIN(currmenu->level, prevmenu->level);
  585. maxlevel = MAX(currmenu->level, prevmenu->level);
  586. if (currmenu->level == maxlevel) {
  587. menu = currmenu;
  588. menu_ = prevmenu;
  589. } else {
  590. menu = prevmenu;
  591. menu_ = currmenu;
  592. }
  593. while (menu->level > minlevel)
  594. menu = menu->parent;
  595. while (menu != menu_) {
  596. menu = menu->parent;
  597. menu_ = menu_->parent;
  598. }
  599. lcamenu = menu;
  600. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  601. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  602. menu->selected = NULL;
  603. XUnmapWindow(dpy, menu->win);
  604. }
  605. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  606. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  607. if (wflag) {
  608. setupmenupos(NULL, menu);
  609. XMoveWindow(dpy, menu->win, menu->x, menu->y);
  610. }
  611. XMapWindow(dpy, menu->win);
  612. }
  613. prevmenu = currmenu;
  614. }
  615. /* draw separator item */
  616. static void
  617. drawseparator(struct Menu *menu, struct Item *item)
  618. {
  619. int y;
  620. y = item->y + item->h/2;
  621. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  622. XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
  623. }
  624. /* draw regular item */
  625. static void
  626. drawitem(struct Menu *menu, struct Item *item, XftColor *color)
  627. {
  628. int x, y;
  629. x = dc.font->height + IMGPADDING;
  630. y = item->y + item->h/2 + dc.font->ascent/2 - 1;
  631. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  632. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  633. x, y, item->label, item->labellen);
  634. /* draw triangle, if item contains a submenu */
  635. if (item->submenu != NULL) {
  636. x = menu->w - dc.font->height/2 - IMGPADDING/2 - triangle_width/2 - 1;
  637. y = item->y + item->h/2 - triangle_height/2 - 1;
  638. XPoint triangle[] = {
  639. {x, y},
  640. {x + triangle_width, y + triangle_height/2},
  641. {x, y + triangle_height},
  642. {x, y}
  643. };
  644. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  645. Convex, CoordModeOrigin);
  646. }
  647. /* draw icon */
  648. if (item->file != NULL) {
  649. x = IMGPADDING / 2;
  650. y = item->y + (item->h - dc.font->height) / 2;
  651. imlib_context_set_drawable(menu->pixmap);
  652. imlib_context_set_image(item->icon);
  653. imlib_render_image_on_drawable(x, y);
  654. }
  655. }
  656. /* draw items of the current menu and of its ancestors */
  657. static void
  658. drawmenu(struct Menu *currmenu)
  659. {
  660. struct Menu *menu;
  661. struct Item *item;
  662. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  663. for (item = menu->list; item != NULL; item = item->next) {
  664. XftColor *color;
  665. /* determine item color */
  666. if (item == menu->selected && item->label != NULL)
  667. color = dc.selected;
  668. else
  669. color = dc.normal;
  670. /* draw item box */
  671. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  672. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  673. menu->w, item->h);
  674. if (item->label == NULL) /* item is a separator */
  675. drawseparator(menu, item);
  676. else /* item is a regular item */
  677. drawitem(menu, item, color);
  678. }
  679. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
  680. menu->w, menu->h, 0, 0);
  681. }
  682. }
  683. /* cycle through the items; non-zero direction is next, zero is prev */
  684. static struct Item *
  685. itemcycle(struct Menu *currmenu, int direction)
  686. {
  687. struct Item *item;
  688. struct Item *lastitem;
  689. item = NULL;
  690. if (direction == ITEMNEXT) {
  691. if (currmenu->selected == NULL)
  692. item = currmenu->list;
  693. else if (currmenu->selected->next != NULL)
  694. item = currmenu->selected->next;
  695. while (item != NULL && item->label == NULL)
  696. item = item->next;
  697. if (item == NULL)
  698. item = currmenu->list;
  699. } else {
  700. for (lastitem = currmenu->list;
  701. lastitem != NULL && lastitem->next != NULL;
  702. lastitem = lastitem->next)
  703. ;
  704. if (currmenu->selected == NULL)
  705. item = lastitem;
  706. else if (currmenu->selected->prev != NULL)
  707. item = currmenu->selected->prev;
  708. while (item != NULL && item->label == NULL)
  709. item = item->prev;
  710. if (item == NULL)
  711. item = lastitem;
  712. }
  713. return item;
  714. }
  715. /* run event loop */
  716. static void
  717. run(struct Menu *currmenu)
  718. {
  719. struct Menu *menu;
  720. struct Item *item;
  721. struct Item *previtem = NULL;
  722. KeySym ksym;
  723. XEvent ev;
  724. mapmenu(currmenu);
  725. while (!XNextEvent(dpy, &ev)) {
  726. switch(ev.type) {
  727. case Expose:
  728. if (ev.xexpose.count == 0)
  729. drawmenu(currmenu);
  730. break;
  731. case MotionNotify:
  732. menu = getmenu(currmenu, ev.xbutton.window);
  733. item = getitem(menu, ev.xbutton.y);
  734. if (menu == NULL || item == NULL || previtem == item)
  735. break;
  736. previtem = item;
  737. menu->selected = item;
  738. if (item->submenu != NULL) {
  739. currmenu = item->submenu;
  740. currmenu->selected = NULL;
  741. } else {
  742. currmenu = menu;
  743. }
  744. mapmenu(currmenu);
  745. drawmenu(currmenu);
  746. break;
  747. case ButtonRelease:
  748. menu = getmenu(currmenu, ev.xbutton.window);
  749. item = getitem(menu, ev.xbutton.y);
  750. if (menu == NULL || item == NULL)
  751. break;
  752. selectitem:
  753. if (item->label == NULL)
  754. break; /* ignore separators */
  755. if (item->submenu != NULL) {
  756. currmenu = item->submenu;
  757. } else {
  758. printf("%s\n", item->output);
  759. return;
  760. }
  761. mapmenu(currmenu);
  762. currmenu->selected = currmenu->list;
  763. drawmenu(currmenu);
  764. break;
  765. case ButtonPress:
  766. menu = getmenu(currmenu, ev.xbutton.window);
  767. if (menu == NULL)
  768. return;
  769. break;
  770. case KeyPress:
  771. ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
  772. /* esc closes xmenu when current menu is the root menu */
  773. if (ksym == XK_Escape && currmenu->parent == NULL)
  774. return;
  775. /* Shift-Tab = ISO_Left_Tab */
  776. if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
  777. ksym = XK_ISO_Left_Tab;
  778. /* cycle through menu */
  779. item = NULL;
  780. if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
  781. item = itemcycle(currmenu, ITEMPREV);
  782. } else if (ksym == XK_Tab || ksym == XK_Down) {
  783. item = itemcycle(currmenu, ITEMNEXT);
  784. } else if ((ksym == XK_Return || ksym == XK_Right) &&
  785. currmenu->selected != NULL) {
  786. item = currmenu->selected;
  787. goto selectitem;
  788. } else if ((ksym == XK_Escape || ksym == XK_Left) &&
  789. currmenu->parent != NULL) {
  790. item = currmenu->parent->selected;
  791. currmenu = currmenu->parent;
  792. mapmenu(currmenu);
  793. } else
  794. break;
  795. currmenu->selected = item;
  796. drawmenu(currmenu);
  797. break;
  798. case LeaveNotify:
  799. previtem = NULL;
  800. currmenu->selected = NULL;
  801. drawmenu(currmenu);
  802. break;
  803. case ConfigureNotify:
  804. menu = getmenu(currmenu, ev.xconfigure.window);
  805. if (menu == NULL)
  806. break;
  807. menu->x = ev.xconfigure.x;
  808. menu->y = ev.xconfigure.y;
  809. break;
  810. case ClientMessage:
  811. /* user closed window */
  812. menu = getmenu(currmenu, ev.xclient.window);
  813. if (menu->parent == NULL)
  814. return; /* closing the root menu closes the program */
  815. currmenu = menu->parent;
  816. mapmenu(currmenu);
  817. break;
  818. }
  819. }
  820. }
  821. /* recursivelly free pixmaps and destroy windows */
  822. static void
  823. freemenu(struct Menu *menu)
  824. {
  825. struct Item *item;
  826. struct Item *tmp;
  827. item = menu->list;
  828. while (item != NULL) {
  829. if (item->submenu != NULL)
  830. freemenu(item->submenu);
  831. tmp = item;
  832. if (tmp->label != tmp->output)
  833. free(tmp->label);
  834. free(tmp->output);
  835. if (tmp->file != NULL) {
  836. free(tmp->file);
  837. if (tmp->icon != NULL) {
  838. imlib_context_set_image(tmp->icon);
  839. imlib_free_image();
  840. }
  841. }
  842. item = item->next;
  843. free(tmp);
  844. }
  845. XFreePixmap(dpy, menu->pixmap);
  846. XftDrawDestroy(menu->draw);
  847. XDestroyWindow(dpy, menu->win);
  848. free(menu);
  849. }
  850. /* cleanup and exit */
  851. static void
  852. cleanup(void)
  853. {
  854. XUngrabPointer(dpy, CurrentTime);
  855. XUngrabKeyboard(dpy, CurrentTime);
  856. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  857. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  858. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  859. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  860. XftColorFree(dpy, visual, colormap, &dc.separator);
  861. XftColorFree(dpy, visual, colormap, &dc.border);
  862. XFreeGC(dpy, dc.gc);
  863. XCloseDisplay(dpy);
  864. }
  865. /* show usage */
  866. static void
  867. usage(void)
  868. {
  869. (void)fprintf(stderr, "usage: xmenu [-w] [title]\n");
  870. exit(1);
  871. }