Skip to content

Commit

Permalink
core: get wayland to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-holmes committed Sep 14, 2024
1 parent 8eb2da1 commit fd4c315
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 121 deletions.
8 changes: 8 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ pub fn build(b: *std.Build) !void {
if (want_examples) try buildExamples(b, optimize, target, module);
}
if (want_core) {
if (Platform.fromTarget(target.result) == .linux) {
if (b.lazyDependency("wayland_headers", .{
.target = target,
.optimize = optimize,
})) |dep| {
module.linkLibrary(dep.artifact("wayland-headers"));
}
}
if (target.result.isDarwin()) {
if (b.lazyDependency("mach_objc", .{
.target = target,
Expand Down
5 changes: 3 additions & 2 deletions src/Core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ fn init(core: *Mod, entities: *mach.Entities.Mod) !void {
var events = EventQueue.init(allocator);
try events.ensureTotalCapacity(8192);

// TODO: remove undefined initialization (disgusting!)
const platform: Platform = undefined;
core.init(.{
.allocator = allocator,
.main_window = main_window,
.events = events,
.input_state = .{},

// TODO: remove undefined initialization (disgusting!)
.platform = undefined,
.platform = platform,

// TODO: these should not be state, they should be components.
.title = title,
Expand Down
65 changes: 34 additions & 31 deletions src/core/Linux.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const mach = @import("../main.zig");
const Core = @import("../Core.zig");
// const X11 = @import("linux/X11.zig");
// const Wayland = @import("linux/Wayland.zig");
const X11 = @import("linux/X11.zig");
const Wayland = @import("linux/Wayland.zig");
const gpu = mach.gpu;
const InitOptions = Core.InitOptions;
const Event = Core.Event;
Expand All @@ -21,14 +21,14 @@ const KeyMods = Core.KeyMods;
const log = std.log.scoped(.mach);
const gamemode_log = std.log.scoped(.gamemode);

// const Backend = union(enum) {
// x11: X11,
// wayland: Wayland,
// };
const Backend = enum { // dummy backend
const BackendEnum = enum {
x11,
wayland,
};
const Backend = union(BackendEnum) {
x11: X11,
wayland: Wayland,
};

pub const Linux = @This();

Expand All @@ -51,13 +51,11 @@ pub fn init(
core: *Core.Mod,
options: InitOptions,
) !void {
_ = core;

linux.allocator = options.allocator;

if (!options.is_app and try wantGamemode(linux.allocator)) linux.gamemode = initLinuxGamemode();

const desired_backend: Backend = blk: {
const desired_backend: BackendEnum = blk: {
const backend = std.process.getEnvVarOwned(
linux.allocator,
"MACH_CORE_BACKEND",
Expand All @@ -83,32 +81,37 @@ pub fn init(
// Try to initialize the desired backend, falling back to the other if that one is not supported
switch (desired_backend) {
.x11 => {
// const x11 = X11.init(core, options) catch |err| switch (err) {
// error.NotSupported => {
// log.err("failed to initialize X11 backend, falling back to Wayland", .{});
// linux.backend = .{ .wayland = try Wayland.init(linux, core, options) };
// },
// else => return err,
// };
// linux.backend = .{ .x11 = x11 };
const x11 = X11.init(linux, core, options) catch |err| switch (err) {
error.NotSupported => {
log.err("failed to initialize X11 backend, falling back to Wayland", .{});
linux.backend = .{ .wayland = try Wayland.init(linux, core, options) };
},
else => return err,
};
linux.backend = .{ .x11 = x11 };
},
.wayland => {
// const wayland = Wayland.init(core, options) catch |err| switch (err) {
// error.NotSupported => {
// log.err("failed to initialize Wayland backend, falling back to X11", .{});
// linux.backend = .{ .x11 = try X11.init(linux, core, options) };
//
// // TODO(core): support X11 in the future
// @panic("X11 is not supported...YET");
// },
// else => return err,
// };
// linux.backend = .{ .wayland = wayland };
const wayland = Wayland.init(linux, core, options) catch |err| switch (err) {
error.LibraryNotFound => {
log.err("failed to initialize Wayland backend, falling back to X11", .{});
linux.backend = .{ .x11 = try X11.init(linux, core, options) };

// TODO(core): support X11 in the future
@panic("X11 is not supported...YET");
},
else => return err,
};
linux.backend = .{ .wayland = wayland };
},
}

switch (linux.backend) {
.wayland => |be| {
linux.surface_descriptor = .{ .next_in_chain = .{ .from_wayland_surface = &be.surface_descriptor } };
},
.x11 => {}, // TODO: setup surface descriptor
}

// linux.size = linux.backend.size;
// linux.surface_descriptor = linux.backend.surface_descriptor;
linux.refresh_rate = 60; // TODO: set to something meaningful

return;
Expand Down
Loading

0 comments on commit fd4c315

Please sign in to comment.