1 /*****************************************************************************
2 * vlc.c: Generic lua inteface functions
3 *****************************************************************************
4 * Copyright (C) 2007 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 *****************************************************************************/
34 #include <vlc_charset.h>
37 #include <lua.h> /* Low level lua C API */
38 #include <lauxlib.h> /* Higher level C API */
39 #include <lualib.h> /* Lua libs */
43 /*****************************************************************************
45 *****************************************************************************/
47 #define INTF_TEXT N_("Lua interface")
48 #define INTF_LONGTEXT N_("Lua interface module to load")
50 #define CONFIG_TEXT N_("Lua inteface configuration")
51 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
54 add_shortcut( "luameta" );
55 set_shortname( N_( "Lua Meta" ) );
56 set_description( _("Fetch metadata using lua scripts") );
57 set_capability( "meta fetcher", 10 );
58 set_callbacks( E_(FindMeta), NULL );
60 set_shortname( N_( "Lua Art" ) );
61 set_description( _("Fetch artwork using lua scripts") );
62 set_capability( "art finder", 10 );
63 set_callbacks( E_(FindArt), NULL );
65 add_shortcut( "luaplaylist" );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
68 set_shortname( _("Lua Playlist") );
69 set_description( _("Lua Playlist Parser Interface") );
70 set_capability( "demux2", 9 );
71 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
73 add_shortcut( "luaintf" );
74 add_shortcut( "luarc" );
75 /* add_shortcut( "rc" ); */
76 add_shortcut( "luahotkeys" );
77 /* add_shortcut( "hotkeys" ); */
78 add_shortcut( "luatelnet" );
79 /* add_shortcut( "telnet" ); */
80 set_description( _("Lua Interface Module") );
81 set_capability( "interface", 0 );
82 add_string( "lua-intf", "dummy", NULL,
83 INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
84 add_string( "lua-config", "", NULL,
85 CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
86 set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
89 /*****************************************************************************
90 * Internal lua<->vlc utils
91 *****************************************************************************/
92 vlc_object_t * vlclua_get_this( lua_State *L )
94 vlc_object_t * p_this;
95 lua_getglobal( L, "vlc" );
96 lua_getfield( L, -1, "private" );
97 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
102 /*****************************************************************************
103 * VLC error code translation
104 *****************************************************************************/
105 int vlclua_push_ret( lua_State *L, int i_error )
107 lua_pushnumber( L, i_error );
108 lua_pushstring( L, vlc_error( i_error ) );
112 /*****************************************************************************
113 * Get the VLC version string
114 *****************************************************************************/
115 int vlclua_version( lua_State *L )
117 lua_pushstring( L, VLC_Version() );
121 /*****************************************************************************
123 *****************************************************************************/
124 int vlclua_quit( lua_State *L )
126 vlc_object_t *p_this = vlclua_get_this( L );
127 /* The rc.c code also stops the playlist ... not sure if this is needed
129 vlc_object_kill( p_this->p_libvlc );
133 /*****************************************************************************
135 *****************************************************************************/
136 int vlclua_volume_set( lua_State *L )
138 vlc_object_t *p_this = vlclua_get_this( L );
139 int i_volume = luaL_checkint( L, -1 );
140 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
141 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
144 int vlclua_volume_get( lua_State *L )
146 vlc_object_t *p_this = vlclua_get_this( L );
147 audio_volume_t i_volume;
148 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
149 lua_pushnumber( L, i_volume );
155 int vlclua_volume_up( lua_State *L )
157 audio_volume_t i_volume;
158 aout_VolumeUp( vlclua_get_this( L ),
159 luaL_optint( L, 1, 1 ),
161 lua_pushnumber( L, i_volume );
165 int vlclua_volume_down( lua_State *L )
167 audio_volume_t i_volume;
168 aout_VolumeDown( vlclua_get_this( L ),
169 luaL_optint( L, 1, 1 ),
171 lua_pushnumber( L, i_volume );
175 /*****************************************************************************
177 *****************************************************************************/
178 int vlclua_stream_new( lua_State *L )
180 vlc_object_t * p_this = vlclua_get_this( L );
182 const char * psz_url;
183 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
184 psz_url = luaL_checkstring( L, -1 );
186 p_stream = stream_UrlNew( p_this, psz_url );
187 if( !p_stream ) return vlclua_error( L );
188 lua_pushlightuserdata( L, p_stream );
192 int vlclua_stream_read( lua_State *L )
198 if( lua_gettop( L ) != 2 ) return vlclua_error( L );
199 p_stream = (stream_t *)luaL_checklightuserdata( L, -2 );
200 n = luaL_checkint( L, -1 );
202 p_read = malloc( n );
203 if( !p_read ) return vlclua_error( L );
204 i_read = stream_Read( p_stream, p_read, n );
205 lua_pushlstring( L, (const char *)p_read, i_read );
210 int vlclua_stream_readline( lua_State *L )
213 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
214 p_stream = (stream_t *)luaL_checklightuserdata( L, -1 );
216 char *psz_line = stream_ReadLine( p_stream );
219 lua_pushstring( L, psz_line );
229 int vlclua_stream_delete( lua_State *L )
232 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
233 p_stream = (stream_t *)luaL_checklightuserdata( L, -1 );
235 stream_Delete( p_stream );
239 /*****************************************************************************
240 * String transformations
241 *****************************************************************************/
242 /* TODO: make it work for any number of arguments */
243 int vlclua_decode_uri( lua_State *L )
245 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
246 const char *psz_cstring = luaL_checkstring( L, 1 );
247 if( !psz_cstring ) return vlclua_error( L );
248 char *psz_string = strdup( psz_cstring );
250 decode_URI( psz_string );
251 lua_pushstring( L, psz_string );
256 /* TODO: make it work for any number of arguments */
257 int vlclua_resolve_xml_special_chars( lua_State *L )
259 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
260 const char *psz_cstring = luaL_checkstring( L, 1 );
261 if( !psz_cstring ) return vlclua_error( L );
262 char *psz_string = strdup( psz_cstring );
264 resolve_xml_special_chars( psz_string );
265 lua_pushstring( L, psz_string );
270 /*****************************************************************************
271 * Messaging facilities
272 *****************************************************************************/
273 /* TODO: make it work for any number of arguments */
274 int vlclua_msg_dbg( lua_State *L )
276 vlc_object_t *p_this = vlclua_get_this( L );
277 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
278 const char *psz_cstring = luaL_checkstring( L, 1 );
279 if( !psz_cstring ) return vlclua_error( L );
280 msg_Dbg( p_this, "%s", psz_cstring );
283 /* TODO: make it work for any number of arguments */
284 int vlclua_msg_warn( lua_State *L )
286 vlc_object_t *p_this = vlclua_get_this( L );
287 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
288 const char *psz_cstring = luaL_checkstring( L, 1 );
289 if( !psz_cstring ) return vlclua_error( L );
290 msg_Warn( p_this, "%s", psz_cstring );
293 /* TODO: make it work for any number of arguments */
294 int vlclua_msg_err( lua_State *L )
296 vlc_object_t *p_this = vlclua_get_this( L );
297 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
298 const char *psz_cstring = luaL_checkstring( L, 1 );
299 if( !psz_cstring ) return vlclua_error( L );
300 msg_Err( p_this, "%s", psz_cstring );
303 /* TODO: make it work for any number of arguments */
304 int vlclua_msg_info( lua_State *L )
306 vlc_object_t *p_this = vlclua_get_this( L );
307 if( lua_gettop( L ) != 1 ) return vlclua_error( L );
308 const char *psz_cstring = luaL_checkstring( L, 1 );
309 if( !psz_cstring ) return vlclua_error( L );
310 msg_Info( p_this, "%s", psz_cstring );
314 /*****************************************************************************
316 *****************************************************************************/
317 static int file_select( const char *file )
319 int i = strlen( file );
320 return i > 4 && !strcmp( file+i-4, ".lua" );
323 static int file_compare( const char **a, const char **b )
325 return strcmp( *a, *b );
328 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
329 char **ppsz_dir_list )
331 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
332 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
335 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
337 const char *psz_vlcpath = config_GetDataDir();
338 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
339 psz_vlcpath, luadirname ) < 0 )
342 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
343 psz_vlcpath, luadirname ) < 0 )
347 if( asprintf( &ppsz_dir_list[1],
348 "share" DIR_SEP "%s", luadirname ) < 0 )
351 # ifdef HAVE_SYS_STAT_H
353 struct stat stat_info;
354 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
355 || !S_ISDIR( stat_info.st_mode ) )
357 free(ppsz_dir_list[1]);
358 if( asprintf( &ppsz_dir_list[1],
359 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
368 /*****************************************************************************
369 * Will execute func on all scripts in luadirname, and stop if func returns
371 *****************************************************************************/
372 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
373 const char * luadirname,
374 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
378 int i_ret = VLC_EGENERIC;
381 char **ppsz_filelist = NULL;
382 char **ppsz_fileend = NULL;
385 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
388 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
389 if( i_ret != VLC_SUCCESS )
391 i_ret = VLC_EGENERIC;
394 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
400 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
403 free( ppsz_filelist );
404 ppsz_filelist = NULL;
412 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
413 dir = utf8_opendir( *ppsz_dir );
416 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
418 if( i_files < 1 ) continue;
419 ppsz_fileend = ppsz_filelist + i_files;
421 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
424 if( asprintf( &psz_filename,
425 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
427 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
429 i_ret = func( p_this, psz_filename, L, user_data );
431 free( psz_filename );
433 if( i_ret == VLC_SUCCESS ) break;
435 if( i_ret == VLC_SUCCESS ) break;
440 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
443 free( ppsz_filelist );
445 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
448 if( dir ) closedir( dir );
454 /*****************************************************************************
455 * Meta data setters utility.
456 * Playlist item table should be on top of the stack when these are called
457 *****************************************************************************/
458 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
459 input_item_t *p_input )
461 #define TRY_META( a, b ) \
462 lua_getfield( L, -1, a ); \
463 if( lua_isstring( L, -1 ) ) \
465 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
466 EnsureUTF8( psz_value ); \
467 msg_Dbg( p_this, #b ": %s", psz_value ); \
468 input_item_Set ## b ( p_input, psz_value ); \
471 lua_pop( L, 1 ); /* pop a */
472 TRY_META( "title", Title );
473 TRY_META( "artist", Artist );
474 TRY_META( "genre", Genre );
475 TRY_META( "copyright", Copyright );
476 TRY_META( "album", Album );
477 TRY_META( "tracknum", TrackNum );
478 TRY_META( "description", Description );
479 TRY_META( "rating", Rating );
480 TRY_META( "date", Date );
481 TRY_META( "setting", Setting );
482 TRY_META( "url", URL );
483 TRY_META( "language", Language );
484 TRY_META( "nowplaying", NowPlaying );
485 TRY_META( "publisher", Publisher );
486 TRY_META( "encodedby", EncodedBy );
487 TRY_META( "arturl", ArtURL );
488 TRY_META( "trackid", TrackID );
491 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
492 input_item_t *p_input )
495 lua_getfield( L, -1, "meta" );
497 if( lua_istable( L, -1 ) )
500 /* ... item meta nil */
501 while( lua_next( L, -2 ) )
503 /* ... item meta key value */
504 if( !lua_isstring( L, -2 ) )
506 msg_Warn( p_this, "Custom meta data category name must be "
509 else if( !lua_istable( L, -1 ) )
511 msg_Warn( p_this, "Custom meta data category contents "
516 const char *psz_meta_category = lua_tostring( L, -2 );
517 msg_Dbg( p_this, "Found custom meta data category: %s",
520 /* ... item meta key value nil */
521 while( lua_next( L, -2 ) )
523 /* ... item meta key value key2 value2 */
524 if( !lua_isstring( L, -2 ) )
526 msg_Warn( p_this, "Custom meta category item name "
527 "must be a string." );
529 else if( !lua_isstring( L, -1 ) )
531 msg_Warn( p_this, "Custom meta category item value "
532 "must be a string." );
536 const char *psz_meta_name =
537 lua_tostring( L, -2 );
538 const char *psz_meta_value =
539 lua_tostring( L, -1 );
540 msg_Dbg( p_this, "Custom meta %s, %s: %s",
541 psz_meta_category, psz_meta_name,
543 input_ItemAddInfo( p_input, psz_meta_category,
544 psz_meta_name, psz_meta_value );
546 lua_pop( L, 1 ); /* pop item */
547 /* ... item meta key value key2 */
549 /* ... item meta key value */
551 lua_pop( L, 1 ); /* pop category */
552 /* ... item meta key */
556 lua_pop( L, 1 ); /* pop "meta" */
557 /* ... item -> back to original stack */
560 /*****************************************************************************
562 ****************************************************************************/
564 * Playlist item table should be on top of the stack when this is called
566 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
567 int *pi_options, char ***pppsz_options )
569 lua_getfield( L, -1, "options" );
570 if( lua_istable( L, -1 ) )
573 while( lua_next( L, -2 ) )
575 if( lua_isstring( L, -1 ) )
577 char *psz_option = strdup( lua_tostring( L, -1 ) );
578 msg_Dbg( p_this, "Option: %s", psz_option );
579 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
584 msg_Warn( p_this, "Option should be a string" );
586 lua_pop( L, 1 ); /* pop option */
589 lua_pop( L, 1 ); /* pop "options" */
592 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
593 playlist_t *p_playlist,
594 input_item_t *p_parent, vlc_bool_t b_play )
599 if( lua_istable( L, -1 ) )
603 while( lua_next( L, -2 ) )
605 /* playlist key item */
606 /* <Parse playlist item> */
607 if( lua_istable( L, -1 ) )
609 lua_getfield( L, -1, "path" );
610 /* playlist key item path */
611 if( lua_isstring( L, -1 ) )
613 const char *psz_path = NULL;
614 const char *psz_name = NULL;
615 char **ppsz_options = NULL;
617 mtime_t i_duration = -1;
618 input_item_t *p_input;
620 /* Read path and name */
621 psz_path = lua_tostring( L, -1 );
622 msg_Dbg( p_this, "Path: %s", psz_path );
623 lua_getfield( L, -2, "name" );
624 /* playlist key item path name */
625 if( lua_isstring( L, -1 ) )
627 psz_name = lua_tostring( L, -1 );
628 msg_Dbg( p_this, "Name: %s", psz_name );
632 if( !lua_isnil( L, -1 ) )
633 msg_Warn( p_this, "Playlist item name should be a string." );
638 lua_getfield( L, -3, "duration" );
639 /* playlist key item path name duration */
640 if( lua_isnumber( L, -1 ) )
642 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
644 else if( !lua_isnil( L, -1 ) )
646 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
648 lua_pop( L, 1 ); /* pop "duration" */
650 /* playlist key item path name */
652 /* Read options: item must be on top of stack */
653 lua_pushvalue( L, -3 );
654 /* playlist key item path name item */
655 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
657 /* Create input item */
658 p_input = input_ItemNewExt( p_playlist, psz_path,
660 (const char **)ppsz_options,
662 lua_pop( L, 3 ); /* pop "path name item" */
663 /* playlist key item */
665 /* Read meta data: item must be on top of stack */
666 vlclua_read_meta_data( p_this, L, p_input );
668 /* Read custom meta data: item must be on top of stack*/
669 vlclua_read_custom_meta_data( p_this, L, p_input );
671 /* Append item to playlist */
672 if( p_parent ) /* Add to node */
673 input_ItemAddSubItem( p_parent, p_input );
674 else if( b_play ) /* Play */
675 playlist_AddInput( p_playlist, p_input,
676 PLAYLIST_APPEND | PLAYLIST_GO,
677 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
679 playlist_AddInput( p_playlist, p_input,
680 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
681 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
682 i_count ++; /* increment counter */
684 while( i_options > 0 )
685 free( ppsz_options[--i_options] );
686 free( ppsz_options );
690 lua_pop( L, 1 ); /* pop "path" */
692 "Playlist item's path should be a string" );
694 /* playlist key item */
698 msg_Warn( p_this, "Playlist item should be a table" );
700 /* <Parse playlist item> */
701 lua_pop( L, 1 ); /* pop the value, keep the key for
702 * the next lua_next() call */
709 msg_Warn( p_this, "Playlist should be a table." );