diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index cc44903..75dbb1d 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -1,10 +1,10 @@ local env_conf = os.getenv("XDG_CONFIG_HOME") if not env_conf then - env_conf = os.getenv("HOME") - if not env_conf then - error("Couldn't determine potential config directory is $HOME set?") - end - env_conf = mez.fs.joinpath(env_conf, ".config") + env_conf = os.getenv("HOME") + if not env_conf then + error("Couldn't determine potential config directory is $HOME set?") + end + env_conf = mez.fs.joinpath(env_conf, ".config") end mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") @@ -12,26 +12,34 @@ package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua -- this is an example mez.input.add_keymap("alt", "a", { - press = function() - print("hello from my keymap") - end + press = function() + print("hello from my keymap") + end }) mez.input.add_keymap("alt", "Return", { - press = function() - mez.api.spawn("foot") - end, + press = function() + mez.api.spawn("foot") + end, }) mez.input.add_keymap("alt", "c", { - press = function () - mez.api.close() - end + press = function () + mez.api.close() + end }) mez.input.add_keymap("alt", "q", { + press = function () + mez.api.exit(); + end +}) + +mez.input.add_keymap("alt", "v", { press = function () - mez.api.exit(); + local view = mez.view.get_focused_id() + mez.view.set_position(view, 100, 100) + mez.view.set_size(view, 100, 100) end }) @@ -59,7 +67,7 @@ end -- }) mez.hook.add("ViewMapPre", { - callback = function() - print("hello world") + callback = function(v) + mez.view.set_size(v, 1000, 1000) end }) diff --git a/src/lua/lua.zig b/src/lua/lua.zig index 774447f..39dea93 100644 --- a/src/lua/lua.zig +++ b/src/lua/lua.zig @@ -10,6 +10,7 @@ const Input = @import("input.zig"); const Api = @import("api.zig"); const View = @import("view.zig"); const Hook = @import("hook.zig"); +const View = @import("view.zig"); const gpa = std.heap.c_allocator; diff --git a/src/types/events.zig b/src/types/events.zig index 8f6a111..59f37e4 100644 --- a/src/types/events.zig +++ b/src/types/events.zig @@ -41,12 +41,12 @@ pub fn put(self: *Events, key: []const u8, hook: *const Hook) !void { // TODO: figure out deletion // pub fn del(self: *Events, key: ???) !void {} -pub fn exec(self: *Events, event: []const u8) void { +pub fn exec(self: *Events, event: []const u8, args: anytype) void { if (self.events.get(event)) |e| { var node = e.first; while (node) |n| : (node = n.next) { const data: *Node = @fieldParentPtr("node", n); - data.hook.callback(); + data.hook.callback(args); // FIXME: not sure why but for some reason our ll doesn't seem to want to // admit that there's nothing after the first node. diff --git a/src/types/hook.zig b/src/types/hook.zig index ae207df..7b2693f 100644 --- a/src/types/hook.zig +++ b/src/types/hook.zig @@ -17,7 +17,13 @@ options: struct { lua_cb_ref_idx: i32, }, -pub fn callback(self: *const Hook) void { +pub fn callback(self: *const Hook, args: anytype) void { + const ArgsType = @TypeOf(args); + const args_type_info = @typeInfo(ArgsType); + if (args_type_info != .@"struct") { + @compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType)); + } + const t = Lua.state.rawGetIndex(zlua.registry_index, self.options.lua_cb_ref_idx); if (t != zlua.LuaType.function) { std.log.err("Failed to call hook, it doesn't have a callback.", .{}); @@ -25,8 +31,14 @@ pub fn callback(self: *const Hook) void { return; } - // TODO: we need to send some data along with the callback, this data will - // change based on the event which the user is hooking into - Lua.state.call(.{ .args = 0, .results = 0 }); + // allow passing any arguments to the lua hook + var i: u8 = 0; + inline for (args, 1..) |field, k| { + try Lua.state.pushAny(field); + i = k; + } + + Lua.state.protectedCall(.{ .args = i }) catch { + }; Lua.state.pop(-1); } diff --git a/src/view.zig b/src/view.zig index b1acd67..a5fdbc3 100644 --- a/src/view.zig +++ b/src/view.zig @@ -131,7 +131,7 @@ fn handleMap(listener: *wl.Listener(void)) void { const view: *View = @fieldParentPtr("map", listener); std.log.debug("Mapping view '{s}'", .{view.xdg_toplevel.title orelse "(unnamed)"}); - server.events.exec("ViewMapPre"); + server.events.exec("ViewMapPre", .{view.id}); view.xdg_toplevel.events.request_fullscreen.add(&view.request_fullscreen); view.xdg_toplevel.events.request_move.add(&view.request_move); @@ -155,7 +155,7 @@ fn handleMap(listener: *wl.Listener(void)) void { view.mapped = true; - server.events.exec("ViewMapPost"); + server.events.exec("ViewMapPost", .{view.id}); } fn handleUnmap(listener: *wl.Listener(void)) void {