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.

xmenu.c 14 KiB

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