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>
43 #include <vlc_services_discovery.h>
45 #include <lua.h> /* Low level lua C API */
46 #include <lauxlib.h> /* Higher level C API */
47 #include <lualib.h> /* Lua libs */
51 /*****************************************************************************
53 *****************************************************************************/
54 #define INTF_TEXT N_("Lua interface")
55 #define INTF_LONGTEXT N_("Lua interface module to load")
57 #define CONFIG_TEXT N_("Lua interface configuration")
58 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
60 static int vlc_sd_probe_Open( vlc_object_t * );
63 set_shortname( N_( "Lua Art" ) )
64 set_description( N_("Fetch artwork using lua scripts") )
65 set_capability( "art finder", 10 )
66 set_callbacks( FindArt, NULL )
69 set_shortname( N_( "Lua Meta Fetcher" ) )
70 set_description( N_("Fetch meta data using lua scripts") )
71 set_capability( "meta fetcher", 10 )
72 set_callbacks( FetchMeta, NULL )
75 set_shortname( N_( "Lua Meta Reader" ) )
76 set_description( N_("Read meta data using lua scripts") )
77 set_capability( "meta reader", 10 )
78 set_callbacks( ReadMeta, NULL )
81 add_shortcut( "luaplaylist" )
82 set_category( CAT_INPUT )
83 set_subcategory( SUBCAT_INPUT_DEMUX )
84 set_shortname( N_("Lua Playlist") )
85 set_description( N_("Lua Playlist Parser Interface") )
86 set_capability( "demux", 2 )
87 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
90 set_description( N_("Lua Interface Module (shortcuts)") )
91 add_shortcut( "luarc" )
93 set_capability( "interface", 25 )
94 set_callbacks( Open_LuaIntf, Close_LuaIntf )
97 set_description( N_("Lua Interface Module") )
98 add_shortcut( "luaintf" )
99 add_shortcut( "luahttp" )
100 add_shortcut( "http" )
101 add_shortcut( "luatelnet" )
102 add_shortcut( "telnet" )
103 add_shortcut( "luahotkeys" )
104 /* add_shortcut( "hotkeys" ) */
105 set_capability( "interface", 0 )
106 add_string( "lua-intf", "dummy", NULL,
107 INTF_TEXT, INTF_LONGTEXT, false )
108 add_string( "lua-config", "", NULL,
109 CONFIG_TEXT, CONFIG_LONGTEXT, false )
110 set_callbacks( Open_LuaIntf, Close_LuaIntf )
113 set_shortname( N_("Lua Extension") )
114 add_shortcut( "luaextension" )
115 set_capability( "extension", 1 )
116 set_callbacks( Open_Extension, Close_Extension )
119 set_description( N_("Lua SD Module") )
120 add_shortcut( "luasd" )
121 set_capability( "services_discovery", 0 )
122 add_string( "lua-sd", "", NULL, "", "", false )
123 set_callbacks( Open_LuaSD, Close_LuaSD )
125 VLC_SD_PROBE_SUBMODULE
129 /*****************************************************************************
131 *****************************************************************************/
132 static int file_select( const char *file )
134 int i = strlen( file );
135 return i > 4 && !strcmp( file+i-4, ".lua" );
138 static int file_compare( const char **a, const char **b )
140 return strcmp( *a, *b );
143 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
146 char *datadir = config_GetUserDir( VLC_DATA_DIR );
147 if( datadir == NULL )
150 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
151 datadir, luadirname ) < 0 )
159 char *psz_datapath = config_GetDataDir( p_this );
160 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
162 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
163 psz_datapath, luadirname ) < 0 )
166 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
167 psz_datapath, luadirname ) < 0 )
173 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
174 psz_datapath, luadirname ) < 0 )
178 free( psz_datapath );
182 void vlclua_dir_list_free( char **ppsz_dir_list )
185 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
189 /*****************************************************************************
190 * Will execute func on all scripts in luadirname, and stop if func returns
192 *****************************************************************************/
193 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
194 const char * luadirname,
195 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
199 int i_ret = VLC_EGENERIC;
201 char **ppsz_filelist = NULL;
202 char **ppsz_fileend = NULL;
205 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
208 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
209 if( i_ret != VLC_SUCCESS )
211 i_ret = VLC_EGENERIC;
214 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
220 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
223 free( ppsz_filelist );
224 ppsz_filelist = NULL;
227 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
228 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
230 if( i_files < 1 ) continue;
231 ppsz_fileend = ppsz_filelist + i_files;
233 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
236 if( asprintf( &psz_filename,
237 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
239 vlclua_dir_list_free( ppsz_dir_list );
242 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
244 i_ret = func( p_this, psz_filename, L, user_data );
246 free( psz_filename );
248 if( i_ret == VLC_SUCCESS ) break;
250 if( i_ret == VLC_SUCCESS ) break;
255 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
258 free( ppsz_filelist );
260 vlclua_dir_list_free( ppsz_dir_list );
266 /*****************************************************************************
267 * Meta data setters utility.
268 * Playlist item table should be on top of the stack when these are called
269 *****************************************************************************/
270 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
271 input_item_t *p_input )
273 #define TRY_META( a, b ) \
274 lua_getfield( L, -1, a ); \
275 if( lua_isstring( L, -1 ) ) \
277 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
278 EnsureUTF8( psz_value ); \
279 msg_Dbg( p_this, #b ": %s", psz_value ); \
280 input_item_Set ## b ( p_input, psz_value ); \
283 lua_pop( L, 1 ); /* pop a */
284 TRY_META( "title", Title );
285 TRY_META( "artist", Artist );
286 TRY_META( "genre", Genre );
287 TRY_META( "copyright", Copyright );
288 TRY_META( "album", Album );
289 TRY_META( "tracknum", TrackNum );
290 TRY_META( "description", Description );
291 TRY_META( "rating", Rating );
292 TRY_META( "date", Date );
293 TRY_META( "setting", Setting );
294 TRY_META( "url", URL );
295 TRY_META( "language", Language );
296 TRY_META( "nowplaying", NowPlaying );
297 TRY_META( "publisher", Publisher );
298 TRY_META( "encodedby", EncodedBy );
299 TRY_META( "arturl", ArtURL );
300 TRY_META( "trackid", TrackID );
303 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
304 input_item_t *p_input )
307 lua_getfield( L, -1, "meta" );
309 if( lua_istable( L, -1 ) )
312 /* ... item meta nil */
313 while( lua_next( L, -2 ) )
315 /* ... item meta key value */
316 if( !lua_isstring( L, -2 ) )
318 msg_Warn( p_this, "Custom meta data category name must be "
321 else if( !lua_istable( L, -1 ) )
323 msg_Warn( p_this, "Custom meta data category contents "
328 const char *psz_meta_category = lua_tostring( L, -2 );
329 msg_Dbg( p_this, "Found custom meta data category: %s",
332 /* ... item meta key value nil */
333 while( lua_next( L, -2 ) )
335 /* ... item meta key value key2 value2 */
336 if( !lua_isstring( L, -2 ) )
338 msg_Warn( p_this, "Custom meta category item name "
339 "must be a string." );
341 else if( !lua_isstring( L, -1 ) )
343 msg_Warn( p_this, "Custom meta category item value "
344 "must be a string." );
348 const char *psz_meta_name =
349 lua_tostring( L, -2 );
350 const char *psz_meta_value =
351 lua_tostring( L, -1 );
352 msg_Dbg( p_this, "Custom meta %s, %s: %s",
353 psz_meta_category, psz_meta_name,
355 input_item_AddInfo( p_input, psz_meta_category,
356 psz_meta_name, "%s", psz_meta_value );
358 lua_pop( L, 1 ); /* pop item */
359 /* ... item meta key value key2 */
361 /* ... item meta key value */
363 lua_pop( L, 1 ); /* pop category */
364 /* ... item meta key */
368 lua_pop( L, 1 ); /* pop "meta" */
369 /* ... item -> back to original stack */
372 /*****************************************************************************
374 ****************************************************************************/
376 * Playlist item table should be on top of the stack when this is called
378 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
379 int *pi_options, char ***pppsz_options )
381 lua_getfield( L, -1, "options" );
382 if( lua_istable( L, -1 ) )
385 while( lua_next( L, -2 ) )
387 if( lua_isstring( L, -1 ) )
389 char *psz_option = strdup( lua_tostring( L, -1 ) );
390 msg_Dbg( p_this, "Option: %s", psz_option );
391 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
396 msg_Warn( p_this, "Option should be a string" );
398 lua_pop( L, 1 ); /* pop option */
401 lua_pop( L, 1 ); /* pop "options" */
404 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
405 playlist_t *p_playlist,
406 input_item_t *p_parent, bool b_play )
410 assert( p_parent || p_playlist );
413 if( lua_istable( L, -1 ) )
417 while( lua_next( L, -2 ) )
419 /* playlist key item */
420 /* <Parse playlist item> */
421 if( lua_istable( L, -1 ) )
423 lua_getfield( L, -1, "path" );
424 /* playlist key item path */
425 if( lua_isstring( L, -1 ) )
427 const char *psz_path = NULL;
428 const char *psz_name = NULL;
429 char **ppsz_options = NULL;
431 mtime_t i_duration = -1;
432 input_item_t *p_input;
434 /* Read path and name */
435 psz_path = lua_tostring( L, -1 );
436 msg_Dbg( p_this, "Path: %s", psz_path );
437 lua_getfield( L, -2, "name" );
438 /* playlist key item path name */
439 if( lua_isstring( L, -1 ) )
441 psz_name = lua_tostring( L, -1 );
442 msg_Dbg( p_this, "Name: %s", psz_name );
446 if( !lua_isnil( L, -1 ) )
447 msg_Warn( p_this, "Playlist item name should be a string." );
452 lua_getfield( L, -3, "duration" );
453 /* playlist key item path name duration */
454 if( lua_isnumber( L, -1 ) )
456 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
458 else if( !lua_isnil( L, -1 ) )
460 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
462 lua_pop( L, 1 ); /* pop "duration" */
464 /* playlist key item path name */
466 /* Read options: item must be on top of stack */
467 lua_pushvalue( L, -3 );
468 /* playlist key item path name item */
469 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
471 /* Create input item */
472 p_input = input_item_NewExt( p_playlist, psz_path,
474 (const char **)ppsz_options,
475 VLC_INPUT_OPTION_TRUSTED,
477 lua_pop( L, 3 ); /* pop "path name item" */
478 /* playlist key item */
480 /* Read meta data: item must be on top of stack */
481 vlclua_read_meta_data( p_this, L, p_input );
483 /* Read custom meta data: item must be on top of stack*/
484 vlclua_read_custom_meta_data( p_this, L, p_input );
486 /* Append item to playlist */
487 if( p_parent ) /* Add to node */
489 input_item_PostSubItem( p_parent, p_input );
491 else /* Play or Enqueue (preparse) */
492 /* FIXME: playlist_AddInput() can fail */
493 playlist_AddInput( p_playlist, p_input,
495 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
496 PLAYLIST_END, true, false );
497 i_count ++; /* increment counter */
498 vlc_gc_decref( p_input );
499 while( i_options > 0 )
500 free( ppsz_options[--i_options] );
501 free( ppsz_options );
505 lua_pop( L, 1 ); /* pop "path" */
507 "Playlist item's path should be a string" );
509 /* playlist key item */
513 msg_Warn( p_this, "Playlist item should be a table" );
515 /* <Parse playlist item> */
516 lua_pop( L, 1 ); /* pop the value, keep the key for
517 * the next lua_next() call */
524 msg_Warn( p_this, "Playlist should be a table." );
529 static int vlc_sd_probe_Open( vlc_object_t *obj )
531 vlc_probe_t *probe = (vlc_probe_t *)obj;
532 char **ppsz_filelist = NULL;
533 char **ppsz_fileend = NULL;
536 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
538 vlclua_dir_list( obj, "sd", ppsz_dir_list );
539 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
544 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
547 free( ppsz_filelist );
548 ppsz_filelist = NULL;
550 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
552 if( i_files < 1 ) continue;
553 ppsz_fileend = ppsz_filelist + i_files;
554 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
557 if( asprintf( &psz_filename,
558 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
562 FILE *fd = utf8_fopen( psz_filename, "r" );
565 char description[256];
566 if( fgets( description, 256, fd ) != NULL )
568 char *temp = strchr( description, '\n' );
571 *(*ppsz_file + strlen(*ppsz_file) - 4 )= '\0';
572 if( asprintf( &psz_name, "lua{sd=%s}", *ppsz_file ) < 0 )
575 free( psz_filename );
578 vlc_sd_probe_Add( probe, psz_name,
584 free( psz_filename );
589 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
592 free( ppsz_filelist );
594 vlclua_dir_list_free( ppsz_dir_list );
595 return VLC_PROBE_CONTINUE;
599 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
602 free( ppsz_filelist );
604 vlclua_dir_list_free( ppsz_dir_list );