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

@ -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;
}