From f39d47e49c5548f35429b0a192dd3a7c9b5689e9 Mon Sep 17 00:00:00 2001 From: Joshua Holmes Date: Fri, 20 Dec 2024 07:16:39 +0000 Subject: [PATCH] linux: allow title to be changed again --- src/core/Linux.zig | 26 ++++++++++++++++---------- src/core/linux/Wayland.zig | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/core/Linux.zig b/src/core/Linux.zig index f9901cb8..21bb2f22 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -63,6 +63,7 @@ pub fn tick(core: *Core) !void { while (windows.next()) |window_id| { const native_opt: ?Native = core.windows.get(window_id, .native); if (native_opt) |native| { + check_for_mach_updates(core, window_id); // check for display server events switch (native) { .x11 => try X11.tick(window_id), @@ -146,16 +147,10 @@ pub fn update(linux: *Linux) !void { } } -pub fn setTitle(linux: *Linux, title: [:0]const u8) void { - const new_title = linux.allocator.dupeZ(u8, title) catch { - log.err("Failed to reallocate memory for new window title", .{}); - return; - }; - linux.allocator.free(linux.title); - linux.title = new_title; - switch (linux.backend) { - .wayland => linux.backend.wayland.setTitle(linux.title), - .x11 => linux.backend.x11.setTitle(linux.title), +fn setTitle(native: *const Native, title: [:0]const u8) void { + switch (native.*) { + .wayland => |wl| Wayland.setTitle(&wl, title), + .x11 => |x| X11.setTitle(&x, title), } } @@ -192,6 +187,17 @@ pub fn setCursorShape(_: *Linux, _: CursorShape) void { return; } +/// Checks for updates in mach object fields. Does nothing if window is not initialized. +fn check_for_mach_updates(core: *Core, window_id: mach.ObjectID) void { + const core_window = core.windows.getValue(window_id); + const native = &core_window.native; + if (native.*) |n| { + if (core.windows.updated(window_id, .title)) { + setTitle(&n, core_window.title); + } + } +} + /// Check if gamemode should be activated pub fn wantGamemode(allocator: std.mem.Allocator) error{ OutOfMemory, InvalidWtf8 }!bool { const use_gamemode = std.process.getEnvVarOwned( diff --git a/src/core/linux/Wayland.zig b/src/core/linux/Wayland.zig index 8da93b8d..87c8cb8f 100644 --- a/src/core/linux/Wayland.zig +++ b/src/core/linux/Wayland.zig @@ -243,7 +243,7 @@ pub fn tick(window_id: mach.ObjectID) !void { _ = libwaylandclient.wl_display_roundtrip(wl.display); } -pub fn setTitle(wl: *Wayland, title: [:0]const u8) void { +pub fn setTitle(wl: *const Native, title: [:0]const u8) void { c.xdg_toplevel_set_title(wl.toplevel, title); }