dwm-systray-6.3_edit.diff (25281B)
1 diff -up a/config.def.h b/config.def.h 2 --- a/config.def.h Fri Apr 29 22:26:10 2022 3 +++ b/config.def.h Fri Apr 29 22:31:30 2022 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 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 @@ -109,8 +114,8 @@ static Key keys[] = { 17 /* click can be ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, or ClkRootWin */ 18 static 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 -up a/dwm.c b/dwm.c 28 --- a/dwm.c Fri Apr 29 22:26:10 2022 29 +++ b/dwm.c Fri Apr 29 22:33:02 2022 30 @@ -58,12 +58,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 @@ -144,6 +159,12 @@ typedef struct { 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 @@ -176,6 +197,7 @@ static void focusstack(const Arg *arg); 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 @@ -194,13 +216,16 @@ static void pop(Client *); 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 @@ -212,6 +237,7 @@ static void seturgent(Client *c, int urg); 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 *); 105 @@ -230,12 +256,16 @@ static int updategeom(void); 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 @@ -244,6 +274,7 @@ static void bstack(Monitor *m); 123 static void bstackhoriz(Monitor *m); 124 125 /* variables */ 126 +static Systray *systray = NULL; 127 static const char broken[] = "broken"; 128 static char stext[256]; 129 static int screen; 130 @@ -266,9 +297,10 @@ static void (*handler[LASTEvent]) (XEvent *) = { 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 @@ -470,7 +502,7 @@ buttonpress(XEvent *e) 143 arg.ui = 1 << i; 144 } else if (ev->x < x + blw) 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 @@ -513,7 +545,14 @@ cleanup(void) 152 XUngrabKey(dpy, AnyKey, AnyModifier, root); 153 while (mons) 154 cleanupmon(mons); 155 - for (i = 0; i < CurLast; i++) 156 + 157 + if (showsystray) { 158 + XUnmapWindow(dpy, systray->win); 159 + XDestroyWindow(dpy, systray->win); 160 + free(systray); 161 + } 162 + 163 + for (i = 0; i < CurLast; i++) 164 drw_cur_free(drw, cursor[i]); 165 for (i = 0; i < LENGTH(colors); i++) 166 free(scheme[i]); 167 @@ -543,9 +582,58 @@ cleanupmon(Monitor *mon) 168 void 169 clientmessage(XEvent *e) 170 { 171 + XWindowAttributes wa; 172 + XSetWindowAttributes swa; 173 XClientMessageEvent *cme = &e->xclient; 174 Client *c = wintoclient(cme->window); 175 176 + if (showsystray && cme->window == systray->win && cme->message_type == netatom[NetSystemTrayOP]) { 177 + /* add systray icons */ 178 + if (cme->data.l[1] == SYSTEM_TRAY_REQUEST_DOCK) { 179 + if (!(c = (Client *)calloc(1, sizeof(Client)))) 180 + die("fatal: could not malloc() %u bytes\n", sizeof(Client)); 181 + if (!(c->win = cme->data.l[2])) { 182 + free(c); 183 + return; 184 + } 185 + c->mon = selmon; 186 + c->next = systray->icons; 187 + systray->icons = c; 188 + if (!XGetWindowAttributes(dpy, c->win, &wa)) { 189 + /* use sane defaults */ 190 + wa.width = bh; 191 + wa.height = bh; 192 + wa.border_width = 0; 193 + } 194 + c->x = c->oldx = c->y = c->oldy = 0; 195 + c->w = c->oldw = wa.width; 196 + c->h = c->oldh = wa.height; 197 + c->oldbw = wa.border_width; 198 + c->bw = 0; 199 + c->isfloating = True; 200 + /* reuse tags field as mapped status */ 201 + c->tags = 1; 202 + updatesizehints(c); 203 + updatesystrayicongeom(c, wa.width, wa.height); 204 + XAddToSaveSet(dpy, c->win); 205 + XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask); 206 + XReparentWindow(dpy, c->win, systray->win, 0, 0); 207 + /* use parents background color */ 208 + swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 209 + XChangeWindowAttributes(dpy, c->win, CWBackPixel, &swa); 210 + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_EMBEDDED_NOTIFY, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 211 + /* FIXME not sure if I have to send these events, too */ 212 + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_FOCUS_IN, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 213 + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 214 + sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_MODALITY_ON, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 215 + XSync(dpy, False); 216 + resizebarwin(selmon); 217 + updatesystray(); 218 + setclientstate(c, NormalState); 219 + } 220 + return; 221 + } 222 + 223 if (!c) 224 return; 225 if (cme->message_type == netatom[NetWMState]) { 226 @@ -598,7 +686,7 @@ configurenotify(XEvent *e) 227 for (c = m->clients; c; c = c->next) 228 if (c->isfullscreen) 229 resizeclient(c, m->mx, m->my, m->mw, m->mh); 230 - XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh); 231 + resizebarwin(m); 232 } 233 focus(NULL); 234 arrange(NULL); 235 @@ -698,6 +786,11 @@ destroynotify(XEvent *e) 236 237 if ((c = wintoclient(ev->window))) 238 unmanage(c, 1); 239 + else if ((c = wintosystrayicon(ev->window))) { 240 + removesystrayicon(c); 241 + resizebarwin(selmon); 242 + updatesystray(); 243 + } 244 } 245 246 void 247 @@ -741,7 +834,7 @@ dirtomon(int dir) 248 void 249 drawbar(Monitor *m) 250 { 251 - int x, w, tw = 0; 252 + int x, w, tw = 0, stw = 0; 253 int boxs = drw->fonts->h / 9; 254 int boxw = drw->fonts->h / 6 + 2; 255 unsigned int i, occ = 0, urg = 0; 256 @@ -750,13 +843,17 @@ drawbar(Monitor *m) 257 if (!m->showbar) 258 return; 259 260 + if(showsystray && m == systraytomon(m) && !systrayonleft) 261 + stw = getsystraywidth(); 262 + 263 /* draw status first so it can be overdrawn by tags later */ 264 if (m == selmon) { /* status is only drawn on selected monitor */ 265 drw_setscheme(drw, scheme[SchemeNorm]); 266 - tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */ 267 - drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0); 268 + tw = TEXTW(stext) - lrpad / 2 + 2; /* 2px extra right padding */ 269 + drw_text(drw, m->ww - tw - stw, 0, tw, bh, lrpad / 2 - 2, stext, 0); 270 } 271 272 + resizebarwin(m); 273 for (c = m->clients; c; c = c->next) { 274 occ |= c->tags; 275 if (c->isurgent) 276 @@ -777,7 +874,7 @@ drawbar(Monitor *m) 277 drw_setscheme(drw, scheme[SchemeNorm]); 278 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); 279 280 - if ((w = m->ww - tw - x) > bh) { 281 + if ((w = m->ww - tw - stw - x) > bh) { 282 if (m->sel) { 283 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]); 284 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); 285 @@ -788,7 +885,7 @@ drawbar(Monitor *m) 286 drw_rect(drw, x, 0, w, bh, 1, 1); 287 } 288 } 289 - drw_map(drw, m->barwin, 0, 0, m->ww, bh); 290 + drw_map(drw, m->barwin, 0, 0, m->ww - stw, bh); 291 } 292 293 void 294 @@ -825,8 +922,11 @@ expose(XEvent *e) 295 Monitor *m; 296 XExposeEvent *ev = &e->xexpose; 297 298 - if (ev->count == 0 && (m = wintomon(ev->window))) 299 + if (ev->count == 0 && (m = wintomon(ev->window))) { 300 drawbar(m); 301 + if (m == selmon) 302 + updatesystray(); 303 + } 304 } 305 306 void 307 @@ -912,9 +1012,17 @@ getatomprop(Client *c, Atom prop) 308 unsigned char *p = NULL; 309 Atom da, atom = None; 310 311 - if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, 312 + /* FIXME getatomprop should return the number of items and a pointer to 313 + * the stored data instead of this workaround */ 314 + Atom req = XA_ATOM; 315 + if (prop == xatom[XembedInfo]) 316 + req = xatom[XembedInfo]; 317 + 318 + if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, req, 319 &da, &di, &dl, &dl, &p) == Success && p) { 320 atom = *(Atom *)p; 321 + if (da == xatom[XembedInfo] && dl == 2) 322 + atom = ((Atom *)p)[1]; 323 XFree(p); 324 } 325 return atom; 326 @@ -948,6 +1056,16 @@ getstate(Window w) 327 return result; 328 } 329 330 +unsigned int 331 +getsystraywidth() 332 +{ 333 + unsigned int w = 0; 334 + Client *i; 335 + if(showsystray) 336 + for(i = systray->icons; i; w += i->w + systrayspacing, i = i->next) ; 337 + return w ? w + systrayspacing : 1; 338 +} 339 + 340 int 341 gettextprop(Window w, Atom atom, char *text, unsigned int size) 342 { 343 @@ -1052,7 +1170,8 @@ killclient(const Arg *arg) 344 { 345 if (!selmon->sel) 346 return; 347 - if (!sendevent(selmon->sel, wmatom[WMDelete])) { 348 + 349 + if (!sendevent(selmon->sel->win, wmatom[WMDelete], NoEventMask, wmatom[WMDelete], CurrentTime, 0 , 0, 0)) { 350 XGrabServer(dpy); 351 XSetErrorHandler(xerrordummy); 352 XSetCloseDownMode(dpy, DestroyAll); 353 @@ -1149,6 +1268,13 @@ maprequest(XEvent *e) 354 static XWindowAttributes wa; 355 XMapRequestEvent *ev = &e->xmaprequest; 356 357 + Client *i; 358 + if ((i = wintosystrayicon(ev->window))) { 359 + sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_EMBEDDED_VERSION); 360 + resizebarwin(selmon); 361 + updatesystray(); 362 + } 363 + 364 if (!XGetWindowAttributes(dpy, ev->window, &wa)) 365 return; 366 if (wa.override_redirect) 367 @@ -1282,7 +1408,18 @@ propertynotify(XEvent *e) 368 Window trans; 369 XPropertyEvent *ev = &e->xproperty; 370 371 - if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 372 + if ((c = wintosystrayicon(ev->window))) { 373 + if (ev->atom == XA_WM_NORMAL_HINTS) { 374 + updatesizehints(c); 375 + updatesystrayicongeom(c, c->w, c->h); 376 + } 377 + else 378 + updatesystrayiconstate(c, ev); 379 + resizebarwin(selmon); 380 + updatesystray(); 381 + } 382 + 383 + if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 384 updatestatus(); 385 else if (ev->state == PropertyDelete) 386 return; /* ignore */ 387 @@ -1333,6 +1470,19 @@ recttomon(int x, int y, int w, int h) 388 } 389 390 void 391 +removesystrayicon(Client *i) 392 +{ 393 + Client **ii; 394 + 395 + if (!showsystray || !i) 396 + return; 397 + for (ii = &systray->icons; *ii && *ii != i; ii = &(*ii)->next); 398 + if (ii) 399 + *ii = i->next; 400 + free(i); 401 +} 402 + 403 +void 404 resize(Client *c, int x, int y, int w, int h, int interact) 405 { 406 if (applysizehints(c, &x, &y, &w, &h, interact)) 407 @@ -1340,6 +1490,14 @@ resize(Client *c, int x, int y, int w, int h, int inte 408 } 409 410 void 411 +resizebarwin(Monitor *m) { 412 + unsigned int w = m->ww; 413 + if (showsystray && m == systraytomon(m) && !systrayonleft) 414 + w -= getsystraywidth(); 415 + XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, w, bh); 416 +} 417 + 418 +void 419 resizeclient(Client *c, int x, int y, int w, int h) 420 { 421 XWindowChanges wc; 422 @@ -1436,6 +1594,19 @@ resizemouse(const Arg *arg) 423 } 424 425 void 426 +resizerequest(XEvent *e) 427 +{ 428 + XResizeRequestEvent *ev = &e->xresizerequest; 429 + Client *i; 430 + 431 + if ((i = wintosystrayicon(ev->window))) { 432 + updatesystrayicongeom(i, ev->width, ev->height); 433 + resizebarwin(selmon); 434 + updatesystray(); 435 + } 436 +} 437 + 438 +void 439 restack(Monitor *m) 440 { 441 Client *c; 442 @@ -1524,26 +1695,37 @@ setclientstate(Client *c, long state) 443 } 444 445 int 446 -sendevent(Client *c, Atom proto) 447 +sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2, long d3, long d4) 448 { 449 int n; 450 - Atom *protocols; 451 + Atom *protocols, mt; 452 int exists = 0; 453 XEvent ev; 454 455 - if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { 456 - while (!exists && n--) 457 - exists = protocols[n] == proto; 458 - XFree(protocols); 459 + if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) { 460 + mt = wmatom[WMProtocols]; 461 + if (XGetWMProtocols(dpy, w, &protocols, &n)) { 462 + while (!exists && n--) 463 + exists = protocols[n] == proto; 464 + XFree(protocols); 465 + } 466 } 467 + else { 468 + exists = True; 469 + mt = proto; 470 + } 471 + 472 if (exists) { 473 ev.type = ClientMessage; 474 - ev.xclient.window = c->win; 475 - ev.xclient.message_type = wmatom[WMProtocols]; 476 + ev.xclient.window = w; 477 + ev.xclient.message_type = mt; 478 ev.xclient.format = 32; 479 - ev.xclient.data.l[0] = proto; 480 - ev.xclient.data.l[1] = CurrentTime; 481 - XSendEvent(dpy, c->win, False, NoEventMask, &ev); 482 + ev.xclient.data.l[0] = d0; 483 + ev.xclient.data.l[1] = d1; 484 + ev.xclient.data.l[2] = d2; 485 + ev.xclient.data.l[3] = d3; 486 + ev.xclient.data.l[4] = d4; 487 + XSendEvent(dpy, w, False, mask, &ev); 488 } 489 return exists; 490 } 491 @@ -1557,7 +1739,7 @@ setfocus(Client *c) 492 XA_WINDOW, 32, PropModeReplace, 493 (unsigned char *) &(c->win), 1); 494 } 495 - sendevent(c, wmatom[WMTakeFocus]); 496 + sendevent(c->win, wmatom[WMTakeFocus], NoEventMask, wmatom[WMTakeFocus], CurrentTime, 0, 0, 0); 497 } 498 499 void 500 @@ -1645,15 +1827,22 @@ setup(void) 501 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False); 502 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False); 503 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); 504 - netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 505 - netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 506 + netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 507 + netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False); 508 + netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False); 509 + netatom[NetSystemTrayOrientation] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False); 510 + netatom[NetSystemTrayOrientationHorz] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False); 511 + netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 512 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 513 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False); 514 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 515 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); 516 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); 517 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False); 518 - /* init cursors */ 519 + xatom[Manager] = XInternAtom(dpy, "MANAGER", False); 520 + xatom[Xembed] = XInternAtom(dpy, "_XEMBED", False); 521 + xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False); 522 + /* init cursors */ 523 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); 524 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 525 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 526 @@ -1661,6 +1850,8 @@ setup(void) 527 scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); 528 for (i = 0; i < LENGTH(colors); i++) 529 scheme[i] = drw_scm_create(drw, colors[i], 3); 530 + /* init system tray */ 531 + updatesystray(); 532 /* init bars */ 533 updatebars(); 534 updatestatus(); 535 @@ -1795,7 +1986,18 @@ togglebar(const Arg *arg) 536 { 537 selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar; 538 updatebarpos(selmon); 539 - XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); 540 + resizebarwin(selmon); 541 + if (showsystray) { 542 + XWindowChanges wc; 543 + if (!selmon->showbar) 544 + wc.y = -bh; 545 + else if (selmon->showbar) { 546 + wc.y = 0; 547 + if (!selmon->topbar) 548 + wc.y = selmon->mh - bh; 549 + } 550 + XConfigureWindow(dpy, systray->win, CWY, &wc); 551 + } 552 arrange(selmon); 553 } 554 555 @@ -1936,11 +2138,18 @@ unmapnotify(XEvent *e) 556 else 557 unmanage(c, 0); 558 } 559 + else if ((c = wintosystrayicon(ev->window))) { 560 + /* KLUDGE! sometimes icons occasionally unmap their windows, but do 561 + * _not_ destroy them. We map those windows back */ 562 + XMapRaised(dpy, c->win); 563 + updatesystray(); 564 + } 565 } 566 567 void 568 updatebars(void) 569 { 570 + unsigned int w; 571 Monitor *m; 572 XSetWindowAttributes wa = { 573 .override_redirect = True, 574 @@ -1951,10 +2160,15 @@ updatebars(void) 575 for (m = mons; m; m = m->next) { 576 if (m->barwin) 577 continue; 578 - m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen), 579 + w = m->ww; 580 + if (showsystray && m == systraytomon(m)) 581 + w -= getsystraywidth(); 582 + m->barwin = XCreateWindow(dpy, root, m->wx, m->by, w, bh, 0, DefaultDepth(dpy, screen), 583 CopyFromParent, DefaultVisual(dpy, screen), 584 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); 585 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); 586 + if (showsystray && m == systraytomon(m)) 587 + XMapRaised(dpy, systray->win); 588 XMapRaised(dpy, m->barwin); 589 XSetClassHint(dpy, m->barwin, &ch); 590 } 591 @@ -2130,9 +2344,128 @@ updatestatus(void) 592 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) 593 strcpy(stext, "dwm-"VERSION); 594 drawbar(selmon); 595 + updatesystray(); 596 } 597 598 + 599 void 600 +updatesystrayicongeom(Client *i, int w, int h) 601 +{ 602 + if (i) { 603 + i->h = bh; 604 + if (w == h) 605 + i->w = bh; 606 + else if (h == bh) 607 + i->w = w; 608 + else 609 + i->w = (int) ((float)bh * ((float)w / (float)h)); 610 + applysizehints(i, &(i->x), &(i->y), &(i->w), &(i->h), False); 611 + /* force icons into the systray dimensions if they don't want to */ 612 + if (i->h > bh) { 613 + if (i->w == i->h) 614 + i->w = bh; 615 + else 616 + i->w = (int) ((float)bh * ((float)i->w / (float)i->h)); 617 + i->h = bh; 618 + } 619 + } 620 +} 621 + 622 +void 623 +updatesystrayiconstate(Client *i, XPropertyEvent *ev) 624 +{ 625 + long flags; 626 + int code = 0; 627 + 628 + if (!showsystray || !i || ev->atom != xatom[XembedInfo] || 629 + !(flags = getatomprop(i, xatom[XembedInfo]))) 630 + return; 631 + 632 + if (flags & XEMBED_MAPPED && !i->tags) { 633 + i->tags = 1; 634 + code = XEMBED_WINDOW_ACTIVATE; 635 + XMapRaised(dpy, i->win); 636 + setclientstate(i, NormalState); 637 + } 638 + else if (!(flags & XEMBED_MAPPED) && i->tags) { 639 + i->tags = 0; 640 + code = XEMBED_WINDOW_DEACTIVATE; 641 + XUnmapWindow(dpy, i->win); 642 + setclientstate(i, WithdrawnState); 643 + } 644 + else 645 + return; 646 + sendevent(i->win, xatom[Xembed], StructureNotifyMask, CurrentTime, code, 0, 647 + systray->win, XEMBED_EMBEDDED_VERSION); 648 +} 649 + 650 +void 651 +updatesystray(void) 652 +{ 653 + XSetWindowAttributes wa; 654 + XWindowChanges wc; 655 + Client *i; 656 + Monitor *m = systraytomon(NULL); 657 + unsigned int x = m->mx + m->mw; 658 + unsigned int sw = TEXTW(stext) - lrpad + systrayspacing; 659 + unsigned int w = 1; 660 + 661 + if (!showsystray) 662 + return; 663 + if (systrayonleft) 664 + x -= sw + lrpad / 2; 665 + if (!systray) { 666 + /* init systray */ 667 + if (!(systray = (Systray *)calloc(1, sizeof(Systray)))) 668 + die("fatal: could not malloc() %u bytes\n", sizeof(Systray)); 669 + systray->win = XCreateSimpleWindow(dpy, root, x, m->by, w, bh, 0, 0, scheme[SchemeSel][ColBg].pixel); 670 + wa.event_mask = ButtonPressMask | ExposureMask; 671 + wa.override_redirect = True; 672 + wa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 673 + XSelectInput(dpy, systray->win, SubstructureNotifyMask); 674 + XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XA_CARDINAL, 32, 675 + PropModeReplace, (unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1); 676 + XChangeWindowAttributes(dpy, systray->win, CWEventMask|CWOverrideRedirect|CWBackPixel, &wa); 677 + XMapRaised(dpy, systray->win); 678 + XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime); 679 + if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) { 680 + sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, netatom[NetSystemTray], systray->win, 0, 0); 681 + XSync(dpy, False); 682 + } 683 + else { 684 + fprintf(stderr, "dwm: unable to obtain system tray.\n"); 685 + free(systray); 686 + systray = NULL; 687 + return; 688 + } 689 + } 690 + for (w = 0, i = systray->icons; i; i = i->next) { 691 + /* make sure the background color stays the same */ 692 + wa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 693 + XChangeWindowAttributes(dpy, i->win, CWBackPixel, &wa); 694 + XMapRaised(dpy, i->win); 695 + w += systrayspacing; 696 + i->x = w; 697 + XMoveResizeWindow(dpy, i->win, i->x, 0, i->w, i->h); 698 + w += i->w; 699 + if (i->mon != m) 700 + i->mon = m; 701 + } 702 + w = w ? w + systrayspacing : 1; 703 + x -= w; 704 + XMoveResizeWindow(dpy, systray->win, x, m->by, w, bh); 705 + wc.x = x; wc.y = m->by; wc.width = w; wc.height = bh; 706 + wc.stack_mode = Above; wc.sibling = m->barwin; 707 + XConfigureWindow(dpy, systray->win, CWX|CWY|CWWidth|CWHeight|CWSibling|CWStackMode, &wc); 708 + XMapWindow(dpy, systray->win); 709 + XMapSubwindows(dpy, systray->win); 710 + /* redraw background */ 711 + XSetForeground(dpy, drw->gc, scheme[SchemeNorm][ColBg].pixel); 712 + XFillRectangle(dpy, systray->win, drw->gc, 0, 0, w, bh); 713 + XSync(dpy, False); 714 +} 715 + 716 +void 717 updatetitle(Client *c) 718 { 719 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) 720 @@ -2223,6 +2556,16 @@ wintoclient(Window w) 721 return NULL; 722 } 723 724 +Client * 725 +wintosystrayicon(Window w) { 726 + Client *i = NULL; 727 + 728 + if (!showsystray || !w) 729 + return i; 730 + for (i = systray->icons; i && i->win != w; i = i->next) ; 731 + return i; 732 +} 733 + 734 Monitor * 735 wintomon(Window w) 736 { 737 @@ -2274,6 +2617,22 @@ xerrorstart(Display *dpy, XErrorEvent *ee) 738 { 739 die("dwm: another window manager is already running"); 740 return -1; 741 +} 742 + 743 +Monitor * 744 +systraytomon(Monitor *m) { 745 + Monitor *t; 746 + int i, n; 747 + if(!systraypinning) { 748 + if(!m) 749 + return selmon; 750 + return m == selmon ? m : NULL; 751 + } 752 + for(n = 1, t = mons; t && t->next; n++, t = t->next) ; 753 + for(i = 1, t = mons; t && t->next && i < systraypinning; i++, t = t->next) ; 754 + if(systraypinningfailfirst && n < systraypinning) 755 + return mons; 756 + return t; 757 } 758 759 void