From 0a5d817f54d5003940f29d0a39c2d8f1ca7ed65f Mon Sep 17 00:00:00 2001 From: Joshua Holmes Date: Fri, 18 Oct 2024 15:29:18 -0700 Subject: [PATCH] core: silently fail to connect to linux display and improve logging --- src/core/Linux.zig | 39 ++++++++++++++++++++------------------ src/core/linux/Wayland.zig | 2 +- src/core/linux/X11.zig | 3 +-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/core/Linux.zig b/src/core/Linux.zig index ade72017..60b56116 100644 --- a/src/core/Linux.zig +++ b/src/core/Linux.zig @@ -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 }; }, @@ -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. @@ -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), diff --git a/src/core/linux/Wayland.zig b/src/core/linux/Wayland.zig index 23d70354..1c35d21d 100644 --- a/src/core/linux/Wayland.zig +++ b/src/core/linux/Wayland.zig @@ -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 = .{ diff --git a/src/core/linux/X11.zig b/src/core/linux/X11.zig index bf84485a..b649ef37 100644 --- a/src/core/linux/X11.zig +++ b/src/core/linux/X11.zig @@ -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);