sites

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

dwm-systray-20180314-3bd8466.diff (22954B)


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