From e6478702385e568d73f7dfcecab9890b50ac078b Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sat, 20 Jun 2026 13:45:17 +0200 Subject: [PATCH] big lua api changes --- .luarc.json | 13 ++ .vscode/settings.json | 12 -- plugin.lua => context.lua | 175 +++++++--------- scripts/0-group_manager.lua | 2 +- scripts/1-oda.lua | 2 +- scripts/1-rft.lua | 4 +- scripts/2-af.lua | 2 +- scripts/2-ert.lua | 4 +- scripts/2-rtp.lua | 18 +- scripts/98-uecp.lua | 58 +++--- scripts/99-data.lua | 134 ++++++------ src/lua_api.c | 400 +++++++++++++++++++----------------- src/lua_api.h | 109 ++++------ src/lua_rds.c | 343 ++++++++++++++++--------------- 14 files changed, 625 insertions(+), 651 deletions(-) create mode 100644 .luarc.json rename plugin.lua => context.lua (77%) diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..e6ad0ef --- /dev/null +++ b/.luarc.json @@ -0,0 +1,13 @@ +{ + "runtime.builtin": { + "debug": "disable", + "os": "disable", + "jit": "disable", + "ffi":"disable", + "package": "disable" + }, + "workspace.library": ["context.lua"], + "diagnostics.disable": [ + "duplicate-set-field" + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e68941f..75d145a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,17 +22,5 @@ "ranges": "c", "span": "c" }, - "Lua.runtime.plugin": "plugin.lua", - "Lua.runtime.builtin": { - "debug": "disable", - "os": "disable", - "jit": "disable", - "ffi": "disable", - "package": "disable" - }, "C_Cpp.dimInactiveRegions": false, - "Lua.diagnostics.disable": [ - "duplicate-set-field", - "lowercase-global" - ] } \ No newline at end of file diff --git a/plugin.lua b/context.lua similarity index 77% rename from plugin.lua rename to context.lua index 4f7979d..c52e9bb 100644 --- a/plugin.lua +++ b/context.lua @@ -1,46 +1,47 @@ ---@meta -dp = {} +---@class Data +Data = {} ---@type string -dp.core_version = "" +Data.core_version = "" ---@type integer -dp.max_programs = 0 +Data.max_programs = 0 ---Executes a CRC-16 CCIIT on given data ---@param data string ---@return integer -function dp.crc16(data) end +function Data.crc16(data) end ---Starts the initialization sequence, also calls the on_init function ---@return nil -function dp.set_program_defaults() end +function Data.set_program_defaults() end ---Saves, loads and resets the state of the data, you might as well restart the whole program. this does NOT cause data loss other than the current internal state ---@return nil -function dp.reset_rds() end +function Data.reset_rds() end ---Forces encoder and modulator data to be saved to disc ---@return nil -function dp.force_save() end +function Data.force_save() end ---Set the program which is actually being modulated. Setting this does not affect the writing program ---@param program_idx integer 0 to (max_programs - 1) -function dp.set_output_program(program_idx) end +function Data.set_output_program(program_idx) end ---@return integer -function dp.get_output_program() end +function Data.get_output_program() end ---Sets the program all of the set/get functions affect ---@param program_idx integer 0 to (max_programs - 1) -function dp.set_writing_program(program_idx) end +function Data.set_writing_program(program_idx) end ---@return integer -function dp.get_writing_program() end +function Data.get_writing_program() end ---Encodes the given UTF-8 string into the unnamed RDS character set ---@param data string ---@return string -function dp.encode_charset(data) end +function Data.encode_charset(data) end hooks = {} @@ -86,8 +87,6 @@ hooks.rt_transmission = {} ---@type function[] hooks.ps_transmission = {} --- Every function with with the s suffixed table means that other versions of that function can be in that list-table, and each one will be called - ---This function is called in order to handle UDP data. The string returned is sent back to the UDP peer as a response ---It should be defined by the user in the script. ---@param data string @@ -117,7 +116,8 @@ function hooks.group(group) end ---@return integer d function hooks.rds2_group(stream) end -rds = {} +---@class RDS +RDS = {} ---Returns an encoded group by the core - groups that could not be encoded decay to PS ---@param group string One character, just one @@ -125,102 +125,73 @@ rds = {} ---@return integer b ---@return integer c ---@return integer d -function rds.encode_group(group) end +function RDS.encode_group(group) end ---@type integer -rds.eon_count = 0 +RDS.eon_count = 0 ----@param pi integer -function rds.set_pi(pi) end ----@return integer -function rds.get_pi() end - ----@param pty integer -function rds.set_pty(pty) end ----@return integer -function rds.get_pty() end - ----@param ecc integer -function rds.set_ecc(ecc) end ----@return integer -function rds.get_ecc() end - ----@param slc_data integer -function rds.set_slc_data(slc_data) end ----@return integer -function rds.get_slc_data() end - ----@param ct boolean -function rds.set_ct(ct) end ----@return boolean -function rds.get_ct() end - ----@param dpty boolean -function rds.set_dpty(dpty) end ----@return boolean -function rds.get_dpty() end - ----@param tp boolean -function rds.set_tp(tp) end ----@return boolean -function rds.get_tp() end - ----@param ta boolean -function rds.set_ta(ta) end ----@return boolean -function rds.get_ta() end +---@type integer +RDS.pi = nil +---@type integer +RDS.ecc = nil +---@type integer +RDS.pty = nil +---@type integer +RDS.slc_data = nil +---@type boolean +RDS.ct = nil +---@type boolean +RDS.dpty = nil +---@type boolean +RDS.tp = nil +---@type boolean +RDS.ta = nil +---@type boolean +RDS.rt_enabled = nil +---@type boolean +RDS.ptyn_enabled = nil -- Feature Flags ----@param enabled boolean -function rds.set_rt_enabled(enabled) end ----@return boolean -function rds.get_rt_enabled() end - ----@param enabled boolean -function rds.set_ptyn_enabled(enabled) end ----@return boolean -function rds.get_ptyn_enabled() end - -- Modulation & Generation ---@param streams integer -function rds.set_streams(streams) end +function RDS.set_streams(streams) end ---@return integer -function rds.get_streams() end +function RDS.get_streams() end -- Program & Linking ---@param linkage boolean -function rds.set_link(linkage) end +function RDS.set_link(linkage) end ---@return boolean -function rds.get_link() end +function RDS.get_link() end -- String Setters ---@param ptyn string Program Type Name (max 8 chars) -function rds.set_ptyn(ptyn) end +function RDS.set_ptyn(ptyn) end ---@param ptyn string Program Type Name (max 8 chars) which is expected to be already encoded in the RDS charset -function rds.set_ptyn_raw(ptyn) end +function RDS.set_ptyn_raw(ptyn) end ---@param ps string Program Service (8 chars) -function rds.set_ps(ps) end +function RDS.set_ps(ps) end ---@param ps string Program Service (8 chars) which is expected to be already encoded in the RDS charset -function rds.set_ps_raw(ps) end +function RDS.set_ps_raw(ps) end ---@param tps string Traffic PS -function rds.set_tps(tps) end +function RDS.set_tps(tps) end ---@param tps string Traffic PS which is expected to be already encoded in the RDS charset -function rds.set_tps_raw(tps) end +function RDS.set_tps_raw(tps) end ---@param rt string Radio Text (max 64 chars) -function rds.set_rt(rt) end +function RDS.set_rt(rt) end ---@param rt string Radio Text (max 64 chars) which is expected to be already encoded in the RDS charset -function rds.set_rt_raw(rt) end +function RDS.set_rt_raw(rt) end ---@return nil -function rds.toggle_rt_ab() end +function RDS.toggle_rt_ab() end ---@param lps string -function rds.set_lps(lps) end +function RDS.set_lps(lps) end ---@return string -function rds.get_lps() end +function RDS.get_lps() end ---The format is a binary format: here are the groups recognized by the core ---0x0 - PS @@ -231,29 +202,29 @@ function rds.get_lps() end ---0x1E - LPS ---0x1F - Fast tuning ---Rest are handled by the hooks.group function ----The designation sent to hooks.group is 100% binary safe as a byte +---The designation sent to hooks.group is 100% binary safe ---This format is implemented, for an easier UECP inplementation ---@param grpseq string -function rds.set_grpseq(grpseq) end +function RDS.set_grpseq(grpseq) end ---@return string -function rds.get_grpseq() end +function RDS.get_grpseq() end ----Puts in a RDS1 group in the buffer, note that block A is filled in always +---Puts in a RDS1 group in the buffer ---@param b integer ---@param c integer ---@param d integer -function rds.put_custom_group(b, c, d) end +function RDS.put_custom_group(b, c, d) end ---Puts in a RDS2 group in the buffer ---@param a integer ---@param b integer ---@param c integer ---@param d integer -function rds.put_rds2_custom_group(a, b, c, d) end +function RDS.put_rds2_custom_group(a, b, c, d) end ---Sets the AFs included in group 0 ---@param afs table -function rds.set_af(afs) end +function RDS.set_af(afs) end ---@class EON_Data ---@field enabled? boolean @@ -268,12 +239,12 @@ function rds.set_af(afs) end ---Sets data about the EON ---@param eon integer Index of the EON we are setting ---@param data EON_Data -function rds.set_eon(eon, data) end +function RDS.set_eon(eon, data) end ---Gets the same data set_rds_eon sets ---@param eon integer ---@return EON_Data -function rds.get_eon(eon) end +function RDS.get_eon(eon) end userdata = {} @@ -298,10 +269,10 @@ function userdata.get() end ---@return string function userdata.get_offset(offset, size) end ---- Extensions start here - The following is made in Lua and can be used as an standard library - +---@class ext ext = {} -rds.ext = {} +---@class RDSext +RDS.ext = {} -- RT Plus Tags ---Sets RT+ tags: type1, start1, len1, type2, start2, len2 @@ -312,29 +283,29 @@ rds.ext = {} ---@param t2 integer ---@param s2 integer ---@param l2 integer -function rds.ext.set_rtplus_tags(ertp, t1, s1, l1, t2, s2, l2) end +function RDS.ext.set_rtplus_tags(ertp, t1, s1, l1, t2, s2, l2) end ---Gets RT+ tags: type1, start1, len1, type2, start2, len2 ---@param ertp boolean ---@return integer type1, integer start1, integer len1, integer type2, integer start2, integer len2 -function rds.ext.get_rtplus_tags(ertp) end +function RDS.ext.get_rtplus_tags(ertp) end ---Toggles RTP or ERTP's toggle switch ---@param ertp boolean -function rds.ext.toggle_rtp(ertp) end +function RDS.ext.toggle_rtp(ertp) end ---Sets the metadata of RTP or ERTP ---@param ertp boolean ---@param running boolean -function rds.ext.set_rtp_meta(ertp, running) end +function RDS.ext.set_rtp_meta(ertp, running) end ---Gets the metadata of RTP and ERTP ---@param ertp boolean ---@return boolean running -function rds.ext.get_rtp_meta(ertp) end +function RDS.ext.get_rtp_meta(ertp) end ---Sets the AFs included in the ODA ---@param afs table -function rds.ext.set_af_oda(afs) end +function RDS.ext.set_af_oda(afs) end ---Registers an ODA to be used in the 0x6 of the group sequence. ODAs are stored as state data, thus running reset_rds will clear it ---Groups 14, 15, 2, 0 cannot be registered either version, groups 10, 4, 1 can be only registered as B, any other is free to take @@ -391,10 +362,10 @@ function ext.register_oda_rds2(aid, data, file_related) end function ext.unregister_oda_rds2(oda_id) end ---@param ert string -function rds.ext.set_ert(ert) end +function RDS.ext.set_ert(ert) end ---@return string -function rds.ext.get_ert() end +function RDS.ext.get_ert() end ---@param designator string ---@param handler function -function rds.ext.register_group(designator, handler) end \ No newline at end of file +function RDS.ext.register_group(designator, handler) end \ No newline at end of file diff --git a/scripts/0-group_manager.lua b/scripts/0-group_manager.lua index 616bfdf..ed13d37 100644 --- a/scripts/0-group_manager.lua +++ b/scripts/0-group_manager.lua @@ -14,7 +14,7 @@ end ---@param designator string ---@param handler function -function rds.ext.register_group(designator, handler) +function RDS.ext.register_group(designator, handler) for i = 1, #designator do local char = designator:sub(i, i) hooks.registered_groups[char] = handler diff --git a/scripts/1-oda.lua b/scripts/1-oda.lua index 4fb7989..f5e5ae1 100644 --- a/scripts/1-oda.lua +++ b/scripts/1-oda.lua @@ -119,7 +119,7 @@ local function group_handler(group_type) elseif group_type == "\xff" then return get_data() else return false, 0, 0, 0 end end -rds.ext.register_group("\x06\xff", group_handler) +RDS.ext.register_group("\x06\xff", group_handler) table.insert(hooks.on_state, function () _RDS_ODAs = {} diff --git a/scripts/1-rft.lua b/scripts/1-rft.lua index 9ca16ea..c234ca9 100644 --- a/scripts/1-rft.lua +++ b/scripts/1-rft.lua @@ -138,7 +138,7 @@ function RftInstance:sendFile(aid, path, id, crc, once) local f_size = #self.file_data if crc == 0 then self.crc_mode = 0 - self.crc_full_file = dp.crc16(self.file_data) + self.crc_full_file = Data.crc16(self.file_data) elseif crc == true or crc == 7 then if f_size <= 40960 then self.crc_mode = 1 elseif f_size > 40960 and f_size <= 81920 then self.crc_mode = 2 @@ -153,7 +153,7 @@ function RftInstance:sendFile(aid, path, id, crc, once) local chunk_size = 5 * multiplier for i = 1, f_size, chunk_size do local chunk = string.sub(self.file_data, i, i + chunk_size - 1) - local v = dp.crc16(chunk) + local v = Data.crc16(chunk) self.crc_data = self.crc_data .. string.char(v >> 8, v & 0xff) end end diff --git a/scripts/2-af.lua b/scripts/2-af.lua index fd78ad9..0f3d025 100644 --- a/scripts/2-af.lua +++ b/scripts/2-af.lua @@ -106,7 +106,7 @@ end ---Sets the AFs included in the ODA and saves them ---@param afs table List of numbers (e.g., {98.1, 102.5}) -function rds.ext.set_af_oda(afs) +function RDS.ext.set_af_oda(afs) _process_af_list(afs) save_af_to_userdata(afs) end diff --git a/scripts/2-ert.lua b/scripts/2-ert.lua index 0761645..0813995 100644 --- a/scripts/2-ert.lua +++ b/scripts/2-ert.lua @@ -41,7 +41,7 @@ function unregister_ert() end end -function rds.ext.set_ert(ert) +function RDS.ext.set_ert(ert) if #ert == 0 then userdata.set_offset(USERDATA_ERT_OFFSET, 128, "") userdata.set_offset(USERDATA_ERT_OFFSET+128, 1, string.char(0)) @@ -72,7 +72,7 @@ function rds.ext.set_ert(ert) if _Ert_oda_id == nil then init_ert() end end -function rds.ext.get_ert() +function RDS.ext.get_ert() local segments = string.byte(userdata.get_offset(USERDATA_ERT_OFFSET+128, 1)) if segments == 0 then return "" end diff --git a/scripts/2-rtp.lua b/scripts/2-rtp.lua index fc36029..0156a1c 100644 --- a/scripts/2-rtp.lua +++ b/scripts/2-rtp.lua @@ -52,7 +52,7 @@ local function init_ertp() end end -function rds.ext.set_rtp_meta(ertp, running) +function RDS.ext.set_rtp_meta(ertp, running) if ertp then if running and _Ertp_oda_id == nil then init_ertp() end userdata.set_offset(USERDATA_RTP_OFFSET+7, 1, string.char(running and 1 or 0)) @@ -61,21 +61,21 @@ function rds.ext.set_rtp_meta(ertp, running) userdata.set_offset(USERDATA_RTP_OFFSET, 1, string.char(running and 1 or 0)) end end -function rds.ext.get_rtp_meta(ertp) +function RDS.ext.get_rtp_meta(ertp) local offset = ertp and (USERDATA_RTP_OFFSET+7) or USERDATA_RTP_OFFSET return string.byte(userdata.get_offset(offset, 1)) ~= 0 end -function rds.ext.toggle_rtp(ertp) +function RDS.ext.toggle_rtp(ertp) if ertp then _Ertp_toggle = not _Ertp_toggle else _Rtp_toggle = not _Rtp_toggle end end -function rds.ext.set_rtplus_tags(ertp, t1, s1, l1, t2, s2, l2) - rds.ext.set_rtp_meta(ertp, true) - rds.ext.toggle_rtp(ertp) +function RDS.ext.set_rtplus_tags(ertp, t1, s1, l1, t2, s2, l2) + RDS.ext.set_rtp_meta(ertp, true) + RDS.ext.toggle_rtp(ertp) userdata.set_offset(ertp and (USERDATA_RTP_OFFSET+8) or (USERDATA_RTP_OFFSET+1), 6, string.char(t1, s1, l1, t2, s2, l2)) end -function rds.ext.get_rtplus_tags(ertp) +function RDS.ext.get_rtplus_tags(ertp) return string.byte(userdata.get_offset(ertp and (USERDATA_RTP_OFFSET+8) or (USERDATA_RTP_OFFSET+1), 6), 1, 6) end @@ -90,6 +90,6 @@ function unregister_rtp(ertp) end table.insert(hooks.on_state, function () - if rds.ext.get_rtp_meta(false) then init_rtp() end - if rds.ext.get_rtp_meta(true) then init_ertp() end + if RDS.ext.get_rtp_meta(false) then init_rtp() end + if RDS.ext.get_rtp_meta(true) then init_ertp() end end) \ No newline at end of file diff --git a/scripts/98-uecp.lua b/scripts/98-uecp.lua index c585c30..ad89ba7 100644 --- a/scripts/98-uecp.lua +++ b/scripts/98-uecp.lua @@ -19,29 +19,29 @@ hooks.rt_transmission[#hooks.rt_transmission + 1] = function () local entry = uecp.rt_buffer[uecp.rt_buffer_index] uecp.rt_tx_remaining = entry.number_tx - rds.set_rt_raw(entry.text) - if entry.toggle_ab then rds.toggle_rt_ab() end + RDS.set_rt_raw(entry.text) + if entry.toggle_ab then RDS.toggle_rt_ab() end end local function dsn_helper(dsn, write) if dsn == 0 then write() elseif dsn == 254 then - local start = dp.get_writing_program() - for i = 1, dp.max_programs do + local start = Data.get_writing_program() + for i = 1, Data.max_programs do local p = i - 1 if p ~= start then - dp.set_writing_program(p) + Data.set_writing_program(p) write() end end elseif dsn == 255 then - for i = 1, dp.max_programs do - dp.set_writing_program(i - 1) + for i = 1, Data.max_programs do + Data.set_writing_program(i - 1) write() end else - dp.set_writing_program(dsn - 1) + Data.set_writing_program(dsn - 1) write() end end @@ -55,7 +55,7 @@ mec_handlers[1] = function(data) local pi_lsb = string.byte(data, 5) dsn_helper(dsn, function() - rds.set_pi((pi_msb << 8) | pi_lsb) + RDS.pi = (pi_msb << 8) | pi_lsb end) return 5 end @@ -65,7 +65,7 @@ mec_handlers[2] = function(data) local psn = string.byte(data, 3) local ps = string.sub(data, 4, 11) -- Static len dsn_helper(dsn, function() - rds.set_ps(ps) + RDS.set_ps(ps) end) return 11 end @@ -80,7 +80,7 @@ mec_handlers[0x21] = function(data) if cut then lps = string.sub(lps, 1, cut - 1) end dsn_helper(dsn, function() - rds.set_lps(lps) + RDS.set_lps(lps) end) return 4 + mel end @@ -90,7 +90,7 @@ mec_handlers[4] = function(data) local psn = string.byte(data, 3) local data = string.byte(data, 4) dsn_helper(dsn, function () - rds.set_dpty((data & 8) ~= 0) + RDS.dpty = (data & 8) ~= 0 end) return 4 end @@ -100,8 +100,8 @@ mec_handlers[3] = function(data) local psn = string.byte(data, 3) local data = string.byte(data, 4) dsn_helper(dsn, function () - rds.set_ta((data & 1) ~= 0) - rds.set_tp((data & 2) ~= 0) + RDS.ta = (data & 1) ~= 0 + RDS.tp = (data & 2) ~= 0 end) return 4 end @@ -111,7 +111,7 @@ mec_handlers[7] = function(data) local psn = string.byte(data, 3) local data = string.byte(data, 4) dsn_helper(dsn, function () - rds.set_pty(data) + RDS.pty = data end) return 4 end @@ -121,7 +121,7 @@ mec_handlers[0x3E] = function(data) local psn = string.byte(data, 3) local ptyn = string.sub(data, 4, 11) -- Static dsn_helper(dsn, function () - rds.set_ptyn(ptyn) + RDS.set_ptyn(ptyn) end) return 11 end @@ -136,7 +136,7 @@ mec_handlers[0x0A] = function(data) uecp.rt_buffer_index = 1 uecp.rt_tx_remaining = 0 dsn_helper(dsn, function () - rds.set_rt_enabled(false) + RDS.rt_enabled = false end) return 4 end @@ -151,7 +151,7 @@ mec_handlers[0x0A] = function(data) uecp.rt_buffer_index = 1 uecp.rt_tx_remaining = 0 dsn_helper(dsn, function () - rds.set_rt_enabled(false) + RDS.rt_enabled = false end) return 5 end @@ -167,9 +167,9 @@ mec_handlers[0x0A] = function(data) uecp.rt_tx_remaining = number_tx -- Seed the encoder immediately with the first entry dsn_helper(dsn, function () - rds.set_rt_raw(rt_text) - if toggle_ab then rds.toggle_rt_ab() end - rds.set_rt_enabled(true) + RDS.set_rt_raw(rt_text) + if toggle_ab then RDS.toggle_rt_ab() end + RDS.rt_enabled = true end) elseif buffer_config == 2 then uecp.rt_buffer[#uecp.rt_buffer + 1] = { text = rt_text, number_tx = number_tx, toggle_ab = toggle_ab } @@ -209,7 +209,7 @@ mec_handlers[0x1A] = function (data) if variant == 0 then -- TODO: maybe make it more than just ecc? idk why but would be cool dsn_helper(dsn, function () - rds.set_ecc(full_data) + RDS.ecc = full_data end) end @@ -230,7 +230,7 @@ mec_handlers[0x24] = function (data) local blockc_lsb = string.byte(data, 5) local blockd_msb = string.byte(data, 6) local blockd_lsb = string.byte(data, 7) - rds.put_custom_group((group << 11) | blockb, (blockc_msb << 8) | blockc_lsb, (blockd_msb << 8) | blockd_lsb) + RDS.put_custom_group((group << 11) | blockb, (blockc_msb << 8) | blockc_lsb, (blockd_msb << 8) | blockd_lsb) return 7 end -- Fuck you mean, i have to implement some fucking "spinning wHELLs"? may the lord have mercy @@ -239,7 +239,7 @@ mec_handlers[0x19] = function (data) -- CT local data = string.byte(data, 2) dsn_helper(255, function () - rds.set_ct(data ~= 0) + RDS.ct = data ~= 0 end) return 2 end @@ -249,7 +249,7 @@ mec_handlers[0x16] = function (data) local mel = string.byte(data, 3) local group_sequence = string.sub(data, 4, 3+mel) dsn_helper(dsn, function () - rds.set_grpseq(group_sequence) + RDS.set_grpseq(group_sequence) end) return 4 + mel end @@ -282,8 +282,8 @@ end mec_handlers[0x1C] = function (data) -- DS select local dsn = string.byte(data, 2) - dp.set_output_program(dsn) - dp.set_writing_program(dsn) + Data.set_output_program(dsn) + Data.set_writing_program(dsn) return 2 end mec_handlers[0x2D] = function (data) @@ -367,7 +367,7 @@ function uecp.parse_uecp(packet) return end - local crc_calculated = dp.crc16(string.sub(unstuffed, 1, 4 + mfl)) + local crc_calculated = Data.crc16(string.sub(unstuffed, 1, 4 + mfl)) local crc_hi = string.byte(unstuffed, 4 + mfl + 1) local crc_stored = (crc_hi << 8) | string.byte(unstuffed, 4 + mfl + 2) @@ -386,6 +386,6 @@ function uecp.parse_uecp(packet) end local advance = handler(string.sub(data, consumed)) consumed = consumed + advance - dp.set_writing_program(dp.get_output_program()) + Data.set_writing_program(Data.get_output_program()) end end \ No newline at end of file diff --git a/scripts/99-data.lua b/scripts/99-data.lua index da0a86c..2c035f7 100644 --- a/scripts/99-data.lua +++ b/scripts/99-data.lua @@ -39,48 +39,48 @@ function hooks.parse_ascii(data) end if cmd == nil then data = data:lower() - if data == "ver" then return string.format("rds95 core v. %s - (C) 2025 radio95 - lua parser\r\n", dp.core_version) + if data == "ver" then return string.format("rds95 core v. %s - (C) 2026 radio95 - lua parser\r\n", Data.core_version) elseif data == "init" then - dp.set_program_defaults() + Data.set_program_defaults() return "+\r\n" elseif data == "reset" then - dp.reset_rds() + Data.reset_rds() return "+\r\n" - elseif data == "pi" then return string.format("PI=%s\r\n", string.format("%x", rds.get_pi())) - elseif data == "pty" then return string.format("PTY=%s\r\n", string.format("%d", rds.get_pty())) - elseif data == "ecc" then return string.format("ECC=%s\r\n", string.format("%x", rds.get_ecc())) - elseif data == "slcd" then return string.format("SLCD=%s\r\n", string.format("%x", rds.get_slc_data())) - elseif data == "ct" then return string.format("CT=%s\r\n", string.format("%d", (rds.get_ct() and 1 or 0))) - elseif data == "dpty" then return string.format("DPTY=%s\r\n", string.format("%d", (rds.get_dpty() and 1 or 0))) - elseif data == "tp" then return string.format("TP=%s\r\n", string.format("%d", (rds.get_tp() and 1 or 0))) - elseif data == "ta" then return string.format("TA=%s\r\n", string.format("%d", (rds.get_ta() and 1 or 0))) - elseif data == "rt1en" then return string.format("RT1EN=%s\r\n", string.format("%d", (rds.get_rt_enabled() and 1 or 0))) - elseif data == "ptynen" then return string.format("PTYNEN=%s\r\n", string.format("%d", (rds.get_ptyn_enabled() and 1 or 0))) - elseif data == "rdsgen" then return string.format("RDSGEN=%s\r\n", string.format("%d", rds.get_streams())) - elseif data == "link" then return string.format("LINK=%s\r\n", string.format("%d", (rds.get_link() and 1 or 0))) + elseif data == "pi" then return string.format("PI=%s\r\n", string.format("%x", RDS.pi)) + elseif data == "pty" then return string.format("PTY=%s\r\n", string.format("%d", RDS.pty)) + elseif data == "ecc" then return string.format("ECC=%s\r\n", string.format("%x", RDS.ecc)) + elseif data == "slcd" then return string.format("SLCD=%s\r\n", string.format("%x", RDS.slc_data)) + elseif data == "ct" then return string.format("CT=%s\r\n", string.format("%d", (RDS.ct and 1 or 0))) + elseif data == "dpty" then return string.format("DPTY=%s\r\n", string.format("%d", (RDS.dpty and 1 or 0))) + elseif data == "tp" then return string.format("TP=%s\r\n", string.format("%d", (RDS.tp and 1 or 0))) + elseif data == "ta" then return string.format("TA=%s\r\n", string.format("%d", (RDS.ta and 1 or 0))) + elseif data == "rt1en" then return string.format("RT1EN=%s\r\n", string.format("%d", (RDS.rt_enabled and 1 or 0))) + elseif data == "ptynen" then return string.format("PTYNEN=%s\r\n", string.format("%d", (RDS.ptyn_enabled and 1 or 0))) + elseif data == "rdsgen" then return string.format("RDSGEN=%s\r\n", string.format("%d", RDS.get_streams())) + elseif data == "link" then return string.format("LINK=%s\r\n", string.format("%d", (RDS.get_link() and 1 or 0))) elseif data == "rtp" then - local t1, s1, l1, t2, s2, l2 = rds.ext.get_rtplus_tags(false) + local t1, s1, l1, t2, s2, l2 = RDS.ext.get_rtplus_tags(false) return string.format("RTP=%d,%d,%d,%d,%d,%d\r\n", t1, s1, l1, t2, s2, l2) elseif data == "ertp" then - local t1, s1, l1, t2, s2, l2 = rds.ext.get_rtplus_tags(true) + local t1, s1, l1, t2, s2, l2 = RDS.ext.get_rtplus_tags(true) return string.format("ERTP=%d,%d,%d,%d,%d,%d\r\n", t1, s1, l1, t2, s2, l2) elseif data == "rtprun" then - local running = rds.ext.get_rtp_meta(false) + local running = RDS.ext.get_rtp_meta(false) local f1 = 2 or (running and 1 or 0) return string.format("RTPRUN=%d\r\n", f1) elseif data == "ertprun" then - local running = rds.ext.get_rtp_meta(true) + local running = RDS.ext.get_rtp_meta(true) local f1 = 2 or (running and 1 or 0) return string.format("ERTPRUN=%d\r\n", f1) - elseif data == "lps" then return string.format("LPS=%s\r\n", rds.get_lps()) - elseif data == "ert" then return string.format("ERT=%s\r\n", rds.ext.get_ert()) + elseif data == "lps" then return string.format("LPS=%s\r\n", RDS.get_lps()) + elseif data == "ert" then return string.format("ERT=%s\r\n", RDS.ext.get_ert()) else local eon_cmd, eon_num = data:match("^eon(%d+)([a-z]+)$") if eon_cmd then local eon_idx = tonumber(eon_cmd) - if not eon_idx or eon_idx < 1 or eon_idx > rds.eon_count then return "?\r\n" end + if not eon_idx or eon_idx < 1 or eon_idx > RDS.eon_count then return "?\r\n" end eon_idx = eon_idx - 1 - local eon_data = rds.get_eon(eon_idx) + local eon_data = RDS.get_eon(eon_idx) if eon_num == "en" then return string.format("EON%dEN=%d\r\n", eon_idx + 1, eon_data.enabled and 1 or 0) elseif eon_num == "pi" then return string.format("EON%dPI=%x\r\n", eon_idx + 1, eon_data.pi) @@ -101,46 +101,46 @@ function hooks.parse_ascii(data) local eon_num, eon_type = cmd:match("^eon(%d+)([a-z]+)$") if eon_num then local eon_idx = tonumber(eon_num) - if not eon_idx or eon_idx < 1 or eon_idx > rds.eon_count then return "?\r\n" end + if not eon_idx or eon_idx < 1 or eon_idx > RDS.eon_count then return "?\r\n" end eon_idx = eon_idx - 1 if eon_type == "en" then local en_val = tonumber(value) if not en_val then return "-\r\n" end local enabled = (en_val ~= 0) - rds.set_eon(eon_idx, { enabled = enabled }) + RDS.set_eon(eon_idx, { enabled = enabled }) return "+\r\n" elseif eon_type == "pi" then local pi_val = tonumber(value, 16) if not pi_val then return "-\r\n" end - rds.set_eon(eon_idx, { pi = pi_val }) + RDS.set_eon(eon_idx, { pi = pi_val }) return "+\r\n" elseif eon_type == "ps" then local ps_val = value:sub(1, 24) - rds.set_eon(eon_idx, { ps = ps_val }) + RDS.set_eon(eon_idx, { ps = ps_val }) return "+\r\n" elseif eon_type == "pty" then local pty_val = tonumber(value) if not pty_val then return "-\r\n" end - rds.set_eon(eon_idx, { pty = pty_val }) + RDS.set_eon(eon_idx, { pty = pty_val }) return "+\r\n" elseif eon_type == "ta" then local ta_val = tonumber(value) if not ta_val then return "-\r\n" end local ta = (ta_val ~= 0) - rds.set_eon(eon_idx, { ta = ta }) - if ta then rds.set_ta(true) end + RDS.set_eon(eon_idx, { ta = ta }) + if ta then RDS.ta = true end return "+\r\n" elseif eon_type == "tp" then local tp_val = tonumber(value) if not tp_val then return "-\r\n" end local tp = (tp_val ~= 0) - rds.set_eon(eon_idx, { tp = tp }) + RDS.set_eon(eon_idx, { tp = tp }) return "+\r\n" elseif eon_type == "af" then local af_table = {} if value == "" or value == "0" then - rds.set_eon(eon_idx, { afs = {} }) + RDS.set_eon(eon_idx, { afs = {} }) return "+\r\n" end for freq_str in value:gmatch("([^,]+)") do @@ -149,12 +149,12 @@ function hooks.parse_ascii(data) else return "-\r\n" end end if #af_table > 25 then return "-\r\n" end - rds.set_eon(eon_idx, { afs = af_table }) + RDS.set_eon(eon_idx, { afs = af_table }) return "+\r\n" elseif eon_type == "dt" then local dt_val = tonumber(value, 16) if not dt_val then return "-\r\n" end - rds.set_eon(eon_idx, { data = dt_val }) + RDS.set_eon(eon_idx, { data = dt_val }) return "+\r\n" else return "?\r\n" end end @@ -163,96 +163,96 @@ function hooks.parse_ascii(data) local pi = tonumber(value, 16) if not pi then return "-\r\n" end if (pi & 0xF000) == 0 then return "-\r\n" end - rds.set_pi(pi) + RDS.pi = pi return "+\r\n" elseif cmd == "ecc" then local ecc = tonumber(value, 16) if not ecc then return "-\r\n" end - rds.set_ecc(ecc) + RDS.ecc = ecc return "+\r\n" elseif cmd == "pty" then local pty = tonumber(value) if not pty then return "-\r\n" end - rds.set_pty(pty) + RDS.pty = pty return "+\r\n" elseif cmd == "ct" then local ct = tonumber(value) if not ct then return "-\r\n" end - rds.set_ct(ct ~= 0) + RDS.ct = ct ~= 0 return "+\r\n" elseif cmd == "dpty" then local dpty = tonumber(value) if not dpty then return "-\r\n" end - rds.set_dpty(dpty ~= 0) + RDS.dpty = dpty ~= 0 return "+\r\n" elseif cmd == "tp" then local tp = tonumber(value) if not tp then return "-\r\n" end - rds.set_tp(tp ~= 0) + RDS.tp = tp ~= 0 return "+\r\n" elseif cmd == "ta" then local ta = tonumber(value) if not ta then return "-\r\n" end - rds.set_ta(ta ~= 0) + RDS.ta = ta ~= 0 return "+\r\n" elseif cmd == "rt1en" then local en = tonumber(value) if not en then return "-\r\n" end - rds.set_rt_enabled(en ~= 0) + RDS.rt_enabled = en ~= 0 return "+\r\n" elseif cmd == "ptynen" then local en = tonumber(value) if not en then return "-\r\n" end - rds.set_ptyn_enabled(en ~= 0) + RDS.ptyn_enabled = en ~= 0 return "+\r\n" elseif cmd == "rdsgen" then local type = tonumber(value) if not type then return "-\r\n" end - rds.set_streams(type) + RDS.set_streams(type) return "+\r\n" elseif cmd == "ptyn" then - rds.set_ptyn(value) + RDS.set_ptyn(value) return "+\r\n" elseif cmd == "ps" then - rds.set_ps(value) + RDS.set_ps(value) return "+\r\n" elseif cmd == "tps" then - rds.set_tps(value) + RDS.set_tps(value) return "+\r\n" elseif cmd == "rt1" or cmd == "text" then uecp.rt_buffer = {} uecp.rt_buffer_index = 1 - uecp.rt_buffer[1] = { text = dp.encode_charset(value), number_tx = 0, toggle_ab = true } + uecp.rt_buffer[1] = { text = Data.encode_charset(value), number_tx = 0, toggle_ab = true } uecp.rt_tx_remaining = 0 - rds.set_rt_enabled(true) - rds.set_rt(value) - rds.toggle_rt_ab() + RDS.rt_enabled = true + RDS.set_rt(value) + RDS.toggle_rt_ab() return "+\r\n" elseif cmd == "lps" then - rds.set_lps(value) + RDS.set_lps(value) return "+\r\n" elseif cmd == "ert" then - rds.ext.set_ert(value) + RDS.ext.set_ert(value) return "+\r\n" elseif cmd == "link" then local link = tonumber(value) if not link then return "-\r\n" end - rds.set_link(link ~= 0) + RDS.set_link(link ~= 0) return "+\r\n" elseif cmd == "program" then local program = tonumber(value) if not program then return "-\r\n" end - if program < 1 or program > dp.max_programs then return "-\r\n" end - dp.set_output_program(program-1) - dp.set_writing_program(program-1) - rds.set_ta(false) + if program < 1 or program > Data.max_programs then return "-\r\n" end + Data.set_output_program(program-1) + Data.set_writing_program(program-1) + RDS.ta = false return "+\r\n" elseif cmd == "rtp" or cmd == "ertp" then local is_ertp = (cmd == "ertp") local t1, s1, l1, t2, s2, l2 = value:match("(%d+),(%d+),(%d+),(%d+),(%d+),(%d+)") if not l2 then return "-\r\n" end - rds.ext.set_rtplus_tags( + RDS.ext.set_rtplus_tags( is_ertp, ---@diagnostic disable-next-line: param-type-mismatch tonumber(t1), tonumber(s1), tonumber(l1), tonumber(t2), tonumber(s2), tonumber(l2) @@ -261,13 +261,13 @@ function hooks.parse_ascii(data) elseif cmd == "g" then local a, b, c, d = value:match("^(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)$") if a and b and c and d then - rds.put_rds2_custom_group(tonumber(a, 16), tonumber(b, 16), tonumber(c, 16), tonumber(d, 16)) + RDS.put_rds2_custom_group(tonumber(a, 16), tonumber(b, 16), tonumber(c, 16), tonumber(d, 16)) return "+\r\n" end local b1, c1, d1 = value:match("^(%x%x%x%x)(%x%x%x%x)(%x%x%x%x)$") if b1 and c1 and d1 then - rds.put_custom_group(tonumber(b1, 16), tonumber(c1, 16), tonumber(d1, 16)) + RDS.put_custom_group(tonumber(b1, 16), tonumber(c1, 16), tonumber(d1, 16)) return "+\r\n" end @@ -282,14 +282,14 @@ function hooks.parse_ascii(data) local f2 = tonumber(f2_str) or 0 local running = (f1 & 1) ~= 0 - rds.ext.set_rtp_meta(is_ertp, running) - if f2 ~= 0 then rds.ext.toggle_rtp(is_ertp) end + RDS.ext.set_rtp_meta(is_ertp, running) + if f2 ~= 0 then RDS.ext.toggle_rtp(is_ertp) end return "+\r\n" elseif cmd == "af" then local af_table = {} if value == "" or value == "0" then - rds.set_af({}) + RDS.set_af({}) return "+\r\n" end @@ -301,13 +301,13 @@ function hooks.parse_ascii(data) if #af_table > 25 then return "-\r\n" end - rds.set_af(af_table) + RDS.set_af(af_table) return "+\r\n" elseif cmd == "afo" then local af_table = {} if value == "" or value == "0" then - rds.ext.set_af_oda({}) + RDS.ext.set_af_oda({}) return "+\r\n" end @@ -319,7 +319,7 @@ function hooks.parse_ascii(data) if #af_table > 25 then return "-\r\n" end - rds.ext.set_af_oda(af_table) + RDS.ext.set_af_oda(af_table) return "+\r\n" else return "?\r\n" end end diff --git a/src/lua_api.c b/src/lua_api.c index b87f7d7..3ba85ed 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -1,53 +1,55 @@ #include "lua_api.h" #include "lua_rds.h" +#include +#include static int in_set_defaults = 0; -extern lua_State *L; +extern lua_State *globalL; extern RDSEncoder* enc; static int writing_program = 0; -int lua_get_userdata(lua_State *localL) { - lua_pushlstring(localL, (const char*)&enc->data[writing_program].lua_data, LUA_USER_DATA); +int lua_get_userdata(lua_State *L) { + lua_pushlstring(L, (const char*)&enc->data[writing_program].lua_data, LUA_USER_DATA); return 1; } -int lua_get_userdata_offset(lua_State *localL) { - uint16_t offset = luaL_checkinteger(localL, 1); - uint16_t size = luaL_checkinteger(localL, 2); - if((offset+size) > LUA_USER_DATA) return luaL_error(localL, "data exceeds limit"); - lua_pushlstring(localL, (const char*)&enc->data[writing_program].lua_data[offset], size); +int lua_get_userdata_offset(lua_State *L) { + uint16_t offset = luaL_checkinteger(L, 1); + uint16_t size = luaL_checkinteger(L, 2); + if((offset+size) > LUA_USER_DATA) return luaL_error(L, "data exceeds limit"); + lua_pushlstring(L, (const char*)&enc->data[writing_program].lua_data[offset], size); return 1; } -int lua_set_userdata(lua_State *localL) { +int lua_set_userdata(lua_State *L) { size_t len; - const char *data = luaL_checklstring(localL, 1, &len); - if(len > LUA_USER_DATA) return luaL_error(localL, "data exceeds limit"); + const char *data = luaL_checklstring(L, 1, &len); + if(len > LUA_USER_DATA) return luaL_error(L, "data exceeds limit"); memset(enc->data[writing_program].lua_data, 0, LUA_USER_DATA); memcpy(enc->data[writing_program].lua_data, data, len); return 0; } -int lua_set_userdata_offset(lua_State *localL) { - uint16_t offset = luaL_checkinteger(localL, 1); - uint16_t size = luaL_checkinteger(localL, 2); +int lua_set_userdata_offset(lua_State *L) { + uint16_t offset = luaL_checkinteger(L, 1); + uint16_t size = luaL_checkinteger(L, 2); size_t len; - const char *data = luaL_checklstring(localL, 3, &len); - if(len > size || (offset + size) > LUA_USER_DATA) return luaL_error(localL, "data exceeds limit"); + const char *data = luaL_checklstring(L, 3, &len); + if(len > size || (offset + size) > LUA_USER_DATA) return luaL_error(L, "data exceeds limit"); memset(enc->data[writing_program].lua_data + offset, 0, size); memcpy(enc->data[writing_program].lua_data + offset, data, len); return 0; } -int lua_force_save(lua_State *localL) { - (void)localL; +int lua_force_save(lua_State *L) { + (void)L; encoder_saveToFile(enc); return 0; } -int lua_set_rds_program_defaults(lua_State *localL) { - (void)localL; +int lua_set_rds_program_defaults(lua_State *L) { + (void)L; if (in_set_defaults) { fprintf(stderr, "Warning: Recursive call to lua_set_rds_program_defaults blocked\n"); return 0; @@ -60,8 +62,8 @@ int lua_set_rds_program_defaults(lua_State *localL) { return 0; } -int lua_reset_rds(lua_State *localL) { - (void)localL; +int lua_reset_rds(lua_State *L) { + (void)L; encoder_saveToFile(enc); encoder_loadFromFile(enc); @@ -70,131 +72,81 @@ int lua_reset_rds(lua_State *localL) { return 0; } -#define BOOL_SETTER(name) \ -int lua_set_rds_##name(lua_State *localL) { \ - if (!lua_isboolean(localL, 1)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 1)); \ - enc->data[writing_program].name = lua_toboolean(localL, 1); \ - return 0; \ -} -#define INT_SETTER(name) \ -int lua_set_rds_##name(lua_State *localL) { \ - enc->data[writing_program].name = luaL_checkinteger(localL, 1); \ - return 0; \ -} #define STR_SETTER(name, function, buffer_size) \ -int lua_set_rds_##name(lua_State *localL) { \ - const char* str = luaL_checklstring(localL, 1, NULL); \ +int lua_set_rds_##name(lua_State *L) { \ + const char* str = luaL_checklstring(L, 1, NULL); \ char converted[buffer_size+1]; \ convert_to_rdscharset(str, converted, buffer_size+1); \ function(enc, converted, writing_program); \ return 0; \ } #define STR_RAW_SETTER(name, function) \ -int lua_set_rds_##name(lua_State *localL) { \ - const char* str = luaL_checklstring(localL, 1, NULL); \ +int lua_set_rds_##name(lua_State *L) { \ + const char* str = luaL_checklstring(L, 1, NULL); \ function(enc, str, writing_program); \ return 0; \ } #define STR_RAW_SETTER_LEN(name, function) \ -int lua_set_rds_##name(lua_State *localL) { \ +int lua_set_rds_##name(lua_State *L) { \ size_t len; \ - const char* str = luaL_checklstring(localL, 1, &len); \ + const char* str = luaL_checklstring(L, 1, &len); \ function(enc, str, len, writing_program); \ return 0; \ } #define AF_SETTER(name, af_field, af_struct, add_func) \ -int lua_set_rds_##name(lua_State *localL) { \ - luaL_checktype(localL, 1, LUA_TTABLE); \ +int lua_set_rds_##name(lua_State *L) { \ + luaL_checktype(L, 1, LUA_TTABLE); \ \ - int n = lua_rawlen(localL, 1); \ + int n = lua_rawlen(L, 1); \ if (n == 0) { \ memset(&(enc->data[writing_program].af_field), 0, sizeof(af_struct)); \ return 0; \ } \ - if(n > 25) return luaL_error(localL, "table length over 25"); \ + if(n > 25) return luaL_error(L, "table length over 25"); \ \ af_struct new_af; \ memset(&new_af, 0, sizeof(af_struct)); \ \ for (int i = 1; i <= n; i++) { \ - lua_rawgeti(localL, 1, i); \ - if (lua_isnumber(localL, -1)) add_func(&new_af, lua_tonumber(localL, -1)); \ - else return luaL_error(localL, "number expected, got %s", luaL_typename(localL, -1)); \ - lua_pop(localL, 1); \ + lua_rawgeti(L, 1, i); \ + if (lua_isnumber(L, -1)) add_func(&new_af, lua_tonumber(L, -1)); \ + else return luaL_error(L, "number expected, got %s", luaL_typename(L, -1)); \ + lua_pop(L, 1); \ } \ memcpy(&(enc->data[writing_program].af_field), &new_af, sizeof(new_af)); \ \ return 0; \ } -#define INT_GETTER(name) \ -int lua_get_rds_##name(lua_State *localL) { \ - lua_pushinteger(localL, enc->data[writing_program].name); \ - return 1; \ -} -#define BOOL_GETTER(name) \ -int lua_get_rds_##name(lua_State *localL) { \ - lua_pushboolean(localL, enc->data[writing_program].name); \ - return 1; \ -} #define STR_RAW_GETTER(name, length) \ -int lua_get_rds_##name(lua_State *localL) { \ - lua_pushlstring(localL, enc->data[writing_program].name, length); \ +int lua_get_rds_##name(lua_State *L) { \ + lua_pushlstring(L, enc->data[writing_program].name, length); \ return 1; \ } -INT_SETTER(pi) -INT_GETTER(pi) -INT_SETTER(pty) -INT_GETTER(pty) - -INT_SETTER(ecc) -INT_GETTER(ecc) - -INT_SETTER(slc_data) -INT_GETTER(slc_data) - -BOOL_SETTER(ct) -BOOL_GETTER(ct) - -BOOL_SETTER(dpty) -BOOL_GETTER(dpty) - -BOOL_SETTER(tp) -BOOL_GETTER(tp) - -BOOL_SETTER(ta) -BOOL_GETTER(ta) - -BOOL_SETTER(rt_enabled) -BOOL_GETTER(rt_enabled) - -BOOL_SETTER(ptyn_enabled) -BOOL_GETTER(ptyn_enabled) - -int lua_set_rds_streams(lua_State *localL) { - enc->enabled_streams = luaL_checkinteger(localL, 1); +int lua_set_rds_streams(lua_State *L) { + enc->enabled_streams = luaL_checkinteger(L, 1); return 0; } -int lua_get_rds_streams(lua_State *localL) { - lua_pushinteger(localL, enc->enabled_streams); +int lua_get_rds_streams(lua_State *L) { + lua_pushinteger(L, enc->enabled_streams); return 1; } -int lua_set_rds_link(lua_State *localL) { - if (!lua_isboolean(localL, 1)) return luaL_error(localL, "boolean expected, got %s", luaL_typename(localL, 1)); - enc->state[writing_program].eon_linkage = lua_toboolean(localL, 1); +int lua_set_rds_link(lua_State *L) { + if (!lua_isboolean(L, 1)) return luaL_error(L, "boolean expected, got %s", luaL_typename(L, 1)); + enc->state[writing_program].eon_linkage = lua_toboolean(L, 1); return 0; } -int lua_get_rds_link(lua_State *localL) { - lua_pushboolean(localL, enc->state[writing_program].eon_linkage); +int lua_get_rds_link(lua_State *L) { + lua_pushboolean(L, enc->state[writing_program].eon_linkage); return 1; } -int lua_set_rds_program(lua_State *localL) { - int program = luaL_checkinteger(localL, 1); +int lua_set_rds_program(lua_State *L) { + int program = luaL_checkinteger(L, 1); if(program >= PROGRAMS) program = (PROGRAMS-1); if(program < 0) program = 0; @@ -207,13 +159,13 @@ int lua_set_rds_program(lua_State *localL) { return 0; } -int lua_get_rds_program(lua_State *localL) { - lua_pushinteger(localL, enc->program); +int lua_get_rds_program(lua_State *L) { + lua_pushinteger(L, enc->program); return 1; } -int lua_set_rds_writing_program(lua_State *localL) { - int program = luaL_checkinteger(localL, 1); +int lua_set_rds_writing_program(lua_State *L) { + int program = luaL_checkinteger(L, 1); if(program >= PROGRAMS) program = (PROGRAMS-1); if(program < 0) program = 0; @@ -221,25 +173,27 @@ int lua_set_rds_writing_program(lua_State *localL) { return 0; } -int lua_get_rds_writing_program(lua_State *localL) { - lua_pushinteger(localL, writing_program); +int lua_get_rds_writing_program(lua_State *L) { + lua_pushinteger(L, writing_program); return 1; } -int lua_put_rds_custom_group(lua_State *localL) { +int lua_put_rds_custom_group(lua_State *L) { + if(enc->state[writing_program].custom_group[0]) return luaL_error(locaL, "group buffer full"); enc->state[writing_program].custom_group[0] = 1; - enc->state[writing_program].custom_group[1] = luaL_checkinteger(localL, 1); - enc->state[writing_program].custom_group[2] = luaL_checkinteger(localL, 2); - enc->state[writing_program].custom_group[3] = luaL_checkinteger(localL, 3); + enc->state[writing_program].custom_group[1] = luaL_checkinteger(L, 1); + enc->state[writing_program].custom_group[2] = luaL_checkinteger(L, 2); + enc->state[writing_program].custom_group[3] = luaL_checkinteger(L, 3); return 0; } -int lua_put_rds2_custom_group(lua_State *localL) { +int lua_put_rds2_custom_group(lua_State *L) { + if(enc->state[writing_program].custom_group2[0]) return luaL_error(locaL, "group buffer full"); enc->state[writing_program].custom_group2[0] = 1; - enc->state[writing_program].custom_group2[1] = luaL_checkinteger(localL, 1); - enc->state[writing_program].custom_group2[2] = luaL_checkinteger(localL, 2); - enc->state[writing_program].custom_group2[3] = luaL_checkinteger(localL, 3); - enc->state[writing_program].custom_group2[4] = luaL_checkinteger(localL, 4); + enc->state[writing_program].custom_group2[1] = luaL_checkinteger(L, 1); + enc->state[writing_program].custom_group2[2] = luaL_checkinteger(L, 2); + enc->state[writing_program].custom_group2[3] = luaL_checkinteger(L, 3); + enc->state[writing_program].custom_group2[4] = luaL_checkinteger(L, 4); return 0; } @@ -261,151 +215,213 @@ STR_RAW_GETTER(grp_sqc, 32) AF_SETTER(af_group0, af, RDSAFs, add_rds_af) -int lua_toggle_rt_ab(lua_State *localL) { - (void)localL; +int lua_toggle_rt_ab(lua_State *L) { + (void)L; TOGGLE(enc->state[writing_program].rt_ab); return 0; } -int lua_set_rds_eon(lua_State *localL) { - int eon = luaL_checkinteger(localL, 1); - if(eon >= EONs) return luaL_error(localL, "eon index exceeded"); +int lua_set_rds_eon(lua_State *L) { + int eon = luaL_checkinteger(L, 1); + if(eon >= EONs) return luaL_error(L, "eon index exceeded"); - if(!lua_istable(localL, 2)) return luaL_error(localL, "table expected, got %s", luaL_typename(localL, 2)); + if(!lua_istable(L, 2)) return luaL_error(L, "table expected, got %s", luaL_typename(L, 2)); - lua_getfield(localL, 2, "enabled"); - if(!lua_isnil(localL, -1)) { - luaL_checktype(localL, -1, LUA_TBOOLEAN); - enc->data[writing_program].eon[eon].enabled = lua_toboolean(localL, -1); - } lua_pop(localL, 1); + lua_getfield(L, 2, "enabled"); + if(!lua_isnil(L, -1)) { + luaL_checktype(L, -1, LUA_TBOOLEAN); + enc->data[writing_program].eon[eon].enabled = lua_toboolean(L, -1); + } lua_pop(L, 1); - lua_getfield(localL, 2, "tp"); - if(!lua_isnil(localL, -1)) { - luaL_checktype(localL, -1, LUA_TBOOLEAN); - enc->data[writing_program].eon[eon].tp = lua_toboolean(localL, -1); - } lua_pop(localL, 1); + lua_getfield(L, 2, "tp"); + if(!lua_isnil(L, -1)) { + luaL_checktype(L, -1, LUA_TBOOLEAN); + enc->data[writing_program].eon[eon].tp = lua_toboolean(L, -1); + } lua_pop(L, 1); - lua_getfield(localL, 2, "ta"); - if(!lua_isnil(localL, -1)) { - luaL_checktype(localL, -1, LUA_TBOOLEAN); - enc->data[writing_program].eon[eon].ta = lua_toboolean(localL, -1); - } lua_pop(localL, 1); + lua_getfield(L, 2, "ta"); + if(!lua_isnil(L, -1)) { + luaL_checktype(L, -1, LUA_TBOOLEAN); + enc->data[writing_program].eon[eon].ta = lua_toboolean(L, -1); + } lua_pop(L, 1); - lua_getfield(localL, 2, "pi"); - if(!lua_isnil(localL, -1)) { - lua_Integer pi = luaL_checkinteger(localL, -1); - lua_pop(localL, 1); + lua_getfield(L, 2, "pi"); + if(!lua_isnil(L, -1)) { + lua_Integer pi = luaL_checkinteger(L, -1); + lua_pop(L, 1); enc->data[writing_program].eon[eon].pi = pi; - } else lua_pop(localL, 1); + } else lua_pop(L, 1); - lua_getfield(localL, 2, "pty"); - if(!lua_isnil(localL, -1)) { - lua_Integer pty = luaL_checkinteger(localL, -1); - lua_pop(localL, 1); + lua_getfield(L, 2, "pty"); + if(!lua_isnil(L, -1)) { + lua_Integer pty = luaL_checkinteger(L, -1); + lua_pop(L, 1); enc->data[writing_program].eon[eon].pty = pty; - } else lua_pop(localL, 1); + } else lua_pop(L, 1); - lua_getfield(localL, 2, "data"); - if(!lua_isnil(localL, -1)) { - lua_Integer data = luaL_checkinteger(localL, -1); - lua_pop(localL, 1); + lua_getfield(L, 2, "data"); + if(!lua_isnil(L, -1)) { + lua_Integer data = luaL_checkinteger(L, -1); + lua_pop(L, 1); enc->data[writing_program].eon[eon].data = data; - } else lua_pop(localL, 1); + } else lua_pop(L, 1); - lua_getfield(localL, 2, "ps"); - if(!lua_isnil(localL, -1)) _strncpy(enc->data[writing_program].eon[eon].ps, luaL_checklstring(localL, -1, NULL), 8); - lua_pop(localL, 1); + lua_getfield(L, 2, "ps"); + if(!lua_isnil(L, -1)) _strncpy(enc->data[writing_program].eon[eon].ps, luaL_checklstring(L, -1, NULL), 8); + lua_pop(L, 1); - lua_getfield(localL, 2, "afs"); - luaL_checktype(localL, -1, LUA_TTABLE); + lua_getfield(L, 2, "afs"); + luaL_checktype(L, -1, LUA_TTABLE); - int n = lua_rawlen(localL, -1); + int n = lua_rawlen(L, -1); if (n == 0) { - lua_pop(localL, 1); + lua_pop(L, 1); memset(&(enc->data[writing_program].eon[eon].af), 0, sizeof(RDSAFs)); return 0; } - if(n > 25) return luaL_error(localL, "table length over 25"); + if(n > 25) return luaL_error(L, "table length over 25"); RDSAFs new_af; memset(&new_af, 0, sizeof(RDSAFs)); for (int i = 1; i <= n; i++) { - lua_rawgeti(localL, -1, i); - if (!lua_isnumber(localL, -1)) { - const char *type = luaL_typename(localL, -1); - lua_pop(localL, 1); - return luaL_error(localL, "number expected, got %s", type); + lua_rawgeti(L, -1, i); + if (!lua_isnumber(L, -1)) { + const char *type = luaL_typename(L, -1); + lua_pop(L, 1); + return luaL_error(L, "number expected, got %s", type); } - add_rds_af(&new_af, lua_tonumber(localL, -1)); - lua_pop(localL, 1); + add_rds_af(&new_af, lua_tonumber(L, -1)); + lua_pop(L, 1); } memcpy(&(enc->data[writing_program].eon[eon].af), &new_af, sizeof(new_af)); - lua_pop(localL, 1); + lua_pop(L, 1); return 0; } -int lua_get_rds_eon(lua_State *localL) { - int eon = luaL_checkinteger(localL, 1); - if(eon >= EONs) return luaL_error(localL, "eon index exceeded"); +int lua_get_rds_eon(lua_State *L) { + int eon = luaL_checkinteger(L, 1); + if(eon >= EONs) return luaL_error(L, "eon index exceeded"); - lua_newtable(localL); - lua_pushboolean(localL, enc->data[writing_program].eon[eon].enabled); + lua_newtable(L); + lua_pushboolean(L, enc->data[writing_program].eon[eon].enabled); lua_setfield(L, -2, "enabled"); - lua_pushinteger(localL, enc->data[writing_program].eon[eon].pi); + lua_pushinteger(L, enc->data[writing_program].eon[eon].pi); lua_setfield(L, -2, "pi"); - lua_pushboolean(localL, enc->data[writing_program].eon[eon].tp); + lua_pushboolean(L, enc->data[writing_program].eon[eon].tp); lua_setfield(L, -2, "tp"); - lua_pushboolean(localL, enc->data[writing_program].eon[eon].ta); + lua_pushboolean(L, enc->data[writing_program].eon[eon].ta); lua_setfield(L, -2, "ta"); - lua_pushinteger(localL, enc->data[writing_program].eon[eon].pty); + lua_pushinteger(L, enc->data[writing_program].eon[eon].pty); lua_setfield(L, -2, "pty"); - lua_pushlstring(localL, enc->data[writing_program].eon[eon].ps, 8); + lua_pushlstring(L, enc->data[writing_program].eon[eon].ps, 8); lua_setfield(L, -2, "ps"); - lua_newtable(localL); // don't have decoding for AF, so just return empty table + lua_newtable(L); // don't have decoding for AF, so just return empty table lua_setfield(L, -2, "afs"); - lua_pushinteger(localL, enc->data[writing_program].eon[eon].data); + lua_pushinteger(L, enc->data[writing_program].eon[eon].data); lua_setfield(L, -2, "data"); return 1; } -int lua_crc16(lua_State *localL) { +int lua_crc16(lua_State *L) { size_t len; - const char* data = luaL_checklstring(localL, 1, &len); - lua_pushinteger(localL, crc16_ccitt(data, len)); + const char* data = luaL_checklstring(L, 1, &len); + lua_pushinteger(L, crc16_ccitt(data, len)); return 1; } -int lua_convert_to_rdscharset(lua_State *localL) { +int lua_convert_to_rdscharset(lua_State *L) { size_t len; - const char *input = luaL_checklstring(localL, 1, &len); + const char *input = luaL_checklstring(L, 1, &len); char output[len + 1]; convert_to_rdscharset(input, output, len + 1); - lua_pushstring(localL, output); + lua_pushstring(L, output); return 1; } -int lua_encode_group(lua_State *localL) { +int lua_encode_group(lua_State *L) { size_t len; - const char *grp = luaL_checklstring(localL, 1, &len); - if(len != 1) return luaL_error(localL, "expected a length of 1"); + const char *grp = luaL_checklstring(L, 1, &len); + if(len != 1) return luaL_error(L, "expected a length of 1"); char PS_GROUP = 0; RDSGroup group; uint8_t good = check_rds_good_group(enc, grp); if(good == 0) { - lua_pushboolean(localL, 0); + lua_pushboolean(L, 0); get_rds_sequence_group(enc, &group, 1, &PS_GROUP); } else { - lua_pushboolean(localL, 1); + lua_pushboolean(L, 1); get_rds_sequence_group(enc, &group, 1, grp); } - lua_pushinteger(localL, group.b); - lua_pushinteger(localL, group.c); - lua_pushinteger(localL, group.d); + lua_pushinteger(L, group.b); + lua_pushinteger(L, group.c); + lua_pushinteger(L, group.d); return 4; +} + +typedef void (*lua_push_fn)(lua_State *L, const void *value); +typedef void (*lua_pull_fn)(lua_State *L, int idx, void *value); + +static void push_int(lua_State *L, const void *value) { lua_pushinteger(L, *(const int *)value); } +static void push_bool(lua_State *L, const void *value) { lua_pushboolean(L, *(const int *)value); } +static void pull_int(lua_State *L, int idx, void *value) { *(int *)value = luaL_checkinteger(L, idx); } +static void pull_bool(lua_State *L, int idx, void *value) { *(int *)value = lua_toboolean(L, idx); } + +typedef struct { + const char *name; + int offset; + lua_push_fn push; + lua_pull_fn pull; +} field_t; + +static const field_t fields[] = { + {"pi", offsetof(RDSData, pi), push_int, pull_int}, + {"pty", offsetof(RDSData, pty), push_int, pull_int}, + {"ecc", offsetof(RDSData, ecc), push_int, pull_int}, + {"slc_data", offsetof(RDSData, slc_data), push_int, pull_int}, + {"ct", offsetof(RDSData, ct), push_bool, pull_bool}, + {"dpty", offsetof(RDSData, dpty), push_bool, pull_bool}, + {"tp", offsetof(RDSData, tp), push_bool, pull_bool}, + {"ta", offsetof(RDSData, ta), push_bool, pull_bool}, + {"rt_enabled", offsetof(RDSData, rt_enabled), push_bool, pull_bool}, + {"ptyn_enabled", offsetof(RDSData, ptyn_enabled), push_bool, pull_bool}, +}; + +int lua_rds__index(lua_State *L) { + const char *key = luaL_checkstring(L, 2); + + RDSData *data = &enc->data[writing_program]; + + for (size_t i = 0; i < sizeof(fields)/sizeof(fields[0]); i++) { + if (strcmp(key, fields[i].name) == 0) { + const void *ptr = (const char *)data + fields[i].offset; + fields[i].push(L, ptr); + return 1; + } + } + + lua_rawget(L, 1); + return 1; +} + +int lua_rds__newindex(lua_State *L) { + const char *key = luaL_checkstring(L, 2); + + RDSData *data = &enc->data[writing_program]; + + for (size_t i = 0; i < sizeof(fields)/sizeof(fields[0]); i++) { + if (strcmp(key, fields[i].name) == 0) { + void *ptr = (char *)data + fields[i].offset; + fields[i].pull(L, 3, ptr); + return 0; + } + } + + lua_rawset(L, 1); + return 0; } \ No newline at end of file diff --git a/src/lua_api.h b/src/lua_api.h index 88d4f43..1dc3772 100644 --- a/src/lua_api.h +++ b/src/lua_api.h @@ -6,84 +6,57 @@ #include "rds.h" #include "fs.h" -int lua_get_userdata(lua_State *localL); -int lua_get_userdata_offset(lua_State *localL); -int lua_set_userdata(lua_State *localL); -int lua_set_userdata_offset(lua_State *localL); -int lua_force_save(lua_State *localL); -int lua_set_rds_program_defaults(lua_State *localL); -int lua_reset_rds(lua_State *localL); +int lua_get_userdata(lua_State *L); +int lua_get_userdata_offset(lua_State *L); +int lua_set_userdata(lua_State *L); +int lua_set_userdata_offset(lua_State *L); +int lua_force_save(lua_State *L); +int lua_set_rds_program_defaults(lua_State *L); +int lua_reset_rds(lua_State *L); -int lua_set_rds_pi(lua_State *localL); -int lua_get_rds_pi(lua_State *localL); +int lua_set_rds_streams(lua_State *L); +int lua_get_rds_streams(lua_State *L); -int lua_set_rds_pty(lua_State *localL); -int lua_get_rds_pty(lua_State *localL); +int lua_set_rds_grp_sqc(lua_State *L); +int lua_get_rds_grp_sqc(lua_State *L); -int lua_set_rds_ecc(lua_State *localL); -int lua_get_rds_ecc(lua_State *localL); +int lua_set_rds_grp_sqc_rds2(lua_State *L); +int lua_get_rds_grp_sqc_rds2(lua_State *L); -int lua_set_rds_slc_data(lua_State *localL); -int lua_get_rds_slc_data(lua_State *localL); +int lua_set_rds_link(lua_State *L); +int lua_get_rds_link(lua_State *L); -int lua_set_rds_ct(lua_State *localL); -int lua_get_rds_ct(lua_State *localL); +int lua_set_rds_program(lua_State *L); +int lua_get_rds_program(lua_State *L); -int lua_set_rds_dpty(lua_State *localL); -int lua_get_rds_dpty(lua_State *localL); +int lua_set_rds_writing_program(lua_State *L); +int lua_get_rds_writing_program(lua_State *L); -int lua_set_rds_tp(lua_State *localL); -int lua_get_rds_tp(lua_State *localL); +int lua_put_rds_custom_group(lua_State *L); +int lua_put_rds2_custom_group(lua_State *L); -int lua_set_rds_ta(lua_State *localL); -int lua_get_rds_ta(lua_State *localL); +int lua_set_rds_lps(lua_State *L); +int lua_get_rds_lps(lua_State *L); +int lua_set_rds_af_group0(lua_State *L); +int lua_set_rds_rt(lua_State *L); +int lua_set_rds_rt_raw(lua_State *L); +int lua_set_rds_tps(lua_State *L); +int lua_set_rds_tps_raw(lua_State *L); +int lua_set_rds_ps(lua_State *L); +int lua_set_rds_ps_raw(lua_State *L); +int lua_set_rds_ptyn(lua_State *L); +int lua_set_rds_ptyn_raw(lua_State *L); +int lua_set_rds_grp_sqc(lua_State *L); -int lua_set_rds_rt_enabled(lua_State *localL); -int lua_get_rds_rt_enabled(lua_State *localL); +int lua_toggle_rt_ab(lua_State *L); -int lua_set_rds_ptyn_enabled(lua_State *localL); -int lua_get_rds_ptyn_enabled(lua_State *localL); +int lua_set_rds_eon(lua_State *L); +int lua_get_rds_eon(lua_State *L); -int lua_set_rds_streams(lua_State *localL); -int lua_get_rds_streams(lua_State *localL); +int lua_crc16(lua_State *L); -int lua_set_rds_grp_sqc(lua_State *localL); -int lua_get_rds_grp_sqc(lua_State *localL); +int lua_convert_to_rdscharset(lua_State *L); +int lua_encode_group(lua_State *L); -int lua_set_rds_grp_sqc_rds2(lua_State *localL); -int lua_get_rds_grp_sqc_rds2(lua_State *localL); - -int lua_set_rds_link(lua_State *localL); -int lua_get_rds_link(lua_State *localL); - -int lua_set_rds_program(lua_State *localL); -int lua_get_rds_program(lua_State *localL); - -int lua_set_rds_writing_program(lua_State *localL); -int lua_get_rds_writing_program(lua_State *localL); - -int lua_put_rds_custom_group(lua_State *localL); -int lua_put_rds2_custom_group(lua_State *localL); - -int lua_set_rds_lps(lua_State *localL); -int lua_get_rds_lps(lua_State *localL); -int lua_set_rds_af_group0(lua_State *localL); -int lua_set_rds_rt(lua_State *localL); -int lua_set_rds_rt_raw(lua_State *localL); -int lua_set_rds_tps(lua_State *localL); -int lua_set_rds_tps_raw(lua_State *localL); -int lua_set_rds_ps(lua_State *localL); -int lua_set_rds_ps_raw(lua_State *localL); -int lua_set_rds_ptyn(lua_State *localL); -int lua_set_rds_ptyn_raw(lua_State *localL); -int lua_set_rds_grp_sqc(lua_State *localL); - -int lua_toggle_rt_ab(lua_State *localL); - -int lua_set_rds_eon(lua_State *localL); -int lua_get_rds_eon(lua_State *localL); - -int lua_crc16(lua_State *localL); - -int lua_convert_to_rdscharset(lua_State *localL); -int lua_encode_group(lua_State *localL); \ No newline at end of file +int lua_rds__index(lua_State *L); +int lua_rds__newindex(lua_State *L); \ No newline at end of file diff --git a/src/lua_rds.c b/src/lua_rds.c index 9e0db44..7e7613e 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -3,38 +3,11 @@ #include "lua_api.h" RDSEncoder* enc = NULL; -lua_State *L = NULL; +lua_State *globalL = NULL; +int hooks_ref = LUA_NOREF; static pthread_mutex_t lua_mutex; -#define lua_registertotable(L,n,f) (lua_pushcfunction(L, (f)), lua_setfield(L, -2, (n))) -uint8_t init_lua(RDSEncoder* _enc) { - static int mutex_initialized = 0; - enc = _enc; - L = luaL_newstate(); - printf("Initializing %s\n", LUA_COPYRIGHT); - if(L == NULL) return 1; - - luaL_requiref(L, "_G", luaopen_base, 1); - luaL_requiref(L, LUA_STRLIBNAME, luaopen_string, 1); - luaL_requiref(L, LUA_TABLIBNAME, luaopen_table, 1); - luaL_requiref(L, LUA_UTF8LIBNAME, luaopen_utf8, 1); - luaL_requiref(L, LUA_COLIBNAME, luaopen_coroutine, 1); - luaL_requiref(L, LUA_MATHLIBNAME, luaopen_math, 1); - luaL_requiref(L, LUA_IOLIBNAME, luaopen_io, 1); - lua_pop(L, 6); - - lua_newtable(L); - lua_setglobal(L, "ext"); - - lua_newtable(L); - lua_setglobal(L, "on_inits"); - lua_newtable(L); - lua_setglobal(L, "on_starts"); - lua_newtable(L); - lua_setglobal(L, "on_states"); - lua_newtable(L); - lua_setglobal(L, "ticks"); - +int init_lua_userdata(lua_State* L) { lua_newtable(L); lua_registertotable(L, "get", lua_get_userdata); lua_registertotable(L, "get_offset", lua_get_userdata_offset); @@ -42,8 +15,10 @@ uint8_t init_lua(RDSEncoder* _enc) { lua_registertotable(L, "set_offset", lua_set_userdata_offset); lua_pushinteger(L, LUA_USER_DATA); lua_setfield(L, -2, "len"); - lua_setglobal(L, "userdata"); + return 1; +} +int init_lua_data(lua_State* L) { lua_newtable(L); lua_registertotable(L, "crc16", lua_crc16); lua_registertotable(L, "force_save", lua_force_save); @@ -58,8 +33,10 @@ uint8_t init_lua(RDSEncoder* _enc) { lua_registertotable(L, "set_writing_program", lua_set_rds_writing_program); lua_registertotable(L, "get_writing_program", lua_get_rds_writing_program); lua_registertotable(L, "encode_charset", lua_convert_to_rdscharset); - lua_setglobal(L, "dp"); + return 1; +} +int init_lua_hooks(lua_State* L) { lua_newtable(L); lua_newtable(L); lua_setfield(L, -2, "on_init"); @@ -75,88 +52,124 @@ uint8_t init_lua(RDSEncoder* _enc) { lua_setfield(L, -2, "rt_transmission"); lua_newtable(L); lua_setfield(L, -2, "ps_transmission"); - lua_setglobal(L, "hooks"); + return 1; +} +static int my_searcher(lua_State *L) { + const char *name = luaL_checkstring(L, 1); + + #define PREFIX "/etc/rds95/" + + char path[512]; + snprintf(path, sizeof(path), + PREFIX "%s.lua", + name); + + for (char *p = path + strlen(PREFIX); *p; p++) { + if (*p == '.') *p = '/'; + } + + FILE *f = fopen(path, "rb"); + if (!f) { + lua_pushfstring(L, "\n\tno file: %s", path); + return 1; + } + + fclose(f); + + if (luaL_loadfile(L, path) == LUA_OK) { + return 1; + } + + lua_pushfstring(L, "\n\terror loading: %s", path); + return 1; +} + +#define lua_registertotable(L,n,f) (lua_pushcfunction(L, (f)), lua_setfield(L, -2, (n))) +uint8_t init_lua(RDSEncoder* _enc) { + static int mutex_initialized = 0; + enc = _enc; + globalL = luaL_newstate(); + printf("Initializing %s\n", LUA_VERSION); + if(globalL == NULL) return 1; + + luaL_requiref(globalL, "_G", luaopen_base, 1); + luaL_requiref(globalL, LUA_STRLIBNAME, luaopen_string, 1); + luaL_requiref(globalL, LUA_TABLIBNAME, luaopen_table, 1); + luaL_requiref(globalL, LUA_UTF8LIBNAME, luaopen_utf8, 1); + luaL_requiref(globalL, LUA_COLIBNAME, luaopen_coroutine, 1); + luaL_requiref(globalL, LUA_MATHLIBNAME, luaopen_math, 1); + luaL_requiref(globalL, LUA_IOLIBNAME, luaopen_io, 1); + luaL_requiref(globalL, "userdata", init_lua_userdata, 1); + luaL_requiref(globalL, "Data", init_lua_data, 1); + lua_pop(globalL, 8); + + luaL_requiref(globalL, LUA_LOADLIBNAME, luaopen_package, 1); lua_newtable(L); + lua_pushcfunction(L, my_searcher); + lua_rawseti(L, -2, 1); + lua_setfield(L, -2, "searchers"); + lua_pop(L, 1); - lua_newtable(L); - lua_setfield(L, -2, "ext"); + luaL_requiref(globalL, "hooks", init_lua_hooks, 1); + hooks_ref = luaL_ref(globalL, LUA_REGISTRYINDEX); - lua_pushinteger(L, EONs); - lua_setfield(L, -2, "eon_count"); + lua_newtable(globalL); + lua_setglobal(globalL, "ext"); - lua_registertotable(L, "set_pi", lua_set_rds_pi); - lua_registertotable(L, "get_pi", lua_get_rds_pi); + lua_newtable(globalL); - lua_registertotable(L, "set_pty", lua_set_rds_pty); - lua_registertotable(L, "get_pty", lua_get_rds_pty); + lua_newtable(globalL); + lua_setfield(globalL, -2, "ext"); - lua_registertotable(L, "set_ecc", lua_set_rds_ecc); - lua_registertotable(L, "get_ecc", lua_get_rds_ecc); + lua_pushinteger(globalL, EONs); + lua_setfield(globalL, -2, "eon_count"); - lua_registertotable(L, "set_slc_data", lua_set_rds_slc_data); - lua_registertotable(L, "get_slc_data", lua_get_rds_slc_data); + lua_registertotable(globalL, "__index", lua_rds__index); + lua_registertotable(globalL, "__newindex", lua_rds__newindex); - lua_registertotable(L, "set_ct", lua_set_rds_ct); - lua_registertotable(L, "get_ct", lua_get_rds_ct); + lua_registertotable(globalL, "set_link", lua_set_rds_link); + lua_registertotable(globalL, "get_link", lua_get_rds_link); - lua_registertotable(L, "set_dpty", lua_set_rds_dpty); - lua_registertotable(L, "get_dpty", lua_get_rds_dpty); + lua_registertotable(globalL, "set_ptyn", lua_set_rds_ptyn); + lua_registertotable(globalL, "set_ptyn_raw", lua_set_rds_ptyn_raw); + lua_registertotable(globalL, "set_ps", lua_set_rds_ps); + lua_registertotable(globalL, "set_ps_raw", lua_set_rds_ps_raw); + lua_registertotable(globalL, "set_tps", lua_set_rds_tps); + lua_registertotable(globalL, "set_tps_raw", lua_set_rds_tps_raw); + lua_registertotable(globalL, "set_rt", lua_set_rds_rt); + lua_registertotable(globalL, "set_rt_raw", lua_set_rds_rt_raw); + lua_registertotable(globalL, "toggle_rt_ab", lua_toggle_rt_ab); - lua_registertotable(L, "set_tp", lua_set_rds_tp); - lua_registertotable(L, "get_tp", lua_get_rds_tp); + lua_registertotable(globalL, "set_lps", lua_set_rds_lps); + lua_registertotable(globalL, "get_lps", lua_get_rds_lps); - lua_registertotable(L, "set_ta", lua_set_rds_ta); - lua_registertotable(L, "get_ta", lua_get_rds_ta); + lua_registertotable(globalL, "set_grpseq", lua_set_rds_grp_sqc); + lua_registertotable(globalL, "get_grpseq", lua_get_rds_grp_sqc); - lua_registertotable(L, "set_rt_enabled", lua_set_rds_rt_enabled); - lua_registertotable(L, "get_rt_enabled", lua_get_rds_rt_enabled); + lua_registertotable(globalL, "put_custom_group", lua_put_rds_custom_group); + lua_registertotable(globalL, "put_rds2_custom_group", lua_put_rds2_custom_group); - lua_registertotable(L, "set_ptyn_enabled", lua_set_rds_ptyn_enabled); - lua_registertotable(L, "get_ptyn_enabled", lua_get_rds_ptyn_enabled); + lua_registertotable(globalL, "set_af", lua_set_rds_af_group0); - lua_registertotable(L, "set_link", lua_set_rds_link); - lua_registertotable(L, "get_link", lua_get_rds_link); + lua_registertotable(globalL, "set_eon", lua_set_rds_eon); + lua_registertotable(globalL, "get_eon", lua_get_rds_eon); - lua_registertotable(L, "set_ptyn", lua_set_rds_ptyn); - lua_registertotable(L, "set_ptyn_raw", lua_set_rds_ptyn_raw); - lua_registertotable(L, "set_ps", lua_set_rds_ps); - lua_registertotable(L, "set_ps_raw", lua_set_rds_ps_raw); - lua_registertotable(L, "set_tps", lua_set_rds_tps); - lua_registertotable(L, "set_tps_raw", lua_set_rds_tps_raw); - lua_registertotable(L, "set_rt", lua_set_rds_rt); - lua_registertotable(L, "set_rt_raw", lua_set_rds_rt_raw); - lua_registertotable(L, "toggle_rt_ab", lua_toggle_rt_ab); + lua_registertotable(globalL, "set_streams", lua_set_rds_streams); + lua_registertotable(globalL, "get_streams", lua_get_rds_streams); - lua_registertotable(L, "set_lps", lua_set_rds_lps); - lua_registertotable(L, "get_lps", lua_get_rds_lps); + lua_registertotable(globalL, "encode_group", lua_encode_group); - lua_registertotable(L, "set_grpseq", lua_set_rds_grp_sqc); - lua_registertotable(L, "get_grpseq", lua_get_rds_grp_sqc); + lua_setglobal(globalL, "rds"); - lua_registertotable(L, "put_custom_group", lua_put_rds_custom_group); - lua_registertotable(L, "put_rds2_custom_group", lua_put_rds2_custom_group); - - lua_registertotable(L, "set_af", lua_set_rds_af_group0); - - lua_registertotable(L, "set_eon", lua_set_rds_eon); - lua_registertotable(L, "get_eon", lua_get_rds_eon); - - lua_registertotable(L, "set_streams", lua_set_rds_streams); - lua_registertotable(L, "get_streams", lua_get_rds_streams); - - lua_registertotable(L, "encode_group", lua_encode_group); - - lua_setglobal(L, "rds"); - - if (luaL_loadfile(L, "/etc/rds95.lua") != LUA_OK) { - fprintf(stderr, "Lua error loading file: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); + if (luaL_loadfile(globalL, "/etc/rds95.lua") != LUA_OK) { + fprintf(stderr, "Lua error loading file: %s\n", lua_tostring(globalL, -1)); + lua_pop(globalL, 1); return 2; } else { - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - printf("Init error: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); + if (lua_pcall(globalL, 0, 0, 0) != LUA_OK) { + printf("Init error: %s\n", lua_tostring(globalL, -1)); + lua_pop(globalL, 1); } } if(mutex_initialized == 0) { @@ -170,124 +183,123 @@ uint8_t init_lua(RDSEncoder* _enc) { void run_lua(char *str, size_t str_len, char *cmd_output, size_t *out_len) { pthread_mutex_lock(&lua_mutex); - lua_getglobal(L, "hooks"); - lua_getfield(L, -1, "data_handle"); + lua_rawgeti(globalL, LUA_REGISTRYINDEX, hooks_ref); + lua_getfield(globalL, -1, "data_handle"); - if (lua_isfunction(L, -1)) { - lua_pushlstring(L, str, str_len); + if (lua_isfunction(globalL, -1)) { + lua_pushlstring(globalL, str, str_len); - if (lua_pcall(L, 1, 1, 0) == LUA_OK) { - if (lua_isstring(L, -1) && cmd_output) { - const char *lua_str = lua_tolstring(L, -1, out_len); + if (lua_pcall(globalL, 1, 1, 0) == LUA_OK) { + if (lua_isstring(globalL, -1) && cmd_output) { + const char *lua_str = lua_tolstring(globalL, -1, out_len); if (*out_len > 254) *out_len = 254; memcpy(cmd_output, lua_str, *out_len); } - } else fprintf(stderr, "Lua error: %s at 'data_handle'\n", lua_tostring(L, -1)); + } else fprintf(stderr, "Lua error: %s at 'data_handle'\n", lua_tostring(globalL, -1)); } else fprintf(stderr, "'data_handle' is not a function\n"); - lua_pop(L, 2); + lua_pop(globalL, 2); pthread_mutex_unlock(&lua_mutex); } int lua_group(RDSGroup* group, const char grp) { pthread_mutex_lock(&lua_mutex); - lua_getglobal(L, "hooks"); - lua_getfield(L, -1, "group"); + lua_rawgeti(globalL, LUA_REGISTRYINDEX, hooks_ref); + lua_getfield(globalL, -1, "group"); - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); + if (!lua_isfunction(globalL, -1)) { + lua_pop(globalL, 2); pthread_mutex_unlock(&lua_mutex); return 0; } - lua_pushlstring(L, &grp, 1); + lua_pushlstring(globalL, &grp, 1); - if (lua_pcall(L, 1, 4, 0) != LUA_OK) { - fprintf(stderr, "Lua error: %s\n", lua_tostring(L, -1)); - lua_pop(L, 2); + if (lua_pcall(globalL, 1, 4, 0) != LUA_OK) { + fprintf(stderr, "Lua error: %s\n", lua_tostring(globalL, -1)); + lua_pop(globalL, 2); pthread_mutex_unlock(&lua_mutex); return 0; } int success = 0; - if (lua_isboolean(L, -4) && lua_toboolean(L, -4) && - lua_isinteger(L, -3) && lua_isinteger(L, -2) && lua_isinteger(L, -1)) { - - group->b = (uint16_t)lua_tointeger(L, -3); - group->c = (uint16_t)lua_tointeger(L, -2); - group->d = (uint16_t)lua_tointeger(L, -1); + if (lua_isboolean(globalL, -4) && lua_toboolean(globalL, -4) && + lua_isinteger(globalL, -3) && lua_isinteger(globalL, -2) && lua_isinteger(globalL, -1)) { + group->b = (uint16_t)lua_tointeger(globalL, -3); + group->c = (uint16_t)lua_tointeger(globalL, -2); + group->d = (uint16_t)lua_tointeger(globalL, -1); success = 1; } - lua_pop(L, 5); + lua_pop(globalL, 5); pthread_mutex_unlock(&lua_mutex); return success; } int lua_rds2_group(RDSGroup* group, int stream) { pthread_mutex_lock(&lua_mutex); - lua_getglobal(L, "hooks"); - lua_getfield(L, -1, "rds2_group"); + lua_rawgeti(globalL, LUA_REGISTRYINDEX, hooks_ref); + lua_getfield(globalL, -1, "rds2_group"); - if (lua_isfunction(L, -1)) { - lua_pushinteger(L, stream); - if (lua_pcall(L, 1, 5, 0) == LUA_OK) { - if (!lua_isboolean(L, -5)) { - lua_pop(L, 6); + if (lua_isfunction(globalL, -1)) { + lua_pushinteger(globalL, stream); + if (lua_pcall(globalL, 1, 5, 0) == LUA_OK) { + if (!lua_isboolean(globalL, -5)) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - if (!lua_isinteger(L, -4)) { - lua_pop(L, 6); + if (!lua_isinteger(globalL, -4)) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - if (!lua_isinteger(L, -3)) { - lua_pop(L, 6); + if (!lua_isinteger(globalL, -3)) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - if (!lua_isinteger(L, -2)) { - lua_pop(L, 6); + if (!lua_isinteger(globalL, -2)) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - if (!lua_isinteger(L, -1)) { - lua_pop(L, 6); + if (!lua_isinteger(globalL, -1)) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - if(lua_toboolean(L, -5) == 0) { - lua_pop(L, 6); + if(lua_toboolean(globalL, -5) == 0) { + lua_pop(globalL, 6); pthread_mutex_unlock(&lua_mutex); return 0; } - group->a = lua_tointeger(L, -4); - group->b = lua_tointeger(L, -3); - group->c = lua_tointeger(L, -2); - group->d = lua_tointeger(L, -1); - lua_pop(L, 6); + group->a = lua_tointeger(globalL, -4); + group->b = lua_tointeger(globalL, -3); + group->c = lua_tointeger(globalL, -2); + group->d = lua_tointeger(globalL, -1); + lua_pop(globalL, 6); } else { - fprintf(stderr, "Lua error: %s at 'rds2_group'\n", lua_tostring(L, -1)); - lua_pop(L, 2); + fprintf(stderr, "Lua error: %s at 'rds2_group'\n", lua_tostring(globalL, -1)); + lua_pop(globalL, 2); } - } else lua_pop(L, 2); + } else lua_pop(globalL, 2); pthread_mutex_unlock(&lua_mutex); return 1; } void lua_call_function_nolock(const char* function) { - lua_getglobal(L, function); + lua_getglobal(globalL, function); - if (lua_isfunction(L, -1)) { - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - fprintf(stderr, "Lua error: %s at '%s'\n", lua_tostring(L, -1), function); - lua_pop(L, 1); + if (lua_isfunction(globalL, -1)) { + if (lua_pcall(globalL, 0, 0, 0) != LUA_OK) { + fprintf(stderr, "Lua error: %s at '%s'\n", lua_tostring(globalL, -1), function); + lua_pop(globalL, 1); } - } else lua_pop(L, 1); + } else lua_pop(globalL, 1); } void lua_call_function(const char* function) { int need_lock = (pthread_mutex_trylock(&lua_mutex) == 0); @@ -300,31 +312,31 @@ void lua_call_function(const char* function) { } void lua_call_tfunction_nolock(const char* name) { - lua_getglobal(L, "hooks"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); + lua_rawgeti(globalL, LUA_REGISTRYINDEX, hooks_ref); + if (!lua_istable(globalL, -1)) { + lua_pop(globalL, 1); return; } - lua_getfield(L, -1, name); + lua_getfield(globalL, -1, name); - if (!lua_istable(L, -1)) { - lua_pop(L, 2); + if (!lua_istable(globalL, -1)) { + lua_pop(globalL, 2); return; } - lua_Integer len = lua_rawlen(L, -1); + lua_Integer len = lua_rawlen(globalL, -1); for (lua_Integer i = 1; i <= len; i++) { - lua_rawgeti(L, -1, i); - if (lua_isfunction(L, -1)) { - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + lua_rawgeti(globalL, -1, i); + if (lua_isfunction(globalL, -1)) { + if (lua_pcall(globalL, 0, 0, 0) != LUA_OK) { fprintf(stderr, "Lua error: %s at '%s[%lld]'\n", - lua_tostring(L, -1), name, (long long)i); - lua_pop(L, 1); + lua_tostring(globalL, -1), name, (long long)i); + lua_pop(globalL, 1); } - } else lua_pop(L, 1); + } else lua_pop(globalL, 1); } - lua_pop(L, 2); // pop table + lua_pop(globalL, 2); // pop table } void lua_call_tfunction(const char* name) { @@ -338,9 +350,10 @@ void lua_call_tfunction(const char* name) { } void destroy_lua() { - if (L) { - lua_close(L); - L = NULL; + if (globalL) { + luaL_unref(globalL, LUA_REGISTRYINDEX, hooks_ref); + lua_close(globalL); + globalL = NULL; } enc = NULL; pthread_mutex_destroy(&lua_mutex);