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 )
127 VLC_SD_PROBE_SUBMODULE
131 /*****************************************************************************
133 *****************************************************************************/
134 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
135 static int file_select( const char *file )
137 int i = strlen( file );
139 for( j = 0; ppsz_lua_exts[j]; j++ )
141 int l = strlen( ppsz_lua_exts[j] );
142 if( !strcmp( file+i-l, ppsz_lua_exts[j] ) )
148 static int file_compare( const char **a, const char **b )
150 return strcmp( *a, *b );
153 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
156 char *datadir = config_GetUserDir( VLC_DATA_DIR );
157 if( datadir == NULL )
160 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
161 datadir, luadirname ) < 0 )
169 char *psz_datapath = config_GetDataDir( p_this );
170 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
172 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
173 psz_datapath, luadirname ) < 0 )
176 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
177 psz_datapath, luadirname ) < 0 )
183 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
184 psz_datapath, luadirname ) < 0 )
188 free( psz_datapath );
192 void vlclua_dir_list_free( char **ppsz_dir_list )
195 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
199 /*****************************************************************************
200 * Will execute func on all scripts in luadirname, and stop if func returns
202 *****************************************************************************/
203 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
204 const char * luadirname,
205 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
209 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
211 int i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
212 if( i_ret != VLC_SUCCESS )
214 i_ret = VLC_EGENERIC;
216 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
218 char **ppsz_filelist;
221 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
222 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
227 char **ppsz_file = ppsz_filelist;
228 char **ppsz_fileend = ppsz_filelist + i_files;
230 while( ppsz_file < ppsz_fileend )
234 if( asprintf( &psz_filename,
235 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
237 free( *(ppsz_file++) );
239 if( likely(psz_filename != NULL) )
241 msg_Dbg( p_this, "Trying Lua playlist script %s",
243 i_ret = func( p_this, psz_filename, L, user_data );
244 free( psz_filename );
245 if( i_ret == VLC_SUCCESS )
250 while( ppsz_file < ppsz_fileend )
251 free( *(ppsz_file++) );
252 free( ppsz_filelist );
254 if( i_ret == VLC_SUCCESS )
257 vlclua_dir_list_free( ppsz_dir_list );
261 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
263 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
265 vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
266 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
268 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
273 if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
274 psz_name, *ppsz_ext ) < 0 )
276 vlclua_dir_list_free( ppsz_dir_list );
280 if( vlc_stat( psz_filename, &st ) == 0
281 && S_ISREG( st.st_mode ) )
283 vlclua_dir_list_free( ppsz_dir_list );
286 free( psz_filename );
289 vlclua_dir_list_free( ppsz_dir_list );
293 /*****************************************************************************
294 * Meta data setters utility.
295 * Playlist item table should be on top of the stack when these are called
296 *****************************************************************************/
297 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
298 input_item_t *p_input )
300 #define TRY_META( a, b ) \
301 lua_getfield( L, -1, a ); \
302 if( lua_isstring( L, -1 ) ) \
304 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
305 EnsureUTF8( psz_value ); \
306 msg_Dbg( p_this, #b ": %s", psz_value ); \
307 input_item_Set ## b ( p_input, psz_value ); \
310 lua_pop( L, 1 ); /* pop a */
311 TRY_META( "title", Title );
312 TRY_META( "artist", Artist );
313 TRY_META( "genre", Genre );
314 TRY_META( "copyright", Copyright );
315 TRY_META( "album", Album );
316 TRY_META( "tracknum", TrackNum );
317 TRY_META( "description", Description );
318 TRY_META( "rating", Rating );
319 TRY_META( "date", Date );
320 TRY_META( "setting", Setting );
321 TRY_META( "url", URL );
322 TRY_META( "language", Language );
323 TRY_META( "nowplaying", NowPlaying );
324 TRY_META( "publisher", Publisher );
325 TRY_META( "encodedby", EncodedBy );
326 TRY_META( "arturl", ArtURL );
327 TRY_META( "trackid", TrackID );
330 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
331 input_item_t *p_input )
334 lua_getfield( L, -1, "meta" );
336 if( lua_istable( L, -1 ) )
339 /* ... item meta nil */
340 while( lua_next( L, -2 ) )
342 /* ... item meta key value */
343 if( !lua_isstring( L, -2 ) )
345 msg_Warn( p_this, "Custom meta data category name must be "
348 else if( !lua_istable( L, -1 ) )
350 msg_Warn( p_this, "Custom meta data category contents "
355 const char *psz_meta_category = lua_tostring( L, -2 );
356 msg_Dbg( p_this, "Found custom meta data category: %s",
359 /* ... item meta key value nil */
360 while( lua_next( L, -2 ) )
362 /* ... item meta key value key2 value2 */
363 if( !lua_isstring( L, -2 ) )
365 msg_Warn( p_this, "Custom meta category item name "
366 "must be a string." );
368 else if( !lua_isstring( L, -1 ) )
370 msg_Warn( p_this, "Custom meta category item value "
371 "must be a string." );
375 const char *psz_meta_name =
376 lua_tostring( L, -2 );
377 const char *psz_meta_value =
378 lua_tostring( L, -1 );
379 msg_Dbg( p_this, "Custom meta %s, %s: %s",
380 psz_meta_category, psz_meta_name,
382 input_item_AddInfo( p_input, psz_meta_category,
383 psz_meta_name, "%s", psz_meta_value );
385 lua_pop( L, 1 ); /* pop item */
386 /* ... item meta key value key2 */
388 /* ... item meta key value */
390 lua_pop( L, 1 ); /* pop category */
391 /* ... item meta key */
395 lua_pop( L, 1 ); /* pop "meta" */
396 /* ... item -> back to original stack */
399 /*****************************************************************************
401 ****************************************************************************/
403 * Playlist item table should be on top of the stack when this is called
405 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
406 int *pi_options, char ***pppsz_options )
408 lua_getfield( L, -1, "options" );
409 if( lua_istable( L, -1 ) )
412 while( lua_next( L, -2 ) )
414 if( lua_isstring( L, -1 ) )
416 char *psz_option = strdup( lua_tostring( L, -1 ) );
417 msg_Dbg( p_this, "Option: %s", psz_option );
418 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
423 msg_Warn( p_this, "Option should be a string" );
425 lua_pop( L, 1 ); /* pop option */
428 lua_pop( L, 1 ); /* pop "options" */
431 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
432 playlist_t *p_playlist,
433 input_item_t *p_parent, bool b_play )
436 input_item_node_t *p_parent_node = NULL;
438 assert( p_parent || p_playlist );
441 if( lua_istable( L, -1 ) )
443 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
446 while( lua_next( L, -2 ) )
448 /* playlist key item */
449 /* <Parse playlist item> */
450 if( lua_istable( L, -1 ) )
452 lua_getfield( L, -1, "path" );
453 /* playlist key item path */
454 if( lua_isstring( L, -1 ) )
456 const char *psz_path = NULL;
457 const char *psz_name = NULL;
458 char **ppsz_options = NULL;
460 mtime_t i_duration = -1;
461 input_item_t *p_input;
463 /* Read path and name */
464 psz_path = lua_tostring( L, -1 );
465 msg_Dbg( p_this, "Path: %s", psz_path );
466 lua_getfield( L, -2, "name" );
467 /* playlist key item path name */
468 if( lua_isstring( L, -1 ) )
470 psz_name = lua_tostring( L, -1 );
471 msg_Dbg( p_this, "Name: %s", psz_name );
475 if( !lua_isnil( L, -1 ) )
476 msg_Warn( p_this, "Playlist item name should be a string." );
481 lua_getfield( L, -3, "duration" );
482 /* playlist key item path name duration */
483 if( lua_isnumber( L, -1 ) )
485 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
487 else if( !lua_isnil( L, -1 ) )
489 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
491 lua_pop( L, 1 ); /* pop "duration" */
493 /* playlist key item path name */
495 /* Read options: item must be on top of stack */
496 lua_pushvalue( L, -3 );
497 /* playlist key item path name item */
498 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
500 /* Create input item */
501 p_input = input_item_NewExt( p_playlist, psz_path,
503 (const char **)ppsz_options,
504 VLC_INPUT_OPTION_TRUSTED,
506 lua_pop( L, 3 ); /* pop "path name item" */
507 /* playlist key item */
509 /* Read meta data: item must be on top of stack */
510 vlclua_read_meta_data( p_this, L, p_input );
512 /* Read custom meta data: item must be on top of stack*/
513 vlclua_read_custom_meta_data( p_this, L, p_input );
515 /* Append item to playlist */
516 if( p_parent ) /* Add to node */
518 input_item_node_AppendItem( p_parent_node, p_input );
520 else /* Play or Enqueue (preparse) */
521 /* FIXME: playlist_AddInput() can fail */
522 playlist_AddInput( p_playlist, p_input,
524 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
525 PLAYLIST_END, true, false );
526 i_count ++; /* increment counter */
527 vlc_gc_decref( p_input );
528 while( i_options > 0 )
529 free( ppsz_options[--i_options] );
530 free( ppsz_options );
534 lua_pop( L, 1 ); /* pop "path" */
536 "Playlist item's path should be a string" );
538 /* playlist key item */
542 msg_Warn( p_this, "Playlist item should be a table" );
544 /* <Parse playlist item> */
545 lua_pop( L, 1 ); /* pop the value, keep the key for
546 * the next lua_next() call */
552 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
553 else input_item_node_Delete( p_parent_node );
558 msg_Warn( p_this, "Playlist should be a table." );
563 static int vlc_sd_probe_Open( vlc_object_t *obj )
565 vlc_probe_t *probe = (vlc_probe_t *)obj;
566 char **ppsz_filelist = NULL;
567 char **ppsz_fileend = NULL;
570 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
572 vlclua_dir_list( obj, "sd", ppsz_dir_list );
573 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
578 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
581 free( ppsz_filelist );
582 ppsz_filelist = NULL;
584 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
586 if( i_files < 1 ) continue;
587 ppsz_fileend = ppsz_filelist + i_files;
588 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
591 if( asprintf( &psz_filename,
592 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
596 FILE *fd = vlc_fopen( psz_filename, "r" );
599 char description[256];
600 if( fgets( description, 256, fd ) != NULL )
602 char *temp = strchr( description, '\n' );
605 *(*ppsz_file + strlen(*ppsz_file) - 4 )= '\0';
606 if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
607 *ppsz_file, description + 17 ) < 0 )
610 free( psz_filename );
613 vlc_sd_probe_Add( probe, psz_name,
619 free( psz_filename );
624 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
627 free( ppsz_filelist );
629 vlclua_dir_list_free( ppsz_dir_list );
630 return VLC_PROBE_CONTINUE;
634 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
637 free( ppsz_filelist );
639 vlclua_dir_list_free( ppsz_dir_list );