]> git.sesse.net Git - vlc/blob - modules/lua/services_discovery.c
enable the macosx GUI to handle negative stop-time
[vlc] / modules / lua / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Services discovery using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2010 VideoLAN and AUTHORS
5  *
6  * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7  *          RĂ©mi Duraffort <ivoire at videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30
31 #include "vlc.h"
32 #include "libs.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static void *Run( void * );
38 static int DoSearch( services_discovery_t *p_sd, const char *psz_query );
39 static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * );
40 static int Control( services_discovery_t *p_sd, int i_command, va_list args );
41
42 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
43
44 /*****************************************************************************
45  * Local structures
46  *****************************************************************************/
47 struct services_discovery_sys_t
48 {
49     lua_State *L;
50     char *psz_filename;
51
52     vlc_thread_t thread;
53     vlc_mutex_t lock;
54     vlc_cond_t cond;
55
56     char **ppsz_query;
57     int i_query;
58 };
59 static const luaL_Reg p_reg[] = { { NULL, NULL } };
60
61 /*****************************************************************************
62  * Open: initialize and create stuff
63  *****************************************************************************/
64 int Open_LuaSD( vlc_object_t *p_this )
65 {
66     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
67     services_discovery_sys_t *p_sys;
68     lua_State *L = NULL;
69     char *psz_name;
70
71     if( !strcmp( p_sd->psz_name, "lua" ) )
72     {
73         // We want to load the module name "lua"
74         // This module can be used to load lua script not registered
75         // as builtin lua SD modules.
76         config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
77         psz_name = var_GetString( p_sd, "lua-sd" );
78     }
79     else
80     {
81         // We are loading a builtin lua sd module.
82         psz_name = strdup(p_sd->psz_name);
83     }
84
85     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
86     {
87         free( psz_name );
88         return VLC_ENOMEM;
89     }
90     p_sd->p_sys = p_sys;
91     p_sd->pf_control = Control;
92     p_sys->psz_filename = vlclua_find_file( "sd", psz_name );
93     if( !p_sys->psz_filename )
94     {
95         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
96                  psz_name );
97         free( psz_name );
98         goto error;
99     }
100     free( psz_name );
101     L = luaL_newstate();
102     if( !L )
103     {
104         msg_Err( p_sd, "Could not create new Lua State" );
105         goto error;
106     }
107     vlclua_set_this( L, p_sd );
108     luaL_openlibs( L );
109     luaL_register( L, "vlc", p_reg );
110     luaopen_input( L );
111     luaopen_msg( L );
112     luaopen_object( L );
113     luaopen_sd_sd( L );
114     luaopen_strings( L );
115     luaopen_variables( L );
116     luaopen_stream( L );
117     luaopen_gettext( L );
118     luaopen_xml( L );
119     lua_pop( L, 1 );
120
121     if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
122     {
123         msg_Warn( p_sd, "Error while setting the module search path for %s",
124                   p_sys->psz_filename );
125         goto error;
126     }
127     if( vlclua_dofile( VLC_OBJECT(p_sd), L, p_sys->psz_filename ) )
128     {
129         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
130                   lua_tostring( L, lua_gettop( L ) ) );
131         lua_pop( L, 1 );
132         goto error;
133     }
134     p_sys->L = L;
135     vlc_mutex_init( &p_sys->lock );
136     vlc_cond_init( &p_sys->cond );
137     TAB_INIT( p_sys->i_query, p_sys->ppsz_query );
138
139     if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
140     {
141         TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
142         vlc_cond_destroy( &p_sys->cond );
143         vlc_mutex_destroy( &p_sys->lock );
144         goto error;
145     }
146     return VLC_SUCCESS;
147
148 error:
149     if( L )
150         lua_close( L );
151     free( p_sys->psz_filename );
152     free( p_sys );
153     return VLC_EGENERIC;
154 }
155
156 /*****************************************************************************
157  * Close: cleanup
158  *****************************************************************************/
159 void Close_LuaSD( vlc_object_t *p_this )
160 {
161     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
162     services_discovery_sys_t *p_sys = p_sd->p_sys;
163
164     vlc_cancel( p_sys->thread );
165     vlc_join( p_sys->thread, NULL );
166
167     for( int i = 0; i < p_sys->i_query; i++ )
168         free( p_sys->ppsz_query[i] );
169     TAB_CLEAN( p_sys->i_query, p_sys->ppsz_query );
170
171     vlc_cond_destroy( &p_sys->cond );
172     vlc_mutex_destroy( &p_sys->lock );
173     free( p_sys->psz_filename );
174     lua_close( p_sys->L );
175     free( p_sys );
176 }
177
178 /*****************************************************************************
179  * Run: Thread entry-point
180  ****************************************************************************/
181 static void* Run( void *data )
182 {
183     services_discovery_t *p_sd = ( services_discovery_t * )data;
184     services_discovery_sys_t *p_sys = p_sd->p_sys;
185     lua_State *L = p_sys->L;
186
187     int cancel = vlc_savecancel();
188
189     lua_getglobal( L, "main" );
190     if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
191     {
192         msg_Err( p_sd, "Error while running script %s, "
193                   "function main(): %s", p_sys->psz_filename,
194                   lua_tostring( L, lua_gettop( L ) ) );
195         lua_pop( L, 1 );
196         vlc_restorecancel( cancel );
197         return NULL;
198     }
199     msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sys->psz_filename );
200
201     /* Force garbage collection, because the core will keep the SD
202      * open, but lua will never gc until lua_close(). */
203     lua_gc( L, LUA_GCCOLLECT, 0 );
204
205     vlc_restorecancel( cancel );
206
207     /* Main loop to handle search requests */
208     vlc_mutex_lock( &p_sys->lock );
209     mutex_cleanup_push( &p_sys->lock );
210     for( ;; )
211     {
212         /* Wait for a request */
213         if( !p_sys->i_query )
214         {
215             vlc_cond_wait( &p_sys->cond, &p_sys->lock );
216             continue;
217         }
218
219         /* Execute one query (protected against cancellation) */
220         char *psz_query = p_sys->ppsz_query[p_sys->i_query - 1];
221         REMOVE_ELEM( p_sys->ppsz_query, p_sys->i_query, p_sys->i_query - 1 );
222         vlc_mutex_unlock( &p_sys->lock );
223
224         cancel = vlc_savecancel();
225         DoSearch( p_sd, psz_query );
226         free( psz_query );
227         /* Force garbage collection, because the core will keep the SD
228          * open, but lua will never gc until lua_close(). */
229         lua_gc( L, LUA_GCCOLLECT, 0 );
230         vlc_restorecancel( cancel );
231
232         vlc_mutex_lock( &p_sys->lock );
233     }
234     vlc_cleanup_run();
235
236     return NULL;
237 }
238
239 /*****************************************************************************
240  * Control: services discrovery control
241  ****************************************************************************/
242 static int Control( services_discovery_t *p_sd, int i_command, va_list args )
243 {
244     services_discovery_sys_t *p_sys = p_sd->p_sys;
245
246     switch( i_command )
247     {
248     case SD_CMD_SEARCH:
249     {
250         const char *psz_query = va_arg( args, const char * );
251         vlc_mutex_lock( &p_sys->lock );
252         TAB_APPEND( p_sys->i_query, p_sys->ppsz_query, strdup( psz_query ) );
253         vlc_cond_signal( &p_sys->cond );
254         vlc_mutex_unlock( &p_sys->lock );
255         break;
256     }
257
258     case SD_CMD_DESCRIPTOR:
259     {
260         services_discovery_descriptor_t *p_desc = va_arg( args,
261                                 services_discovery_descriptor_t * );
262         return FillDescriptor( p_sd, p_desc );
263     }
264     }
265
266     return VLC_SUCCESS;
267 }
268
269 /*****************************************************************************
270  * DoSearch: search for a given query
271  ****************************************************************************/
272 static int DoSearch( services_discovery_t *p_sd, const char *psz_query )
273 {
274     services_discovery_sys_t *p_sys = p_sd->p_sys;
275     lua_State *L = p_sys->L;
276
277     /* Lookup for the 'search' function */
278     lua_getglobal( L, "search" );
279     if( !lua_isfunction( L, lua_gettop( L ) ) )
280     {
281         msg_Err( p_sd, "The script '%s' does not define any 'search' function",
282                  p_sys->psz_filename );
283         lua_pop( L, 1 );
284         return VLC_EGENERIC;
285     }
286
287     /* Push the query */
288     lua_pushstring( L, psz_query );
289
290     /* Call the 'search' function */
291     if( lua_pcall( L, 1, 0, 0 ) )
292     {
293         msg_Err( p_sd, "Error while running the script '%s': %s",
294                  p_sys->psz_filename, lua_tostring( L, lua_gettop( L ) ) );
295         lua_pop( L, 1 );
296         return VLC_EGENERIC;
297     }
298
299     return VLC_SUCCESS;
300 }
301
302 /** List of capabilities */
303 static const char *const ppsz_capabilities[] = {
304     "search",
305     NULL
306 };
307
308 /*****************************************************************************
309  * FillDescriptor: call the descriptor function and fill the structure
310  ****************************************************************************/
311 static int FillDescriptor( services_discovery_t *p_sd,
312                            services_discovery_descriptor_t *p_desc )
313 {
314     services_discovery_sys_t *p_sys = p_sd->p_sys;
315     int i_ret = VLC_EGENERIC;
316
317     /* Create a new lua thread */
318     lua_State *L = luaL_newstate();
319
320     if( vlclua_dofile( VLC_OBJECT(p_sd), L, p_sys->psz_filename ) )
321     {
322         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
323                  lua_tostring( L, -1 ) );
324         goto end;
325     }
326
327     /* Call the "descriptor" function */
328     lua_getglobal( L, "descriptor" );
329     if( !lua_isfunction( L, -1 ) || lua_pcall( L, 0, 1, 0 ) )
330     {
331         msg_Warn( p_sd, "Error getting the descriptor in '%s': %s",
332                   p_sys->psz_filename, lua_tostring( L, -1 ) );
333         goto end;
334     }
335
336     /* Get the different fields of the returned table */
337     lua_getfield( L, -1, "short_description" );
338     p_desc->psz_short_desc = luaL_strdupornull( L, -1 );
339     lua_pop( L, 1 );
340
341     lua_getfield( L, -1, "icon" );
342     p_desc->psz_icon_url = luaL_strdupornull( L, -1 );
343     lua_pop( L, 1 );
344
345     lua_getfield( L, -1, "url" );
346     p_desc->psz_url = luaL_strdupornull( L, -1 );
347     lua_pop( L, 1 );
348
349     lua_getfield( L, -1, "capabilities" );
350     p_desc->i_capabilities = 0;
351     if( lua_istable( L, -1 ) )
352     {
353         /* List all table entries */
354         lua_pushnil( L );
355         while( lua_next( L, -2 ) != 0 )
356         {
357             /* Key is at index -2 and value at index -1 */
358             const char *psz_cap = luaL_checkstring( L, -1 );
359             int i_cap = 0;
360             const char *psz_iter;
361             for( psz_iter = *ppsz_capabilities; psz_iter;
362                  psz_iter = ppsz_capabilities[ ++i_cap ] )
363             {
364                 if( !strcmp( psz_iter, psz_cap ) )
365                 {
366                     p_desc->i_capabilities |= 1 << i_cap;
367                     break;
368                 }
369             }
370
371             lua_pop( L, 1 );
372
373             if( !psz_iter )
374                 msg_Warn( p_sd, "Services discovery capability '%s' unknown in "
375                                 "script '%s'", psz_cap, p_sys->psz_filename );
376         }
377     }
378
379     lua_pop( L, 1 );
380     i_ret = VLC_SUCCESS;
381
382 end:
383     lua_close( L );
384     return i_ret;
385
386 }