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 set_description( N_("Lua Interface Module (shortcuts)") )
77 add_shortcut( "luarc" )
79 set_capability( "interface", 15 )
80 set_callbacks( Open_LuaIntf, Close_LuaIntf )
83 set_description( N_("Lua Interface Module") )
84 add_shortcut( "luaintf" )
85 add_shortcut( "luahttp" )
86 add_shortcut( "http" )
87 add_shortcut( "luatelnet" )
88 add_shortcut( "telnet" )
89 add_shortcut( "luahotkeys" )
90 /* add_shortcut( "hotkeys" ) */
91 set_capability( "interface", 0 )
92 add_string( "lua-intf", "dummy", NULL,
93 INTF_TEXT, INTF_LONGTEXT, false )
94 add_string( "lua-config", "", NULL,
95 CONFIG_TEXT, CONFIG_LONGTEXT, false )
96 set_callbacks( Open_LuaIntf, Close_LuaIntf )
99 /*****************************************************************************
101 *****************************************************************************/
102 static int file_select( const char *file )
104 int i = strlen( file );
105 return i > 4 && !strcmp( file+i-4, ".lua" );
108 static int file_compare( const char **a, const char **b )
110 return strcmp( *a, *b );
113 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
116 char *datadir = config_GetUserDir( VLC_DATA_DIR );
117 if( datadir == NULL )
120 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
121 datadir, luadirname ) < 0 )
129 char *psz_datapath = config_GetDataDir( p_this );
130 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
132 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
133 psz_datapath, luadirname ) < 0 )
136 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
137 psz_datapath, luadirname ) < 0 )
143 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
144 psz_datapath, luadirname ) < 0 )
148 free( psz_datapath );
152 void vlclua_dir_list_free( char **ppsz_dir_list )
155 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
159 /*****************************************************************************
160 * Will execute func on all scripts in luadirname, and stop if func returns
162 *****************************************************************************/
163 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
164 const char * luadirname,
165 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
169 int i_ret = VLC_EGENERIC;
171 char **ppsz_filelist = NULL;
172 char **ppsz_fileend = NULL;
175 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
178 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
179 if( i_ret != VLC_SUCCESS )
181 i_ret = VLC_EGENERIC;
184 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
190 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
193 free( ppsz_filelist );
194 ppsz_filelist = NULL;
197 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
198 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
200 if( i_files < 1 ) continue;
201 ppsz_fileend = ppsz_filelist + i_files;
203 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
206 if( asprintf( &psz_filename,
207 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
209 vlclua_dir_list_free( ppsz_dir_list );
212 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
214 i_ret = func( p_this, psz_filename, L, user_data );
216 free( psz_filename );
218 if( i_ret == VLC_SUCCESS ) break;
220 if( i_ret == VLC_SUCCESS ) break;
225 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
228 free( ppsz_filelist );
230 vlclua_dir_list_free( ppsz_dir_list );
236 /*****************************************************************************
237 * Meta data setters utility.
238 * Playlist item table should be on top of the stack when these are called
239 *****************************************************************************/
240 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
241 input_item_t *p_input )
243 #define TRY_META( a, b ) \
244 lua_getfield( L, -1, a ); \
245 if( lua_isstring( L, -1 ) ) \
247 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
248 EnsureUTF8( psz_value ); \
249 msg_Dbg( p_this, #b ": %s", psz_value ); \
250 input_item_Set ## b ( p_input, psz_value ); \
253 lua_pop( L, 1 ); /* pop a */
254 TRY_META( "title", Title );
255 TRY_META( "artist", Artist );
256 TRY_META( "genre", Genre );
257 TRY_META( "copyright", Copyright );
258 TRY_META( "album", Album );
259 TRY_META( "tracknum", TrackNum );
260 TRY_META( "description", Description );
261 TRY_META( "rating", Rating );
262 TRY_META( "date", Date );
263 TRY_META( "setting", Setting );
264 TRY_META( "url", URL );
265 TRY_META( "language", Language );
266 TRY_META( "nowplaying", NowPlaying );
267 TRY_META( "publisher", Publisher );
268 TRY_META( "encodedby", EncodedBy );
269 TRY_META( "arturl", ArtURL );
270 TRY_META( "trackid", TrackID );
273 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
274 input_item_t *p_input )
277 lua_getfield( L, -1, "meta" );
279 if( lua_istable( L, -1 ) )
282 /* ... item meta nil */
283 while( lua_next( L, -2 ) )
285 /* ... item meta key value */
286 if( !lua_isstring( L, -2 ) )
288 msg_Warn( p_this, "Custom meta data category name must be "
291 else if( !lua_istable( L, -1 ) )
293 msg_Warn( p_this, "Custom meta data category contents "
298 const char *psz_meta_category = lua_tostring( L, -2 );
299 msg_Dbg( p_this, "Found custom meta data category: %s",
302 /* ... item meta key value nil */
303 while( lua_next( L, -2 ) )
305 /* ... item meta key value key2 value2 */
306 if( !lua_isstring( L, -2 ) )
308 msg_Warn( p_this, "Custom meta category item name "
309 "must be a string." );
311 else if( !lua_isstring( L, -1 ) )
313 msg_Warn( p_this, "Custom meta category item value "
314 "must be a string." );
318 const char *psz_meta_name =
319 lua_tostring( L, -2 );
320 const char *psz_meta_value =
321 lua_tostring( L, -1 );
322 msg_Dbg( p_this, "Custom meta %s, %s: %s",
323 psz_meta_category, psz_meta_name,
325 input_item_AddInfo( p_input, psz_meta_category,
326 psz_meta_name, "%s", psz_meta_value );
328 lua_pop( L, 1 ); /* pop item */
329 /* ... item meta key value key2 */
331 /* ... item meta key value */
333 lua_pop( L, 1 ); /* pop category */
334 /* ... item meta key */
338 lua_pop( L, 1 ); /* pop "meta" */
339 /* ... item -> back to original stack */
342 /*****************************************************************************
344 ****************************************************************************/
346 * Playlist item table should be on top of the stack when this is called
348 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
349 int *pi_options, char ***pppsz_options )
351 lua_getfield( L, -1, "options" );
352 if( lua_istable( L, -1 ) )
355 while( lua_next( L, -2 ) )
357 if( lua_isstring( L, -1 ) )
359 char *psz_option = strdup( lua_tostring( L, -1 ) );
360 msg_Dbg( p_this, "Option: %s", psz_option );
361 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
366 msg_Warn( p_this, "Option should be a string" );
368 lua_pop( L, 1 ); /* pop option */
371 lua_pop( L, 1 ); /* pop "options" */
374 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
375 playlist_t *p_playlist,
376 input_item_t *p_parent, bool b_play )
380 assert( p_parent || p_playlist );
383 if( lua_istable( L, -1 ) )
387 while( lua_next( L, -2 ) )
389 /* playlist key item */
390 /* <Parse playlist item> */
391 if( lua_istable( L, -1 ) )
393 lua_getfield( L, -1, "path" );
394 /* playlist key item path */
395 if( lua_isstring( L, -1 ) )
397 const char *psz_path = NULL;
398 const char *psz_name = NULL;
399 char **ppsz_options = NULL;
401 mtime_t i_duration = -1;
402 input_item_t *p_input;
404 /* Read path and name */
405 psz_path = lua_tostring( L, -1 );
406 msg_Dbg( p_this, "Path: %s", psz_path );
407 lua_getfield( L, -2, "name" );
408 /* playlist key item path name */
409 if( lua_isstring( L, -1 ) )
411 psz_name = lua_tostring( L, -1 );
412 msg_Dbg( p_this, "Name: %s", psz_name );
416 if( !lua_isnil( L, -1 ) )
417 msg_Warn( p_this, "Playlist item name should be a string." );
422 lua_getfield( L, -3, "duration" );
423 /* playlist key item path name duration */
424 if( lua_isnumber( L, -1 ) )
426 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
428 else if( !lua_isnil( L, -1 ) )
430 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
432 lua_pop( L, 1 ); /* pop "duration" */
434 /* playlist key item path name */
436 /* Read options: item must be on top of stack */
437 lua_pushvalue( L, -3 );
438 /* playlist key item path name item */
439 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
441 /* Create input item */
442 p_input = input_item_NewExt( p_playlist, psz_path,
444 (const char **)ppsz_options,
445 VLC_INPUT_OPTION_TRUSTED,
447 lua_pop( L, 3 ); /* pop "path name item" */
448 /* playlist key item */
450 /* Read meta data: item must be on top of stack */
451 vlclua_read_meta_data( p_this, L, p_input );
453 /* Read custom meta data: item must be on top of stack*/
454 vlclua_read_custom_meta_data( p_this, L, p_input );
456 /* Append item to playlist */
457 if( p_parent ) /* Add to node */
458 input_item_AddSubItem( p_parent, p_input );
459 else /* Play or Enqueue (preparse) */
460 /* FIXME: playlist_AddInput() can fail */
461 playlist_AddInput( p_playlist, p_input,
463 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
464 PLAYLIST_END, true, false );
465 i_count ++; /* increment counter */
466 vlc_gc_decref( p_input );
467 while( i_options > 0 )
468 free( ppsz_options[--i_options] );
469 free( ppsz_options );
473 lua_pop( L, 1 ); /* pop "path" */
475 "Playlist item's path should be a string" );
477 /* playlist key item */
481 msg_Warn( p_this, "Playlist item should be a table" );
483 /* <Parse playlist item> */
484 lua_pop( L, 1 ); /* pop the value, keep the key for
485 * the next lua_next() call */
492 msg_Warn( p_this, "Playlist should be a table." );