mirror of
https://github.com/MezzalunaWM/Mezzaluna.git
synced 2026-03-08 04:57:32 -04:00
update keymap func
This commit is contained in:
parent
23ef0049f7
commit
cc0862adae
5 changed files with 50 additions and 40 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue