diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index eb67e6f..cc44903 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -57,3 +57,9 @@ end -- print("goodbye from my keymap") -- end -- }) + +mez.hook.add("ViewMapPre", { + callback = function() + print("hello world") + end +}) diff --git a/src/cursor.zig b/src/cursor.zig index 5f52d06..44c8bc9 100644 --- a/src/cursor.zig +++ b/src/cursor.zig @@ -89,8 +89,11 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void { if(focused_view) |view| { view.scene_tree.node.setPosition( - std.math.clamp(@as(c_int, @intFromFloat(self.wlr_cursor.x)) - self.drag_view_offset_x, 0, std.math.maxInt(u32)), - std.math.clamp(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - self.drag_view_offset_y, 0, std.math.maxInt(u32)) + // TODO: add a lua option to configure the behavior of this, by + // default it will be the following: + @as(c_int, @intFromFloat(self.wlr_cursor.x)) - self.drag_view_offset_x, + @as(c_int, @intFromFloat(self.wlr_cursor.y)) - self.drag_view_offset_y + // and the user should be able to configure if it clamps or not ); } }, @@ -100,8 +103,9 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void { if(focused_view) |view| { _ = view.xdg_toplevel.setSize( - @intCast(@as(c_int, @intFromFloat(self.wlr_cursor.x)) - view.scene_tree.node.x), - @intCast(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - view.scene_tree.node.y) + // TODO: configure the min and max using lua? + std.math.clamp(@as(c_int, @as(i32, @intFromFloat(self.wlr_cursor.x)) - view.scene_tree.node.x), 10, std.math.maxInt(i32)), + std.math.clamp(@as(c_int, @as(i32, @intFromFloat(self.wlr_cursor.y)) - view.scene_tree.node.y), 10, std.math.maxInt(i32)) ); } }, diff --git a/src/keyboard.zig b/src/keyboard.zig index a360ce0..260ef75 100644 --- a/src/keyboard.zig +++ b/src/keyboard.zig @@ -6,7 +6,7 @@ const Keyboard = @This(); const std = @import("std"); const gpa = std.heap.c_allocator; const server = &@import("main.zig").server; -const Keymap = @import("keymap.zig"); +const Keymap = @import("types/keymap.zig"); const Utils = @import("utils.zig"); const wl = @import("wayland").server.wl; @@ -80,10 +80,10 @@ fn handleKey(_: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.even if (server.seat.keyboard_group.keyboard.xkb_state) |xkb_state| { for (xkb_state.keyGetSyms(keycode)) |sym| { if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| { - if (event.state == .pressed and map.lua_press_ref_idx > 0) { + if (event.state == .pressed and map.options.lua_press_ref_idx > 0) { map.callback(false); handled = true; - } else if (event.state == .released and map.lua_release_ref_idx > 0) { + } else if (event.state == .released and map.options.lua_release_ref_idx > 0) { map.callback(true); handled = true; } diff --git a/src/lua/hook.zig b/src/lua/hook.zig new file mode 100644 index 0000000..6273f2f --- /dev/null +++ b/src/lua/hook.zig @@ -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; +} diff --git a/src/lua/input.zig b/src/lua/input.zig index e1633e0..8b42796 100644 --- a/src/lua/input.zig +++ b/src/lua/input.zig @@ -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; }; diff --git a/src/lua/lua.zig b/src/lua/lua.zig index a764f5f..774447f 100644 --- a/src/lua/lua.zig +++ b/src/lua/lua.zig @@ -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); diff --git a/src/server.zig b/src/server.zig index 6875df5..d42d8b9 100644 --- a/src/server.zig +++ b/src/server.zig @@ -11,7 +11,9 @@ const Keyboard = @import("keyboard.zig"); const Output = @import("output.zig"); const View = @import("view.zig"); const Utils = @import("utils.zig"); -const Keymap = @import("keymap.zig"); +const Keymap = @import("types/keymap.zig"); +const Hook = @import("types/hook.zig"); +const Events = @import("types/events.zig"); const gpa = std.heap.c_allocator; const server = &@import("main.zig").server; @@ -34,7 +36,11 @@ allocator: *wlr.Allocator, root: Root, seat: Seat, cursor: Cursor, + +// lua data keymaps: std.AutoHashMap(u64, Keymap), +hooks: std.ArrayList(*Hook), +events: Events, // Backend listeners new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput), @@ -90,6 +96,8 @@ pub fn init(self: *Server) void { .seat = undefined, .cursor = undefined, .keymaps = .init(gpa), + .hooks = try .initCapacity(gpa, 10), // TODO: choose how many slots to start with + .events = try .init(gpa), }; self.renderer.initServer(wl_server) catch { diff --git a/src/types/events.zig b/src/types/events.zig new file mode 100644 index 0000000..8f6a111 --- /dev/null +++ b/src/types/events.zig @@ -0,0 +1,56 @@ +pub const Events = @This(); + +const std = @import("std"); + +const Hook = @import("hook.zig"); + +const Node = struct { + hook: *const Hook, + node: std.SinglyLinkedList.Node, +}; + +events: std.StringHashMap(*std.SinglyLinkedList), +allocator: std.mem.Allocator, + +pub fn init(allocator: std.mem.Allocator) !Events { + return Events{ + .allocator = allocator, + .events = .init(allocator), + }; +} + +pub fn put(self: *Events, key: []const u8, hook: *const Hook) !void { + var ll: *std.SinglyLinkedList = undefined; + if (self.events.get(key)) |sll| { + ll = sll; + } else { + ll = try self.allocator.create(std.SinglyLinkedList); + try self.events.put(key, ll); + if (self.events.get(key)) |sll| { + ll = sll; + } + } + const data = try self.allocator.create(Node); + data.* = .{ + .hook = hook, + .node = .{}, + }; + ll.prepend(&data.node); +} + +// TODO: figure out deletion +// pub fn del(self: *Events, key: ???) !void {} + +pub fn exec(self: *Events, event: []const u8) 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(); + + // 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. + break; + } + } +} diff --git a/src/types/hook.zig b/src/types/hook.zig new file mode 100644 index 0000000..ae207df --- /dev/null +++ b/src/types/hook.zig @@ -0,0 +1,32 @@ +//! This is a simple way to define a hook. +const Hook = @This(); + +const std = @import("std"); + +const xkb = @import("xkbcommon"); +const wlr = @import("wlroots"); +const zlua = @import("zlua"); + +const Event = @import("events.zig"); +const Lua = &@import("../main.zig").lua; + +events: std.ArrayList([]const u8), // a list of events +options: struct { + // group: []const u8, // TODO: do we need groups? + /// This is the location of the callback lua function in the lua registry + lua_cb_ref_idx: i32, +}, + +pub fn callback(self: *const Hook) void { + 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.", .{}); + Lua.state.pop(1); + 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 }); + Lua.state.pop(-1); +} diff --git a/src/keymap.zig b/src/types/keymap.zig similarity index 73% rename from src/keymap.zig rename to src/types/keymap.zig index 93a74c8..1b43e27 100644 --- a/src/keymap.zig +++ b/src/types/keymap.zig @@ -8,20 +8,20 @@ const xkb = @import("xkbcommon"); const wlr = @import("wlroots"); const zlua = @import("zlua"); -const Lua = &@import("main.zig").lua; +const Lua = &@import("../main.zig").lua; modifier: wlr.Keyboard.ModifierMask, keycode: xkb.Keysym, -/// This is the location of the on press lua function in the lua registry -lua_press_ref_idx: i32, -/// This is the location of the on release lua function in the lua registry -lua_release_ref_idx: i32, options: struct { repeat: bool, + /// This is the location of the on press lua function in the lua registry + lua_press_ref_idx: i32, + /// This is the location of the on release lua function in the lua registry + lua_release_ref_idx: i32, }, pub fn callback(self: *const Keymap, release: bool) void { - const lua_ref_idx = if(release) self.lua_release_ref_idx else self.lua_press_ref_idx; + const lua_ref_idx = if (release) self.options.lua_release_ref_idx else self.options.lua_press_ref_idx; const t = Lua.state.rawGetIndex(zlua.registry_index, lua_ref_idx); if (t != zlua.LuaType.function) { diff --git a/src/view.zig b/src/view.zig index ac31715..4bba96c 100644 --- a/src/view.zig +++ b/src/view.zig @@ -113,6 +113,8 @@ 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"); + view.xdg_toplevel.events.request_fullscreen.add(&view.request_fullscreen); view.xdg_toplevel.events.request_move.add(&view.request_move); view.xdg_toplevel.events.request_resize.add(&view.request_resize); @@ -134,6 +136,8 @@ fn handleMap(listener: *wl.Listener(void)) void { // Here is where we should tile and set size view.mapped = true; + + server.events.exec("ViewMapPost"); } fn handleUnmap(listener: *wl.Listener(void)) void { @@ -251,20 +255,12 @@ fn handleRequestMinimize( std.log.err("Unimplemented request minimize", .{}); } -fn handleRequestMaximize( - listener: *wl.Listener(void) -) void { - const view: *View = @fieldParentPtr("request_fullscreen", listener); - _ = view; - std.log.err("Unimplemented request maximize", .{}); -} - fn handleSetAppId( listener: *wl.Listener(void) ) void { const view: *View = @fieldParentPtr("set_app_id", listener); _ = view; - std.log.err("Unimplemented request maximize", .{}); + std.log.err("Unimplemented set appid", .{}); } fn handleSetTitle(