core: switch to pure-Zig mach-gamemode library

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-05-19 16:07:41 -07:00
parent 2fb8fde05c
commit 11c76a9747
5 changed files with 36 additions and 25 deletions

View file

@ -2,7 +2,6 @@ const std = @import("std");
const builtin = @import("builtin");
const system_sdk = @import("libs/mach-glfw/system_sdk.zig");
const glfw = @import("libs/mach-glfw/build.zig");
const gamemode = @import("libs/mach-gamemode/build.zig");
const gpu_dawn = @import("libs/mach-gpu-dawn/sdk.zig").Sdk(.{
.glfw_include_dir = sdkPath("/libs/mach-glfw/upstream/glfw/include"),
.system_sdk = system_sdk,
@ -14,7 +13,6 @@ const core = @import("sdk.zig").Sdk(.{
.gpu = gpu,
.gpu_dawn = gpu_dawn,
.glfw = glfw,
.gamemode = gamemode,
});
pub fn build(b: *std.Build) !void {

View file

@ -7,5 +7,9 @@
.url = "https://github.com/hexops/mach-sysjs/archive/b71eb0531f337fcca5a2b245f595355260109a34.tar.gz",
.hash = "12208b30f1d9c229d1e64483354610207c9aa06350a46558560b818d597800ed86e0",
},
.mach_gamemode = .{
.url = "https://github.com/hexops/mach-gamemode/archive/936bcd7767bf7cae720d36d930599c20fdc87b1d.tar.gz",
.hash = "12203cb075bec68c8cb4cfa1337bc9dabe47f055e377d1b8144b2c21f9a2577efb07",
},
},
}

View file

@ -1 +0,0 @@
../../gamemode

View file

