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

View file

@ -49,3 +49,9 @@ end
-- print("goodbye from my keymap") -- print("goodbye from my keymap")
-- end -- end
-- }) -- })
mez.hook.add_hook("WinMapPre", {
callback = function()
print("hello world")
end
})

81
src/lua/hook.zig Normal file
View file

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

View file

@ -8,6 +8,7 @@ const Bridge = @import("bridge.zig");
const Fs = @import("fs.zig"); const Fs = @import("fs.zig");
const Input = @import("input.zig"); const Input = @import("input.zig");
const Api = @import("api.zig"); const Api = @import("api.zig");
const Hook = @import("hook.zig");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
@ -64,6 +65,11 @@ pub fn init(self: *Lua) !void {
self.state.newLib(input_funcs); self.state.newLib(input_funcs);
self.state.setField(-2, "input"); 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); const api_funcs = zlua.fnRegsFromType(Api);
self.state.newLib(api_funcs); self.state.newLib(api_funcs);

View file

@ -12,6 +12,8 @@ const Output = @import("output.zig");
const View = @import("view.zig"); const View = @import("view.zig");
const Utils = @import("utils.zig"); const Utils = @import("utils.zig");
const Keymap = @import("keymap.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 gpa = std.heap.c_allocator;
const server = &@import("main.zig").server; const server = &@import("main.zig").server;
@ -34,7 +36,11 @@ allocator: *wlr.Allocator,
root: Root, root: Root,
seat: Seat, seat: Seat,
cursor: Cursor, cursor: Cursor,
// lua data
keymaps: std.AutoHashMap(u64, Keymap), keymaps: std.AutoHashMap(u64, Keymap),
hooks: std.ArrayList(*Hook),
events: Events,
// Backend listeners // Backend listeners
new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput), new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput),
@ -90,6 +96,8 @@ pub fn init(self: *Server) void {
.seat = undefined, .seat = undefined,
.cursor = undefined, .cursor = undefined,
.keymaps = .init(gpa), .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 { self.renderer.initServer(wl_server) catch {

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

View file

@ -100,6 +100,8 @@ 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("WinMapPre");
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);
view.xdg_toplevel.events.request_resize.add(&view.request_resize); 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 // Here is where we should tile and set size
view.mapped = true; view.mapped = true;
server.events.exec("WinMapPost");
} }
fn handleUnmap(listener: *wl.Listener(void)) void { fn handleUnmap(listener: *wl.Listener(void)) void {