sites

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

dwm-systray-20260516-44dbc68.diff (25173B)


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