A mirror of phillbush's xmenu.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

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