mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-02-26 20:33:53 +01:00
lua!
This commit is contained in:
50
src/lua_rds.c
Normal file
50
src/lua_rds.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "lua_rds.h"
|
||||
|
||||
static RDSModulator* mod = NULL;
|
||||
static lua_State *L = NULL;
|
||||
|
||||
int lua_set_rds_pi(lua_State *localL) {
|
||||
int pi_value = luaL_checkinteger(localL, 1);
|
||||
mod->enc->data[mod->enc->program].pi = pi_value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void init_lua(RDSModulator* rds_mod) {
|
||||
mod = rds_mod;
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
lua_register(L, "set_rds_pi", lua_set_rds_pi);
|
||||
}
|
||||
|
||||
void run_lua(char *str, char *cmd_output) {
|
||||
lua_pushstring(L, str);
|
||||
lua_setglobal(L, "cmd");
|
||||
|
||||
int top = lua_gettop(L);
|
||||
|
||||
char path[128];
|
||||
snprintf(path, sizeof(path), "%s/.command95.lua", getenv("HOME"));
|
||||
if (luaL_dofile(L, path) != LUA_OK) {
|
||||
const char *err = lua_tostring(L, -1);
|
||||
fprintf(stderr, "Lua error: %s\n", err);
|
||||
lua_pop(L, 1);
|
||||
lua_settop(L, top);
|
||||
return;
|
||||
}
|
||||
|
||||
lua_getglobal(L, "cmd_output");
|
||||
|
||||
if (lua_isstring(L, -1)) {
|
||||
const char * message = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
if(cmd_output) strcpy(cmd_output, message);
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_lua(void) {
|
||||
if (L) {
|
||||
lua_close(L);
|
||||
L = NULL;
|
||||
}
|
||||
mod = NULL;
|
||||
}
|
||||
11
src/lua_rds.h
Normal file
11
src/lua_rds.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
#include <rds.h>
|
||||
#include <modulator.h>
|
||||
|
||||
int lua_set_rds_pi(lua_State *L);
|
||||
void init_lua(RDSModulator* rds_mod);
|
||||
void run_lua(char *str, char *cmd_output);
|
||||
void destroy_lua();
|
||||
@@ -31,6 +31,7 @@ int open_udp_server(int port, RDSModulator* rds_mod) {
|
||||
poller.events = POLLIN;
|
||||
|
||||
mod = rds_mod;
|
||||
init_lua(rds_mod);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -60,6 +61,7 @@ void poll_udp_server() {
|
||||
|
||||
memset(cmd_output, 0, BUF_SIZE);
|
||||
process_ascii_cmd(mod, cmd_buf, cmd_output);
|
||||
run_lua(cmd_buf, NULL);
|
||||
|
||||
size_t out_len = strlen(cmd_output);
|
||||
if (out_len > 0) {
|
||||
@@ -74,4 +76,5 @@ void poll_udp_server() {
|
||||
void close_udp_server() {
|
||||
if (sockfd >= 0) close(sockfd);
|
||||
sockfd = -1;
|
||||
destroy_lua();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <netinet/in.h>
|
||||
#include "modulator.h"
|
||||
#include "ascii_cmd.h"
|
||||
#include "lua_rds.h"
|
||||
|
||||
int open_udp_server(int port, RDSModulator *rds_mod);
|
||||
void poll_udp_server();
|
||||
|
||||
Reference in New Issue
Block a user