diff --git a/src/lua/api.zig b/src/lua/api.zig index edf63e9..66b0b14 100644 --- a/src/lua/api.zig +++ b/src/lua/api.zig @@ -7,53 +7,33 @@ const gpa = std.heap.c_allocator; const env_map = &@import("../main.zig").env_map; const server = &@import("../main.zig").server; +/// ---Spawn new application via the shell command +/// ---@param cmd string Command to be run by a shell pub fn spawn(L: *zlua.Lua) i32 { - const nargs: i32 = L.getTop(); - - if (nargs < 1) { - L.raiseErrorStr("Expected at least one arguments", .{}); - return 0; - } - - L.checkType(1, .string); - - const cmd = L.toString(1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - }; + const cmd = L.checkString(1); var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa); child.env_map = env_map; child.spawn() catch { - std.log.err("Unable to spawn process \"{s}\"", .{cmd}); + L.raiseErrorStr("Unable to spawn process", .{}); // TODO: Give more descriptive error }; - return 0; + L.pushNil(); + return 1; } +/// ---Exit mezzaluna pub fn exit(L: *zlua.Lua) i32 { - const nargs: i32 = L.getTop(); - - if (nargs != 0) { - L.raiseErrorStr("Expected no arguments", .{}); - } - server.wl_server.terminate(); - return 0; + L.pushNil(); + return 1; } +/// ---Change to a different virtual terminal +/// ---@param vt_num integer virtual terminal number to switch to pub fn change_vt(L: *zlua.Lua) i32 { - const nargs: i32 = L.getTop(); - - if (nargs != 1) { - L.raiseErrorStr("Expected 1 argument, found {d}", .{nargs}); - } - - L.checkType(1, .number); - - const vt_num: c_uint = @intCast(L.toInteger(1) catch { - L.raiseErrorStr("Failed to switch vt", .{}); - }); + const vt_num: c_uint = @intCast(L.checkInteger(1)); if (server.session) |session| { std.log.debug("Changing virtual terminal to {d}", .{vt_num}); @@ -64,5 +44,6 @@ pub fn change_vt(L: *zlua.Lua) i32 { L.raiseErrorStr("Mez has not been initialized yet", .{}); } - return 0; + L.pushNil(); + return 1; } diff --git a/src/lua/hook.zig b/src/lua/hook.zig index 6273f2f..8bd0f37 100644 --- a/src/lua/hook.zig +++ b/src/lua/hook.zig @@ -1,10 +1,10 @@ const Hook = @This(); const std = @import("std"); +const zlua = @import("zlua"); const THook = @import("../types/hook.zig"); - -const zlua = @import("zlua"); +const Utils = @import("../utils.zig"); const gpa = std.heap.c_allocator; const server = &@import("../main.zig").server; @@ -12,14 +12,9 @@ const server = &@import("../main.zig").server; pub fn add(L: *zlua.Lua) i32 { L.checkType(2, .table); - var hook: *THook = gpa.create(THook) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; - hook.events = std.ArrayList([]const u8).initCapacity(gpa, 1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + errdefer Utils.oomPanic(); + var hook: *THook = try gpa.create(THook); + hook.events = try std.ArrayList([]const u8).initCapacity(gpa, 1); // We support both a string and a table of strings as the first value of // add. Regardless of which type is passed in we create an arraylist of @@ -28,47 +23,28 @@ pub fn add(L: *zlua.Lua) i32 { L.pushNil(); while (L.next(1)) { if (L.isString(-1)) { - const s = L.toString(-1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; - hook.events.append(gpa, s) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + const s = L.checkString(-1); + try hook.events.append(gpa, s); } L.pop(1); } } else if (L.isString(1)) { - const s = L.toString(1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; - hook.events.append(gpa, s) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + const s = L.checkString(1); + try hook.events.append(gpa, s); } _ = L.pushString("callback"); _ = L.getTable(2); if (L.isFunction(-1)) { hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; + L.raiseErrorStr("Lua error check your config", .{}); // TODO: Give more descriptive error }; } - server.hooks.append(gpa, hook) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + try server.hooks.append(gpa, hook); for (hook.events.items) |value| { - server.events.put(value, hook) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + try server.events.put(value, hook); } return 0; diff --git a/src/lua/input.zig b/src/lua/input.zig index 8b42796..c43acd2 100644 --- a/src/lua/input.zig +++ b/src/lua/input.zig @@ -1,13 +1,13 @@ const Api = @This(); const std = @import("std"); -const Keymap = @import("../types/keymap.zig"); - const zlua = @import("zlua"); const xkb = @import("xkbcommon"); const wlr = @import("wlroots"); -const gpa = std.heap.c_allocator; +const Keymap = @import("../types/keymap.zig"); +const Utils = @import("../utils.zig"); + const server = &@import("../main.zig").server; fn parse_modkeys(modStr: []const u8) wlr.Keyboard.ModifierMask { @@ -25,32 +25,20 @@ fn parse_modkeys(modStr: []const u8) wlr.Keyboard.ModifierMask { } 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; - }; + const mod = L.checkString(1); keymap.modifier = parse_modkeys(mod); - const key = L.toString(2) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + const key = L.checkString(2); keymap.keycode = xkb.Keysym.fromName(key, .no_flags); _ = L.pushString("press"); _ = L.getTable(3); if (L.isFunction(-1)) { keymap.options.lua_press_ref_idx = L.ref(zlua.registry_index) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; + L.raiseErrorStr("Lua error check your config", .{}); // TODO: Insert more descrptive errors }; } @@ -58,8 +46,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 { _ = L.getTable(3); if (L.isFunction(-1)) { keymap.options.lua_release_ref_idx = L.ref(zlua.registry_index) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; + L.raiseErrorStr("Lua error check your config", .{}); // TODO: Insert more descrptive errors }; } @@ -68,12 +55,10 @@ pub fn add_keymap(L: *zlua.Lua) i32 { 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}); - return 0; - }; + server.keymaps.put(hash, keymap) catch Utils.oomPanic(); - return 0; + L.pushNil(); + return 1; } pub fn del_keymap(L: *zlua.Lua) i32 { @@ -81,18 +66,15 @@ pub fn del_keymap(L: *zlua.Lua) i32 { L.checkType(2, .string); var keymap: Keymap = undefined; - const mod = L.toString(1) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + const mod = L.checkString(1); + keymap.modifier = parse_modkeys(mod); - const key = L.toString(2) catch { - L.raiseErrorStr("Lua error check your config", .{}); - return 0; - }; + const key = L.checkString(2); + keymap.keycode = xkb.Keysym.fromName(key, .no_flags); _ = server.keymaps.remove(Keymap.hash(keymap.modifier, keymap.keycode)); - return 0; + L.pushNil(); + return 1; }