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.
 
 
 
 
 

573 lines
14 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. XSizeHints sizeh;
  294. struct Item *item;
  295. /* calculate items positions and menu height */
  296. for (item = menu->list; item != NULL; item = item->next) {
  297. item->y = menu->h;
  298. if (item->label == NULL) /* height for separator item */
  299. menu->h += geom.separator;
  300. else
  301. menu->h += geom.itemh;
  302. }
  303. /* calculate menu's x and y positions */
  304. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  305. if (screengeom.screenw - screengeom.cursx >= menu->w)
  306. menu->x = screengeom.cursx;
  307. else if (screengeom.cursx > menu->w)
  308. menu->x = screengeom.cursx - menu->w;
  309. if (screengeom.screenh - screengeom.cursy >= menu->h)
  310. menu->y = screengeom.cursy;
  311. else if (screengeom.screenh > menu->h)
  312. menu->y = screengeom.screenh - menu->h;
  313. } else { /* else, calculate in respect to parent menu */
  314. /* search for the item in parent menu that generates this menu */
  315. for (item = menu->parent->list; item->submenu != menu; item = item->next)
  316. ;
  317. if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w)
  318. menu->x = menu->parent->x + menu->parent->w;
  319. else if (menu->parent->x > menu->w)
  320. menu->x = menu->parent->x - menu->w;
  321. if (screengeom.screenh - (item->y + menu->parent->y) > menu->h)
  322. menu->y = item->y + menu->parent->y;
  323. else if (screengeom.screenh - menu->parent->y > menu->h)
  324. menu->y = menu->parent->y;
  325. else if (screengeom.screenh > menu->h)
  326. menu->y = screengeom.screenh - menu->h;
  327. }
  328. /* update menu geometry */
  329. changes.height = menu->h;
  330. changes.x = menu->x;
  331. changes.y = menu->y;
  332. XConfigureWindow(dpy, menu->win, CWHeight | CWX | CWY, &changes);
  333. /* set window manager size hints */
  334. sizeh.flags = PMaxSize | PMinSize;
  335. sizeh.min_width = sizeh.max_width = menu->w;
  336. sizeh.min_height = sizeh.max_height = menu->h;
  337. XSetWMNormalHints(dpy, menu->win, &sizeh);
  338. /* calculate positions of submenus */
  339. for (item = menu->list; item != NULL; item = item->next) {
  340. if (item->submenu != NULL)
  341. calcmenu(item->submenu);
  342. }
  343. }
  344. /* get menu and item of given window and position */
  345. static void
  346. getmenuitem(Window win, int y,
  347. struct Menu **menu_ret, struct Item **item_ret)
  348. {
  349. struct Menu *menu = NULL;
  350. struct Item *item = NULL;
  351. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  352. if (menu->win == win) {
  353. for (item = menu->list; item != NULL; item = item->next) {
  354. if (y >= item->y && y <= item->y + item->h) {
  355. goto done;
  356. }
  357. }
  358. }
  359. }
  360. done:
  361. *menu_ret = menu;
  362. *item_ret = item;
  363. }
  364. /* set currentmenu to menu, umap previous menus and map current menu and its parents */
  365. static void
  366. setcurrmenu(struct Menu *currmenu_new)
  367. {
  368. struct Menu *menu;
  369. if (currmenu_new == currmenu)
  370. return;
  371. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  372. XUnmapWindow(dpy, menu->win);
  373. }
  374. currmenu = currmenu_new;
  375. for (menu = currmenu; menu != NULL; menu = menu->parent)
  376. XMapWindow(dpy, menu->win);
  377. }
  378. /* draw items of the current menu and of its ancestors */
  379. static void
  380. drawmenu(void)
  381. {
  382. struct Menu *menu;
  383. struct Item *item;
  384. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  385. for (item = menu->list; item != NULL; item = item->next) {
  386. unsigned long *color;
  387. size_t labellen;
  388. int labelx, labely;
  389. /* determine item color */
  390. if (item->label == NULL)
  391. color = dc.decoration;
  392. else if (item == menu->selected)
  393. color = dc.pressed;
  394. else
  395. color = dc.unpressed;
  396. /* draw item box */
  397. XSetForeground(dpy, dc.gc, color[ColorBG]);
  398. XFillRectangle(dpy, menu->win, dc.gc, 0, item->y,
  399. geom.itemw, item->h);
  400. /* continue if item is a separator */
  401. if (item->label == NULL)
  402. continue;
  403. /* draw item label */
  404. labellen = strlen(item->label);
  405. labelx = 0 + dc.fonth;
  406. labely = item->y + dc.fonth + geom.itemb;
  407. XSetForeground(dpy, dc.gc, color[ColorFG]);
  408. XDrawString(dpy, menu->win, dc.gc, labelx, labely, item->label, labellen);
  409. /* draw triangle, if item contains a submenu */
  410. if (item->submenu != NULL) {
  411. int trianglex = geom.itemw - (geom.itemb + dc.fonth);
  412. int triangley = item->y + geom.itemb;
  413. XPoint triangle[] = {
  414. {trianglex, triangley},
  415. {trianglex + dc.fonth, triangley + dc.fonth/2},
  416. {trianglex, triangley + dc.fonth},
  417. {trianglex, triangley}
  418. };
  419. XFillPolygon(dpy, menu->win, dc.gc, triangle, LEN(triangle),
  420. Convex, CoordModeOrigin);
  421. }
  422. }
  423. }
  424. }
  425. /* run event loop */
  426. static void
  427. run(void)
  428. {
  429. struct Menu *menu;
  430. struct Item *item;
  431. struct Item *previtem = NULL;
  432. XEvent ev;
  433. setcurrmenu(rootmenu);
  434. while (!XNextEvent(dpy, &ev)) {
  435. switch(ev.type) {
  436. case Expose:
  437. drawmenu();
  438. break;
  439. case MotionNotify:
  440. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  441. if (menu != NULL && item != NULL) {
  442. if (previtem != item) {
  443. if (item->submenu != NULL)
  444. setcurrmenu(item->submenu);
  445. else
  446. setcurrmenu(menu);
  447. previtem = item;
  448. } else if (menu->selected != item)
  449. menu->selected = item;
  450. }
  451. drawmenu();
  452. break;
  453. case ButtonRelease:
  454. getmenuitem(ev.xbutton.window, ev.xbutton.y, &menu, &item);
  455. if (menu != NULL && item != NULL) {
  456. if (item->label == NULL)
  457. break; /* ignore separators */
  458. if (item->submenu != NULL) {
  459. setcurrmenu(item->submenu);
  460. } else {
  461. printf("%s\n", item->output);
  462. cleanupexit();
  463. }
  464. drawmenu();
  465. } else {
  466. cleanupexit();
  467. }
  468. break;
  469. }
  470. }
  471. }
  472. /* cleanup and exit */
  473. static void
  474. cleanupexit(void)
  475. {
  476. XCloseDisplay(dpy);
  477. exit(0);
  478. }
  479. /* show usage */
  480. static void
  481. usage(void)
  482. {
  483. (void)fprintf(stderr, "usage: xmenu [-w] menuname\n");
  484. exit(1);
  485. }