sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-systray-6.7.diff (24497B)


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