From f548918e135af2de6e53c7bccec51da5e82e0335 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 25 Aug 2024 19:04:37 -0700 Subject: [PATCH] core: move linux-specific code to Linux.zig Signed-off-by: Stephen Gutekanst --- src/Core.zig | 48 ---------------------------------------------- src/core/Linux.zig | 35 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/Core.zig b/src/Core.zig index 05d2a8fb..d477e076 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -6,9 +6,6 @@ const mach = @import("main.zig"); const gpu = mach.gpu; const log = std.log.scoped(.mach); -// TODO: move to Linux.zig -const gamemode_log = std.log.scoped(.gamemode); - // Whether or not you can drive the main loop in a non-blocking fashion, or if the underlying // platform must take control and drive the main loop itself. pub const supports_non_blocking = switch (build_options.core_platform) { @@ -135,9 +132,6 @@ state: enum { } = .running, frame: mach.time.Frequency, -// TODO: move to Linux.zig -linux_gamemode: ?bool = null, - // Might be accessed by Platform backend input: mach.time.Frequency, swap_chain_update: std.Thread.ResetEvent = .{}, @@ -278,11 +272,6 @@ fn init(core: *Mod, entities: *mach.Entities.Mod) !void { try core.set(state.main_window, .width, state.platform.size.width); try core.set(state.main_window, .height, state.platform.size.height); - // TODO: move to Linux.zig - if (builtin.os.tag == .linux and !options.is_app and - state.linux_gamemode == null and try wantGamemode(options.allocator)) - state.linux_gamemode = initLinuxGamemode(); - state.frame = .{ .target = 0 }; state.input = .{ .target = 1 }; try state.frame.start(); @@ -362,14 +351,6 @@ pub fn deinit(entities: *mach.Entities.Mod, core: *Mod) !void { } } - // TODO: move to Linux.zig - if (builtin.os.tag == .linux and - state.linux_gamemode != null and - state.linux_gamemode.?) - { - deinitLinuxGamemode(); - } - state.platform.deinit(); state.swap_chain.release(); state.queue.release(); @@ -733,35 +714,6 @@ pub inline fn printUnhandledErrorCallback(_: void, ty: gpu.ErrorType, message: [ std.process.exit(1); } -// TODO: move to Linux.zig -/// Check if gamemode should be activated -pub fn wantGamemode(allocator: std.mem.Allocator) error{ OutOfMemory, InvalidWtf8 }!bool { - const use_gamemode = std.process.getEnvVarOwned( - allocator, - "MACH_USE_GAMEMODE", - ) catch |err| switch (err) { - error.EnvironmentVariableNotFound => return true, - else => |e| return e, - }; - defer allocator.free(use_gamemode); - - return !(std.ascii.eqlIgnoreCase(use_gamemode, "off") or std.ascii.eqlIgnoreCase(use_gamemode, "false")); -} - -// TODO: move to Linux.zig -pub fn initLinuxGamemode() bool { - mach.gamemode.start(); - if (!mach.gamemode.isActive()) return false; - gamemode_log.info("gamemode: activated", .{}); - return true; -} - -// TODO: move to Linux.zig -pub fn deinitLinuxGamemode() void { - mach.gamemode.stop(); - gamemode_log.info("gamemode: deactivated", .{}); -} - pub fn detectBackendType(allocator: std.mem.Allocator) !gpu.BackendType { const backend = std.process.getEnvVarOwned( allocator, diff --git a/src/core/Linux.zig b/src/core/Linux.zig index 7572830f..a9c16acc 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -17,6 +17,7 @@ const Key = Core.Key; const KeyMods = Core.KeyMods; const log = std.log.scoped(.mach); +const gamemode_log = std.log.scoped(.gamemode); pub const Linux = @This(); @@ -33,19 +34,21 @@ headless: bool, refresh_rate: u32, size: Size, surface_descriptor: gpu.Surface.Descriptor, +gamemode: ?bool = null, pub fn init( linux: *Linux, core: *Core.Mod, options: InitOptions, ) !void { - _ = linux; - _ = options; _ = core; + + if (!options.is_app and try wantGamemode(options.allocator)) linux.gamemode = initLinuxGamemode(); return; } -pub fn deinit(_: *Linux) void { +pub fn deinit(linux: *Linux) void { + if (linux.gamemode != null and linux.gamemode.?) deinitLinuxGamemode(); return; } @@ -89,6 +92,32 @@ pub fn setCursorShape(_: *Linux, _: CursorShape) void { return; } +/// Check if gamemode should be activated +pub fn wantGamemode(allocator: std.mem.Allocator) error{ OutOfMemory, InvalidWtf8 }!bool { + const use_gamemode = std.process.getEnvVarOwned( + allocator, + "MACH_USE_GAMEMODE", + ) catch |err| switch (err) { + error.EnvironmentVariableNotFound => return true, + else => |e| return e, + }; + defer allocator.free(use_gamemode); + + return !(std.ascii.eqlIgnoreCase(use_gamemode, "off") or std.ascii.eqlIgnoreCase(use_gamemode, "false")); +} + +pub fn initLinuxGamemode() bool { + mach.gamemode.start(); + if (!mach.gamemode.isActive()) return false; + gamemode_log.info("gamemode: activated", .{}); + return true; +} + +pub fn deinitLinuxGamemode() void { + mach.gamemode.stop(); + gamemode_log.info("gamemode: deactivated", .{}); +} + ///! Taken from https://github.com/glfw/glfw/blob/master/src/xkb_unicode.c const KeySym = c_ulong; const keysym_table = &[_]struct { KeySym, u21 }{