dwm

My dwm fork
git clone https://git.kausban.com/dwm/raw/.git
Log | Files | Refs | README

dwm-systray-6.4_edit.diff (23873B)


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