git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit7d8ab13c93fac925203486896d250398b4ddb9c8
parent76011900a7
authorIsaac Freund <[email protected]>
date2020-04-17 19:31
Close layer surfaces on output destroy

 src/layer_surface.zig | 20 +++++++++++---------
 src/output.zig        | 17 +++++++++++++++++
 src/server.zig        |  1 +
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/src/layer_surface.zig b/src/layer_surface.zig
index b9825e7..6aa1b9d 100644
--- a/src/layer_surface.zig
+++ b/src/layer_surface.zig
@@ -58,18 +58,18 @@ pub const LayerSurface = struct {
 
     fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
         const layer_surface = @fieldParentPtr(LayerSurface, "listen_destroy", listener.?);
-        Log.Debug.log("Layer surface '{}' destroyed", .{layer_surface.wlr_layer_surface.namespace});
-
-        const node = @fieldParentPtr(std.TailQueue(LayerSurface).Node, "data", layer_surface);
-        layer_surface.output.layers[@intCast(usize, @enumToInt(layer_surface.layer))].remove(node);
-        layer_surface.output.root.server.allocator.destroy(node);
+        const output = layer_surface.output;
 
-        layer_surface.output.arrangeLayers();
+        Log.Debug.log("Layer surface '{}' destroyed", .{layer_surface.wlr_layer_surface.namespace});
 
         // Remove listeners active the entire lifetime of the layer surface
-        c.wl_list_remove(&layer_surface.listen_destroy);
-        c.wl_list_remove(&layer_surface.listen_map);
-        c.wl_list_remove(&layer_surface.listen_unmap);
+        c.wl_list_remove(&layer_surface.listen_destroy.link);
+        c.wl_list_remove(&layer_surface.listen_map.link);
+        c.wl_list_remove(&layer_surface.listen_unmap.link);
+
+        const node = @fieldParentPtr(std.TailQueue(LayerSurface).Node, "data", layer_surface);
+        output.layers[@intCast(usize, @enumToInt(layer_surface.layer))].remove(node);
+        output.root.server.allocator.destroy(node);
     }
 
     fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
@@ -101,6 +101,8 @@ pub const LayerSurface = struct {
         // remove listeners only active while the layer surface is mapped
         c.wl_list_remove(&layer_surface.listen_commit.link);
         c.wl_list_remove(&layer_surface.listen_new_popup.link);
+
+        layer_surface.output.arrangeLayers();
     }
 
     fn handleCommit(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
diff --git a/src/output.zig b/src/output.zig
index 5751421..e951fe3 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -379,6 +379,8 @@ pub const Output = struct {
         const destroyed_output = @fieldParentPtr(Output, "listen_destroy", listener.?);
         const root = destroyed_output.root;
 
+        Log.Debug.log("Output {} destroyed", .{destroyed_output.wlr_output.name});
+
         // Use the first output in the list that is not the one being destroyed.
         // If there is no other real output, use the noop output.
         var output_it = root.outputs.first;
@@ -395,6 +397,21 @@ pub const Output = struct {
             node.view.output = fallback_output;
         }
 
+        // Close all layer surfaces on the destroyed output
+        for (destroyed_output.layers) |*layer, layer_idx| {
+            while (layer.pop()) |node| {
+                const layer_surface = &node.data;
+                c.wlr_layer_surface_v1_close(layer_surface.wlr_layer_surface);
+                // We need to move the closing layer surface to the noop output
+                // since it is not immediately destoryed. This just a request
+                // to close which will trigger unmap and destroy events in
+                // response, and the LayerSurface needs a valid output to
+                // handle them.
+                root.noop_output.layers[layer_idx].prepend(node);
+                layer_surface.output = &root.noop_output;
+            }
+        }
+
         // If any seat has the destroyed output focused, focus the fallback one
         var seat_it = root.server.input_manager.seats.first;
         while (seat_it) |seat_node| : (seat_it = seat_node.next) {
diff --git a/src/server.zig b/src/server.zig
index a165b5d..df1beb3 100644
--- a/src/server.zig
+++ b/src/server.zig
@@ -131,6 +131,7 @@ pub const Server = struct {
     fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
         const server = @fieldParentPtr(Server, "listen_new_output", listener.?);
         const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
+        Log.Debug.log("New output {}", .{wlr_output.name});
         server.root.addOutput(wlr_output);
     }