A mirror of phillbush's xmenu.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

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