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.
 
 
 
 
 

657 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 cleanup(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. cleanup();
  128. return 0;
  129. }
  130. /* get color from color string */
  131. static unsigned long
  132. getcolor(const char *s)
  133. {
  134. XColor color;
  135. if(!XAllocNamedColor(dpy, colormap, s, &color, &color))
  136. errx(1, "cannot allocate color: %s", s);
  137. return color.pixel;
  138. }
  139. /* init draw context */
  140. static void
  141. setupdc(void)
  142. {
  143. /* get color pixels */
  144. dc.unpressed[ColorBG] = getcolor(UNPRESSEDBG);
  145. dc.unpressed[ColorFG] = getcolor(UNPRESSEDFG);
  146. dc.pressed[ColorBG] = getcolor(PRESSEDBG);
  147. dc.pressed[ColorFG] = getcolor(PRESSEDFG);
  148. dc.decoration[ColorBG] = getcolor(DECORATIONBG);
  149. dc.decoration[ColorFG] = getcolor(DECORATIONFG);
  150. /* try to get font */
  151. if ((dc.font = XLoadQueryFont(dpy, FONT)) == NULL)
  152. errx(1, "cannot load font");
  153. dc.fonth = dc.font->ascent + dc.font->descent;
  154. /* create GC and set its font */
  155. dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
  156. XSetFont(dpy, dc.gc, dc.font->fid);
  157. }
  158. /* init menu geometry values */
  159. static void
  160. setupgeom(void)
  161. {
  162. geom.itemb = ITEMB;
  163. geom.itemh = dc.fonth + ITEMB * 2;
  164. geom.itemw = ITEMW;
  165. geom.border = BORDER;
  166. geom.separator = SEPARATOR;
  167. }
  168. /* grab pointer */
  169. static void
  170. setupgrab(void)
  171. {
  172. XGrabPointer(dpy, rootwin, True, ButtonPressMask | ButtonReleaseMask,
  173. GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
  174. }
  175. /* allocate an item */
  176. static struct Item *
  177. allocitem(const char *label, const char *output)
  178. {
  179. struct Item *item;
  180. if ((item = malloc(sizeof *item)) == NULL)
  181. err(1, "malloc");
  182. if (*label == '\0') {
  183. item->label = NULL;
  184. item->output = NULL;
  185. } else {
  186. if ((item->label = strdup(label)) == NULL)
  187. err(1, "strdup");
  188. if ((item->output = strdup(output)) == NULL)
  189. err(1, "strdup");
  190. }
  191. item->y = 0;
  192. item->h = item->label ? geom.itemh : geom.separator;
  193. if (item->label == NULL)
  194. item->labellen = 0;
  195. else
  196. item->labellen = strlen(item->label);
  197. item->next = NULL;
  198. item->submenu = NULL;
  199. return item;
  200. }
  201. /* allocate a menu */
  202. static struct Menu *
  203. allocmenu(struct Menu *parent, struct Item *list, unsigned level)
  204. {
  205. XSetWindowAttributes swa;
  206. struct Menu *menu;
  207. if ((menu = malloc(sizeof *menu)) == NULL)
  208. err(1, "malloc");
  209. menu->parent = parent;
  210. menu->list = list;
  211. menu->caller = NULL;
  212. menu->selected = NULL;
  213. menu->w = geom.itemw;
  214. menu->h = 0; /* calculated by calcmenu() */
  215. menu->x = 0; /* calculated by calcmenu() */
  216. menu->y = 0; /* calculated by calcmenu() */
  217. menu->level = level;
  218. swa.override_redirect = override_redirect;
  219. swa.background_pixel = dc.decoration[ColorBG];
  220. swa.border_pixel = dc.decoration[ColorFG];
  221. swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
  222. | PointerMotionMask | LeaveWindowMask;
  223. menu->win = XCreateWindow(dpy, rootwin, 0, 0, geom.itemw, geom.itemh, geom.border,
  224. CopyFromParent, CopyFromParent, CopyFromParent,
  225. CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
  226. &swa);
  227. return menu;
  228. }
  229. /* create menus and items from the stdin */
  230. static void
  231. parsestdin(void)
  232. {
  233. char *s, buf[BUFSIZ];
  234. char *label, *output;
  235. unsigned level = 0;
  236. unsigned i;
  237. struct Item *curritem = NULL; /* item currently being read */
  238. struct Menu *prevmenu = NULL; /* menu the previous item was added to */
  239. struct Item *item; /* dummy item for for loops */
  240. struct Menu *menu; /* dummy menu for for loops */
  241. size_t count = 0; /* number of items in the current menu */
  242. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  243. level = 0;
  244. s = buf;
  245. while (*s == '\t') {
  246. level++;
  247. s++;
  248. }
  249. label = output = s;
  250. while (*s != '\0' && *s != '\t' && *s != '\n')
  251. s++;
  252. while (*s == '\t')
  253. *s++ = '\0';
  254. if (*s != '\0' && *s != '\n')
  255. output = s;
  256. while (*s != '\0' && *s != '\n')
  257. s++;
  258. if (*s == '\n')
  259. *s = '\0';
  260. curritem = allocitem(label, output);
  261. if (prevmenu == NULL) { /* there is no menu yet */
  262. menu = allocmenu(NULL, curritem, level);
  263. rootmenu = menu;
  264. prevmenu = menu;
  265. count = 1;
  266. } else if (level < prevmenu->level) { /* item is continuation of a parent menu*/
  267. for (menu = prevmenu, i = level;
  268. menu != NULL && i < prevmenu->level;
  269. menu = menu->parent, i++)
  270. ;
  271. if (menu == NULL)
  272. errx(1, "reached NULL menu");
  273. for (item = menu->list; item->next != NULL; item = item->next)
  274. ;
  275. item->next = curritem;
  276. prevmenu = menu;
  277. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  278. for (item = prevmenu->list; item->next != NULL; item = item->next)
  279. ;
  280. item->next = curritem;
  281. } else if (level > prevmenu->level) { /* item begins a new menu */
  282. menu = allocmenu(prevmenu, curritem, level);
  283. for (item = prevmenu->list; item->next != NULL; item = item->next)
  284. ;
  285. item->submenu = menu;
  286. menu->caller = item;
  287. prevmenu = menu;
  288. }
  289. count++;
  290. }
  291. }
  292. /* calculate screen geometry */
  293. static void
  294. calcscreengeom(void)
  295. {
  296. Window w1, w2; /* unused variables */
  297. int a, b; /* unused variables */
  298. unsigned mask; /* unused variable */
  299. XQueryPointer(dpy, rootwin, &w1, &w2, &screengeom.cursx, &screengeom.cursy, &a, &b, &mask);
  300. screengeom.screenw = DisplayWidth(dpy, screen);
  301. screengeom.screenh = DisplayHeight(dpy, screen);
  302. }
  303. /* recursivelly calculate height and position of the menus */
  304. static void
  305. calcmenu(struct Menu *menu)
  306. {
  307. XWindowChanges changes;
  308. XSizeHints sizeh;
  309. struct Item *item;
  310. int labelwidth;
  311. /* calculate items positions and menu width and height */
  312. menu->w = geom.itemw;
  313. for (item = menu->list; item != NULL; item = item->next) {
  314. item->y = menu->h;
  315. if (item->label == NULL) /* height for separator item */
  316. menu->h += geom.separator;
  317. else
  318. menu->h += geom.itemh;
  319. labelwidth = XTextWidth(dc.font, item->label, item->labellen) + dc.fonth * 2;
  320. menu->w = MAX(menu->w, labelwidth);
  321. }
  322. /* calculate menu's x and y positions */
  323. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  324. if (screengeom.screenw - screengeom.cursx >= menu->w)
  325. menu->x = screengeom.cursx;
  326. else if (screengeom.cursx > menu->w)
  327. menu->x = screengeom.cursx - menu->w;
  328. if (screengeom.screenh - screengeom.cursy >= menu->h)
  329. menu->y = screengeom.cursy;
  330. else if (screengeom.screenh > menu->h)
  331. menu->y = screengeom.screenh - menu->h;
  332. } else { /* else, calculate in respect to parent menu */
  333. /* search for the item in parent menu that generates this menu */
  334. for (item = menu->parent->list; item->submenu != menu; item = item->next)
  335. ;
  336. if (screengeom.screenw - (menu->parent->x + menu->parent->w) >= menu->w)
  337. menu->x = menu->parent->x + menu->parent->w;
  338. else if (menu->parent->x > menu->w)
  339. menu->x = menu->parent->x - menu->w;
  340. if (screengeom.screenh - (item->y + menu->parent->y) > menu->h)
  341. menu->y = item->y + menu->parent->y;
  342. else if (screengeom.screenh - menu->parent->y > menu->h)
  343. menu->y = menu->parent->y;
  344. else if (screengeom.screenh > menu->h)
  345. menu->y = screengeom.screenh - menu->h;
  346. }
  347. /* update menu geometry */
  348. changes.height = menu->h;
  349. changes.width = menu->w;
  350. changes.x = menu->x;
  351. changes.y = menu->y;
  352. XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
  353. /* set window manager size hints */
  354. sizeh.flags = PMaxSize | PMinSize;
  355. sizeh.min_width = sizeh.max_width = menu->w;
  356. sizeh.min_height = sizeh.max_height = menu->h;
  357. XSetWMNormalHints(dpy, menu->win, &sizeh);
  358. /* create pixmap */
  359. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  360. DefaultDepth(dpy, screen));
  361. /* calculate positions of submenus */
  362. for (item = menu->list; item != NULL; item = item->next) {
  363. if (item->submenu != NULL)
  364. calcmenu(item->submenu);
  365. }
  366. }
  367. /* get menu and item of given window and position */
  368. static void
  369. getmenuitem(Window win, int y,
  370. struct Menu **menu_ret, struct Item **item_ret)
  371. {
  372. struct Menu *menu = NULL;
  373. struct Item *item = NULL;
  374. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  375. if (menu->win == win) {
  376. for (item = menu->list; item != NULL; item = item->next) {
  377. if (y >= item->y && y <= item->y + item->h) {
  378. goto done;
  379. }
  380. }
  381. }
  382. }
  383. done:
  384. *menu_ret = menu;
  385. *item_ret = item;
  386. }
  387. /* set currentmenu to menu, umap previous menus and map current menu and its parents */
  388. static void
  389. setcurrmenu(struct Menu *currmenu_new)
  390. {
  391. struct Menu *menu, *menu_;
  392. struct Item *item;
  393. struct Menu *lcamenu; /* lowest common ancestor menu */
  394. unsigned minlevel; /* level of the closest to root menu */
  395. unsigned maxlevel; /* level of the closest to root menu */
  396. if (currmenu_new == currmenu)
  397. return;
  398. /* find lowest common ancestor menu */
  399. lcamenu = rootmenu;
  400. if (currmenu != NULL) {
  401. minlevel = MIN(currmenu_new->level, currmenu->level);
  402. maxlevel = MAX(currmenu_new->level, currmenu->level);
  403. if (currmenu_new->level == maxlevel) {
  404. menu = currmenu_new;
  405. menu_ = currmenu;
  406. } else {
  407. menu = currmenu;
  408. menu_ = currmenu_new;
  409. }
  410. while (menu->level > minlevel)
  411. menu = menu->parent;
  412. while (menu != menu_) {
  413. menu = menu->parent;
  414. menu_ = menu_->parent;
  415. }
  416. lcamenu = menu;
  417. }
  418. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  419. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  420. XUnmapWindow(dpy, menu->win);
  421. }
  422. currmenu = currmenu_new;
  423. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  424. item = NULL;
  425. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  426. XMapWindow(dpy, menu->win);
  427. if (item != NULL)
  428. menu->selected = item;
  429. item = menu->caller;
  430. }
  431. }
  432. /* draw items of the current menu and of its ancestors */
  433. static void
  434. drawmenu(void)
  435. {
  436. struct Menu *menu;
  437. struct Item *item;
  438. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  439. for (item = menu->list; item != NULL; item = item->next) {
  440. unsigned long *color;
  441. int labelx, labely;
  442. /* determine item color */
  443. if (item->label == NULL)
  444. color = dc.decoration;
  445. else if (item == menu->selected)
  446. color = dc.pressed;
  447. else
  448. color = dc.unpressed;
  449. /* draw item box */
  450. XSetForeground(dpy, dc.gc, color[ColorBG]);
  451. XDrawRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  452. menu->w, item->h);
  453. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  454. menu->w, item->h);
  455. /* continue if item is a separator */
  456. if (item->label == NULL)
  457. continue;
  458. /* draw item label */
  459. labelx = 0 + dc.fonth;
  460. labely = item->y + dc.fonth + geom.itemb;
  461. XSetForeground(dpy, dc.gc, color[ColorFG]);
  462. XDrawString(dpy, menu->pixmap, dc.gc, labelx, labely,
  463. item->label, item->labellen);
  464. /* draw triangle, if item contains a submenu */
  465. if (item->submenu != NULL) {
  466. int trianglex = menu->w - dc.fonth + geom.itemb - 1;
  467. int triangley = item->y + (3 * item->h)/8 -1;
  468. XPoint triangle[] = {
  469. {trianglex, triangley},
  470. {trianglex + item->h/8 + 1, item->y + item->h/2},
  471. {trianglex, triangley + item->h/4 + 2},
  472. {trianglex, triangley}
  473. };
  474. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  475. Convex, CoordModeOrigin);
  476. }
  477. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, item->y,
  478. menu->w, item->h, 0, item->y);
  479. }
  480. }
  481. }
  482. /* run event loop */
  483. static void
  484. run(void)
  485. {
  486. struct Menu *menu;
  487. struct Item *item;
  488. struct Item *previtem = NULL;
  489. XEvent ev;
  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. return;
  521. }
  522. drawmenu();
  523. } else {
  524. return;
  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. cleanup(void)
  548. {
  549. freewindow(rootmenu);
  550. XFreeFont(dpy, dc.font);
  551. XFreeGC(dpy, dc.gc);
  552. XCloseDisplay(dpy);
  553. }
  554. /* show usage */
  555. static void
  556. usage(void)
  557. {
  558. (void)fprintf(stderr, "usage: xmenu [-w]\n");
  559. exit(1);
  560. }