Simplify initialization functions

This commit is contained in:
Isaac Freund 2020-03-25 16:24:21 +01:00
parent 34e47360f5
commit ac0c0449dc
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
3 changed files with 53 additions and 77 deletions

View file

@ -34,84 +34,66 @@ pub const Cursor = struct {
grab_height: c_int,
resize_edges: u32,
pub fn create(seat: *Seat) !Self {
const cursor = Self{
.seat = seat,
pub fn init(self: *Self, seat: *Seat) !void {
self.seat = seat;
// Creates a wlroots utility for tracking the cursor image shown on screen.
//
// TODO: free this, it allocates!
.wlr_cursor = c.wlr_cursor_create() orelse
return error.CantCreateWlrCursor,
// Creates a wlroots utility for tracking the cursor image shown on screen.
//
// TODO: free this, it allocates!
self.wlr_cursor = c.wlr_cursor_create() orelse
return error.CantCreateWlrCursor;
// 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.
//
// TODO: free this, it allocates!
.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse
return error.CantCreateWlrXCursorManager,
// 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.
//
// TODO: free this, it allocates!
self.wlr_xcursor_manager = c.wlr_xcursor_manager_create(null, 24) orelse
return error.CantCreateWlrXCursorManager;
.listen_motion = c.wl_listener{
.link = undefined,
.notify = handleMotion,
},
.listen_motion_absolute = c.wl_listener{
.link = undefined,
.notify = handleMotionAbsolute,
},
.listen_button = c.wl_listener{
.link = undefined,
.notify = handleButton,
},
.listen_axis = c.wl_listener{
.link = undefined,
.notify = handleAxis,
},
.listen_frame = c.wl_listener{
.link = undefined,
.notify = handleFrame,
},
c.wlr_cursor_attach_output_layout(self.wlr_cursor, seat.server.root.wlr_output_layout);
_ = c.wlr_xcursor_manager_load(self.wlr_xcursor_manager, 1);
.listen_request_set_cursor = c.wl_listener{
.link = undefined,
.notify = handleRequestSetCursor,
},
self.mode = CursorMode.Passthrough;
self.grabbed_view = null;
self.grab_x = 0.0;
self.grab_y = 0.0;
self.grab_width = 0;
self.grab_height = 0;
self.resize_edges = 0;
.mode = CursorMode.Passthrough,
.grabbed_view = null,
.grab_x = 0.0,
.grab_y = 0.0,
.grab_width = 0,
.grab_height = 0,
.resize_edges = 0,
};
c.wlr_cursor_attach_output_layout(cursor.wlr_cursor, seat.server.root.wlr_output_layout);
_ = c.wlr_xcursor_manager_load(cursor.wlr_xcursor_manager, 1);
return cursor;
}
pub fn init(self: *Self) void {
// 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
// it will generate aggregate events for all of them. In these events, we
// can choose how we want to process them, forwarding them to clients and
// moving the cursor around. See following post for more detail:
// https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html
self.listen_motion.notify = handleMotion;
c.wl_signal_add(&self.wlr_cursor.events.motion, &self.listen_motion);
self.listen_motion_absolute.notify = handleMotionAbsolute;
c.wl_signal_add(&self.wlr_cursor.events.motion_absolute, &self.listen_motion_absolute);
self.listen_button.notify = handleButton;
c.wl_signal_add(&self.wlr_cursor.events.button, &self.listen_button);
self.listen_axis.notify = handleAxis;
c.wl_signal_add(&self.wlr_cursor.events.axis, &self.listen_axis);
self.listen_frame.notify = handleFrame;
c.wl_signal_add(&self.wlr_cursor.events.frame, &self.listen_frame);
// This listens for clients requesting a specific cursor image
self.listen_request_set_cursor.notify = handleRequestSetCursor;
c.wl_signal_add(&self.seat.wlr_seat.events.request_set_cursor, &self.listen_request_set_cursor);
}
pub fn destroy(self: *Self) void {
c.wlr_xcursor_manager_destroy(self.wlr_xcursor_manager);
c.wlr_cursor_destroy(self.wlr_cursor);
}
fn processMove(self: *Self, time: u32) void {
// Move the grabbed view to the new position.
// TODO: log on null
@ -215,7 +197,7 @@ pub const Cursor = struct {
c.wlr_seat_pointer_notify_enter(wlr_seat, surface, sx, sy);
if (!focus_changed) {
// The enter event contains coordinates, so we only need to notify
// on motion if the focus did not change.
//on motion if the focus did not change.
c.wlr_seat_pointer_notify_motion(wlr_seat, time, sx, sy);
}
} else {

View file

@ -18,34 +18,29 @@ pub const Seat = struct {
// Mulitple keyboards are handled separately
keyboards: std.TailQueue(Keyboard),
pub fn create(server: *Server) !Self {
var seat = Self{
.server = server,
.wlr_seat = undefined,
.listen_new_input = c.wl_listener{
.link = undefined,
.notify = handleNewInput,
},
.cursor = undefined,
.keyboards = std.TailQueue(Keyboard).init(),
};
pub fn init(self: *Self, server: *Server) !void {
self.server = server;
// This seems to be the default seat name used by compositors
seat.wlr_seat = c.wlr_seat_create(server.wl_display, "seat0") orelse
// This will be automatically destroyed when the display is destroyed
self.wlr_seat = c.wlr_seat_create(server.wl_display, "seat0") orelse
return error.CantCreateWlrSeat;
return seat;
}
try self.cursor.init(self);
errdefer self.cursor.destroy();
pub fn init(self: *Self) !void {
self.cursor = try Cursor.create(self);
self.cursor.init();
self.keyboards = std.TailQueue(Keyboard).init();
// Set up handler for all new input devices made available. This
// includes keyboards, pointers, touch, etc.
self.listen_new_input.notify = handleNewInput;
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input);
}
pub fn destroy(self: *Self) void {
self.cursor.destroy();
}
fn addKeyboard(self: *Self, device: *c.wlr_input_device) !void {
c.wlr_seat_set_keyboard(self.wlr_seat, device);

View file

@ -60,8 +60,7 @@ pub const Server = struct {
try self.root.init(self);
self.seat = try Seat.create(self);
try self.seat.init();
try self.seat.init(self);
// Register our listeners for new outputs and xdg_surfaces.
self.listen_new_output.notify = handle_new_output;