From 71a751f1ad405dbfcaa2abcac6edd0d2fdc94512 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Fri, 21 Aug 2020 19:08:52 +0200 Subject: [PATCH] seat: clean up initialization --- river/Cursor.zig | 37 +++++++++++++++++++------------------ river/InputManager.zig | 42 +++++++++++++++++------------------------- river/Seat.zig | 41 ++++++++++++++--------------------------- 3 files changed, 50 insertions(+), 70 deletions(-) diff --git a/river/Cursor.zig b/river/Cursor.zig index 4e24b7e..1ca75d7 100644 --- a/river/Cursor.zig +++ b/river/Cursor.zig @@ -212,34 +212,35 @@ wlr_cursor: *c.wlr_cursor, wlr_xcursor_manager: *c.wlr_xcursor_manager, /// Number of distinct buttons currently pressed -pressed_count: u32, +pressed_count: u32 = 0, /// Current cursor mode as well as any state needed to implement that mode -mode: Mode, +mode: Mode = .passthrough, -listen_axis: c.wl_listener, -listen_button: c.wl_listener, -listen_frame: c.wl_listener, -listen_motion_absolute: c.wl_listener, -listen_motion: c.wl_listener, -listen_request_set_cursor: c.wl_listener, +listen_axis: c.wl_listener = undefined, +listen_button: c.wl_listener = undefined, +listen_frame: c.wl_listener = undefined, +listen_motion_absolute: c.wl_listener = undefined, +listen_motion: c.wl_listener = undefined, +listen_request_set_cursor: c.wl_listener = undefined, 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.OutOfMemory; - c.wlr_cursor_attach_output_layout(self.wlr_cursor, seat.input_manager.server.root.wlr_output_layout); + const wlr_cursor = c.wlr_cursor_create() orelse return error.OutOfMemory; + errdefer c.wlr_cursor_destroy(wlr_cursor); + c.wlr_cursor_attach_output_layout(wlr_cursor, seat.input_manager.server.root.wlr_output_layout); // This is here so that self.wlr_xcursor_manager doesn't need to be an // optional pointer. This isn't optimal as it does a needless allocation, // but this is not a hot path. - self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, default_size) orelse - return error.OutOfMemory; - try self.setTheme(null, null); + const wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, default_size) orelse return error.OutOfMemory; + errdefer c.wlr_xcursor_manager_destroy(wlr_xcursor_manager); - self.pressed_count = 0; - self.mode = .passthrough; + self.* = .{ + .seat = seat, + .wlr_cursor = wlr_cursor, + .wlr_xcursor_manager = wlr_xcursor_manager, + }; + try self.setTheme(null, null); // wlr_cursor *only* displays an image on screen. It does not move around // when the pointer moves. However, we can attach input devices to it, and diff --git a/river/InputManager.zig b/river/InputManager.zig index a4e39ed..b6f5866 100644 --- a/river/InputManager.zig +++ b/river/InputManager.zig @@ -35,46 +35,38 @@ server: *Server, wlr_idle: *c.wlr_idle, wlr_input_inhibit_manager: *c.wlr_input_inhibit_manager, -seats: std.TailQueue(Seat), +seats: std.TailQueue(Seat) = std.TailQueue(Seat).init(), default_seat: *Seat, -exclusive_client: ?*c.wl_client, +exclusive_client: ?*c.wl_client = null, -listen_inhibit_activate: c.wl_listener, -listen_inhibit_deactivate: c.wl_listener, -listen_new_input: c.wl_listener, +listen_inhibit_activate: c.wl_listener = undefined, +listen_inhibit_deactivate: c.wl_listener = undefined, +listen_new_input: c.wl_listener = undefined, pub fn init(self: *Self, server: *Server) !void { - self.server = server; - - // These are automatically freed when the display is destroyed - self.wlr_idle = c.wlr_idle_create(server.wl_display) orelse return error.OutOfMemory; - self.wlr_input_inhibit_manager = c.wlr_input_inhibit_manager_create(server.wl_display) orelse - return error.OutOfMemory; - - self.seats = std.TailQueue(Seat).init(); - const seat_node = try util.gpa.create(std.TailQueue(Seat).Node); + + self.* = .{ + .server = server, + // These are automatically freed when the display is destroyed + .wlr_idle = c.wlr_idle_create(server.wl_display) orelse return error.OutOfMemory, + .wlr_input_inhibit_manager = c.wlr_input_inhibit_manager_create(server.wl_display) orelse + return error.OutOfMemory, + .default_seat = &seat_node.data, + }; + try seat_node.data.init(self, default_seat_name); - self.default_seat = &seat_node.data; self.seats.prepend(seat_node); if (build_options.xwayland) c.wlr_xwayland_set_seat(server.wlr_xwayland, self.default_seat.wlr_seat); - self.exclusive_client = null; - // Set up all listeners self.listen_inhibit_activate.notify = handleInhibitActivate; - c.wl_signal_add( - &self.wlr_input_inhibit_manager.events.activate, - &self.listen_inhibit_activate, - ); + c.wl_signal_add(&self.wlr_input_inhibit_manager.events.activate, &self.listen_inhibit_activate); self.listen_inhibit_deactivate.notify = handleInhibitDeactivate; - c.wl_signal_add( - &self.wlr_input_inhibit_manager.events.deactivate, - &self.listen_inhibit_deactivate, - ); + c.wl_signal_add(&self.wlr_input_inhibit_manager.events.deactivate, &self.listen_inhibit_deactivate); self.listen_new_input.notify = handleNewInput; c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input); diff --git a/river/Seat.zig b/river/Seat.zig index 8f03550..7ab0fb6 100644 --- a/river/Seat.zig +++ b/river/Seat.zig @@ -44,55 +44,42 @@ input_manager: *InputManager, wlr_seat: *c.wlr_seat, /// Multiple mice are handled by the same Cursor -cursor: Cursor, +cursor: Cursor = undefined, /// Mulitple keyboards are handled separately -keyboards: std.TailQueue(Keyboard), +keyboards: std.TailQueue(Keyboard) = std.TailQueue(Keyboard).init(), /// ID of the current keymap mode -mode_id: usize, +mode_id: usize = 0, /// Currently focused output, may be the noop output if no focused_output: *Output, /// Currently focused view/layer surface if any -focused: FocusTarget, +focused: FocusTarget = .none, /// Stack of views in most recently focused order /// If there is a currently focused view, it is on top. -focus_stack: ViewStack(*View), +focus_stack: ViewStack(*View) = ViewStack(*View){}, /// List of status tracking objects relaying changes to this seat to clients. -status_trackers: std.SinglyLinkedList(SeatStatus), +status_trackers: std.SinglyLinkedList(SeatStatus) = std.SinglyLinkedList(SeatStatus).init(), /// State of pointer modifier; Used for pointer operations such as move ans resize. -pointer_modifier: bool, +pointer_modifier: bool = false, -listen_request_set_selection: c.wl_listener, +listen_request_set_selection: c.wl_listener = undefined, pub fn init(self: *Self, input_manager: *InputManager, name: [*:0]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) orelse return error.OutOfMemory; + self.* = .{ + .input_manager = input_manager, + // This will be automatically destroyed when the display is destroyed + .wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name) orelse return error.OutOfMemory, + .focused_output = &self.input_manager.server.root.noop_output, + }; self.wlr_seat.data = self; try self.cursor.init(self); - errdefer self.cursor.deinit(); - - self.keyboards = std.TailQueue(Keyboard).init(); - - self.mode_id = 0; - - self.focused_output = &self.input_manager.server.root.noop_output; - - self.focused = .none; - - self.focus_stack = ViewStack(*View){}; - - self.status_trackers = std.SinglyLinkedList(SeatStatus).init(); - - self.pointer_modifier = false; self.listen_request_set_selection.notify = handleRequestSetSelection; c.wl_signal_add(&self.wlr_seat.events.request_set_selection, &self.listen_request_set_selection);