Skip to content

Commit

Permalink
core: silently fail to connect to linux display and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-holmes authored and emidoots committed Oct 21, 2024
1 parent 2f095fd commit 0a5d817
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
39 changes: 21 additions & 18 deletions src/core/Linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,28 @@ pub fn init(
// Try to initialize the desired backend, falling back to the other if that one is not supported
switch (desired_backend) {
.x11 => blk: {
const x11 = X11.init(linux, core, options) catch |err| switch (err) {
error.LibraryNotFound => {
log.err("failed to initialize X11 backend, falling back to Wayland", .{});
linux.backend = .{ .wayland = try Wayland.init(linux, core, options) };

break :blk;
},
else => return err,
const x11 = X11.init(linux, core, options) catch |err| {
const err_msg = switch (err) {
error.LibraryNotFound => "Missing X11 library",
error.FailedToConnectToDisplay => "Failed to connect to display",
else => "An unknown error occured while trying to connect to X11",
};
log.err("{s}\nFalling back to Wayland\n", .{err_msg});
linux.backend = .{ .wayland = try Wayland.init(linux, core, options) };
break :blk;
};
linux.backend = .{ .x11 = x11 };
},
.wayland => blk: {
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) };

break :blk;
},
else => return err,
const wayland = Wayland.init(linux, core, options) catch |err| {
const err_msg = switch (err) {
error.LibraryNotFound => "Missing Wayland library",
error.FailedToConnectToDisplay => "Failed to connect to display",
else => "An unknown error occured while trying to connect to Wayland",
};
log.err("{s}\nFalling back to X11\n", .{err_msg});
linux.backend = .{ .x11 = try X11.init(linux, core, options) };
break :blk;
};
linux.backend = .{ .wayland = wayland };
},
Expand Down Expand Up @@ -196,13 +198,13 @@ pub fn wantGamemode(allocator: std.mem.Allocator) error{ OutOfMemory, InvalidWtf
pub fn initLinuxGamemode() bool {
mach.gamemode.start();
if (!mach.gamemode.isActive()) return false;
gamemode_log.info("gamemode: activated", .{});
gamemode_log.info("gamemode: activated\n", .{});
return true;
}

pub fn deinitLinuxGamemode() void {
mach.gamemode.stop();
gamemode_log.info("gamemode: deactivated", .{});
gamemode_log.info("gamemode: deactivated\n", .{});
}

/// Used to inform users that some features are not present. Remove when features are complete.
Expand All @@ -213,6 +215,7 @@ fn warnAboutIncompleteFeatures(backend: BackendEnum, missing_features_x11: []con
\\{s}
\\
\\Contributions welcome!
\\
;
const bullet_points = switch (backend) {
.x11 => try generateFeatureBulletPoints(missing_features_x11, alloc),
Expand Down
2 changes: 1 addition & 1 deletion src/core/linux/Wayland.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn init(
.libxkbcommon = try LibXkbCommon.load(),
.libwaylandclient = libwaylandclient_global,
.interfaces = Interfaces{},
.display = libwaylandclient_global.wl_display_connect(null) orelse return error.FailedToConnectToWaylandDisplay,
.display = libwaylandclient_global.wl_display_connect(null) orelse return error.FailedToConnectToDisplay,
.title = try options.allocator.dupeZ(u8, options.title),
.size = &linux.size,
.modifiers = .{
Expand Down
3 changes: 1 addition & 2 deletions src/core/linux/X11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ pub fn init(
else => return err,
};
const display = libx11.XOpenDisplay(null) orelse {
std.log.err("X11: Cannot open display", .{});
return error.CannotOpenDisplay;
return error.FailedToConnectToDisplay;
};
const screen = c.DefaultScreen(display);
const visual = c.DefaultVisual(display, screen);
Expand Down

0 comments on commit 0a5d817

Please sign in to comment.