started to standardize and comment other apis

This commit is contained in:
Harrison DiAmbrosio 2025-11-27 00:10:25 -05:00
parent 3a0a29b55d
commit a764be6af6
3 changed files with 42 additions and 103 deletions

View file

@ -7,53 +7,33 @@ const gpa = std.heap.c_allocator;
const env_map = &@import("../main.zig").env_map; const env_map = &@import("../main.zig").env_map;
const server = &@import("../main.zig").server; 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 { pub fn spawn(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const cmd = L.checkString(1);
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", .{});
};
var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa); var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa);
child.env_map = env_map; child.env_map = env_map;
child.spawn() catch { 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 { pub fn exit(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop();
if (nargs != 0) {
L.raiseErrorStr("Expected no arguments", .{});
}
server.wl_server.terminate(); 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 { pub fn change_vt(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const vt_num: c_uint = @intCast(L.checkInteger(1));
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", .{});
});
if (server.session) |session| { if (server.session) |session| {
std.log.debug("Changing virtual terminal to {d}", .{vt_num}); 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", .{}); L.raiseErrorStr("Mez has not been initialized yet", .{});
} }
return 0; L.pushNil();
return 1;
} }

View file

@ -1,10 +1,10 @@
const Hook = @This(); const Hook = @This();
const std = @import("std"); const std = @import("std");
const zlua = @import("zlua");
const THook = @import("../types/hook.zig"); const THook = @import("../types/hook.zig");
const Utils = @import("../utils.zig");
const zlua = @import("zlua");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
const server = &@import("../main.zig").server; const server = &@import("../main.zig").server;
@ -12,14 +12,9 @@ const server = &@import("../main.zig").server;
pub fn add(L: *zlua.Lua) i32 { pub fn add(L: *zlua.Lua) i32 {
L.checkType(2, .table); L.checkType(2, .table);
var hook: *THook = gpa.create(THook) catch { errdefer Utils.oomPanic();
L.raiseErrorStr("Lua error check your config", .{}); var hook: *THook = try gpa.create(THook);
return 0; hook.events = try std.ArrayList([]const u8).initCapacity(gpa, 1);
};
hook.events = std.ArrayList([]const u8).initCapacity(gpa, 1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
// We support both a string and a table of strings as the first value of // 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 // 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(); L.pushNil();
while (L.next(1)) { while (L.next(1)) {
if (L.isString(-1)) { if (L.isString(-1)) {
const s = L.toString(-1) catch { const s = L.checkString(-1);
L.raiseErrorStr("Lua error check your config", .{}); try hook.events.append(gpa, s);
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
} }
L.pop(1); L.pop(1);
} }
} else if (L.isString(1)) { } else if (L.isString(1)) {
const s = L.toString(1) catch { const s = L.checkString(1);
L.raiseErrorStr("Lua error check your config", .{}); try hook.events.append(gpa, s);
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
} }
_ = L.pushString("callback"); _ = L.pushString("callback");
_ = L.getTable(2); _ = L.getTable(2);
if (L.isFunction(-1)) { if (L.isFunction(-1)) {
hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch { hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch {
L.raiseErrorStr("Lua error check your config", .{}); L.raiseErrorStr("Lua error check your config", .{}); // TODO: Give more descriptive error
return 0;
}; };
} }
server.hooks.append(gpa, hook) catch { try server.hooks.append(gpa, hook);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
for (hook.events.items) |value| { for (hook.events.items) |value| {
server.events.put(value, hook) catch { try server.events.put(value, hook);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
} }
return 0; return 0;

View file

@ -1,13 +1,13 @@
const Api = @This(); const Api = @This();
const std = @import("std"); const std = @import("std");
const Keymap = @import("../types/keymap.zig");
const zlua = @import("zlua"); const zlua = @import("zlua");
const xkb = @import("xkbcommon"); const xkb = @import("xkbcommon");
const wlr = @import("wlroots"); 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; const server = &@import("../main.zig").server;
fn parse_modkeys(modStr: []const u8) wlr.Keyboard.ModifierMask { 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 { 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; var keymap: Keymap = undefined;
keymap.options.repeat = true; keymap.options.repeat = true;
const mod = L.toString(1) catch { const mod = L.checkString(1);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
keymap.modifier = parse_modkeys(mod); keymap.modifier = parse_modkeys(mod);
const key = L.toString(2) catch { const key = L.checkString(2);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
keymap.keycode = xkb.Keysym.fromName(key, .no_flags); keymap.keycode = xkb.Keysym.fromName(key, .no_flags);
_ = L.pushString("press"); _ = L.pushString("press");
_ = L.getTable(3); _ = L.getTable(3);
if (L.isFunction(-1)) { if (L.isFunction(-1)) {
keymap.options.lua_press_ref_idx = L.ref(zlua.registry_index) catch { keymap.options.lua_press_ref_idx = L.ref(zlua.registry_index) catch {
L.raiseErrorStr("Lua error check your config", .{}); L.raiseErrorStr("Lua error check your config", .{}); // TODO: Insert more descrptive errors
return 0;
}; };
} }
@ -58,8 +46,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
_ = L.getTable(3); _ = L.getTable(3);
if (L.isFunction(-1)) { if (L.isFunction(-1)) {
keymap.options.lua_release_ref_idx = L.ref(zlua.registry_index) catch { keymap.options.lua_release_ref_idx = L.ref(zlua.registry_index) catch {
L.raiseErrorStr("Lua error check your config", .{}); L.raiseErrorStr("Lua error check your config", .{}); // TODO: Insert more descrptive errors
return 0;
}; };
} }
@ -68,12 +55,10 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
keymap.options.repeat = L.isNil(-1) or L.toBoolean(-1); 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 Utils.oomPanic();
std.log.err("Failed to add keymap to keymaps: {}", .{err});
return 0;
};
return 0; L.pushNil();
return 1;
} }
pub fn del_keymap(L: *zlua.Lua) i32 { pub fn del_keymap(L: *zlua.Lua) i32 {
@ -81,18 +66,15 @@ pub fn del_keymap(L: *zlua.Lua) i32 {
L.checkType(2, .string); L.checkType(2, .string);
var keymap: Keymap = undefined; var keymap: Keymap = undefined;
const mod = L.toString(1) catch { const mod = L.checkString(1);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
keymap.modifier = parse_modkeys(mod); keymap.modifier = parse_modkeys(mod);
const key = L.toString(2) catch { const key = L.checkString(2);
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
keymap.keycode = xkb.Keysym.fromName(key, .no_flags); keymap.keycode = xkb.Keysym.fromName(key, .no_flags);
_ = server.keymaps.remove(Keymap.hash(keymap.modifier, keymap.keycode)); _ = server.keymaps.remove(Keymap.hash(keymap.modifier, keymap.keycode));
return 0; L.pushNil();
return 1;
} }