From 79bb0accacc1ada0920710e7b534b485cd60f856 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Fri, 26 Jun 2020 00:59:31 +0200 Subject: [PATCH] code: improve error handling consistency --- river/Control.zig | 2 +- river/Cursor.zig | 6 ++---- river/DecorationManager.zig | 2 +- river/InputManager.zig | 3 +-- river/Keyboard.zig | 6 ++---- river/Output.zig | 10 ++-------- river/Root.zig | 12 ++++-------- river/Seat.zig | 5 ++--- river/Server.zig | 8 ++++---- river/StatusManager.zig | 2 +- riverctl/main.zig | 7 +++---- 11 files changed, 23 insertions(+), 40 deletions(-) diff --git a/river/Control.zig b/river/Control.zig index d84f519..2b8ee4d 100644 --- a/river/Control.zig +++ b/river/Control.zig @@ -47,7 +47,7 @@ pub fn init(self: *Self, server: *Server) !void { protocol_version, self, bind, - ) orelse return error.CantCreateWlGlobal; + ) orelse return error.OutOfMemory; self.args_map = std.AutoHashMap(u32, std.ArrayList([]const u8)).init(util.gpa); diff --git a/river/Cursor.zig b/river/Cursor.zig index 18bb9bf..e826952 100644 --- a/river/Cursor.zig +++ b/river/Cursor.zig @@ -59,15 +59,13 @@ pub fn init(self: *Self, seat: *Seat) !void { self.seat = seat; // Creates a wlroots utility for tracking the cursor image shown on screen. - self.wlr_cursor = c.wlr_cursor_create() orelse - return error.CantCreateWlrCursor; + self.wlr_cursor = c.wlr_cursor_create() orelse return error.OutOfMemory; // Creates an xcursor manager, another wlroots utility which loads up // Xcursor themes to source cursor images from and makes sure that cursor // images are available at all scale factors on the screen (necessary for // HiDPI support). We add a cursor theme at scale factor 1 to begin with. - self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse - return error.CantCreateWlrXCursorManager; + self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse return error.OutOfMemory; c.wlr_cursor_attach_output_layout(self.wlr_cursor, seat.input_manager.server.root.wlr_output_layout); if (c.wlr_xcursor_manager_load(self.wlr_xcursor_manager, 1) == 0) { if (build_options.xwayland) { diff --git a/river/DecorationManager.zig b/river/DecorationManager.zig index 1b844e9..2eb1509 100644 --- a/river/DecorationManager.zig +++ b/river/DecorationManager.zig @@ -33,7 +33,7 @@ listen_new_toplevel_decoration: c.wl_listener, pub fn init(self: *Self, server: *Server) !void { self.wlr_xdg_decoration_manager = c.wlr_xdg_decoration_manager_v1_create(server.wl_display) orelse - return error.CantCreateWlrXdgDecorationManager; + return error.OutOfMemory; self.listen_new_toplevel_decoration.notify = handleNewToplevelDecoration; c.wl_signal_add( diff --git a/river/InputManager.zig b/river/InputManager.zig index 9c0895c..10293ea 100644 --- a/river/InputManager.zig +++ b/river/InputManager.zig @@ -46,8 +46,7 @@ pub fn init(self: *Self, server: *Server) !void { // This is automatically freed when the display is destroyed self.wlr_input_inhibit_manager = - c.wlr_input_inhibit_manager_create(server.wl_display) orelse - return error.CantCreateInputInhibitManager; + c.wlr_input_inhibit_manager_create(server.wl_display) orelse return error.OutOfMemory; self.seats = std.TailQueue(Seat).init(); diff --git a/river/Keyboard.zig b/river/Keyboard.zig index 12b043f..6fab20f 100644 --- a/river/Keyboard.zig +++ b/river/Keyboard.zig @@ -46,16 +46,14 @@ pub fn init(self: *Self, seat: *Seat, wlr_input_device: *c.wlr_input_device) !vo .variant = null, .options = null, }; - const context = c.xkb_context_new(.XKB_CONTEXT_NO_FLAGS) orelse - return error.CantCreateXkbContext; + const context = c.xkb_context_new(.XKB_CONTEXT_NO_FLAGS) orelse return error.CreateXkbContextError; defer c.xkb_context_unref(context); const keymap = c.xkb_keymap_new_from_names( context, &rules, .XKB_KEYMAP_COMPILE_NO_FLAGS, - ) orelse - return error.CantCreateXkbKeymap; + ) orelse return error.CreateXkbKeymapError; defer c.xkb_keymap_unref(keymap); // TODO: handle failure after https://github.com/swaywm/wlroots/pull/2081 diff --git a/river/Output.zig b/river/Output.zig index 5ac7306..2f0398e 100644 --- a/river/Output.zig +++ b/river/Output.zig @@ -82,16 +82,10 @@ pub fn init(self: *Self, root: *Root, wlr_output: *c.wlr_output) !void { // refresh rate), and each monitor supports only a specific set of modes. We // just pick the monitor's preferred mode, a more sophisticated compositor // would let the user configure it. - - // if not empty - if (c.wl_list_empty(&wlr_output.modes) == 0) { - // TODO: handle failure - const mode = c.wlr_output_preferred_mode(wlr_output); + if (c.wlr_output_preferred_mode(wlr_output)) |mode| { c.wlr_output_set_mode(wlr_output, mode); c.wlr_output_enable(wlr_output, true); - if (!c.wlr_output_commit(wlr_output)) { - return error.CantCommitWlrOutputMode; - } + if (!c.wlr_output_commit(wlr_output)) return error.OutputCommitFailed; } self.root = root; diff --git a/river/Root.zig b/river/Root.zig index c5301a8..7bcb83e 100644 --- a/river/Root.zig +++ b/river/Root.zig @@ -56,19 +56,15 @@ pub fn init(self: *Self, server: *Server) !void { // Create an output layout, which a wlroots utility for working with an // arrangement of screens in a physical layout. - self.wlr_output_layout = c.wlr_output_layout_create() orelse - return error.CantCreateWlrOutputLayout; + self.wlr_output_layout = c.wlr_output_layout_create() orelse return error.OutOfMemory; errdefer c.wlr_output_layout_destroy(self.wlr_output_layout); self.outputs = std.TailQueue(Output).init(); - const noop_wlr_output = c.river_wlr_noop_add_output(server.noop_backend) orelse - return error.CantAddNoopOutput; + const noop_wlr_output = c.river_wlr_noop_add_output(server.noop_backend) orelse return error.OutOfMemory; try self.noop_output.init(self, noop_wlr_output); - if (build_options.xwayland) { - self.xwayland_unmanaged_views = std.TailQueue(XwaylandUnmanaged).init(); - } + if (build_options.xwayland) self.xwayland_unmanaged_views = std.TailQueue(XwaylandUnmanaged).init(); self.pending_configures = 0; @@ -76,7 +72,7 @@ pub fn init(self: *Self, server: *Server) !void { self.server.wl_event_loop, handleTimeout, self, - ) orelse return error.CantCreateTimer; + ) orelse return error.AddTimerError; } pub fn deinit(self: *Self) void { diff --git a/river/Seat.zig b/river/Seat.zig index d025921..9991963 100644 --- a/river/Seat.zig +++ b/river/Seat.zig @@ -75,12 +75,11 @@ pub fn init(self: *Self, input_manager: *InputManager, name: []const u8) !void { self.input_manager = input_manager; // This will be automatically destroyed when the display is destroyed - self.wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name.ptr) orelse - return error.CantCreateWlrSeat; + self.wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name.ptr) orelse return error.OutOfMemory; self.wlr_seat.data = self; try self.cursor.init(self); - errdefer self.cursor.destroy(); + errdefer self.cursor.deinit(); self.keyboards = std.TailQueue(Keyboard).init(); diff --git a/river/Server.zig b/river/Server.zig index 7269b96..b31858a 100644 --- a/river/Server.zig +++ b/river/Server.zig @@ -145,11 +145,11 @@ pub fn deinit(self: *Self) void { /// Create the socket, start the backend, and setup the environment pub fn start(self: Self) !void { - const socket = c.wl_display_add_socket_auto(self.wl_display) orelse return error.CantAddSocket; - if (!c.river_wlr_backend_start(self.wlr_backend)) return error.CantStartBackend; - if (c.setenv("WAYLAND_DISPLAY", socket, 1) < 0) return error.CantSetEnv; + const socket = c.wl_display_add_socket_auto(self.wl_display) orelse return error.AddSocketError; + if (!c.river_wlr_backend_start(self.wlr_backend)) return error.StartBackendError; + if (c.setenv("WAYLAND_DISPLAY", socket, 1) < 0) return error.SetenvError; if (build_options.xwayland) { - if (c.setenv("DISPLAY", &self.wlr_xwayland.display_name, 1) < 0) return error.CantSetEnv; + if (c.setenv("DISPLAY", &self.wlr_xwayland.display_name, 1) < 0) return error.SetenvError; } } diff --git a/river/StatusManager.zig b/river/StatusManager.zig index 4c789e1..7481f3c 100644 --- a/river/StatusManager.zig +++ b/river/StatusManager.zig @@ -51,7 +51,7 @@ pub fn init(self: *Self, server: *Server) !void { protocol_version, self, bind, - ) orelse return error.CantCreateWlGlobal; + ) orelse return error.OutOfMemory; self.listen_display_destroy.notify = handleDisplayDestroy; c.wl_display_add_destroy_listener(server.wl_display, &self.listen_display_destroy); diff --git a/riverctl/main.zig b/riverctl/main.zig index 8e02a06..ec804d7 100644 --- a/riverctl/main.zig +++ b/riverctl/main.zig @@ -36,11 +36,10 @@ var river_control_optional: ?*c.zriver_control_v1 = null; var wl_seat_optional: ?*c.wl_seat = null; pub fn main() !void { - const wl_display = c.wl_display_connect(null) orelse return error.CantConnectToDisplay; + const wl_display = c.wl_display_connect(null) orelse return error.ConnectError; const wl_registry = c.wl_display_get_registry(wl_display); - if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) - return error.FailedToAddListener; + if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) unreachable; if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed; const river_control = river_control_optional orelse return error.RiverControlNotAdvertised; @@ -56,7 +55,7 @@ pub fn main() !void { command_callback, &command_callback_listener, null, - ) < 0) return error.FailedToAddListener; + ) < 0) unreachable; // Loop until our callback is called and we exit. while (true) if (c.wl_display_dispatch(wl_display) < 0) return error.DispatchFailed;