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.
 
 
 
 
 

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