| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
#ifndef _GNU_SOURCE |
|---|
| 28 |
# define _GNU_SOURCE |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#ifdef HAVE_CONFIG_H |
|---|
| 32 |
# include "config.h" |
|---|
| 33 |
#endif |
|---|
| 34 |
|
|---|
| 35 |
#include <vlc_common.h> |
|---|
| 36 |
#include <vlc_meta.h> |
|---|
| 37 |
#include <vlc_charset.h> |
|---|
| 38 |
|
|---|
| 39 |
#include <vlc_interface.h> |
|---|
| 40 |
#include <vlc_playlist.h> |
|---|
| 41 |
#include <vlc_aout.h> |
|---|
| 42 |
|
|---|
| 43 |
#include <lua.h> |
|---|
| 44 |
#include <lauxlib.h> |
|---|
| 45 |
#include <lualib.h> |
|---|
| 46 |
|
|---|
| 47 |
#include "vlc.h" |
|---|
| 48 |
#include "libs.h" |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
struct intf_sys_t |
|---|
| 54 |
{ |
|---|
| 55 |
char *psz_filename; |
|---|
| 56 |
lua_State *L; |
|---|
| 57 |
}; |
|---|
| 58 |
|
|---|
| 59 |
static void Run( intf_thread_t *p_intf ); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
static char *FindFile( const char *psz_name ) |
|---|
| 65 |
{ |
|---|
| 66 |
char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL }; |
|---|
| 67 |
char **ppsz_dir; |
|---|
| 68 |
vlclua_dir_list( "intf", ppsz_dir_list ); |
|---|
| 69 |
for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ ) |
|---|
| 70 |
{ |
|---|
| 71 |
char *psz_filename; |
|---|
| 72 |
FILE *fp; |
|---|
| 73 |
if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir, |
|---|
| 74 |
psz_name ) < 0 ) |
|---|
| 75 |
{ |
|---|
| 76 |
vlclua_dir_list_free( ppsz_dir_list ); |
|---|
| 77 |
return NULL; |
|---|
| 78 |
} |
|---|
| 79 |
fp = fopen( psz_filename, "r" ); |
|---|
| 80 |
if( fp ) |
|---|
| 81 |
{ |
|---|
| 82 |
fclose( fp ); |
|---|
| 83 |
vlclua_dir_list_free( ppsz_dir_list ); |
|---|
| 84 |
return psz_filename; |
|---|
| 85 |
} |
|---|
| 86 |
free( psz_filename ); |
|---|
| 87 |
} |
|---|
| 88 |
vlclua_dir_list_free( ppsz_dir_list ); |
|---|
| 89 |
return NULL; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
static inline void luaL_register_submodule( lua_State *L, const char *psz_name, |
|---|
| 93 |
const luaL_Reg *l ) |
|---|
| 94 |
{ |
|---|
| 95 |
lua_newtable( L ); |
|---|
| 96 |
luaL_register( L, NULL, l ); |
|---|
| 97 |
lua_setfield( L, -2, psz_name ); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
static const struct |
|---|
| 101 |
{ |
|---|
| 102 |
const char *psz_shortcut; |
|---|
| 103 |
const char *psz_name; |
|---|
| 104 |
} pp_shortcuts[] = { |
|---|
| 105 |
{ "luarc", "rc" }, |
|---|
| 106 |
|
|---|
| 107 |
{ "luahotkeys", "hotkeys" }, |
|---|
| 108 |
|
|---|
| 109 |
{ "luatelnet", "telnet" }, |
|---|
| 110 |
|
|---|
| 111 |
{ "luahttp", "http" }, |
|---|
| 112 |
|
|---|
| 113 |
{ NULL, NULL } }; |
|---|
| 114 |
|
|---|
| 115 |
static bool WordInList( const char *psz_list, const char *psz_word ) |
|---|
| 116 |
{ |
|---|
| 117 |
const char *psz_str = strstr( psz_list, psz_word ); |
|---|
| 118 |
int i_len = strlen( psz_word ); |
|---|
| 119 |
while( psz_str ) |
|---|
| 120 |
{ |
|---|
| 121 |
if( (psz_str == psz_list || *(psz_str-1) == ',' ) |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
&& ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) ) |
|---|
| 125 |
return true; |
|---|
| 126 |
psz_str = strstr( psz_str, psz_word ); |
|---|
| 127 |
} |
|---|
| 128 |
return false; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
static char *GetModuleName( intf_thread_t *p_intf ) |
|---|
| 132 |
{ |
|---|
| 133 |
int i; |
|---|
| 134 |
const char *psz_intf; |
|---|
| 135 |
if( *p_intf->psz_intf == '$' ) |
|---|
| 136 |
psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 ); |
|---|
| 137 |
else |
|---|
| 138 |
psz_intf = p_intf->psz_intf; |
|---|
| 139 |
for( i = 0; pp_shortcuts[i].psz_name; i++ ) |
|---|
| 140 |
{ |
|---|
| 141 |
if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) |
|---|
| 142 |
return strdup( pp_shortcuts[i].psz_name ); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
return config_GetPsz( p_intf, "lua-intf" ); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
static const luaL_Reg p_reg[] = { { NULL, NULL } }; |
|---|
| 149 |
|
|---|
| 150 |
int Open_LuaIntf( vlc_object_t *p_this ) |
|---|
| 151 |
{ |
|---|
| 152 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 153 |
intf_sys_t *p_sys; |
|---|
| 154 |
lua_State *L; |
|---|
| 155 |
|
|---|
| 156 |
char *psz_name = GetModuleName( p_intf ); |
|---|
| 157 |
const char *psz_config; |
|---|
| 158 |
bool b_config_set = false; |
|---|
| 159 |
if( !psz_name ) psz_name = strdup( "dummy" ); |
|---|
| 160 |
|
|---|
| 161 |
p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) ); |
|---|
| 162 |
if( !p_intf->p_sys ) |
|---|
| 163 |
{ |
|---|
| 164 |
free( psz_name ); |
|---|
| 165 |
return VLC_ENOMEM; |
|---|
| 166 |
} |
|---|
| 167 |
p_sys = p_intf->p_sys; |
|---|
| 168 |
p_sys->psz_filename = FindFile( psz_name ); |
|---|
| 169 |
if( !p_sys->psz_filename ) |
|---|
| 170 |
{ |
|---|
| 171 |
msg_Err( p_intf, "Couldn't find lua interface script \"%s\".", |
|---|
| 172 |
psz_name ); |
|---|
| 173 |
free( psz_name ); |
|---|
| 174 |
free( p_sys ); |
|---|
| 175 |
return VLC_EGENERIC; |
|---|
| 176 |
} |
|---|
| 177 |
msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename ); |
|---|
| 178 |
|
|---|
| 179 |
L = luaL_newstate(); |
|---|
| 180 |
if( !L ) |
|---|
| 181 |
{ |
|---|
| 182 |
msg_Err( p_intf, "Could not create new Lua State" ); |
|---|
| 183 |
free( psz_name ); |
|---|
| 184 |
free( p_sys ); |
|---|
| 185 |
return VLC_EGENERIC; |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
luaL_openlibs( L ); |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
luaL_register( L, "vlc", p_reg ); |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
lua_pushlightuserdata( L, p_intf ); |
|---|
| 195 |
lua_setfield( L, -2, "private" ); |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
luaopen_acl( L ); |
|---|
| 199 |
luaopen_config( L ); |
|---|
| 200 |
luaopen_volume( L ); |
|---|
| 201 |
luaopen_httpd( L ); |
|---|
| 202 |
luaopen_input( L ); |
|---|
| 203 |
luaopen_msg( L ); |
|---|
| 204 |
luaopen_misc( L ); |
|---|
| 205 |
luaopen_net( L ); |
|---|
| 206 |
luaopen_object( L ); |
|---|
| 207 |
luaopen_osd( L ); |
|---|
| 208 |
luaopen_playlist( L ); |
|---|
| 209 |
luaopen_sd( L ); |
|---|
| 210 |
luaopen_stream( L ); |
|---|
| 211 |
luaopen_strings( L ); |
|---|
| 212 |
luaopen_variables( L ); |
|---|
| 213 |
luaopen_video( L ); |
|---|
| 214 |
luaopen_vlm( L ); |
|---|
| 215 |
luaopen_volume( L ); |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
lua_pop( L, 1 ); |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
{ |
|---|
| 223 |
char *psz_command; |
|---|
| 224 |
char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR); |
|---|
| 225 |
*psz_char = '\0'; |
|---|
| 226 |
|
|---|
| 227 |
if( asprintf( &psz_command, |
|---|
| 228 |
"package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path", |
|---|
| 229 |
p_sys->psz_filename ) < 0 ) |
|---|
| 230 |
return VLC_EGENERIC; |
|---|
| 231 |
*psz_char = DIR_SEP_CHAR; |
|---|
| 232 |
if( luaL_dostring( L, psz_command ) ) |
|---|
| 233 |
return VLC_EGENERIC; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
psz_config = config_GetPsz( p_intf, "lua-config" ); |
|---|
| 238 |
if( psz_config && *psz_config ) |
|---|
| 239 |
{ |
|---|
| 240 |
char *psz_buffer; |
|---|
| 241 |
if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 ) |
|---|
| 242 |
{ |
|---|
| 243 |
printf("%s\n", psz_buffer); |
|---|
| 244 |
if( luaL_dostring( L, psz_buffer ) == 1 ) |
|---|
| 245 |
msg_Err( p_intf, "Error while parsing \"lua-config\"." ); |
|---|
| 246 |
free( psz_buffer ); |
|---|
| 247 |
lua_getglobal( L, "config" ); |
|---|
| 248 |
if( lua_istable( L, -1 ) ) |
|---|
| 249 |
{ |
|---|
| 250 |
lua_getfield( L, -1, psz_name ); |
|---|
| 251 |
if( lua_istable( L, -1 ) ) |
|---|
| 252 |
{ |
|---|
| 253 |
lua_setglobal( L, "config" ); |
|---|
| 254 |
b_config_set = true; |
|---|
| 255 |
} |
|---|
| 256 |
} |
|---|
| 257 |
} |
|---|
| 258 |
} |
|---|
| 259 |
if( b_config_set == false ) |
|---|
| 260 |
{ |
|---|
| 261 |
lua_newtable( L ); |
|---|
| 262 |
lua_setglobal( L, "config" ); |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
p_sys->L = L; |
|---|
| 266 |
|
|---|
| 267 |
p_intf->pf_run = Run; |
|---|
| 268 |
p_intf->psz_header = strdup( psz_name ); |
|---|
| 269 |
|
|---|
| 270 |
free( psz_name ); |
|---|
| 271 |
return VLC_SUCCESS; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
void Close_LuaIntf( vlc_object_t *p_this ) |
|---|
| 275 |
{ |
|---|
| 276 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 277 |
|
|---|
| 278 |
lua_close( p_intf->p_sys->L ); |
|---|
| 279 |
free( p_intf->p_sys ); |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
static void Run( intf_thread_t *p_intf ) |
|---|
| 283 |
{ |
|---|
| 284 |
int canc = vlc_savecancel( ); |
|---|
| 285 |
lua_State *L = p_intf->p_sys->L; |
|---|
| 286 |
|
|---|
| 287 |
if( luaL_dofile( L, p_intf->p_sys->psz_filename ) ) |
|---|
| 288 |
{ |
|---|
| 289 |
msg_Err( p_intf, "Error loading script %s: %s", |
|---|
| 290 |
p_intf->p_sys->psz_filename, |
|---|
| 291 |
lua_tostring( L, lua_gettop( L ) ) ); |
|---|
| 292 |
lua_pop( L, 1 ); |
|---|
| 293 |
} |
|---|
| 294 |
vlc_restorecancel( canc ); |
|---|
| 295 |
} |
|---|