2020-03-22 14:42:55 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const c = @import("c.zig").c;
|
|
|
|
|
2020-03-23 08:50:20 -07:00
|
|
|
const Output = @import("output.zig").Output;
|
2020-03-25 07:59:24 -07:00
|
|
|
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;
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
pub const Server = struct {
|
2020-03-24 12:48:38 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-23 13:51:46 -07:00
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
|
2020-03-25 07:59:24 -07:00
|
|
|
root: Root,
|
|
|
|
seat: Seat,
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
wl_display: *c.wl_display,
|
2020-03-22 17:21:15 -07:00
|
|
|
wlr_backend: *c.wlr_backend,
|
|
|
|
wlr_renderer: *c.wlr_renderer,
|
|
|
|
|
|
|
|
listen_new_output: c.wl_listener,
|
|
|
|
|
2020-03-23 06:04:54 -07:00
|
|
|
wlr_xdg_shell: *c.wlr_xdg_shell,
|
|
|
|
listen_new_xdg_surface: c.wl_listener,
|
|
|
|
|
2020-03-25 07:59:24 -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, manging Wayland globals, and so on.
|
2020-03-25 07:59:24 -07:00
|
|
|
self.wl_display = c.wl_display_create() orelse
|
2020-03-22 17:21:15 -07:00
|
|
|
return error.CantCreateWlDisplay;
|
2020-03-25 07:59:24 -07:00
|
|
|
errdefer c.wl_display_destroy(self.wl_display);
|
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.
|
|
|
|
//
|
2020-03-25 07:59:24 -07:00
|
|
|
// This frees itself.when the wl_display is destroyed.
|
|
|
|
self.wlr_backend = c.zag_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-25 07:59:24 -07:00
|
|
|
self.wlr_renderer = c.zag_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
|
2020-03-25 07:59:24 -07:00
|
|
|
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
|
|
|
|
|
|
|
// These both free themselves when the wl_display is destroyed
|
2020-03-25 07:59:24 -07:00
|
|
|
_ = c.wlr_compositor_create(self.wl_display, self.wlr_renderer) orelse
|
2020-03-22 17:21:15 -07:00
|
|
|
return error.CantCreateWlrCompositor;
|
2020-03-25 07:59:24 -07:00
|
|
|
_ = c.wlr_data_device_manager_create(self.wl_display) orelse
|
2020-03-22 17:21:15 -07:00
|
|
|
return error.CantCreateWlrDataDeviceManager;
|
|
|
|
|
2020-03-25 07:59:24 -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;
|
|
|
|
|
2020-03-25 07:59:24 -07:00
|
|
|
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);
|
2020-03-23 13:51:46 -07:00
|
|
|
|
|
|
|
// Register our listeners for new outputs and xdg_surfaces.
|
2020-03-25 08:29:30 -07:00
|
|
|
self.listen_new_output.notify = handleNewOutput;
|
2020-03-24 12:03:48 -07:00
|
|
|
c.wl_signal_add(&self.wlr_backend.events.new_output, &self.listen_new_output);
|
2020-03-25 07:59:24 -07:00
|
|
|
|
2020-03-25 08:29:30 -07:00
|
|
|
self.listen_new_xdg_surface.notify = handleNewXdgSurface;
|
2020-03-24 12:03:48 -07:00
|
|
|
c.wl_signal_add(&self.wlr_xdg_shell.events.new_surface, &self.listen_new_xdg_surface);
|
2020-03-23 13:51:46 -07:00
|
|
|
}
|
|
|
|
|
2020-03-22 17:21:15 -07:00
|
|
|
/// Free allocated memory and clean up
|
2020-03-25 07:59:24 -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);
|
2020-03-25 07:59:24 -07:00
|
|
|
self.root.destroy();
|
2020-03-22 17:21:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the socket, set WAYLAND_DISPLAY, and start the backend
|
2020-03-24 12:48:38 -07:00
|
|
|
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-23 08:50:20 -07:00
|
|
|
if (!c.zag_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 and run the
|
|
|
|
// startup command if requested. */
|
|
|
|
if (c.setenv("WAYLAND_DISPLAY", socket, 1) == -1) {
|
|
|
|
return error.CantSetEnv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enter the wayland event loop and block until the compositor is exited
|
2020-03-24 12:48:38 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-25 08:29:30 -07:00
|
|
|
pub fn handleKeybinding(self: *Self, sym: c.xkb_keysym_t) bool {
|
2020-03-22 14:42:55 -07:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This function assumes the proper modifier is held down.
|
|
|
|
switch (sym) {
|
2020-03-23 08:50:20 -07:00
|
|
|
c.XKB_KEY_Escape => c.wl_display_terminate(self.wl_display),
|
2020-03-26 18:05:57 -07:00
|
|
|
c.XKB_KEY_j => self.root.focusNextView(),
|
|
|
|
c.XKB_KEY_k => self.root.focusPrevView(),
|
2020-03-26 18:10:31 -07:00
|
|
|
c.XKB_KEY_Return => {
|
|
|
|
// Spawn an instance of alacritty
|
|
|
|
// const argv = [_][]const u8{ "/bin/sh", "-c", "WAYLAND_DEBUG=1 alacritty" };
|
|
|
|
const argv = [_][]const u8{ "/bin/sh", "-c", "alacritty" };
|
|
|
|
const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch unreachable;
|
|
|
|
std.ChildProcess.spawn(child) catch unreachable;
|
|
|
|
},
|
2020-03-22 14:42:55 -07:00
|
|
|
else => return false,
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-23 04:22:48 -07:00
|
|
|
|
2020-03-25 08:29:30 -07:00
|
|
|
fn handleNewOutput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-24 12:35:45 -07:00
|
|
|
const server = @fieldParentPtr(Server, "listen_new_output", listener.?);
|
|
|
|
const wlr_output = @ptrCast(*c.wlr_output, @alignCast(@alignOf(*c.wlr_output), data));
|
2020-03-25 07:59:24 -07:00
|
|
|
server.root.addOutput(wlr_output);
|
2020-03-23 06:04:54 -07:00
|
|
|
}
|
|
|
|
|
2020-03-25 08:29:30 -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.
|
2020-03-24 12:35:45 -07:00
|
|
|
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) {
|
2020-03-25 07:59:24 -07:00
|
|
|
// TODO: log
|
2020-03-23 06:04:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-25 07:59:24 -07:00
|
|
|
// toplevel surfaces are tracked and managed by the root
|
|
|
|
server.root.addView(wlr_xdg_surface);
|
2020-03-23 04:22:48 -07:00
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
};
|