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,

View file

@ -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 }{