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 add_string( "lua-longname", "", NULL, "", "", false )
126 set_callbacks( Open_LuaSD, Close_LuaSD )
129 set_description( N_("Freebox TV") )
130 add_shortcut( "freebox" )
131 set_capability( "services_discovery", 0 )
132 set_callbacks( Open_LuaSD, Close_LuaSD )
135 set_description( N_("French TV") )
136 add_shortcut( "frenchtv" )
137 set_capability( "services_discovery", 0 )
138 set_callbacks( Open_LuaSD, Close_LuaSD )
140 VLC_SD_PROBE_SUBMODULE
144 /*****************************************************************************
146 *****************************************************************************/
147 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
148 static int file_select( const char *file )
150 int i = strlen( file );
152 for( j = 0; ppsz_lua_exts[j]; j++ )
154 int l = strlen( ppsz_lua_exts[j] );
155 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
161 static int file_compare( const char **a, const char **b )
163 return strcmp( *a, *b );
166 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
167 char ***pppsz_dir_list )
169 #define MAX_DIR_LIST_SIZE 5
170 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
171 if (!*pppsz_dir_list)
173 char **ppsz_dir_list = *pppsz_dir_list;
176 char *datadir = config_GetUserDir( VLC_DATA_DIR );
178 if( likely(datadir != NULL)
179 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
180 datadir, luadirname ) != -1) )
184 #if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
185 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
186 config_GetLibDir(), luadirname ) != -1) )
190 char *psz_datapath = config_GetDataDir( p_this );
191 if( likely(psz_datapath != NULL) )
193 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
194 psz_datapath, luadirname ) != -1) )
197 #if defined(__APPLE__) || defined(SYS_BEOS)
198 if( likely(asprintf( &ppsz_dir_list[i],
199 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
200 psz_datapath, luadirname ) != -1) )
203 free( psz_datapath );
206 ppsz_dir_list[i] = NULL;
208 assert( i < MAX_DIR_LIST_SIZE);
213 void vlclua_dir_list_free( char **ppsz_dir_list )
216 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
218 free( ppsz_dir_list );
221 /*****************************************************************************
222 * Will execute func on all scripts in luadirname, and stop if func returns
224 *****************************************************************************/
225 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
226 const char * luadirname,
227 int (*func)(vlc_object_t *, const char *, void *),
230 char **ppsz_dir_list = NULL;
232 int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
233 if( i_ret != VLC_SUCCESS )
235 i_ret = VLC_EGENERIC;
237 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
239 char **ppsz_filelist;
242 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
243 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
248 char **ppsz_file = ppsz_filelist;
249 char **ppsz_fileend = ppsz_filelist + i_files;
251 while( ppsz_file < ppsz_fileend )
255 if( asprintf( &psz_filename,
256 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
258 free( *(ppsz_file++) );
260 if( likely(psz_filename != NULL) )
262 msg_Dbg( p_this, "Trying Lua playlist script %s",
264 i_ret = func( p_this, psz_filename, user_data );
265 free( psz_filename );
266 if( i_ret == VLC_SUCCESS )
271 while( ppsz_file < ppsz_fileend )
272 free( *(ppsz_file++) );
273 free( ppsz_filelist );
275 if( i_ret == VLC_SUCCESS )
278 vlclua_dir_list_free( ppsz_dir_list );
282 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
284 char **ppsz_dir_list = NULL;
286 vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
287 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
289 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
294 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
295 psz_name, *ppsz_ext ) < 0 )
297 vlclua_dir_list_free( ppsz_dir_list );
301 if( vlc_stat( psz_filename, &st ) == 0
302 && S_ISREG( st.st_mode ) )
304 vlclua_dir_list_free( ppsz_dir_list );
307 free( psz_filename );
310 vlclua_dir_list_free( ppsz_dir_list );
314 /*****************************************************************************
315 * Meta data setters utility.
316 * Playlist item table should be on top of the stack when these are called
317 *****************************************************************************/
318 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
319 input_item_t *p_input )
321 #define TRY_META( a, b ) \
322 lua_getfield( L, -1, a ); \
323 if( lua_isstring( L, -1 ) && \
324 strcmp( lua_tostring( L, -1 ), "" ) ) \
326 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
327 EnsureUTF8( psz_value ); \
328 msg_Dbg( p_this, #b ": %s", psz_value ); \
329 input_item_Set ## b ( p_input, psz_value ); \
332 lua_pop( L, 1 ); /* pop a */
333 TRY_META( "title", Title );
334 TRY_META( "artist", Artist );
335 TRY_META( "genre", Genre );
336 TRY_META( "copyright", Copyright );
337 TRY_META( "album", Album );
338 TRY_META( "tracknum", TrackNum );
339 TRY_META( "description", Description );
340 TRY_META( "rating", Rating );
341 TRY_META( "date", Date );
342 TRY_META( "setting", Setting );
343 TRY_META( "url", URL );
344 TRY_META( "language", Language );
345 TRY_META( "nowplaying", NowPlaying );
346 TRY_META( "publisher", Publisher );
347 TRY_META( "encodedby", EncodedBy );
348 TRY_META( "arturl", ArtURL );
349 TRY_META( "trackid", TrackID );
352 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
353 input_item_t *p_input )
356 lua_getfield( L, -1, "meta" );
358 if( lua_istable( L, -1 ) )
361 /* ... item meta nil */
362 while( lua_next( L, -2 ) )
364 /* ... item meta key value */
365 if( !lua_isstring( L, -2 ) )
367 msg_Warn( p_this, "Custom meta data category name must be "
370 else if( !lua_istable( L, -1 ) )
372 msg_Warn( p_this, "Custom meta data category contents "
377 const char *psz_meta_category = lua_tostring( L, -2 );
378 msg_Dbg( p_this, "Found custom meta data category: %s",
381 /* ... item meta key value nil */
382 while( lua_next( L, -2 ) )
384 /* ... item meta key value key2 value2 */
385 if( !lua_isstring( L, -2 ) )
387 msg_Warn( p_this, "Custom meta category item name "
388 "must be a string." );
390 else if( !lua_isstring( L, -1 ) )
392 msg_Warn( p_this, "Custom meta category item value "
393 "must be a string." );
397 const char *psz_meta_name =
398 lua_tostring( L, -2 );
399 const char *psz_meta_value =
400 lua_tostring( L, -1 );
401 msg_Dbg( p_this, "Custom meta %s, %s: %s",
402 psz_meta_category, psz_meta_name,
404 input_item_AddInfo( p_input, psz_meta_category,
405 psz_meta_name, "%s", psz_meta_value );
407 lua_pop( L, 1 ); /* pop item */
408 /* ... item meta key value key2 */
410 /* ... item meta key value */
412 lua_pop( L, 1 ); /* pop category */
413 /* ... item meta key */
417 lua_pop( L, 1 ); /* pop "meta" */
418 /* ... item -> back to original stack */
421 /*****************************************************************************
423 ****************************************************************************/
425 * Playlist item table should be on top of the stack when this is called
427 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
428 int *pi_options, char ***pppsz_options )
430 lua_getfield( L, -1, "options" );
431 if( lua_istable( L, -1 ) )
434 while( lua_next( L, -2 ) )
436 if( lua_isstring( L, -1 ) )
438 char *psz_option = strdup( lua_tostring( L, -1 ) );
439 msg_Dbg( p_this, "Option: %s", psz_option );
440 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
445 msg_Warn( p_this, "Option should be a string" );
447 lua_pop( L, 1 ); /* pop option */
450 lua_pop( L, 1 ); /* pop "options" */
453 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
454 playlist_t *p_playlist,
455 input_item_t *p_parent, bool b_play )
458 input_item_node_t *p_parent_node = NULL;
460 assert( p_parent || p_playlist );
463 if( lua_istable( L, -1 ) )
465 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
468 while( lua_next( L, -2 ) )
470 /* playlist key item */
471 /* <Parse playlist item> */
472 if( lua_istable( L, -1 ) )
474 lua_getfield( L, -1, "path" );
475 /* playlist key item path */
476 if( lua_isstring( L, -1 ) )
478 const char *psz_path = NULL;
479 const char *psz_name = NULL;
480 char **ppsz_options = NULL;
482 mtime_t i_duration = -1;
483 input_item_t *p_input;
485 /* Read path and name */
486 psz_path = lua_tostring( L, -1 );
487 msg_Dbg( p_this, "Path: %s", psz_path );
488 lua_getfield( L, -2, "name" );
489 /* playlist key item path name */
490 if( lua_isstring( L, -1 ) )
492 psz_name = lua_tostring( L, -1 );
493 msg_Dbg( p_this, "Name: %s", psz_name );
497 if( !lua_isnil( L, -1 ) )
498 msg_Warn( p_this, "Playlist item name should be a string." );
503 lua_getfield( L, -3, "duration" );
504 /* playlist key item path name duration */
505 if( lua_isnumber( L, -1 ) )
507 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
509 else if( !lua_isnil( L, -1 ) )
511 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
513 lua_pop( L, 1 ); /* pop "duration" */
515 /* playlist key item path name */
517 /* Read options: item must be on top of stack */
518 lua_pushvalue( L, -3 );
519 /* playlist key item path name item */
520 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
522 /* Create input item */
523 p_input = input_item_NewExt( p_playlist, psz_path,
525 (const char **)ppsz_options,
526 VLC_INPUT_OPTION_TRUSTED,
528 lua_pop( L, 3 ); /* pop "path name item" */
529 /* playlist key item */
531 /* Read meta data: item must be on top of stack */
532 vlclua_read_meta_data( p_this, L, p_input );
534 /* Read custom meta data: item must be on top of stack*/
535 vlclua_read_custom_meta_data( p_this, L, p_input );
537 /* Append item to playlist */
538 if( p_parent ) /* Add to node */
540 input_item_node_AppendItem( p_parent_node, p_input );
542 else /* Play or Enqueue (preparse) */
543 /* FIXME: playlist_AddInput() can fail */
544 playlist_AddInput( p_playlist, p_input,
546 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
547 PLAYLIST_END, true, false );
548 i_count ++; /* increment counter */
549 vlc_gc_decref( p_input );
550 while( i_options > 0 )
551 free( ppsz_options[--i_options] );
552 free( ppsz_options );
556 lua_pop( L, 1 ); /* pop "path" */
558 "Playlist item's path should be a string" );
560 /* playlist key item */
564 msg_Warn( p_this, "Playlist item should be a table" );
566 /* <Parse playlist item> */
567 lua_pop( L, 1 ); /* pop the value, keep the key for
568 * the next lua_next() call */
574 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
575 else input_item_node_Delete( p_parent_node );
580 msg_Warn( p_this, "Playlist should be a table." );
585 static char *escape( const char *psz )
590 /* Count the number of ' and \ in the string */
591 const char *it = psz;
595 if( *it == '\'' || *it == '\\' )
601 return strdup( psz );
603 char *psz_esc = malloc( strlen( psz ) + i_esc + 1 );
608 if( *it == '\'' || *it == '\\' )
616 static int vlc_sd_probe_Open( vlc_object_t *obj )
618 vlc_probe_t *probe = (vlc_probe_t *)obj;
619 char **ppsz_filelist = NULL;
620 char **ppsz_fileend = NULL;
623 char **ppsz_dir_list = NULL;
626 vlclua_dir_list( obj, "sd", &ppsz_dir_list );
627 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
632 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
635 free( ppsz_filelist );
636 ppsz_filelist = NULL;
638 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
640 if( i_files < 1 ) continue;
641 ppsz_fileend = ppsz_filelist + i_files;
642 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
645 if( asprintf( &psz_filename,
646 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
653 msg_Err( probe, "Could not create new Lua State" );
654 free( psz_filename );
658 if( vlclua_add_modules_path( probe, L, psz_filename ) )
660 msg_Err( probe, "Error while setting the module search path for %s",
662 free( psz_filename );
665 if( luaL_dofile( L, psz_filename ) )
668 msg_Err( probe, "Error loading script %s: %s", psz_filename,
669 lua_tostring( L, lua_gettop( L ) ) );
671 free( psz_filename );
676 char *temp = strchr( *ppsz_file, '.' );
679 lua_getglobal( L, "descriptor" );
680 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
683 if( !( psz_longname = strdup( *ppsz_file ) ) )
685 free( psz_filename );
691 lua_getfield( L, -1, "title" );
692 if( !lua_isstring( L, -1 ) ||
693 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
695 free( psz_filename );
700 char *psz_file_esc = escape( *ppsz_file );
701 char *psz_longname_esc = escape( psz_longname );
702 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
703 psz_file_esc, psz_longname_esc ) < 0 )
705 free( psz_file_esc );
706 free( psz_longname_esc );
707 free( psz_filename );
708 free( psz_longname );
711 free( psz_file_esc );
712 free( psz_longname_esc );
713 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
715 free( psz_longname );
716 free( psz_filename );
722 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
725 free( ppsz_filelist );
727 vlclua_dir_list_free( ppsz_dir_list );
728 return VLC_PROBE_CONTINUE;
732 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
735 free( ppsz_filelist );
739 vlclua_dir_list_free( ppsz_dir_list );
743 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
746 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
748 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
749 psz_path, *ppsz_ext );
756 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
758 /* Setup the module search path:
759 * * "The script's directory"/modules
760 * * "The script's parent directory"/modules
761 * * and so on for all the next directories in the directory list
763 char *psz_path = strdup( psz_filename );
767 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
775 /* psz_path now holds the file's directory */
776 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
784 /* Push package on stack */
786 lua_getglobal( L, "package" );
788 /* psz_path now holds the file's parent directory */
789 count += vlclua_add_modules_path_inner( L, psz_path );
790 *psz_char = DIR_SEP_CHAR;
792 /* psz_path now holds the file's directory */
793 count += vlclua_add_modules_path_inner( L, psz_path );
795 char **ppsz_dir_list = NULL;
796 vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
797 char **ppsz_dir = ppsz_dir_list;
799 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
802 for( ; *ppsz_dir; ppsz_dir++ )
804 psz_path = *ppsz_dir;
805 /* FIXME: doesn't work well with meta/... modules due to the double
807 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
810 vlclua_dir_list_free( ppsz_dir_list );
815 count += vlclua_add_modules_path_inner( L, psz_path );
816 *psz_char = DIR_SEP_CHAR;
817 count += vlclua_add_modules_path_inner( L, psz_path );
820 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
821 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
822 lua_setfield( L, -2, "path"); /* Set package.path */
823 lua_pop( L, 1 ); /* Pop the package module */
825 vlclua_dir_list_free( ppsz_dir_list );