river/src/server.zig

191 lines
7.7 KiB
Zig
Raw Normal View History

2020-03-22 14:42:55 -07:00
const std = @import("std");
2020-03-29 10:36:15 -07:00
const c = @import("c.zig");
2020-04-07 10:16:38 -07:00
const command = @import("command.zig");
2020-03-22 14:42:55 -07:00
const Config = @import("config.zig").Config;
const DecorationManager = @import("decoration_manager.zig").DecorationManager;
const Log = @import("log.zig").Log;
2020-03-23 08:50:20 -07:00
const Output = @import("output.zig").Output;
const Root = @import("root.zig").Root;
2020-03-23 08:50:20 -07:00
const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack;
2020-03-23 08:50:20 -07:00
2020-03-22 14:42:55 -07:00
pub const Server = struct {
const Self = @This();
allocator: *std.mem.Allocator,
2020-03-22 14:42:55 -07:00
wl_display: *c.wl_display,
wl_event_loop: *c.wl_event_loop,
2020-03-22 17:21:15 -07:00
wlr_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer,
2020-03-23 06:04:54 -07:00
wlr_xdg_shell: *c.wlr_xdg_shell,
wlr_layer_shell: *c.wlr_layer_shell_v1,
decoration_manager: DecorationManager,
root: Root,
seat: Seat,
config: Config,
listen_new_output: c.wl_listener,
2020-03-23 06:04:54 -07:00
listen_new_xdg_surface: c.wl_listener,
listen_new_layer_surface: c.wl_listener,
2020-03-23 06:04:54 -07:00
pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
self.allocator = allocator;
2020-03-22 14:42:55 -07:00
// The Wayland display is managed by libwayland. It handles accepting
// clients from the Unix socket, managing Wayland globals, and so on.
self.wl_display = c.wl_display_create() orelse
2020-03-22 17:21:15 -07:00
return error.CantCreateWlDisplay;
errdefer c.wl_display_destroy(self.wl_display);
2020-03-22 17:21:15 -07:00
// Should never return null if the display was created successfully
self.wl_event_loop = c.wl_display_get_event_loop(self.wl_display) orelse
return error.CantGetEventLoop;
2020-03-22 17:21:15 -07:00
// The wlr_backend abstracts the input/output hardware. Autocreate chooses
// the best option based on the environment, for example DRM when run from
// a tty or wayland if WAYLAND_DISPLAY is set.
//
// This frees itself.when the wl_display is destroyed.
2020-03-28 03:42:58 -07:00
self.wlr_backend = c.river_wlr_backend_autocreate(self.wl_display) orelse
2020-03-22 17:21:15 -07:00
return error.CantCreateWlrBackend;
2020-03-22 14:42:55 -07:00
// 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
// supports for shared memory, this configures that for clients.
2020-03-28 03:42:58 -07:00
self.wlr_renderer = c.river_wlr_backend_get_renderer(self.wlr_backend) orelse
2020-03-22 17:21:15 -07:00
return error.CantGetWlrRenderer;
2020-03-23 08:50:20 -07:00
// TODO: Handle failure after https://github.com/swaywm/wlroots/pull/2080
c.wlr_renderer_init_wl_display(self.wlr_renderer, self.wl_display); // orelse
2020-03-23 08:50:20 -07:00
// return error.CantInitWlDisplay;
2020-03-22 17:21:15 -07:00
self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse
2020-03-23 06:04:54 -07:00
return error.CantCreateWlrXdgShell;
self.wlr_layer_shell = c.wlr_layer_shell_v1_create(self.wl_display) orelse
return error.CantCreateWlrLayerShell;
try self.decoration_manager.init(self);
try self.root.init(self);
2020-03-22 17:21:15 -07:00
2020-03-25 08:24:21 -07:00
try self.seat.init(self);
try self.config.init(self.allocator);
2020-04-10 08:23:25 -07:00
// These all free themselves when the wl_display is destroyed
_ = c.wlr_compositor_create(self.wl_display, self.wlr_renderer) orelse
return error.CantCreateWlrCompositor;
_ = c.wlr_data_device_manager_create(self.wl_display) orelse
return error.CantCreateWlrDataDeviceManager;
_ = 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_output.notify = handleNewOutput;
c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
self.listen_new_xdg_surface.notify = handleNewXdgSurface;
c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface);
self.listen_new_layer_surface.notify = handleNewLayerSurface;
c.wl_signal_add(&self.wlr_layer_shell.events.new_surface, &self.listen_new_layer_surface);
}
2020-03-22 17:21:15 -07:00
/// Free allocated memory and clean up
2020-03-29 06:58:04 -07:00
pub fn destroy(self: Self) void {
2020-03-22 17:21:15 -07:00
c.wl_display_destroy_clients(self.wl_display);
c.wl_display_destroy(self.wl_display);
self.root.destroy();
2020-03-22 17:21:15 -07:00
}
/// Create the socket, set WAYLAND_DISPLAY, and start the backend
pub fn start(self: Self) !void {
2020-03-22 17:21:15 -07:00
// Add a Unix socket to the Wayland display.
2020-03-23 04:22:48 -07:00
const socket = c.wl_display_add_socket_auto(self.wl_display) orelse
2020-03-22 17:21:15 -07:00
return error.CantAddSocket;
// Start the backend. This will enumerate outputs and inputs, become the DRM
// master, etc
2020-03-28 03:42:58 -07:00
if (!c.river_wlr_backend_start(self.wlr_backend)) {
2020-03-22 17:21:15 -07:00
return error.CantStartBackend;
}
// Set the WAYLAND_DISPLAY environment variable to our socket
2020-03-22 17:21:15 -07:00
if (c.setenv("WAYLAND_DISPLAY", socket, 1) == -1) {
return error.CantSetEnv;
}
}
/// Enter the wayland event loop and block until the compositor is exited
pub fn run(self: Self) void {
2020-03-23 08:50:20 -07:00
c.wl_display_run(self.wl_display);
2020-03-22 14:42:55 -07:00
}
fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const server = @fieldParentPtr(Server, "listen_new_output", listener.?);
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
server.root.addOutput(wlr_output);
2020-03-23 06:04:54 -07:00
}
fn handleNewXdgSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
2020-03-23 06:04:54 -07:00
// This event is raised when wlr_xdg_shell receives a new xdg surface from a
// client, either a toplevel (application window) or popup.
const server = @fieldParentPtr(Server, "listen_new_xdg_surface", listener.?);
const wlr_xdg_surface = @ptrCast(*c.wlr_xdg_surface, @alignCast(@alignOf(*c.wlr_xdg_surface), data));
2020-03-23 06:04:54 -07:00
if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
// TODO: log
2020-03-23 06:04:54 -07:00
return;
}
// toplevel surfaces are tracked and managed by the root
server.root.addView(wlr_xdg_surface);
2020-03-23 04:22:48 -07:00
}
/// This event is raised when the layer_shell recieves a new surface from a client.
fn handleNewLayerSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const server = @fieldParentPtr(Server, "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 ({},{},{},{})",
.{
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,
},
);
// TODO: this is insufficent for multi output support
if (server.root.outputs.first) |node| {
const output = &node.data;
if (wlr_layer_surface.output == null) {
wlr_layer_surface.output = output.wlr_output;
}
output.addLayerSurface(wlr_layer_surface) catch unreachable;
} else {
Log.Error.log(
"No output available for layer surface '{}' autoassign",
.{wlr_layer_surface.namespace},
);
c.wlr_layer_surface_v1_close(wlr_layer_surface);
}
}
2020-03-22 14:42:55 -07:00
};