1 /*****************************************************************************
2 * vlc.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
41 #include <vlc_charset.h>
44 #include <vlc_services_discovery.h>
47 #include <lua.h> /* Low level lua C API */
48 #include <lauxlib.h> /* Higher level C API */
49 #include <lualib.h> /* Lua libs */
53 /*****************************************************************************
55 *****************************************************************************/
56 #define INTF_TEXT N_("Lua interface")
57 #define INTF_LONGTEXT N_("Lua interface module to load")
59 #define CONFIG_TEXT N_("Lua interface configuration")
60 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
62 static int vlc_sd_probe_Open( vlc_object_t * );
65 set_shortname( N_( "Lua Art" ) )
66 set_description( N_("Fetch artwork using lua scripts") )
67 set_capability( "art finder", 10 )
68 set_callbacks( FindArt, NULL )
71 set_shortname( N_( "Lua Meta Fetcher" ) )
72 set_description( N_("Fetch meta data using lua scripts") )
73 set_capability( "meta fetcher", 10 )
74 set_callbacks( FetchMeta, NULL )
77 set_shortname( N_( "Lua Meta Reader" ) )
78 set_description( N_("Read meta data using lua scripts") )
79 set_capability( "meta reader", 10 )
80 set_callbacks( ReadMeta, NULL )
83 add_shortcut( "luaplaylist" )
84 set_category( CAT_INPUT )
85 set_subcategory( SUBCAT_INPUT_DEMUX )
86 set_shortname( N_("Lua Playlist") )
87 set_description( N_("Lua Playlist Parser Interface") )
88 set_capability( "demux", 2 )
89 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
92 set_description( N_("Lua Interface Module (shortcuts)") )
93 add_shortcut( "luarc" )
95 set_capability( "interface", 25 )
96 set_callbacks( Open_LuaIntf, Close_LuaIntf )
99 set_description( N_("Lua Interface Module") )
100 add_shortcut( "luaintf" )
101 add_shortcut( "luahttp" )
102 add_shortcut( "http" )
103 add_shortcut( "luatelnet" )
104 add_shortcut( "telnet" )
105 add_shortcut( "luahotkeys" )
106 /* add_shortcut( "hotkeys" ) */
107 set_capability( "interface", 0 )
108 add_string( "lua-intf", "dummy", NULL,
109 INTF_TEXT, INTF_LONGTEXT, false )
110 add_string( "lua-config", "", NULL,
111 CONFIG_TEXT, CONFIG_LONGTEXT, false )
112 set_callbacks( Open_LuaIntf, Close_LuaIntf )
115 set_shortname( N_("Lua Extension") )
116 add_shortcut( "luaextension" )
117 set_capability( "extension", 1 )
118 set_callbacks( Open_Extension, Close_Extension )
121 set_description( N_("Lua SD Module") )
122 add_shortcut( "luasd" )
123 set_capability( "services_discovery", 0 )
124 add_string( "lua-sd", "", NULL, "", "", false )
125 set_callbacks( Open_LuaSD, Close_LuaSD )
128 set_description( N_("Freebox TV") )
129 add_shortcut( "freebox" )
130 set_capability( "services_discovery", 0 )
131 set_callbacks( Open_LuaSD, Close_LuaSD )
134 set_description( N_("French TV") )
135 add_shortcut( "frenchtv" )
136 set_capability( "services_discovery", 0 )
137 set_callbacks( Open_LuaSD, Close_LuaSD )
139 VLC_SD_PROBE_SUBMODULE
143 /*****************************************************************************
145 *****************************************************************************/
146 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
147 static int file_select( const char *file )
149 int i = strlen( file );
151 for( j = 0; ppsz_lua_exts[j]; j++ )
153 int l = strlen( ppsz_lua_exts[j] );
154 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
160 static int file_compare( const char **a, const char **b )
162 return strcmp( *a, *b );
165 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
166 char **ppsz_dir_list )
169 char *datadir = config_GetUserDir( VLC_DATA_DIR );
171 if( likely(datadir != NULL)
172 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
173 datadir, luadirname ) != -1) )
177 #if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
178 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
179 config_GetLibDir(), luadirname ) != -1) )
183 char *psz_datapath = config_GetDataDir( p_this );
184 if( likely(psz_datapath != NULL) )
186 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
187 psz_datapath, luadirname ) != -1) )
190 #if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
191 if( likely(asprintf( &ppsz_dir_list[i],
192 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
193 psz_datapath, luadirname ) != -1) )
196 free( psz_datapath );
199 ppsz_dir_list[i] = NULL;
203 void vlclua_dir_list_free( char **ppsz_dir_list )
206 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
210 /*****************************************************************************
211 * Will execute func on all scripts in luadirname, and stop if func returns
213 *****************************************************************************/
214 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
215 const char * luadirname,
216 int (*func)(vlc_object_t *, const char *, void *),
219 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
221 int i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
222 if( i_ret != VLC_SUCCESS )
224 i_ret = VLC_EGENERIC;
226 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
228 char **ppsz_filelist;
231 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
232 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
237 char **ppsz_file = ppsz_filelist;
238 char **ppsz_fileend = ppsz_filelist + i_files;
240 while( ppsz_file < ppsz_fileend )
244 if( asprintf( &psz_filename,
245 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
247 free( *(ppsz_file++) );
249 if( likely(psz_filename != NULL) )
251 msg_Dbg( p_this, "Trying Lua playlist script %s",
253 i_ret = func( p_this, psz_filename, user_data );
254 free( psz_filename );
255 if( i_ret == VLC_SUCCESS )
260 while( ppsz_file < ppsz_fileend )
261 free( *(ppsz_file++) );
262 free( ppsz_filelist );
264 if( i_ret == VLC_SUCCESS )
267 vlclua_dir_list_free( ppsz_dir_list );
271 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
273 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
275 vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
276 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
278 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
283 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
284 psz_name, *ppsz_ext ) < 0 )
286 vlclua_dir_list_free( ppsz_dir_list );
290 if( vlc_stat( psz_filename, &st ) == 0
291 && S_ISREG( st.st_mode ) )
293 vlclua_dir_list_free( ppsz_dir_list );
296 free( psz_filename );
299 vlclua_dir_list_free( ppsz_dir_list );
303 /*****************************************************************************
304 * Meta data setters utility.
305 * Playlist item table should be on top of the stack when these are called
306 *****************************************************************************/
307 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
308 input_item_t *p_input )
310 #define TRY_META( a, b ) \
311 lua_getfield( L, -1, a ); \
312 if( lua_isstring( L, -1 ) ) \
314 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
315 EnsureUTF8( psz_value ); \
316 msg_Dbg( p_this, #b ": %s", psz_value ); \
317 input_item_Set ## b ( p_input, psz_value ); \
320 lua_pop( L, 1 ); /* pop a */
321 TRY_META( "title", Title );
322 TRY_META( "artist", Artist );
323 TRY_META( "genre", Genre );
324 TRY_META( "copyright", Copyright );
325 TRY_META( "album", Album );
326 TRY_META( "tracknum", TrackNum );
327 TRY_META( "description", Description );
328 TRY_META( "rating", Rating );
329 TRY_META( "date", Date );
330 TRY_META( "setting", Setting );
331 TRY_META( "url", URL );
332 TRY_META( "language", Language );
333 TRY_META( "nowplaying", NowPlaying );
334 TRY_META( "publisher", Publisher );
335 TRY_META( "encodedby", EncodedBy );
336 TRY_META( "arturl", ArtURL );
337 TRY_META( "trackid", TrackID );
340 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
341 input_item_t *p_input )
344 lua_getfield( L, -1, "meta" );
346 if( lua_istable( L, -1 ) )
349 /* ... item meta nil */
350 while( lua_next( L, -2 ) )
352 /* ... item meta key value */
353 if( !lua_isstring( L, -2 ) )
355 msg_Warn( p_this, "Custom meta data category name must be "
358 else if( !lua_istable( L, -1 ) )
360 msg_Warn( p_this, "Custom meta data category contents "
365 const char *psz_meta_category = lua_tostring( L, -2 );
366 msg_Dbg( p_this, "Found custom meta data category: %s",
369 /* ... item meta key value nil */
370 while( lua_next( L, -2 ) )
372 /* ... item meta key value key2 value2 */
373 if( !lua_isstring( L, -2 ) )
375 msg_Warn( p_this, "Custom meta category item name "
376 "must be a string." );
378 else if( !lua_isstring( L, -1 ) )
380 msg_Warn( p_this, "Custom meta category item value "
381 "must be a string." );
385 const char *psz_meta_name =
386 lua_tostring( L, -2 );
387 const char *psz_meta_value =
388 lua_tostring( L, -1 );
389 msg_Dbg( p_this, "Custom meta %s, %s: %s",
390 psz_meta_category, psz_meta_name,
392 input_item_AddInfo( p_input, psz_meta_category,
393 psz_meta_name, "%s", psz_meta_value );
395 lua_pop( L, 1 ); /* pop item */
396 /* ... item meta key value key2 */
398 /* ... item meta key value */
400 lua_pop( L, 1 ); /* pop category */
401 /* ... item meta key */
405 lua_pop( L, 1 ); /* pop "meta" */
406 /* ... item -> back to original stack */
409 /*****************************************************************************
411 ****************************************************************************/
413 * Playlist item table should be on top of the stack when this is called
415 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
416 int *pi_options, char ***pppsz_options )
418 lua_getfield( L, -1, "options" );
419 if( lua_istable( L, -1 ) )
422 while( lua_next( L, -2 ) )
424 if( lua_isstring( L, -1 ) )
426 char *psz_option = strdup( lua_tostring( L, -1 ) );
427 msg_Dbg( p_this, "Option: %s", psz_option );
428 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
433 msg_Warn( p_this, "Option should be a string" );
435 lua_pop( L, 1 ); /* pop option */
438 lua_pop( L, 1 ); /* pop "options" */
441 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
442 playlist_t *p_playlist,
443 input_item_t *p_parent, bool b_play )
446 input_item_node_t *p_parent_node = NULL;
448 assert( p_parent || p_playlist );
451 if( lua_istable( L, -1 ) )
453 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
456 while( lua_next( L, -2 ) )
458 /* playlist key item */
459 /* <Parse playlist item> */
460 if( lua_istable( L, -1 ) )
462 lua_getfield( L, -1, "path" );
463 /* playlist key item path */
464 if( lua_isstring( L, -1 ) )
466 const char *psz_path = NULL;
467 const char *psz_name = NULL;
468 char **ppsz_options = NULL;
470 mtime_t i_duration = -1;
471 input_item_t *p_input;
473 /* Read path and name */
474 psz_path = lua_tostring( L, -1 );
475 msg_Dbg( p_this, "Path: %s", psz_path );
476 lua_getfield( L, -2, "name" );
477 /* playlist key item path name */
478 if( lua_isstring( L, -1 ) )
480 psz_name = lua_tostring( L, -1 );
481 msg_Dbg( p_this, "Name: %s", psz_name );
485 if( !lua_isnil( L, -1 ) )
486 msg_Warn( p_this, "Playlist item name should be a string." );
491 lua_getfield( L, -3, "duration" );
492 /* playlist key item path name duration */
493 if( lua_isnumber( L, -1 ) )
495 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
497 else if( !lua_isnil( L, -1 ) )
499 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
501 lua_pop( L, 1 ); /* pop "duration" */
503 /* playlist key item path name */
505 /* Read options: item must be on top of stack */
506 lua_pushvalue( L, -3 );
507 /* playlist key item path name item */
508 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
510 /* Create input item */
511 p_input = input_item_NewExt( p_playlist, psz_path,
513 (const char **)ppsz_options,
514 VLC_INPUT_OPTION_TRUSTED,
516 lua_pop( L, 3 ); /* pop "path name item" */
517 /* playlist key item */
519 /* Read meta data: item must be on top of stack */
520 vlclua_read_meta_data( p_this, L, p_input );
522 /* Read custom meta data: item must be on top of stack*/
523 vlclua_read_custom_meta_data( p_this, L, p_input );
525 /* Append item to playlist */
526 if( p_parent ) /* Add to node */
528 input_item_node_AppendItem( p_parent_node, p_input );
530 else /* Play or Enqueue (preparse) */
531 /* FIXME: playlist_AddInput() can fail */
532 playlist_AddInput( p_playlist, p_input,
534 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
535 PLAYLIST_END, true, false );
536 i_count ++; /* increment counter */
537 vlc_gc_decref( p_input );
538 while( i_options > 0 )
539 free( ppsz_options[--i_options] );
540 free( ppsz_options );
544 lua_pop( L, 1 ); /* pop "path" */
546 "Playlist item's path should be a string" );
548 /* playlist key item */
552 msg_Warn( p_this, "Playlist item should be a table" );
554 /* <Parse playlist item> */
555 lua_pop( L, 1 ); /* pop the value, keep the key for
556 * the next lua_next() call */
562 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
563 else input_item_node_Delete( p_parent_node );
568 msg_Warn( p_this, "Playlist should be a table." );
573 static int vlc_sd_probe_Open( vlc_object_t *obj )
575 vlc_probe_t *probe = (vlc_probe_t *)obj;
576 char **ppsz_filelist = NULL;
577 char **ppsz_fileend = NULL;
580 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
583 vlclua_dir_list( obj, "sd", ppsz_dir_list );
584 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
589 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
592 free( ppsz_filelist );
593 ppsz_filelist = NULL;
595 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
597 if( i_files < 1 ) continue;
598 ppsz_fileend = ppsz_filelist + i_files;
599 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
602 if( asprintf( &psz_filename,
603 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
610 msg_Err( probe, "Could not create new Lua State" );
614 if( vlclua_add_modules_path( probe, L, psz_filename ) )
616 msg_Err( probe, "Error while setting the module search path for %s",
620 if( luaL_dofile( L, psz_filename ) )
623 msg_Err( probe, "Error loading script %s: %s", psz_filename,
624 lua_tostring( L, lua_gettop( L ) ) );
629 char *temp = strchr( *ppsz_file, '.' );
632 lua_getglobal( L, "descriptor" );
633 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
636 if( !( psz_longname = strdup( *ppsz_file ) ) )
638 free( psz_filename );
644 lua_getfield( L, -1, "title" );
645 if( !lua_isstring( L, -1 ) ||
646 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
648 free( psz_filename );
652 if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
653 *ppsz_file, psz_longname ) < 0 )
655 free( psz_filename );
656 free( psz_longname );
659 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
661 free( psz_longname );
662 free( psz_filename );
668 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
671 free( ppsz_filelist );
673 vlclua_dir_list_free( ppsz_dir_list );
674 return VLC_PROBE_CONTINUE;
678 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
681 free( ppsz_filelist );
685 vlclua_dir_list_free( ppsz_dir_list );
689 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
691 /* FIXME: don't use luaL_dostring */
692 char *psz_command = NULL;
693 if( asprintf( &psz_command,
694 "package.path =[[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
700 if( luaL_dostring( L, psz_command ) )
710 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
712 /* Setup the module search path:
713 * * "The script's directory"/modules
714 * * "The script's parent directory"/modules
715 * * and so on for all the next directories in the directory list
718 char *psz_path = strdup( psz_filename );
722 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
730 /* psz_path now holds the file's directory */
731 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
739 /* psz_path now holds the file's parent directory */
740 if( vlclua_add_modules_path_inner( L, psz_path ) )
745 *psz_char = DIR_SEP_CHAR;
747 /* psz_path now holds the file's directory */
748 if( vlclua_add_modules_path_inner( L, psz_path ) )
754 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
755 vlclua_dir_list( obj, psz_char+1/* gruik? */, ppsz_dir_list );
756 char **ppsz_dir = ppsz_dir_list;
758 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
761 for( ; *ppsz_dir; ppsz_dir++ )
763 psz_path = *ppsz_dir;
764 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
769 if( vlclua_add_modules_path_inner( L, psz_path ) )
771 *psz_char = DIR_SEP_CHAR;
773 if( vlclua_add_modules_path_inner( L, psz_path ) )
777 vlclua_dir_list_free( ppsz_dir_list );
781 vlclua_dir_list_free( ppsz_dir_list );