A mirror of phillbush's xmenu.
 
 
 
 
 

560 行
13 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. /* macros */
  9. #define LEN(x) (sizeof (x) / sizeof (x[0]))
  10. /* color enum */
  11. enum {ColorFG, ColorBG, ColorLast};
  12. /* draw context structure */
  13. struct DC {
  14. unsigned long unpressed[ColorLast];
  15. unsigned long pressed[ColorLast];
  16. unsigned long decoration[ColorLast];
  17. Drawable d;
  18. GC gc;
  19. XFontStruct *font;
  20. int fonth;
  21. };
  22. /* menu geometry structure */
  23. struct Geometry {
  24. int itemb; /* item border */
  25. int itemw; /* item width */
  26. int itemh; /* item height */
  27. int border; /* window border width */
  28. int separator; /* menu separator width */
  29. };
  30. /* screen geometry structure */
  31. struct ScreenGeometry {
  32. int cursx, cursy; /* cursor position */
  33. int screenw, screenh; /* screen width and height */
  34. };
  35. /* menu item structure */
  36. struct Item {
  37. char *label;
  38. char *output;
  39. int y; /* only y is necessary, item's x is always 0 relative to the menu*/
  40. struct Item *next;
  41. struct Menu *submenu;
  42. };
  43. /* menu structure */
  44. struct Menu {
  45. struct Menu *parent;
  46. struct Item *list;
  47. struct Item *selected;
  48. int x, y, w, h;
  49. unsigned level;
  50. Window win;
  51. };
  52. /* function declarations */
  53. static unsigned long getcolor(const char *s);
  54. static void setupdc(void);
  55. static void setupgeom(void);
  56. static void setupgrab(void);
  57. static struct Item *allocitem(const char *label, const char *output);
  58. static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
  59. static void getmenuitem(Window win, int y,
  60. struct Menu **menu_ret, struct Item **item_ret);
  61. static void drawmenu(void);
  62. static void calcscreengeom(void);
  63. static void calcmenu(struct Menu *menu);
  64. static void setcurrmenu(struct Menu *currmenu_new);
  65. static void parsestdin(void);
  66. static void run(void);
  67. static void cleanupexit(void);
  68. static void usage(void);
  69. /* X variables */
  70. static Colormap colormap;
  71. static Display *dpy;
  72. static Window rootwin;
  73. static int screen;
  74. static struct DC dc;
  75. /* menu variables */
  76. static struct Menu *rootmenu = NULL;
  77. static struct Menu *currmenu = NULL;
  78. /* geometry variables */
  79. static struct Geometry geom;
  80. static struct ScreenGeometry screengeom;
  81. /* flag variables */
  82. static Bool override_redirect = True;
  83. #include "config.h"
  84. int
  85. main(int argc, char *argv[])
  86. {
  87. int ch;
  88. while ((ch = getopt(argc, argv, "w")) != -1) {
  89. switch (ch) {
  90. case 'w':
  91. override_redirect = False;
  92. break;
  93. default:
  94. usage();
  95. break;
  96. }
  97. }
  98. argc -= optind;
  99. argv += optind;
  100. /* open connection to server and set X variables */
  101. if ((dpy = XOpenDisplay(NULL)) == NULL)
  102. errx(1, "cannot open display");
  103. screen = DefaultScreen(dpy);
  104. rootwin = RootWindow(dpy, screen);
  105. colormap = DefaultColormap(dpy, screen);
  106. /* setup */
  107. setupdc();
  108. setupgeom();
  109. setupgrab();
  110. /* generate menus and recalculate them */
  111. parsestdin();
  112. if (rootmenu == NULL)
  113. errx(1, "no menu generated");
  114. calcscreengeom();
  115. calcmenu(rootmenu);
  116. /* run event loop */
  117. run();
  118. return 1; /* UNREACHABLE */
  119. }
  120. /* get color from color string */
  121. static unsigned long
  122. getcolor(const char *s)
  123. {
  124. XColor color;
  125. if(!XAllocNamedColor(dpy, colormap, s, &color, &color))
  126. errx(1, "cannot allocate color: %s", s);
  127. return color.pixel;
  128. }
  129. /* init draw context */
  130. static void
  131. setupdc(void)
  132. {
  133. /* get color pixels */
  134. dc.unpressed[ColorBG] = getcolor(UNPRESSEDBG);
  135. dc.unpressed[ColorFG] = getcolor(UNPRESSEDFG);
  136. dc.pressed[ColorBG] = getcolor(PRESSEDBG);
  137. dc.pressed[ColorFG] = getcolor(PRESSEDFG);
  138. dc.decoration[ColorBG] = getcolor(DECORATIONBG);
  139. dc.decoration[ColorFG] = getcolor(DECORATIONFG);
  140. /* try to get font */
  141. if ((dc.font = XLoadQueryFont(dpy, FONT)) == NULL)
  142. errx(1, "cannot load font");
  143. dc.fonth = dc.font->ascent + dc.font->descent;
  144. /* create GC and set its font */
  145. dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
  146. XSetFont(dpy, dc.gc, dc.font->fid);
  147. }
  148. /* init menu geometry values */
  149. static void
  150. setupgeom(void)
  151. {
  152. geom.itemb = ITEMB;
  153. geom.itemh = dc.fonth + ITEMB * 2;
  154. geom.itemw = ITEMW;
  155. geom.border = BORDER;
  156. geom.separator = SEPARATOR;
  157. }
  158. /* grab pointer */
  159. static void
  160. setupgrab(void)
  161. {
  162. XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask,
  163. GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
  164. }
  165. /* allocate an item */
  166. static struct Item *
  167. allocitem(const char *label, const char *output)
  168. {
  169. struct Item *item;
  170. if ((item = malloc(sizeof *item)) == NULL)
  171. err(1, "malloc");
  172. if ((item->label = strdup(label)) == NULL)
  173. err(1, "strdup");
  174. if ((item->output = strdup(output)) == NULL)
  175. err(1, "strdup");
  176. item->y = 0;
  177. item->next = NULL;
  178. item->submenu = NULL;
  179. return item;
  180. }
  181. /* allocate a menu */
  182. static struct Menu *
  183. allocmenu(struct Menu *parent, struct Item *list, unsigned level)
  184. {
  185. XSetWindowAttributes swa;
  186. struct Menu *menu;
  187. if ((menu = malloc(sizeof *menu)) == NULL)
  188. err(1, "malloc");
  189. menu->parent = parent;
  190. menu->list = list;
  191. menu->selected = NULL;
  192. menu->w = geom.itemw;
  193. menu->h = 0; /* calculated by calcmenu() */
  194. menu->x = 0; /* calculated by calcmenu() */
  195. menu->y = 0; /* calculated by calcmenu() */
  196. menu->level = level;
  197. swa.override_redirect = override_redirect;
  198. swa.background_pixel = dc.decoration[ColorBG];
  199. swa.border_pixel = dc.decoration[ColorFG];
  200. swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
  201. | PointerMotionMask;
  202. menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
  203. CopyFromParent, CopyFromParent, CopyFromParent,
  204. CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
  205. &swa);
  206. return menu;
  207. }
  208. /* create menus and items from the stdin */
  209. static void
  210. parsestdin(void)
  211. {
  212. char *s, buf[BUFSIZ];
  213. char *label, *output;
  214. unsigned level = 0;
  215. unsigned i;
  216. struct Item *curritem = NULL; /* item currently being read */
  217. struct Menu *prevmenu = NULL; /* menu the previous item was added to */
  218. struct Item *item; /* dummy item for for loops */
  219. struct Menu *menu; /* dummy menu for for loops */
  220. size_t count = 0; /* number of items in the current menu */
  221. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  222. level = 0;
  223. s = buf;
  224. while (*s == '\t') {
  225. level++;
  226. s++;
  227. }
  228. label = output = s;
  229. while (*s != '\0' && *s != '\t' && *s != '\n')
  230. s++;
  231. while (*s == '\t')
  232. *s++ = '\0';
  233. if (*s != '\0' && *s != '\n')
  234. output = s;
  235. while (*s != '\0' && *s != '\n')
  236. s++;
  237. if (*s == '\n')
  238. *s = '\0';
  239. curritem = allocitem(label, output);
  240. if (prevmenu == NULL) { /* there is no menu yet */
  241. menu = allocmenu(NULL, curritem, level);
  242. rootmenu = menu;
  243. prevmenu = menu;
  244. count = 1;
  245. } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
  246. for (menu = prevmenu, i = level;
  247. menu != NULL && i < prevmenu->level;
  248. menu = menu->parent, i++)
  249. ;
  250. if (menu == NULL)
  251. errx(1, "reached NULL menu");
  252. for (item = menu->list; item->next != NULL; item = item->next)
  253. ;
  254. item->next = curritem;
  255. prevmenu = menu;
  256. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  257. for (item = prevmenu->list; item->next != NULL; item = item->next)
  258. ;
  259. item->next = curritem;
  260. } else if (level > prevmenu->level) { /* item begins a new menu */
  261. menu = allocmenu(prevmenu, curritem, level);
  262. for (item = prevmenu->list; item->next != NULL; item = item->next)
  263. ;
  264. item->submenu = menu;
  265. prevmenu = menu;
  266. }
  267. count++;
  268. }
  269. }
  270. /* calculate screen geometry */
  271. static void
  272. calcscreengeom(void)
  273. {
  274. Window w1, w2; /* unused variables */
  275. int a, b; /* unused variables */
  276. unsigned mask; /* unused variable */
  277. XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
  278. screengeom.screenw = DisplayWidth(dpy, screen);
  279. screengeom.screenh = DisplayHeight(dpy, screen);
  280. }
  281. /* recursivelly calculate height and position of the menus */
  282. static void
  283. calcmenu(struct Menu *menu)
  284. {
  285. XWindowChanges changes;
  286. struct Item *item;
  287. /* calculate items positions and menu height */
  288. for (item = menu->list; item != NULL; item = item->next) {
  289. item->y = menu->h;
  290. if (*item->label == '\0') /* height for separator item */
  291. menu->h += geom.separator;
  292. else
  293. menu->h += geom.itemh;
  294. }
  295. /* calculate menu's x and y positions */
  296. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  297. if (screengeom.screenw - screengeom.cursx >= menu->w)
  298. menu->x = screengeom.cursx;
  299. else if (screengeom.cursx > menu->w)
  300. menu->x = screengeom.cursx - menu->w;
  301. if (screengeom.screenh - screengeom.cursy >= menu->h)
  302. menu->y = screengeom.cursy;
  303. else if (screengeom.screenh > menu->h)
  304. menu->y = screengeom.screenh - menu->h;
  305. } else { /* else, calculate in respect to parent menu */
  306. /* search for the item in parent menu that generates this menu */
  307. for (item = menu->parent->list; item->submenu != menu; item = item->next)
  308. ;
  309. if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w)
  310. menu->x = menu->parent->x + menu->parent->w;
  311. else if (menu->parent->x > menu->w)
  312. menu->x = menu->parent->x - menu->w;
  313. if (screengeom.screenh - (item->y + menu->parent->y) > menu->h)
  314. menu->y = item->y + menu->parent->y;
  315. else if (screengeom.screenh - menu->parent->y > menu->h)
  316. menu->y = menu->parent->y;
  317. else if (screengeom.screenh > menu->h)
  318. menu->y = screengeom.screenh - menu->h;
  319. }
  320. /* update menu geometry */
  321. changes.height = menu->h;
  322. changes.x = menu->x;
  323. changes.y = menu->y;
  324. XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes);
  325. /* calculate positions of submenus */
  326. for (item = menu->list; item != NULL; item = item->next) {
  327. if (item->submenu != NULL)
  328. calcmenu(item->submenu);
  329. }
  330. }
  331. /* get menu and item of given window and position */
  332. static void
  333. getmenuitem(Window win, int y,
  334. struct Menu **menu_ret, struct Item **item_ret)
  335. {
  336. struct Menu *menu = NULL;
  337. struct Item *item = NULL;
  338. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  339. if (menu->win == win) {
  340. for (item = menu->list; item != NULL; item = item->next) {
  341. if (y >= item->y && y <= item->y + geom.itemh) {
  342. goto done;
  343. }
  344. }
  345. }
  346. }
  347. done:
  348. *menu_ret = menu;
  349. *item_ret = item;
  350. }
  351. /* set currentmenu to menu, umap previous menus and map current menu and its parents */
  352. static void
  353. setcurrmenu(struct Menu *currmenu_new)
  354. {
  355. struct Menu *menu;
  356. if (currmenu_new == currmenu)
  357. return;
  358. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  359. XUnmapWindow(dpy, menu->win);
  360. }
  361. currmenu = currmenu_new;
  362. for (menu = currmenu; menu != NULL; menu = menu->parent)
  363. XMapWindow(dpy, menu->win);
  364. }
  365. /* draw items of the current menu and of its ancestors */
  366. static void
  367. drawmenu(void)
  368. {
  369. struct Menu *menu;
  370. struct Item *item;
  371. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  372. size_t nitems; /* number of items before current item */
  373. nitems = 0;
  374. for (item = menu->list; item != NULL; item = item->next) {
  375. unsigned long *color;
  376. size_t labellen;
  377. int labelx, labely;
  378. int y;
  379. /* determine item color */
  380. if (item == menu->selected)
  381. color = dc.pressed;
  382. else
  383. color = dc.unpressed;
  384. /* calculate item's y position */
  385. y = nitems * geom.itemh;
  386. /* draw item box */
  387. XSetForeground(dpy, dc.gc, color[ColorBG]);
  388. XFillRectangle(dpy, menu->win, dc.gc, 0, y,
  389. geom.itemw, geom.itemh);
  390. /* draw item label */
  391. labellen = strlen(item->label);
  392. labelx = 0 + dc.fonth;
  393. labely = y + dc.fonth + geom.itemb;
  394. XSetForeground(dpy, dc.gc, color[ColorFG]);
  395. XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen);
  396. /* draw triangle, if item contains a submenu */
  397. if (item->submenu != NULL) {
  398. int trianglex = geom.itemw - (geom.itemb + dc.fonth);
  399. int triangley = y + geom.itemb;
  400. XPoint triangle[] = {
  401. {trianglex, triangley},
  402. {trianglex + dc.fonth, triangley + dc.fonth/2},
  403. {trianglex, triangley + dc.fonth},
  404. {trianglex, triangley}
  405. };
  406. XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle),
  407. Convex, CoordModeOrigin);
  408. }
  409. nitems++;
  410. }
  411. }
  412. }
  413. /* run event loop */
  414. static void
  415. run(void)
  416. {
  417. struct Menu *menu;
  418. struct Item *item;
  419. struct Item *previtem = NULL;
  420. XEvent ev;
  421. setcurrmenu(rootmenu);
  422. while (!XNextEvent(dpy, &ev)) {
  423. switch(ev.type) {
  424. case Expose:
  425. drawmenu();
  426. break;
  427. case MotionNotify:
  428. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  429. if (menu != NULL && item != NULL) {
  430. if (previtem != item) {
  431. if (item->submenu != NULL)
  432. setcurrmenu(item->submenu);
  433. else
  434. setcurrmenu(menu);
  435. previtem = item;
  436. } else if (menu->selected != item)
  437. menu->selected = item;
  438. }
  439. drawmenu();
  440. break;
  441. case ButtonRelease:
  442. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  443. if (menu != NULL && item != NULL) {
  444. if (item->submenu != NULL) {
  445. setcurrmenu(item->submenu);
  446. } else {
  447. printf("%s\n", item->output);
  448. cleanupexit();
  449. }
  450. drawmenu();
  451. } else {
  452. cleanupexit();
  453. }
  454. break;
  455. }
  456. }
  457. }
  458. /* cleanup and exit */
  459. static void
  460. cleanupexit(void)
  461. {
  462. XCloseDisplay(dpy);
  463. exit(0);
  464. }
  465. /* show usage */
  466. static void
  467. usage(void)
  468. {
  469. (void)fprintf(stderr, "usage: xmenu [-w] menuname\n");
  470. exit(1);
  471. }