diff --git a/plugin.lua b/plugin.lua index dd18952..3d65f60 100644 --- a/plugin.lua +++ b/plugin.lua @@ -22,6 +22,10 @@ function reset_rds() end ---It should be defined by the user in the script. ---@return nil function on_init() end +---This function is called by the C core after we initialize the encoder (always, every start) +---It should be defined by the user in the script. +---@return nil +function on_start() end ---@param pi integer function set_rds_pi(pi) end diff --git a/src/lua_rds.c b/src/lua_rds.c index 5c52723..73482f1 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -6,7 +6,7 @@ static lua_State *L = NULL; int lua_set_rds_program_defaults(lua_State *localL) { (void)localL; set_rds_defaults(mod->enc, mod->enc->program); - lua_on_init(); + lua_call_function("on_init"); return 0; } @@ -535,7 +535,7 @@ void run_lua(char *str, char *cmd_output) { } } -void lua_on_init() { +void lua_call_function(const char* function) { char path[128]; snprintf(path, sizeof(path), "%s/.rds95.command.lua", getenv("HOME")); @@ -556,17 +556,14 @@ void lua_on_init() { return; } - lua_getglobal(L, "on_init"); + lua_getglobal(L, function); if (lua_isfunction(L, -1)) { if (lua_pcall(L, 0, 0, 0) != LUA_OK) { fprintf(stderr, "Lua error running 'on_init': %s\n", lua_tostring(L, -1)); lua_pop(L, 1); } - } else { - // printf("Note: 'on_init' function not found in Lua script. Skipping.\n"); - lua_pop(L, 1); - } + } else lua_pop(L, 1); } void destroy_lua(void) { diff --git a/src/lua_rds.h b/src/lua_rds.h index ca440b2..7c83047 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -8,5 +8,5 @@ void init_lua(RDSModulator* rds_mod); void run_lua(char *str, char *cmd_output); -void lua_on_init(); +void lua_call_function(const char* function); void destroy_lua(); \ No newline at end of file diff --git a/src/rds.c b/src/rds.c index 04ee6e8..eff1826 100644 --- a/src/rds.c +++ b/src/rds.c @@ -381,8 +381,9 @@ void init_rds_encoder(RDSEncoder* enc) { if (encoder_loadFromFile(enc)) { printf("Encoder file will be reinitialized.\n"); - lua_on_init(); + lua_call_function("on_init"); } encoder_saveToFile(enc); for(int i = 0; i < PROGRAMS; i++) reset_rds_state(enc, i); + lua_call_function("on_start"); } \ No newline at end of file