core: move linux-specific code to Linux.zig

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-08-25 19:04:37 -07:00
parent cd85a2d623
commit f548918e13
2 changed files with 32 additions and 51 deletions

View file

@ -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,