A mirror of phillbush's xmenu.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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