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

View file

@ -57,3 +57,9 @@ end
-- print("goodbye from my keymap") -- print("goodbye from my keymap")
-- end -- end
-- }) -- })
mez.hook.add("ViewMapPre", {
callback = function()
print("hello world")
end
})

View file

@ -89,8 +89,11 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void {
if(focused_view) |view| { if(focused_view) |view| {
view.scene_tree.node.setPosition( 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)), // TODO: add a lua option to configure the behavior of this, by
std.math.clamp(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - self.drag_view_offset_y, 0, std.math.maxInt(u32)) // 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| { if(focused_view) |view| {
_ = view.xdg_toplevel.setSize( _ = view.xdg_toplevel.setSize(
@intCast(@as(c_int, @intFromFloat(self.wlr_cursor.x)) - view.scene_tree.node.x), // TODO: configure the min and max using lua?
@intCast(@as(c_int, @intFromFloat(self.wlr_cursor.y)) - view.scene_tree.node.y) 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))
); );
} }
}, },

View file

@ -6,7 +6,7 @@ const Keyboard = @This();
const std = @import("std"); const std = @import("std");
const gpa = std.heap.c_allocator; const gpa = std.heap.c_allocator;
const server = &@import("main.zig").server; const server = &@import("main.zig").server;
const Keymap = @import("keymap.zig"); const Keymap = @import("types/keymap.zig");
const Utils = @import("utils.zig"); const Utils = @import("utils.zig");
const wl = @import("wayland").server.wl; 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| { if (server.seat.keyboard_group.keyboard.xkb_state) |xkb_state| {
for (xkb_state.keyGetSyms(keycode)) |sym| { for (xkb_state.keyGetSyms(keycode)) |sym| {
if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| { 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); map.callback(false);
handled = true; 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); map.callback(true);
handled = true; handled = true;
} }

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

View file

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

View file

@ -11,7 +11,9 @@ const Keyboard = @import("keyboard.zig");
const Output = @import("output.zig"); const Output = @import("output.zig");
const View = @import("view.zig"); const View = @import("view.zig");
const Utils = @import("utils.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 gpa = std.heap.c_allocator;
const server = &@import("main.zig").server; const server = &@import("main.zig").server;
@ -34,7 +36,11 @@ allocator: *wlr.Allocator,
root: Root, root: Root,
seat: Seat, seat: Seat,
cursor: Cursor, cursor: Cursor,
// lua data
keymaps: std.AutoHashMap(u64, Keymap), keymaps: std.AutoHashMap(u64, Keymap),
hooks: std.ArrayList(*Hook),
events: Events,
// Backend listeners // Backend listeners
new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput), new_input: wl.Listener(*wlr.InputDevice) = .init(handleNewInput),
@ -90,6 +96,8 @@ pub fn init(self: *Server) void {
.seat = undefined, .seat = undefined,
.cursor = undefined, .cursor = undefined,
.keymaps = .init(gpa), .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 { self.renderer.initServer(wl_server) catch {

56
src/types/events.zig Normal file
View file

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

32
src/types/hook.zig Normal file
View file

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

View file

@ -8,20 +8,20 @@ const xkb = @import("xkbcommon");
const wlr = @import("wlroots"); const wlr = @import("wlroots");
const zlua = @import("zlua"); const zlua = @import("zlua");
const Lua = &@import("main.zig").lua; const Lua = &@import("../main.zig").lua;
modifier: wlr.Keyboard.ModifierMask, modifier: wlr.Keyboard.ModifierMask,
keycode: xkb.Keysym, 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 { options: struct {
repeat: bool, 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 { 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); const t = Lua.state.rawGetIndex(zlua.registry_index, lua_ref_idx);
if (t != zlua.LuaType.function) { if (t != zlua.LuaType.function) {

View file

@ -113,6 +113,8 @@ fn handleMap(listener: *wl.Listener(void)) void {
const view: *View = @fieldParentPtr("map", listener); const view: *View = @fieldParentPtr("map", listener);
std.log.debug("Mapping view '{s}'", .{view.xdg_toplevel.title orelse "(unnamed)"}); 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_fullscreen.add(&view.request_fullscreen);
view.xdg_toplevel.events.request_move.add(&view.request_move); view.xdg_toplevel.events.request_move.add(&view.request_move);
view.xdg_toplevel.events.request_resize.add(&view.request_resize); 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 // Here is where we should tile and set size
view.mapped = true; view.mapped = true;
server.events.exec("ViewMapPost");
} }
fn handleUnmap(listener: *wl.Listener(void)) void { fn handleUnmap(listener: *wl.Listener(void)) void {
@ -251,20 +255,12 @@ fn handleRequestMinimize(
std.log.err("Unimplemented request minimize", .{}); 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( fn handleSetAppId(
listener: *wl.Listener(void) listener: *wl.Listener(void)
) void { ) void {
const view: *View = @fieldParentPtr("set_app_id", listener); const view: *View = @fieldParentPtr("set_app_id", listener);
_ = view; _ = view;
std.log.err("Unimplemented request maximize", .{}); std.log.err("Unimplemented set appid", .{});
} }
fn handleSetTitle( fn handleSetTitle(