diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index 069e955..1839c12 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -35,6 +35,12 @@ mez.input.add_keymap("alt", "q", { end }) +for i = 1, 12 do + mez.input.add_keymap("ctrl|alt", "XF86Switch_VT_"..i, { + press = function() mez.api.chvt(i) end + }) +end + -- mez.input.add_keymap("alt", "a", { -- press = function() -- print("hello from my keymap") diff --git a/src/lua/api.zig b/src/lua/api.zig index 9e0e3f7..e61eea0 100644 --- a/src/lua/api.zig +++ b/src/lua/api.zig @@ -1,5 +1,6 @@ const std = @import("std"); const zlua = @import("zlua"); +const wlr = @import("wlroots"); const gpa = std.heap.c_allocator; @@ -53,7 +54,23 @@ pub fn exit(L: *zlua.Lua) i32 { return 0; } - server.deinit(); + server.wl_server.terminate(); + + return 0; +} + +pub fn chvt(L: *zlua.Lua) i32 { + L.checkType(1, .number); + const f = L.toNumber(-1) catch unreachable; + const n: u32 = @intFromFloat(f); + + if (server.session) |session| { + wlr.Session.changeVt(session, n) catch { + L.raiseErrorStr("Failed to switch vt", .{}); + }; + } else { + L.raiseErrorStr("Mez has not been initialized yet", .{}); + } return 0; } diff --git a/src/lua/input.zig b/src/lua/input.zig index 9ecc72d..e1633e0 100644 --- a/src/lua/input.zig +++ b/src/lua/input.zig @@ -10,32 +10,10 @@ const wlr = @import("wlroots"); const gpa = std.heap.c_allocator; const server = &@import("../main.zig").server; -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, .table); - - var keymap: Keymap = undefined; - keymap.options = .{ - .repeat = true, - }; - - const mod = L.toString(1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; - var it = std.mem.splitScalar(u8, mod, '|'); +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| { - // TODO: can we generate this at comptime? if (std.mem.eql(u8, m, "shift")) { modifiers.shift = true; } else if (std.mem.eql(u8, m, "caps")) { @@ -54,7 +32,26 @@ pub fn add_keymap(L: *zlua.Lua) i32 { modifiers.mod5 = true; } } - keymap.modifier = modifiers; + + return modifiers; +} + +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, .table); + + var keymap: Keymap = undefined; + keymap.options = .{ + .repeat = true, + }; + + const mod = L.toString(1) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + keymap.modifier = parse_modkeys(mod); const key = L.toString(2) catch { L.raiseErrorStr("Lua error check your config", .{}); @@ -93,7 +90,23 @@ pub fn add_keymap(L: *zlua.Lua) i32 { return 0; } -pub fn get_keybind(L: *zlua.Lua) i32 { - _ = L; +pub fn del_keymap(L: *zlua.Lua) i32 { + L.checkType(1, .string); + L.checkType(2, .string); + + var keymap: Keymap = undefined; + const mod = L.toString(1) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + keymap.modifier = parse_modkeys(mod); + + const key = L.toString(2) catch { + L.raiseErrorStr("Lua error check your config", .{}); + return 0; + }; + keymap.keycode = xkb.Keysym.fromName(key, .no_flags); + _ = server.keymaps.remove(Keymap.hash(keymap.modifier, keymap.keycode)); + return 0; }