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.
 
 
 
 
 

1002 lines
26 KiB

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