From cc0862adae6ae7df917d126b718575aeeac9123b Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 24 Oct 2025 22:48:22 -0400 Subject: [PATCH] update keymap func --- runtime/share/mezzaluna/init.lua | 11 +++++--- src/keyboard.zig | 7 +++-- src/keymap.zig | 19 +++++++++----- src/lua/{api.zig => input.zig} | 45 +++++++++++++++----------------- src/lua/lua.zig | 8 +++--- 5 files changed, 50 insertions(+), 40 deletions(-) rename src/lua/{api.zig => input.zig} (69%) diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 767c610..2cdcd22 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -11,6 +11,11 @@ mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua") -- this is an example -mez.api.add_keymap("alt", "a", function() - print("hello from my keymap") -end) +-- mez.input.add_keymap("alt", "a", { +-- press = function() +-- print("hello from my keymap") +-- end, +-- release = function() +-- print("goodbye from my keymap") +-- end +-- }) diff --git a/src/keyboard.zig b/src/keyboard.zig index c2af9ea..0c168e1 100644 --- a/src/keyboard.zig +++ b/src/keyboard.zig @@ -83,8 +83,11 @@ fn handleKey(_: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.even const modifiers = server.keyboard.wlr_keyboard.getModifiers(); for (server.keyboard.wlr_keyboard.xkb_state.?.keyGetSyms(keycode)) |sym| { if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| { - if (event.state == .pressed and !map.options.on_release) { - map.callback(); + if (event.state == .pressed and map.lua_press_ref_idx > 0) { + map.callback(false); + handled = true; + } else if (event.state == .released and map.lua_release_ref_idx > 0) { + map.callback(true); handled = true; } } diff --git a/src/keymap.zig b/src/keymap.zig index c6363d6..34dd1dd 100644 --- a/src/keymap.zig +++ b/src/keymap.zig @@ -12,20 +12,25 @@ const Lua = &@import("main.zig").lua; modifier: wlr.Keyboard.ModifierMask, keycode: xkb.Keysym, -/// This is the location of the lua function in the lua registry -lua_ref_idx: i32, +/// 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 { - /// if false the callback is called on press - on_release: bool, + repeat: bool, }, -pub fn callback(self: *const Keymap) void { - const t = Lua.state.rawGetIndex(zlua.registry_index, self.lua_ref_idx); +pub fn callback(self: *const Keymap, release: bool) void { + var lua_ref_idx = self.lua_press_ref_idx; + if (release) { + lua_ref_idx = self.lua_release_ref_idx; + } + const t = Lua.state.rawGetIndex(zlua.registry_index, lua_ref_idx); if (t != zlua.LuaType.function) { std.log.err("Failed to call keybind, it doesn't have a callback.", .{}); + Lua.state.pop(-1); return; } - Lua.state.pushValue(1); Lua.state.call(.{ .args = 0, .results = 0 }); Lua.state.pop(-1); } diff --git a/src/lua/api.zig b/src/lua/input.zig similarity index 69% rename from src/lua/api.zig rename to src/lua/input.zig index 0f097c6..514ef17 100644 --- a/src/lua/api.zig +++ b/src/lua/input.zig @@ -11,16 +11,9 @@ const wlr = @import("wlroots"); const gpa = std.heap.c_allocator; pub fn add_keymap(L: *zlua.Lua) i32 { - const nargs: i32 = L.getTop(); - if (nargs < 3) { - L.raiseErrorStr("Expected at least three arguments", .{}); - return 0; - } - - // ensure the first three agrs of the correct types L.checkType(1, .string); L.checkType(2, .string); - L.checkType(3, .function); + L.checkType(3, .table); var keymap: Keymap = undefined; @@ -58,24 +51,28 @@ pub fn add_keymap(L: *zlua.Lua) i32 { }; keymap.keycode = xkb.Keysym.fromName(key, .no_flags); - L.checkType(3, .function); - keymap.lua_ref_idx = L.ref(zlua.registry_index) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; - - // FIXME: for som reason I can't seem to get this to validate that the 4th - // argument exists unless there's a 5th argument. It doesn't seem to matter - // what type the 5th is just that it's there. - if (nargs == 4) { - // L.checkType(4, .table); - // _ = L.pushString("on_release"); - // _ = L.getTable(4); - // const b = L.toBoolean(-1); - // L.pop(-1); - // L.pop(-1); + _ = L.pushString("press"); + _ = L.getTable(3); + if (L.isFunction(-1)) { + keymap.lua_press_ref_idx = L.ref(zlua.registry_index) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; } + _ = L.pushString("release"); + _ = L.getTable(3); + if (L.isFunction(-1)) { + keymap.lua_release_ref_idx = L.ref(zlua.registry_index) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + } + + _ = L.pushString("repeat"); + _ = L.getTable(3); + keymap.options.repeat = L.isNil(-1) or L.toBoolean(-1); + const hash = Keymap.hash(keymap.modifier, keymap.keycode); server.keymaps.put(hash, keymap) catch |err| { std.log.err("Failed to add keymap to keymaps: {}", .{err}); diff --git a/src/lua/lua.zig b/src/lua/lua.zig index e63df2d..5e2db41 100644 --- a/src/lua/lua.zig +++ b/src/lua/lua.zig @@ -6,7 +6,7 @@ const zlua = @import("zlua"); const Bridge = @import("bridge.zig"); const Fs = @import("fs.zig"); -const Api = @import("api.zig"); +const Input = @import("input.zig"); const gpa = std.heap.c_allocator; @@ -59,9 +59,9 @@ pub fn init(self: *Lua) !void { self.state.setField(-2, "fs"); } { - const api_funcs = zlua.fnRegsFromType(Api); - self.state.newLib(api_funcs); - self.state.setField(-2, "api"); + const input_funcs = zlua.fnRegsFromType(Input); + self.state.newLib(input_funcs); + self.state.setField(-2, "input"); } }