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.
 
 
 
 
 

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