dotfiles

config files for my linux setup
git clone git://edryd.org/dotfiles
Log | Files | Refs | README

dwm-6.1-systray.diff (22375B)


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