Use Self = @This() in all structs

This seems to be the idiomatic way to do things.
This commit is contained in:
Isaac Freund 2020-03-24 20:48:38 +01:00
parent 45c7e27935
commit 46fe1baa96
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
6 changed files with 42 additions and 30 deletions

View file

@ -12,6 +12,8 @@ const CursorMode = enum {
}; };
pub const Cursor = struct { pub const Cursor = struct {
const Self = @This();
seat: *Seat, seat: *Seat,
wlr_cursor: *c.wlr_cursor, wlr_cursor: *c.wlr_cursor,
wlr_xcursor_manager: *c.wlr_xcursor_manager, wlr_xcursor_manager: *c.wlr_xcursor_manager,
@ -32,8 +34,8 @@ pub const Cursor = struct {
grab_height: c_int, grab_height: c_int,
resize_edges: u32, resize_edges: u32,
pub fn create(seat: *Seat) !@This() { pub fn create(seat: *Seat) !Self {
const cursor = @This(){ const cursor = Self{
.seat = seat, .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.
@ -53,28 +55,28 @@ pub const Cursor = struct {
.listen_motion = c.wl_listener{ .listen_motion = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_motion, .notify = handle_motion,
}, },
.listen_motion_absolute = c.wl_listener{ .listen_motion_absolute = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_motion_absolute, .notify = handle_motion_absolute,
}, },
.listen_button = c.wl_listener{ .listen_button = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_button, .notify = handle_button,
}, },
.listen_axis = c.wl_listener{ .listen_axis = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_axis, .notify = handle_axis,
}, },
.listen_frame = c.wl_listener{ .listen_frame = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_frame, .notify = handle_frame,
}, },
.listen_request_set_cursor = c.wl_listener{ .listen_request_set_cursor = c.wl_listener{
.link = undefined, .link = undefined,
.notify = @This().handle_request_set_cursor, .notify = handle_request_set_cursor,
}, },
.mode = CursorMode.Passthrough, .mode = CursorMode.Passthrough,
@ -93,7 +95,7 @@ pub const Cursor = struct {
return cursor; return cursor;
} }
pub fn init(self: *@This()) void { pub fn init(self: *Self) void {
// wlr_cursor *only* displays an image on screen. It does not move around // 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 // 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 // it will generate aggregate events for all of them. In these events, we
@ -110,7 +112,7 @@ pub const Cursor = struct {
c.wl_signal_add(&self.seat.wlr_seat.events.request_set_cursor, &self.listen_request_set_cursor); c.wl_signal_add(&self.seat.wlr_seat.events.request_set_cursor, &self.listen_request_set_cursor);
} }
fn process_move(self: *@This(), time: u32) void { fn process_move(self: *Self, time: u32) void {
// Move the grabbed view to the new position. // Move the grabbed view to the new position.
// TODO: log on null // TODO: log on null
if (self.grabbed_view) |view| { if (self.grabbed_view) |view| {
@ -119,7 +121,7 @@ pub const Cursor = struct {
} }
} }
fn process_resize(self: *@This(), time: u32) void { fn process_resize(self: *Self, time: u32) void {
// Resizing the grabbed view can be a little bit complicated, because we // Resizing the grabbed view can be a little bit complicated, because we
// could be resizing from any corner or edge. This not only resizes the view // could be resizing from any corner or edge. This not only resizes the view
// on one or two axes, but can also move the view if you resize from the top // on one or two axes, but can also move the view if you resize from the top
@ -168,7 +170,7 @@ pub const Cursor = struct {
); );
} }
fn process_motion(self: *@This(), time: u32) void { fn process_motion(self: *Self, time: u32) void {
// If the mode is non-passthrough, delegate to those functions. // If the mode is non-passthrough, delegate to those functions.
if (self.mode == CursorMode.Move) { if (self.mode == CursorMode.Move) {
self.process_move(time); self.process_move(time);

View file

@ -4,6 +4,8 @@ const c = @import("c.zig").c;
const Seat = @import("seat.zig").Seat; const Seat = @import("seat.zig").Seat;
pub const Keyboard = struct { pub const Keyboard = struct {
const Self = @This();
seat: *Seat, seat: *Seat,
device: *c.wlr_input_device, device: *c.wlr_input_device,
wlr_keyboard: *c.wlr_keyboard, wlr_keyboard: *c.wlr_keyboard,
@ -11,7 +13,7 @@ pub const Keyboard = struct {
listen_modifiers: c.wl_listener, listen_modifiers: c.wl_listener,
listen_key: c.wl_listener, listen_key: c.wl_listener,
pub fn init(self: *@This(), seat: *Seat, device: *c.wlr_input_device) !void { pub fn init(self: *Self, seat: *Seat, device: *c.wlr_input_device) !void {
self.seat = seat; self.seat = seat;
self.device = device; self.device = device;
self.wlr_keyboard = device.unnamed_37.keyboard; self.wlr_keyboard = device.unnamed_37.keyboard;

View file

@ -12,11 +12,13 @@ const RenderData = struct {
}; };
pub const Output = struct { pub const Output = struct {
const Self = @This();
server: *Server, server: *Server,
wlr_output: *c.wlr_output, wlr_output: *c.wlr_output,
listen_frame: c.wl_listener, listen_frame: c.wl_listener,
pub fn init(self: *@This(), server: *Server, wlr_output: *c.wlr_output) !void { pub fn init(self: *Self, server: *Server, wlr_output: *c.wlr_output) !void {
// Some backends don't have modes. DRM+KMS does, and we need to set a mode // Some backends don't have modes. DRM+KMS does, and we need to set a mode
// before we can use the output. The mode is a tuple of (width, height, // before we can use the output. The mode is a tuple of (width, height,
// 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

View file

@ -7,6 +7,8 @@ const Server = @import("server.zig").Server;
// TODO: InputManager and multi-seat support // TODO: InputManager and multi-seat support
pub const Seat = struct { pub const Seat = struct {
const Self = @This();
server: *Server, server: *Server,
wlr_seat: *c.wlr_seat, wlr_seat: *c.wlr_seat,
listen_new_input: c.wl_listener, listen_new_input: c.wl_listener,
@ -16,8 +18,8 @@ pub const Seat = struct {
// Mulitple keyboards are handled separately // Mulitple keyboards are handled separately
keyboards: std.TailQueue(Keyboard), keyboards: std.TailQueue(Keyboard),
pub fn create(server: *Server) !@This() { pub fn create(server: *Server) !Self {
var seat = @This(){ var seat = Self{
.server = server, .server = server,
.wlr_seat = undefined, .wlr_seat = undefined,
.listen_new_input = c.wl_listener{ .listen_new_input = c.wl_listener{
@ -35,7 +37,7 @@ pub const Seat = struct {
return seat; return seat;
} }
pub fn init(self: *@This()) !void { pub fn init(self: *Self) !void {
self.cursor = try Cursor.create(self); self.cursor = try Cursor.create(self);
self.cursor.init(); self.cursor.init();
@ -44,7 +46,7 @@ pub const Seat = struct {
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input); c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input);
} }
fn add_keyboard(self: *@This(), device: *c.wlr_input_device) !void { fn add_keyboard(self: *Self, device: *c.wlr_input_device) !void {
c.wlr_seat_set_keyboard(self.wlr_seat, device); c.wlr_seat_set_keyboard(self.wlr_seat, device);
const node = try self.keyboards.allocateNode(self.server.allocator); const node = try self.keyboards.allocateNode(self.server.allocator);
@ -52,7 +54,7 @@ pub const Seat = struct {
self.keyboards.append(node); self.keyboards.append(node);
} }
fn add_pointer(self: *@This(), device: *c.struct_wlr_input_device) void { fn add_pointer(self: *Self, device: *c.struct_wlr_input_device) void {
// We don't do anything special with pointers. All of our pointer handling // We don't do anything special with pointers. All of our pointer handling
// is proxied through wlr_cursor. On another compositor, you might take this // is proxied through wlr_cursor. On another compositor, you might take this
// opportunity to do libinput configuration on the device to set // opportunity to do libinput configuration on the device to set

View file

@ -6,6 +6,8 @@ const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View; const View = @import("view.zig").View;
pub const Server = struct { pub const Server = struct {
const Self = @This();
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
wl_display: *c.wl_display, wl_display: *c.wl_display,
@ -25,8 +27,8 @@ pub const Server = struct {
seat: Seat, seat: Seat,
pub fn create(allocator: *std.mem.Allocator) !@This() { pub fn create(allocator: *std.mem.Allocator) !Self {
var server: @This() = undefined; var server: Self = undefined;
server.allocator = allocator; server.allocator = allocator;
// The Wayland display is managed by libwayland. It handles accepting // The Wayland display is managed by libwayland. It handles accepting
@ -76,7 +78,7 @@ pub const Server = struct {
return server; return server;
} }
pub fn init(self: *@This()) !void { pub fn init(self: *Self) !void {
self.seat = try Seat.create(self); self.seat = try Seat.create(self);
try self.seat.init(); try self.seat.init();
@ -89,14 +91,14 @@ pub const Server = struct {
} }
/// Free allocated memory and clean up /// Free allocated memory and clean up
pub fn deinit(self: @This()) void { pub fn deinit(self: Self) void {
c.wl_display_destroy_clients(self.wl_display); c.wl_display_destroy_clients(self.wl_display);
c.wl_display_destroy(self.wl_display); c.wl_display_destroy(self.wl_display);
c.wlr_output_layout_destroy(self.wlr_output_layout); c.wlr_output_layout_destroy(self.wlr_output_layout);
} }
/// Create the socket, set WAYLAND_DISPLAY, and start the backend /// Create the socket, set WAYLAND_DISPLAY, and start the backend
pub fn start(self: @This()) !void { pub fn start(self: Self) !void {
// Add a Unix socket to the Wayland display. // Add a Unix socket to the Wayland display.
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse const socket = c.wl_display_add_socket_auto(self.wl_display) orelse
return error.CantAddSocket; return error.CantAddSocket;
@ -115,11 +117,11 @@ pub const Server = struct {
} }
/// Enter the wayland event loop and block until the compositor is exited /// Enter the wayland event loop and block until the compositor is exited
pub fn run(self: @This()) void { pub fn run(self: Self) void {
c.wl_display_run(self.wl_display); c.wl_display_run(self.wl_display);
} }
pub fn handle_keybinding(self: *@This(), sym: c.xkb_keysym_t) bool { pub fn handle_keybinding(self: *Self, sym: c.xkb_keysym_t) bool {
// Here we handle compositor keybindings. This is when the compositor is // Here we handle compositor keybindings. This is when the compositor is
// processing keys, rather than passing them on to the client for its own // processing keys, rather than passing them on to the client for its own
// processing. // processing.
@ -171,7 +173,7 @@ pub const Server = struct {
/// Finds the top most view under the output layout coordinates lx, ly /// Finds the top most view under the output layout coordinates lx, ly
/// returns the view if found, and a pointer to the wlr_surface as well as the surface coordinates /// returns the view if found, and a pointer to the wlr_surface as well as the surface coordinates
pub fn desktop_view_at(self: *@This(), lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View { pub fn desktop_view_at(self: *Self, lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View {
var it = self.views.last; var it = self.views.last;
while (it) |node| : (it = node.prev) { while (it) |node| : (it = node.prev) {
if (node.data.is_at(lx, ly, surface, sx, sy)) { if (node.data.is_at(lx, ly, surface, sx, sy)) {

View file

@ -4,6 +4,8 @@ const c = @import("c.zig").c;
const Server = @import("server.zig").Server; const Server = @import("server.zig").Server;
pub const View = struct { pub const View = struct {
const Self = @This();
server: *Server, server: *Server,
wlr_xdg_surface: *c.wlr_xdg_surface, wlr_xdg_surface: *c.wlr_xdg_surface,
@ -17,7 +19,7 @@ pub const View = struct {
// listen_request_move: c.wl_listener, // listen_request_move: c.wl_listener,
// listen_request_resize: c.wl_listener, // listen_request_resize: c.wl_listener,
pub fn init(self: *@This(), server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void { pub fn init(self: *Self, server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void {
self.server = server; self.server = server;
self.wlr_xdg_surface = wlr_xdg_surface; self.wlr_xdg_surface = wlr_xdg_surface;
@ -74,7 +76,7 @@ pub const View = struct {
// // ignore for now // // ignore for now
// } // }
fn focus(self: *@This(), surface: *c.wlr_surface) void { fn focus(self: *Self, surface: *c.wlr_surface) void {
const server = self.server; const server = self.server;
const wlr_seat = server.seat.wlr_seat; const wlr_seat = server.seat.wlr_seat;
const prev_surface = wlr_seat.keyboard_state.focused_surface; const prev_surface = wlr_seat.keyboard_state.focused_surface;
@ -120,7 +122,7 @@ pub const View = struct {
); );
} }
fn is_at(self: *@This(), lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) bool { fn is_at(self: *Self, lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) bool {
// XDG toplevels may have nested surfaces, such as popup windows for context // XDG toplevels may have nested surfaces, such as popup windows for context
// menus or tooltips. This function tests if any of those are underneath the // menus or tooltips. This function tests if any of those are underneath the
// coordinates lx and ly (in output Layout Coordinates). If so, it sets the // coordinates lx and ly (in output Layout Coordinates). If so, it sets the