Make Server a toplevel struct

This commit is contained in:
Isaac Freund 2020-05-02 16:20:32 +02:00
parent ce05dddea3
commit 5dbff2c018
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
6 changed files with 167 additions and 168 deletions

View File

@ -2,7 +2,7 @@ const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Decoration = @import("decoration.zig").Decoration; const Decoration = @import("decoration.zig").Decoration;
const Server = @import("server.zig").Server; const Server = @import("server.zig");
pub const DecorationManager = struct { pub const DecorationManager = struct {
const Self = @This(); const Self = @This();

View File

@ -3,7 +3,7 @@ const c = @import("c.zig");
const Log = @import("log.zig").Log; const Log = @import("log.zig").Log;
const Seat = @import("seat.zig").Seat; const Seat = @import("seat.zig").Seat;
const Server = @import("server.zig").Server; const Server = @import("server.zig");
pub const InputManager = struct { pub const InputManager = struct {
const Self = @This(); const Self = @This();

View File

@ -2,7 +2,7 @@ const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Log = @import("log.zig").Log; const Log = @import("log.zig").Log;
const Server = @import("server.zig").Server; const Server = @import("server.zig");
pub fn main() !void { pub fn main() !void {
Log.init(Log.Debug); Log.init(Log.Debug);

View File

@ -4,7 +4,7 @@ const c = @import("c.zig");
const Box = @import("box.zig"); const Box = @import("box.zig");
const LayerSurface = @import("layer_surface.zig").LayerSurface; const LayerSurface = @import("layer_surface.zig").LayerSurface;
const Output = @import("output.zig").Output; const Output = @import("output.zig").Output;
const Server = @import("server.zig").Server; const Server = @import("server.zig");
const View = @import("view.zig").View; const View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack; const ViewStack = @import("view_stack.zig").ViewStack;

View File

@ -5,7 +5,7 @@ const util = @import("util.zig");
const Box = @import("box.zig").Box; const Box = @import("box.zig").Box;
const Log = @import("log.zig").Log; const Log = @import("log.zig").Log;
const Output = @import("output.zig").Output; const Output = @import("output.zig").Output;
const Server = @import("server.zig").Server; const Server = @import("server.zig");
const Seat = @import("seat.zig").Seat; const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View; const View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack; const ViewStack = @import("view_stack.zig").ViewStack;

View File

@ -1,4 +1,7 @@
const Self = @This();
const std = @import("std"); const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Config = @import("config.zig").Config; const Config = @import("config.zig").Config;
@ -10,196 +13,192 @@ const Root = @import("root.zig").Root;
const View = @import("view.zig").View; const View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack; const ViewStack = @import("view_stack.zig").ViewStack;
pub const Server = struct { allocator: *std.mem.Allocator,
const Self = @This();
allocator: *std.mem.Allocator, wl_display: *c.wl_display,
wl_event_loop: *c.wl_event_loop,
wlr_backend: *c.wlr_backend,
noop_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer,
wl_display: *c.wl_display, wlr_xdg_shell: *c.wlr_xdg_shell,
wl_event_loop: *c.wl_event_loop, wlr_layer_shell: *c.wlr_layer_shell_v1,
wlr_backend: *c.wlr_backend,
noop_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer,
wlr_xdg_shell: *c.wlr_xdg_shell, decoration_manager: DecorationManager,
wlr_layer_shell: *c.wlr_layer_shell_v1, input_manager: InputManager,
root: Root,
config: Config,
decoration_manager: DecorationManager, listen_new_output: c.wl_listener,
input_manager: InputManager, listen_new_xdg_surface: c.wl_listener,
root: Root, listen_new_layer_surface: c.wl_listener,
config: Config,
listen_new_output: c.wl_listener, pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
listen_new_xdg_surface: c.wl_listener, self.allocator = allocator;
listen_new_layer_surface: c.wl_listener,
pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { // The Wayland display is managed by libwayland. It handles accepting
self.allocator = allocator; // clients from the Unix socket, managing Wayland globals, and so on.
self.wl_display = c.wl_display_create() orelse
return error.CantCreateWlDisplay;
errdefer c.wl_display_destroy(self.wl_display);
// The Wayland display is managed by libwayland. It handles accepting // Should never return null if the display was created successfully
// clients from the Unix socket, managing Wayland globals, and so on. self.wl_event_loop = c.wl_display_get_event_loop(self.wl_display) orelse
self.wl_display = c.wl_display_create() orelse return error.CantGetEventLoop;
return error.CantCreateWlDisplay;
errdefer c.wl_display_destroy(self.wl_display);
// Should never return null if the display was created successfully // The wlr_backend abstracts the input/output hardware. Autocreate chooses
self.wl_event_loop = c.wl_display_get_event_loop(self.wl_display) orelse // the best option based on the environment, for example DRM when run from
return error.CantGetEventLoop; // a tty or wayland if WAYLAND_DISPLAY is set. This frees itself when the
// wl_display is destroyed.
self.wlr_backend = c.river_wlr_backend_autocreate(self.wl_display) orelse
return error.CantCreateWlrBackend;
// The wlr_backend abstracts the input/output hardware. Autocreate chooses // This backend is used to create a noop output for use when no actual
// the best option based on the environment, for example DRM when run from // outputs are available. This frees itself when the wl_display is destroyed.
// a tty or wayland if WAYLAND_DISPLAY is set. This frees itself when the self.noop_backend = c.river_wlr_noop_backend_create(self.wl_display) orelse
// wl_display is destroyed. return error.CantCreateNoopBackend;
self.wlr_backend = c.river_wlr_backend_autocreate(self.wl_display) orelse
return error.CantCreateWlrBackend;
// This backend is used to create a noop output for use when no actual // If we don't provide a renderer, autocreate makes a GLES2 renderer for us.
// outputs are available. This frees itself when the wl_display is destroyed. // The renderer is responsible for defining the various pixel formats it
self.noop_backend = c.river_wlr_noop_backend_create(self.wl_display) orelse // supports for shared memory, this configures that for clients.
return error.CantCreateNoopBackend; self.wlr_renderer = c.river_wlr_backend_get_renderer(self.wlr_backend) orelse
return error.CantGetWlrRenderer;
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(self.wlr_renderer, self.wl_display); // orelse
// return error.CantInitWlDisplay;
// If we don't provide a renderer, autocreate makes a GLES2 renderer for us. self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse
// The renderer is responsible for defining the various pixel formats it return error.CantCreateWlrXdgShell;
// supports for shared memory, this configures that for clients.
self.wlr_renderer = c.river_wlr_backend_get_renderer(self.wlr_backend) orelse
return error.CantGetWlrRenderer;
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(self.wlr_renderer, self.wl_display); // orelse
// return error.CantInitWlDisplay;
self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse self.wlr_layer_shell = c.wlr_layer_shell_v1_create(self.wl_display) orelse
return error.CantCreateWlrXdgShell; return error.CantCreateWlrLayerShell;
self.wlr_layer_shell = c.wlr_layer_shell_v1_create(self.wl_display) orelse try self.decoration_manager.init(self);
return error.CantCreateWlrLayerShell; try self.root.init(self);
// Must be called after root is initialized
try self.input_manager.init(self);
try self.config.init(self.allocator);
try self.decoration_manager.init(self); // These all free themselves when the wl_display is destroyed
try self.root.init(self); _ = c.wlr_compositor_create(self.wl_display, self.wlr_renderer) orelse
// Must be called after root is initialized return error.CantCreateWlrCompositor;
try self.input_manager.init(self); _ = c.wlr_data_device_manager_create(self.wl_display) orelse
try self.config.init(self.allocator); return error.CantCreateWlrDataDeviceManager;
_ = c.wlr_screencopy_manager_v1_create(self.wl_display) orelse
return error.CantCreateWlrScreencopyManager;
_ = c.wlr_xdg_output_manager_v1_create(self.wl_display, self.root.wlr_output_layout) orelse
return error.CantCreateWlrOutputManager;
// These all free themselves when the wl_display is destroyed // Register listeners for events on our globals
_ = c.wlr_compositor_create(self.wl_display, self.wlr_renderer) orelse self.listen_new_output.notify = handleNewOutput;
return error.CantCreateWlrCompositor; c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
_ = c.wlr_data_device_manager_create(self.wl_display) orelse
return error.CantCreateWlrDataDeviceManager;
_ = c.wlr_screencopy_manager_v1_create(self.wl_display) orelse
return error.CantCreateWlrScreencopyManager;
_ = c.wlr_xdg_output_manager_v1_create(self.wl_display, self.root.wlr_output_layout) orelse
return error.CantCreateWlrOutputManager;
// Register listeners for events on our globals self.listen_new_xdg_surface.notify = handleNewXdgSurface;
self.listen_new_output.notify = handleNewOutput; c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface);
c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
self.listen_new_xdg_surface.notify = handleNewXdgSurface; self.listen_new_layer_surface.notify = handleNewLayerSurface;
c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface); c.wl_signal_add(&self.wlr_layer_shell.events.new_surface, &self.listen_new_layer_surface);
}
self.listen_new_layer_surface.notify = handleNewLayerSurface; /// Free allocated memory and clean up
c.wl_signal_add(&self.wlr_layer_shell.events.new_surface, &self.listen_new_layer_surface); pub fn deinit(self: *Self) void {
// Note: order is important here
c.wl_display_destroy_clients(self.wl_display);
c.wl_display_destroy(self.wl_display);
self.input_manager.deinit();
self.root.deinit();
}
/// Create the socket, set WAYLAND_DISPLAY, and start the backend
pub fn start(self: Self) !void {
// Add a Unix socket to the Wayland display.
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse
return error.CantAddSocket;
// Start the backend. This will enumerate outputs and inputs, become the DRM
// master, etc
if (!c.river_wlr_backend_start(self.wlr_backend)) {
return error.CantStartBackend;
} }
/// Free allocated memory and clean up // Set the WAYLAND_DISPLAY environment variable to our socket
pub fn deinit(self: *Self) void { if (c.setenv("WAYLAND_DISPLAY", socket, 1) == -1) {
// Note: order is important here return error.CantSetEnv;
c.wl_display_destroy_clients(self.wl_display); }
c.wl_display_destroy(self.wl_display); }
self.input_manager.deinit();
self.root.deinit(); /// Enter the wayland event loop and block until the compositor is exited
pub fn run(self: Self) void {
c.wl_display_run(self.wl_display);
}
fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_new_output", listener.?);
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
Log.Debug.log("New output {}", .{wlr_output.name});
self.root.addOutput(wlr_output);
}
fn handleNewXdgSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
// This event is raised when wlr_xdg_shell receives a new xdg surface from a
// client, either a toplevel (application window) or popup.
const self = @fieldParentPtr(Self, "listen_new_xdg_surface", listener.?);
const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data));
if (wlr_xdg_surface.role == .WLR_XDG_SURFACE_ROLE_POPUP) {
Log.Debug.log("New xdg_popup", .{});
return;
} }
/// Create the socket, set WAYLAND_DISPLAY, and start the backend Log.Debug.log("New xdg_toplevel", .{});
pub fn start(self: Self) !void {
// Add a Unix socket to the Wayland display.
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse
return error.CantAddSocket;
// Start the backend. This will enumerate outputs and inputs, become the DRM self.input_manager.default_seat.focused_output.addView(wlr_xdg_surface);
// master, etc }
if (!c.river_wlr_backend_start(self.wlr_backend)) {
return error.CantStartBackend;
}
// Set the WAYLAND_DISPLAY environment variable to our socket /// This event is raised when the layer_shell recieves a new surface from a client.
if (c.setenv("WAYLAND_DISPLAY", socket, 1) == -1) { fn handleNewLayerSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
return error.CantSetEnv; const self = @fieldParentPtr(Self, "listen_new_layer_surface", listener.?);
} const wlr_layer_surface = @ptrCast(
} *c.wlr_layer_surface_v1,
@alignCast(@alignOf(*c.wlr_layer_surface_v1), data),
);
/// Enter the wayland event loop and block until the compositor is exited Log.Debug.log(
pub fn run(self: Self) void { "New layer surface: namespace {}, layer {}, anchor {}, size {}x{}, margin ({},{},{},{}), exclusive_zone {}",
c.wl_display_run(self.wl_display); .{
} wlr_layer_surface.namespace,
wlr_layer_surface.client_pending.layer,
wlr_layer_surface.client_pending.anchor,
wlr_layer_surface.client_pending.desired_width,
wlr_layer_surface.client_pending.desired_height,
wlr_layer_surface.client_pending.margin.top,
wlr_layer_surface.client_pending.margin.right,
wlr_layer_surface.client_pending.margin.bottom,
wlr_layer_surface.client_pending.margin.left,
wlr_layer_surface.client_pending.exclusive_zone,
},
);
fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { // If the new layer surface does not have an output assigned to it, use the
const self = @fieldParentPtr(Self, "listen_new_output", listener.?); // first output or close the surface if none are available.
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data)); if (wlr_layer_surface.output == null) {
Log.Debug.log("New output {}", .{wlr_output.name}); if (self.root.outputs.first) |node| {
self.root.addOutput(wlr_output); const output = &node.data;
} Log.Debug.log(
"New layer surface had null output, assigning it to output {}",
fn handleNewXdgSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { .{output.wlr_output.name},
// This event is raised when wlr_xdg_shell receives a new xdg surface from a );
// client, either a toplevel (application window) or popup. wlr_layer_surface.output = output.wlr_output;
const self = @fieldParentPtr(Self, "listen_new_xdg_surface", listener.?); } else {
const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data)); Log.Error.log(
"No output available for layer surface '{}'",
if (wlr_xdg_surface.role == .WLR_XDG_SURFACE_ROLE_POPUP) { .{wlr_layer_surface.namespace},
Log.Debug.log("New xdg_popup", .{}); );
c.wlr_layer_surface_v1_close(wlr_layer_surface);
return; return;
} }
Log.Debug.log("New xdg_toplevel", .{});
self.input_manager.default_seat.focused_output.addView(wlr_xdg_surface);
} }
/// This event is raised when the layer_shell recieves a new surface from a client. const output = @ptrCast(*Output, @alignCast(@alignOf(*Output), wlr_layer_surface.output.*.data));
fn handleNewLayerSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { output.addLayerSurface(wlr_layer_surface) catch unreachable;
const self = @fieldParentPtr(Self, "listen_new_layer_surface", listener.?); }
const wlr_layer_surface = @ptrCast(
*c.wlr_layer_surface_v1,
@alignCast(@alignOf(*c.wlr_layer_surface_v1), data),
);
Log.Debug.log(
"New layer surface: namespace {}, layer {}, anchor {}, size {}x{}, margin ({},{},{},{}), exclusive_zone {}",
.{
wlr_layer_surface.namespace,
wlr_layer_surface.client_pending.layer,
wlr_layer_surface.client_pending.anchor,
wlr_layer_surface.client_pending.desired_width,
wlr_layer_surface.client_pending.desired_height,
wlr_layer_surface.client_pending.margin.top,
wlr_layer_surface.client_pending.margin.right,
wlr_layer_surface.client_pending.margin.bottom,
wlr_layer_surface.client_pending.margin.left,
wlr_layer_surface.client_pending.exclusive_zone,
},
);
// If the new layer surface does not have an output assigned to it, use the
// first output or close the surface if none are available.
if (wlr_layer_surface.output == null) {
if (self.root.outputs.first) |node| {
const output = &node.data;
Log.Debug.log(
"New layer surface had null output, assigning it to output {}",
.{output.wlr_output.name},
);
wlr_layer_surface.output = output.wlr_output;
} else {
Log.Error.log(
"No output available for layer surface '{}'",
.{wlr_layer_surface.namespace},
);
c.wlr_layer_surface_v1_close(wlr_layer_surface);
return;
}
}
const output = @ptrCast(*Output, @alignCast(@alignOf(*Output), wlr_layer_surface.output.*.data));
output.addLayerSurface(wlr_layer_surface) catch unreachable;
}
};