]> git.sesse.net Git - vlc/blob - modules/misc/lua/services_discovery.c
467a4aae4a34f98dfd629ebfa333d29b56c87e65
[vlc] / modules / misc / lua / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Services discovery using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2009 VideoLAN and AUTHORS
5  *
6  * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_services_discovery.h>
29
30 #include "vlc.h"
31 #include "libs.h"
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static void *Run( void * );
37
38 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
39
40 /*****************************************************************************
41  * Local structures
42  *****************************************************************************/
43 struct services_discovery_sys_t
44 {
45     lua_State *L;
46     char *psz_filename;
47     vlc_thread_t thread;
48 };
49 static const luaL_Reg p_reg[] = { { NULL, NULL } };
50
51 /*****************************************************************************
52  * Open: initialize and create stuff
53  *****************************************************************************/
54 int Open_LuaSD( vlc_object_t *p_this )
55 {
56     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
57     services_discovery_sys_t *p_sys;
58     lua_State *L = NULL;
59     char *psz_name = NULL;
60
61     if( !strcmp(p_sd->psz_name, "lua"))
62     {
63         // We want to load the module name "lua"
64         // This module can be used to load lua script not registered
65         // as builtin lua SD modules.
66         config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
67         psz_name = var_CreateGetString( p_sd, "lua-sd" );
68     }
69     else
70     {
71         // We are loading a builtin lua sd module.
72         psz_name = strdup(p_sd->psz_name);
73     }
74
75     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
76     {
77         free( psz_name );
78         return VLC_ENOMEM;
79     }
80     p_sd->p_sys = p_sys;
81     p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
82     if( !p_sys->psz_filename )
83     {
84         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
85                  psz_name );
86         free( psz_name );
87         goto error;
88     }
89     free( psz_name );
90     L = luaL_newstate();
91     if( !L )
92     {
93         msg_Err( p_sd, "Could not create new Lua State" );
94         goto error;
95     }
96     vlclua_set_this( L, p_sd );
97     luaL_openlibs( L );
98     luaL_register( L, "vlc", p_reg );
99     luaopen_input( L );
100     luaopen_msg( L );
101     luaopen_misc( L );
102     luaopen_net( L );
103     luaopen_object( L );
104     luaopen_sd( L );
105     luaopen_strings( L );
106     luaopen_variables( L );
107     luaopen_stream( L );
108     luaopen_gettext( L );
109     luaopen_xml( L );
110     luaopen_md5( L );
111     lua_pop( L, 1 );
112
113     if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
114     {
115         msg_Warn( p_sd, "Error while setting the module search path for %s",
116                   p_sys->psz_filename );
117         goto error;
118     }
119     if( luaL_dofile( L, p_sys->psz_filename ) )
120     {
121
122         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
123                   lua_tostring( L, lua_gettop( L ) ) );
124         lua_pop( L, 1 );
125         goto error;
126     }
127     p_sys->L = L;
128     if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
129     {
130         goto error;
131     }
132     return VLC_SUCCESS;
133 error:
134     if( L )
135         lua_close( L );
136     free( p_sys->psz_filename );
137     free( p_sys );
138     return VLC_EGENERIC;
139 }
140
141 /*****************************************************************************
142  * Close: cleanup
143  *****************************************************************************/
144 void Close_LuaSD( vlc_object_t *p_this )
145 {
146     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
147
148     vlc_join (p_sd->p_sys->thread, NULL);
149     free( p_sd->p_sys->psz_filename );
150     lua_close( p_sd->p_sys->L );
151     free( p_sd->p_sys );
152 }
153
154 /*****************************************************************************
155  * Run: Thread entry-point
156  ****************************************************************************/
157 static void* Run( void *data )
158 {
159     services_discovery_t *p_sd = ( services_discovery_t * )data;
160     services_discovery_sys_t *p_sys = p_sd->p_sys;
161     lua_State *L = p_sys->L;
162
163     lua_getglobal( L, "main" );
164     if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
165     {
166         msg_Err( p_sd, "Error while running script %s, "
167                   "function main(): %s", p_sys->psz_filename,
168                   lua_tostring( L, lua_gettop( L ) ) );
169         lua_pop( L, 1 );
170         return NULL;
171     }
172     msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
173
174     /* Force garbage collection, because the core will keep the SD
175      * open, but lua will never gc until lua_close(). */
176     lua_gc( L, LUA_GCCOLLECT, 0 );
177
178     return NULL;
179 }