A mirror of phillbush's xmenu.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/Xresource.h>
  11. #include <X11/XKBlib.h>
  12. #include <X11/Xft/Xft.h>
  13. #include <Imlib2.h>
  14. #include "xmenu.h"
  15. /*
  16. * Function declarations
  17. */
  18. /* initializers, and their helper routines */
  19. static void ealloccolor(const char *s, XftColor *color);
  20. static void initresources(void);
  21. static void initdc(void);
  22. static void initconfig(void);
  23. static void initatoms(void);
  24. /* structure builders, and their helper routines */
  25. static struct Item *allocitem(const char *label, const char *output, char *file);
  26. static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
  27. static struct Menu *buildmenutree(unsigned level, const char *label, const char *output, char *file);
  28. static struct Menu *parsestdin(void);
  29. /* image loader */
  30. static Imlib_Image loadicon(const char *file);
  31. /* structure setters, and their helper routines */
  32. static void setupitems(struct Menu *menu);
  33. static void setupmenupos(struct Menu *menu);
  34. static void setupmenu(struct Menu *menu, XClassHint *classh);
  35. /* grabbers */
  36. static void grabpointer(void);
  37. static void grabkeyboard(void);
  38. /* window drawers and mappers */
  39. static void mapmenu(struct Menu *currmenu);
  40. static void drawseparator(struct Menu *menu, struct Item *item);
  41. static void drawitem(struct Menu *menu, struct Item *item, XftColor *color);
  42. static void drawmenu(struct Menu *currmenu);
  43. /* main event loop, and its helper routines */
  44. static struct Menu *getmenu(struct Menu *currmenu, Window win);
  45. static struct Item *getitem(struct Menu *menu, int y);
  46. static struct Item *itemcycle(struct Menu *currmenu, int direction);
  47. static void run(struct Menu *currmenu);
  48. /* cleaners */
  49. static void cleanmenu(struct Menu *menu);
  50. static void cleanup(void);
  51. /* show usage */
  52. static void usage(void);
  53. /*
  54. * Variable declarations
  55. */
  56. /* X stuff */
  57. static Display *dpy;
  58. static int screen;
  59. static Visual *visual;
  60. static Window rootwin;
  61. static Colormap colormap;
  62. static struct DC dc;
  63. static Atom utf8string;
  64. static Atom wmdelete;
  65. static Atom netatom[NetLast];
  66. /* flags */
  67. static int wflag = 0; /* whether to let the window manager control XMenu */
  68. static int iflag = 0; /* whether to disable icons */
  69. /* include config variable */
  70. #include "config.h"
  71. /*
  72. * Function implementations
  73. */
  74. /* xmenu: generate menu from stdin and print selected entry to stdout */
  75. int
  76. main(int argc, char *argv[])
  77. {
  78. struct Menu *rootmenu;
  79. XClassHint classh;
  80. int ch;
  81. while ((ch = getopt(argc, argv, "iw")) != -1) {
  82. switch (ch) {
  83. case 'i':
  84. iflag = 1;
  85. break;
  86. case 'w':
  87. wflag = 1;
  88. break;
  89. default:
  90. usage();
  91. break;
  92. }
  93. }
  94. argc -= optind;
  95. argv += optind;
  96. if (argc > 1)
  97. usage();
  98. /* open connection to server and set X variables */
  99. if ((dpy = XOpenDisplay(NULL)) == NULL)
  100. errx(1, "cannot open display");
  101. screen = DefaultScreen(dpy);
  102. visual = DefaultVisual(dpy, screen);
  103. rootwin = RootWindow(dpy, screen);
  104. colormap = DefaultColormap(dpy, screen);
  105. /* imlib2 stuff */
  106. if (!iflag) {
  107. imlib_set_cache_size(2048 * 1024);
  108. imlib_context_set_dither(1);
  109. imlib_context_set_display(dpy);
  110. imlib_context_set_visual(visual);
  111. imlib_context_set_colormap(colormap);
  112. }
  113. /* initializers */
  114. initresources();
  115. initdc();
  116. initconfig();
  117. initatoms();
  118. /* set window class */
  119. classh.res_class = PROGNAME;
  120. if (argc == 1)
  121. classh.res_name = *argv;
  122. else
  123. classh.res_name = PROGNAME;
  124. /* generate menus and set them up */
  125. rootmenu = parsestdin();
  126. if (rootmenu == NULL)
  127. errx(1, "no menu generated");
  128. setupmenu(rootmenu, &classh);
  129. /* grab mouse and keyboard */
  130. if (!wflag) {
  131. grabpointer();
  132. grabkeyboard();
  133. }
  134. /* run event loop */
  135. run(rootmenu);
  136. /* freeing stuff */
  137. cleanmenu(rootmenu);
  138. cleanup();
  139. return 0;
  140. }
  141. /* get color from color string */
  142. static void
  143. ealloccolor(const char *s, XftColor *color)
  144. {
  145. if(!XftColorAllocName(dpy, visual, colormap, s, color))
  146. errx(1, "cannot allocate color: %s", s);
  147. }
  148. /* read xrdb for configuration options */
  149. static void
  150. initresources(void)
  151. {
  152. char *xrm;
  153. long n;
  154. char *type;
  155. XrmDatabase xdb;
  156. XrmValue xval;
  157. XrmInitialize();
  158. if ((xrm = XResourceManagerString(dpy)) == NULL)
  159. return;
  160. xdb = XrmGetStringDatabase(xrm);
  161. if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
  162. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  163. config.border_pixels = n;
  164. if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
  165. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  166. config.separator_pixels = n;
  167. if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True)
  168. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  169. config.height_pixels = n;
  170. if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
  171. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  172. config.width_pixels = n;
  173. if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True)
  174. if ((n = strtol(xval.addr, NULL, 10)) > 0)
  175. config.gap_pixels = n;
  176. if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
  177. config.background_color = strdup(xval.addr);
  178. if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
  179. config.foreground_color = strdup(xval.addr);
  180. if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
  181. config.selbackground_color = strdup(xval.addr);
  182. if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
  183. config.selforeground_color = strdup(xval.addr);
  184. if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
  185. config.separator_color = strdup(xval.addr);
  186. if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
  187. config.border_color = strdup(xval.addr);
  188. if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
  189. config.font = strdup(xval.addr);
  190. XrmDestroyDatabase(xdb);
  191. }
  192. /* init draw context */
  193. static void
  194. initdc(void)
  195. {
  196. /* get color pixels */
  197. ealloccolor(config.background_color, &dc.normal[ColorBG]);
  198. ealloccolor(config.foreground_color, &dc.normal[ColorFG]);
  199. ealloccolor(config.selbackground_color, &dc.selected[ColorBG]);
  200. ealloccolor(config.selforeground_color, &dc.selected[ColorFG]);
  201. ealloccolor(config.separator_color, &dc.separator);
  202. ealloccolor(config.border_color, &dc.border);
  203. /* try to get font */
  204. if ((dc.font = XftFontOpenName(dpy, screen, config.font)) == NULL)
  205. errx(1, "cannot load font");
  206. /* create common GC */
  207. dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
  208. }
  209. /* calculate configuration values that are not set manually */
  210. static void
  211. initconfig(void)
  212. {
  213. Window dw; /* dummy variable */
  214. int di; /* dummy variable */
  215. unsigned du; /* dummy variable */
  216. XQueryPointer(dpy, rootwin, &dw, &dw, &config.cursx, &config.cursy, &di, &di, &du);
  217. config.screenw = DisplayWidth(dpy, screen);
  218. config.screenh = DisplayHeight(dpy, screen);
  219. config.iconsize = config.height_pixels - config.iconpadding * 2;
  220. }
  221. /* intern atoms */
  222. static void
  223. initatoms(void)
  224. {
  225. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  226. wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  227. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  228. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  229. netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
  230. }
  231. /* allocate an item */
  232. static struct Item *
  233. allocitem(const char *label, const char *output, char *file)
  234. {
  235. struct Item *item;
  236. if ((item = malloc(sizeof *item)) == NULL)
  237. err(1, "malloc");
  238. if (label == NULL) {
  239. item->label = NULL;
  240. item->output = NULL;
  241. } else {
  242. if ((item->label = strdup(label)) == NULL)
  243. err(1, "strdup");
  244. if (label == output) {
  245. item->output = item->label;
  246. } else {
  247. if ((item->output = strdup(output)) == NULL)
  248. err(1, "strdup");
  249. }
  250. }
  251. if (file == NULL) {
  252. item->file = NULL;
  253. } else {
  254. if ((item->file = strdup(file)) == NULL)
  255. err(1, "strdup");
  256. }
  257. item->y = 0;
  258. item->h = 0;
  259. if (item->label == NULL)
  260. item->labellen = 0;
  261. else
  262. item->labellen = strlen(item->label);
  263. item->next = NULL;
  264. item->submenu = NULL;
  265. item->icon = NULL;
  266. return item;
  267. }
  268. /* allocate a menu and create its window */
  269. static struct Menu *
  270. allocmenu(struct Menu *parent, struct Item *list, unsigned level)
  271. {
  272. XSetWindowAttributes swa;
  273. struct Menu *menu;
  274. if ((menu = malloc(sizeof *menu)) == NULL)
  275. err(1, "malloc");
  276. menu->parent = parent;
  277. menu->list = list;
  278. menu->caller = NULL;
  279. menu->selected = NULL;
  280. menu->w = 0; /* calculated by setupmenu() */
  281. menu->h = 0; /* calculated by setupmenu() */
  282. menu->x = 0; /* calculated by setupmenu() */
  283. menu->y = 0; /* calculated by setupmenu() */
  284. menu->level = level;
  285. swa.override_redirect = (wflag) ? False : True;
  286. swa.background_pixel = dc.normal[ColorBG].pixel;
  287. swa.border_pixel = dc.border.pixel;
  288. swa.save_under = True; /* pop-up windows should save_under*/
  289. swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
  290. | PointerMotionMask | LeaveWindowMask;
  291. if (wflag)
  292. swa.event_mask |= StructureNotifyMask;
  293. menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, 0,
  294. CopyFromParent, CopyFromParent, CopyFromParent,
  295. CWOverrideRedirect | CWBackPixel |
  296. CWBorderPixel | CWEventMask | CWSaveUnder,
  297. &swa);
  298. return menu;
  299. }
  300. /* build the menu tree */
  301. static struct Menu *
  302. buildmenutree(unsigned level, const char *label, const char *output, char *file)
  303. {
  304. static struct Menu *prevmenu = NULL; /* menu the previous item was added to */
  305. static struct Menu *rootmenu = NULL; /* menu to be returned */
  306. struct Item *curritem = NULL; /* item currently being read */
  307. struct Item *item; /* dummy item for loops */
  308. struct Menu *menu; /* dummy menu for loops */
  309. unsigned i;
  310. /* create the item */
  311. curritem = allocitem(label, output, file);
  312. /* put the item in the menu tree */
  313. if (prevmenu == NULL) { /* there is no menu yet */
  314. menu = allocmenu(NULL, curritem, level);
  315. rootmenu = menu;
  316. prevmenu = menu;
  317. curritem->prev = NULL;
  318. } else if (level < prevmenu->level) { /* item is continuation of a parent menu */
  319. /* go up the menu tree until find the menu this item continues */
  320. for (menu = prevmenu, i = level;
  321. menu != NULL && i != prevmenu->level;
  322. menu = menu->parent, i++)
  323. ;
  324. if (menu == NULL)
  325. errx(1, "reached NULL menu");
  326. /* find last item in the new menu */
  327. for (item = menu->list; item->next != NULL; item = item->next)
  328. ;
  329. prevmenu = menu;
  330. item->next = curritem;
  331. curritem->prev = item;
  332. } else if (level == prevmenu->level) { /* item is a continuation of current menu */
  333. /* find last item in the previous menu */
  334. for (item = prevmenu->list; item->next != NULL; item = item->next)
  335. ;
  336. item->next = curritem;
  337. curritem->prev = item;
  338. } else if (level > prevmenu->level) { /* item begins a new menu */
  339. menu = allocmenu(prevmenu, curritem, level);
  340. /* find last item in the previous menu */
  341. for (item = prevmenu->list; item->next != NULL; item = item->next)
  342. ;
  343. prevmenu = menu;
  344. menu->caller = item;
  345. item->submenu = menu;
  346. curritem->prev = NULL;
  347. }
  348. return rootmenu;
  349. }
  350. /* create menus and items from the stdin */
  351. static struct Menu *
  352. parsestdin(void)
  353. {
  354. struct Menu *rootmenu;
  355. char *s, buf[BUFSIZ];
  356. char *file, *label, *output;
  357. unsigned level = 0;
  358. rootmenu = NULL;
  359. while (fgets(buf, BUFSIZ, stdin) != NULL) {
  360. /* get the indentation level */
  361. level = strspn(buf, "\t");
  362. /* get the label */
  363. s = level + buf;
  364. label = strtok(s, "\t\n");
  365. /* get the filename */
  366. file = NULL;
  367. if (label != NULL && strncmp(label, "IMG:", 4) == 0) {
  368. file = label + 4;
  369. label = strtok(NULL, "\t\n");
  370. }
  371. /* get the output */
  372. output = strtok(NULL, "\n");
  373. if (output == NULL) {
  374. output = label;
  375. } else {
  376. while (*output == '\t')
  377. output++;
  378. }
  379. rootmenu = buildmenutree(level, label, output, file);
  380. }
  381. return rootmenu;
  382. }
  383. /* load and scale icon */
  384. static Imlib_Image
  385. loadicon(const char *file)
  386. {
  387. Imlib_Image icon;
  388. int width;
  389. int height;
  390. int imgsize;
  391. icon = imlib_load_image(file);
  392. if (icon == NULL)
  393. errx(1, "cannot load icon %s", file);
  394. imlib_context_set_image(icon);
  395. width = imlib_image_get_width();
  396. height = imlib_image_get_height();
  397. imgsize = MIN(width, height);
  398. icon = imlib_create_cropped_scaled_image(0, 0, imgsize, imgsize,
  399. config.iconsize,
  400. config.iconsize);
  401. return icon;
  402. }
  403. /* setup the height, width and icon of the items of a menu */
  404. static void
  405. setupitems(struct Menu *menu)
  406. {
  407. XGlyphInfo ext;
  408. struct Item *item;
  409. int itemwidth;
  410. menu->w = config.width_pixels;
  411. for (item = menu->list; item != NULL; item = item->next) {
  412. item->y = menu->h;
  413. if (item->label == NULL) /* height for separator item */
  414. item->h = config.separator_pixels;
  415. else
  416. item->h = config.height_pixels;
  417. menu->h += item->h;
  418. /* get length of item->label rendered in the font */
  419. XftTextExtentsUtf8(dpy, dc.font, (XftChar8 *)item->label,
  420. item->labellen, &ext);
  421. /*
  422. * set menu width
  423. *
  424. * the item width depends on the size of its label (ext.xOff),
  425. * and it is only used to calculate the width of the menu (which
  426. * is equal to the width of the largest item).
  427. *
  428. * the horizontal padding appears 4 times through the width of a
  429. * item: before and after its icon, and before and after its triangle
  430. * if the iflag is set (icons are disabled) then the horizontal
  431. * padding appears before the label and around the triangle.
  432. */
  433. itemwidth = ext.xOff + config.triangle_width + config.horzpadding * 3;
  434. itemwidth += (iflag) ? 0 : config.iconsize + config.horzpadding;
  435. menu->w = MAX(menu->w, itemwidth);
  436. /* create icon */
  437. if (item->file != NULL && !iflag)
  438. item->icon = loadicon(item->file);
  439. }
  440. }
  441. /* setup the position of a menu */
  442. static void
  443. setupmenupos(struct Menu *menu)
  444. {
  445. int width, height;
  446. width = menu->w + config.border_pixels * 2;
  447. height = menu->h + config.border_pixels * 2;
  448. if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
  449. if (config.screenw - config.cursx >= menu->w)
  450. menu->x = config.cursx;
  451. else if (config.cursx > width)
  452. menu->x = config.cursx - width;
  453. if (config.screenh - config.cursy >= height)
  454. menu->y = config.cursy;
  455. else if (config.screenh > height)
  456. menu->y = config.screenh - height;
  457. } else { /* else, calculate in respect to parent menu */
  458. if (config.screenw - (menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels) >= width)
  459. menu->x = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
  460. else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
  461. menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
  462. if (config.screenh - (menu->caller->y + menu->parent->y) > height)
  463. menu->y = menu->caller->y + menu->parent->y;
  464. else if (config.screenh - menu->parent->y > height)
  465. menu->y = menu->parent->y;
  466. else if (config.screenh > height)
  467. menu->y = config.screenh - height;
  468. }
  469. }
  470. /* recursivelly setup menu configuration and its pixmap */
  471. static void
  472. setupmenu(struct Menu *menu, XClassHint *classh)
  473. {
  474. char *title;
  475. struct Item *item;
  476. XWindowChanges changes;
  477. XSizeHints sizeh;
  478. XTextProperty wintitle;
  479. /* setup size and position of menus */
  480. setupitems(menu);
  481. setupmenupos(menu);
  482. /* update menu geometry */
  483. changes.border_width = config.border_pixels;
  484. changes.height = menu->h;
  485. changes.width = menu->w;
  486. changes.x = menu->x;
  487. changes.y = menu->y;
  488. XConfigureWindow(dpy, menu->win, CWBorderWidth | CWWidth | CWHeight | CWX | CWY, &changes);
  489. /* set window title (used if wflag is on) */
  490. if (menu->parent == NULL) {
  491. title = classh->res_name;
  492. } else {
  493. title = menu->caller->output;
  494. }
  495. XStringListToTextProperty(&title, 1, &wintitle);
  496. /* set window manager hints */
  497. sizeh.flags = PMaxSize | PMinSize;
  498. sizeh.min_width = sizeh.max_width = menu->w;
  499. sizeh.min_height = sizeh.max_height = menu->h;
  500. XSetWMProperties(dpy, menu->win, &wintitle, NULL, NULL, 0, &sizeh, NULL, classh);
  501. /* create pixmap and XftDraw */
  502. menu->pixmap = XCreatePixmap(dpy, menu->win, menu->w, menu->h,
  503. DefaultDepth(dpy, screen));
  504. menu->draw = XftDrawCreate(dpy, menu->pixmap, visual, colormap);
  505. /* set WM protocols and ewmh window properties */
  506. XSetWMProtocols(dpy, menu->win, &wmdelete, 1);
  507. XChangeProperty(dpy, menu->win, netatom[NetWMName], utf8string, 8,
  508. PropModeReplace, (unsigned char *)title, strlen(title));
  509. XChangeProperty(dpy, menu->win, netatom[NetWMWindowType], XA_ATOM, 32,
  510. PropModeReplace,
  511. (unsigned char *)&netatom[NetWMWindowTypePopupMenu], 1);
  512. /* calculate positions of submenus */
  513. for (item = menu->list; item != NULL; item = item->next) {
  514. if (item->submenu != NULL)
  515. setupmenu(item->submenu, classh);
  516. }
  517. }
  518. /* try to grab pointer, we may have to wait for another process to ungrab */
  519. static void
  520. grabpointer(void)
  521. {
  522. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  523. int i;
  524. for (i = 0; i < 1000; i++) {
  525. if (XGrabPointer(dpy, rootwin, True, ButtonPressMask,
  526. GrabModeAsync, GrabModeAsync, None,
  527. None, CurrentTime) == GrabSuccess)
  528. return;
  529. nanosleep(&ts, NULL);
  530. }
  531. errx(1, "cannot grab keyboard");
  532. }
  533. /* try to grab keyboard, we may have to wait for another process to ungrab */
  534. static void
  535. grabkeyboard(void)
  536. {
  537. struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
  538. int i;
  539. for (i = 0; i < 1000; i++) {
  540. if (XGrabKeyboard(dpy, rootwin, True, GrabModeAsync,
  541. GrabModeAsync, CurrentTime) == GrabSuccess)
  542. return;
  543. nanosleep(&ts, NULL);
  544. }
  545. errx(1, "cannot grab keyboard");
  546. }
  547. /* umap previous menus and map current menu and its parents */
  548. static void
  549. mapmenu(struct Menu *currmenu)
  550. {
  551. static struct Menu *prevmenu = NULL;
  552. struct Menu *menu, *menu_;
  553. struct Menu *lcamenu; /* lowest common ancestor menu */
  554. unsigned minlevel; /* level of the closest to root menu */
  555. unsigned maxlevel; /* level of the closest to root menu */
  556. /* do not remap current menu if it wasn't updated*/
  557. if (prevmenu == currmenu)
  558. return;
  559. /* if this is the first time mapping, skip calculations */
  560. if (prevmenu == NULL) {
  561. XMapWindow(dpy, currmenu->win);
  562. prevmenu = currmenu;
  563. return;
  564. }
  565. /* find lowest common ancestor menu */
  566. minlevel = MIN(currmenu->level, prevmenu->level);
  567. maxlevel = MAX(currmenu->level, prevmenu->level);
  568. if (currmenu->level == maxlevel) {
  569. menu = currmenu;
  570. menu_ = prevmenu;
  571. } else {
  572. menu = prevmenu;
  573. menu_ = currmenu;
  574. }
  575. while (menu->level > minlevel)
  576. menu = menu->parent;
  577. while (menu != menu_) {
  578. menu = menu->parent;
  579. menu_ = menu_->parent;
  580. }
  581. lcamenu = menu;
  582. /* unmap menus from currmenu (inclusive) until lcamenu (exclusive) */
  583. for (menu = prevmenu; menu != lcamenu; menu = menu->parent) {
  584. menu->selected = NULL;
  585. XUnmapWindow(dpy, menu->win);
  586. }
  587. /* map menus from currmenu (inclusive) until lcamenu (exclusive) */
  588. for (menu = currmenu; menu != lcamenu; menu = menu->parent) {
  589. if (wflag) {
  590. setupmenupos(menu);
  591. XMoveWindow(dpy, menu->win, menu->x, menu->y);
  592. }
  593. XMapWindow(dpy, menu->win);
  594. }
  595. prevmenu = currmenu;
  596. }
  597. /* draw separator item */
  598. static void
  599. drawseparator(struct Menu *menu, struct Item *item)
  600. {
  601. int y;
  602. y = item->y + item->h/2;
  603. XSetForeground(dpy, dc.gc, dc.separator.pixel);
  604. XDrawLine(dpy, menu->pixmap, dc.gc, config.horzpadding, y,
  605. menu->w - config.horzpadding, y);
  606. }
  607. /* draw regular item */
  608. static void
  609. drawitem(struct Menu *menu, struct Item *item, XftColor *color)
  610. {
  611. int x, y;
  612. x = config.horzpadding;
  613. x += (iflag) ? 0 : config.horzpadding + config.iconsize;
  614. y = item->y + (item->h + dc.font->ascent) / 2;
  615. XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
  616. XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
  617. x, y, (XftChar8 *)item->label, item->labellen);
  618. /* draw triangle, if item contains a submenu */
  619. if (item->submenu != NULL) {
  620. x = menu->w - config.triangle_width - config.horzpadding;
  621. y = item->y + (item->h - config.triangle_height + 1) / 2;
  622. XPoint triangle[] = {
  623. {x, y},
  624. {x + config.triangle_width, y + config.triangle_height/2},
  625. {x, y + config.triangle_height},
  626. {x, y}
  627. };
  628. XFillPolygon(dpy, menu->pixmap, dc.gc, triangle, LEN(triangle),
  629. Convex, CoordModeOrigin);
  630. }
  631. /* draw icon */
  632. if (item->icon != NULL) {
  633. x = config.horzpadding;
  634. y = item->y + config.iconpadding;
  635. imlib_context_set_drawable(menu->pixmap);
  636. imlib_context_set_image(item->icon);
  637. imlib_render_image_on_drawable(x, y);
  638. }
  639. }
  640. /* draw items of the current menu and of its ancestors */
  641. static void
  642. drawmenu(struct Menu *currmenu)
  643. {
  644. struct Menu *menu;
  645. struct Item *item;
  646. for (menu = currmenu; menu != NULL; menu = menu->parent) {
  647. for (item = menu->list; item != NULL; item = item->next) {
  648. XftColor *color;
  649. /* determine item color */
  650. if (item == menu->selected && item->label != NULL)
  651. color = dc.selected;
  652. else
  653. color = dc.normal;
  654. /* draw item box */
  655. XSetForeground(dpy, dc.gc, color[ColorBG].pixel);
  656. XFillRectangle(dpy, menu->pixmap, dc.gc, 0, item->y,
  657. menu->w, item->h);
  658. if (item->label == NULL) /* item is a separator */
  659. drawseparator(menu, item);
  660. else /* item is a regular item */
  661. drawitem(menu, item, color);
  662. }
  663. XCopyArea(dpy, menu->pixmap, menu->win, dc.gc, 0, 0,
  664. menu->w, menu->h, 0, 0);
  665. }
  666. }
  667. /* get menu of given window */
  668. static struct Menu *
  669. getmenu(struct Menu *currmenu, Window win)
  670. {
  671. struct Menu *menu;
  672. for (menu = currmenu; menu != NULL; menu = menu->parent)
  673. if (menu->win == win)
  674. return menu;
  675. return NULL;
  676. }
  677. /* get item of given menu and position */
  678. static struct Item *
  679. getitem(struct Menu *menu, int y)
  680. {
  681. struct Item *item;
  682. if (menu == NULL)
  683. return NULL;
  684. for (item = menu->list; item != NULL; item = item->next)
  685. if (y >= item->y && y <= item->y + item->h)
  686. return item;
  687. return NULL;
  688. }
  689. /* cycle through the items; non-zero direction is next, zero is prev */
  690. static struct Item *
  691. itemcycle(struct Menu *currmenu, int direction)
  692. {
  693. struct Item *item;
  694. struct Item *lastitem;
  695. item = NULL;
  696. if (direction == ITEMNEXT) {
  697. if (currmenu->selected == NULL)
  698. item = currmenu->list;
  699. else if (currmenu->selected->next != NULL)
  700. item = currmenu->selected->next;
  701. while (item != NULL && item->label == NULL)
  702. item = item->next;
  703. if (item == NULL)
  704. item = currmenu->list;
  705. } else {
  706. for (lastitem = currmenu->list;
  707. lastitem != NULL && lastitem->next != NULL;
  708. lastitem = lastitem->next)
  709. ;
  710. if (currmenu->selected == NULL)
  711. item = lastitem;
  712. else if (currmenu->selected->prev != NULL)
  713. item = currmenu->selected->prev;
  714. while (item != NULL && item->label == NULL)
  715. item = item->prev;
  716. if (item == NULL)
  717. item = lastitem;
  718. }
  719. return item;
  720. }
  721. /* run event loop */
  722. static void
  723. run(struct Menu *currmenu)
  724. {
  725. struct Menu *menu;
  726. struct Item *item;
  727. struct Item *previtem = NULL;
  728. KeySym ksym;
  729. XEvent ev;
  730. mapmenu(currmenu);
  731. while (!XNextEvent(dpy, &ev)) {
  732. switch(ev.type) {
  733. case Expose:
  734. if (ev.xexpose.count == 0)
  735. drawmenu(currmenu);
  736. break;
  737. case MotionNotify:
  738. menu = getmenu(currmenu, ev.xbutton.window);
  739. item = getitem(menu, ev.xbutton.y);
  740. if (menu == NULL || item == NULL || previtem == item)
  741. break;
  742. previtem = item;
  743. menu->selected = item;
  744. if (item->submenu != NULL) {
  745. currmenu = item->submenu;
  746. currmenu->selected = NULL;
  747. } else {
  748. currmenu = menu;
  749. }
  750. mapmenu(currmenu);
  751. drawmenu(currmenu);
  752. break;
  753. case ButtonRelease:
  754. menu = getmenu(currmenu, ev.xbutton.window);
  755. item = getitem(menu, ev.xbutton.y);
  756. if (menu == NULL || item == NULL)
  757. break;
  758. selectitem:
  759. if (item->label == NULL)
  760. break; /* ignore separators */
  761. if (item->submenu != NULL) {
  762. currmenu = item->submenu;
  763. } else {
  764. printf("%s\n", item->output);
  765. return;
  766. }
  767. mapmenu(currmenu);
  768. currmenu->selected = currmenu->list;
  769. drawmenu(currmenu);
  770. break;
  771. case ButtonPress:
  772. menu = getmenu(currmenu, ev.xbutton.window);
  773. if (menu == NULL)
  774. return;
  775. break;
  776. case KeyPress:
  777. ksym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 0, 0);
  778. /* esc closes xmenu when current menu is the root menu */
  779. if (ksym == XK_Escape && currmenu->parent == NULL)
  780. return;
  781. /* Shift-Tab = ISO_Left_Tab */
  782. if (ksym == XK_Tab && (ev.xkey.state & ShiftMask))
  783. ksym = XK_ISO_Left_Tab;
  784. /* cycle through menu */
  785. item = NULL;
  786. if (ksym == XK_ISO_Left_Tab || ksym == XK_Up) {
  787. item = itemcycle(currmenu, ITEMPREV);
  788. } else if (ksym == XK_Tab || ksym == XK_Down) {
  789. item = itemcycle(currmenu, ITEMNEXT);
  790. } else if ((ksym == XK_Return || ksym == XK_Right) &&
  791. currmenu->selected != NULL) {
  792. item = currmenu->selected;
  793. goto selectitem;
  794. } else if ((ksym == XK_Escape || ksym == XK_Left) &&
  795. currmenu->parent != NULL) {
  796. item = currmenu->parent->selected;
  797. currmenu = currmenu->parent;
  798. mapmenu(currmenu);
  799. } else
  800. break;
  801. currmenu->selected = item;
  802. drawmenu(currmenu);
  803. break;
  804. case LeaveNotify:
  805. previtem = NULL;
  806. currmenu->selected = NULL;
  807. drawmenu(currmenu);
  808. break;
  809. case ConfigureNotify:
  810. menu = getmenu(currmenu, ev.xconfigure.window);
  811. if (menu == NULL)
  812. break;
  813. menu->x = ev.xconfigure.x;
  814. menu->y = ev.xconfigure.y;
  815. break;
  816. case ClientMessage:
  817. if ((unsigned long) ev.xclient.data.l[0] != wmdelete)
  818. break;
  819. /* user closed window */
  820. menu = getmenu(currmenu, ev.xclient.window);
  821. if (menu->parent == NULL)
  822. return; /* closing the root menu closes the program */
  823. currmenu = menu->parent;
  824. mapmenu(currmenu);
  825. break;
  826. }
  827. }
  828. }
  829. /* recursivelly free pixmaps and destroy windows */
  830. static void
  831. cleanmenu(struct Menu *menu)
  832. {
  833. struct Item *item;
  834. struct Item *tmp;
  835. item = menu->list;
  836. while (item != NULL) {
  837. if (item->submenu != NULL)
  838. cleanmenu(item->submenu);
  839. tmp = item;
  840. if (tmp->label != tmp->output)
  841. free(tmp->label);
  842. free(tmp->output);
  843. if (tmp->file != NULL) {
  844. free(tmp->file);
  845. if (tmp->icon != NULL) {
  846. imlib_context_set_image(tmp->icon);
  847. imlib_free_image();
  848. }
  849. }
  850. item = item->next;
  851. free(tmp);
  852. }
  853. XFreePixmap(dpy, menu->pixmap);
  854. XftDrawDestroy(menu->draw);
  855. XDestroyWindow(dpy, menu->win);
  856. free(menu);
  857. }
  858. /* cleanup X and exit */
  859. static void
  860. cleanup(void)
  861. {
  862. XUngrabPointer(dpy, CurrentTime);
  863. XUngrabKeyboard(dpy, CurrentTime);
  864. XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
  865. XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
  866. XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
  867. XftColorFree(dpy, visual, colormap, &dc.selected[ColorFG]);
  868. XftColorFree(dpy, visual, colormap, &dc.separator);
  869. XftColorFree(dpy, visual, colormap, &dc.border);
  870. XFreeGC(dpy, dc.gc);
  871. XCloseDisplay(dpy);
  872. }
  873. /* show usage */
  874. static void
  875. usage(void)
  876. {
  877. (void)fprintf(stderr, "usage: xmenu [-iw] [title]\n");
  878. exit(1);
  879. }