Restore floating window position on monitor reattach

This commit is contained in:
6z7y 2026-04-27 18:43:09 +03:00
parent 2d4463dd83
commit 83745cf12b
2 changed files with 53 additions and 36 deletions

View File

@ -6,3 +6,4 @@ Moves clients to their old output when it is reattached.
- [2024-04-07](https://codeberg.org/dwl/dwl-patches/raw/branch/main/patches/restore-monitor/restore-monitor.patch) - [2024-04-07](https://codeberg.org/dwl/dwl-patches/raw/branch/main/patches/restore-monitor/restore-monitor.patch)
### Authors ### Authors
- [eyusupov](https://codeberg.org/eyusupov) - [eyusupov](https://codeberg.org/eyusupov)
- [6z7y](https://codeberg.org/6z7y)

View File

@ -1,25 +1,40 @@
From e42ca1c539437d3098d80983cfe2ad6f938d7a08 Mon Sep 17 00:00:00 2001 From e42ca1c539437d3098d80983cfe2ad6f938d7a08 Mon Sep 17 00:00:00 2001
From: Eldar Yusupov <eyusupov@gmail.com> From: Eldar Yusupov <eyusupov@gmail.com>
Date: Sun, 17 Mar 2024 19:12:29 +0300 Date: Sun, 17 Mar 2024 19:12:29 +0300
Subject: [PATCH] Restore correct monitor for client when it is reattached Subject: [PATCH] Restore correct monitor and floating position when reattached
--- ---
dwl.c | 24 ++++++++++++++++++++++-- dwl.c | 33 ++++++++++++++++++++++++++++++---
1 file changed, 22 insertions(+), 2 deletions(-) 1 file changed, 30 insertions(+), 3 deletions(-)
diff --git a/dwl.c b/dwl.c diff --git a/dwl.c b/dwl.c
index bf763df..d8d8139 100644 index 44f3ad9..41548cd 100644
--- a/dwl.c --- a/dwl.c
+++ b/dwl.c +++ b/dwl.c
@@ -107,6 +107,7 @@ typedef struct { @@ -106,6 +106,8 @@ typedef struct {
unsigned int type; /* XDGShell or X11* */ unsigned int type; /* XDGShell or X11* */
struct wlr_box geom; /* layout-relative, includes border */
Monitor *mon; Monitor *mon;
+ char *output; + char *output;
+ struct wlr_box floatgeom; /* saved geom for floating restore after monitor reconnect */
struct wlr_scene_tree *scene; struct wlr_scene_tree *scene;
struct wlr_scene_rect *border[4]; /* top, bottom, left, right */ struct wlr_scene_rect *border[4]; /* top, bottom, left, right */
struct wlr_scene_tree *scene_surface; struct wlr_scene_tree *scene_surface;
@@ -869,6 +870,7 @@ createmon(struct wl_listener *listener, void *data) @@ -807,6 +809,13 @@ closemon(Monitor *m)
}
wl_list_for_each(c, &clients, link) {
+
+ /* Save floating geom now, before destroymon modifies it below.
+ * destroymon subtracts m->w.width from c->geom.x for floating
+ * windows, which corrupts the layout-absolute position we need
+ * to restore on reconnect. */
+ if (c->isfloating && c->mon == m) c->floatgeom = c->geom;
+
if (c->isfloating && c->geom.x > m->m.width)
resize(c, (struct wlr_box){.x = c->geom.x - m->w.width, .y = c->geom.y,
.width = c->geom.width, .height = c->geom.height}, 0);
@@ -1045,6 +1054,7 @@ createmon(struct wl_listener *listener, void *data)
size_t i; size_t i;
struct wlr_output_state state; struct wlr_output_state state;
Monitor *m; Monitor *m;
@ -27,53 +42,54 @@ index bf763df..d8d8139 100644
if (!wlr_output_init_render(wlr_output, alloc, drw)) if (!wlr_output_init_render(wlr_output, alloc, drw))
return; return;
@@ -938,6 +940,13 @@ createmon(struct wl_listener *listener, void *data) @@ -1114,6 +1124,15 @@ createmon(struct wl_listener *listener, void *data)
wlr_output_layout_add_auto(output_layout, wlr_output); wlr_output_layout_add_auto(output_layout, wlr_output);
else else
wlr_output_layout_add(output_layout, wlr_output, m->m.x, m->m.y); wlr_output_layout_add(output_layout, wlr_output, m->m.x, m->m.y);
+ +
+ wl_list_for_each(c, &clients, link) { + wl_list_for_each(c, &clients, link) {
+ if (strcmp(wlr_output->name, c->output) == 0) { + if (!c->output || strcmp(wlr_output->name, c->output) != 0)
+ continue;
+ c->mon = m; + c->mon = m;
+ } + if (c->isfloating)
+ resize(c, c->floatgeom, 0);
+ } + }
+ updatemons(NULL, NULL); + updatemons(NULL, NULL);
} }
void void
@@ -1186,6 +1195,7 @@ destroynotify(struct wl_listener *listener, void *data) @@ -1347,7 +1366,8 @@ destroynotify(struct wl_listener *listener, void *data)
wl_list_remove(&c->map.link);
wl_list_remove(&c->unmap.link); wl_list_remove(&c->unmap.link);
wl_list_remove(&c->maximize.link);
} }
- free(c);
+ free(c->output); + free(c->output);
free(c); + free(c);
} }
@@ -1618,6 +1628,10 @@ mapnotify(struct wl_listener *listener, void *data) void
@@ -1792,6 +1812,9 @@ mapnotify(struct wl_listener *listener, void *data)
} else { } else {
applyrules(c); applyrules(c);
} }
+ c->output = strdup(c->mon->wlr_output->name); + c->output = strdup(c->mon->wlr_output->name);
+ if (c->output == NULL) { + if (c->output == NULL) die("oom");
+ die("oom"); +
+ }
printstatus(); printstatus();
unset_fullscreen: unset_fullscreen:
@@ -2565,8 +2579,14 @@ void @@ -2705,8 +2728,12 @@ void
tagmon(const Arg *arg) tagmon(const Arg *arg)
{ {
Client *sel = focustop(selmon); Client *sel = focustop(selmon);
- if (sel) - if (sel)
- setmon(sel, dirtomon(arg->i), 0); - setmon(sel, dirtomon(arg->i), 0);
+ if (!sel) + if (!sel) return;
+ return; +
+ setmon(sel, dirtomon(arg->i), 0); + setmon(sel, dirtomon(arg->i), 0);
+ free(sel->output); + free(sel->output);
+ sel->output = strdup(sel->mon->wlr_output->name); + sel->output = strdup(sel->mon->wlr_output->name);
+ if (sel->output == NULL) { + if (sel->output == NULL) die("oom");
+ die("oom");
+ }
} }
void void