code: improve error handling consistency

This commit is contained in:
Isaac Freund 2020-06-26 00:59:31 +02:00
parent 8fc045b445
commit 79bb0accac
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
11 changed files with 23 additions and 40 deletions

View file

@ -47,7 +47,7 @@ pub fn init(self: *Self, server: *Server) !void {
protocol_version, protocol_version,
self, self,
bind, bind,
) orelse return error.CantCreateWlGlobal; ) orelse return error.OutOfMemory;
self.args_map = std.AutoHashMap(u32, std.ArrayList([]const u8)).init(util.gpa); self.args_map = std.AutoHashMap(u32, std.ArrayList([]const u8)).init(util.gpa);

View file

@ -59,15 +59,13 @@ pub fn init(self: *Self, seat: *Seat) !void {
self.seat = seat; self.seat = seat;
// Creates a wlroots utility for tracking the cursor image shown on screen. // Creates a wlroots utility for tracking the cursor image shown on screen.
self.wlr_cursor = c.wlr_cursor_create() orelse self.wlr_cursor = c.wlr_cursor_create() orelse return error.OutOfMemory;
return error.CantCreateWlrCursor;
// Creates an xcursor manager, another wlroots utility which loads up // Creates an xcursor manager, another wlroots utility which loads up
// Xcursor themes to source cursor images from and makes sure that cursor // Xcursor themes to source cursor images from and makes sure that cursor
// images are available at all scale factors on the screen (necessary for // 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. // 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 self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse return error.OutOfMemory;
return error.CantCreateWlrXCursorManager;
c.wlr_cursor_attach_output_layout(self.wlr_cursor, seat.input_manager.server.root.wlr_output_layout); 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 (c.wlr_xcursor_manager_load(self.wlr_xcursor_manager, 1) == 0) {
if (build_options.xwayland) { if (build_options.xwayland) {

View file

@ -33,7 +33,7 @@ listen_new_toplevel_decoration: c.wl_listener,
pub fn init(self: *Self, server: *Server) !void { pub fn init(self: *Self, server: *Server) !void {
self.wlr_xdg_decoration_manager = c.wlr_xdg_decoration_manager_v1_create(server.wl_display) orelse 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; self.listen_new_toplevel_decoration.notify = handleNewToplevelDecoration;
c.wl_signal_add( c.wl_signal_add(

View file

@ -46,8 +46,7 @@ pub fn init(self: *Self, server: *Server) !void {
// This is automatically freed when the display is destroyed // This is automatically freed when the display is destroyed
self.wlr_input_inhibit_manager = self.wlr_input_inhibit_manager =
c.wlr_input_inhibit_manager_create(server.wl_display) orelse c.wlr_input_inhibit_manager_create(server.wl_display) orelse return error.OutOfMemory;
return error.CantCreateInputInhibitManager;
self.seats = std.TailQueue(Seat).init(); self.seats = std.TailQueue(Seat).init();

View file

@ -46,16 +46,14 @@ pub fn init(self: *Self, seat: *Seat, wlr_input_device: *c.wlr_input_device) !vo
.variant = null, .variant = null,
.options = null, .options = null,
}; };
const context = c.xkb_context_new(.XKB_CONTEXT_NO_FLAGS) orelse const context = c.xkb_context_new(.XKB_CONTEXT_NO_FLAGS) orelse return error.CreateXkbContextError;
return error.CantCreateXkbContext;
defer c.xkb_context_unref(context); defer c.xkb_context_unref(context);
const keymap = c.xkb_keymap_new_from_names( const keymap = c.xkb_keymap_new_from_names(
context, context,
&rules, &rules,
.XKB_KEYMAP_COMPILE_NO_FLAGS, .XKB_KEYMAP_COMPILE_NO_FLAGS,
) orelse ) orelse return error.CreateXkbKeymapError;
return error.CantCreateXkbKeymap;
defer c.xkb_keymap_unref(keymap); defer c.xkb_keymap_unref(keymap);
// TODO: handle failure after https://github.com/swaywm/wlroots/pull/2081 // TODO: handle failure after https://github.com/swaywm/wlroots/pull/2081

View file

@ -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 // refresh rate), and each monitor supports only a specific set of modes. We
// just pick the monitor's preferred mode, a more sophisticated compositor // just pick the monitor's preferred mode, a more sophisticated compositor
// would let the user configure it. // would let the user configure it.
if (c.wlr_output_preferred_mode(wlr_output)) |mode| {
// if not empty
if (c.wl_list_empty(&wlr_output.modes) == 0) {
// TODO: handle failure
const mode = c.wlr_output_preferred_mode(wlr_output);
c.wlr_output_set_mode(wlr_output, mode); c.wlr_output_set_mode(wlr_output, mode);
c.wlr_output_enable(wlr_output, true); c.wlr_output_enable(wlr_output, true);
if (!c.wlr_output_commit(wlr_output)) { if (!c.wlr_output_commit(wlr_output)) return error.OutputCommitFailed;
return error.CantCommitWlrOutputMode;
}
} }
self.root = root; self.root = root;

View file

@ -56,19 +56,15 @@ pub fn init(self: *Self, server: *Server) !void {
// Create an output layout, which a wlroots utility for working with an // Create an output layout, which a wlroots utility for working with an
// arrangement of screens in a physical layout. // arrangement of screens in a physical layout.
self.wlr_output_layout = c.wlr_output_layout_create() orelse self.wlr_output_layout = c.wlr_output_layout_create() orelse return error.OutOfMemory;
return error.CantCreateWlrOutputLayout;
errdefer c.wlr_output_layout_destroy(self.wlr_output_layout); errdefer c.wlr_output_layout_destroy(self.wlr_output_layout);
self.outputs = std.TailQueue(Output).init(); self.outputs = std.TailQueue(Output).init();
const noop_wlr_output = c.river_wlr_noop_add_output(server.noop_backend) orelse const noop_wlr_output = c.river_wlr_noop_add_output(server.noop_backend) orelse return error.OutOfMemory;
return error.CantAddNoopOutput;
try self.noop_output.init(self, noop_wlr_output); try self.noop_output.init(self, noop_wlr_output);
if (build_options.xwayland) { if (build_options.xwayland) self.xwayland_unmanaged_views = std.TailQueue(XwaylandUnmanaged).init();
self.xwayland_unmanaged_views = std.TailQueue(XwaylandUnmanaged).init();
}
self.pending_configures = 0; self.pending_configures = 0;
@ -76,7 +72,7 @@ pub fn init(self: *Self, server: *Server) !void {
self.server.wl_event_loop, self.server.wl_event_loop,
handleTimeout, handleTimeout,
self, self,
) orelse return error.CantCreateTimer; ) orelse return error.AddTimerError;
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {

View file

@ -75,12 +75,11 @@ pub fn init(self: *Self, input_manager: *InputManager, name: []const u8) !void {
self.input_manager = input_manager; self.input_manager = input_manager;
// This will be automatically destroyed when the display is destroyed // 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 self.wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name.ptr) orelse return error.OutOfMemory;
return error.CantCreateWlrSeat;
self.wlr_seat.data = self; self.wlr_seat.data = self;
try self.cursor.init(self); try self.cursor.init(self);
errdefer self.cursor.destroy(); errdefer self.cursor.deinit();
self.keyboards = std.TailQueue(Keyboard).init(); self.keyboards = std.TailQueue(Keyboard).init();

View file

@ -145,11 +145,11 @@ pub fn deinit(self: *Self) void {
/// Create the socket, start the backend, and setup the environment /// Create the socket, start the backend, and setup the environment
pub fn start(self: Self) !void { pub fn start(self: Self) !void {
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse return error.CantAddSocket; 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.CantStartBackend; if (!c.river_wlr_backend_start(self.wlr_backend)) return error.StartBackendError;
if (c.setenv("WAYLAND_DISPLAY", socket, 1) < 0) return error.CantSetEnv; if (c.setenv("WAYLAND_DISPLAY", socket, 1) < 0) return error.SetenvError;
if (build_options.xwayland) { 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;
} }
} }

View file

@ -51,7 +51,7 @@ pub fn init(self: *Self, server: *Server) !void {
protocol_version, protocol_version,
self, self,
bind, bind,
) orelse return error.CantCreateWlGlobal; ) orelse return error.OutOfMemory;
self.listen_display_destroy.notify = handleDisplayDestroy; self.listen_display_destroy.notify = handleDisplayDestroy;
c.wl_display_add_destroy_listener(server.wl_display, &self.listen_display_destroy); c.wl_display_add_destroy_listener(server.wl_display, &self.listen_display_destroy);

View file

@ -36,11 +36,10 @@ var river_control_optional: ?*c.zriver_control_v1 = null;
var wl_seat_optional: ?*c.wl_seat = null; var wl_seat_optional: ?*c.wl_seat = null;
pub fn main() !void { 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); const wl_registry = c.wl_display_get_registry(wl_display);
if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) unreachable;
return error.FailedToAddListener;
if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed; if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed;
const river_control = river_control_optional orelse return error.RiverControlNotAdvertised; const river_control = river_control_optional orelse return error.RiverControlNotAdvertised;
@ -56,7 +55,7 @@ pub fn main() !void {
command_callback, command_callback,
&command_callback_listener, &command_callback_listener,
null, null,
) < 0) return error.FailedToAddListener; ) < 0) unreachable;
// Loop until our callback is called and we exit. // Loop until our callback is called and we exit.
while (true) if (c.wl_display_dispatch(wl_display) < 0) return error.DispatchFailed; while (true) if (c.wl_display_dispatch(wl_display) < 0) return error.DispatchFailed;