From e03248a4d5feaaacb130416be6e467a04de81f78 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 22 Jul 2022 09:18:52 +0200 Subject: [PATCH 01/11] Revert "do not call signal-unsafe function inside sighanlder" This reverts commit 6613d9f9a1a5630bab30bc2b70bdc793977073ee. Discussed on the mailinglist: https://lists.suckless.org/hackers/2207/18405.html --- dwm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dwm.c b/dwm.c index 7c0f978..b3c43ee 100644 --- a/dwm.c +++ b/dwm.c @@ -1541,8 +1541,6 @@ setup(void) Atom utf8string; /* clean up any zombies immediately */ - if (signal(SIGCHLD, sigchld) == SIG_ERR) - die("can't install SIGCHLD handler:"); sigchld(0); /* init screen */ @@ -1640,6 +1638,8 @@ showhide(Client *c) void sigchld(int unused) { + if (signal(SIGCHLD, sigchld) == SIG_ERR) + die("can't install SIGCHLD handler:"); while (0 < waitpid(-1, NULL, WNOHANG)); } From 786f6e2a6f8466ad94706781bc93bc6eb6e69512 Mon Sep 17 00:00:00 2001 From: Stein Date: Mon, 1 Aug 2022 11:42:44 +0200 Subject: [PATCH 02/11] unmanage: stop listening for events for unmanaged windows This is in particular to avoid flickering in dwm (and high CPU usage) when hovering the mouse over a tabbed window that was previously managed by dwm. Consider the following two scenarios: 1) We start tabbed (window 0xc000003), tabbed is managed by the window manager. We start st being embedded into tabbed. $ st -w 0xc000003 What happens here is that: - tabbed gets a MapRequest for the st window - tabbed reparents the st window - tabbed will receive X events for the window The window manager will have no awareness of the st window and the X server will not send X events to the window manager relating to the st window. There is no flickering or any other issues relating to focus. 2) We start tabbed (window 0xc000003), tabbed is managed by the window manager. We start st as normal (window 0xd400005). What happens here is that: - the window manager gets a MapRequest for the st window - dwm manages the st window as a normal client - dwm will receive X events for the window Now we use xdotool to trigger a reparenting of the st window into tabbed. $ xdotool windowreparent 0xd400005 0xc000003 What happens here is that: - tabbed gets a MapRequest for the st window - tabbed reparents the st window - the window manager gets an UnmapNotify - the window manager no longer manages the st window - both the window manager and tabbed will receive X events for the st window In dwm move the mouse cursor over the tabbed window. What happens now is that: - dwm will receive a FocusIn event for the tabbed window - dwm will set input focus for the tabbed window - tabbed will receive a FocusIn event for the main window - tabbed will give focus to the window on the currently selected tab - which again triggers a FocusIn event which dwm receives - dwm determines that the window that the FocusIn event is for (0xd400005) is not the currently selected client (tabbed) - dwm sets input focus for the tabbed window - this causes an infinite loop as long as the mouse cursor hovers the tabbed window, resulting in flickering and high CPU usage The fix here is to tell the X server that we are no longer interested in receiving events for this window when the window manager stops managing the window. --- dwm.c | 1 + 1 file changed, 1 insertion(+) diff --git a/dwm.c b/dwm.c index b3c43ee..c0c3b9b 100644 --- a/dwm.c +++ b/dwm.c @@ -1780,6 +1780,7 @@ unmanage(Client *c, int destroyed) wc.border_width = c->oldbw; XGrabServer(dpy); /* avoid race conditions */ XSetErrorHandler(xerrordummy); + XSelectInput(dpy, c->win, NoEventMask); XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */ XUngrabButton(dpy, AnyButton, AnyModifier, c->win); setclientstate(c, WithdrawnState); From 5b2e5e7a4001479e4dc3e245f96e49f7ea0da658 Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Fri, 29 Jul 2022 18:26:04 -0500 Subject: [PATCH 03/11] spawn: reduce 2 lines, change fprintf() + perror() + exit() to die("... :") when calling die and the last character of the string corresponds to ':', die() will call perror(). See util.c Also change EXIT_SUCCESS to EXIT_FAILURE --- dwm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dwm.c b/dwm.c index c0c3b9b..61713b7 100644 --- a/dwm.c +++ b/dwm.c @@ -1653,9 +1653,7 @@ spawn(const Arg *arg) close(ConnectionNumber(dpy)); setsid(); execvp(((char **)arg->v)[0], (char **)arg->v); - fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]); - perror(" failed"); - exit(EXIT_SUCCESS); + die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]); } } From 5e76e7e21da042c493c59235ca82d7275f20a7e4 Mon Sep 17 00:00:00 2001 From: NRK Date: Sat, 6 Aug 2022 04:27:13 +0600 Subject: [PATCH 04/11] code-style: simplify some checks main change here is making the `zoom()` logic saner. the rest of the changes are just small stuff which accumulated on my local branch. pop() must not be called with NULL. and `zoom()` achieves this, but in a very (unnecessarily) complicated way: if c == NULL then nexttiled() will return NULL as well, so we enter this branch: if (c == nexttiled(selmon->clients)) in here the !c check fails and the function returns before calling pop() if (!c || !(c = nexttiled(c->next))) return; however, none of this was needed. we can simply return early if c was NULL. Also `c` is set to `selmon->sel` so we can use `c` in the first check instead which makes things shorter. --- dwm.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/dwm.c b/dwm.c index 61713b7..967c9e8 100644 --- a/dwm.c +++ b/dwm.c @@ -918,13 +918,11 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) text[0] = '\0'; if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems) return 0; - if (name.encoding == XA_STRING) + if (name.encoding == XA_STRING) { strncpy(text, (char *)name.value, size - 1); - else { - if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { - strncpy(text, *list, size - 1); - XFreeStringList(list); - } + } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { + strncpy(text, *list, size - 1); + XFreeStringList(list); } text[size - 1] = '\0'; XFree(name.value); @@ -1099,9 +1097,7 @@ maprequest(XEvent *e) static XWindowAttributes wa; XMapRequestEvent *ev = &e->xmaprequest; - if (!XGetWindowAttributes(dpy, ev->window, &wa)) - return; - if (wa.override_redirect) + if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect) return; if (!wintoclient(ev->window)) manage(ev->window, &wa); @@ -1603,7 +1599,6 @@ setup(void) focus(NULL); } - void seturgent(Client *c, int urg) { @@ -2124,12 +2119,10 @@ zoom(const Arg *arg) { Client *c = selmon->sel; - if (!selmon->lt[selmon->sellt]->arrange - || (selmon->sel && selmon->sel->isfloating)) + if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating) + return; + if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next))) return; - if (c == nexttiled(selmon->clients)) - if (!c || !(c = nexttiled(c->next))) - return; pop(c); } From e0dee911455cee739a5b05a994828f4a37a2764d Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Mon, 8 Aug 2022 10:43:09 +0200 Subject: [PATCH 05/11] sync code-style patch from libsl --- util.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/util.c b/util.c index fe044fc..96b82c9 100644 --- a/util.c +++ b/util.c @@ -6,18 +6,9 @@ #include "util.h" -void * -ecalloc(size_t nmemb, size_t size) -{ - void *p; - - if (!(p = calloc(nmemb, size))) - die("calloc:"); - return p; -} - void -die(const char *fmt, ...) { +die(const char *fmt, ...) +{ va_list ap; va_start(ap, fmt); @@ -33,3 +24,13 @@ die(const char *fmt, ...) { exit(1); } + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} From a859676ead17017bbe81b4989b2f2e0b00a0b4ba Mon Sep 17 00:00:00 2001 From: Stein Date: Tue, 9 Aug 2022 10:38:08 +0200 Subject: [PATCH 06/11] Simplify client y-offset correction The reasoning behind the original line may be lost to time as it does not make much sense checking the position on the x-axis to determine how to position the client on the y-axis. In the context of multi-monitor setups the monitor y position (m->my) may be greater than 0 (say 500), in which case the window could be placed out of view if: - the window attributes have a 0 value for the y position and - we end up using the y position of bh (e.g. 22) If the aim is to avoid a new floating client covering the bar then restricting y position to be at least that of the window area (m->wy) should cover the two cases of using a top bar and using a bottom bar. --- dwm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dwm.c b/dwm.c index 967c9e8..87d0ada 100644 --- a/dwm.c +++ b/dwm.c @@ -1049,9 +1049,7 @@ manage(Window w, XWindowAttributes *wa) if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh) c->y = c->mon->my + c->mon->mh - HEIGHT(c); c->x = MAX(c->x, c->mon->mx); - /* only fix client y-offset, if the client center might cover the bar */ - c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx) - && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my); + c->y = MAX(c->y, c->mon->wy); c->bw = borderpx; wc.border_width = c->bw; From 44adafe0069e73aa03a3829d7bb39591cd8b3f1d Mon Sep 17 00:00:00 2001 From: Stein Date: Thu, 11 Aug 2022 11:15:55 +0200 Subject: [PATCH 07/11] Make floating windows spawn within the monitor's window area This is a follow-up on this thread: https://lists.suckless.org/hackers/2208/18462.html The orginal code had constraints such that if a window's starting attributes (position and size) were to place the window outside of the edges of the monitor, then the window would be moved into view at the closest monitor edge. There was an exception to this where if a top bar is used then the window should not obscure the bar if present, which meant to place the window within the window area instead. The proposed change here makes it the general rule that floating windows should spawn within the window area rather than within the monitor area. This makes it simple and consistent with no exceptions and it makes the intention of the code clear. This has the benefit of making the behaviour consistent regardless of whether the user is using a top bar or a bottom bar. Additionally this will have an effect on patches that modify the size of the window area. For example if the insets patch is used to reserve space on the left hand side of the monitor for a dock or a vertical bar then new floating clients will not obscure that area. --- dwm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dwm.c b/dwm.c index 87d0ada..f2bc4ba 100644 --- a/dwm.c +++ b/dwm.c @@ -1044,11 +1044,11 @@ manage(Window w, XWindowAttributes *wa) applyrules(c); } - if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw) - c->x = c->mon->mx + c->mon->mw - WIDTH(c); - if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh) - c->y = c->mon->my + c->mon->mh - HEIGHT(c); - c->x = MAX(c->x, c->mon->mx); + if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww) + c->x = c->mon->wx + c->mon->ww - WIDTH(c); + if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh) + c->y = c->mon->wy + c->mon->wh - HEIGHT(c); + c->x = MAX(c->x, c->mon->wx); c->y = MAX(c->y, c->mon->wy); c->bw = borderpx; From 5799dd1fca6576b662d299e210cd5933b29d502d Mon Sep 17 00:00:00 2001 From: Stein Date: Mon, 15 Aug 2022 14:31:22 +0200 Subject: [PATCH 08/11] Remove blw variable in favour of calculating the value when needed The purpose and reasoning behind the bar layout width (blw) variable in dwm the way it is today may not be immediately obvious. The use of the variable makes more sense when looking at commit 2ce37bc from 2009 where blw was initialised in the setup function and it represented the maximum of all available layout symbols. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) { w = TEXTW(layouts[i].symbol); blw = MAX(blw, w); } As such the layout symbol back then was fixed in size and both drawbar and buttonpress depended on this variable. The the way the blw variable is set today in drawbar means that it merely caches the size of the layout symbol for the last bar drawn. While unlikely to happen in practice it is possible that the last bar drawn is not that of the currently selected monitor, which can result in misaligned button clicks if there is a difference in layout symbol width between monitors. --- dwm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dwm.c b/dwm.c index f2bc4ba..253aba7 100644 --- a/dwm.c +++ b/dwm.c @@ -240,7 +240,7 @@ static const char broken[] = "broken"; static char stext[256]; static int screen; static int sw, sh; /* X display screen geometry width, height */ -static int bh, blw = 0; /* bar geometry */ +static int bh; /* bar height */ static int lrpad; /* sum of left and right padding for text */ static int (*xerrorxlib)(Display *, XErrorEvent *); static unsigned int numlockmask = 0; @@ -440,7 +440,7 @@ buttonpress(XEvent *e) if (i < LENGTH(tags)) { click = ClkTagBar; arg.ui = 1 << i; - } else if (ev->x < x + blw) + } else if (ev->x < x + TEXTW(selmon->ltsymbol)) click = ClkLtSymbol; else if (ev->x > selmon->ww - (int)TEXTW(stext)) click = ClkStatusText; @@ -731,7 +731,7 @@ drawbar(Monitor *m) urg & 1 << i); x += w; } - w = blw = TEXTW(m->ltsymbol); + w = TEXTW(m->ltsymbol); drw_setscheme(drw, scheme[SchemeNorm]); x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); From 84d7322113c2bf023f5eaa8537fb0e72d4105046 Mon Sep 17 00:00:00 2001 From: NRK Date: Thu, 18 Aug 2022 22:13:08 +0600 Subject: [PATCH 09/11] config.def.h: make keys and buttons const pretty much all other variables are declared as const when they're not modified. --- config.def.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.def.h b/config.def.h index a2ac963..9efa774 100644 --- a/config.def.h +++ b/config.def.h @@ -60,7 +60,7 @@ static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; static const char *termcmd[] = { "st", NULL }; -static Key keys[] = { +static const Key keys[] = { /* modifier key function argument */ { MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, @@ -99,7 +99,7 @@ static Key keys[] = { /* button definitions */ /* click can be ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, or ClkRootWin */ -static Button buttons[] = { +static const Button buttons[] = { /* click event mask button function argument */ { ClkLtSymbol, 0, Button1, setlayout, {0} }, { ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} }, From c2b748e7931e5f28984efc236f9b1a212dbc65e8 Mon Sep 17 00:00:00 2001 From: Stein Date: Fri, 26 Aug 2022 14:48:46 +0200 Subject: [PATCH 10/11] Remove dmenumon variable Reasoning: Since 2011 dmenu has been capable of working out which monitor currently has focus in a Xinerama setup, making the use of the -m flag more or less redundant. This is easily demonstrated by using dmenu in any other window manager. There used to be a nodmenu patch that provided these changes: https://git.suckless.org/sites/commit/ed68e3629de4ef2ca2d3f8893a79fb570b4c0cbc.html but this was removed on the basis that it was very easy to work out and apply manually if needed. The proposal here is to remove this dependency from dwm. The mechanism of the dmenumon variable could be provided via a patch if need be. The edge case scenario that dmenu does not handle on its own, and the effect of removing this mechanism, is that if the user trigger focusmon via keybindings to change focus to another monitor that has no clients, then dmenu will open on the monitor containing the window with input focus (or the monitor with the mouse cursor if no windows have input focus). If this edge case is important to cover then this can be addressed by setting input focus to selmon->barwin in the focus function if there is no client to give focus to (rather than giving focus back to the root window). --- config.def.h | 3 +-- dwm.c | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/config.def.h b/config.def.h index 9efa774..061ad66 100644 --- a/config.def.h +++ b/config.def.h @@ -56,8 +56,7 @@ static const Layout layouts[] = { #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } /* commands */ -static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ -static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; +static const char *dmenucmd[] = { "dmenu_run", "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; static const char *termcmd[] = { "st", NULL }; static const Key keys[] = { diff --git a/dwm.c b/dwm.c index 253aba7..e5efb6a 100644 --- a/dwm.c +++ b/dwm.c @@ -1639,8 +1639,6 @@ sigchld(int unused) void spawn(const Arg *arg) { - if (arg->v == dmenucmd) - dmenumon[0] = '0' + selmon->num; if (fork() == 0) { if (dpy) close(ConnectionNumber(dpy)); From 970f37697358574e127019eb0ee2f5725ec05ce0 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 16 Sep 2022 23:06:47 +0200 Subject: [PATCH 11/11] remove workaround for a crash with color emojis on some systems, now fixed in libXft 2.3.5 https://gitlab.freedesktop.org/xorg/lib/libxft/-/blob/libXft-2.3.5/NEWS --- drw.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/drw.c b/drw.c index ced7d37..a58a2b4 100644 --- a/drw.c +++ b/drw.c @@ -133,19 +133,6 @@ xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern) die("no font specified."); } - /* Do not allow using color fonts. This is a workaround for a BadLength - * error from Xft with color glyphs. Modelled on the Xterm workaround. See - * https://bugzilla.redhat.com/show_bug.cgi?id=1498269 - * https://lists.suckless.org/dev/1701/30932.html - * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=916349 - * and lots more all over the internet. - */ - FcBool iscol; - if(FcPatternGetBool(xfont->pattern, FC_COLOR, 0, &iscol) == FcResultMatch && iscol) { - XftFontClose(drw->dpy, xfont); - return NULL; - } - font = ecalloc(1, sizeof(Fnt)); font->xfont = xfont; font->pattern = pattern; @@ -368,7 +355,6 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp fcpattern = FcPatternDuplicate(drw->fonts->pattern); FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue); - FcPatternAddBool(fcpattern, FC_COLOR, FcFalse); FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); FcDefaultSubstitute(fcpattern);