w merge by me <3

This commit is contained in:
Harrison DiAmbrosio 2025-11-25 16:02:25 -05:00
commit b3b83fd287
11 changed files with 220 additions and 45 deletions

81
src/lua/hook.zig Normal file
View file

@ -0,0 +1,81 @@
const Hook = @This();
const std = @import("std");
const THook = @import("../types/hook.zig");
const zlua = @import("zlua");
const gpa = std.heap.c_allocator;
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;
};
// 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
// []const u8's
if (L.isTable(1)) {
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;
};
}
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;
};
}
_ = 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;
};
}
server.hooks.append(gpa, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
for (hook.events.items) |value| {
server.events.put(value, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
}
return 0;
}
pub fn del(L: *zlua.Lua) i32 {
// TODO: impl
_ = L;
return 0;
}

View file

@ -1,7 +1,7 @@
const Api = @This();
const std = @import("std");
const Keymap = @import("../keymap.zig");
const Keymap = @import("../types/keymap.zig");
const zlua = @import("zlua");
const xkb = @import("xkbcommon");
@ -14,22 +14,10 @@ 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| {
if (std.mem.eql(u8, m, "shift")) {
modifiers.shift = true;
} else if (std.mem.eql(u8, m, "caps")) {
modifiers.caps = true;
} else if (std.mem.eql(u8, m, "ctrl")) {
modifiers.ctrl = true;
} else if (std.mem.eql(u8, m, "alt")) {
modifiers.alt = true;
} else if (std.mem.eql(u8, m, "mod2")) {
modifiers.mod2 = true;
} else if (std.mem.eql(u8, m, "mod3")) {
modifiers.mod3 = true;
} else if (std.mem.eql(u8, m, "logo")) {
modifiers.logo = true;
} else if (std.mem.eql(u8, m, "mod5")) {
modifiers.mod5 = true;
inline for (std.meta.fields(@TypeOf(modifiers))) |f| {
if (f.type == bool and std.mem.eql(u8, m, f.name)) {
@field(modifiers, f.name) = true;
}
}
}
@ -43,9 +31,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
L.checkType(3, .table);
var keymap: Keymap = undefined;
keymap.options = .{
.repeat = true,
};
keymap.options.repeat = true;
const mod = L.toString(1) catch {
L.raiseErrorStr("Lua error check your config", .{});
@ -62,7 +48,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
_ = L.pushString("press");
_ = L.getTable(3);
if (L.isFunction(-1)) {
keymap.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", .{});
return 0;
};
@ -71,7 +57,7 @@ pub fn add_keymap(L: *zlua.Lua) i32 {
_ = L.pushString("release");
_ = L.getTable(3);
if (L.isFunction(-1)) {
keymap.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", .{});
return 0;
};

View file

@ -9,6 +9,7 @@ const Fs = @import("fs.zig");
const Input = @import("input.zig");
const Api = @import("api.zig");
const View = @import("view.zig");
const Hook = @import("hook.zig");
const gpa = std.heap.c_allocator;
@ -65,6 +66,11 @@ pub fn init(self: *Lua) !void {
self.state.newLib(input_funcs);
self.state.setField(-2, "input");
}
{
const hook_funcs = zlua.fnRegsFromType(Hook);
self.state.newLib(hook_funcs);
self.state.setField(-2, "hook");
}
{
const api_funcs = zlua.fnRegsFromType(Api);
self.state.newLib(api_funcs);