mirror of
https://github.com/MezzalunaWM/Mezzaluna.git
synced 2026-03-07 19:49:53 -05:00
starting of root module refactor
This commit is contained in:
parent
9cb148e3b1
commit
275f96615b
5 changed files with 121 additions and 120 deletions
|
|
@ -5,11 +5,14 @@ const Server = @import("server.zig").Server;
|
||||||
|
|
||||||
const gpa = std.heap.c_allocator;
|
const gpa = std.heap.c_allocator;
|
||||||
|
|
||||||
|
|
||||||
|
pub var server: Server = undefined;
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
wlr.log.init(.debug, null);
|
wlr.log.init(.debug, null);
|
||||||
std.debug.print("Starting mezzaluna", .{});
|
|
||||||
|
|
||||||
var server: Server = undefined;
|
std.log.info("Starting mezzaluna", .{});
|
||||||
|
|
||||||
try server.init();
|
try server.init();
|
||||||
|
|
||||||
var buf: [11]u8 = undefined;
|
var buf: [11]u8 = undefined;
|
||||||
|
|
@ -25,10 +28,12 @@ pub fn main() !void {
|
||||||
try child.spawn();
|
try child.spawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std.log.info("Starting backend", .{});
|
||||||
server.backend.start() catch |err| {
|
server.backend.start() catch |err| {
|
||||||
std.debug.panic("Failed to start backend: {}", .{err});
|
std.debug.panic("Failed to start backend: {}", .{err});
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std.log.info("Starting server", .{});
|
||||||
server.wl_server.run();
|
server.wl_server.run();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,60 +5,55 @@ const gpa = std.heap.c_allocator;
|
||||||
const wl = @import("wayland").server.wl;
|
const wl = @import("wayland").server.wl;
|
||||||
const wlr = @import("wlroots");
|
const wlr = @import("wlroots");
|
||||||
|
|
||||||
const Server = @import("server.zig").Server;
|
const server = &@import("main.zig").server;
|
||||||
|
|
||||||
pub const Output = struct {
|
pub const Output = struct {
|
||||||
server: *Server,
|
wlr_output: *wlr.Output,
|
||||||
wlr_output: *wlr.Output,
|
scene_output: *wlr.SceneOutput,
|
||||||
|
|
||||||
frame: wl.Listener(*wlr.Output) = .init(handleFrame),
|
frame: wl.Listener(*wlr.Output) = .init(handleFrame),
|
||||||
request_state: wl.Listener(*wlr.Output.event.RequestState) = .init(handleRequestState),
|
request_state: wl.Listener(*wlr.Output.event.RequestState) = .init(handleRequestState),
|
||||||
destroy: wl.Listener(*wlr.Output) = .init(handleDestroy),
|
destroy: wl.Listener(*wlr.Output) = .init(handleDestroy),
|
||||||
|
|
||||||
// The wlr.Output should be destroyed by the caller on failure to trigger cleanup.
|
// The wlr.Output should be destroyed by the caller on failure to trigger cleanup.
|
||||||
pub fn create(server: *Server, wlr_output: *wlr.Output) !void {
|
pub fn create(wlr_output: *wlr.Output) !*Output {
|
||||||
const output = try gpa.create(Output);
|
const output = try gpa.create(Output);
|
||||||
|
|
||||||
output.* = .{
|
output.* = .{
|
||||||
.server = server,
|
.wlr_output = wlr_output,
|
||||||
.wlr_output = wlr_output,
|
.scene_output = try server.root.scene.createSceneOutput(wlr_output)
|
||||||
};
|
};
|
||||||
wlr_output.events.frame.add(&output.frame);
|
wlr_output.events.frame.add(&output.frame);
|
||||||
wlr_output.events.request_state.add(&output.request_state);
|
wlr_output.events.request_state.add(&output.request_state);
|
||||||
wlr_output.events.destroy.add(&output.destroy);
|
wlr_output.events.destroy.add(&output.destroy);
|
||||||
|
|
||||||
const layout_output = try server.output_layout.addAuto(wlr_output);
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
const scene_output = try server.scene.createSceneOutput(wlr_output);
|
pub fn handleFrame(_: *wl.Listener(*wlr.Output), wlr_output: *wlr.Output) void {
|
||||||
server.scene_output_layout.addOutput(layout_output, scene_output);
|
const scene_output = server.scene.getSceneOutput(wlr_output).?;
|
||||||
}
|
_ = scene_output.commit(null);
|
||||||
|
|
||||||
pub fn handleFrame(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {
|
var now = posix.clock_gettime(posix.CLOCK.MONOTONIC) catch @panic("CLOCK_MONOTONIC not supported");
|
||||||
const output: *Output = @fieldParentPtr("frame", listener);
|
scene_output.sendFrameDone(&now);
|
||||||
|
}
|
||||||
|
|
||||||
const scene_output = output.server.scene.getSceneOutput(output.wlr_output).?;
|
pub fn handleRequestState(
|
||||||
_ = scene_output.commit(null);
|
listener: *wl.Listener(*wlr.Output.event.RequestState),
|
||||||
|
event: *wlr.Output.event.RequestState,
|
||||||
|
) void {
|
||||||
|
const output: *Output = @fieldParentPtr("request_state", listener);
|
||||||
|
|
||||||
var now = posix.clock_gettime(posix.CLOCK.MONOTONIC) catch @panic("CLOCK_MONOTONIC not supported");
|
_ = output.wlr_output.commitState(event.state);
|
||||||
scene_output.sendFrameDone(&now);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handleRequestState(
|
pub fn handleDestroy(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {
|
||||||
listener: *wl.Listener(*wlr.Output.event.RequestState),
|
const output: *Output = @fieldParentPtr("destroy", listener);
|
||||||
event: *wlr.Output.event.RequestState,
|
|
||||||
) void {
|
|
||||||
const output: *Output = @fieldParentPtr("request_state", listener);
|
|
||||||
|
|
||||||
_ = output.wlr_output.commitState(event.state);
|
output.frame.link.remove();
|
||||||
}
|
output.request_state.link.remove();
|
||||||
|
output.destroy.link.remove();
|
||||||
|
|
||||||
pub fn handleDestroy(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {
|
gpa.destroy(output);
|
||||||
const output: *Output = @fieldParentPtr("destroy", listener);
|
}
|
||||||
|
|
||||||
output.frame.link.remove();
|
|
||||||
output.request_state.link.remove();
|
|
||||||
output.destroy.link.remove();
|
|
||||||
|
|
||||||
gpa.destroy(output);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
71
src/root.zig
Normal file
71
src/root.zig
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const wl = @import("wayland").server.wl;
|
||||||
|
const wlr = @import("wlroots");
|
||||||
|
|
||||||
|
const Output = @import("output.zig").Output;
|
||||||
|
|
||||||
|
const server = &@import("main.zig").server;
|
||||||
|
|
||||||
|
pub const Root = struct {
|
||||||
|
scene: *wlr.Scene,
|
||||||
|
|
||||||
|
output_layout: *wlr.OutputLayout,
|
||||||
|
|
||||||
|
new_output: wl.Listener(*wlr.Output),
|
||||||
|
|
||||||
|
pub fn init(root: *Root) !void {
|
||||||
|
std.log.info("Creating root of mezzaluna", .{});
|
||||||
|
|
||||||
|
const output_layout = try wlr.OutputLayout.create(server.wl_server);
|
||||||
|
errdefer output_layout.destroy();
|
||||||
|
|
||||||
|
const scene = try wlr.Scene.create();
|
||||||
|
errdefer scene.tree.node.destroy();
|
||||||
|
|
||||||
|
root.* = .{
|
||||||
|
.scene = scene,
|
||||||
|
.output_layout = output_layout,
|
||||||
|
|
||||||
|
.new_output = .init(handleNewOutput),
|
||||||
|
};
|
||||||
|
|
||||||
|
server.backend.events.new_output.add(&root.new_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn addOutput(self: *Root, new_output: *Output) void {
|
||||||
|
_ = self.output_layout.addAuto(new_output.wlr_output) catch {
|
||||||
|
std.log.err("failed to add new output to output layout\n", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
_ = self.scene.createSceneOutput(new_output.wlr_output) catch {
|
||||||
|
std.log.err("failed to create scene output for new output", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn handleNewOutput(_: *wl.Listener(*wlr.Output), wlr_output: *wlr.Output) void {
|
||||||
|
std.log.info("Handling a new output - {s}", .{wlr_output.name});
|
||||||
|
|
||||||
|
if (!wlr_output.initRender(server.allocator, server.renderer)) return;
|
||||||
|
|
||||||
|
var state = wlr.Output.State.init();
|
||||||
|
defer state.finish();
|
||||||
|
|
||||||
|
state.setEnabled(true);
|
||||||
|
|
||||||
|
if (wlr_output.preferredMode()) |mode| {
|
||||||
|
state.setMode(mode);
|
||||||
|
}
|
||||||
|
if (!wlr_output.commitState(&state)) return;
|
||||||
|
|
||||||
|
const new_output = Output.create(wlr_output) catch {
|
||||||
|
std.log.err("failed to allocate new output", .{});
|
||||||
|
wlr_output.destroy();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
server.root.addOutput(new_output);
|
||||||
|
}
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
const wl = @import("wayland").server.wl;
|
|
||||||
const wlr = @import("wlroots");
|
|
||||||
|
|
||||||
// server owns
|
|
||||||
// - compositor
|
|
||||||
//
|
|
||||||
// wlr_compositor owms
|
|
||||||
// - a list of wlr_surfaces
|
|
||||||
//
|
|
||||||
// wlr_surfaces own_buffers
|
|
||||||
//
|
|
||||||
// struct wl_resource *_surface;
|
|
||||||
// wl_resource_for_each(_surface, &server->compositor->surfaces) {
|
|
||||||
// struct wlr_surface *surface = wlr_surface_from_resource(_surface);
|
|
||||||
// if (!wlr_surface_has_buffer(surface)) {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// struct wlr_box render_box = {
|
|
||||||
// .x = 20, .y = 20,
|
|
||||||
// .width = surface->current->width,
|
|
||||||
// .height = surface->current->height
|
|
||||||
// };
|
|
||||||
// float matrix[16];
|
|
||||||
// wlr_matrix_project_box(&matrix, &render_box,
|
|
||||||
// surface->current->transform,
|
|
||||||
// 0, &wlr_output->transform_matrix);
|
|
||||||
// wlr_render_with_matrix(renderer, surface->texture, &matrix, 1.0f);
|
|
||||||
// wlr_surface_send_frame_done(surface, &now);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// wlr_scene owns
|
|
||||||
// - list of outputs
|
|
||||||
// - wlr_scene_tree
|
|
||||||
//
|
|
||||||
// wlr_scene_tree owns
|
|
||||||
// - its own wlr_scene_node
|
|
||||||
// - list of its children wlr_scene_node
|
|
||||||
//
|
|
||||||
// wlr_scene_node can be TREE, RECT or BUFFER and owns
|
|
||||||
// - its own type
|
|
||||||
// - a pointer to its parent wlr_scene_tree
|
|
||||||
// - a list of children as wlr_scene_trees
|
|
||||||
// - boolean if enabled
|
|
||||||
// - x, y position relative to parent
|
|
||||||
// - wl signal for destroy
|
|
||||||
// - *void arbitrary data
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5,6 +5,7 @@ const wl = @import("wayland").server.wl;
|
||||||
const wlr = @import("wlroots");
|
const wlr = @import("wlroots");
|
||||||
|
|
||||||
const Output = @import("output.zig").Output;
|
const Output = @import("output.zig").Output;
|
||||||
|
const Root = @import("root.zig").Root;
|
||||||
|
|
||||||
pub const Server = struct {
|
pub const Server = struct {
|
||||||
allocator: *wlr.Allocator,
|
allocator: *wlr.Allocator,
|
||||||
|
|
@ -14,7 +15,6 @@ pub const Server = struct {
|
||||||
shm: *wlr.Shm,
|
shm: *wlr.Shm,
|
||||||
scene: *wlr.Scene,
|
scene: *wlr.Scene,
|
||||||
output_layout: *wlr.OutputLayout,
|
output_layout: *wlr.OutputLayout,
|
||||||
scene_output_layout: *wlr.SceneOutputLayout,
|
|
||||||
xdg_shell: *wlr.XdgShell,
|
xdg_shell: *wlr.XdgShell,
|
||||||
seat: *wlr.Seat,
|
seat: *wlr.Seat,
|
||||||
|
|
||||||
|
|
@ -24,7 +24,7 @@ pub const Server = struct {
|
||||||
|
|
||||||
compositor: *wlr.Compositor,
|
compositor: *wlr.Compositor,
|
||||||
|
|
||||||
new_output: wl.Listener(*wlr.Output) = .init(newOutput),
|
root: Root,
|
||||||
|
|
||||||
pub fn init(server: *Server) !void {
|
pub fn init(server: *Server) !void {
|
||||||
const wl_server = try wl.Server.create();
|
const wl_server = try wl.Server.create();
|
||||||
|
|
@ -45,13 +45,14 @@ pub const Server = struct {
|
||||||
.allocator = try wlr.Allocator.autocreate(backend, renderer),
|
.allocator = try wlr.Allocator.autocreate(backend, renderer),
|
||||||
.scene = scene,
|
.scene = scene,
|
||||||
.output_layout = output_layout,
|
.output_layout = output_layout,
|
||||||
.scene_output_layout = try scene.attachOutputLayout(output_layout),
|
|
||||||
.xdg_shell = try wlr.XdgShell.create(wl_server, 2),
|
.xdg_shell = try wlr.XdgShell.create(wl_server, 2),
|
||||||
.event_loop = event_loop,
|
.event_loop = event_loop,
|
||||||
.session = session,
|
.session = session,
|
||||||
.compositor = try wlr.Compositor.create(wl_server, 6, renderer),
|
.compositor = try wlr.Compositor.create(wl_server, 6, renderer),
|
||||||
.shm = try wlr.Shm.createWithRenderer(wl_server, 1, renderer),
|
.shm = try wlr.Shm.createWithRenderer(wl_server, 1, renderer),
|
||||||
.seat = try wlr.Seat.create(wl_server, "default"),
|
.seat = try wlr.Seat.create(wl_server, "default"),
|
||||||
|
|
||||||
|
.root = undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
try server.renderer.initServer(wl_server);
|
try server.renderer.initServer(wl_server);
|
||||||
|
|
@ -60,27 +61,6 @@ pub const Server = struct {
|
||||||
_ = try wlr.Subcompositor.create(server.wl_server);
|
_ = try wlr.Subcompositor.create(server.wl_server);
|
||||||
_ = try wlr.DataDeviceManager.create(server.wl_server);
|
_ = try wlr.DataDeviceManager.create(server.wl_server);
|
||||||
|
|
||||||
server.backend.events.new_output.add(&server.new_output);
|
try Root.init(&server.root);
|
||||||
}
|
|
||||||
|
|
||||||
fn newOutput(listener: *wl.Listener(*wlr.Output), wlr_output: *wlr.Output) void {
|
|
||||||
const server: *Server = @fieldParentPtr("new_output", listener);
|
|
||||||
|
|
||||||
if (!wlr_output.initRender(server.allocator, server.renderer)) return;
|
|
||||||
|
|
||||||
var state = wlr.Output.State.init();
|
|
||||||
defer state.finish();
|
|
||||||
|
|
||||||
state.setEnabled(true);
|
|
||||||
if (wlr_output.preferredMode()) |mode| {
|
|
||||||
state.setMode(mode);
|
|
||||||
}
|
|
||||||
if (!wlr_output.commitState(&state)) return;
|
|
||||||
|
|
||||||
Output.create(server, wlr_output) catch {
|
|
||||||
std.log.err("failed to allocate new output", .{});
|
|
||||||
wlr_output.destroy();
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue