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 <lua.h> /* Low level lua C API */
45 #include <lauxlib.h> /* Higher level C API */
46 #include <lualib.h> /* Lua libs */
50 /*****************************************************************************
52 *****************************************************************************/
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>, ...}, ...'.")
61 set_shortname( N_( "Lua Art" ) )
62 set_description( N_("Fetch artwork using lua scripts") )
63 set_capability( "art finder", 10 )
64 set_callbacks( FindArt, NULL )
67 add_shortcut( "luaplaylist" )
68 set_category( CAT_INPUT )
69 set_subcategory( SUBCAT_INPUT_DEMUX )
70 set_shortname( N_("Lua Playlist") )
71 set_description( N_("Lua Playlist Parser Interface") )
72 set_capability( "demux", 2 )
73 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
76 add_shortcut( "luaintf" )
77 add_shortcut( "luarc" )
78 /* add_shortcut( "rc" ) */
79 add_shortcut( "luahotkeys" )
80 /* add_shortcut( "hotkeys" ) */
81 add_shortcut( "luatelnet" )
82 /* add_shortcut( "telnet" ) */
83 add_shortcut( "luahttp" )
84 /* add_shortcut( "http" ) */
85 set_description( N_("Lua Interface Module") )
86 set_capability( "interface", 0 )
87 add_string( "lua-intf", "dummy", NULL,
88 INTF_TEXT, INTF_LONGTEXT, false )
89 add_string( "lua-config", "", NULL,
90 CONFIG_TEXT, CONFIG_LONGTEXT, false )
91 set_callbacks( Open_LuaIntf, Close_LuaIntf )
94 /*****************************************************************************
96 *****************************************************************************/
97 static int file_select( const char *file )
99 int i = strlen( file );
100 return i > 4 && !strcmp( file+i-4, ".lua" );
103 static int file_compare( const char **a, const char **b )
105 return strcmp( *a, *b );
108 int vlclua_dir_list( const char *luadirname, char **ppsz_dir_list )
111 char *datadir = config_GetUserDataDir();
112 if( datadir == NULL )
115 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
116 datadir, luadirname ) < 0 )
124 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
126 const char *psz_vlcpath = config_GetDataDir();
127 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
128 psz_vlcpath, luadirname ) < 0 )
131 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
132 psz_vlcpath, luadirname ) < 0 )
138 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
139 config_GetDataDir (), luadirname ) < 0 )
146 void vlclua_dir_list_free( char **ppsz_dir_list )
149 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
153 /*****************************************************************************
154 * Will execute func on all scripts in luadirname, and stop if func returns
156 *****************************************************************************/
157 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
158 const char * luadirname,
159 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
163 int i_ret = VLC_EGENERIC;
165 char **ppsz_filelist = NULL;
166 char **ppsz_fileend = NULL;
169 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
172 i_ret = vlclua_dir_list( luadirname, ppsz_dir_list );
173 if( i_ret != VLC_SUCCESS )
175 i_ret = VLC_EGENERIC;
178 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
184 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
187 free( ppsz_filelist );
188 ppsz_filelist = NULL;
191 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
192 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
194 if( i_files < 1 ) continue;
195 ppsz_fileend = ppsz_filelist + i_files;
197 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
200 if( asprintf( &psz_filename,
201 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
203 vlclua_dir_list_free( ppsz_dir_list );
206 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
208 i_ret = func( p_this, psz_filename, L, user_data );
210 free( psz_filename );
212 if( i_ret == VLC_SUCCESS ) break;
214 if( i_ret == VLC_SUCCESS ) break;
219 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
222 free( ppsz_filelist );
224 vlclua_dir_list_free( ppsz_dir_list );
230 /*****************************************************************************
231 * Meta data setters utility.
232 * Playlist item table should be on top of the stack when these are called
233 *****************************************************************************/
234 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
235 input_item_t *p_input )
237 #define TRY_META( a, b ) \
238 lua_getfield( L, -1, a ); \
239 if( lua_isstring( L, -1 ) ) \
241 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
242 EnsureUTF8( psz_value ); \
243 msg_Dbg( p_this, #b ": %s", psz_value ); \
244 input_item_Set ## b ( p_input, psz_value ); \
247 lua_pop( L, 1 ); /* pop a */
248 TRY_META( "title", Title );
249 TRY_META( "artist", Artist );
250 TRY_META( "genre", Genre );
251 TRY_META( "copyright", Copyright );
252 TRY_META( "album", Album );
253 TRY_META( "tracknum", TrackNum );
254 TRY_META( "description", Description );
255 TRY_META( "rating", Rating );
256 TRY_META( "date", Date );
257 TRY_META( "setting", Setting );
258 TRY_META( "url", URL );
259 TRY_META( "language", Language );
260 TRY_META( "nowplaying", NowPlaying );
261 TRY_META( "publisher", Publisher );
262 TRY_META( "encodedby", EncodedBy );
263 TRY_META( "arturl", ArtURL );
264 TRY_META( "trackid", TrackID );
267 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
268 input_item_t *p_input )
271 lua_getfield( L, -1, "meta" );
273 if( lua_istable( L, -1 ) )
276 /* ... item meta nil */
277 while( lua_next( L, -2 ) )
279 /* ... item meta key value */
280 if( !lua_isstring( L, -2 ) )
282 msg_Warn( p_this, "Custom meta data category name must be "
285 else if( !lua_istable( L, -1 ) )
287 msg_Warn( p_this, "Custom meta data category contents "
292 const char *psz_meta_category = lua_tostring( L, -2 );
293 msg_Dbg( p_this, "Found custom meta data category: %s",
296 /* ... item meta key value nil */
297 while( lua_next( L, -2 ) )
299 /* ... item meta key value key2 value2 */
300 if( !lua_isstring( L, -2 ) )
302 msg_Warn( p_this, "Custom meta category item name "
303 "must be a string." );
305 else if( !lua_isstring( L, -1 ) )
307 msg_Warn( p_this, "Custom meta category item value "
308 "must be a string." );
312 const char *psz_meta_name =
313 lua_tostring( L, -2 );
314 const char *psz_meta_value =
315 lua_tostring( L, -1 );
316 msg_Dbg( p_this, "Custom meta %s, %s: %s",
317 psz_meta_category, psz_meta_name,
319 input_item_AddInfo( p_input, psz_meta_category,
320 psz_meta_name, psz_meta_value );
322 lua_pop( L, 1 ); /* pop item */
323 /* ... item meta key value key2 */
325 /* ... item meta key value */
327 lua_pop( L, 1 ); /* pop category */
328 /* ... item meta key */
332 lua_pop( L, 1 ); /* pop "meta" */
333 /* ... item -> back to original stack */
336 /*****************************************************************************
338 ****************************************************************************/
340 * Playlist item table should be on top of the stack when this is called
342 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
343 int *pi_options, char ***pppsz_options )
345 lua_getfield( L, -1, "options" );
346 if( lua_istable( L, -1 ) )
349 while( lua_next( L, -2 ) )
351 if( lua_isstring( L, -1 ) )
353 char *psz_option = strdup( lua_tostring( L, -1 ) );
354 msg_Dbg( p_this, "Option: %s", psz_option );
355 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
360 msg_Warn( p_this, "Option should be a string" );
362 lua_pop( L, 1 ); /* pop option */
365 lua_pop( L, 1 ); /* pop "options" */
368 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
369 playlist_t *p_playlist,
370 input_item_t *p_parent, bool b_play )
374 assert( p_parent || p_playlist );
377 if( lua_istable( L, -1 ) )
381 while( lua_next( L, -2 ) )
383 /* playlist key item */
384 /* <Parse playlist item> */
385 if( lua_istable( L, -1 ) )
387 lua_getfield( L, -1, "path" );
388 /* playlist key item path */
389 if( lua_isstring( L, -1 ) )
391 const char *psz_path = NULL;
392 const char *psz_name = NULL;
393 char **ppsz_options = NULL;
395 mtime_t i_duration = -1;
396 input_item_t *p_input;
398 /* Read path and name */
399 psz_path = lua_tostring( L, -1 );
400 msg_Dbg( p_this, "Path: %s", psz_path );
401 lua_getfield( L, -2, "name" );
402 /* playlist key item path name */
403 if( lua_isstring( L, -1 ) )
405 psz_name = lua_tostring( L, -1 );
406 msg_Dbg( p_this, "Name: %s", psz_name );
410 if( !lua_isnil( L, -1 ) )
411 msg_Warn( p_this, "Playlist item name should be a string." );
416 lua_getfield( L, -3, "duration" );
417 /* playlist key item path name duration */
418 if( lua_isnumber( L, -1 ) )
420 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
422 else if( !lua_isnil( L, -1 ) )
424 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
426 lua_pop( L, 1 ); /* pop "duration" */
428 /* playlist key item path name */
430 /* Read options: item must be on top of stack */
431 lua_pushvalue( L, -3 );
432 /* playlist key item path name item */
433 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
435 /* Create input item */
436 p_input = input_item_NewExt( p_playlist, psz_path,
438 (const char **)ppsz_options,
439 VLC_INPUT_OPTION_TRUSTED,
441 lua_pop( L, 3 ); /* pop "path name item" */
442 /* playlist key item */
444 /* Read meta data: item must be on top of stack */
445 vlclua_read_meta_data( p_this, L, p_input );
447 /* Read custom meta data: item must be on top of stack*/
448 vlclua_read_custom_meta_data( p_this, L, p_input );
450 /* Append item to playlist */
451 if( p_parent ) /* Add to node */
452 input_item_AddSubItem( p_parent, p_input );
453 else /* Play or Enqueue (preparse) */
454 /* FIXME: playlist_AddInput() can fail */
455 playlist_AddInput( p_playlist, p_input,
457 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
458 PLAYLIST_END, true, false );
459 i_count ++; /* increment counter */
460 vlc_gc_decref( p_input );
461 while( i_options > 0 )
462 free( ppsz_options[--i_options] );
463 free( ppsz_options );
467 lua_pop( L, 1 ); /* pop "path" */
469 "Playlist item's path should be a string" );
471 /* playlist key item */
475 msg_Warn( p_this, "Playlist item should be a table" );
477 /* <Parse playlist item> */
478 lua_pop( L, 1 ); /* pop the value, keep the key for
479 * the next lua_next() call */
486 msg_Warn( p_this, "Playlist should be a table." );