diff --git a/src/core/Linux.zig b/src/core/Linux.zig index fde006f2..e6641efd 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -33,6 +33,7 @@ const Backend = union(BackendEnum) { pub const Linux = @This(); allocator: std.mem.Allocator, +title: [:0]const u8, display_mode: DisplayMode, vsync_mode: VSyncMode, @@ -63,9 +64,12 @@ pub fn init( linux.refresh_rate = 60; // TODO: set to something meaningful linux.vsync_mode = .triple; linux.size = options.size; + linux.title = try options.allocator.dupeZ(u8, options.title); + linux.border = options.border; + linux.cursor_mode = .normal; + linux.cursor_shape = .arrow; if (!options.headless) { - // TODO: this function does nothing right now - setDisplayMode(linux, options.display_mode); + linux.display_mode = options.display_mode; } const desired_backend: BackendEnum = blk: { @@ -130,6 +134,9 @@ pub fn init( pub fn deinit(linux: *Linux) void { if (linux.gamemode != null and linux.gamemode.?) deinitLinuxGamemode(); + + linux.allocator.free(linux.title); + switch (linux.backend) { .wayland => linux.backend.wayland.deinit(linux), .x11 => linux.backend.x11.deinit(linux), @@ -140,8 +147,8 @@ pub fn deinit(linux: *Linux) void { pub fn update(linux: *Linux) !void { switch (linux.backend) { - .wayland => try linux.backend.wayland.update(), - .x11 => try linux.backend.x11.update(), + .wayland => try linux.backend.wayland.update(linux), + .x11 => try linux.backend.x11.update(linux), } return; } diff --git a/src/core/linux/Wayland.zig b/src/core/linux/Wayland.zig index 917af5f0..c3db2829 100644 --- a/src/core/linux/Wayland.zig +++ b/src/core/linux/Wayland.zig @@ -47,8 +47,6 @@ export fn wl_proxy_destroy(proxy: ?*c.struct_wl_proxy) void { state: *Core, core: *Core, -title: [:0]const u8, -size: *Core.Size, surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface, configured: bool = false, @@ -85,8 +83,6 @@ pub fn init( .libwaylandclient = libwaylandclient_global, .interfaces = Interfaces{}, .display = libwaylandclient_global.wl_display_connect(null) orelse return error.FailedToConnectToDisplay, - .title = try options.allocator.dupeZ(u8, options.title), - .size = &linux.size, .modifiers = .{ .alt = false, .caps_lock = false, @@ -137,8 +133,8 @@ pub fn init( region, 0, 0, - @intCast(wl.size.width), - @intCast(wl.size.height), + @intCast(linux.size.width), + @intCast(linux.size.height), ); c.wl_surface_set_opaque_region(wl.surface, region); c.wl_region_destroy(region); @@ -160,7 +156,7 @@ pub fn init( // This space intentionally left blank } - c.xdg_toplevel_set_title(toplevel, wl.title); + c.xdg_toplevel_set_title(toplevel, options.title); const decoration = c.zxdg_decoration_manager_v1_get_toplevel_decoration( wl.interfaces.zxdg_decoration_manager_v1, @@ -182,7 +178,9 @@ pub fn deinit( linux.allocator.destroy(wl.surface_descriptor); } -pub fn update(wl: *Wayland) !void { +pub fn update(wl: *Wayland, linux: *Linux) !void { + _ = linux; + while (wl.libwaylandclient.wl_display_flush(wl.display) == -1) { if (std.posix.errno(-1) == std.posix.E.AGAIN) { log.err("flush error", .{}); @@ -754,7 +752,7 @@ const xdg_surface_listener = struct { wl.configured = true; } - setContentAreaOpaque(wl, wl.size.*); + setContentAreaOpaque(wl, linux.size); } const listener = c.xdg_surface_listener{ .configure = @ptrCast(&xdgSurfaceHandleConfigure) }; @@ -769,10 +767,9 @@ const xdg_toplevel_listener = struct { fn xdgToplevelHandleConfigure(linux: *Linux, toplevel: ?*c.struct_xdg_toplevel, width: i32, height: i32, states: [*c]c.struct_wl_array) callconv(.C) void { _ = toplevel; _ = states; - const wl = &linux.backend.wayland; if (width > 0 and height > 0) { - wl.size.* = .{ .width = @intCast(width), .height = @intCast(height) }; + linux.size = .{ .width = @intCast(width), .height = @intCast(height) }; } } diff --git a/src/core/linux/X11.zig b/src/core/linux/X11.zig index 9486a375..4e686a0e 100644 --- a/src/core/linux/X11.zig +++ b/src/core/linux/X11.zig @@ -43,8 +43,6 @@ libxcursor: ?LibXCursor, libxkbcommon: LibXkbCommon, gl_ctx: ?*LibGL.Context, display: *c.Display, -width: c_int, -height: c_int, empty_event_pipe: [2]std.c.fd_t, wm_protocols: c.Atom, wm_delete_window: c.Atom, @@ -59,21 +57,12 @@ net_wm_window_type_dock: c.Atom, root_window: c.Window, window: c.Window, backend_type: gpu.BackendType, -refresh_rate: u32, hidden_cursor: c.Cursor, // Mutable fields only used by main thread cursors: [@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor, // Mutable state fields; read/write by any thread -title: [:0]const u8, -display_mode: DisplayMode = .windowed, -vsync_mode: VSyncMode = .triple, -border: bool, -headless: bool, -size: *Core.Size, -cursor_mode: CursorMode = .normal, -cursor_shape: CursorShape = .arrow, surface_descriptor: *gpu.Surface.DescriptorFromXlibWindow, pub fn init( @@ -139,7 +128,7 @@ pub fn init( }; const blank_pixmap = libx11.XCreatePixmap(display, window, 1, 1, 1); var color = c.XColor{}; - const refresh_rate: u16 = blk: { + linux.refresh_rate = blk: { if (libxrr != null) { const conf = libxrr.?.XRRGetScreenInfo(display, root_window); break :blk @intCast(libxrr.?.XRRConfigCurrentRate(conf)); @@ -162,8 +151,6 @@ pub fn init( .libxrr = libxrr, .empty_event_pipe = try std.posix.pipe(), .gl_ctx = null, - .width = window_attrs.width, - .height = window_attrs.height, .wm_protocols = libx11.XInternAtom(display, "WM_PROTOCOLS", c.False), .wm_delete_window = libx11.XInternAtom(display, "WM_DELETE_WINDOW", c.False), .net_wm_ping = libx11.XInternAtom(display, "NET_WM_PING", c.False), @@ -178,12 +165,6 @@ pub fn init( .window = window, .hidden_cursor = libx11.XCreatePixmapCursor(display, blank_pixmap, blank_pixmap, &color, &color, 0, 0), .backend_type = try Core.detectBackendType(options.allocator), - .refresh_rate = refresh_rate, - .title = options.title, - .display_mode = .windowed, - .border = options.border, - .headless = options.headless, - .size = &linux.size, .cursors = std.mem.zeroes([@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor), .surface_descriptor = surface_descriptor, .libxkbcommon = try LibXkbCommon.load(), @@ -272,11 +253,11 @@ pub fn deinit( } // Called on the main thread -pub fn update(x11: *X11) !void { +pub fn update(x11: *X11, linux: *Linux) !void { while (c.QLength(x11.display) != 0) { var event: c.XEvent = undefined; _ = x11.libx11.XNextEvent(x11.display, &event); - x11.processEvent(&event); + x11.processEvent(linux, &event); } _ = x11.libx11.XFlush(x11.display); @@ -508,7 +489,7 @@ fn getCursorPos(x11: *X11) Position { return .{ .x = @floatFromInt(cursor_x), .y = @floatFromInt(cursor_y) }; } -fn processEvent(x11: *X11, event: *c.XEvent) void { +fn processEvent(x11: *X11, linux: *Linux, event: *c.XEvent) void { switch (event.type) { c.KeyPress, c.KeyRelease => { // TODO: key repeat event @@ -607,16 +588,16 @@ fn processEvent(x11: *X11, event: *c.XEvent) void { x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } }); }, c.ConfigureNotify => { - if (event.xconfigure.width != x11.size.width or - event.xconfigure.height != x11.size.height) + if (event.xconfigure.width != linux.size.width or + event.xconfigure.height != linux.size.height) { - x11.size.width = @intCast(event.xconfigure.width); - x11.size.height = @intCast(event.xconfigure.height); + linux.size.width = @intCast(event.xconfigure.width); + linux.size.height = @intCast(event.xconfigure.height); x11.core.swap_chain_update.set(); x11.state.pushEvent(.{ .framebuffer_resize = .{ - .width = x11.size.width, - .height = x11.size.height, + .width = linux.size.width, + .height = linux.size.height, }, }); }