aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2020-03-25 15:59:24 +0100
committerIsaac Freund <ifreund@ifreund.xyz>2020-03-25 15:59:24 +0100
commit34e47360f5f8412d950eab788acc60073f86b0b3 (patch)
tree455b53b800578bd5856b74e7c3e63bb9c8b6f625 /src
parenta3eb33a7b1394e7eae45950e0e59c471ee1475fb (diff)
downloadriver-34e47360f5f8412d950eab788acc60073f86b0b3.tar.gz
river-34e47360f5f8412d950eab788acc60073f86b0b3.tar.xz
Add Root struct
Server handles listening for wayland signals. It delegates input handling to Seat and output handling to Root.
Diffstat (limited to 'src')
-rw-r--r--src/cursor.zig6
-rw-r--r--src/main.zig5
-rw-r--r--src/output.zig16
-rw-r--r--src/root.zig62
-rw-r--r--src/server.zig86
-rw-r--r--src/view.zig26
6 files changed, 114 insertions, 87 deletions
diff --git a/src/cursor.zig b/src/cursor.zig
index 390328f..5b408b2 100644
--- a/src/cursor.zig
+++ b/src/cursor.zig
@@ -89,7 +89,7 @@ pub const Cursor = struct {
.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);
return cursor;
@@ -184,7 +184,7 @@ pub const Cursor = struct {
var sx: f64 = undefined;
var sy: f64 = undefined;
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.y,
&opt_surface,
@@ -278,7 +278,7 @@ pub const Cursor = struct {
var sy: f64 = undefined;
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.y,
&surface,
diff --git a/src/main.zig b/src/main.zig
index 86be73c..bd9589f 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -8,11 +8,10 @@ pub fn main() !void {
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();
- try server.init();
-
try server.start();
// Spawn an instance of alacritty
diff --git a/src/output.zig b/src/output.zig
index 06806d1..9c79f91 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const c = @import("c.zig").c;
+const Root = @import("root.zig").Root;
const Server = @import("server.zig").Server;
const View = @import("view.zig").View;
@@ -14,11 +15,11 @@ const RenderData = struct {
pub const Output = struct {
const Self = @This();
- server: *Server,
+ root: *Root,
wlr_output: *c.wlr_output,
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
// 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
@@ -27,6 +28,7 @@ pub const Output = struct {
// if not empty
if (c.wl_list_empty(&wlr_output.modes) == 0) {
+ // TODO: handle failure
const mode = c.wlr_output_preferred_mode(wlr_output);
c.wlr_output_set_mode(wlr_output, mode);
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;
// 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
// compositor would let the user configure the arrangement of outputs in the
// 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
// 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,
// generally at the output's refresh rate (e.g. 60Hz).
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;
_ = 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.
// 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) {
const view = &node.data;
if (!view.mapped) {
@@ -136,7 +138,7 @@ pub const Output = struct {
// output-local coordinates, or (2000 - 1920).
var ox: 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);
oy += @intToFloat(f64, view.y + sy);
diff --git a/src/root.zig b/src/root.zig
new file mode 100644
index 0000000..fca4591
--- /dev/null
+++ b/src/root.zig
@@ -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;
+ }
+};
diff --git a/src/server.zig b/src/server.zig
index 81dfe46..06185db 100644
--- a/src/server.zig
+++ b/src/server.zig
@@ -2,6 +2,7 @@ const std = @import("std");
const c = @import("c.zig").c;
const Output = @import("output.zig").Output;
+const Root = @import("root.zig").Root;
const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View;
@@ -10,91 +11,71 @@ pub const Server = struct {
allocator: *std.mem.Allocator,
+ root: Root,
+ seat: Seat,
+
wl_display: *c.wl_display,
wlr_backend: *c.wlr_backend,
wlr_renderer: *c.wlr_renderer,
- wlr_output_layout: *c.wlr_output_layout,
- outputs: std.TailQueue(Output),
-
listen_new_output: c.wl_listener,
wlr_xdg_shell: *c.wlr_xdg_shell,
listen_new_xdg_surface: c.wl_listener,
- // Must stay ordered, first in list is "on top" visually
- views: std.TailQueue(View),
-
- seat: Seat,
-
- pub fn create(allocator: *std.mem.Allocator) !Self {
- var server: Self = undefined;
- server.allocator = allocator;
+ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
+ self.allocator = allocator;
// The Wayland display is managed by libwayland. It handles accepting
// 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;
- 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 best option based on the environment, for example DRM when run from
// a tty or wayland if WAYLAND_DISPLAY is set.
//
- // This frees itserver.when the wl_display is destroyed.
- server.wlr_backend = c.zag_wlr_backend_autocreate(server.wl_display) orelse
+ // This frees itself.when the wl_display is destroyed.
+ self.wlr_backend = c.zag_wlr_backend_autocreate(self.wl_display) orelse
return error.CantCreateWlrBackend;
// 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.
- 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;
// 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;
// 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;
- _ = c.wlr_data_device_manager_create(server.wl_display) orelse
+ _ = c.wlr_data_device_manager_create(self.wl_display) orelse
return error.CantCreateWlrDataDeviceManager;
- // Create an output layout, which a wlroots utility for working with an
- // 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
+ self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse
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);
try self.seat.init();
// Register our listeners for new outputs and xdg_surfaces.
- // This can't be done in create() as wl_signal_add() creates a pointer
- // to the wl_list link in our wl_listener, a pointer that would be
- // broken when returning from create();
+ self.listen_new_output.notify = handle_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);
}
/// 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(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
@@ -148,11 +129,7 @@ pub const Server = struct {
fn handle_new_output(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));
-
- // TODO: Handle failure
- const node = server.outputs.allocateNode(server.allocator) catch unreachable;
- node.data.init(server, wlr_output) catch unreachable;
- server.outputs.append(node);
+ server.root.addOutput(wlr_output);
}
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));
if (wlr_xdg_surface.role != c.enum_wlr_xdg_surface_role.WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
+ // TODO: log
return;
}
- // Create a View to handle this toplevel surface
- const node = server.views.allocateNode(server.allocator) catch unreachable;
- 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;
+ // toplevel surfaces are tracked and managed by the root
+ server.root.addView(wlr_xdg_surface);
}
};
diff --git a/src/view.zig b/src/view.zig
index 950d198..b04efe4 100644
--- a/src/view.zig
+++ b/src/view.zig
@@ -1,12 +1,12 @@
const std = @import("std");
const c = @import("c.zig").c;
-const Server = @import("server.zig").Server;
+const Root = @import("root.zig").Root;
pub const View = struct {
const Self = @This();
- server: *Server,
+ root: *Root,
wlr_xdg_surface: *c.wlr_xdg_surface,
mapped: bool,
@@ -19,8 +19,8 @@ pub const View = struct {
// listen_request_move: c.wl_listener,
// listen_request_resize: c.wl_listener,
- pub fn init(self: *Self, server: *Server, wlr_xdg_surface: *c.wlr_xdg_surface) void {
- self.server = server;
+ pub fn init(self: *Self, root: *Root, wlr_xdg_surface: *c.wlr_xdg_surface) void {
+ self.root = root;
self.wlr_xdg_surface = wlr_xdg_surface;
self.mapped = false;
@@ -55,17 +55,17 @@ pub const View = struct {
fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
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) {
if (&node.data == view) {
break node;
}
} else unreachable;
- server.views.remove(target);
- server.views.destroyNode(target, server.allocator);
+ root.views.remove(target);
+ root.views.destroyNode(target, root.server.allocator);
}
// 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 {
- const server = self.server;
- const wlr_seat = server.seat.wlr_seat;
+ const root = self.root;
+ const wlr_seat = root.server.seat.wlr_seat;
const prev_surface = wlr_seat.keyboard_state.focused_surface;
if (prev_surface == surface) {
@@ -95,7 +95,7 @@ pub const View = struct {
}
// Find the node
- var it = server.views.first;
+ var it = root.views.first;
const target = while (it) |node| : (it = node.next) {
if (&node.data == self) {
break node;
@@ -103,8 +103,8 @@ pub const View = struct {
} else unreachable;
// Move the view to the front
- server.views.remove(target);
- server.views.prepend(target);
+ root.views.remove(target);
+ root.views.prepend(target);
// Activate the new surface
_ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, true);