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 Reader" ) )
68 set_description( N_("Fetch meta data using lua scripts") )
69 set_capability( "meta reader", 10 )
70 set_callbacks( ReadMeta, NULL )
73 add_shortcut( "luaplaylist" )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_DEMUX )
76 set_shortname( N_("Lua Playlist") )
77 set_description( N_("Lua Playlist Parser Interface") )
78 set_capability( "demux", 2 )
79 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
82 set_description( N_("Lua Interface Module (shortcuts)") )
83 add_shortcut( "luarc" )
85 set_capability( "interface", 25 )
86 set_callbacks( Open_LuaIntf, Close_LuaIntf )
89 set_description( N_("Lua Interface Module") )
90 add_shortcut( "luaintf" )
91 add_shortcut( "luahttp" )
92 add_shortcut( "http" )
93 add_shortcut( "luatelnet" )
94 add_shortcut( "telnet" )
95 add_shortcut( "luahotkeys" )
96 /* add_shortcut( "hotkeys" ) */
97 set_capability( "interface", 0 )
98 add_string( "lua-intf", "dummy", NULL,
99 INTF_TEXT, INTF_LONGTEXT, false )
100 add_string( "lua-config", "", NULL,
101 CONFIG_TEXT, CONFIG_LONGTEXT, false )
102 set_callbacks( Open_LuaIntf, Close_LuaIntf )
105 /*****************************************************************************
107 *****************************************************************************/
108 static int file_select( const char *file )
110 int i = strlen( file );
111 return i > 4 && !strcmp( file+i-4, ".lua" );
114 static int file_compare( const char **a, const char **b )
116 return strcmp( *a, *b );
119 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
122 char *datadir = config_GetUserDir( VLC_DATA_DIR );
123 if( datadir == NULL )
126 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
127 datadir, luadirname ) < 0 )
135 char *psz_datapath = config_GetDataDir( p_this );
136 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
138 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
139 psz_datapath, luadirname ) < 0 )
142 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
143 psz_datapath, luadirname ) < 0 )
149 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
150 psz_datapath, luadirname ) < 0 )
154 free( psz_datapath );
158 void vlclua_dir_list_free( char **ppsz_dir_list )
161 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
165 /*****************************************************************************
166 * Will execute func on all scripts in luadirname, and stop if func returns
168 *****************************************************************************/
169 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
170 const char * luadirname,
171 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
175 int i_ret = VLC_EGENERIC;
177 char **ppsz_filelist = NULL;
178 char **ppsz_fileend = NULL;
181 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
184 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
185 if( i_ret != VLC_SUCCESS )
187 i_ret = VLC_EGENERIC;
190 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
196 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
199 free( ppsz_filelist );
200 ppsz_filelist = NULL;
203 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
204 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
206 if( i_files < 1 ) continue;
207 ppsz_fileend = ppsz_filelist + i_files;
209 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
212 if( asprintf( &psz_filename,
213 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
215 vlclua_dir_list_free( ppsz_dir_list );
218 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
220 i_ret = func( p_this, psz_filename, L, user_data );
222 free( psz_filename );
224 if( i_ret == VLC_SUCCESS ) break;
226 if( i_ret == VLC_SUCCESS ) break;
231 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
234 free( ppsz_filelist );
236 vlclua_dir_list_free( ppsz_dir_list );
242 /*****************************************************************************
243 * Meta data setters utility.
244 * Playlist item table should be on top of the stack when these are called
245 *****************************************************************************/
246 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
247 input_item_t *p_input )
249 #define TRY_META( a, b ) \
250 lua_getfield( L, -1, a ); \
251 if( lua_isstring( L, -1 ) ) \
253 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
254 EnsureUTF8( psz_value ); \
255 msg_Dbg( p_this, #b ": %s", psz_value ); \
256 input_item_Set ## b ( p_input, psz_value ); \
259 lua_pop( L, 1 ); /* pop a */
260 TRY_META( "title", Title );
261 TRY_META( "artist", Artist );
262 TRY_META( "genre", Genre );
263 TRY_META( "copyright", Copyright );
264 TRY_META( "album", Album );
265 TRY_META( "tracknum", TrackNum );
266 TRY_META( "description", Description );
267 TRY_META( "rating", Rating );
268 TRY_META( "date", Date );
269 TRY_META( "setting", Setting );
270 TRY_META( "url", URL );
271 TRY_META( "language", Language );
272 TRY_META( "nowplaying", NowPlaying );
273 TRY_META( "publisher", Publisher );
274 TRY_META( "encodedby", EncodedBy );
275 TRY_META( "arturl", ArtURL );
276 TRY_META( "trackid", TrackID );
279 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
280 input_item_t *p_input )
283 lua_getfield( L, -1, "meta" );
285 if( lua_istable( L, -1 ) )
288 /* ... item meta nil */
289 while( lua_next( L, -2 ) )
291 /* ... item meta key value */
292 if( !lua_isstring( L, -2 ) )
294 msg_Warn( p_this, "Custom meta data category name must be "
297 else if( !lua_istable( L, -1 ) )
299 msg_Warn( p_this, "Custom meta data category contents "
304 const char *psz_meta_category = lua_tostring( L, -2 );
305 msg_Dbg( p_this, "Found custom meta data category: %s",
308 /* ... item meta key value nil */
309 while( lua_next( L, -2 ) )
311 /* ... item meta key value key2 value2 */
312 if( !lua_isstring( L, -2 ) )
314 msg_Warn( p_this, "Custom meta category item name "
315 "must be a string." );
317 else if( !lua_isstring( L, -1 ) )
319 msg_Warn( p_this, "Custom meta category item value "
320 "must be a string." );
324 const char *psz_meta_name =
325 lua_tostring( L, -2 );
326 const char *psz_meta_value =
327 lua_tostring( L, -1 );
328 msg_Dbg( p_this, "Custom meta %s, %s: %s",
329 psz_meta_category, psz_meta_name,
331 input_item_AddInfo( p_input, psz_meta_category,
332 psz_meta_name, "%s", psz_meta_value );
334 lua_pop( L, 1 ); /* pop item */
335 /* ... item meta key value key2 */
337 /* ... item meta key value */
339 lua_pop( L, 1 ); /* pop category */
340 /* ... item meta key */
344 lua_pop( L, 1 ); /* pop "meta" */
345 /* ... item -> back to original stack */
348 /*****************************************************************************
350 ****************************************************************************/
352 * Playlist item table should be on top of the stack when this is called
354 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
355 int *pi_options, char ***pppsz_options )
357 lua_getfield( L, -1, "options" );
358 if( lua_istable( L, -1 ) )
361 while( lua_next( L, -2 ) )
363 if( lua_isstring( L, -1 ) )
365 char *psz_option = strdup( lua_tostring( L, -1 ) );
366 msg_Dbg( p_this, "Option: %s", psz_option );
367 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
372 msg_Warn( p_this, "Option should be a string" );
374 lua_pop( L, 1 ); /* pop option */
377 lua_pop( L, 1 ); /* pop "options" */
380 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
381 playlist_t *p_playlist,
382 input_item_t *p_parent, bool b_play )
386 assert( p_parent || p_playlist );
389 if( lua_istable( L, -1 ) )
393 while( lua_next( L, -2 ) )
395 /* playlist key item */
396 /* <Parse playlist item> */
397 if( lua_istable( L, -1 ) )
399 lua_getfield( L, -1, "path" );
400 /* playlist key item path */
401 if( lua_isstring( L, -1 ) )
403 const char *psz_path = NULL;
404 const char *psz_name = NULL;
405 char **ppsz_options = NULL;
407 mtime_t i_duration = -1;
408 input_item_t *p_input;
410 /* Read path and name */
411 psz_path = lua_tostring( L, -1 );
412 msg_Dbg( p_this, "Path: %s", psz_path );
413 lua_getfield( L, -2, "name" );
414 /* playlist key item path name */
415 if( lua_isstring( L, -1 ) )
417 psz_name = lua_tostring( L, -1 );
418 msg_Dbg( p_this, "Name: %s", psz_name );
422 if( !lua_isnil( L, -1 ) )
423 msg_Warn( p_this, "Playlist item name should be a string." );
428 lua_getfield( L, -3, "duration" );
429 /* playlist key item path name duration */
430 if( lua_isnumber( L, -1 ) )
432 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
434 else if( !lua_isnil( L, -1 ) )
436 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
438 lua_pop( L, 1 ); /* pop "duration" */
440 /* playlist key item path name */
442 /* Read options: item must be on top of stack */
443 lua_pushvalue( L, -3 );
444 /* playlist key item path name item */
445 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
447 /* Create input item */
448 p_input = input_item_NewExt( p_playlist, psz_path,
450 (const char **)ppsz_options,
451 VLC_INPUT_OPTION_TRUSTED,
453 lua_pop( L, 3 ); /* pop "path name item" */
454 /* playlist key item */
456 /* Read meta data: item must be on top of stack */
457 vlclua_read_meta_data( p_this, L, p_input );
459 /* Read custom meta data: item must be on top of stack*/
460 vlclua_read_custom_meta_data( p_this, L, p_input );
462 /* Append item to playlist */
463 if( p_parent ) /* Add to node */
464 input_item_AddSubItem( p_parent, p_input );
465 else /* Play or Enqueue (preparse) */
466 /* FIXME: playlist_AddInput() can fail */
467 playlist_AddInput( p_playlist, p_input,
469 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
470 PLAYLIST_END, true, false );
471 i_count ++; /* increment counter */
472 vlc_gc_decref( p_input );
473 while( i_options > 0 )
474 free( ppsz_options[--i_options] );
475 free( ppsz_options );
479 lua_pop( L, 1 ); /* pop "path" */
481 "Playlist item's path should be a string" );
483 /* playlist key item */
487 msg_Warn( p_this, "Playlist item should be a table" );
489 /* <Parse playlist item> */
490 lua_pop( L, 1 ); /* pop the value, keep the key for
491 * the next lua_next() call */
498 msg_Warn( p_this, "Playlist should be a table." );