diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 6d6cbdd..9bfd52d 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -24,3 +24,12 @@ mez.api.add_keymap("alt", "p", function() print("spawning foot") mez.api.spawn("wmenu-run") 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 84b9868..d693391 100644 --- a/src/keymap.zig +++ b/src/keymap.zig @@ -12,15 +12,20 @@ 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); diff --git a/src/lua/api.zig b/src/lua/input.zig similarity index 81% rename from src/lua/api.zig rename to src/lua/input.zig index c2aebb7..1282a21 100644 --- a/src/lua/api.zig +++ b/src/lua/input.zig @@ -22,7 +22,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 { // 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; keymap.options = .{ @@ -63,24 +63,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"); } }