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 Interface Module") )
66 set_description( N_("Interfaces implemented using lua scripts") )
67 add_shortcut( "luaintf" )
68 add_shortcut( "luahttp" )
69 add_shortcut( "http" )
70 add_shortcut( "luatelnet" )
71 add_shortcut( "telnet" )
72 add_shortcut( "luahotkeys" )
73 /* add_shortcut( "hotkeys" ) */
74 set_capability( "interface", 0 )
75 set_category( CAT_INTERFACE )
76 set_subcategory( SUBCAT_INTERFACE_CONTROL )
77 add_string( "lua-intf", "dummy", NULL,
78 INTF_TEXT, INTF_LONGTEXT, false )
79 add_string( "lua-config", "", NULL,
80 CONFIG_TEXT, CONFIG_LONGTEXT, false )
81 set_callbacks( Open_LuaIntf, Close_LuaIntf )
84 set_shortname( N_( "Lua Meta Fetcher" ) )
85 set_description( N_("Fetch meta data using lua scripts") )
86 set_capability( "meta fetcher", 10 )
87 set_callbacks( FetchMeta, NULL )
90 set_shortname( N_( "Lua Meta Reader" ) )
91 set_description( N_("Read meta data using lua scripts") )
92 set_capability( "meta reader", 10 )
93 set_callbacks( ReadMeta, NULL )
96 add_shortcut( "luaplaylist" )
97 set_shortname( N_("Lua Playlist") )
98 set_description( N_("Lua Playlist Parser Interface") )
99 set_capability( "demux", 2 )
100 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
103 set_description( N_("Lua Interface Module (shortcuts)") )
104 add_shortcut( "luarc" )
106 set_capability( "interface", 25 )
107 set_callbacks( Open_LuaIntf, Close_LuaIntf )
110 set_shortname( N_( "Lua Art" ) )
111 set_description( N_("Fetch artwork using lua scripts") )
112 set_capability( "art finder", 10 )
113 set_callbacks( FindArt, NULL )
116 set_shortname( N_("Lua Extension") )
117 add_shortcut( "luaextension" )
118 set_capability( "extension", 1 )
119 set_callbacks( Open_Extension, Close_Extension )
122 set_description( N_("Lua SD Module") )
123 add_shortcut( "luasd" )
124 set_capability( "services_discovery", 0 )
125 add_string( "lua-sd", "", NULL, NULL, NULL, false )
127 add_string( "lua-longname", "", NULL, NULL, NULL, false )
129 set_callbacks( Open_LuaSD, Close_LuaSD )
132 set_description( N_("Freebox TV") )
133 add_shortcut( "freebox" )
134 set_capability( "services_discovery", 0 )
135 set_callbacks( Open_LuaSD, Close_LuaSD )
138 set_description( N_("French TV") )
139 add_shortcut( "frenchtv" )
140 set_capability( "services_discovery", 0 )
141 set_callbacks( Open_LuaSD, Close_LuaSD )
143 VLC_SD_PROBE_SUBMODULE
147 /*****************************************************************************
149 *****************************************************************************/
150 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
151 static int file_select( const char *file )
153 int i = strlen( file );
155 for( j = 0; ppsz_lua_exts[j]; j++ )
157 int l = strlen( ppsz_lua_exts[j] );
158 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
164 static int file_compare( const char **a, const char **b )
166 return strcmp( *a, *b );
169 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
170 char ***pppsz_dir_list )
172 #define MAX_DIR_LIST_SIZE 5
173 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
174 if (!*pppsz_dir_list)
176 char **ppsz_dir_list = *pppsz_dir_list;
179 char *datadir = config_GetUserDir( VLC_DATA_DIR );
181 if( likely(datadir != NULL)
182 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
183 datadir, luadirname ) != -1) )
187 #if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
188 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
189 config_GetLibDir(), luadirname ) != -1) )
193 char *psz_datapath = config_GetDataDir( p_this );
194 if( likely(psz_datapath != NULL) )
196 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
197 psz_datapath, luadirname ) != -1) )
200 #if defined(__APPLE__) || defined(SYS_BEOS)
201 if( likely(asprintf( &ppsz_dir_list[i],
202 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
203 psz_datapath, luadirname ) != -1) )
206 free( psz_datapath );
209 ppsz_dir_list[i] = NULL;
211 assert( i < MAX_DIR_LIST_SIZE);
216 void vlclua_dir_list_free( char **ppsz_dir_list )
219 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
221 free( ppsz_dir_list );
224 /*****************************************************************************
225 * Will execute func on all scripts in luadirname, and stop if func returns
227 *****************************************************************************/
228 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
229 const char * luadirname,
230 int (*func)(vlc_object_t *, const char *, void *),
233 char **ppsz_dir_list = NULL;
235 int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
236 if( i_ret != VLC_SUCCESS )
238 i_ret = VLC_EGENERIC;
240 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
242 char **ppsz_filelist;
245 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
246 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
251 char **ppsz_file = ppsz_filelist;
252 char **ppsz_fileend = ppsz_filelist + i_files;
254 while( ppsz_file < ppsz_fileend )
258 if( asprintf( &psz_filename,
259 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
261 free( *(ppsz_file++) );
263 if( likely(psz_filename != NULL) )
265 msg_Dbg( p_this, "Trying Lua playlist script %s",
267 i_ret = func( p_this, psz_filename, user_data );
268 free( psz_filename );
269 if( i_ret == VLC_SUCCESS )
274 while( ppsz_file < ppsz_fileend )
275 free( *(ppsz_file++) );
276 free( ppsz_filelist );
278 if( i_ret == VLC_SUCCESS )
281 vlclua_dir_list_free( ppsz_dir_list );
285 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
287 char **ppsz_dir_list = NULL;
289 vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
290 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
292 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
297 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
298 psz_name, *ppsz_ext ) < 0 )
300 vlclua_dir_list_free( ppsz_dir_list );
304 if( vlc_stat( psz_filename, &st ) == 0
305 && S_ISREG( st.st_mode ) )
307 vlclua_dir_list_free( ppsz_dir_list );
310 free( psz_filename );
313 vlclua_dir_list_free( ppsz_dir_list );
317 /*****************************************************************************
318 * Meta data setters utility.
319 * Playlist item table should be on top of the stack when these are called
320 *****************************************************************************/
321 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
322 input_item_t *p_input )
324 #define TRY_META( a, b ) \
325 lua_getfield( L, -1, a ); \
326 if( lua_isstring( L, -1 ) && \
327 strcmp( lua_tostring( L, -1 ), "" ) ) \
329 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
330 EnsureUTF8( psz_value ); \
331 msg_Dbg( p_this, #b ": %s", psz_value ); \
332 input_item_Set ## b ( p_input, psz_value ); \
335 lua_pop( L, 1 ); /* pop a */
336 TRY_META( "title", Title );
337 TRY_META( "artist", Artist );
338 TRY_META( "genre", Genre );
339 TRY_META( "copyright", Copyright );
340 TRY_META( "album", Album );
341 TRY_META( "tracknum", TrackNum );
342 TRY_META( "description", Description );
343 TRY_META( "rating", Rating );
344 TRY_META( "date", Date );
345 TRY_META( "setting", Setting );
346 TRY_META( "url", URL );
347 TRY_META( "language", Language );
348 TRY_META( "nowplaying", NowPlaying );
349 TRY_META( "publisher", Publisher );
350 TRY_META( "encodedby", EncodedBy );
351 TRY_META( "arturl", ArtURL );
352 TRY_META( "trackid", TrackID );
355 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
356 input_item_t *p_input )
359 lua_getfield( L, -1, "meta" );
361 if( lua_istable( L, -1 ) )
364 /* ... item meta nil */
365 while( lua_next( L, -2 ) )
367 /* ... item meta key value */
368 if( !lua_isstring( L, -2 ) )
370 msg_Warn( p_this, "Custom meta data category name must be "
373 else if( !lua_istable( L, -1 ) )
375 msg_Warn( p_this, "Custom meta data category contents "
380 const char *psz_meta_category = lua_tostring( L, -2 );
381 msg_Dbg( p_this, "Found custom meta data category: %s",
384 /* ... item meta key value nil */
385 while( lua_next( L, -2 ) )
387 /* ... item meta key value key2 value2 */
388 if( !lua_isstring( L, -2 ) )
390 msg_Warn( p_this, "Custom meta category item name "
391 "must be a string." );
393 else if( !lua_isstring( L, -1 ) )
395 msg_Warn( p_this, "Custom meta category item value "
396 "must be a string." );
400 const char *psz_meta_name =
401 lua_tostring( L, -2 );
402 const char *psz_meta_value =
403 lua_tostring( L, -1 );
404 msg_Dbg( p_this, "Custom meta %s, %s: %s",
405 psz_meta_category, psz_meta_name,
407 input_item_AddInfo( p_input, psz_meta_category,
408 psz_meta_name, "%s", psz_meta_value );
410 lua_pop( L, 1 ); /* pop item */
411 /* ... item meta key value key2 */
413 /* ... item meta key value */
415 lua_pop( L, 1 ); /* pop category */
416 /* ... item meta key */
420 lua_pop( L, 1 ); /* pop "meta" */
421 /* ... item -> back to original stack */
424 /*****************************************************************************
426 ****************************************************************************/
428 * Playlist item table should be on top of the stack when this is called
430 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
431 int *pi_options, char ***pppsz_options )
433 lua_getfield( L, -1, "options" );
434 if( lua_istable( L, -1 ) )
437 while( lua_next( L, -2 ) )
439 if( lua_isstring( L, -1 ) )
441 char *psz_option = strdup( lua_tostring( L, -1 ) );
442 msg_Dbg( p_this, "Option: %s", psz_option );
443 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
448 msg_Warn( p_this, "Option should be a string" );
450 lua_pop( L, 1 ); /* pop option */
453 lua_pop( L, 1 ); /* pop "options" */
456 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
457 playlist_t *p_playlist,
458 input_item_t *p_parent, bool b_play )
461 input_item_node_t *p_parent_node = NULL;
463 assert( p_parent || p_playlist );
466 if( lua_istable( L, -1 ) )
468 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
471 while( lua_next( L, -2 ) )
473 /* playlist key item */
474 /* <Parse playlist item> */
475 if( lua_istable( L, -1 ) )
477 lua_getfield( L, -1, "path" );
478 /* playlist key item path */
479 if( lua_isstring( L, -1 ) )
481 const char *psz_path = NULL;
482 const char *psz_name = NULL;
483 char **ppsz_options = NULL;
485 mtime_t i_duration = -1;
486 input_item_t *p_input;
488 /* Read path and name */
489 psz_path = lua_tostring( L, -1 );
490 msg_Dbg( p_this, "Path: %s", psz_path );
491 lua_getfield( L, -2, "name" );
492 /* playlist key item path name */
493 if( lua_isstring( L, -1 ) )
495 psz_name = lua_tostring( L, -1 );
496 msg_Dbg( p_this, "Name: %s", psz_name );
500 if( !lua_isnil( L, -1 ) )
501 msg_Warn( p_this, "Playlist item name should be a string." );
506 lua_getfield( L, -3, "duration" );
507 /* playlist key item path name duration */
508 if( lua_isnumber( L, -1 ) )
510 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
512 else if( !lua_isnil( L, -1 ) )
514 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
516 lua_pop( L, 1 ); /* pop "duration" */
518 /* playlist key item path name */
520 /* Read options: item must be on top of stack */
521 lua_pushvalue( L, -3 );
522 /* playlist key item path name item */
523 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
525 /* Create input item */
526 p_input = input_item_NewExt( p_playlist, psz_path,
528 (const char **)ppsz_options,
529 VLC_INPUT_OPTION_TRUSTED,
531 lua_pop( L, 3 ); /* pop "path name item" */
532 /* playlist key item */
534 /* Read meta data: item must be on top of stack */
535 vlclua_read_meta_data( p_this, L, p_input );
537 /* Read custom meta data: item must be on top of stack*/
538 vlclua_read_custom_meta_data( p_this, L, p_input );
540 /* Append item to playlist */
541 if( p_parent ) /* Add to node */
543 input_item_node_AppendItem( p_parent_node, p_input );
545 else /* Play or Enqueue (preparse) */
546 /* FIXME: playlist_AddInput() can fail */
547 playlist_AddInput( p_playlist, p_input,
549 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
550 PLAYLIST_END, true, false );
551 i_count ++; /* increment counter */
552 vlc_gc_decref( p_input );
553 while( i_options > 0 )
554 free( ppsz_options[--i_options] );
555 free( ppsz_options );
559 lua_pop( L, 1 ); /* pop "path" */
561 "Playlist item's path should be a string" );
563 /* playlist key item */
567 msg_Warn( p_this, "Playlist item should be a table" );
569 /* <Parse playlist item> */
570 lua_pop( L, 1 ); /* pop the value, keep the key for
571 * the next lua_next() call */
577 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
578 else input_item_node_Delete( p_parent_node );
583 msg_Warn( p_this, "Playlist should be a table." );
588 static int vlc_sd_probe_Open( vlc_object_t *obj )
590 vlc_probe_t *probe = (vlc_probe_t *)obj;
591 char **ppsz_filelist = NULL;
592 char **ppsz_fileend = NULL;
595 char **ppsz_dir_list = NULL;
598 vlclua_dir_list( obj, "sd", &ppsz_dir_list );
599 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
604 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
607 free( ppsz_filelist );
608 ppsz_filelist = NULL;
610 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
612 if( i_files < 1 ) continue;
613 ppsz_fileend = ppsz_filelist + i_files;
614 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
617 if( asprintf( &psz_filename,
618 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
625 msg_Err( probe, "Could not create new Lua State" );
626 free( psz_filename );
630 if( vlclua_add_modules_path( probe, L, psz_filename ) )
632 msg_Err( probe, "Error while setting the module search path for %s",
634 free( psz_filename );
637 if( luaL_dofile( L, psz_filename ) )
640 msg_Err( probe, "Error loading script %s: %s", psz_filename,
641 lua_tostring( L, lua_gettop( L ) ) );
643 free( psz_filename );
648 char *temp = strchr( *ppsz_file, '.' );
651 lua_getglobal( L, "descriptor" );
652 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
655 if( !( psz_longname = strdup( *ppsz_file ) ) )
657 free( psz_filename );
663 lua_getfield( L, -1, "title" );
664 if( !lua_isstring( L, -1 ) ||
665 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
667 free( psz_filename );
672 char *psz_file_esc = config_StringEscape( *ppsz_file );
673 char *psz_longname_esc = config_StringEscape( psz_longname );
674 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
675 psz_file_esc, psz_longname_esc ) < 0 )
677 free( psz_file_esc );
678 free( psz_longname_esc );
679 free( psz_filename );
680 free( psz_longname );
683 free( psz_file_esc );
684 free( psz_longname_esc );
685 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
687 free( psz_longname );
688 free( psz_filename );
694 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
697 free( ppsz_filelist );
699 vlclua_dir_list_free( ppsz_dir_list );
700 return VLC_PROBE_CONTINUE;
704 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
707 free( ppsz_filelist );
711 vlclua_dir_list_free( ppsz_dir_list );
715 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
718 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
720 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
721 psz_path, *ppsz_ext );
728 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
730 /* Setup the module search path:
731 * * "The script's directory"/modules
732 * * "The script's parent directory"/modules
733 * * and so on for all the next directories in the directory list
735 char *psz_path = strdup( psz_filename );
739 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
747 /* psz_path now holds the file's directory */
748 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
756 /* Push package on stack */
758 lua_getglobal( L, "package" );
760 /* psz_path now holds the file's parent directory */
761 count += vlclua_add_modules_path_inner( L, psz_path );
762 *psz_char = DIR_SEP_CHAR;
764 /* psz_path now holds the file's directory */
765 count += vlclua_add_modules_path_inner( L, psz_path );
767 char **ppsz_dir_list = NULL;
768 vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
769 char **ppsz_dir = ppsz_dir_list;
771 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
774 for( ; *ppsz_dir; ppsz_dir++ )
776 psz_path = *ppsz_dir;
777 /* FIXME: doesn't work well with meta/... modules due to the double
779 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
782 vlclua_dir_list_free( ppsz_dir_list );
787 count += vlclua_add_modules_path_inner( L, psz_path );
788 *psz_char = DIR_SEP_CHAR;
789 count += vlclua_add_modules_path_inner( L, psz_path );
792 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
793 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
794 lua_setfield( L, -2, "path"); /* Set package.path */
795 lua_pop( L, 1 ); /* Pop the package module */
797 vlclua_dir_list_free( ppsz_dir_list );