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 set_shortname( N_( "Lua Meta Fetcher" ) )
68 set_description( N_("Fetch meta data using lua scripts") )
69 set_capability( "meta fetcher", 10 )
70 set_callbacks( FetchMeta, NULL )
73 set_shortname( N_( "Lua Meta Reader" ) )
74 set_description( N_("Read meta data using lua scripts") )
75 set_capability( "meta reader", 10 )
76 set_callbacks( ReadMeta, NULL )
79 add_shortcut( "luaplaylist" )
80 set_category( CAT_INPUT )
81 set_subcategory( SUBCAT_INPUT_DEMUX )
82 set_shortname( N_("Lua Playlist") )
83 set_description( N_("Lua Playlist Parser Interface") )
84 set_capability( "demux", 2 )
85 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
88 set_description( N_("Lua Interface Module (shortcuts)") )
89 add_shortcut( "luarc" )
91 set_capability( "interface", 25 )
92 set_callbacks( Open_LuaIntf, Close_LuaIntf )
95 set_description( N_("Lua Interface Module") )
96 add_shortcut( "luaintf" )
97 add_shortcut( "luahttp" )
98 add_shortcut( "http" )
99 add_shortcut( "luatelnet" )
100 add_shortcut( "telnet" )
101 add_shortcut( "luahotkeys" )
102 /* add_shortcut( "hotkeys" ) */
103 set_capability( "interface", 0 )
104 add_string( "lua-intf", "dummy", NULL,
105 INTF_TEXT, INTF_LONGTEXT, false )
106 add_string( "lua-config", "", NULL,
107 CONFIG_TEXT, CONFIG_LONGTEXT, false )
108 set_callbacks( Open_LuaIntf, Close_LuaIntf )
111 set_shortname( N_("Lua Extension") )
112 add_shortcut( "luaextension" )
113 set_capability( "extension", 1 )
114 set_callbacks( Open_Extension, Close_Extension )
117 /*****************************************************************************
119 *****************************************************************************/
120 static int file_select( const char *file )
122 int i = strlen( file );
123 return i > 4 && !strcmp( file+i-4, ".lua" );
126 static int file_compare( const char **a, const char **b )
128 return strcmp( *a, *b );
131 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
134 char *datadir = config_GetUserDir( VLC_DATA_DIR );
135 if( datadir == NULL )
138 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
139 datadir, luadirname ) < 0 )
147 char *psz_datapath = config_GetDataDir( p_this );
148 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
150 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
151 psz_datapath, luadirname ) < 0 )
154 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
155 psz_datapath, luadirname ) < 0 )
161 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
162 psz_datapath, luadirname ) < 0 )
166 free( psz_datapath );
170 void vlclua_dir_list_free( char **ppsz_dir_list )
173 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
177 /*****************************************************************************
178 * Will execute func on all scripts in luadirname, and stop if func returns
180 *****************************************************************************/
181 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
182 const char * luadirname,
183 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
187 int i_ret = VLC_EGENERIC;
189 char **ppsz_filelist = NULL;
190 char **ppsz_fileend = NULL;
193 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
196 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
197 if( i_ret != VLC_SUCCESS )
199 i_ret = VLC_EGENERIC;
202 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
208 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
211 free( ppsz_filelist );
212 ppsz_filelist = NULL;
215 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
216 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
218 if( i_files < 1 ) continue;
219 ppsz_fileend = ppsz_filelist + i_files;
221 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
224 if( asprintf( &psz_filename,
225 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
227 vlclua_dir_list_free( ppsz_dir_list );
230 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
232 i_ret = func( p_this, psz_filename, L, user_data );
234 free( psz_filename );
236 if( i_ret == VLC_SUCCESS ) break;
238 if( i_ret == VLC_SUCCESS ) break;
243 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
246 free( ppsz_filelist );
248 vlclua_dir_list_free( ppsz_dir_list );
254 /*****************************************************************************
255 * Meta data setters utility.
256 * Playlist item table should be on top of the stack when these are called
257 *****************************************************************************/
258 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
259 input_item_t *p_input )
261 #define TRY_META( a, b ) \
262 lua_getfield( L, -1, a ); \
263 if( lua_isstring( L, -1 ) ) \
265 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
266 EnsureUTF8( psz_value ); \
267 msg_Dbg( p_this, #b ": %s", psz_value ); \
268 input_item_Set ## b ( p_input, psz_value ); \
271 lua_pop( L, 1 ); /* pop a */
272 TRY_META( "title", Title );
273 TRY_META( "artist", Artist );
274 TRY_META( "genre", Genre );
275 TRY_META( "copyright", Copyright );
276 TRY_META( "album", Album );
277 TRY_META( "tracknum", TrackNum );
278 TRY_META( "description", Description );
279 TRY_META( "rating", Rating );
280 TRY_META( "date", Date );
281 TRY_META( "setting", Setting );
282 TRY_META( "url", URL );
283 TRY_META( "language", Language );
284 TRY_META( "nowplaying", NowPlaying );
285 TRY_META( "publisher", Publisher );
286 TRY_META( "encodedby", EncodedBy );
287 TRY_META( "arturl", ArtURL );
288 TRY_META( "trackid", TrackID );
291 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
292 input_item_t *p_input )
295 lua_getfield( L, -1, "meta" );
297 if( lua_istable( L, -1 ) )
300 /* ... item meta nil */
301 while( lua_next( L, -2 ) )
303 /* ... item meta key value */
304 if( !lua_isstring( L, -2 ) )
306 msg_Warn( p_this, "Custom meta data category name must be "
309 else if( !lua_istable( L, -1 ) )
311 msg_Warn( p_this, "Custom meta data category contents "
316 const char *psz_meta_category = lua_tostring( L, -2 );
317 msg_Dbg( p_this, "Found custom meta data category: %s",
320 /* ... item meta key value nil */
321 while( lua_next( L, -2 ) )
323 /* ... item meta key value key2 value2 */
324 if( !lua_isstring( L, -2 ) )
326 msg_Warn( p_this, "Custom meta category item name "
327 "must be a string." );
329 else if( !lua_isstring( L, -1 ) )
331 msg_Warn( p_this, "Custom meta category item value "
332 "must be a string." );
336 const char *psz_meta_name =
337 lua_tostring( L, -2 );
338 const char *psz_meta_value =
339 lua_tostring( L, -1 );
340 msg_Dbg( p_this, "Custom meta %s, %s: %s",
341 psz_meta_category, psz_meta_name,
343 input_item_AddInfo( p_input, psz_meta_category,
344 psz_meta_name, "%s", psz_meta_value );
346 lua_pop( L, 1 ); /* pop item */
347 /* ... item meta key value key2 */
349 /* ... item meta key value */
351 lua_pop( L, 1 ); /* pop category */
352 /* ... item meta key */
356 lua_pop( L, 1 ); /* pop "meta" */
357 /* ... item -> back to original stack */
360 /*****************************************************************************
362 ****************************************************************************/
364 * Playlist item table should be on top of the stack when this is called
366 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
367 int *pi_options, char ***pppsz_options )
369 lua_getfield( L, -1, "options" );
370 if( lua_istable( L, -1 ) )
373 while( lua_next( L, -2 ) )
375 if( lua_isstring( L, -1 ) )
377 char *psz_option = strdup( lua_tostring( L, -1 ) );
378 msg_Dbg( p_this, "Option: %s", psz_option );
379 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
384 msg_Warn( p_this, "Option should be a string" );
386 lua_pop( L, 1 ); /* pop option */
389 lua_pop( L, 1 ); /* pop "options" */
392 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
393 playlist_t *p_playlist,
394 input_item_t *p_parent, bool b_play )
398 assert( p_parent || p_playlist );
401 if( lua_istable( L, -1 ) )
405 while( lua_next( L, -2 ) )
407 /* playlist key item */
408 /* <Parse playlist item> */
409 if( lua_istable( L, -1 ) )
411 lua_getfield( L, -1, "path" );
412 /* playlist key item path */
413 if( lua_isstring( L, -1 ) )
415 const char *psz_path = NULL;
416 const char *psz_name = NULL;
417 char **ppsz_options = NULL;
419 mtime_t i_duration = -1;
420 input_item_t *p_input;
422 /* Read path and name */
423 psz_path = lua_tostring( L, -1 );
424 msg_Dbg( p_this, "Path: %s", psz_path );
425 lua_getfield( L, -2, "name" );
426 /* playlist key item path name */
427 if( lua_isstring( L, -1 ) )
429 psz_name = lua_tostring( L, -1 );
430 msg_Dbg( p_this, "Name: %s", psz_name );
434 if( !lua_isnil( L, -1 ) )
435 msg_Warn( p_this, "Playlist item name should be a string." );
440 lua_getfield( L, -3, "duration" );
441 /* playlist key item path name duration */
442 if( lua_isnumber( L, -1 ) )
444 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
446 else if( !lua_isnil( L, -1 ) )
448 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
450 lua_pop( L, 1 ); /* pop "duration" */
452 /* playlist key item path name */
454 /* Read options: item must be on top of stack */
455 lua_pushvalue( L, -3 );
456 /* playlist key item path name item */
457 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
459 /* Create input item */
460 p_input = input_item_NewExt( p_playlist, psz_path,
462 (const char **)ppsz_options,
463 VLC_INPUT_OPTION_TRUSTED,
465 lua_pop( L, 3 ); /* pop "path name item" */
466 /* playlist key item */
468 /* Read meta data: item must be on top of stack */
469 vlclua_read_meta_data( p_this, L, p_input );
471 /* Read custom meta data: item must be on top of stack*/
472 vlclua_read_custom_meta_data( p_this, L, p_input );
474 /* Append item to playlist */
475 if( p_parent ) /* Add to node */
476 input_item_AddSubItem( p_parent, p_input );
477 else /* Play or Enqueue (preparse) */
478 /* FIXME: playlist_AddInput() can fail */
479 playlist_AddInput( p_playlist, p_input,
481 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
482 PLAYLIST_END, true, false );
483 i_count ++; /* increment counter */
484 vlc_gc_decref( p_input );
485 while( i_options > 0 )
486 free( ppsz_options[--i_options] );
487 free( ppsz_options );
491 lua_pop( L, 1 ); /* pop "path" */
493 "Playlist item's path should be a string" );
495 /* playlist key item */
499 msg_Warn( p_this, "Playlist item should be a table" );
501 /* <Parse playlist item> */
502 lua_pop( L, 1 ); /* pop the value, keep the key for
503 * the next lua_next() call */
510 msg_Warn( p_this, "Playlist should be a table." );