actually setup the remote_lua protocol on the server

This commit is contained in:
Squibid 2025-11-28 18:16:33 -05:00
parent ffbe496599
commit 7ed5c4840d
Signed by: squibid
GPG key ID: BECE5684D3C4005D
3 changed files with 36 additions and 15 deletions

View file

@ -3,15 +3,16 @@ const RemoteLua = @This();
const std = @import("std"); const std = @import("std");
const wayland = @import("wayland"); const wayland = @import("wayland");
const Utils = @import("utils.zig"); const Utils = @import("utils.zig");
const Lua = @import("lua/lua.zig");
const wl = wayland.server.wl; const wl = wayland.server.wl;
const mez = wayland.server.zmez; const mez = wayland.server.zmez;
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
const server = &@import("main.zig").server; const server = &@import("main.zig").server;
const Lua = &@import("main.zig").lua;
id: usize, id: usize,
remote_lua_v1: *mez.RemoteLuaV1, remote_lua_v1: *mez.RemoteLuaV1,
L: Lua,
pub fn sendNewLogEntry(str: [*:0]const u8) void { pub fn sendNewLogEntry(str: [*:0]const u8) void {
for (server.remote_lua_clients.items) |c| { for (server.remote_lua_clients.items) |c| {
@ -24,32 +25,49 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void {
const node = try gpa.create(RemoteLua); const node = try gpa.create(RemoteLua);
errdefer gpa.destroy(node); errdefer gpa.destroy(node);
node = .{ node.* = .{
.remote_lua_v1 = remote_lua_v1, .remote_lua_v1 = remote_lua_v1,
.id = server.remote_lua_clients.items.len, .id = server.remote_lua_clients.items.len,
.L = undefined,
}; };
server.remote_lua_clients.append(gpa, node); try node.L.init();
errdefer node.L.deinit();
try server.remote_lua_clients.append(gpa, node);
remote_lua_v1.setHandler(*RemoteLua, handleRequest, handleDestroy, &node); remote_lua_v1.setHandler(*RemoteLua, handleRequest, handleDestroy, node);
} }
fn handleRequest( fn handleRequest(
remote_lua_v1: *mez.RemoteLuaV1, remote_lua_v1: *mez.RemoteLuaV1,
request: mez.RemoteLuaV1.Request, request: mez.RemoteLuaV1.Request,
_: *RemoteLua, remote: *RemoteLua,
) void { ) void {
switch (request) { switch (request) {
.destroy => remote_lua_v1.destroy(), .destroy => remote_lua_v1.destroy(),
.push_lua => |req| { .push_lua => |req| {
Lua.state.loadString(req.lua_chunk) catch { const chunk = std.mem.sliceTo(req.lua_chunk, 0);
const errTxt: []const u8 = Lua.state.toString(-1) catch unreachable; remote.L.state.loadString(chunk) catch catchLuaFail(remote);
try sendNewLogEntry("repl: " ++ errTxt); remote.L.state.protectedCall(.{}) catch catchLuaFail(remote);
};
}, },
} }
} }
fn catchLuaFail(remote: *RemoteLua) void {
const err_txt: []const u8 = remote.L.state.toString(-1) catch unreachable;
const txt = std.mem.concat(gpa, u8, &[_][]const u8{ "repl: ", err_txt }) catch Utils.oomPanic();
defer gpa.free(txt);
// must add the sentinel back to pass the data over the wire
const w_sentinel = gpa.allocSentinel(u8, txt.len, 0) catch Utils.oomPanic();
defer gpa.free(w_sentinel);
std.mem.copyForwards(u8, w_sentinel, txt[0..txt.len]);
sendNewLogEntry(w_sentinel);
return;
}
fn handleDestroy(_: *mez.RemoteLuaV1, remote_lua: *RemoteLua) void { fn handleDestroy(_: *mez.RemoteLuaV1, remote_lua: *RemoteLua) void {
server.remote_lua_clients.swapRemove(remote_lua.id); _ = server.remote_lua_clients.swapRemove(remote_lua.id);
remote_lua.L.deinit();
gpa.destroy(remote_lua); gpa.destroy(remote_lua);
} }

View file

@ -12,7 +12,7 @@ const server = &@import("main.zig").server;
global: *wl.Global, global: *wl.Global,
pub fn init() ?*RemoteLuaManager { pub fn init() !?*RemoteLuaManager {
const self = try gpa.create(RemoteLuaManager); const self = try gpa.create(RemoteLuaManager);
self.global = try wl.Global.create(server.wl_server, mez.RemoteLuaManagerV1, 1, ?*anyopaque, null, bind); self.global = try wl.Global.create(server.wl_server, mez.RemoteLuaManagerV1, 1, ?*anyopaque, null, bind);

View file

@ -15,6 +15,7 @@ const Keymap = @import("types/keymap.zig");
const Hook = @import("types/hook.zig"); const Hook = @import("types/hook.zig");
const Events = @import("types/events.zig"); const Events = @import("types/events.zig");
const RemoteLua = @import("RemoteLua.zig"); const RemoteLua = @import("RemoteLua.zig");
const RemoteLuaManager = @import("RemoteLuaManager.zig");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
const server = &@import("main.zig").server; const server = &@import("main.zig").server;
@ -25,6 +26,7 @@ renderer: *wlr.Renderer,
backend: *wlr.Backend, backend: *wlr.Backend,
event_loop: *wl.EventLoop, event_loop: *wl.EventLoop,
session: ?*wlr.Session, session: ?*wlr.Session,
remote_lua_manager: ?*RemoteLuaManager,
shm: *wlr.Shm, shm: *wlr.Shm,
xdg_shell: *wlr.XdgShell, xdg_shell: *wlr.XdgShell,
@ -97,6 +99,7 @@ pub fn init(self: *Server) void {
.root = undefined, .root = undefined,
.seat = undefined, .seat = undefined,
.cursor = undefined, .cursor = undefined,
.remote_lua_manager = RemoteLuaManager.init() catch Utils.oomPanic(),
.keymaps = .init(gpa), .keymaps = .init(gpa),
.hooks = try .initCapacity(gpa, 10), // TODO: choose how many slots to start with .hooks = try .initCapacity(gpa, 10), // TODO: choose how many slots to start with
.events = try .init(gpa), .events = try .init(gpa),