Add Root struct

Server handles listening for wayland signals. It delegates input handling
to Seat and output handling to Root.
This commit is contained in:
Isaac Freund 2020-03-25 15:59:24 +01:00
parent a3eb33a7b1
commit 34e47360f5
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
6 changed files with 114 additions and 87 deletions

View file

@ -89,7 +89,7 @@ pub const Cursor = struct {
.resize_edges = 0, .resize_edges = 0,
}; };
c.wlr_cursor_attach_output_layout(cursor.wlr_cursor, seat.server.wlr_output_layout); 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); _ = c.wlr_xcursor_manager_load(cursor.wlr_xcursor_manager, 1);
return cursor; return cursor;
@ -184,7 +184,7 @@ pub const Cursor = struct {
var sx: f64 = undefined; var sx: f64 = undefined;
var sy: f64 = undefined; var sy: f64 = undefined;
var opt_surface: ?*c.wlr_surface = null; var opt_surface: ?*c.wlr_surface = null;
const view = self.seat.server.desktop_view_at( const view = self.seat.server.root.viewAt(
self.wlr_cursor.x, self.wlr_cursor.x,
self.wlr_cursor.y, self.wlr_cursor.y,
&opt_surface, &opt_surface,
@ -278,7 +278,7 @@ pub const Cursor = struct {
var sy: f64 = undefined; var sy: f64 = undefined;
var surface: ?*c.wlr_surface = null; var surface: ?*c.wlr_surface = null;
const view = cursor.seat.server.desktop_view_at( const view = cursor.seat.server.root.viewAt(
cursor.wlr_cursor.x, cursor.wlr_cursor.x,
cursor.wlr_cursor.y, cursor.wlr_cursor.y,
&surface, &surface,

View file

@ -8,11 +8,10 @@ pub fn main() !void {
c.wlr_log_init(c.enum_wlr_log_importance.WLR_DEBUG, null); c.wlr_log_init(c.enum_wlr_log_importance.WLR_DEBUG, null);
var server = try Server.create(std.heap.c_allocator); var server: Server = undefined;
try server.init(std.heap.c_allocator);
defer server.destroy(); defer server.destroy();
try server.init();
try server.start(); try server.start();
// Spawn an instance of alacritty // Spawn an instance of alacritty

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const Root = @import("root.zig").Root;
const Server = @import("server.zig").Server; const Server = @import("server.zig").Server;
const View = @import("view.zig").View; const View = @import("view.zig").View;
@ -14,11 +15,11 @@ const RenderData = struct {
pub const Output = struct { pub const Output = struct {
const Self = @This(); const Self = @This();
server: *Server, root: *Root,
wlr_output: *c.wlr_output, wlr_output: *c.wlr_output,
listen_frame: c.wl_listener, listen_frame: c.wl_listener,
pub fn init(self: *Self, server: *Server, wlr_output: *c.wlr_output) !void { pub fn init(self: *Self, root: *Root, 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
@ -27,6 +28,7 @@ pub const Output = struct {
// if not empty // if not empty
if (c.wl_list_empty(&wlr_output.modes) == 0) { if (c.wl_list_empty(&wlr_output.modes) == 0) {
// TODO: handle failure
const mode = c.wlr_output_preferred_mode(wlr_output); 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);
@ -35,7 +37,7 @@ pub const Output = struct {
} }
} }
self.server = server; self.root = root;
self.wlr_output = wlr_output; self.wlr_output = wlr_output;
// Sets up a listener for the frame notify event. // Sets up a listener for the frame notify event.
@ -46,7 +48,7 @@ pub const Output = struct {
// from left-to-right in the order they appear. A more sophisticated // from left-to-right in the order they appear. A more sophisticated
// compositor would let the user configure the arrangement of outputs in the // compositor would let the user configure the arrangement of outputs in the
// layout. // layout.
c.wlr_output_layout_add_auto(server.wlr_output_layout, wlr_output); c.wlr_output_layout_add_auto(root.wlr_output_layout, wlr_output);
// Creating the global adds a wl_output global to the display, which Wayland // Creating the global adds a wl_output global to the display, which Wayland
// clients can see to find out information about the output (such as // clients can see to find out information about the output (such as
@ -58,7 +60,7 @@ pub const Output = struct {
// This function is called every time an output is ready to display a frame, // This function is called every time an output is ready to display a frame,
// generally at the output's refresh rate (e.g. 60Hz). // generally at the output's refresh rate (e.g. 60Hz).
const output = @fieldParentPtr(Output, "listen_frame", listener.?); const output = @fieldParentPtr(Output, "listen_frame", listener.?);
const renderer = output.server.wlr_renderer; const renderer = output.root.server.wlr_renderer;
var now: c.struct_timespec = undefined; var now: c.struct_timespec = undefined;
_ = c.clock_gettime(c.CLOCK_MONOTONIC, &now); _ = c.clock_gettime(c.CLOCK_MONOTONIC, &now);
@ -79,7 +81,7 @@ pub const Output = struct {
// Each subsequent view is rendered on top of the last. // Each subsequent view is rendered on top of the last.
// The first view in the list is "on top" so iterate in reverse. // The first view in the list is "on top" so iterate in reverse.
var it = output.server.views.last; var it = output.root.views.last;
while (it) |node| : (it = node.prev) { while (it) |node| : (it = node.prev) {
const view = &node.data; const view = &node.data;
if (!view.mapped) { if (!view.mapped) {
@ -136,7 +138,7 @@ pub const Output = struct {
// output-local coordinates, or (2000 - 1920). // output-local coordinates, or (2000 - 1920).
var ox: f64 = 0.0; var ox: f64 = 0.0;
var oy: f64 = 0.0; var oy: f64 = 0.0;
c.wlr_output_layout_output_coords(view.server.wlr_output_layout, output, &ox, &oy); c.wlr_output_layout_output_coords(view.root.wlr_output_layout, output, &ox, &oy);
ox += @intToFloat(f64, view.x + sx); ox += @intToFloat(f64, view.x + sx);
oy += @intToFloat(f64, view.y + sy); oy += @intToFloat(f64, view.y + sy);

62
src/root.zig Normal file
View file

@ -0,0 +1,62 @@
const std = @import("std");
const c = @import("c.zig").c;
const Output = @import("output.zig").Output;
const Server = @import("server.zig").Server;
const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View;
/// Responsible for all windowing operations
pub const Root = struct {
const Self = @This();
server: *Server,
wlr_output_layout: *c.wlr_output_layout,
outputs: std.TailQueue(Output),
// Must stay ordered, first N views in list are the masters
views: std.TailQueue(View),
pub fn init(self: *Self, server: *Server) !void {
self.server = server;
// 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;
errdefer c.wlr_output_layout_destroy(self.wlr_output_layout);
self.outputs = std.TailQueue(Output).init();
self.views = std.TailQueue(View).init();
}
pub fn destroy(self: *Self) void {
c.wlr_output_layout_destroy(self.wlr_output_layout);
}
pub fn addOutput(self: *Self, wlr_output: *c.wlr_output) void {
// TODO: Handle failure
const node = self.outputs.allocateNode(self.server.allocator) catch unreachable;
node.data.init(self, wlr_output) catch unreachable;
self.outputs.append(node);
}
pub fn addView(self: *Self, wlr_xdg_surface: *c.wlr_xdg_surface) void {
const node = self.views.allocateNode(self.server.allocator) catch unreachable;
node.data.init(self, wlr_xdg_surface);
self.views.append(node);
}
/// 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
pub fn viewAt(self: *Self, lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View {
var it = self.views.last;
while (it) |node| : (it = node.prev) {
if (node.data.isAt(lx, ly, surface, sx, sy)) {
return &node.data;
}
}
return null;
}
};

View file

@ -2,6 +2,7 @@ const std = @import("std");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const Output = @import("output.zig").Output; const Output = @import("output.zig").Output;
const Root = @import("root.zig").Root;
const Seat = @import("seat.zig").Seat; const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View; const View = @import("view.zig").View;
@ -10,91 +11,71 @@ pub const Server = struct {
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
root: Root,
seat: Seat,
wl_display: *c.wl_display, wl_display: *c.wl_display,
wlr_backend: *c.wlr_backend, wlr_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer, wlr_renderer: *c.wlr_renderer,
wlr_output_layout: *c.wlr_output_layout,
outputs: std.TailQueue(Output),
listen_new_output: c.wl_listener, listen_new_output: c.wl_listener,
wlr_xdg_shell: *c.wlr_xdg_shell, wlr_xdg_shell: *c.wlr_xdg_shell,
listen_new_xdg_surface: c.wl_listener, listen_new_xdg_surface: c.wl_listener,
// Must stay ordered, first in list is "on top" visually pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
views: std.TailQueue(View), self.allocator = allocator;
seat: Seat,
pub fn create(allocator: *std.mem.Allocator) !Self {
var server: Self = undefined;
server.allocator = allocator;
// The Wayland display is managed by libwayland. It handles accepting // The Wayland display is managed by libwayland. It handles accepting
// clients from the Unix socket, manging Wayland globals, and so on. // clients from the Unix socket, manging Wayland globals, and so on.
server.wl_display = c.wl_display_create() orelse self.wl_display = c.wl_display_create() orelse
return error.CantCreateWlDisplay; return error.CantCreateWlDisplay;
errdefer c.wl_display_destroy(server.wl_display); errdefer c.wl_display_destroy(self.wl_display);
// The wlr_backend abstracts the input/output hardware. Autocreate chooses // The wlr_backend abstracts the input/output hardware. Autocreate chooses
// the best option based on the environment, for example DRM when run from // the best option based on the environment, for example DRM when run from
// a tty or wayland if WAYLAND_DISPLAY is set. // a tty or wayland if WAYLAND_DISPLAY is set.
// //
// This frees itserver.when the wl_display is destroyed. // This frees itself.when the wl_display is destroyed.
server.wlr_backend = c.zag_wlr_backend_autocreate(server.wl_display) orelse self.wlr_backend = c.zag_wlr_backend_autocreate(self.wl_display) orelse
return error.CantCreateWlrBackend; return error.CantCreateWlrBackend;
// If we don't provide a renderer, autocreate makes a GLES2 renderer for us. // If we don't provide a renderer, autocreate makes a GLES2 renderer for us.
// The renderer is responsible for defining the various pixel formats it // The renderer is responsible for defining the various pixel formats it
// supports for shared memory, this configures that for clients. // supports for shared memory, this configures that for clients.
server.wlr_renderer = c.zag_wlr_backend_get_renderer(server.wlr_backend) orelse self.wlr_renderer = c.zag_wlr_backend_get_renderer(self.wlr_backend) orelse
return error.CantGetWlrRenderer; return error.CantGetWlrRenderer;
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080 // TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(server.wlr_renderer, server.wl_display); // orelse c.wlr_renderer_init_wl_display(self.wlr_renderer, self.wl_display); // orelse
// return error.CantInitWlDisplay; // return error.CantInitWlDisplay;
// These both free themselves when the wl_display is destroyed // These both free themselves when the wl_display is destroyed
_ = c.wlr_compositor_create(server.wl_display, server.wlr_renderer) orelse _ = c.wlr_compositor_create(self.wl_display, self.wlr_renderer) orelse
return error.CantCreateWlrCompositor; return error.CantCreateWlrCompositor;
_ = c.wlr_data_device_manager_create(server.wl_display) orelse _ = c.wlr_data_device_manager_create(self.wl_display) orelse
return error.CantCreateWlrDataDeviceManager; return error.CantCreateWlrDataDeviceManager;
// Create an output layout, which a wlroots utility for working with an self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse
// arrangement of screens in a physical layout.
server.wlr_output_layout = c.wlr_output_layout_create() orelse
return error.CantCreateWlrOutputLayout;
errdefer c.wlr_output_layout_destroy(server.wlr_output_layout);
// Don't register the wl_listeners yet as they must first be pointer-stable
server.outputs = std.TailQueue(Output).init();
server.listen_new_output.notify = handle_new_output;
server.views = std.TailQueue(View).init();
server.wlr_xdg_shell = c.wlr_xdg_shell_create(server.wl_display) orelse
return error.CantCreateWlrXdgShell; return error.CantCreateWlrXdgShell;
server.listen_new_xdg_surface.notify = handle_new_xdg_surface;
return server; try self.root.init(self);
}
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();
// Register our listeners for new outputs and xdg_surfaces. // Register our listeners for new outputs and xdg_surfaces.
// This can't be done in create() as wl_signal_add() creates a pointer self.listen_new_output.notify = handle_new_output;
// to the wl_list link in our wl_listener, a pointer that would be
// broken when returning from create();
c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output); c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
self.listen_new_xdg_surface.notify = handle_new_xdg_surface;
c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface); c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface);
} }
/// Free allocated memory and clean up /// Free allocated memory and clean up
pub fn destroy(self: Self) void { pub fn destroy(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); self.root.destroy();
} }
/// Create the socket, set WAYLAND_DISPLAY, and start the backend /// Create the socket, set WAYLAND_DISPLAY, and start the backend
@ -148,11 +129,7 @@ pub const Server = struct {
fn handle_new_output(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_output(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const server = @fieldParentPtr(Server, "listen_new_output", listener.?); const server = @fieldParentPtr(Server, "listen_new_output", listener.?);
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data)); const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
server.root.addOutput(wlr_output);
// TODO: Handle failure
const node = server.outputs.allocateNode(server.allocator) catch unreachable;
node.data.init(server, wlr_output) catch unreachable;
server.outputs.append(node);
} }
fn handle_new_xdg_surface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handle_new_xdg_surface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -162,24 +139,11 @@ pub const Server = struct {
const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data)); const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data));
if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) { if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
// TODO: log
return; return;
} }
// Create a View to handle this toplevel surface // toplevel surfaces are tracked and managed by the root
const node = server.views.allocateNode(server.allocator) catch unreachable; server.root.addView(wlr_xdg_surface);
node.data.init(server, wlr_xdg_surface);
server.views.append(node);
}
/// 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
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;
while (it) |node| : (it = node.prev) {
if (node.data.isAt(lx, ly, surface, sx, sy)) {
return &node.data;
}
}
return null;
} }
}; };

View file

@ -1,12 +1,12 @@
const std = @import("std"); const std = @import("std");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const Server = @import("server.zig").Server; const Root = @import("root.zig").Root;
pub const View = struct { pub const View = struct {
const Self = @This(); const Self = @This();
server: *Server, root: *Root,
wlr_xdg_surface: *c.wlr_xdg_surface, wlr_xdg_surface: *c.wlr_xdg_surface,
mapped: bool, mapped: bool,
@ -19,8 +19,8 @@ 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: *Self, server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void { pub fn init(self: *Self, root: *Root, wlr_xdg_surface: *c.wlr_xdg_surface) void {
self.server = server; self.root = root;
self.wlr_xdg_surface = wlr_xdg_surface; self.wlr_xdg_surface = wlr_xdg_surface;
self.mapped = false; self.mapped = false;
@ -55,17 +55,17 @@ pub const View = struct {
fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const view = @fieldParentPtr(View, "listen_destroy", listener.?); const view = @fieldParentPtr(View, "listen_destroy", listener.?);
const server = view.server; const root = view.root;
var it = server.views.first; var it = root.views.first;
const target = while (it) |node| : (it = node.next) { const target = while (it) |node| : (it = node.next) {
if (&node.data == view) { if (&node.data == view) {
break node; break node;
} }
} else unreachable; } else unreachable;
server.views.remove(target); root.views.remove(target);
server.views.destroyNode(target, server.allocator); root.views.destroyNode(target, root.server.allocator);
} }
// fn xdgToplevelRequestMove(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { // fn xdgToplevelRequestMove(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
@ -77,8 +77,8 @@ pub const View = struct {
// } // }
fn focus(self: *Self, surface: *c.wlr_surface) void { fn focus(self: *Self, surface: *c.wlr_surface) void {
const server = self.server; const root = self.root;
const wlr_seat = server.seat.wlr_seat; const wlr_seat = root.server.seat.wlr_seat;
const prev_surface = wlr_seat.keyboard_state.focused_surface; const prev_surface = wlr_seat.keyboard_state.focused_surface;
if (prev_surface == surface) { if (prev_surface == surface) {
@ -95,7 +95,7 @@ pub const View = struct {
} }
// Find the node // Find the node
var it = server.views.first; var it = root.views.first;
const target = while (it) |node| : (it = node.next) { const target = while (it) |node| : (it = node.next) {
if (&node.data == self) { if (&node.data == self) {
break node; break node;
@ -103,8 +103,8 @@ pub const View = struct {
} else unreachable; } else unreachable;
// Move the view to the front // Move the view to the front
server.views.remove(target); root.views.remove(target);
server.views.prepend(target); root.views.prepend(target);
// Activate the new surface // Activate the new surface
_ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, true); _ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, true);