Update monitor positions also on removal d93ff488
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.
Santtu Lakkala · 2022-02-21 16:58 1 file(s) · +34 −34
dwm.c +34 −34
1876 1876
				memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1877 1877
		XFree(info);
1878 1878
		nn = j;
1879 -
		if (n <= nn) { /* new monitors available */
1880 -
			for (i = 0; i < (nn - n); i++) {
1881 -
				for (m = mons; m && m->next; m = m->next);
1882 -
				if (m)
1883 -
					m->next = createmon();
1884 -
				else
1885 -
					mons = createmon();
1879 +
1880 +
		/* new monitors if nn > n */
1881 +
		for (i = n; i < nn; i++) {
1882 +
			for (m = mons; m && m->next; m = m->next);
1883 +
			if (m)
1884 +
				m->next = createmon();
1885 +
			else
1886 +
				mons = createmon();
1887 +
		}
1888 +
		for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1889 +
			if (i >= n
1890 +
			|| unique[i].x_org != m->mx || unique[i].y_org != m->my
1891 +
			|| unique[i].width != m->mw || unique[i].height != m->mh)
1892 +
			{
1893 +
				dirty = 1;
1894 +
				m->num = i;
1895 +
				m->mx = m->wx = unique[i].x_org;
1896 +
				m->my = m->wy = unique[i].y_org;
1897 +
				m->mw = m->ww = unique[i].width;
1898 +
				m->mh = m->wh = unique[i].height;
1899 +
				updatebarpos(m);
1886 1900
			}
1887 -
			for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1888 -
				if (i >= n
1889 -
				|| unique[i].x_org != m->mx || unique[i].y_org != m->my
1890 -
				|| unique[i].width != m->mw || unique[i].height != m->mh)
1891 -
				{
1892 -
					dirty = 1;
1893 -
					m->num = i;
1894 -
					m->mx = m->wx = unique[i].x_org;
1895 -
					m->my = m->wy = unique[i].y_org;
1896 -
					m->mw = m->ww = unique[i].width;
1897 -
					m->mh = m->wh = unique[i].height;
1898 -
					updatebarpos(m);
1899 -
				}
1900 -
		} else { /* less monitors available nn < n */
1901 -
			for (i = nn; i < n; i++) {
1902 -
				for (m = mons; m && m->next; m = m->next);
1903 -
				while ((c = m->clients)) {
1904 -
					dirty = 1;
1905 -
					m->clients = c->next;
1906 -
					detachstack(c);
1907 -
					c->mon = mons;
1908 -
					attach(c);
1909 -
					attachstack(c);
1910 -
				}
1911 -
				if (m == selmon)
1912 -
					selmon = mons;
1913 -
				cleanupmon(m);
1901 +
		/* removed monitors if n > nn */
1902 +
		for (i = nn; i < n; i++) {
1903 +
			for (m = mons; m && m->next; m = m->next);
1904 +
			while ((c = m->clients)) {
1905 +
				dirty = 1;
1906 +
				m->clients = c->next;
1907 +
				detachstack(c);
1908 +
				c->mon = mons;
1909 +
				attach(c);
1910 +
				attachstack(c);
1914 1911
			}
1912 +
			if (m == selmon)
1913 +
				selmon = mons;
1914 +
			cleanupmon(m);
1915 1915
		}
1916 1916
		free(unique);
1917 1917
	} else