From 15c63e8334f8256959eb58eb1a87d7d28572c907 Mon Sep 17 00:00:00 2001 From: Joshua Holmes Date: Tue, 19 Nov 2024 19:36:51 -0800 Subject: [PATCH] core: implement `Linux.setTitle()` --- src/core/Linux.zig | 22 +++++++++++++--------- src/core/linux/Wayland.zig | 14 ++++++++++---- src/core/linux/X11.zig | 4 ++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/core/Linux.zig b/src/core/Linux.zig index 6cc135b2..8f0f2dac 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -49,8 +49,8 @@ backend: Backend, // these arrays are used as info messages to the user that some features are missing // please keep these up to date until we can remove them -const MISSING_FEATURES_X11 = [_][]const u8{ "Resizing window", "Changing display mode", "VSync", "Setting window border/title/cursor" }; -const MISSING_FEATURES_WAYLAND = [_][]const u8{ "Changing display mode", "VSync", "Setting window border/title/cursor" }; +const MISSING_FEATURES_X11 = [_][]const u8{ "Resizing window", "Changing display mode", "VSync", "Setting window border/cursor" }; +const MISSING_FEATURES_WAYLAND = [_][]const u8{ "Changing display mode", "VSync", "Setting window border/cursor" }; pub fn init( linux: *Linux, @@ -128,8 +128,6 @@ pub fn init( // warn about incomplete features // TODO: remove this when linux is not missing major features try warnAboutIncompleteFeatures(linux.backend, &MISSING_FEATURES_X11, &MISSING_FEATURES_WAYLAND, options.allocator); - - return; } pub fn deinit(linux: *Linux) void { @@ -141,8 +139,6 @@ pub fn deinit(linux: *Linux) void { .wayland => linux.backend.wayland.deinit(linux), .x11 => linux.backend.x11.deinit(linux), } - - return; } pub fn update(linux: *Linux) !void { @@ -150,11 +146,19 @@ pub fn update(linux: *Linux) !void { .wayland => try linux.backend.wayland.update(linux), .x11 => try linux.backend.x11.update(linux), } - return; } -pub fn setTitle(_: *Linux, _: [:0]const u8) void { - return; +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), + } } pub fn setDisplayMode(_: *Linux, _: DisplayMode) void { diff --git a/src/core/linux/Wayland.zig b/src/core/linux/Wayland.zig index dae420fd..4b13c2a9 100644 --- a/src/core/linux/Wayland.zig +++ b/src/core/linux/Wayland.zig @@ -52,6 +52,7 @@ configured: bool = false, display: *c.wl_display, surface: *c.wl_surface, +toplevel: *c.xdg_toplevel, interfaces: Interfaces, libwaylandclient: LibWaylandClient, @@ -99,6 +100,7 @@ pub fn init( }, .surface_descriptor = undefined, .surface = undefined, + .toplevel = undefined, }, }; var wl = &linux.backend.wayland; @@ -138,13 +140,13 @@ pub fn init( } const xdg_surface = c.xdg_wm_base_get_xdg_surface(wl.interfaces.xdg_wm_base, wl.surface) orelse return error.UnableToCreateXdgSurface; - const toplevel = c.xdg_surface_get_toplevel(xdg_surface) orelse return error.UnableToGetXdgTopLevel; + wl.toplevel = c.xdg_surface_get_toplevel(xdg_surface) orelse return error.UnableToGetXdgTopLevel; // TODO: handle this return value _ = c.xdg_surface_add_listener(xdg_surface, &xdg_surface_listener.listener, linux); // TODO: handle this return value - _ = c.xdg_toplevel_add_listener(toplevel, &xdg_toplevel_listener.listener, linux); + _ = c.xdg_toplevel_add_listener(wl.toplevel, &xdg_toplevel_listener.listener, linux); // Commit changes to surface c.wl_surface_commit(wl.surface); @@ -153,11 +155,11 @@ pub fn init( // This space intentionally left blank } - c.xdg_toplevel_set_title(toplevel, options.title); + c.xdg_toplevel_set_title(wl.toplevel, options.title); const decoration = c.zxdg_decoration_manager_v1_get_toplevel_decoration( wl.interfaces.zxdg_decoration_manager_v1, - toplevel, + wl.toplevel, ) orelse return error.UnableToGetToplevelDecoration; c.zxdg_toplevel_decoration_v1_set_mode(decoration, c.ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); @@ -204,6 +206,10 @@ pub fn update(wl: *Wayland, linux: *Linux) !void { wl.core.input.tick(); } +pub fn setTitle(wl: *Wayland, title: [:0]const u8) void { + c.xdg_toplevel_set_title(wl.toplevel, title); +} + const LibXkbCommon = struct { handle: std.DynLib, diff --git a/src/core/linux/X11.zig b/src/core/linux/X11.zig index d8a626b0..7c94dbb0 100644 --- a/src/core/linux/X11.zig +++ b/src/core/linux/X11.zig @@ -265,6 +265,10 @@ pub fn update(x11: *X11, linux: *Linux) !void { x11.core.input.tick(); } +pub fn setTitle(x11: *X11, title: [:0]const u8) void { + _ = x11.libx11.XStoreName(x11.display, x11.window, title); +} + const LibX11 = struct { handle: std.DynLib, XInitThreads: *const @TypeOf(c.XInitThreads),