mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-07-29 16:29:17 +02:00
big lua api changes
This commit is contained in:
+208
-192
@@ -1,53 +1,55 @@
|
||||
#include "lua_api.h"
|
||||
#include "lua_rds.h"
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
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;
|
||||
}
|
||||
+41
-68
@@ -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);
|
||||
int lua_rds__index(lua_State *L);
|
||||
int lua_rds__newindex(lua_State *L);
|
||||
+178
-165
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user