refactored view and output to use the lua aux lib

This commit is contained in:
Harrison DiAmbrosio 2025-11-26 23:48:40 -05:00
parent 9fe54377df
commit 3a0a29b55d
5 changed files with 204 additions and 231 deletions

View file

@ -20,7 +20,8 @@ mez.input.add_keymap("alt", "Return", {
mez.input.add_keymap("alt", "c", { mez.input.add_keymap("alt", "c", {
press = function () press = function ()
mez.api.close() print("closing")
mez.view.close(0)
end end
}) })
@ -32,12 +33,7 @@ mez.input.add_keymap("alt", "q", {
mez.input.add_keymap("alt", "v", { mez.input.add_keymap("alt", "v", {
press = function () press = function ()
local id = mez.output.get_focused_id() print(mez.view.check())
print(mez.output.get_name(id))
print(mez.output.get_serial(id))
print(mez.output.get_model(id))
print(mez.output.get_make(id))
end end
}) })
@ -61,19 +57,20 @@ for i = 1, 12 do
}) })
end end
mez.input.add_keymap("alt", "t", {
press = function() tiler() end
})
local tiler = function () local tiler = function ()
local res = mez.output.get_resolution(mez.output.get_focused_id())
local all = mez.view.get_all_ids() local all = mez.view.get_all_ids()
for i, id in ipairs(all) do for i, id in ipairs(all) do
mez.view.set_position(id, (1920 / #all) * (i - 1), 0) mez.view.set_position(id, (res.width/ #all) * (i - 1), 0)
mez.view.set_size(id, 1920 / #all, 1080) mez.view.set_size(id, res.width / #all, res.height)
end end
end end
mez.input.add_keymap("alt", "t", {
press = function() tiler() end
})
mez.hook.add("ViewMapPre", { mez.hook.add("ViewMapPre", {
callback = function(v) callback = function(v)
tiler() tiler()

View file

@ -19,7 +19,6 @@ pub fn spawn(L: *zlua.Lua) i32 {
const cmd = L.toString(1) catch { const cmd = L.toString(1) catch {
L.raiseErrorStr("Lua error check your config", .{}); L.raiseErrorStr("Lua error check your config", .{});
return 0;
}; };
var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa); var child = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", cmd }, gpa);
@ -31,27 +30,11 @@ pub fn spawn(L: *zlua.Lua) i32 {
return 0; return 0;
} }
pub fn close(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop();
if (nargs != 0) {
L.raiseErrorStr("Expected no arguments", .{});
return 0;
}
if(server.seat.focused_view) |view| {
view.xdg_toplevel.sendClose();
}
return 0;
}
pub fn exit(L: *zlua.Lua) i32 { pub fn exit(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const nargs: i32 = L.getTop();
if (nargs != 0) { if (nargs != 0) {
L.raiseErrorStr("Expected no arguments", .{}); L.raiseErrorStr("Expected no arguments", .{});
return 0;
} }
server.wl_server.terminate(); server.wl_server.terminate();
@ -60,13 +43,21 @@ pub fn exit(L: *zlua.Lua) i32 {
} }
pub fn change_vt(L: *zlua.Lua) i32 { pub fn change_vt(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop();
if (nargs != 1) {
L.raiseErrorStr("Expected 1 argument, found {d}", .{nargs});
}
L.checkType(1, .number); L.checkType(1, .number);
const f = L.toNumber(-1) catch unreachable;
const n: u32 = @intFromFloat(f); const vt_num: c_uint = @intCast(L.toInteger(1) catch {
L.raiseErrorStr("Failed to switch vt", .{});
});
if (server.session) |session| { if (server.session) |session| {
std.log.debug("Changing virtual terminal to {d}", .{ n }); std.log.debug("Changing virtual terminal to {d}", .{vt_num});
wlr.Session.changeVt(session, n) catch { wlr.Session.changeVt(session, vt_num) catch {
L.raiseErrorStr("Failed to switch vt", .{}); L.raiseErrorStr("Failed to switch vt", .{});
}; };
} else { } else {

View file

@ -1,12 +1,14 @@
const std = @import("std"); const std = @import("std");
const zlua = @import("zlua"); const zlua = @import("zlua");
const wlr = @import("wlroots");
const Output = @import("../output.zig"); const Output = @import("../output.zig");
const gpa = std.heap.c_allocator;
const server = &@import("../main.zig").server; const server = &@import("../main.zig").server;
// ---@alias output_id integer
// ---Get the ids for all available outputs
// ---@return output_id[]?
pub fn get_all_ids(L: *zlua.Lua) i32 { pub fn get_all_ids(L: *zlua.Lua) i32 {
var it = server.root.scene.outputs.iterator(.forward); var it = server.root.scene.outputs.iterator(.forward);
var index: usize = 1; var index: usize = 1;
@ -26,175 +28,155 @@ pub fn get_all_ids(L: *zlua.Lua) i32 {
return 1; return 1;
} }
// ---Get the id for the focused output
// ---@return output_id?
pub fn get_focused_id(L: *zlua.Lua) i32 { pub fn get_focused_id(L: *zlua.Lua) i32 {
if(server.seat.focused_output) |output| { if(server.seat.focused_output) |output| {
L.pushInteger(@intCast(output.id)); L.pushInteger(@intCast(output.id));
return 1; return 1;
} }
return 0; L.pushNil();
return 1;
} }
// ---Get refresh rate for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return integer?
pub fn get_rate(L: *zlua.Lua) i32 { pub fn get_rate(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} L.pushInteger(@intCast(o.wlr_output.refresh));
L.checkType(1, .number);
const output_id: u64 = @as(u64, @intCast(L.toInteger(1) catch {
L.raiseErrorStr("Arg is not convertable to an int", .{});
}));
if(server.root.outputById(output_id)) |output| {
L.pushInteger(@intCast(output.wlr_output.refresh));
return 1; return 1;
} }
return 0; L.pushNil();
return 1;
} }
// ---Get resolution in pixels of the output
// ---@param output_id output_id 0 maps to focused output
// ---@return { width: integer, height: integer }?
pub fn get_resolution(L: *zlua.Lua) i32 { pub fn get_resolution(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
}
L.checkType(1, .number);
const output_id: u64 = @as(u64, @intCast(L.toInteger(1) catch {
L.raiseErrorStr("Arg is not convertable to an int", .{});
}));
if(server.root.outputById(output_id)) |output| {
L.newTable(); L.newTable();
_ = L.pushString("width"); _ = L.pushString("width");
L.pushInteger(@intCast(output.wlr_output.width)); L.pushInteger(@intCast(o.wlr_output.width));
L.setTable(-3); L.setTable(-3);
_ = L.pushString("height"); _ = L.pushString("height");
L.pushInteger(@intCast(output.wlr_output.height)); L.pushInteger(@intCast(o.wlr_output.height));
L.setTable(-3); L.setTable(-3);
return 1; return 1;
} }
return 0; L.pushNil();
return 1;
} }
// Return the serial of the output // ---Get the serial for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return string?
pub fn get_serial(L: *zlua.Lua) i32 { pub fn get_serial(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} if(o.wlr_output.serial == null) {
L.pushNil();
L.checkType(1, .number);
errdefer L.raiseErrorStr("Arg is not convertable to an int", .{});
const output_id: u64 = @intCast(try L.toInteger(1));
if(server.root.outputById(output_id)) |output| {
if(output.wlr_output.serial == null) return 0;
_ = L.pushString(std.mem.span(output.wlr_output.serial.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(o.wlr_output.serial.?));
return 1;
}
L.pushNil();
return 1;
} }
// Return the make of the output // ---Get the make for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return string?
pub fn get_make(L: *zlua.Lua) i32 { pub fn get_make(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} if(o.wlr_output.make == null) {
L.pushNil();
L.checkType(1, .number);
errdefer L.raiseErrorStr("Arg is not convertable to an int", .{});
const output_id: u64 = @intCast(try L.toInteger(1));
if(server.root.outputById(output_id)) |output| {
if(output.wlr_output.make == null) return 0;
_ = L.pushString(std.mem.span(output.wlr_output.make.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(o.wlr_output.make.?));
return 1;
}
L.pushNil();
return 1;
} }
// Return the model of the output // ---Get the model for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return stirng?
pub fn get_model(L: *zlua.Lua) i32 { pub fn get_model(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} if(o.wlr_output.model == null) {
L.pushNil();
L.checkType(1, .number);
errdefer L.raiseErrorStr("Arg is not convertable to an int", .{});
const output_id: u64 = @intCast(try L.toInteger(1));
if(server.root.outputById(output_id)) |output| {
if(output.wlr_output.model == null) return 0;
_ = L.pushString(std.mem.span(output.wlr_output.model.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(o.wlr_output.model.?));
return 1;
}
L.pushNil();
return 1;
} }
// Return the description of the output // ---Get the description for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return stirng?
pub fn get_description(L: *zlua.Lua) i32 { pub fn get_description(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} if(o.wlr_output.description == null) {
L.pushNil();
L.checkType(1, .number);
errdefer L.raiseErrorStr("Arg is not convertable to an int", .{});
const output_id: u64 = @intCast(try L.toInteger(1));
if(server.root.outputById(output_id)) |output| {
if(output.wlr_output.description == null) return 0;
_ = L.pushString(std.mem.span(output.wlr_output.description.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(o.wlr_output.description.?));
return 1;
}
L.pushNil();
return 1;
} }
// Return the name of the output // ---Get the description for the output
// ---@param output_id output_id 0 maps to focused output
// ---@return stirng
pub fn get_name(L: *zlua.Lua) i32 { pub fn get_name(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const output_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id);
L.raiseErrorStr("Expected 1 argument, found", .{nargs}); if(output) |o| {
} _ = L.pushString(std.mem.span(o.wlr_output.name));
L.checkType(1, .number);
errdefer L.raiseErrorStr("Arg is not convertable to an int", .{});
const output_id: u64 = @intCast(try L.toInteger(1));
if(server.root.outputById(output_id)) |output| {
_ = L.pushString(std.mem.span(output.wlr_output.name));
return 1; return 1;
} }
return 0; L.pushNil();
return 1;
} }

View file

@ -1,13 +1,14 @@
const std = @import("std"); const std = @import("std");
const zlua = @import("zlua"); const zlua = @import("zlua");
const wlr = @import("wlroots");
const View = @import("../view.zig"); const View = @import("../view.zig");
const gpa = std.heap.c_allocator;
const server = &@import("../main.zig").server; const server = &@import("../main.zig").server;
// ---@alias view_id integer
// ---Get the ids for all available views
// ---@return view_id[]?
pub fn get_all_ids(L: *zlua.Lua) i32 { pub fn get_all_ids(L: *zlua.Lua) i32 {
var it = server.root.scene.tree.children.iterator(.forward); var it = server.root.scene.tree.children.iterator(.forward);
var index: usize = 1; var index: usize = 1;
@ -27,132 +28,126 @@ pub fn get_all_ids(L: *zlua.Lua) i32 {
return 1; return 1;
} }
pub fn check(L: *zlua.Lua) i32 {
L.pushNil();
return 1;
}
// ---Get the id for the focused view
// ---@return view_id?
pub fn get_focused_id(L: *zlua.Lua) i32 { pub fn get_focused_id(L: *zlua.Lua) i32 {
if(server.seat.focused_view) |view| { if(server.seat.focused_view) |view| {
L.pushInteger(@intCast(view.id)); L.pushInteger(@intCast(view.id));
return 1; return 1;
} }
return 0; L.pushNil();
return 1;
} }
// ---Close the view with view_id
// ---@param view_id view_id 0 maps to focused view
pub fn close(L: *zlua.Lua) i32 {
const view_id: u64 = @intCast(L.checkInteger(1));
const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id);
if(view) |v| {
v.close();
}
L.pushNil();
return 1;
}
// ---Position the view by it's top left corner
// ---@param view_id view_id 0 maps to focused view
// ---@param x number x position for view
// ---@param y number y position for view
pub fn set_position(L: *zlua.Lua) i32 { pub fn set_position(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const view_id: u64 = @intCast(L.checkInteger(1));
const x: i32 = @intFromFloat(@round(L.checkNumber(2)));
const y: i32 = @intFromFloat(@round(L.checkNumber(3)));
if (nargs != 3) { const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id);
L.raiseErrorStr("Expected 3 arguments, found {d}", .{nargs}); if(view) |v| {
return 0; v.setPosition(x, y);
} }
for (1..@intCast(nargs + 1)) |i| { L.pushNil();
L.checkType(@intCast(i), .number); return 1;
}
const view_id: u64 = @as(u64, @intCast(L.toInteger(1) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const x: i32 = @as(i32, @intFromFloat(L.toNumber(2) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const y: i32 = @as(i32, @intFromFloat(L.toNumber(3) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const view = server.root.viewById(view_id);
if(view == null) {
L.raiseErrorStr("View with id {d} does not exist", .{view_id});
return 0;
}
view.?.setPosition(x, y);
return 0;
} }
// ---Resize the view by it's top left corner
// ---@param view_id view_id 0 maps to focused view
// ---@param width number width for view
// ---@param height number height for view
pub fn set_size(L: *zlua.Lua) i32 { pub fn set_size(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const view_id: u64 = @intCast(L.checkInteger(1));
const width: i32 = @intFromFloat(@round(L.checkNumber(2)));
const height: i32 = @intFromFloat(@round(L.checkNumber(3)));
if (nargs != 3) { const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id);
L.raiseErrorStr("Expected 3 arguments, found {d}", .{nargs}); if(view) |v| {
return 0; v.setSize(width, height);
} }
for (1..@intCast(nargs + 1)) |i| { L.pushNil();
L.checkType(@intCast(i), .number); return 1;
}
const view_id: u64 = @as(u64, @intCast(L.toInteger(1) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const width: i32 = @as(i32, @intFromFloat(L.toNumber(2) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const height: i32 = @as(i32, @intFromFloat(L.toNumber(3) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const view = server.root.viewById(view_id);
if(view == null) {
L.raiseErrorStr("View with id {d} does not exist", .{view_id});
return 0;
}
view.?.setSize(width, height);
return 0;
} }
// ---Remove focus from current view, and set to given id
// ---@param view_id view_id
pub fn set_focused(L: *zlua.Lua) i32 { pub fn set_focused(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const view_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { if(server.root.viewById(view_id)) |view| {
L.raiseErrorStr("Expected 1 arguments, found {d}", .{nargs}); view.setFocused();
return 0; L.pushNil();
return 1;
} }
L.checkType(1, .number); L.pushNil();
return 1;
const view_id: u64 = @as(u64, @intCast(L.toInteger(1) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
const view = server.root.viewById(view_id);
if(view == null) {
L.raiseErrorStr("View with id {d} does not exist", .{view_id});
return 0;
}
view.?.setFocused();
return 0;
} }
// ---Get the title of the view
// ---@param view_id view_id 0 maps to focused view
// ---@return string?
pub fn get_title(L: *zlua.Lua) i32 { pub fn get_title(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const view_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id);
L.raiseErrorStr("Expected 1 arguments, found {d}", .{nargs}); if(view) |v| {
return 0; if(v.xdg_toplevel.title == null) {
} L.pushNil();
L.checkType(1, .number);
const view_id: u64 = @as(u64, @intCast(L.toInteger(1) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
if(server.root.viewById(view_id)) |view| {
if(view.xdg_toplevel.title == null) return 0;
_ = L.pushString(std.mem.span(view.xdg_toplevel.title.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(v.xdg_toplevel.title.?));
return 1;
}
L.pushNil();
return 1;
} }
// ---Get the app_id of the view
// ---@param view_id view_id 0 maps to focused view
// ---@return string?
pub fn get_app_id(L: *zlua.Lua) i32 { pub fn get_app_id(L: *zlua.Lua) i32 {
const nargs: i32 = L.getTop(); const view_id: u64 = @intCast(L.checkInteger(1));
if(nargs != 1) { const view: ?*View = if (view_id == 0) server.seat.focused_view else server.root.viewById(view_id);
L.raiseErrorStr("Expected 1 arguments, found {d}", .{nargs}); if(view) |v| {
return 0; if(v.xdg_toplevel.app_id == null) {
} L.pushNil();
L.checkType(1, .number);
const view_id: u64 = @as(u64, @intCast(L.toInteger(1) catch { L.raiseErrorStr("Arg is not convertable to an int", .{}); }));
if(server.root.viewById(view_id)) |view| {
if(view.xdg_toplevel.app_id == null) return 0;
_ = L.pushString(std.mem.span(view.xdg_toplevel.app_id.?));
return 1; return 1;
} }
return 0; _ = L.pushString(std.mem.span(v.xdg_toplevel.app_id.?));
return 1;
}
L.pushNil();
return 1;
} }

View file

@ -112,6 +112,14 @@ pub fn setFocused(self: *View) void {
self.focused = true; self.focused = true;
} }
pub fn close(self: *View) void {
if(self.focused) {
server.seat.focused_view = null;
}
self.xdg_toplevel.sendClose();
}
pub fn setPosition(self: *View, x: i32, y: i32) void { pub fn setPosition(self: *View, x: i32, y: i32) void {
self.scene_tree.node.setPosition(x, y); self.scene_tree.node.setPosition(x, y);
} }