intial setup for passing arbitrary data to hooks

This commit is contained in:
Squibid 2025-11-22 18:16:43 -05:00
parent eeb6bf2278
commit 2c130539f6
Signed by: squibid
GPG key ID: BECE5684D3C4005D
4 changed files with 42 additions and 24 deletions

View file

@ -1,10 +1,10 @@
local env_conf = os.getenv("XDG_CONFIG_HOME") local env_conf = os.getenv("XDG_CONFIG_HOME")
if not env_conf then if not env_conf then
env_conf = os.getenv("HOME") env_conf = os.getenv("HOME")
if not env_conf then if not env_conf then
error("Couldn't determine potential config directory is $HOME set?") error("Couldn't determine potential config directory is $HOME set?")
end end
env_conf = mez.fs.joinpath(env_conf, ".config") env_conf = mez.fs.joinpath(env_conf, ".config")
end end
mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua")
@ -12,27 +12,27 @@ package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua
-- this is an example -- this is an example
mez.input.add_keymap("alt", "a", { mez.input.add_keymap("alt", "a", {
press = function() press = function()
print("hello from my keymap") print("hello from my keymap")
end end
}) })
mez.input.add_keymap("alt", "Return", { mez.input.add_keymap("alt", "Return", {
press = function() press = function()
mez.api.spawn("foot") mez.api.spawn("foot")
end, end,
}) })
mez.input.add_keymap("alt", "c", { mez.input.add_keymap("alt", "c", {
press = function () press = function ()
mez.api.close() mez.api.close()
end end
}) })
mez.input.add_keymap("alt", "q", { mez.input.add_keymap("alt", "q", {
press = function () press = function ()
mez.api.exit(); mez.api.exit();
end end
}) })
for i = 1, 12 do for i = 1, 12 do
@ -51,7 +51,8 @@ end
-- }) -- })
mez.hook.add_hook("ViewMapPre", { mez.hook.add_hook("ViewMapPre", {
callback = function() callback = function(a)
print(a)
print("hello world") print("hello world")
end end
}) })

View file

@ -41,12 +41,12 @@ pub fn put(self: *Events, key: []const u8, hook: *const Hook) !void {
// TODO: figure out deletion // TODO: figure out deletion
// pub fn del(self: *Events, key: ???) !void {} // pub fn del(self: *Events, key: ???) !void {}
pub fn exec(self: *Events, event: []const u8) void { pub fn exec(self: *Events, event: []const u8, args: anytype) void {
if (self.events.get(event)) |e| { if (self.events.get(event)) |e| {
var node = e.first; var node = e.first;
while (node) |n| : (node = n.next) { while (node) |n| : (node = n.next) {
const data: *Node = @fieldParentPtr("node", n); const data: *Node = @fieldParentPtr("node", n);
data.hook.callback(); data.hook.callback(args);
// FIXME: not sure why but for some reason our ll doesn't seem to want to // 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. // admit that there's nothing after the first node.

View file

@ -17,7 +17,13 @@ options: struct {
lua_cb_ref_idx: i32, lua_cb_ref_idx: i32,
}, },
pub fn callback(self: *const Hook) void { pub fn callback(self: *const Hook, args: anytype) void {
const ArgsType = @TypeOf(args);
const args_type_info = @typeInfo(ArgsType);
if (args_type_info != .@"struct") {
@compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType));
}
const t = Lua.state.rawGetIndex(zlua.registry_index, self.options.lua_cb_ref_idx); const t = Lua.state.rawGetIndex(zlua.registry_index, self.options.lua_cb_ref_idx);
if (t != zlua.LuaType.function) { if (t != zlua.LuaType.function) {
std.log.err("Failed to call hook, it doesn't have a callback.", .{}); std.log.err("Failed to call hook, it doesn't have a callback.", .{});
@ -25,8 +31,19 @@ pub fn callback(self: *const Hook) void {
return; return;
} }
var i: u8 = 0;
inline for (args, 0..) |field, k| {
// std.log.debug("{any}", .{field});
// oh dear god I hope this works
std.log.debug("sldkjf {any}", .{field});
try Lua.state.pushAny(field);
i = k;
}
// TODO: we need to send some data along with the callback, this data will // 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 // change based on the event which the user is hooking into
Lua.state.call(.{ .args = 0, .results = 0 }); Lua.state.protectedCall(.{ .args = i, .results = 0 }) catch {
};
Lua.state.pop(-1); Lua.state.pop(-1);
} }

View file

@ -100,7 +100,7 @@ fn handleMap(listener: *wl.Listener(void)) void {
const view: *View = @fieldParentPtr("map", listener); const view: *View = @fieldParentPtr("map", listener);
std.log.debug("Mapping view '{s}'", .{view.xdg_toplevel.title orelse "(unnamed)"}); std.log.debug("Mapping view '{s}'", .{view.xdg_toplevel.title orelse "(unnamed)"});
server.events.exec("ViewMapPre"); server.events.exec("ViewMapPre", .{view.id});
view.xdg_toplevel.events.request_fullscreen.add(&view.request_fullscreen); 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_move.add(&view.request_move);
@ -124,7 +124,7 @@ fn handleMap(listener: *wl.Listener(void)) void {
view.mapped = true; view.mapped = true;
server.events.exec("ViewMapPost"); server.events.exec("ViewMapPost", .{});
} }
fn handleUnmap(listener: *wl.Listener(void)) void { fn handleUnmap(listener: *wl.Listener(void)) void {