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.
 
 
 
 
 

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