From fcba68282dc537806cbcc2da647a3c701c20768b Mon Sep 17 00:00:00 2001 From: Joshua Holmes Date: Thu, 10 Oct 2024 12:54:00 -0700 Subject: [PATCH] core: enable x11 and set as default for linux --- src/core/Linux.zig | 49 +++++++++++++++++++++++------------------- src/core/linux/X11.zig | 10 ++++----- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/core/Linux.zig b/src/core/Linux.zig index 7b9e0077..e826fca7 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -54,6 +54,14 @@ pub fn init( linux.allocator = options.allocator; if (!options.is_app and try wantGamemode(linux.allocator)) linux.gamemode = initLinuxGamemode(); + linux.headless = options.headless; + linux.refresh_rate = 60; // TODO: set to something meaningful + linux.vsync_mode = .triple; + linux.size = options.size; + if (!options.headless) { + // TODO: this function does nothing right now + setDisplayMode(linux, options.display_mode); + } const desired_backend: BackendEnum = blk: { const backend = std.process.getEnvVarOwned( @@ -61,8 +69,7 @@ pub fn init( "MACH_CORE_BACKEND", ) catch |err| switch (err) { error.EnvironmentVariableNotFound => { - // TODO(core): default to .x11 in the future - break :blk .wayland; + break :blk .x11; }, else => return err, }; @@ -73,31 +80,27 @@ pub fn init( std.debug.panic("mach: unknown MACH_CORE_BACKEND: {s}", .{backend}); }; - if (desired_backend == .x11) { - // TODO(core): support X11 in the future - @panic("X11 is not supported...YET"); - } - // Try to initialize the desired backend, falling back to the other if that one is not supported switch (desired_backend) { - .x11 => { - // const x11 = X11.init(linux, core, options) catch |err| switch (err) { - // error.NotSupported => { - // log.err("failed to initialize X11 backend, falling back to Wayland", .{}); - // linux.backend = .{ .wayland = try Wayland.init(linux, core, options) }; - // }, - // else => return err, - // }; - // linux.backend = .{ .x11 = x11 }; + .x11 => blk: { + const x11 = X11.init(linux, core, options) catch |err| switch (err) { + error.LibraryNotFound => { + log.err("failed to initialize X11 backend, falling back to Wayland", .{}); + linux.backend = .{ .wayland = try Wayland.init(linux, core, options) }; + + break :blk; + }, + else => return err, + }; + linux.backend = .{ .x11 = x11 }; }, - .wayland => { + .wayland => blk: { const wayland = Wayland.init(linux, core, options) catch |err| switch (err) { error.LibraryNotFound => { log.err("failed to initialize Wayland backend, falling back to X11", .{}); linux.backend = .{ .x11 = try X11.init(linux, core, options) }; - // TODO(core): support X11 in the future - @panic("X11 is not supported...YET"); + break :blk; }, else => return err, }; @@ -114,8 +117,6 @@ pub fn init( }, } - linux.refresh_rate = 60; // TODO: set to something meaningful - return; } @@ -129,7 +130,11 @@ pub fn deinit(linux: *Linux) void { return; } -pub fn update(_: *Linux) !void { +pub fn update(linux: *Linux) !void { + switch (linux.backend) { + .wayland => {}, + .x11 => try linux.backend.x11.update(), + } return; } diff --git a/src/core/linux/X11.zig b/src/core/linux/X11.zig index c73835cf..bf84485a 100644 --- a/src/core/linux/X11.zig +++ b/src/core/linux/X11.zig @@ -74,7 +74,7 @@ display_mode: DisplayMode = .windowed, vsync_mode: VSyncMode = .triple, border: bool, headless: bool, -size: Core.Size, +size: *Core.Size, cursor_mode: CursorMode = .normal, cursor_shape: CursorShape = .arrow, surface_descriptor: *gpu.Surface.DescriptorFromXlibWindow, @@ -119,8 +119,8 @@ pub fn init( root_window, @divFloor(libx11.XDisplayWidth(display, screen), 2), // TODO: add window width? @divFloor(libx11.XDisplayHeight(display, screen), 2), // TODO: add window height? - options.size.width, - options.size.height, + linux.size.width, + linux.size.height, 0, c.DefaultDepth(display, screen), c.InputOutput, @@ -130,7 +130,7 @@ pub fn init( ); var window_attrs: c.XWindowAttributes = undefined; _ = libx11.XGetWindowAttributes(display, window, &window_attrs); - const window_size = Core.Size{ + linux.size = Core.Size{ .width = @intCast(window_attrs.width), .height = @intCast(window_attrs.height), }; @@ -180,7 +180,7 @@ pub fn init( .display_mode = .windowed, .border = options.border, .headless = options.headless, - .size = window_size, + .size = &linux.size, .cursors = std.mem.zeroes([@typeInfo(CursorShape).@"enum".fields.len]?c.Cursor), .surface_descriptor = surface_descriptor, .libxkbcommon = try LibXkbCommon.load(),