Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

switcher fixes

oxydizerc7958f58a6f2
diff --git a/src/Switcher.zig b/src/Switcher.zig
index b08a3ac..d14ee34 100644
--- a/src/Switcher.zig
+++ b/src/Switcher.zig
@@ -289,23 +289,40 @@ pub fn refreshIcon(self: *Self, card: *Card) void {
pub fn updatePreview(card: *Card, tl: *@import("Toplevel.zig")) void {
const preview = card.preview orelse return;
card.pw = 0;
+ if (tl.backend == .shell) {
+ // Settings has no wl_surface: share the body it presents instead.
+ const cc = tl.backend.shell.control_center orelse return clearPreview(preview);
+ const body = cc.buffer_node;
+ if (body.buffer_width <= 0 or body.buffer_height <= 0 or cc.panel_box.width <= 0 or cc.panel_box.height <= 0) return clearPreview(preview);
+ if (!preview_binding.setOwned(preview, body)) return clearPreview(preview);
+ card.source = .{ .x = 0, .y = 0, .width = @floatFromInt(body.buffer_width), .height = @floatFromInt(body.buffer_height) };
+ fit(card, @floatFromInt(cc.panel_box.width), @floatFromInt(cc.panel_box.height));
+ return;
+ }
if (tl.surface()) |surface| {
if (surface.current.transform == .normal and surface.buffer != null) {
if (!preview_binding.setSurface(preview, surface)) return;
surface.getBufferSourceBox(&card.source);
- const sw: f32 = @floatFromInt(@max(1, surface.current.width));
- const sh: f32 = @floatFromInt(@max(1, surface.current.height));
- card.aspect = sw / sh;
- const fit = @min(208 / sw, 140 / sh);
- card.pw = @max(1, @as(i32, @intFromFloat(@round(sw * fit))));
- card.ph = @max(1, @as(i32, @intFromFloat(@round(sh * fit))));
+ fit(card, @floatFromInt(@max(1, surface.current.width)), @floatFromInt(@max(1, surface.current.height)));
return;
}
}
+ clearPreview(preview);
+}
+
+fn clearPreview(preview: *sn.Buffer) void {
_ = preview_binding.setSurface(preview, null);
preview.node.setEnabled(false);
}
+/// Size the linear card's thumbnail for content of `sw`×`sh` logical pixels.
+fn fit(card: *Card, sw: f32, sh: f32) void {
+ card.aspect = sw / sh;
+ const factor = @min(208 / sw, 140 / sh);
+ card.pw = @max(1, @as(i32, @intFromFloat(@round(sw * factor))));
+ card.ph = @max(1, @as(i32, @intFromFloat(@round(sh * factor))));
+}
+
pub fn committed(self: *Self, tl: *@import("Toplevel.zig")) void {
if (!self.active()) return;
for (self.cards.items) |*card| {
diff --git a/src/control_center/paint.zig b/src/control_center/paint.zig
index ac02b1d..7245309 100644
--- a/src/control_center/paint.zig
+++ b/src/control_center/paint.zig
@@ -193,6 +193,7 @@ pub fn paintContent(cc: *ControlCenter) void {
_ = cc.server.world.projection.presentOwned(cc.buffer_node, cc.server.renderer, &buf.base, if (full) null else &region);
cc.buffer_node.setDestSize(cc.panel_box.width, cc.panel_box.height);
buf.base.drop();
+ cc.server.switcher.committed(cc.toplevel);
panel_present.addPaint(elapsed_ns);
cc.painted_scale = scale;
cc.dirty = false;
diff --git a/src/switcher/preview.zig b/src/switcher/preview.zig
index 9fb3632..e85609d 100644
--- a/src/switcher/preview.zig
+++ b/src/switcher/preview.zig
@@ -8,3 +8,9 @@ extern fn rediwm_switcher_preview_set_surface(preview: *sn.Buffer, surface: ?*wl
/// Refresh on every source commit, and pass null to clear. The preview node owns
/// the binding; its destruction or the source's destruction detaches it safely.
pub const setSurface = rediwm_switcher_preview_set_surface;
+
+extern fn rediwm_switcher_preview_set_owned(preview: *sn.Buffer, source: *sn.Buffer) bool;
+
+/// Mirror a compositor-owned buffer node (a `.shell` window's body). Call after
+/// each present; false when the source has no buffer to share.
+pub const setOwned = rediwm_switcher_preview_set_owned;
diff --git a/src/switcher_preview.c b/src/switcher_preview.c
index 4aac5df..505860c 100644
--- a/src/switcher_preview.c
+++ b/src/switcher_preview.c
@@ -70,32 +70,68 @@ static void handle_output_sample(struct wl_listener *listener, void *data) {
event->release_timeline, event->release_point, event->output->output->event_loop);
}
+static struct preview_binding *find_binding(struct rediwm_buffer *preview) {
+ struct wlr_addon *addon = wlr_addon_find(&preview->node.addons, NULL, &binding_impl);
+ struct preview_binding *binding;
+ return addon ? wl_container_of(addon, binding, addon) : NULL;
+}
+
+static struct preview_binding *create_binding(struct rediwm_buffer *preview) {
+ struct preview_binding *binding = calloc(1, sizeof(*binding));
+ if (!binding) return NULL;
+ binding->preview = preview;
+ wlr_addon_init(&binding->addon, &preview->node.addons, NULL, &binding_impl);
+ wl_list_init(&binding->surface_destroy.link);
+ binding->node_destroy.notify = handle_node_destroy;
+ wl_signal_add(&preview->node.events.destroy, &binding->node_destroy);
+ binding->surface_destroy.notify = handle_surface_destroy;
+ binding->output_sample.notify = handle_output_sample;
+ wl_signal_add(&preview->events.output_sample, &binding->output_sample);
+ return binding;
+}
+
+static void follow_surface(struct preview_binding *binding, struct wlr_surface *surface) {
+ if (binding->surface == surface) return;
+ wl_list_remove(&binding->surface_destroy.link);
+ wl_list_init(&binding->surface_destroy.link);
+ binding->surface = surface;
+ if (surface) wl_signal_add(&surface->events.destroy, &binding->surface_destroy);
+}
+
+bool rediwm_switcher_preview_set_owned(struct rediwm_buffer *preview,
+ struct rediwm_buffer *source) {
+ struct wlr_buffer *buffer = source ? source->buffer : NULL;
+ if (!buffer) {
+ rediwm_switcher_preview_set_surface(preview, NULL);
+ return false;
+ }
+ struct preview_binding *binding = find_binding(preview);
+ if (!binding && !(binding = create_binding(preview))) return false;
+ follow_surface(binding, NULL);
+ unmark_buffer(binding);
+ // `rediwm_projection_present_owned` updates this client buffer in place while
+ // nothing but ignorable locks hold it; the thumbnail's lock must stay one of them.
+ struct wlr_client_buffer *client = wlr_client_buffer_get(buffer);
+ if (client) {
+ client->private.n_ignore_locks++;
+ binding->marked = client;
+ }
+ rediwm_buffer_set_buffer(preview, buffer);
+ return true;
+}
+
bool rediwm_switcher_preview_set_surface(struct rediwm_buffer *preview,
struct wlr_surface *surface) {
- struct wlr_addon *addon = wlr_addon_find(&preview->node.addons, NULL, &binding_impl);
- struct preview_binding *binding = addon ? wl_container_of(addon, binding, addon) : NULL;
+ struct preview_binding *binding = find_binding(preview);
if (!binding) {
if (!surface) {
rediwm_buffer_set_buffer(preview, NULL);
return true;
}
- binding = calloc(1, sizeof(*binding));
+ binding = create_binding(preview);
if (!binding) return false;
- binding->preview = preview;
- wlr_addon_init(&binding->addon, &preview->node.addons, NULL, &binding_impl);
- wl_list_init(&binding->surface_destroy.link);
- binding->node_destroy.notify = handle_node_destroy;
- wl_signal_add(&preview->node.events.destroy, &binding->node_destroy);
- binding->surface_destroy.notify = handle_surface_destroy;
- binding->output_sample.notify = handle_output_sample;
- wl_signal_add(&preview->events.output_sample, &binding->output_sample);
- }
- if (binding->surface != surface) {
- wl_list_remove(&binding->surface_destroy.link);
- wl_list_init(&binding->surface_destroy.link);
- binding->surface = surface;
- if (surface) wl_signal_add(&surface->events.destroy, &binding->surface_destroy);
}
+ follow_surface(binding, surface);
unmark_buffer(binding);
struct wlr_client_buffer *buffer = surface ? surface->buffer : NULL;
struct rediwm_buffer_options options = {0};
diff --git a/src/switcher_preview.h b/src/switcher_preview.h
index 56fb18a..e85d1f0 100644
--- a/src/switcher_preview.h
+++ b/src/switcher_preview.h
@@ -9,3 +9,8 @@ struct wlr_surface;
// bound, replace/clear content only through this helper. Geometry stays caller-owned.
// The node owns the binding and a destroyed surface clears its content.
bool rediwm_switcher_preview_set_surface(struct rediwm_buffer *, struct wlr_surface *);
+
+// Show what a compositor-owned buffer node (`rediwm_projection_present_owned`)
+// presents, keeping its in-place texture updates possible. Call after each
+// present; false (and no content) when the source holds no buffer.
+bool rediwm_switcher_preview_set_owned(struct rediwm_buffer *, struct rediwm_buffer *source);