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, NULL, NULL, false )
126 add_string( "lua-longname", "", NULL, NULL, NULL, false )
128 set_callbacks( Open_LuaSD, Close_LuaSD )
131 set_description( N_("Freebox TV") )
132 add_shortcut( "freebox" )
133 set_capability( "services_discovery", 0 )
134 set_callbacks( Open_LuaSD, Close_LuaSD )
137 set_description( N_("French TV") )
138 add_shortcut( "frenchtv" )
139 set_capability( "services_discovery", 0 )
140 set_callbacks( Open_LuaSD, Close_LuaSD )
142 VLC_SD_PROBE_SUBMODULE
146 /*****************************************************************************
148 *****************************************************************************/
149 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
150 static int file_select( const char *file )
152 int i = strlen( file );
154 for( j = 0; ppsz_lua_exts[j]; j++ )
156 int l = strlen( ppsz_lua_exts[j] );
157 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
163 static int file_compare( const char **a, const char **b )
165 return strcmp( *a, *b );
168 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
169 char ***pppsz_dir_list )
171 #define MAX_DIR_LIST_SIZE 5
172 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
173 if (!*pppsz_dir_list)
175 char **ppsz_dir_list = *pppsz_dir_list;
178 char *datadir = config_GetUserDir( VLC_DATA_DIR );
180 if( likely(datadir != NULL)
181 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
182 datadir, luadirname ) != -1) )
186 #if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
187 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
188 config_GetLibDir(), luadirname ) != -1) )
192 char *psz_datapath = config_GetDataDir( p_this );
193 if( likely(psz_datapath != NULL) )
195 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
196 psz_datapath, luadirname ) != -1) )
199 #if defined(__APPLE__) || defined(SYS_BEOS)
200 if( likely(asprintf( &ppsz_dir_list[i],
201 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
202 psz_datapath, luadirname ) != -1) )
205 free( psz_datapath );
208 ppsz_dir_list[i] = NULL;
210 assert( i < MAX_DIR_LIST_SIZE);
215 void vlclua_dir_list_free( char **ppsz_dir_list )
218 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
220 free( ppsz_dir_list );
223 /*****************************************************************************
224 * Will execute func on all scripts in luadirname, and stop if func returns
226 *****************************************************************************/
227 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
228 const char * luadirname,
229 int (*func)(vlc_object_t *, const char *, void *),
232 char **ppsz_dir_list = NULL;
234 int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
235 if( i_ret != VLC_SUCCESS )
237 i_ret = VLC_EGENERIC;
239 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
241 char **ppsz_filelist;
244 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
245 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
250 char **ppsz_file = ppsz_filelist;
251 char **ppsz_fileend = ppsz_filelist + i_files;
253 while( ppsz_file < ppsz_fileend )
257 if( asprintf( &psz_filename,
258 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
260 free( *(ppsz_file++) );
262 if( likely(psz_filename != NULL) )
264 msg_Dbg( p_this, "Trying Lua playlist script %s",
266 i_ret = func( p_this, psz_filename, user_data );
267 free( psz_filename );
268 if( i_ret == VLC_SUCCESS )
273 while( ppsz_file < ppsz_fileend )
274 free( *(ppsz_file++) );
275 free( ppsz_filelist );
277 if( i_ret == VLC_SUCCESS )
280 vlclua_dir_list_free( ppsz_dir_list );
284 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
286 char **ppsz_dir_list = NULL;
288 vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
289 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
291 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
296 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
297 psz_name, *ppsz_ext ) < 0 )
299 vlclua_dir_list_free( ppsz_dir_list );
303 if( vlc_stat( psz_filename, &st ) == 0
304 && S_ISREG( st.st_mode ) )
306 vlclua_dir_list_free( ppsz_dir_list );
309 free( psz_filename );
312 vlclua_dir_list_free( ppsz_dir_list );
316 /*****************************************************************************
317 * Meta data setters utility.
318 * Playlist item table should be on top of the stack when these are called
319 *****************************************************************************/
320 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
321 input_item_t *p_input )
323 #define TRY_META( a, b ) \
324 lua_getfield( L, -1, a ); \
325 if( lua_isstring( L, -1 ) && \
326 strcmp( lua_tostring( L, -1 ), "" ) ) \
328 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
329 EnsureUTF8( psz_value ); \
330 msg_Dbg( p_this, #b ": %s", psz_value ); \
331 input_item_Set ## b ( p_input, psz_value ); \
334 lua_pop( L, 1 ); /* pop a */
335 TRY_META( "title", Title );
336 TRY_META( "artist", Artist );
337 TRY_META( "genre", Genre );
338 TRY_META( "copyright", Copyright );
339 TRY_META( "album", Album );
340 TRY_META( "tracknum", TrackNum );
341 TRY_META( "description", Description );
342 TRY_META( "rating", Rating );
343 TRY_META( "date", Date );
344 TRY_META( "setting", Setting );
345 TRY_META( "url", URL );
346 TRY_META( "language", Language );
347 TRY_META( "nowplaying", NowPlaying );
348 TRY_META( "publisher", Publisher );
349 TRY_META( "encodedby", EncodedBy );
350 TRY_META( "arturl", ArtURL );
351 TRY_META( "trackid", TrackID );
354 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
355 input_item_t *p_input )
358 lua_getfield( L, -1, "meta" );
360 if( lua_istable( L, -1 ) )
363 /* ... item meta nil */
364 while( lua_next( L, -2 ) )
366 /* ... item meta key value */
367 if( !lua_isstring( L, -2 ) )
369 msg_Warn( p_this, "Custom meta data category name must be "
372 else if( !lua_istable( L, -1 ) )
374 msg_Warn( p_this, "Custom meta data category contents "
379 const char *psz_meta_category = lua_tostring( L, -2 );
380 msg_Dbg( p_this, "Found custom meta data category: %s",
383 /* ... item meta key value nil */
384 while( lua_next( L, -2 ) )
386 /* ... item meta key value key2 value2 */
387 if( !lua_isstring( L, -2 ) )
389 msg_Warn( p_this, "Custom meta category item name "
390 "must be a string." );
392 else if( !lua_isstring( L, -1 ) )
394 msg_Warn( p_this, "Custom meta category item value "
395 "must be a string." );
399 const char *psz_meta_name =
400 lua_tostring( L, -2 );
401 const char *psz_meta_value =
402 lua_tostring( L, -1 );
403 msg_Dbg( p_this, "Custom meta %s, %s: %s",
404 psz_meta_category, psz_meta_name,
406 input_item_AddInfo( p_input, psz_meta_category,
407 psz_meta_name, "%s", psz_meta_value );
409 lua_pop( L, 1 ); /* pop item */
410 /* ... item meta key value key2 */
412 /* ... item meta key value */
414 lua_pop( L, 1 ); /* pop category */
415 /* ... item meta key */
419 lua_pop( L, 1 ); /* pop "meta" */
420 /* ... item -> back to original stack */
423 /*****************************************************************************
425 ****************************************************************************/
427 * Playlist item table should be on top of the stack when this is called
429 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
430 int *pi_options, char ***pppsz_options )
432 lua_getfield( L, -1, "options" );
433 if( lua_istable( L, -1 ) )
436 while( lua_next( L, -2 ) )
438 if( lua_isstring( L, -1 ) )
440 char *psz_option = strdup( lua_tostring( L, -1 ) );
441 msg_Dbg( p_this, "Option: %s", psz_option );
442 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
447 msg_Warn( p_this, "Option should be a string" );
449 lua_pop( L, 1 ); /* pop option */
452 lua_pop( L, 1 ); /* pop "options" */
455 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
456 playlist_t *p_playlist,
457 input_item_t *p_parent, bool b_play )
460 input_item_node_t *p_parent_node = NULL;
462 assert( p_parent || p_playlist );
465 if( lua_istable( L, -1 ) )
467 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
470 while( lua_next( L, -2 ) )
472 /* playlist key item */
473 /* <Parse playlist item> */
474 if( lua_istable( L, -1 ) )
476 lua_getfield( L, -1, "path" );
477 /* playlist key item path */
478 if( lua_isstring( L, -1 ) )
480 const char *psz_path = NULL;
481 const char *psz_name = NULL;
482 char **ppsz_options = NULL;
484 mtime_t i_duration = -1;
485 input_item_t *p_input;
487 /* Read path and name */
488 psz_path = lua_tostring( L, -1 );
489 msg_Dbg( p_this, "Path: %s", psz_path );
490 lua_getfield( L, -2, "name" );
491 /* playlist key item path name */
492 if( lua_isstring( L, -1 ) )
494 psz_name = lua_tostring( L, -1 );
495 msg_Dbg( p_this, "Name: %s", psz_name );
499 if( !lua_isnil( L, -1 ) )
500 msg_Warn( p_this, "Playlist item name should be a string." );
505 lua_getfield( L, -3, "duration" );
506 /* playlist key item path name duration */
507 if( lua_isnumber( L, -1 ) )
509 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
511 else if( !lua_isnil( L, -1 ) )
513 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
515 lua_pop( L, 1 ); /* pop "duration" */
517 /* playlist key item path name */
519 /* Read options: item must be on top of stack */
520 lua_pushvalue( L, -3 );
521 /* playlist key item path name item */
522 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
524 /* Create input item */
525 p_input = input_item_NewExt( p_playlist, psz_path,
527 (const char **)ppsz_options,
528 VLC_INPUT_OPTION_TRUSTED,
530 lua_pop( L, 3 ); /* pop "path name item" */
531 /* playlist key item */
533 /* Read meta data: item must be on top of stack */
534 vlclua_read_meta_data( p_this, L, p_input );
536 /* Read custom meta data: item must be on top of stack*/
537 vlclua_read_custom_meta_data( p_this, L, p_input );
539 /* Append item to playlist */
540 if( p_parent ) /* Add to node */
542 input_item_node_AppendItem( p_parent_node, p_input );
544 else /* Play or Enqueue (preparse) */
545 /* FIXME: playlist_AddInput() can fail */
546 playlist_AddInput( p_playlist, p_input,
548 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
549 PLAYLIST_END, true, false );
550 i_count ++; /* increment counter */
551 vlc_gc_decref( p_input );
552 while( i_options > 0 )
553 free( ppsz_options[--i_options] );
554 free( ppsz_options );
558 lua_pop( L, 1 ); /* pop "path" */
560 "Playlist item's path should be a string" );
562 /* playlist key item */
566 msg_Warn( p_this, "Playlist item should be a table" );
568 /* <Parse playlist item> */
569 lua_pop( L, 1 ); /* pop the value, keep the key for
570 * the next lua_next() call */
576 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
577 else input_item_node_Delete( p_parent_node );
582 msg_Warn( p_this, "Playlist should be a table." );
587 static int vlc_sd_probe_Open( vlc_object_t *obj )
589 vlc_probe_t *probe = (vlc_probe_t *)obj;
590 char **ppsz_filelist = NULL;
591 char **ppsz_fileend = NULL;
594 char **ppsz_dir_list = NULL;
597 vlclua_dir_list( obj, "sd", &ppsz_dir_list );
598 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
603 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
606 free( ppsz_filelist );
607 ppsz_filelist = NULL;
609 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
611 if( i_files < 1 ) continue;
612 ppsz_fileend = ppsz_filelist + i_files;
613 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
616 if( asprintf( &psz_filename,
617 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
624 msg_Err( probe, "Could not create new Lua State" );
625 free( psz_filename );
629 if( vlclua_add_modules_path( probe, L, psz_filename ) )
631 msg_Err( probe, "Error while setting the module search path for %s",
633 free( psz_filename );
636 if( luaL_dofile( L, psz_filename ) )
639 msg_Err( probe, "Error loading script %s: %s", psz_filename,
640 lua_tostring( L, lua_gettop( L ) ) );
642 free( psz_filename );
647 char *temp = strchr( *ppsz_file, '.' );
650 lua_getglobal( L, "descriptor" );
651 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
654 if( !( psz_longname = strdup( *ppsz_file ) ) )
656 free( psz_filename );
662 lua_getfield( L, -1, "title" );
663 if( !lua_isstring( L, -1 ) ||
664 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
666 free( psz_filename );
671 char *psz_file_esc = config_StringEscape( *ppsz_file );
672 char *psz_longname_esc = config_StringEscape( psz_longname );
673 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
674 psz_file_esc, psz_longname_esc ) < 0 )
676 free( psz_file_esc );
677 free( psz_longname_esc );
678 free( psz_filename );
679 free( psz_longname );
682 free( psz_file_esc );
683 free( psz_longname_esc );
684 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
686 free( psz_longname );
687 free( psz_filename );
693 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
696 free( ppsz_filelist );
698 vlclua_dir_list_free( ppsz_dir_list );
699 return VLC_PROBE_CONTINUE;
703 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
706 free( ppsz_filelist );
710 vlclua_dir_list_free( ppsz_dir_list );
714 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
717 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
719 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
720 psz_path, *ppsz_ext );
727 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
729 /* Setup the module search path:
730 * * "The script's directory"/modules
731 * * "The script's parent directory"/modules
732 * * and so on for all the next directories in the directory list
734 char *psz_path = strdup( psz_filename );
738 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
746 /* psz_path now holds the file's directory */
747 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
755 /* Push package on stack */
757 lua_getglobal( L, "package" );
759 /* psz_path now holds the file's parent directory */
760 count += vlclua_add_modules_path_inner( L, psz_path );
761 *psz_char = DIR_SEP_CHAR;
763 /* psz_path now holds the file's directory */
764 count += vlclua_add_modules_path_inner( L, psz_path );
766 char **ppsz_dir_list = NULL;
767 vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
768 char **ppsz_dir = ppsz_dir_list;
770 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
773 for( ; *ppsz_dir; ppsz_dir++ )
775 psz_path = *ppsz_dir;
776 /* FIXME: doesn't work well with meta/... modules due to the double
778 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
781 vlclua_dir_list_free( ppsz_dir_list );
786 count += vlclua_add_modules_path_inner( L, psz_path );
787 *psz_char = DIR_SEP_CHAR;
788 count += vlclua_add_modules_path_inner( L, psz_path );
791 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
792 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
793 lua_setfield( L, -2, "path"); /* Set package.path */
794 lua_pop( L, 1 ); /* Pop the package module */
796 vlclua_dir_list_free( ppsz_dir_list );