diff --git a/src/lua/Api.zig b/src/lua/Api.zig index 81f560e..f553ca9 100644 --- a/src/lua/Api.zig +++ b/src/lua/Api.zig @@ -3,6 +3,7 @@ const zlua = @import("zlua"); const wlr = @import("wlroots"); const Utils = @import("../Utils.zig"); +const LuaUtils = @import("LuaUtils.zig"); const gpa = std.heap.c_allocator; @@ -36,7 +37,11 @@ pub fn exit(L: *zlua.Lua) i32 { /// ---Change to a different virtual terminal /// ---@param vt_num integer virtual terminal number to switch to pub fn change_vt(L: *zlua.Lua) i32 { - const vt_num: c_uint = @intCast(L.checkInteger(1)); + const vt_num = num: { + const res = LuaUtils.coerceInteger(c_uint, L.checkInteger(1)) catch |err| break :num err; + if (res < 1) break :num error.InvalidInteger; + break :num res; + } catch L.raiseErrorStr("The vt number must be >= 1 and < inf", .{}); if (server.session) |session| { std.log.debug("Changing virtual terminal to {d}", .{vt_num}); diff --git a/src/lua/Output.zig b/src/lua/Output.zig index cdc505b..1c16dbe 100644 --- a/src/lua/Output.zig +++ b/src/lua/Output.zig @@ -2,9 +2,14 @@ const std = @import("std"); const zlua = @import("zlua"); const Output = @import("../Output.zig"); +const LuaUtils = @import("LuaUtils.zig"); const server = &@import("../main.zig").server; +fn output_id_err(L: *zlua.Lua) noreturn { + L.raiseErrorStr("The output id must be >= 0 and < inf", .{}); +} + /// ---@alias output_id integer /// ---Get the ids for all available outputs @@ -44,7 +49,7 @@ pub fn get_focused_id(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return integer? pub fn get_rate(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -60,7 +65,7 @@ pub fn get_rate(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return { width: integer, height: integer }? pub fn get_resolution(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -85,7 +90,7 @@ pub fn get_resolution(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return string? pub fn get_serial(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -106,7 +111,7 @@ pub fn get_serial(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return string? pub fn get_make(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -127,7 +132,7 @@ pub fn get_make(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return stirng? pub fn get_model(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -148,7 +153,7 @@ pub fn get_model(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return stirng? pub fn get_description(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| { @@ -169,7 +174,7 @@ pub fn get_description(L: *zlua.Lua) i32 { /// ---@param output_id output_id 0 maps to focused output /// ---@return stirng pub fn get_name(L: *zlua.Lua) i32 { - const output_id: u64 = @intCast(L.checkInteger(1)); + const output_id = LuaUtils.coerceInteger(u64, L.checkInteger(1)) catch output_id_err(L); const output: ?*Output = if (output_id == 0) server.seat.focused_output else server.root.outputById(output_id); if(output) |o| {