Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

727 lines
23 KiB

4 years ago
  1. diff --git a/config.def.h b/config.def.h
  2. index 1c0b587..2d824d1 100644
  3. --- a/config.def.h
  4. +++ b/config.def.h
  5. @@ -3,6 +3,10 @@
  6. /* appearance */
  7. static const unsigned int borderpx = 1; /* border pixel of windows */
  8. static const unsigned int snap = 32; /* snap pixel */
  9. +static const unsigned int systraypinning = 0; /* 0: sloppy systray follows selected monitor, >0: pin systray to monitor X */
  10. +static const unsigned int systrayspacing = 2; /* systray spacing */
  11. +static const int systraypinningfailfirst = 1; /* 1: if pinning fails, display systray on the first monitor, False: display systray on the last monitor*/
  12. +static const int showsystray = 1; /* 0 means no systray */
  13. static const int showbar = 1; /* 0 means no bar */
  14. static const int topbar = 1; /* 0 means bottom bar */
  15. static const char *fonts[] = { "monospace:size=10" };
  16. diff --git a/dwm.c b/dwm.c
  17. index 664c527..abce13d 100644
  18. --- a/dwm.c
  19. +++ b/dwm.c
  20. @@ -57,12 +57,30 @@
  21. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  22. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  23. +#define SYSTEM_TRAY_REQUEST_DOCK 0
  24. +
  25. +/* XEMBED messages */
  26. +#define XEMBED_EMBEDDED_NOTIFY 0
  27. +#define XEMBED_WINDOW_ACTIVATE 1
  28. +#define XEMBED_FOCUS_IN 4
  29. +#define XEMBED_MODALITY_ON 10
  30. +
  31. +#define XEMBED_MAPPED (1 << 0)
  32. +#define XEMBED_WINDOW_ACTIVATE 1
  33. +#define XEMBED_WINDOW_DEACTIVATE 2
  34. +
  35. +#define VERSION_MAJOR 0
  36. +#define VERSION_MINOR 0
  37. +#define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR
  38. +
  39. /* enums */
  40. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  41. enum { SchemeNorm, SchemeSel }; /* color schemes */
  42. enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
  43. + NetSystemTray, NetSystemTrayOP, NetSystemTrayOrientation, NetSystemTrayOrientationHorz,
  44. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  45. NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
  46. +enum { Manager, Xembed, XembedInfo, XLast }; /* Xembed atoms */
  47. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  48. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  49. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  50. @@ -141,6 +159,12 @@ typedef struct {
  51. int monitor;
  52. } Rule;
  53. +typedef struct Systray Systray;
  54. +struct Systray {
  55. + Window win;
  56. + Client *icons;
  57. +};
  58. +
  59. /* function declarations */
  60. static void applyrules(Client *c);
  61. static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
  62. @@ -172,6 +196,7 @@ static void focusstack(const Arg *arg);
  63. static Atom getatomprop(Client *c, Atom prop);
  64. static int getrootptr(int *x, int *y);
  65. static long getstate(Window w);
  66. +static unsigned int getsystraywidth();
  67. static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
  68. static void grabbuttons(Client *c, int focused);
  69. static void grabkeys(void);
  70. @@ -189,13 +214,16 @@ static void pop(Client *);
  71. static void propertynotify(XEvent *e);
  72. static void quit(const Arg *arg);
  73. static Monitor *recttomon(int x, int y, int w, int h);
  74. +static void removesystrayicon(Client *i);
  75. static void resize(Client *c, int x, int y, int w, int h, int interact);
  76. +static void resizebarwin(Monitor *m);
  77. static void resizeclient(Client *c, int x, int y, int w, int h);
  78. static void resizemouse(const Arg *arg);
  79. +static void resizerequest(XEvent *e);
  80. static void restack(Monitor *m);
  81. static void run(void);
  82. static void scan(void);
  83. -static int sendevent(Client *c, Atom proto);
  84. +static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2, long d3, long d4);
  85. static void sendmon(Client *c, Monitor *m);
  86. static void setclientstate(Client *c, long state);
  87. static void setfocus(Client *c);
  88. @@ -207,6 +235,7 @@ static void seturgent(Client *c, int urg);
  89. static void showhide(Client *c);
  90. static void sigchld(int unused);
  91. static void spawn(const Arg *arg);
  92. +static Monitor *systraytomon(Monitor *m);
  93. static void tag(const Arg *arg);
  94. static void tagmon(const Arg *arg);
  95. static void tile(Monitor *);
  96. @@ -224,18 +253,23 @@ static int updategeom(void);
  97. static void updatenumlockmask(void);
  98. static void updatesizehints(Client *c);
  99. static void updatestatus(void);
  100. +static void updatesystray(void);
  101. +static void updatesystrayicongeom(Client *i, int w, int h);
  102. +static void updatesystrayiconstate(Client *i, XPropertyEvent *ev);
  103. static void updatetitle(Client *c);
  104. static void updatewindowtype(Client *c);
  105. static void updatewmhints(Client *c);
  106. static void view(const Arg *arg);
  107. static Client *wintoclient(Window w);
  108. static Monitor *wintomon(Window w);
  109. +static Client *wintosystrayicon(Window w);
  110. static int xerror(Display *dpy, XErrorEvent *ee);
  111. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  112. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  113. static void zoom(const Arg *arg);
  114. /* variables */
  115. +static Systray *systray = NULL;
  116. static const char broken[] = "broken";
  117. static char stext[256];
  118. static int screen;
  119. @@ -258,9 +292,10 @@ static void (*handler[LASTEvent]) (XEvent *) = {
  120. [MapRequest] = maprequest,
  121. [MotionNotify] = motionnotify,
  122. [PropertyNotify] = propertynotify,
  123. + [ResizeRequest] = resizerequest,
  124. [UnmapNotify] = unmapnotify
  125. };
  126. -static Atom wmatom[WMLast], netatom[NetLast];
  127. +static Atom wmatom[WMLast], netatom[NetLast], xatom[XLast];
  128. static int running = 1;
  129. static Cur *cursor[CurLast];
  130. static Clr **scheme;
  131. @@ -440,7 +475,7 @@ buttonpress(XEvent *e)
  132. arg.ui = 1 << i;
  133. } else if (ev->x < x + blw)
  134. click = ClkLtSymbol;
  135. - else if (ev->x > selmon->ww - (int)TEXTW(stext))
  136. + else if (ev->x > selmon->ww - (int)TEXTW(stext) - getsystraywidth())
  137. click = ClkStatusText;
  138. else
  139. click = ClkWinTitle;
  140. @@ -483,6 +518,11 @@ cleanup(void)
  141. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  142. while (mons)
  143. cleanupmon(mons);
  144. + if (showsystray) {
  145. + XUnmapWindow(dpy, systray->win);
  146. + XDestroyWindow(dpy, systray->win);
  147. + free(systray);
  148. + }
  149. for (i = 0; i < CurLast; i++)
  150. drw_cur_free(drw, cursor[i]);
  151. for (i = 0; i < LENGTH(colors); i++)
  152. @@ -513,9 +553,57 @@ cleanupmon(Monitor *mon)
  153. void
  154. clientmessage(XEvent *e)
  155. {
  156. + XWindowAttributes wa;
  157. + XSetWindowAttributes swa;
  158. XClientMessageEvent *cme = &e->xclient;
  159. Client *c = wintoclient(cme->window);
  160. + if (showsystray && cme->window == systray->win && cme->message_type == netatom[NetSystemTrayOP]) {
  161. + /* add systray icons */
  162. + if (cme->data.l[1] == SYSTEM_TRAY_REQUEST_DOCK) {
  163. + if (!(c = (Client *)calloc(1, sizeof(Client))))
  164. + die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  165. + if (!(c->win = cme->data.l[2])) {
  166. + free(c);
  167. + return;
  168. + }
  169. + c->mon = selmon;
  170. + c->next = systray->icons;
  171. + systray->icons = c;
  172. + if (!XGetWindowAttributes(dpy, c->win, &wa)) {
  173. + /* use sane defaults */
  174. + wa.width = bh;
  175. + wa.height = bh;
  176. + wa.border_width = 0;
  177. + }
  178. + c->x = c->oldx = c->y = c->oldy = 0;
  179. + c->w = c->oldw = wa.width;
  180. + c->h = c->oldh = wa.height;
  181. + c->oldbw = wa.border_width;
  182. + c->bw = 0;
  183. + c->isfloating = True;
  184. + /* reuse tags field as mapped status */
  185. + c->tags = 1;
  186. + updatesizehints(c);
  187. + updatesystrayicongeom(c, wa.width, wa.height);
  188. + XAddToSaveSet(dpy, c->win);
  189. + XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask);
  190. + XReparentWindow(dpy, c->win, systray->win, 0, 0);
  191. + /* use parents background color */
  192. + swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  193. + XChangeWindowAttributes(dpy, c->win, CWBackPixel, &swa);
  194. + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_EMBEDDED_NOTIFY, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
  195. + /* FIXME not sure if I have to send these events, too */
  196. + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_FOCUS_IN, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
  197. + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
  198. + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_MODALITY_ON, 0 , systray->win, XEMBED_EMBEDDED_VERSION);
  199. + XSync(dpy, False);
  200. + resizebarwin(selmon);
  201. + updatesystray();
  202. + setclientstate(c, NormalState);
  203. + }
  204. + return;
  205. + }
  206. if (!c)
  207. return;
  208. if (cme->message_type == netatom[NetWMState]) {
  209. @@ -568,7 +656,7 @@ configurenotify(XEvent *e)
  210. for (c = m->clients; c; c = c->next)
  211. if (c->isfullscreen)
  212. resizeclient(c, m->mx, m->my, m->mw, m->mh);
  213. - XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
  214. + resizebarwin(m);
  215. }
  216. focus(NULL);
  217. arrange(NULL);
  218. @@ -653,6 +741,11 @@ destroynotify(XEvent *e)
  219. if ((c = wintoclient(ev->window)))
  220. unmanage(c, 1);
  221. + else if ((c = wintosystrayicon(ev->window))) {
  222. + removesystrayicon(c);
  223. + resizebarwin(selmon);
  224. + updatesystray();
  225. + }
  226. }
  227. void
  228. @@ -696,19 +789,23 @@ dirtomon(int dir)
  229. void
  230. drawbar(Monitor *m)
  231. {
  232. - int x, w, tw = 0;
  233. + int x, w, tw = 0, stw = 0;
  234. int boxs = drw->fonts->h / 9;
  235. int boxw = drw->fonts->h / 6 + 2;
  236. unsigned int i, occ = 0, urg = 0;
  237. Client *c;
  238. + if(showsystray && m == systraytomon(m))
  239. + stw = getsystraywidth();
  240. +
  241. /* draw status first so it can be overdrawn by tags later */
  242. if (m == selmon) { /* status is only drawn on selected monitor */
  243. drw_setscheme(drw, scheme[SchemeNorm]);
  244. - tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
  245. - drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
  246. + tw = TEXTW(stext) - lrpad / 2 + 2; /* 2px right padding */
  247. + drw_text(drw, m->ww - tw - stw, 0, tw, bh, lrpad / 2 - 2, stext, 0);
  248. }
  249. + resizebarwin(m);
  250. for (c = m->clients; c; c = c->next) {
  251. occ |= c->tags;
  252. if (c->isurgent)
  253. @@ -729,7 +826,7 @@ drawbar(Monitor *m)
  254. drw_setscheme(drw, scheme[SchemeNorm]);
  255. x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
  256. - if ((w = m->ww - tw - x) > bh) {
  257. + if ((w = m->ww - tw - stw - x) > bh) {
  258. if (m->sel) {
  259. drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
  260. drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
  261. @@ -740,7 +837,7 @@ drawbar(Monitor *m)
  262. drw_rect(drw, x, 0, w, bh, 1, 1);
  263. }
  264. }
  265. - drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  266. + drw_map(drw, m->barwin, 0, 0, m->ww - stw, bh);
  267. }
  268. void
  269. @@ -777,8 +874,11 @@ expose(XEvent *e)
  270. Monitor *m;
  271. XExposeEvent *ev = &e->xexpose;
  272. - if (ev->count == 0 && (m = wintomon(ev->window)))
  273. + if (ev->count == 0 && (m = wintomon(ev->window))) {
  274. drawbar(m);
  275. + if (m == selmon)
  276. + updatesystray();
  277. + }
  278. }
  279. void
  280. @@ -863,10 +963,17 @@ getatomprop(Client *c, Atom prop)
  281. unsigned long dl;
  282. unsigned char *p = NULL;
  283. Atom da, atom = None;
  284. + /* FIXME getatomprop should return the number of items and a pointer to
  285. + * the stored data instead of this workaround */
  286. + Atom req = XA_ATOM;
  287. + if (prop == xatom[XembedInfo])
  288. + req = xatom[XembedInfo];
  289. - if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  290. + if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, req,
  291. &da, &di, &dl, &dl, &p) == Success && p) {
  292. atom = *(Atom *)p;
  293. + if (da == xatom[XembedInfo] && dl == 2)
  294. + atom = ((Atom *)p)[1];
  295. XFree(p);
  296. }
  297. return atom;
  298. @@ -900,6 +1007,16 @@ getstate(Window w)
  299. return result;
  300. }
  301. +unsigned int
  302. +getsystraywidth()
  303. +{
  304. + unsigned int w = 0;
  305. + Client *i;
  306. + if(showsystray)
  307. + for(i = systray->icons; i; w += i->w + systrayspacing, i = i->next) ;
  308. + return w ? w + systrayspacing : 1;
  309. +}
  310. +
  311. int
  312. gettextprop(Window w, Atom atom, char *text, unsigned int size)
  313. {
  314. @@ -1004,7 +1121,7 @@ killclient(const Arg *arg)
  315. {
  316. if (!selmon->sel)
  317. return;
  318. - if (!sendevent(selmon->sel, wmatom[WMDelete])) {
  319. + if (!sendevent(selmon->sel->win, wmatom[WMDelete], NoEventMask, wmatom[WMDelete], CurrentTime, 0 , 0, 0)) {
  320. XGrabServer(dpy);
  321. XSetErrorHandler(xerrordummy);
  322. XSetCloseDownMode(dpy, DestroyAll);
  323. @@ -1092,6 +1209,12 @@ maprequest(XEvent *e)
  324. {
  325. static XWindowAttributes wa;
  326. XMapRequestEvent *ev = &e->xmaprequest;
  327. + Client *i;
  328. + if ((i = wintosystrayicon(ev->window))) {
  329. + sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_EMBEDDED_VERSION);
  330. + resizebarwin(selmon);
  331. + updatesystray();
  332. + }
  333. if (!XGetWindowAttributes(dpy, ev->window, &wa))
  334. return;
  335. @@ -1216,6 +1339,16 @@ propertynotify(XEvent *e)
  336. Window trans;
  337. XPropertyEvent *ev = &e->xproperty;
  338. + if ((c = wintosystrayicon(ev->window))) {
  339. + if (ev->atom == XA_WM_NORMAL_HINTS) {
  340. + updatesizehints(c);
  341. + updatesystrayicongeom(c, c->w, c->h);
  342. + }
  343. + else
  344. + updatesystrayiconstate(c, ev);
  345. + resizebarwin(selmon);
  346. + updatesystray();
  347. + }
  348. if ((ev->window == root) && (ev->atom == XA_WM_NAME))
  349. updatestatus();
  350. else if (ev->state == PropertyDelete)
  351. @@ -1266,6 +1399,20 @@ recttomon(int x, int y, int w, int h)
  352. return r;
  353. }
  354. +void
  355. +removesystrayicon(Client *i)
  356. +{
  357. + Client **ii;
  358. +
  359. + if (!showsystray || !i)
  360. + return;
  361. + for (ii = &systray->icons; *ii && *ii != i; ii = &(*ii)->next);
  362. + if (ii)
  363. + *ii = i->next;
  364. + free(i);
  365. +}
  366. +
  367. +
  368. void
  369. resize(Client *c, int x, int y, int w, int h, int interact)
  370. {
  371. @@ -1273,6 +1420,14 @@ resize(Client *c, int x, int y, int w, int h, int interact)
  372. resizeclient(c, x, y, w, h);
  373. }
  374. +void
  375. +resizebarwin(Monitor *m) {
  376. + unsigned int w = m->ww;
  377. + if (showsystray && m == systraytomon(m))
  378. + w -= getsystraywidth();
  379. + XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, w, bh);
  380. +}
  381. +
  382. void
  383. resizeclient(Client *c, int x, int y, int w, int h)
  384. {
  385. @@ -1345,6 +1500,19 @@ resizemouse(const Arg *arg)
  386. }
  387. }
  388. +void
  389. +resizerequest(XEvent *e)
  390. +{
  391. + XResizeRequestEvent *ev = &e->xresizerequest;
  392. + Client *i;
  393. +
  394. + if ((i = wintosystrayicon(ev->window))) {
  395. + updatesystrayicongeom(i, ev->width, ev->height);
  396. + resizebarwin(selmon);
  397. + updatesystray();
  398. + }
  399. +}
  400. +
  401. void
  402. restack(Monitor *m)
  403. {
  404. @@ -1434,26 +1602,36 @@ setclientstate(Client *c, long state)
  405. }
  406. int
  407. -sendevent(Client *c, Atom proto)
  408. +sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2, long d3, long d4)
  409. {
  410. int n;
  411. - Atom *protocols;
  412. + Atom *protocols, mt;
  413. int exists = 0;
  414. XEvent ev;
  415. - if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  416. - while (!exists && n--)
  417. - exists = protocols[n] == proto;
  418. - XFree(protocols);
  419. + if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) {
  420. + mt = wmatom[WMProtocols];
  421. + if (XGetWMProtocols(dpy, w, &protocols, &n)) {
  422. + while (!exists && n--)
  423. + exists = protocols[n] == proto;
  424. + XFree(protocols);
  425. + }
  426. + }
  427. + else {
  428. + exists = True;
  429. + mt = proto;
  430. }
  431. if (exists) {
  432. ev.type = ClientMessage;
  433. - ev.xclient.window = c->win;
  434. - ev.xclient.message_type = wmatom[WMProtocols];
  435. + ev.xclient.window = w;
  436. + ev.xclient.message_type = mt;
  437. ev.xclient.format = 32;
  438. - ev.xclient.data.l[0] = proto;
  439. - ev.xclient.data.l[1] = CurrentTime;
  440. - XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  441. + ev.xclient.data.l[0] = d0;
  442. + ev.xclient.data.l[1] = d1;
  443. + ev.xclient.data.l[2] = d2;
  444. + ev.xclient.data.l[3] = d3;
  445. + ev.xclient.data.l[4] = d4;
  446. + XSendEvent(dpy, w, False, mask, &ev);
  447. }
  448. return exists;
  449. }
  450. @@ -1467,7 +1645,7 @@ setfocus(Client *c)
  451. XA_WINDOW, 32, PropModeReplace,
  452. (unsigned char *) &(c->win), 1);
  453. }
  454. - sendevent(c, wmatom[WMTakeFocus]);
  455. + sendevent(c->win, wmatom[WMTakeFocus], NoEventMask, wmatom[WMTakeFocus], CurrentTime, 0, 0, 0);
  456. }
  457. void
  458. @@ -1556,6 +1734,10 @@ setup(void)
  459. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  460. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  461. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  462. + netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
  463. + netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False);
  464. + netatom[NetSystemTrayOrientation] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False);
  465. + netatom[NetSystemTrayOrientationHorz] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False);
  466. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  467. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  468. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  469. @@ -1563,6 +1745,9 @@ setup(void)
  470. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  471. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  472. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  473. + xatom[Manager] = XInternAtom(dpy, "MANAGER", False);
  474. + xatom[Xembed] = XInternAtom(dpy, "_XEMBED", False);
  475. + xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False);
  476. /* init cursors */
  477. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  478. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  479. @@ -1571,6 +1756,8 @@ setup(void)
  480. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  481. for (i = 0; i < LENGTH(colors); i++)
  482. scheme[i] = drw_scm_create(drw, colors[i], 3);
  483. + /* init system tray */
  484. + updatesystray();
  485. /* init bars */
  486. updatebars();
  487. updatestatus();
  488. @@ -1704,7 +1891,18 @@ togglebar(const Arg *arg)
  489. {
  490. selmon->showbar = !selmon->showbar;
  491. updatebarpos(selmon);
  492. - XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  493. + resizebarwin(selmon);
  494. + if (showsystray) {
  495. + XWindowChanges wc;
  496. + if (!selmon->showbar)
  497. + wc.y = -bh;
  498. + else if (selmon->showbar) {
  499. + wc.y = 0;
  500. + if (!selmon->topbar)
  501. + wc.y = selmon->mh - bh;
  502. + }
  503. + XConfigureWindow(dpy, systray->win, CWY, &wc);
  504. + }
  505. arrange(selmon);
  506. }
  507. @@ -1799,11 +1997,18 @@ unmapnotify(XEvent *e)
  508. else
  509. unmanage(c, 0);
  510. }
  511. + else if ((c = wintosystrayicon(ev->window))) {
  512. + /* KLUDGE! sometimes icons occasionally unmap their windows, but do
  513. + * _not_ destroy them. We map those windows back */
  514. + XMapRaised(dpy, c->win);
  515. + updatesystray();
  516. + }
  517. }
  518. void
  519. updatebars(void)
  520. {
  521. + unsigned int w;
  522. Monitor *m;
  523. XSetWindowAttributes wa = {
  524. .override_redirect = True,
  525. @@ -1814,10 +2019,15 @@ updatebars(void)
  526. for (m = mons; m; m = m->next) {
  527. if (m->barwin)
  528. continue;
  529. - m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  530. + w = m->ww;
  531. + if (showsystray && m == systraytomon(m))
  532. + w -= getsystraywidth();
  533. + m->barwin = XCreateWindow(dpy, root, m->wx, m->by, w, bh, 0, DefaultDepth(dpy, screen),
  534. CopyFromParent, DefaultVisual(dpy, screen),
  535. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  536. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  537. + if (showsystray && m == systraytomon(m))
  538. + XMapRaised(dpy, systray->win);
  539. XMapRaised(dpy, m->barwin);
  540. XSetClassHint(dpy, m->barwin, &ch);
  541. }
  542. @@ -1993,6 +2203,121 @@ updatestatus(void)
  543. if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  544. strcpy(stext, "dwm-"VERSION);
  545. drawbar(selmon);
  546. + updatesystray();
  547. +}
  548. +
  549. +void
  550. +updatesystrayicongeom(Client *i, int w, int h)
  551. +{
  552. + if (i) {
  553. + i->h = bh;
  554. + if (w == h)
  555. + i->w = bh;
  556. + else if (h == bh)
  557. + i->w = w;
  558. + else
  559. + i->w = (int) ((float)bh * ((float)w / (float)h));
  560. + applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False);
  561. + /* force icons into the systray dimensions if they don't want to */
  562. + if (i->h > bh) {
  563. + if (i->w == i->h)
  564. + i->w = bh;
  565. + else
  566. + i->w = (int) ((float)bh * ((float)i->w / (float)i->h));
  567. + i->h = bh;
  568. + }
  569. + }
  570. +}
  571. +
  572. +void
  573. +updatesystrayiconstate(Client *i, XPropertyEvent *ev)
  574. +{
  575. + long flags;
  576. + int code = 0;
  577. +
  578. + if (!showsystray || !i || ev->atom != xatom[XembedInfo] ||
  579. + !(flags = getatomprop(i, xatom[XembedInfo])))
  580. + return;
  581. +
  582. + if (flags & XEMBED_MAPPED && !i->tags) {
  583. + i->tags = 1;
  584. + code = XEMBED_WINDOW_ACTIVATE;
  585. + XMapRaised(dpy, i->win);
  586. + setclientstate(i, NormalState);
  587. + }
  588. + else if (!(flags & XEMBED_MAPPED) && i->tags) {
  589. + i->tags = 0;
  590. + code = XEMBED_WINDOW_DEACTIVATE;
  591. + XUnmapWindow(dpy, i->win);
  592. + setclientstate(i, WithdrawnState);
  593. + }
  594. + else
  595. + return;
  596. + sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0,
  597. + systray->win, XEMBED_EMBEDDED_VERSION);
  598. +}
  599. +
  600. +void
  601. +updatesystray(void)
  602. +{
  603. + XSetWindowAttributes wa;
  604. + XWindowChanges wc;
  605. + Client *i;
  606. + Monitor *m = systraytomon(NULL);
  607. + unsigned int x = m->mx + m->mw;
  608. + unsigned int w = 1;
  609. +
  610. + if (!showsystray)
  611. + return;
  612. + if (!systray) {
  613. + /* init systray */
  614. + if (!(systray = (Systray *)calloc(1, sizeof(Systray))))
  615. + die("fatal: could not malloc() %u bytes\n", sizeof(Systray));
  616. + systray->win = XCreateSimpleWindow(dpy, root, x, m->by, w, bh, 0, 0, scheme[SchemeSel][ColBg].pixel);
  617. + wa.event_mask = ButtonPressMask | ExposureMask;
  618. + wa.override_redirect = True;
  619. + wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  620. + XSelectInput(dpy, systray->win, SubstructureNotifyMask);
  621. + XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XA_CARDINAL, 32,
  622. + PropModeReplace, (unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1);
  623. + XChangeWindowAttributes(dpy, systray->win, CWEventMask|CWOverrideRedirect|CWBackPixel, &wa);
  624. + XMapRaised(dpy, systray->win);
  625. + XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime);
  626. + if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
  627. + sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, netatom[NetSystemTray], systray->win, 0, 0);
  628. + XSync(dpy, False);
  629. + }
  630. + else {
  631. + fprintf(stderr, "dwm: unable to obtain system tray.\n");
  632. + free(systray);
  633. + systray = NULL;
  634. + return;
  635. + }
  636. + }
  637. + for (w = 0, i = systray->icons; i; i = i->next) {
  638. + /* make sure the background color stays the same */
  639. + wa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  640. + XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa);
  641. + XMapRaised(dpy, i->win);
  642. + w += systrayspacing;
  643. + i->x = w;
  644. + XMoveResizeWindow(dpy, i->win, i->x, 0, i->w, i->h);
  645. + w += i->w;
  646. + if (i->mon != m)
  647. + i->mon = m;
  648. + }
  649. + w = w ? w + systrayspacing : 1;
  650. + x -= w;
  651. + XMoveResizeWindow(dpy, systray->win, x, m->by, w, bh);
  652. + wc.x = x; wc.y = m->by; wc.width = w; wc.height = bh;
  653. + wc.stack_mode = Above; wc.sibling = m->barwin;
  654. + XConfigureWindow(dpy, systray->win, CWX|CWY|CWWidth|CWHeight|CWSibling|CWStackMode, &wc);
  655. + XMapWindow(dpy, systray->win);
  656. + XMapSubwindows(dpy, systray->win);
  657. + /* redraw background */
  658. + XSetForeground(dpy, drw->gc, scheme[SchemeNorm][ColBg].pixel);
  659. + XFillRectangle(dpy, systray->win, drw->gc, 0, 0, w, bh);
  660. + XSync(dpy, False);
  661. }
  662. void
  663. @@ -2060,6 +2385,16 @@ wintoclient(Window w)
  664. return NULL;
  665. }
  666. +Client *
  667. +wintosystrayicon(Window w) {
  668. + Client *i = NULL;
  669. +
  670. + if (!showsystray || !w)
  671. + return i;
  672. + for (i = systray->icons; i && i->win != w; i = i->next) ;
  673. + return i;
  674. +}
  675. +
  676. Monitor *
  677. wintomon(Window w)
  678. {
  679. @@ -2113,6 +2448,22 @@ xerrorstart(Display *dpy, XErrorEvent *ee)
  680. return -1;
  681. }
  682. +Monitor *
  683. +systraytomon(Monitor *m) {
  684. + Monitor *t;
  685. + int i, n;
  686. + if(!systraypinning) {
  687. + if(!m)
  688. + return selmon;
  689. + return m == selmon ? m : NULL;
  690. + }
  691. + for(n = 1, t = mons; t && t->next; n++, t = t->next) ;
  692. + for(i = 1, t = mons; t && t->next && i < systraypinning; i++, t = t->next) ;
  693. + if(systraypinningfailfirst && n < systraypinning)
  694. + return mons;
  695. + return t;
  696. +}
  697. +
  698. void
  699. zoom(const Arg *arg)
  700. {