Update monitor positions also on removal
authorSanttu Lakkala <inz@inz.fi>
Mon, 21 Feb 2022 14:58:28 +0000 (16:58 +0200)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Sat, 16 Apr 2022 14:59:03 +0000 (16:59 +0200)
When monitors are removed, the coordinates of existing monitors may
change, if the removed monitors had smaller coordinates than the
remaining ones.

Remove special case handling so that the same update-if-necessary loop
is run also in the case when monitors are removed.

dwm.c

diff --git a/dwm.c b/dwm.c
index d8075adcf7fffd1fb200858e306cae89db18f2ba..0fc328a583c25817a5028a32b07130496c950f32 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -1876,42 +1876,42 @@ updategeom(void)
                                memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
                XFree(info);
                nn = j;
-               if (n <= nn) { /* new monitors available */
-                       for (i = 0; i < (nn - n); i++) {
-                               for (m = mons; m && m->next; m = m->next);
-                               if (m)
-                                       m->next = createmon();
-                               else
-                                       mons = createmon();
+
+               /* new monitors if nn > n */
+               for (i = n; i < nn; i++) {
+                       for (m = mons; m && m->next; m = m->next);
+                       if (m)
+                               m->next = createmon();
+                       else
+                               mons = createmon();
+               }
+               for (i = 0, m = mons; i < nn && m; m = m->next, i++)
+                       if (i >= n
+                       || unique[i].x_org != m->mx || unique[i].y_org != m->my
+                       || unique[i].width != m->mw || unique[i].height != m->mh)
+                       {
+                               dirty = 1;
+                               m->num = i;
+                               m->mx = m->wx = unique[i].x_org;
+                               m->my = m->wy = unique[i].y_org;
+                               m->mw = m->ww = unique[i].width;
+                               m->mh = m->wh = unique[i].height;
+                               updatebarpos(m);
                        }
-                       for (i = 0, m = mons; i < nn && m; m = m->next, i++)
-                               if (i >= n
-                               || unique[i].x_org != m->mx || unique[i].y_org != m->my
-                               || unique[i].width != m->mw || unique[i].height != m->mh)
-                               {
-                                       dirty = 1;
-                                       m->num = i;
-                                       m->mx = m->wx = unique[i].x_org;
-                                       m->my = m->wy = unique[i].y_org;
-                                       m->mw = m->ww = unique[i].width;
-                                       m->mh = m->wh = unique[i].height;
-                                       updatebarpos(m);
-                               }
-               } else { /* less monitors available nn < n */
-                       for (i = nn; i < n; i++) {
-                               for (m = mons; m && m->next; m = m->next);
-                               while ((c = m->clients)) {
-                                       dirty = 1;
-                                       m->clients = c->next;
-                                       detachstack(c);
-                                       c->mon = mons;
-                                       attach(c);
-                                       attachstack(c);
-                               }
-                               if (m == selmon)
-                                       selmon = mons;
-                               cleanupmon(m);
+               /* removed monitors if n > nn */
+               for (i = nn; i < n; i++) {
+                       for (m = mons; m && m->next; m = m->next);
+                       while ((c = m->clients)) {
+                               dirty = 1;
+                               m->clients = c->next;
+                               detachstack(c);
+                               c->mon = mons;
+                               attach(c);
+                               attachstack(c);
                        }
+                       if (m == selmon)
+                               selmon = mons;
+                       cleanupmon(m);
                }
                free(unique);
        } else