]> git.sesse.net Git - vlc/blob - modules/misc/lua/services_discovery.c
d37133de265bcd7a876876efa9893922d2e63e9f
[vlc] / modules / misc / lua / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Services discovery using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Fabio Ritrovato <exsephiroth87 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 #include <vlc_playlist.h>
30
31 #include "vlc.h"
32 #include "libs.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static void *Run( void * );
38
39 static const char * const ppsz_sd_options[] = { "sd", NULL };
40
41 /*****************************************************************************
42  * Local structures
43  *****************************************************************************/
44 struct services_discovery_sys_t
45 {
46     lua_State *L;
47     char *psz_filename;
48     vlc_thread_t thread;
49 };
50 static const luaL_Reg p_reg[] = { { NULL, NULL } };
51
52 /*****************************************************************************
53  *
54  *****************************************************************************/
55 static char *FindFile( vlc_object_t *p_this, const char *psz_name )
56 {
57     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
58     char **ppsz_dir;
59     vlclua_dir_list( p_this, "sd", ppsz_dir_list );
60     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
61     {
62         char *psz_filename;
63         FILE *fp;
64         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
65                       psz_name ) < 0 )
66         {
67             vlclua_dir_list_free( ppsz_dir_list );
68             return NULL;
69         }
70         fp = utf8_fopen( psz_filename, "r" );
71         if( fp )
72         {
73             fclose( fp );
74             vlclua_dir_list_free( ppsz_dir_list );
75             return psz_filename;
76         }
77         free( psz_filename );
78     }
79     vlclua_dir_list_free( ppsz_dir_list );
80     return NULL;
81 }
82
83 /*****************************************************************************
84  * Open: initialize and create stuff
85  *****************************************************************************/
86 int Open_LuaSD( vlc_object_t *p_this )
87 {
88     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
89     services_discovery_sys_t *p_sys;
90     lua_State *L;
91     char *psz_name = NULL;
92
93     config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
94     psz_name = var_CreateGetString( p_sd, "lua-sd" );
95     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
96         return VLC_ENOMEM;
97     p_sd->p_sys = p_sys;
98     p_sys->psz_filename = FindFile( p_this, psz_name );
99     if( !p_sys->psz_filename )
100     {
101         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
102                  psz_name );
103         free( psz_name );
104         free( p_sys );
105         return VLC_EGENERIC;
106     }
107     free( psz_name );
108     L = luaL_newstate();
109     if( !L )
110     {
111         msg_Err( p_sd, "Could not create new Lua State" );
112         free( p_sys->psz_filename );
113         free( p_sys );
114         return VLC_EGENERIC;
115     }
116     luaL_openlibs( L );
117     luaL_register( L, "vlc", p_reg );
118     lua_pushlightuserdata( L, p_sd );
119     lua_setfield( L, -2, "private" );
120     luaopen_input( L );
121     luaopen_msg( L );
122     luaopen_misc( L );
123     luaopen_net( L );
124     luaopen_object( L );
125     luaopen_playlist( L );
126     luaopen_sd( L );
127     luaopen_strings( L );
128     luaopen_variables( L );
129     luaopen_stream( L );
130     lua_pop( L, 1 );
131     if( luaL_dofile( L, p_sys->psz_filename ) )
132     {
133
134         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
135                   lua_tostring( L, lua_gettop( L ) ) );
136         lua_pop( L, 1 );
137         free( p_sys->psz_filename );
138         free( p_sys );
139         return VLC_EGENERIC;
140     }
141     p_sys->L = L;
142     if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
143     {
144         free( p_sys->psz_filename );
145         free( p_sys );
146         return VLC_EGENERIC;
147     }
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * Close: cleanup
153  *****************************************************************************/
154 void Close_LuaSD( vlc_object_t *p_this )
155 {
156     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
157
158     vlc_join (p_sd->p_sys->thread, NULL);
159     free( p_sd->p_sys->psz_filename );
160     lua_close( p_sd->p_sys->L );
161     free( p_sd->p_sys );
162 }
163
164 /*****************************************************************************
165  * Run: Thread entry-point
166  ****************************************************************************/
167 static void* Run( void *data )
168 {
169     services_discovery_t *p_sd = ( services_discovery_t * )data;
170     services_discovery_sys_t *p_sys = p_sd->p_sys;
171     lua_State *L = p_sys->L;
172
173     lua_getglobal( L, "main" );
174     if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
175     {
176         msg_Err( p_sd, "Error while runing script %s, "
177                   "function main(): %s", p_sys->psz_filename,
178                   lua_tostring( L, lua_gettop( L ) ) );
179         lua_pop( L, 1 );
180         return NULL;
181     }
182     //msg_Info( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );
183     return NULL;
184 }