core: remove redundant management of several properties in Linux

Properties include title, size, border, cursor_mode, cursor_shape, display_mode, refresh_rate
This commit is contained in:
Joshua Holmes 2024-11-16 15:36:26 -08:00 committed by Emi Gutekanst
parent f90fb1170b
commit a1dfaa2032
3 changed files with 29 additions and 44 deletions

View file

@ -33,6 +33,7 @@ const Backend = union(BackendEnum) {
pub const Linux = @This(); pub const Linux = @This();
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
title: [:0]const u8,
display_mode: DisplayMode, display_mode: DisplayMode,
vsync_mode: VSyncMode, vsync_mode: VSyncMode,
@ -63,9 +64,12 @@ pub fn init(
linux.refresh_rate = 60; // TODO: set to something meaningful linux.refresh_rate = 60; // TODO: set to something meaningful
linux.vsync_mode = .triple; linux.vsync_mode = .triple;
linux.size = options.size; 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) { if (!options.headless) {
// TODO: this function does nothing right now linux.display_mode = options.display_mode;
setDisplayMode(linux, options.display_mode);
} }
const desired_backend: BackendEnum = blk: { const desired_backend: BackendEnum = blk: {
@ -130,6 +134,9 @@ pub fn init(
pub fn deinit(linux: *Linux) void { pub fn deinit(linux: *Linux) void {
if (linux.gamemode != null and linux.gamemode.?) deinitLinuxGamemode(); if (linux.gamemode != null and linux.gamemode.?) deinitLinuxGamemode();
linux.allocator.free(linux.title);
switch (linux.backend) { switch (linux.backend) {
.wayland => linux.backend.wayland.deinit(linux), .wayland => linux.backend.wayland.deinit(linux),
.x11 => linux.backend.x11.deinit(linux), .x11 => linux.backend.x11.deinit(linux),
@ -140,8 +147,8 @@ pub fn deinit(linux: *Linux) void {
pub fn update(linux: *Linux) !void { pub fn update(linux: *Linux) !void {
switch (linux.backend) { switch (linux.backend) {
.wayland => try linux.backend.wayland.update(), .wayland => try linux.backend.wayland.update(linux),
.x11 => try linux.backend.x11.update(), .x11 => try linux.backend.x11.update(linux),
} }
return; return;
} }

View file

@ -47,8 +47,6 @@ export fn wl_proxy_destroy(proxy: ?*c.struct_wl_proxy) void {
state: *Core, state: *Core,
core: *Core, core: *Core,
title: [:0]const u8,
size: *Core.Size,
surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface, surface_descriptor: *gpu.Surface.DescriptorFromWaylandSurface,
configured: bool = false, configured: bool = false,
@ -85,8 +83,6 @@ pub fn init(
.libwaylandclient = libwaylandclient_global, .libwaylandclient = libwaylandclient_global,
.interfaces = Interfaces{}, .interfaces = Interfaces{},
.display = libwaylandclient_global.wl_display_connect(null) orelse return error.FailedToConnectToDisplay, .display = libwaylandclient_global.wl_display_connect(null) orelse return error.FailedToConnectToDisplay,
.title = try options.allocator.dupeZ(u8, options.title),
.size = &linux.size,
.modifiers = .{ .modifiers = .{
.alt = false, .alt = false,
.caps_lock = false, .caps_lock = false,
@ -137,8 +133,8 @@ pub fn init(
region, region,
0, 0,
0, 0,
@intCast(wl.size.width), @intCast(linux.size.width),
@intCast(wl.size.height), @intCast(linux.size.height),
); );
c.wl_surface_set_opaque_region(wl.surface, region); c.wl_surface_set_opaque_region(wl.surface, region);
c.wl_region_destroy(region); c.wl_region_destroy(region);
@ -160,7 +156,7 @@ pub fn init(
// This space intentionally left blank // 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( const decoration = c.zxdg_decoration_manager_v1_get_toplevel_decoration(
wl.interfaces.zxdg_decoration_manager_v1, wl.interfaces.zxdg_decoration_manager_v1,
@ -182,7 +178,9 @@ pub fn deinit(
linux.allocator.destroy(wl.surface_descriptor); 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) { while (wl.libwaylandclient.wl_display_flush(wl.display) == -1) {
if (std.posix.errno(-1) == std.posix.E.AGAIN) { if (std.posix.errno(-1) == std.posix.E.AGAIN) {
log.err("flush error", .{}); log.err("flush error", .{});
@ -754,7 +752,7 @@ const xdg_surface_listener = struct {
wl.configured = true; wl.configured = true;
} }
setContentAreaOpaque(wl, wl.size.*); setContentAreaOpaque(wl, linux.size);
} }
const listener = c.xdg_surface_listener{ .configure = @ptrCast(&xdgSurfaceHandleConfigure) }; 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 { fn xdgToplevelHandleConfigure(linux: *Linux, toplevel: ?*c.struct_xdg_toplevel, width: i32, height: i32, states: [*c]c.struct_wl_array) callconv(.C) void {
_ = toplevel; _ = toplevel;
_ = states; _ = states;
const wl = &linux.backend.wayland;
if (width > 0 and height > 0) { if (width > 0 and height > 0) {
wl.size.* = .{ .width = @intCast(width), .height = @intCast(height) }; linux.size = .{ .width = @intCast(width), .height = @intCast(height) };
} }
} }

View file

@ -43,8 +43,6 @@ libxcursor: ?LibXCursor,
libxkbcommon: LibXkbCommon, libxkbcommon: LibXkbCommon,
gl_ctx: ?*LibGL.Context, gl_ctx: ?*LibGL.Context,
display: *c.Display, display: *c.Display,
width: c_int,
height: c_int,
empty_event_pipe: [2]std.c.fd_t, empty_event_pipe: [2]std.c.fd_t,
wm_protocols: c.Atom, wm_protocols: c.Atom,
wm_delete_window: c.Atom, wm_delete_window: c.Atom,
@ -59,21 +57,12 @@ net_wm_window_type_dock: c.Atom,
root_window: c.Window, root_window: c.Window,
window: c.Window, window: c.Window,
backend_type: gpu.BackendType, backend_type: gpu.BackendType,
refresh_rate: u32,
hidden_cursor: c.Cursor, hidden_cursor: c.Cursor,
// Mutable fields only used by main thread // Mutable fields only used by main thread
cursors: [@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor, cursors: [@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor,
// Mutable state fields; read/write by any thread // 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, surface_descriptor: *gpu.Surface.DescriptorFromXlibWindow,
pub fn init( pub fn init(
@ -139,7 +128,7 @@ pub fn init(
}; };
const blank_pixmap = libx11.XCreatePixmap(display, window, 1, 1, 1); const blank_pixmap = libx11.XCreatePixmap(display, window, 1, 1, 1);
var color = c.XColor{}; var color = c.XColor{};
const refresh_rate: u16 = blk: { linux.refresh_rate = blk: {
if (libxrr != null) { if (libxrr != null) {
const conf = libxrr.?.XRRGetScreenInfo(display, root_window); const conf = libxrr.?.XRRGetScreenInfo(display, root_window);
break :blk @intCast(libxrr.?.XRRConfigCurrentRate(conf)); break :blk @intCast(libxrr.?.XRRConfigCurrentRate(conf));
@ -162,8 +151,6 @@ pub fn init(
.libxrr = libxrr, .libxrr = libxrr,
.empty_event_pipe = try std.posix.pipe(), .empty_event_pipe = try std.posix.pipe(),
.gl_ctx = null, .gl_ctx = null,
.width = window_attrs.width,
.height = window_attrs.height,
.wm_protocols = libx11.XInternAtom(display, "WM_PROTOCOLS", c.False), .wm_protocols = libx11.XInternAtom(display, "WM_PROTOCOLS", c.False),
.wm_delete_window = libx11.XInternAtom(display, "WM_DELETE_WINDOW", c.False), .wm_delete_window = libx11.XInternAtom(display, "WM_DELETE_WINDOW", c.False),
.net_wm_ping = libx11.XInternAtom(display, "NET_WM_PING", c.False), .net_wm_ping = libx11.XInternAtom(display, "NET_WM_PING", c.False),
@ -178,12 +165,6 @@ pub fn init(
.window = window, .window = window,
.hidden_cursor = libx11.XCreatePixmapCursor(display, blank_pixmap, blank_pixmap, &color, &color, 0, 0), .hidden_cursor = libx11.XCreatePixmapCursor(display, blank_pixmap, blank_pixmap, &color, &color, 0, 0),
.backend_type = try Core.detectBackendType(options.allocator), .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), .cursors = std.mem.zeroes([@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor),
.surface_descriptor = surface_descriptor, .surface_descriptor = surface_descriptor,
.libxkbcommon = try LibXkbCommon.load(), .libxkbcommon = try LibXkbCommon.load(),
@ -272,11 +253,11 @@ pub fn deinit(
} }
// Called on the main thread // 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) { while (c.QLength(x11.display) != 0) {
var event: c.XEvent = undefined; var event: c.XEvent = undefined;
_ = x11.libx11.XNextEvent(x11.display, &event); _ = x11.libx11.XNextEvent(x11.display, &event);
x11.processEvent(&event); x11.processEvent(linux, &event);
} }
_ = x11.libx11.XFlush(x11.display); _ = x11.libx11.XFlush(x11.display);
@ -508,7 +489,7 @@ fn getCursorPos(x11: *X11) Position {
return .{ .x = @floatFromInt(cursor_x), .y = @floatFromInt(cursor_y) }; 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) { switch (event.type) {
c.KeyPress, c.KeyRelease => { c.KeyPress, c.KeyRelease => {
// TODO: key repeat event // 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 } } }); x11.state.pushEvent(.{ .mouse_motion = .{ .pos = .{ .x = x, .y = y } } });
}, },
c.ConfigureNotify => { c.ConfigureNotify => {
if (event.xconfigure.width != x11.size.width or if (event.xconfigure.width != linux.size.width or
event.xconfigure.height != x11.size.height) event.xconfigure.height != linux.size.height)
{ {
x11.size.width = @intCast(event.xconfigure.width); linux.size.width = @intCast(event.xconfigure.width);
x11.size.height = @intCast(event.xconfigure.height); linux.size.height = @intCast(event.xconfigure.height);
x11.core.swap_chain_update.set(); x11.core.swap_chain_update.set();
x11.state.pushEvent(.{ x11.state.pushEvent(.{
.framebuffer_resize = .{ .framebuffer_resize = .{
.width = x11.size.width, .width = linux.size.width,
.height = x11.size.height, .height = linux.size.height,
}, },
}); });
} }