From b3322eeb9032bcc5d07c4b18e04258ccb40541ec Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 21 Nov 2025 23:28:40 -0500 Subject: [PATCH] inital support for hooks... Currently the following hooks are available: - WinMapPre - WinMapPost --- runtime/share/mezzaluna/init.lua | 6 +++ src/lua/hook.zig | 81 ++++++++++++++++++++++++++++++++ src/lua/lua.zig | 6 +++ src/server.zig | 8 ++++ src/types/events.zig | 56 ++++++++++++++++++++++ src/types/hook.zig | 32 +++++++++++++ src/view.zig | 4 ++ 7 files changed, 193 insertions(+) create mode 100644 src/lua/hook.zig create mode 100644 src/types/events.zig create mode 100644 src/types/hook.zig diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 1839c12..f814277 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -49,3 +49,9 @@ end -- print("goodbye from my keymap") -- end -- }) + +mez.hook.add_hook("WinMapPre", { + callback = function() + print("hello world") + end +}) diff --git a/src/lua/hook.zig b/src/lua/hook.zig new file mode 100644 index 0000000..cf26c57 --- /dev/null +++ b/src/lua/hook.zig @@ -0,0 +1,81 @@ +const Hook = @This(); + +const std = @import("std"); + +const THook = @import("../types/hook.zig"); + +const zlua = @import("zlua"); + +const gpa = std.heap.c_allocator; +const server = &@import("../main.zig").server; + +pub fn add_hook(L: *zlua.Lua) i32 { + L.checkType(2, .table); + + var hook: *THook = gpa.create(THook) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + hook.events = std.ArrayList([]const u8).initCapacity(gpa, 1) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + + // We support both a string and a table of strings as the first value of + // add_hook. Regardless of which type is passed in we create an arraylist of + // []const u8's + if (L.isTable(1)) { + L.pushNil(); + while (L.next(1)) { + if (L.isString(-1)) { + const s = L.toString(-1) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + hook.events.append(gpa, s) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + } + L.pop(1); + } + } else if (L.isString(1)) { + const s = L.toString(1) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + hook.events.append(gpa, s) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + } + + _ = L.pushString("callback"); + _ = L.getTable(2); + if (L.isFunction(-1)) { + hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + } + + server.hooks.append(gpa, hook) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + + for (hook.events.items) |value| { + server.events.put(value, hook) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + } + + return 0; +} + +pub fn del_hook(L: *zlua.Lua) i32 { + // TODO: impl + _ = L; + return 0; +} diff --git a/src/lua/lua.zig b/src/lua/lua.zig index 43d2255..4a3ee58 100644 --- a/src/lua/lua.zig +++ b/src/lua/lua.zig @@ -8,6 +8,7 @@ const Bridge = @import("bridge.zig"); const Fs = @import("fs.zig"); const Input = @import("input.zig"); const Api = @import("api.zig"); +const Hook = @import("hook.zig"); const gpa = std.heap.c_allocator; @@ -64,6 +65,11 @@ pub fn init(self: *Lua) !void { self.state.newLib(input_funcs); self.state.setField(-2, "input"); } + { + const hook_funcs = zlua.fnRegsFromType(Hook); + self.state.newLib(hook_funcs); + self.state.setField(-2, "hook"); + } { const api_funcs = zlua.fnRegsFromType(Api); self.state.newLib(api_funcs); diff --git a/src/server.zig b/src/server.zig index d564f4f..12670eb 100644 --- a/src/server.zig +++ b/src/server.zig @@ -12,6 +12,8 @@ const Output = @import("output.zig"); const View = @import("view.zig"); const Utils = @import("utils.zig"); const Keymap = @import("keymap.zig"); +const Hook = @import("types/hook.zig"); +const Events = @import("types/events.zig"); const gpa = std.heap.c_allocator; const server = &@import("main.zig").server; @@ -34,7 +36,11 @@ allocator: *wlr.Allocator, root: Root, seat: Seat, cursor: Cursor, + +// lua data keymaps: std.AutoHashMap(u64, Keymap), +hooks: std.ArrayList(*Hook), +events: Events, // Backend listeners new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput), @@ -90,6 +96,8 @@ pub fn init(self: *Server) void { .seat = undefined, .cursor = undefined, .keymaps = .init(gpa), + .hooks = try .initCapacity(gpa, 10), // TODO: choose how many slots to start with + .events = try .init(gpa), }; self.renderer.initServer(wl_server) catch { diff --git a/src/types/events.zig b/src/types/events.zig new file mode 100644 index 0000000..8f6a111 --- /dev/null +++ b/src/types/events.zig @@ -0,0 +1,56 @@ +pub const Events = @This(); + +const std = @import("std"); + +const Hook = @import("hook.zig"); + +const Node = struct { + hook: *const Hook, + node: std.SinglyLinkedList.Node, +}; + +events: std.StringHashMap(*std.SinglyLinkedList), +allocator: std.mem.Allocator, + +pub fn init(allocator: std.mem.Allocator) !Events { + return Events{ + .allocator = allocator, + .events = .init(allocator), + }; +} + +pub fn put(self: *Events, key: []const u8, hook: *const Hook) !void { + var ll: *std.SinglyLinkedList = undefined; + if (self.events.get(key)) |sll| { + ll = sll; + } else { + ll = try self.allocator.create(std.SinglyLinkedList); + try self.events.put(key, ll); + if (self.events.get(key)) |sll| { + ll = sll; + } + } + const data = try self.allocator.create(Node); + data.* = .{ + .hook = hook, + .node = .{}, + }; + ll.prepend(&data.node); +} + +// TODO: figure out deletion +// pub fn del(self: *Events, key: ???) !void {} + +pub fn exec(self: *Events, event: []const u8) void { + if (self.events.get(event)) |e| { + var node = e.first; + while (node) |n| : (node = n.next) { + const data: *Node = @fieldParentPtr("node", n); + data.hook.callback(); + + // FIXME: not sure why but for some reason our ll doesn't seem to want to + // admit that there's nothing after the first node. + break; + } + } +} diff --git a/src/types/hook.zig b/src/types/hook.zig new file mode 100644 index 0000000..ae207df --- /dev/null +++ b/src/types/hook.zig @@ -0,0 +1,32 @@ +//! This is a simple way to define a hook. +const Hook = @This(); + +const std = @import("std"); + +const xkb = @import("xkbcommon"); +const wlr = @import("wlroots"); +const zlua = @import("zlua"); + +const Event = @import("events.zig"); +const Lua = &@import("../main.zig").lua; + +events: std.ArrayList([]const u8), // a list of events +options: struct { + // group: []const u8, // TODO: do we need groups? + /// This is the location of the callback lua function in the lua registry + lua_cb_ref_idx: i32, +}, + +pub fn callback(self: *const Hook) void { + const t = Lua.state.rawGetIndex(zlua.registry_index, self.options.lua_cb_ref_idx); + if (t != zlua.LuaType.function) { + std.log.err("Failed to call hook, it doesn't have a callback.", .{}); + Lua.state.pop(1); + return; + } + + // TODO: we need to send some data along with the callback, this data will + // change based on the event which the user is hooking into + Lua.state.call(.{ .args = 0, .results = 0 }); + Lua.state.pop(-1); +} diff --git a/src/view.zig b/src/view.zig index bedb2c7..07818df 100644 --- a/src/view.zig +++ b/src/view.zig @@ -100,6 +100,8 @@ fn handleMap(listener: *wl.Listener(void)) void { const view: *View = @fieldParentPtr("map", listener); std.log.debug("Mapping view '{s}'", .{view.xdg_toplevel.title orelse "(unnamed)"}); + server.events.exec("WinMapPre"); + view.xdg_toplevel.events.request_fullscreen.add(&view.request_fullscreen); view.xdg_toplevel.events.request_move.add(&view.request_move); view.xdg_toplevel.events.request_resize.add(&view.request_resize); @@ -121,6 +123,8 @@ fn handleMap(listener: *wl.Listener(void)) void { // Here is where we should tile and set size view.mapped = true; + + server.events.exec("WinMapPost"); } fn handleUnmap(listener: *wl.Listener(void)) void {