core: move linux-specific code to Linux.zig
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
cd85a2d623
commit
f548918e13
2 changed files with 32 additions and 51 deletions
48
src/Core.zig
48
src/Core.zig
|
|
@ -6,9 +6,6 @@ const mach = @import("main.zig");
|
||||||
const gpu = mach.gpu;
|
const gpu = mach.gpu;
|
||||||
const log = std.log.scoped(.mach);
|
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
|
// 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.
|
// platform must take control and drive the main loop itself.
|
||||||
pub const supports_non_blocking = switch (build_options.core_platform) {
|
pub const supports_non_blocking = switch (build_options.core_platform) {
|
||||||
|
|
@ -135,9 +132,6 @@ state: enum {
|
||||||
} = .running,
|
} = .running,
|
||||||
frame: mach.time.Frequency,
|
frame: mach.time.Frequency,
|
||||||
|
|
||||||
// TODO: move to Linux.zig
|
|
||||||
linux_gamemode: ?bool = null,
|
|
||||||
|
|
||||||
// Might be accessed by Platform backend
|
// Might be accessed by Platform backend
|
||||||
input: mach.time.Frequency,
|
input: mach.time.Frequency,
|
||||||
swap_chain_update: std.Thread.ResetEvent = .{},
|
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, .width, state.platform.size.width);
|
||||||
try core.set(state.main_window, .height, state.platform.size.height);
|
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.frame = .{ .target = 0 };
|
||||||
state.input = .{ .target = 1 };
|
state.input = .{ .target = 1 };
|
||||||
try state.frame.start();
|
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.platform.deinit();
|
||||||
state.swap_chain.release();
|
state.swap_chain.release();
|
||||||
state.queue.release();
|
state.queue.release();
|
||||||
|
|
@ -733,35 +714,6 @@ pub inline fn printUnhandledErrorCallback(_: void, ty: gpu.ErrorType, message: [
|
||||||
std.process.exit(1);
|
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 {
|
pub fn detectBackendType(allocator: std.mem.Allocator) !gpu.BackendType {
|
||||||
const backend = std.process.getEnvVarOwned(
|
const backend = std.process.getEnvVarOwned(
|
||||||
allocator,
|
allocator,
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ const Key = Core.Key;
|
||||||
const KeyMods = Core.KeyMods;
|
const KeyMods = Core.KeyMods;
|
||||||
|
|
||||||
const log = std.log.scoped(.mach);
|
const log = std.log.scoped(.mach);
|
||||||
|
const gamemode_log = std.log.scoped(.gamemode);
|
||||||
|
|
||||||
pub const Linux = @This();
|
pub const Linux = @This();
|
||||||
|
|
||||||
|
|
@ -33,19 +34,21 @@ headless: bool,
|
||||||
refresh_rate: u32,
|
refresh_rate: u32,
|
||||||
size: Size,
|
size: Size,
|
||||||
surface_descriptor: gpu.Surface.Descriptor,
|
surface_descriptor: gpu.Surface.Descriptor,
|
||||||
|
gamemode: ?bool = null,
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
linux: *Linux,
|
linux: *Linux,
|
||||||
core: *Core.Mod,
|
core: *Core.Mod,
|
||||||
options: InitOptions,
|
options: InitOptions,
|
||||||
) !void {
|
) !void {
|
||||||
_ = linux;
|
|
||||||
_ = options;
|
|
||||||
_ = core;
|
_ = core;
|
||||||
|
|
||||||
|
if (!options.is_app and try wantGamemode(options.allocator)) linux.gamemode = initLinuxGamemode();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(_: *Linux) void {
|
pub fn deinit(linux: *Linux) void {
|
||||||
|
if (linux.gamemode != null and linux.gamemode.?) deinitLinuxGamemode();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,6 +92,32 @@ pub fn setCursorShape(_: *Linux, _: CursorShape) void {
|
||||||
return;
|
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
|
///! Taken from https://github.com/glfw/glfw/blob/master/src/xkb_unicode.c
|
||||||
const KeySym = c_ulong;
|
const KeySym = c_ulong;
|
||||||
const keysym_table = &[_]struct { KeySym, u21 }{
|
const keysym_table = &[_]struct { KeySym, u21 }{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue