1 /*****************************************************************************
2 * services_discovery.c : Services discovery using lua scripts
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
6 * Authors: Fabio Ritrovato <exsephiroth87 at videolan dot org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_services_discovery.h>
29 #include <vlc_playlist.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static void *Run( void * );
39 static const char * const ppsz_sd_options[] = { "sd", NULL };
41 /*****************************************************************************
43 *****************************************************************************/
44 struct services_discovery_sys_t
50 static const luaL_Reg p_reg[] = { { NULL, NULL } };
52 /*****************************************************************************
53 * Open: initialize and create stuff
54 *****************************************************************************/
55 int Open_LuaSD( vlc_object_t *p_this )
57 services_discovery_t *p_sd = ( services_discovery_t * )p_this;
58 services_discovery_sys_t *p_sys;
60 char *psz_name = NULL;
62 config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
63 psz_name = var_CreateGetString( p_sd, "lua-sd" );
64 if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
67 p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
68 if( !p_sys->psz_filename )
70 msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
80 msg_Err( p_sd, "Could not create new Lua State" );
81 free( p_sys->psz_filename );
86 luaL_register( L, "vlc", p_reg );
87 lua_pushlightuserdata( L, p_sd );
88 lua_setfield( L, -2, "private" );
94 luaopen_playlist( L );
97 luaopen_variables( L );
100 if( luaL_dofile( L, p_sys->psz_filename ) )
103 msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
104 lua_tostring( L, lua_gettop( L ) ) );
106 free( p_sys->psz_filename );
111 if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
113 free( p_sys->psz_filename );
120 /*****************************************************************************
122 *****************************************************************************/
123 void Close_LuaSD( vlc_object_t *p_this )
125 services_discovery_t *p_sd = ( services_discovery_t * )p_this;
127 vlc_join (p_sd->p_sys->thread, NULL);
128 free( p_sd->p_sys->psz_filename );
129 lua_close( p_sd->p_sys->L );
133 /*****************************************************************************
134 * Run: Thread entry-point
135 ****************************************************************************/
136 static void* Run( void *data )
138 services_discovery_t *p_sd = ( services_discovery_t * )data;
139 services_discovery_sys_t *p_sys = p_sd->p_sys;
140 lua_State *L = p_sys->L;
142 lua_getglobal( L, "main" );
143 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
145 msg_Err( p_sd, "Error while runing script %s, "
146 "function main(): %s", p_sys->psz_filename,
147 lua_tostring( L, lua_gettop( L ) ) );
151 //msg_Info( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );