From b3322eeb9032bcc5d07c4b18e04258ccb40541ec Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 21 Nov 2025 23:28:40 -0500 Subject: [PATCH 1/9] 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 { From 41cbe1726217220ce28599e8bb81e46f7a305a8f Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 21 Nov 2025 23:32:03 -0500 Subject: [PATCH 2/9] cleanup the keymap stuff --- src/keyboard.zig | 6 +++--- src/lua/input.zig | 10 ++++------ src/server.zig | 2 +- src/{ => types}/keymap.zig | 12 ++++++------ 4 files changed, 14 insertions(+), 16 deletions(-) rename src/{ => types}/keymap.zig (73%) diff --git a/src/keyboard.zig b/src/keyboard.zig index a360ce0..260ef75 100644 --- a/src/keyboard.zig +++ b/src/keyboard.zig @@ -6,7 +6,7 @@ const Keyboard = @This(); const std = @import("std"); const gpa = std.heap.c_allocator; const server = &@import("main.zig").server; -const Keymap = @import("keymap.zig"); +const Keymap = @import("types/keymap.zig"); const Utils = @import("utils.zig"); const wl = @import("wayland").server.wl; @@ -80,10 +80,10 @@ fn handleKey(_: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.even if (server.seat.keyboard_group.keyboard.xkb_state) |xkb_state| { for (xkb_state.keyGetSyms(keycode)) |sym| { if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| { - if (event.state == .pressed and map.lua_press_ref_idx > 0) { + if (event.state == .pressed and map.options.lua_press_ref_idx > 0) { map.callback(false); handled = true; - } else if (event.state == .released and map.lua_release_ref_idx > 0) { + } else if (event.state == .released and map.options.lua_release_ref_idx > 0) { map.callback(true); handled = true; } diff --git a/src/lua/input.zig b/src/lua/input.zig index e1633e0..5ecea31 100644 --- a/src/lua/input.zig +++ b/src/lua/input.zig @@ -1,7 +1,7 @@ const Api = @This(); const std = @import("std"); -const Keymap = @import("../keymap.zig"); +const Keymap = @import("../types/keymap.zig"); const zlua = @import("zlua"); const xkb = @import("xkbcommon"); @@ -43,9 +43,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 { L.checkType(3, .table); var keymap: Keymap = undefined; - keymap.options = .{ - .repeat = true, - }; + keymap.options.repeat = true; const mod = L.toString(1) catch { L.raiseErrorStr("Lua error check your config", .{}); @@ -62,7 +60,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 { _ = L.pushString("press"); _ = L.getTable(3); if (L.isFunction(-1)) { - keymap.lua_press_ref_idx = L.ref(zlua.registry_index) catch { + keymap.options.lua_press_ref_idx = L.ref(zlua.registry_index) catch { L.raiseErrorStr("Lua error check your config", .{}); return 0; }; @@ -71,7 +69,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 { _ = L.pushString("release"); _ = L.getTable(3); if (L.isFunction(-1)) { - keymap.lua_release_ref_idx = L.ref(zlua.registry_index) catch { + keymap.options.lua_release_ref_idx = L.ref(zlua.registry_index) catch { L.raiseErrorStr("Lua error check your config", .{}); return 0; }; diff --git a/src/server.zig b/src/server.zig index 12670eb..1aafa5a 100644 --- a/src/server.zig +++ b/src/server.zig @@ -11,7 +11,7 @@ const Keyboard = @import("keyboard.zig"); const Output = @import("output.zig"); const View = @import("view.zig"); const Utils = @import("utils.zig"); -const Keymap = @import("keymap.zig"); +const Keymap = @import("types/keymap.zig"); const Hook = @import("types/hook.zig"); const Events = @import("types/events.zig"); diff --git a/src/keymap.zig b/src/types/keymap.zig similarity index 73% rename from src/keymap.zig rename to src/types/keymap.zig index 93a74c8..1b43e27 100644 --- a/src/keymap.zig +++ b/src/types/keymap.zig @@ -8,20 +8,20 @@ const xkb = @import("xkbcommon"); const wlr = @import("wlroots"); const zlua = @import("zlua"); -const Lua = &@import("main.zig").lua; +const Lua = &@import("../main.zig").lua; modifier: wlr.Keyboard.ModifierMask, keycode: xkb.Keysym, -/// This is the location of the on press lua function in the lua registry -lua_press_ref_idx: i32, -/// This is the location of the on release lua function in the lua registry -lua_release_ref_idx: i32, options: struct { repeat: bool, + /// This is the location of the on press lua function in the lua registry + lua_press_ref_idx: i32, + /// This is the location of the on release lua function in the lua registry + lua_release_ref_idx: i32, }, pub fn callback(self: *const Keymap, release: bool) void { - const lua_ref_idx = if(release) self.lua_release_ref_idx else self.lua_press_ref_idx; + const lua_ref_idx = if (release) self.options.lua_release_ref_idx else self.options.lua_press_ref_idx; const t = Lua.state.rawGetIndex(zlua.registry_index, lua_ref_idx); if (t != zlua.LuaType.function) { From eeb6bf2278716a3f51eb6d2e6b6df592ffb274e0 Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 21 Nov 2025 23:34:08 -0500 Subject: [PATCH 3/9] rename WinMap{Pre,Post} to ViewMap{Pre,Post} cause View is better --- runtime/share/mezzaluna/init.lua | 2 +- src/view.zig | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index f814277..f55ff0b 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -50,7 +50,7 @@ end -- end -- }) -mez.hook.add_hook("WinMapPre", { +mez.hook.add_hook("ViewMapPre", { callback = function() print("hello world") end diff --git a/src/view.zig b/src/view.zig index 07818df..9327de8 100644 --- a/src/view.zig +++ b/src/view.zig @@ -100,7 +100,7 @@ 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"); + server.events.exec("ViewMapPre"); view.xdg_toplevel.events.request_fullscreen.add(&view.request_fullscreen); view.xdg_toplevel.events.request_move.add(&view.request_move); @@ -124,7 +124,7 @@ fn handleMap(listener: *wl.Listener(void)) void { view.mapped = true; - server.events.exec("WinMapPost"); + server.events.exec("ViewMapPost"); } fn handleUnmap(listener: *wl.Listener(void)) void { From 9648dc7872c0fa2e36e556196c904b55d99247ac Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 22 Nov 2025 18:27:41 -0500 Subject: [PATCH 4/9] fix crashing when a view is resized too small this is done by limiting to a minimum of 10x10 pixels, we may make this configurable using lua in the future --- src/cursor.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cursor.zig b/src/cursor.zig index 0a076f8..9beee6b 100644 --- a/src/cursor.zig +++ b/src/cursor.zig @@ -100,8 +100,9 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void { if(focused_view) |view| { _ = view.xdg_toplevel.setSize( - @intCast(@as(c_int, @intFromFloat(self.wlr_cursor.x)) - view.scene_tree.node.x), - @intCast(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - view.scene_tree.node.y) + // TODO: configure the min and max using lua? + std.math.clamp(@as(c_int, @as(i32, @intFromFloat(self.wlr_cursor.x)) - view.scene_tree.node.x), 10, std.math.maxInt(i32)), + std.math.clamp(@as(c_int, @as(i32, @intFromFloat(self.wlr_cursor.y)) - view.scene_tree.node.y), 10, std.math.maxInt(i32)) ); } }, From c5e566736ad0f070dafea4955c1c5ebed5a96ac8 Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 22 Nov 2025 18:48:16 -0500 Subject: [PATCH 5/9] update log statement --- src/view.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/view.zig b/src/view.zig index 9327de8..b27ff6f 100644 --- a/src/view.zig +++ b/src/view.zig @@ -257,7 +257,7 @@ fn handleSetAppId( ) void { const view: *View = @fieldParentPtr("set_app_id", listener); _ = view; - std.log.err("Unimplemented request maximize", .{}); + std.log.err("Unimplemented set appid", .{}); } fn handleSetTitle( From 746ec6ba6fa075b61ad21b3a43ef1d3d5e315d15 Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 22 Nov 2025 18:49:46 -0500 Subject: [PATCH 6/9] remove duplicate function --- src/view.zig | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/view.zig b/src/view.zig index b27ff6f..1b98ff5 100644 --- a/src/view.zig +++ b/src/view.zig @@ -244,14 +244,6 @@ fn handleRequestMinimize( std.log.err("Unimplemented request minimize", .{}); } -fn handleRequestMaximize( - listener: *wl.Listener(void) -) void { - const view: *View = @fieldParentPtr("request_fullscreen", listener); - _ = view; - std.log.err("Unimplemented request maximize", .{}); -} - fn handleSetAppId( listener: *wl.Listener(void) ) void { From 36afbe0832155d207a594b419b3e82638f440d1e Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 22 Nov 2025 20:46:57 -0500 Subject: [PATCH 7/9] default view moving shouldn't clamp to the visible area --- src/cursor.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cursor.zig b/src/cursor.zig index 9beee6b..2a72a67 100644 --- a/src/cursor.zig +++ b/src/cursor.zig @@ -89,8 +89,11 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void { if(focused_view) |view| { view.scene_tree.node.setPosition( - std.math.clamp(@as(c_int, @intFromFloat(self.wlr_cursor.x)) - self.drag_view_offset_x, 0, std.math.maxInt(u32)), - std.math.clamp(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - self.drag_view_offset_y, 0, std.math.maxInt(u32)) + // TODO: add a lua option to configure the behavior of this, by + // default it will be the following: + @as(c_int, @intFromFloat(self.wlr_cursor.x)) - self.drag_view_offset_x, + @as(c_int, @intFromFloat(self.wlr_cursor.y)) - self.drag_view_offset_y + // and the user should be able to configure if it clamps or not ); } }, From 4fff609df9d7e3718f8e2ae618e1d24aeb3ebfc1 Mon Sep 17 00:00:00 2001 From: Squibid Date: Sun, 23 Nov 2025 17:08:44 -0500 Subject: [PATCH 8/9] generate if branching for modifier keys at comptime --- src/lua/input.zig | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/lua/input.zig b/src/lua/input.zig index 5ecea31..8b42796 100644 --- a/src/lua/input.zig +++ b/src/lua/input.zig @@ -14,22 +14,10 @@ fn parse_modkeys(modStr: []const u8) wlr.Keyboard.ModifierMask { var it = std.mem.splitScalar(u8, modStr, '|'); var modifiers = wlr.Keyboard.ModifierMask{}; while (it.next()) |m| { - if (std.mem.eql(u8, m, "shift")) { - modifiers.shift = true; - } else if (std.mem.eql(u8, m, "caps")) { - modifiers.caps = true; - } else if (std.mem.eql(u8, m, "ctrl")) { - modifiers.ctrl = true; - } else if (std.mem.eql(u8, m, "alt")) { - modifiers.alt = true; - } else if (std.mem.eql(u8, m, "mod2")) { - modifiers.mod2 = true; - } else if (std.mem.eql(u8, m, "mod3")) { - modifiers.mod3 = true; - } else if (std.mem.eql(u8, m, "logo")) { - modifiers.logo = true; - } else if (std.mem.eql(u8, m, "mod5")) { - modifiers.mod5 = true; + inline for (std.meta.fields(@TypeOf(modifiers))) |f| { + if (f.type == bool and std.mem.eql(u8, m, f.name)) { + @field(modifiers, f.name) = true; + } } } From f9b9d4140214fab4781dd899bc90049d091df984 Mon Sep 17 00:00:00 2001 From: Squibid Date: Sun, 23 Nov 2025 21:05:29 -0500 Subject: [PATCH 9/9] rename mez.hook.add_hook -> mez.hook.add --- runtime/share/mezzaluna/init.lua | 2 +- src/lua/hook.zig | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index f55ff0b..019861e 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -50,7 +50,7 @@ end -- end -- }) -mez.hook.add_hook("ViewMapPre", { +mez.hook.add("ViewMapPre", { callback = function() print("hello world") end diff --git a/src/lua/hook.zig b/src/lua/hook.zig index cf26c57..6273f2f 100644 --- a/src/lua/hook.zig +++ b/src/lua/hook.zig @@ -9,7 +9,7 @@ const zlua = @import("zlua"); const gpa = std.heap.c_allocator; const server = &@import("../main.zig").server; -pub fn add_hook(L: *zlua.Lua) i32 { +pub fn add(L: *zlua.Lua) i32 { L.checkType(2, .table); var hook: *THook = gpa.create(THook) catch { @@ -22,7 +22,7 @@ pub fn add_hook(L: *zlua.Lua) i32 { }; // 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 + // add. Regardless of which type is passed in we create an arraylist of // []const u8's if (L.isTable(1)) { L.pushNil(); @@ -74,7 +74,7 @@ pub fn add_hook(L: *zlua.Lua) i32 { return 0; } -pub fn del_hook(L: *zlua.Lua) i32 { +pub fn del(L: *zlua.Lua) i32 { // TODO: impl _ = L; return 0;