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.
 
 
 
 
 

1000 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 and create its window */
  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. return menu;
  289. }
  290. /* build the menu tree */
  291. static struct Menu *
  292. buildmenutree(unsigned level, const char *label, const char *output, char *file)
  293. {
  294. static struct Menu *prevmenu = NULL; /* menu the previous item was added to */
  295. static struct Menu *rootmenu = NULL; /* menu to be returned */
  296. struct Item *curritem = NULL; /* item currently being read */
  297. struct Item *item; /* dummy item for loops */
  298. struct Menu *menu; /* dummy menu for loops */
  299. unsigned i;
  300. /* create the item */
  301. curritem = allocitem(label, output, file);
  302. /* put the item in the menu tree */
  303. if (prevmenu == NULL) { /* there is no menu yet */
  304. menu = allocmenu(NULL, curritem, level);
  305. rootmenu = menu;
  306. prevmenu = menu;
  307. curritem->prev = NULL;
  308. } else if (level < prevmenu->level) { /* item is continuation of a parent menu */
  309. /* go up the menu tree until find the menu this item continues */
  310. for (menu = prevmenu, i = level;
  311. menu != NULL && i != prevmenu->level;
  312. menu = menu->parent, i++)
  313. ;
  314. if (menu == NULL)
  315. errx(1, "reached NULL menu");
  316. /* find last item in the new menu */
  317. for (item = menu->list; item->next != NULL; item = item->next)
  318. ;
  319. prevmenu = menu;
  320. item->next = curritem;
  321. curritem->prev = item;
  322. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  323. /* find last item in the previous menu */
  324. for (item = prevmenu->list; item->next != NULL; item = item->next)
  325. ;
  326. item->next = curritem;
  327. curritem->prev = item;
  328. } else if (level > prevmenu->level) { /* item begins a new menu */
  329. menu = allocmenu(prevmenu, curritem, level);
  330. /* find last item in the previous menu */
  331. for (item = prevmenu->list; item->next != NULL; item = item->next)
  332. ;
  333. prevmenu = menu;
  334. menu->caller = item;
  335. item->submenu = menu;
  336. curritem->prev = NULL;
  337. }
  338. return rootmenu;
  339. }
  340. /* create menus and items from the stdin */
  341. static struct Menu *
  342. parsestdin(void)
  343. {
  344. struct Menu *rootmenu;
  345. char *s, buf[BUFSIZ];
  346. char *file, *label, *output;
  347. unsigned level = 0;
  348. rootmenu = NULL;
  349. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  350. /* get the indentation level */
  351. level = strspn(buf, "\t");
  352. /* get the label */
  353. s = level + buf;
  354. label = strtok(s, "\t\n");
  355. /* get the filename */
  356. file = NULL;
  357. if (label != NULL && strncmp(label, "IMG:", 4) == 0) {
  358. file = label + 4;
  359. label = strtok(NULL, "\t\n");
  360. }
  361. /* get the output */
  362. output = strtok(NULL, "\n");
  363. if (output == NULL) {
  364. output = label;
  365. } else {
  366. while (*output == '\t')
  367. output++;
  368. }
  369. rootmenu = buildmenutree(level, label, output, file);
  370. }
  371. return rootmenu;
  372. }
  373. /* load and scale icon */
  374. static Imlib_Image
  375. loadicon(const char *file, int size)
  376. {
  377. Imlib_Image icon;
  378. int width;
  379. int height;
  380. int imgsize;
  381. icon = imlib_load_image(file);
  382. if (icon == NULL)
  383. errx(1, "cannot load icon %s", file);
  384. imlib_context_set_image(icon);
  385. width = imlib_image_get_width();
  386. height = imlib_image_get_height();
  387. imgsize = MIN(width, height);
  388. icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize, size, size);
  389. return icon;
  390. }
  391. /* setup the size of a menu and the position of its items */
  392. static void
  393. setupmenusize(struct Menu *menu)
  394. {
  395. XGlyphInfo ext;
  396. struct Item *item;
  397. int labelwidth;
  398. menu->w = config.width_pixels;
  399. for (item = menu->list; item != NULL; item = item->next) {
  400. item->y = menu->h;
  401. if (item->label == NULL) /* height for separator item */
  402. item->h = config.separator_pixels;
  403. else
  404. item->h = config.height_pixels;
  405. menu->h += item->h;
  406. /* get length of item->label rendered in the font */
  407. XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
  408. item->labellen, &ext);
  409. /* set menu width */
  410. labelwidth = ext.xOff + item->h * 2;
  411. menu->w = MAX(menu->w, labelwidth);
  412. /* create icon */
  413. if (item->file != NULL)
  414. item->icon = loadicon(item->file, item->h - config.iconpadding * 2);
  415. }
  416. }
  417. /* setup the position of a menu */
  418. static void
  419. setupmenupos(struct Menu *menu)
  420. {
  421. int width, height;
  422. width = menu->w + config.border_pixels * 2;
  423. height = menu->h + config.border_pixels * 2;
  424. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  425. if (config.screenw - config.cursx >= menu->w)
  426. menu->x = config.cursx;
  427. else if (config.cursx > width)
  428. menu->x = config.cursx - width;
  429. if (config.screenh - config.cursy >= height)
  430. menu->y = config.cursy;
  431. else if (config.screenh > height)
  432. menu->y = config.screenh - height;
  433. } else { /* else, calculate in respect to parent menu */
  434. if (config.screenw - (menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels) >= width)
  435. menu->x = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
  436. else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
  437. menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
  438. if (config.screenh - (menu->caller->y + menu->parent->y) > height)
  439. menu->y = menu->caller->y + menu->parent->y;
  440. else if (config.screenh - menu->parent->y > height)
  441. menu->y = menu->parent->y;
  442. else if (config.screenh > height)
  443. menu->y = config.screenh - height;
  444. }
  445. }
  446. /* recursivelly setup menu configuration and its pixmap */
  447. static void
  448. setupmenu(struct Menu *menu, XClassHint *classh)
  449. {
  450. char *title;
  451. struct Item *item;
  452. XWindowChanges changes;
  453. XSizeHints sizeh;
  454. XTextProperty wintitle;
  455. /* setup size and position of menus */
  456. setupmenusize(menu);
  457. setupmenupos(menu);
  458. /* update menu geometry */
  459. changes.border_width = config.border_pixels;
  460. changes.height = menu->h;
  461. changes.width = menu->w;
  462. changes.x = menu->x;
  463. changes.y = menu->y;
  464. XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
  465. /* set window title (used if wflag is on) */
  466. if (menu->parent == NULL) {
  467. title = classh->res_name;
  468. } else {
  469. title = menu->caller->output;
  470. }
  471. XStringListToTextProperty(&title, 1, &wintitle);
  472. /* set window manager hints */
  473. sizeh.flags = PMaxSize | PMinSize;
  474. sizeh.min_width = sizeh.max_width = menu->w;
  475. sizeh.min_height = sizeh.max_height = menu->h;
  476. XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh);
  477. /* create pixmap and XftDraw */
  478. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  479. DefaultDepth(dpy, screen));
  480. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  481. /* set WM protocols and ewmh window properties */
  482. XSetWMProtocols(dpy, menu->win, &wmdelete, 1);
  483. XChangeProperty(dpy, menu->win, netatom[NetWMName], utf8string, 8,
  484. PropModeReplace, (unsigned char *)title, strlen(title));
  485. XChangeProperty(dpy, menu->win, netatom[NetWMWindowType], XA_ATOM, 32,
  486. PropModeReplace,
  487. (unsigned char *)&netatom[NetWMWindowTypePopupMenu], 1);
  488. /* calculate positions of submenus */
  489. for (item = menu->list; item != NULL; item = item->next) {
  490. if (item->submenu != NULL)
  491. setupmenu(item->submenu, classh);
  492. }
  493. }
  494. /* try to grab pointer, we may have to wait for another process to ungrab */
  495. static void
  496. grabpointer(void)
  497. {
  498. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  499. int i;
  500. for (i = 0; i < 1000; i++) {
  501. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  502. GrabModeAsync, GrabModeAsync, None,
  503. None, CurrentTime) == GrabSuccess)
  504. return;
  505. nanosleep(&ts, NULL);
  506. }
  507. errx(1, "cannot grab keyboard");
  508. }
  509. /* try to grab keyboard, we may have to wait for another process to ungrab */
  510. static void
  511. grabkeyboard(void)
  512. {
  513. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  514. int i;
  515. for (i = 0; i < 1000; i++) {
  516. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  517. GrabModeAsync, CurrentTime) == GrabSuccess)
  518. return;
  519. nanosleep(&ts, NULL);
  520. }
  521. errx(1, "cannot grab keyboard");
  522. }
  523. /* umap previous menus and map current menu and its parents */
  524. static void
  525. mapmenu(struct Menu *currmenu)
  526. {
  527. static struct Menu *prevmenu = NULL;
  528. struct Menu *menu, *menu_;
  529. struct Menu *lcamenu; /* lowest common ancestor menu */
  530. unsigned minlevel; /* level of the closest to root menu */
  531. unsigned maxlevel; /* level of the closest to root menu */
  532. /* do not remap current menu if it wasn't updated*/
  533. if (prevmenu == currmenu)
  534. return;
  535. /* if this is the first time mapping, skip calculations */
  536. if (prevmenu == NULL) {
  537. XMapWindow(dpy, currmenu->win);
  538. prevmenu = currmenu;
  539. return;
  540. }
  541. /* find lowest common ancestor menu */
  542. minlevel = MIN(currmenu->level, prevmenu->level);
  543. maxlevel = MAX(currmenu->level, prevmenu->level);
  544. if (currmenu->level == maxlevel) {
  545. menu = currmenu;
  546. menu_ = prevmenu;
  547. } else {
  548. menu = prevmenu;
  549. menu_ = currmenu;
  550. }
  551. while (menu->level > minlevel)
  552. menu = menu->parent;
  553. while (menu != menu_) {
  554. menu = menu->parent;
  555. menu_ = menu_->parent;
  556. }
  557. lcamenu = menu;
  558. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  559. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  560. menu->selected = NULL;
  561. XUnmapWindow(dpy, menu->win);
  562. }
  563. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  564. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  565. if (wflag) {
  566. setupmenupos(menu);
  567. XMoveWindow(dpy, menu->win, menu->x, menu->y);
  568. }
  569. XMapWindow(dpy, menu->win);
  570. }
  571. prevmenu = currmenu;
  572. }
  573. /* draw separator item */
  574. static void
  575. drawseparator(struct Menu *menu, struct Item *item)
  576. {
  577. int y;
  578. y = item->y + item->h/2;
  579. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  580. XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
  581. }
  582. /* draw regular item */
  583. static void
  584. drawitem(struct Menu *menu, struct Item *item, XftColor *color)
  585. {
  586. int x, y;
  587. x = item->h;
  588. y = item->y + (item->h + dc.font->ascent) / 2;
  589. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  590. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  591. x, y, (XftChar8 *)item->label, item->labellen);
  592. /* draw triangle, if item contains a submenu */
  593. if (item->submenu != NULL) {
  594. x = menu->w - (item->h + config.triangle_width + 1) / 2;
  595. y = item->y + (item->h - config.triangle_height + 1) / 2;
  596. XPoint triangle[] = {
  597. {x, y},
  598. {x + config.triangle_width, y + config.triangle_height/2},
  599. {x, y + config.triangle_height},
  600. {x, y}
  601. };
  602. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  603. Convex, CoordModeOrigin);
  604. }
  605. /* draw icon */
  606. if (item->file != NULL) {
  607. x = config.iconpadding;
  608. y = item->y + config.iconpadding;
  609. imlib_context_set_drawable(menu->pixmap);
  610. imlib_context_set_image(item->icon);
  611. imlib_render_image_on_drawable(x, y);
  612. }
  613. }
  614. /* draw items of the current menu and of its ancestors */
  615. static void
  616. drawmenu(struct Menu *currmenu)
  617. {
  618. struct Menu *menu;
  619. struct Item *item;
  620. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  621. for (item = menu->list; item != NULL; item = item->next) {
  622. XftColor *color;
  623. /* determine item color */
  624. if (item == menu->selected && item->label != NULL)
  625. color = dc.selected;
  626. else
  627. color = dc.normal;
  628. /* draw item box */
  629. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  630. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  631. menu->w, item->h);
  632. if (item->label == NULL) /* item is a separator */
  633. drawseparator(menu, item);
  634. else /* item is a regular item */
  635. drawitem(menu, item, color);
  636. }
  637. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
  638. menu->w, menu->h, 0, 0);
  639. }
  640. }
  641. /* get menu of given window */
  642. static struct Menu *
  643. getmenu(struct Menu *currmenu, Window win)
  644. {
  645. struct Menu *menu;
  646. for (menu = currmenu; menu != NULL; menu = menu->parent)
  647. if (menu->win == win)
  648. return menu;
  649. return NULL;
  650. }
  651. /* get item of given menu and position */
  652. static struct Item *
  653. getitem(struct Menu *menu, int y)
  654. {
  655. struct Item *item;
  656. if (menu == NULL)
  657. return NULL;
  658. for (item = menu->list; item != NULL; item = item->next)
  659. if (y >= item->y && y <= item->y + item->h)
  660. return item;
  661. return NULL;
  662. }
  663. /* cycle through the items; non-zero direction is next, zero is prev */
  664. static struct Item *
  665. itemcycle(struct Menu *currmenu, int direction)
  666. {
  667. struct Item *item;
  668. struct Item *lastitem;
  669. item = NULL;
  670. if (direction == ITEMNEXT) {
  671. if (currmenu->selected == NULL)
  672. item = currmenu->list;
  673. else if (currmenu->selected->next != NULL)
  674. item = currmenu->selected->next;
  675. while (item != NULL && item->label == NULL)
  676. item = item->next;
  677. if (item == NULL)
  678. item = currmenu->list;
  679. } else {
  680. for (lastitem = currmenu->list;
  681. lastitem != NULL && lastitem->next != NULL;
  682. lastitem = lastitem->next)
  683. ;
  684. if (currmenu->selected == NULL)
  685. item = lastitem;
  686. else if (currmenu->selected->prev != NULL)
  687. item = currmenu->selected->prev;
  688. while (item != NULL && item->label == NULL)
  689. item = item->prev;
  690. if (item == NULL)
  691. item = lastitem;
  692. }
  693. return item;
  694. }
  695. /* run event loop */
  696. static void
  697. run(struct Menu *currmenu)
  698. {
  699. struct Menu *menu;
  700. struct Item *item;
  701. struct Item *previtem = NULL;
  702. KeySym ksym;
  703. XEvent ev;
  704. mapmenu(currmenu);
  705. while (!XNextEvent(dpy, &ev)) {
  706. switch(ev.type) {
  707. case Expose:
  708. if (ev.xexpose.count == 0)
  709. drawmenu(currmenu);
  710. break;
  711. case MotionNotify:
  712. menu = getmenu(currmenu, ev.xbutton.window);
  713. item = getitem(menu, ev.xbutton.y);
  714. if (menu == NULL || item == NULL || previtem == item)
  715. break;
  716. previtem = item;
  717. menu->selected = item;
  718. if (item->submenu != NULL) {
  719. currmenu = item->submenu;
  720. currmenu->selected = NULL;
  721. } else {
  722. currmenu = menu;
  723. }
  724. mapmenu(currmenu);
  725. drawmenu(currmenu);
  726. break;
  727. case ButtonRelease:
  728. menu = getmenu(currmenu, ev.xbutton.window);
  729. item = getitem(menu, ev.xbutton.y);
  730. if (menu == NULL || item == NULL)
  731. break;
  732. selectitem:
  733. if (item->label == NULL)
  734. break; /* ignore separators */
  735. if (item->submenu != NULL) {
  736. currmenu = item->submenu;
  737. } else {
  738. printf("%s\n", item->output);
  739. return;
  740. }
  741. mapmenu(currmenu);
  742. currmenu->selected = currmenu->list;
  743. drawmenu(currmenu);
  744. break;
  745. case ButtonPress:
  746. menu = getmenu(currmenu, ev.xbutton.window);
  747. if (menu == NULL)
  748. return;
  749. break;
  750. case KeyPress:
  751. ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
  752. /* esc closes xmenu when current menu is the root menu */
  753. if (ksym == XK_Escape && currmenu->parent == NULL)
  754. return;
  755. /* Shift-Tab = ISO_Left_Tab */
  756. if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
  757. ksym = XK_ISO_Left_Tab;
  758. /* cycle through menu */
  759. item = NULL;
  760. if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
  761. item = itemcycle(currmenu, ITEMPREV);
  762. } else if (ksym == XK_Tab || ksym == XK_Down) {
  763. item = itemcycle(currmenu, ITEMNEXT);
  764. } else if ((ksym == XK_Return || ksym == XK_Right) &&
  765. currmenu->selected != NULL) {
  766. item = currmenu->selected;
  767. goto selectitem;
  768. } else if ((ksym == XK_Escape || ksym == XK_Left) &&
  769. currmenu->parent != NULL) {
  770. item = currmenu->parent->selected;
  771. currmenu = currmenu->parent;
  772. mapmenu(currmenu);
  773. } else
  774. break;
  775. currmenu->selected = item;
  776. drawmenu(currmenu);
  777. break;
  778. case LeaveNotify:
  779. previtem = NULL;
  780. currmenu->selected = NULL;
  781. drawmenu(currmenu);
  782. break;
  783. case ConfigureNotify:
  784. menu = getmenu(currmenu, ev.xconfigure.window);
  785. if (menu == NULL)
  786. break;
  787. menu->x = ev.xconfigure.x;
  788. menu->y = ev.xconfigure.y;
  789. break;
  790. case ClientMessage:
  791. if ((unsigned long) ev.xclient.data.l[0] != wmdelete)
  792. break;
  793. /* user closed window */
  794. menu = getmenu(currmenu, ev.xclient.window);
  795. if (menu->parent == NULL)
  796. return; /* closing the root menu closes the program */
  797. currmenu = menu->parent;
  798. mapmenu(currmenu);
  799. break;
  800. }
  801. }
  802. }
  803. /* recursivelly free pixmaps and destroy windows */
  804. static void
  805. cleanmenu(struct Menu *menu)
  806. {
  807. struct Item *item;
  808. struct Item *tmp;
  809. item = menu->list;
  810. while (item != NULL) {
  811. if (item->submenu != NULL)
  812. cleanmenu(item->submenu);
  813. tmp = item;
  814. if (tmp->label != tmp->output)
  815. free(tmp->label);
  816. free(tmp->output);
  817. if (tmp->file != NULL) {
  818. free(tmp->file);
  819. if (tmp->icon != NULL) {
  820. imlib_context_set_image(tmp->icon);
  821. imlib_free_image();
  822. }
  823. }
  824. item = item->next;
  825. free(tmp);
  826. }
  827. XFreePixmap(dpy, menu->pixmap);
  828. XftDrawDestroy(menu->draw);
  829. XDestroyWindow(dpy, menu->win);
  830. free(menu);
  831. }
  832. /* cleanup X and exit */
  833. static void
  834. cleanup(void)
  835. {
  836. XUngrabPointer(dpy, CurrentTime);
  837. XUngrabKeyboard(dpy, CurrentTime);
  838. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  839. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  840. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  841. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  842. XftColorFree(dpy, visual, colormap, &dc.separator);
  843. XftColorFree(dpy, visual, colormap, &dc.border);
  844. XFreeGC(dpy, dc.gc);
  845. XCloseDisplay(dpy);
  846. }
  847. /* show usage */
  848. static void
  849. usage(void)
  850. {
  851. (void)fprintf(stderr, "usage: xmenu [-w] [title]\n");
  852. exit(1);
  853. }