@ -17,12 +17,15 @@ pub fn Sdk(comptime deps: anytype) type {
pub fn module(b: *std.Build) *std.build.Module {
if (_module) |m| return m;
const gamemode_dep = b.dependency("mach_gamemode", .{});
_module = b.createModule(.{
.source_file = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{
.{ .name = "gpu", .module = deps.gpu.module(b) },
.{ .name = "glfw", .module = deps.glfw.module(b) },
.{ .name = "gamemode", .module = deps.gamemode.module(b) },
.{ .name = "gamemode", .module = gamemode_dep.module("mach-gamemode") },
},
});
return _module.?;
@ -42,8 +45,8 @@ pub fn Sdk(comptime deps: anytype) type {
main_tests.addModule("glfw", deps.glfw.module(b));
try deps.glfw.link(b, main_tests, .{});
if (target.isLinux()) {
main_tests.addModule("gamemode", deps.gamemode.module(b));
deps.gamemode.link(main_tests);
const gamemode_dep = b.dependency("mach_gamemode", .{});
main_tests.addModule("gamemode", gamemode_dep.module("mach-gamemode"));
}
main_tests.addIncludePath(sdkPath("/include"));
b.installArtifact(main_tests);
@ -61,8 +64,8 @@ pub fn Sdk(comptime deps: anytype) type {
lib.addModule("glfw", deps.glfw.module(b));
lib.addModule("gpu", deps.gpu.module(b));
if (target.isLinux()) {
lib.addModule("gamemode", deps.gamemode.module(b));
deps.gamemode.link(lib);
const gamemode_dep = b.dependency("mach_gamemode", .{});
lib.addModule("gamemode", gamemode_dep.module("mach-gamemode"));
}
try deps.glfw.link(b, lib, options.glfw_options);
try deps.gpu.link(b, lib, options.gpuOptions());
@ -150,8 +153,10 @@ pub fn Sdk(comptime deps: anytype) type {
});
exe.addModule("glfw", deps.glfw.module(b));
if (target.os.tag == .linux)
exe.addModule("gamemode", deps.gamemode.module(b));
if (target.os.tag == .linux) {
const gamemode_dep = b.dependency("mach_gamemode", .{});
exe.addModule("gamemode", gamemode_dep.module("mach-gamemode"));
}
break :blk exe;
}
@ -176,8 +181,6 @@ pub fn Sdk(comptime deps: anytype) type {
if (app.platform != .web) {
try deps.glfw.link(app.b, app.step, options.glfw_options);
deps.gpu.link(app.b, app.step, options.gpuOptions()) catch return error.FailedToLinkGPU;
if (app.step.target.isLinux())
deps.gamemode.link(app.step);
}
}

View file

@ -17,6 +17,8 @@ const CursorMode = @import("../../Core.zig").CursorMode;
const Key = @import("../../Core.zig").Key;
const KeyMods = @import("../../Core.zig").KeyMods;
const log = std.log.scoped(.mach);
pub const Core = @This();
allocator: std.mem.Allocator,
@ -107,7 +109,7 @@ pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
}
const instance = gpu.createInstance(null) orelse {
std.log.err("mach: failed to create GPU instance", .{});
log.err("failed to create GPU instance", .{});
std.process.exit(1);
};
const surface = util.createSurfaceForWindow(instance, window, comptime util.detectGLFWOptions());
@ -118,9 +120,12 @@ pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
.power_preference = options.power_preference,
.force_fallback_adapter = false,
}, &response, util.requestAdapterCallback);
log.err("failed to create GPU adapter: {?s}", .{response.message});
log.info("-> maybe try MACH_GPU_BACKEND=opengl ?", .{});
std.process.exit(1);
if (response.status != .success) {
std.log.err("mach: failed to create GPU adapter: {?s}", .{response.message});
std.log.info("-> maybe try MACH_GPU_BACKEND=opengl ?", .{});
log.err("failed to create GPU adapter: {?s}", .{response.message});
log.info("-> maybe try MACH_GPU_BACKEND=opengl ?", .{});
std.process.exit(1);
}
@ -128,10 +133,10 @@ pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
var props = std.mem.zeroes(gpu.Adapter.Properties);
response.adapter.getProperties(&props);
if (props.backend_type == .null) {
std.log.err("no backend found for {s} adapter", .{props.adapter_type.name()});
log.err("no backend found for {s} adapter", .{props.adapter_type.name()});
std.process.exit(1);
}
std.log.info("mach: found {s} backend on {s} adapter: {s}, {s}\n", .{
log.info("found {s} backend on {s} adapter: {s}, {s}\n", .{
props.backend_type.name(),
props.adapter_type.name(),
props.name,
@ -146,7 +151,7 @@ pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
.limits = limits,
}) else null,
}) orelse {
std.log.err("mach: failed to create GPU device\n", .{});
log.err("failed to create GPU device\n", .{});
std.process.exit(1);
};
gpu_device.setUncapturedErrorCallback({}, util.printUnhandledErrorCallback);
@ -344,7 +349,7 @@ pub inline fn pollEvents(self: *Core) EventIterator {
}
glfw.getErrorCode() catch |err| switch (err) {
error.PlatformError => std.log.err("glfw: failed to poll events", .{}),
error.PlatformError => log.err("glfw: failed to poll events", .{}),
error.InvalidValue => unreachable,
else => unreachable,
};
@ -578,7 +583,7 @@ pub fn setCursorShape(self: *Core, cursor: CursorShape) void {
// TODO: In the future we shouldn't hit this because we'll provide backup
// custom cursors.
// See https://github.com/hexops/mach/pull/352 for more info
std.log.warn("mach: setCursorShape: {s} not yet supported\n", .{@tagName(cursor)});
log.warn("setCursorShape: {s} not yet supported\n", .{@tagName(cursor)});
}
self.current_cursor = cursor;
@ -761,7 +766,7 @@ fn toMachMods(mods: glfw.Mods) KeyMods {
/// Default GLFW error handling callback
fn errorCallback(error_code: glfw.ErrorCode, description: [:0]const u8) void {
std.log.err("glfw: {}: {s}\n", .{ error_code, description });
log.err("glfw: {}: {s}\n", .{ error_code, description });
}
fn getEnvVarOwned(allocator: std.mem.Allocator, key: []const u8) error{ OutOfMemory, InvalidUtf8 }!?[]u8 {
@ -782,18 +787,20 @@ fn activateGamemode(allocator: std.mem.Allocator) error{ OutOfMemory, InvalidUtf
fn initLinuxGamemode() bool {
const gamemode = @import("gamemode");
_ = gamemode.init();
gamemode.requestStart() catch |err| {
if (!std.mem.containsAtLeast(u8, gamemode.errorString(), 1, "dlopen failed"))
std.log.err("Gamemode error {} -> {s}", .{ err, gamemode.errorString() });
log.err("gamemode: {s}", .{@errorName(err)});
return false;
};
std.log.info("Gamemode activated", .{});
log.info("gamemode: activated", .{});
return true;
}
fn deinitLinuxGamemode() void {
const gamemode = @import("gamemode");
gamemode.requestEnd() catch |err| {
std.log.err("Gamemode error {} -> {s}", .{ err, gamemode.errorString() });
log.err("gamemode: error {s}", .{@errorName(err)});
};
gamemode.deinit();
}