inital support for hooks...

Currently the following hooks are available:
 - WinMapPre
 - WinMapPost
This commit is contained in:
Squibid 2025-11-21 23:28:40 -05:00
parent b45544c97a
commit b3322eeb90
Signed by: squibid
GPG key ID: BECE5684D3C4005D
7 changed files with 193 additions and 0 deletions

56
src/types/events.zig Normal file
View file

@ -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;
}
}
}

32
src/types/hook.zig Normal file
View file

@ -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);
}