This commit is contained in:
Harrison DiAmbrosio 2025-10-24 23:45:11 -04:00
commit 3cf05f2d9a
5 changed files with 50 additions and 29 deletions

View file

@ -24,3 +24,12 @@ mez.api.add_keymap("alt", "p", function()
print("spawning foot") print("spawning foot")
mez.api.spawn("wmenu-run") mez.api.spawn("wmenu-run")
end) end)
-- mez.input.add_keymap("alt", "a", {
-- press = function()
-- print("hello from my keymap")
-- end,
-- release = function()
-- print("goodbye from my keymap")
-- end
-- })

View file

@ -83,8 +83,11 @@ fn handleKey(_: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.even
const modifiers = server.keyboard.wlr_keyboard.getModifiers(); const modifiers = server.keyboard.wlr_keyboard.getModifiers();
for (server.keyboard.wlr_keyboard.xkb_state.?.keyGetSyms(keycode)) |sym| { for (server.keyboard.wlr_keyboard.xkb_state.?.keyGetSyms(keycode)) |sym| {
if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| { if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| {
if (event.state == .pressed and !map.options.on_release) { if (event.state == .pressed and map.lua_press_ref_idx > 0) {
map.callback(); map.callback(false);
handled = true;
} else if (event.state == .released and map.lua_release_ref_idx > 0) {
map.callback(true);
handled = true; handled = true;
} }
} }

View file

@ -12,15 +12,20 @@ const Lua = &@import("main.zig").lua;
modifier: wlr.Keyboard.ModifierMask, modifier: wlr.Keyboard.ModifierMask,
keycode: xkb.Keysym, keycode: xkb.Keysym,
/// This is the location of the lua function in the lua registry /// This is the location of the on press lua function in the lua registry
lua_ref_idx: i32, 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 { options: struct {
/// if false the callback is called on press repeat: bool,
on_release: bool,
}, },
pub fn callback(self: *const Keymap) void { pub fn callback(self: *const Keymap, release: bool) void {
const t = Lua.state.rawGetIndex(zlua.registry_index, self.lua_ref_idx); 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) { if (t != zlua.LuaType.function) {
std.log.err("Failed to call keybind, it doesn't have a callback.", .{}); std.log.err("Failed to call keybind, it doesn't have a callback.", .{});
Lua.state.pop(1); Lua.state.pop(1);

View file

@ -22,7 +22,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
// ensure the first three agrs of the correct types // ensure the first three agrs of the correct types
L.checkType(1, .string); L.checkType(1, .string);
L.checkType(2, .string); L.checkType(2, .string);
L.checkType(3, .function); L.checkType(3, .table);
var keymap: Keymap = undefined; var keymap: Keymap = undefined;
keymap.options = .{ keymap.options = .{
@ -63,24 +63,28 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
}; };
keymap.keycode = xkb.Keysym.fromName(key, .no_flags); keymap.keycode = xkb.Keysym.fromName(key, .no_flags);
L.checkType(3, .function); _ = L.pushString("press");
keymap.lua_ref_idx = L.ref(zlua.registry_index) catch { _ = 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", .{}); L.raiseErrorStr("Lua error check your config", .{});
return 0; 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("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); const hash = Keymap.hash(keymap.modifier, keymap.keycode);
server.keymaps.put(hash, keymap) catch |err| { server.keymaps.put(hash, keymap) catch |err| {
std.log.err("Failed to add keymap to keymaps: {}", .{err}); std.log.err("Failed to add keymap to keymaps: {}", .{err});

View file

@ -6,7 +6,7 @@ const zlua = @import("zlua");
const Bridge = @import("bridge.zig"); const Bridge = @import("bridge.zig");
const Fs = @import("fs.zig"); const Fs = @import("fs.zig");
const Api = @import("api.zig"); const Input = @import("input.zig");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
@ -59,9 +59,9 @@ pub fn init(self: *Lua) !void {
self.state.setField(-2, "fs"); self.state.setField(-2, "fs");
} }
{ {
const api_funcs = zlua.fnRegsFromType(Api); const input_funcs = zlua.fnRegsFromType(Input);
self.state.newLib(api_funcs); self.state.newLib(input_funcs);
self.state.setField(-2, "api"); self.state.setField(-2, "input");
} }
} }