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>, ...}, ...'.")
55 add_shortcut( "luameta" );
56 set_shortname( N_( "Lua Meta" ) );
57 set_description( _("Fetch metadata using lua scripts") );
58 set_capability( "meta fetcher", 10 );
59 set_callbacks( E_(FindMeta), NULL );
61 set_shortname( N_( "Lua Art" ) );
62 set_description( _("Fetch artwork using lua scripts") );
63 set_capability( "art finder", 10 );
64 set_callbacks( E_(FindArt), NULL );
66 add_shortcut( "luaplaylist" );
67 set_category( CAT_INPUT );
68 set_subcategory( SUBCAT_INPUT_DEMUX );
69 set_shortname( _("Lua Playlist") );
70 set_description( _("Lua Playlist Parser Interface") );
71 set_capability( "demux2", 2 );
72 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
74 add_shortcut( "luaintf" );
75 add_shortcut( "luarc" );
76 /* add_shortcut( "rc" ); */
77 add_shortcut( "luahotkeys" );
78 /* add_shortcut( "hotkeys" ); */
79 add_shortcut( "luatelnet" );
80 /* add_shortcut( "telnet" ); */
81 add_shortcut( "luahttp" );
82 /* add_shortcut( "http" ); */
83 set_description( _("Lua Interface Module") );
84 set_capability( "interface", 0 );
85 add_string( "lua-intf", "dummy", NULL,
86 INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
87 add_string( "lua-config", "", NULL,
88 CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
89 set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
92 /*****************************************************************************
93 * Internal lua<->vlc utils
94 *****************************************************************************/
95 vlc_object_t * vlclua_get_this( lua_State *L )
97 vlc_object_t * p_this;
98 lua_getglobal( L, "vlc" );
99 lua_getfield( L, -1, "private" );
100 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
105 /*****************************************************************************
106 * VLC error code translation
107 *****************************************************************************/
108 int vlclua_push_ret( lua_State *L, int i_error )
110 lua_pushnumber( L, i_error );
111 lua_pushstring( L, vlc_error( i_error ) );
115 /*****************************************************************************
116 * Get the VLC version string
117 *****************************************************************************/
118 int vlclua_version( lua_State *L )
120 lua_pushstring( L, VLC_Version() );
124 /*****************************************************************************
125 * Get the VLC copyright
126 *****************************************************************************/
127 int vlclua_copyright( lua_State *L )
129 lua_pushstring( L, COPYRIGHT_MESSAGE );
133 /*****************************************************************************
134 * Get the VLC license msg/disclaimer
135 *****************************************************************************/
136 int vlclua_license( lua_State *L )
138 lua_pushstring( L, LICENSE_MSG );
142 /*****************************************************************************
144 *****************************************************************************/
145 int vlclua_quit( lua_State *L )
147 vlc_object_t *p_this = vlclua_get_this( L );
148 /* The rc.c code also stops the playlist ... not sure if this is needed
150 vlc_object_kill( p_this->p_libvlc );
154 /*****************************************************************************
155 * Global properties getters
156 *****************************************************************************/
157 int vlclua_datadir( lua_State *L )
159 lua_pushstring( L, config_GetDataDir() );
162 int vlclua_homedir( lua_State *L )
164 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
167 int vlclua_configdir( lua_State *L )
169 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
172 int vlclua_cachedir( lua_State *L )
174 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
177 int vlclua_datadir_list( lua_State *L )
179 const char *psz_dirname = luaL_checkstring( L, 1 );
180 vlc_object_t *p_this = vlclua_get_this( L );
181 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
182 char **ppsz_dir = ppsz_dir_list;
185 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
188 for( ; *ppsz_dir; ppsz_dir++ )
190 lua_pushstring( L, *ppsz_dir );
191 lua_rawseti( L, -2, i );
197 /*****************************************************************************
199 *****************************************************************************/
200 int vlclua_volume_set( lua_State *L )
202 vlc_object_t *p_this = vlclua_get_this( L );
203 int i_volume = luaL_checkint( L, 1 );
204 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
205 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
208 int vlclua_volume_get( lua_State *L )
210 vlc_object_t *p_this = vlclua_get_this( L );
211 audio_volume_t i_volume;
212 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
213 lua_pushnumber( L, i_volume );
219 int vlclua_volume_up( lua_State *L )
221 audio_volume_t i_volume;
222 aout_VolumeUp( vlclua_get_this( L ),
223 luaL_optint( L, 1, 1 ),
225 lua_pushnumber( L, i_volume );
229 int vlclua_volume_down( lua_State *L )
231 audio_volume_t i_volume;
232 aout_VolumeDown( vlclua_get_this( L ),
233 luaL_optint( L, 1, 1 ),
235 lua_pushnumber( L, i_volume );
239 /*****************************************************************************
241 *****************************************************************************/
242 int vlclua_stream_new( lua_State *L )
244 vlc_object_t * p_this = vlclua_get_this( L );
246 const char * psz_url;
247 psz_url = luaL_checkstring( L, -1 );
248 p_stream = stream_UrlNew( p_this, psz_url );
250 return luaL_error( L, "Error when opening url: `%s'", psz_url );
251 lua_pushlightuserdata( L, p_stream );
255 int vlclua_stream_read( lua_State *L )
261 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
262 n = luaL_checkint( L, 2 );
263 p_read = malloc( n );
264 if( !p_read ) return vlclua_error( L );
265 i_read = stream_Read( p_stream, p_read, n );
266 lua_pushlstring( L, (const char *)p_read, i_read );
271 int vlclua_stream_readline( lua_State *L )
274 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
275 char *psz_line = stream_ReadLine( p_stream );
278 lua_pushstring( L, psz_line );
286 int vlclua_stream_delete( lua_State *L )
289 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
290 stream_Delete( p_stream );
294 /*****************************************************************************
295 * String transformations
296 *****************************************************************************/
297 int vlclua_decode_uri( lua_State *L )
299 int i_top = lua_gettop( L );
301 for( i = 1; i <= i_top; i++ )
303 const char *psz_cstring = luaL_checkstring( L, 1 );
304 char *psz_string = strdup( psz_cstring );
305 lua_remove( L, 1 ); /* remove elements to prevent being limited by
306 * the stack's size (this function will work with
307 * up to (stack size - 1) arguments */
308 decode_URI( psz_string );
309 lua_pushstring( L, psz_string );
315 int vlclua_resolve_xml_special_chars( lua_State *L )
317 int i_top = lua_gettop( L );
319 for( i = 1; i <= i_top; i++ )
321 const char *psz_cstring = luaL_checkstring( L, 1 );
322 char *psz_string = strdup( psz_cstring );
323 lua_remove( L, 1 ); /* remove elements to prevent being limited by
324 * the stack's size (this function will work with
325 * up to (stack size - 1) arguments */
326 resolve_xml_special_chars( psz_string );
327 lua_pushstring( L, psz_string );
333 int vlclua_convert_xml_special_chars( lua_State *L )
335 int i_top = lua_gettop( L );
337 for( i = 1; i <= i_top; i++ )
339 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
341 lua_pushstring( L, psz_string );
347 /*****************************************************************************
348 * Messaging facilities
349 *****************************************************************************/
350 int vlclua_msg_dbg( lua_State *L )
352 int i_top = lua_gettop( L );
353 vlc_object_t *p_this = vlclua_get_this( L );
355 for( i = 1; i <= i_top; i++ )
356 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
359 int vlclua_msg_warn( lua_State *L )
361 int i_top = lua_gettop( L );
362 vlc_object_t *p_this = vlclua_get_this( L );
364 for( i = 1; i <= i_top; i++ )
365 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
368 int vlclua_msg_err( lua_State *L )
370 int i_top = lua_gettop( L );
371 vlc_object_t *p_this = vlclua_get_this( L );
373 for( i = 1; i <= i_top; i++ )
374 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
377 int vlclua_msg_info( lua_State *L )
379 int i_top = lua_gettop( L );
380 vlc_object_t *p_this = vlclua_get_this( L );
382 for( i = 1; i <= i_top; i++ )
383 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
387 /*****************************************************************************
389 *****************************************************************************/
390 static int file_select( const char *file )
392 int i = strlen( file );
393 return i > 4 && !strcmp( file+i-4, ".lua" );
396 static int file_compare( const char **a, const char **b )
398 return strcmp( *a, *b );
401 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
402 char **ppsz_dir_list )
404 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
405 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
408 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
410 const char *psz_vlcpath = config_GetDataDir();
411 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
412 psz_vlcpath, luadirname ) < 0 )
415 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
416 psz_vlcpath, luadirname ) < 0 )
420 if( asprintf( &ppsz_dir_list[1],
421 "share" DIR_SEP "%s", luadirname ) < 0 )
424 # ifdef HAVE_SYS_STAT_H
426 struct stat stat_info;
427 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
428 || !S_ISDIR( stat_info.st_mode ) )
430 free(ppsz_dir_list[1]);
431 if( asprintf( &ppsz_dir_list[1],
432 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
441 /*****************************************************************************
442 * Will execute func on all scripts in luadirname, and stop if func returns
444 *****************************************************************************/
445 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
446 const char * luadirname,
447 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
451 int i_ret = VLC_EGENERIC;
454 char **ppsz_filelist = NULL;
455 char **ppsz_fileend = NULL;
458 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
461 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
462 if( i_ret != VLC_SUCCESS )
464 i_ret = VLC_EGENERIC;
467 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
473 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
476 free( ppsz_filelist );
477 ppsz_filelist = NULL;
485 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
486 dir = utf8_opendir( *ppsz_dir );
489 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
491 if( i_files < 1 ) continue;
492 ppsz_fileend = ppsz_filelist + i_files;
494 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
497 if( asprintf( &psz_filename,
498 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
500 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
502 i_ret = func( p_this, psz_filename, L, user_data );
504 free( psz_filename );
506 if( i_ret == VLC_SUCCESS ) break;
508 if( i_ret == VLC_SUCCESS ) break;
513 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
516 free( ppsz_filelist );
518 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
521 if( dir ) closedir( dir );
527 /*****************************************************************************
528 * Meta data setters utility.
529 * Playlist item table should be on top of the stack when these are called
530 *****************************************************************************/
531 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
532 input_item_t *p_input )
534 #define TRY_META( a, b ) \
535 lua_getfield( L, -1, a ); \
536 if( lua_isstring( L, -1 ) ) \
538 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
539 EnsureUTF8( psz_value ); \
540 msg_Dbg( p_this, #b ": %s", psz_value ); \
541 input_item_Set ## b ( p_input, psz_value ); \
544 lua_pop( L, 1 ); /* pop a */
545 TRY_META( "title", Title );
546 TRY_META( "artist", Artist );
547 TRY_META( "genre", Genre );
548 TRY_META( "copyright", Copyright );
549 TRY_META( "album", Album );
550 TRY_META( "tracknum", TrackNum );
551 TRY_META( "description", Description );
552 TRY_META( "rating", Rating );
553 TRY_META( "date", Date );
554 TRY_META( "setting", Setting );
555 TRY_META( "url", URL );
556 TRY_META( "language", Language );
557 TRY_META( "nowplaying", NowPlaying );
558 TRY_META( "publisher", Publisher );
559 TRY_META( "encodedby", EncodedBy );
560 TRY_META( "arturl", ArtURL );
561 TRY_META( "trackid", TrackID );
564 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
565 input_item_t *p_input )
568 lua_getfield( L, -1, "meta" );
570 if( lua_istable( L, -1 ) )
573 /* ... item meta nil */
574 while( lua_next( L, -2 ) )
576 /* ... item meta key value */
577 if( !lua_isstring( L, -2 ) )
579 msg_Warn( p_this, "Custom meta data category name must be "
582 else if( !lua_istable( L, -1 ) )
584 msg_Warn( p_this, "Custom meta data category contents "
589 const char *psz_meta_category = lua_tostring( L, -2 );
590 msg_Dbg( p_this, "Found custom meta data category: %s",
593 /* ... item meta key value nil */
594 while( lua_next( L, -2 ) )
596 /* ... item meta key value key2 value2 */
597 if( !lua_isstring( L, -2 ) )
599 msg_Warn( p_this, "Custom meta category item name "
600 "must be a string." );
602 else if( !lua_isstring( L, -1 ) )
604 msg_Warn( p_this, "Custom meta category item value "
605 "must be a string." );
609 const char *psz_meta_name =
610 lua_tostring( L, -2 );
611 const char *psz_meta_value =
612 lua_tostring( L, -1 );
613 msg_Dbg( p_this, "Custom meta %s, %s: %s",
614 psz_meta_category, psz_meta_name,
616 input_ItemAddInfo( p_input, psz_meta_category,
617 psz_meta_name, psz_meta_value );
619 lua_pop( L, 1 ); /* pop item */
620 /* ... item meta key value key2 */
622 /* ... item meta key value */
624 lua_pop( L, 1 ); /* pop category */
625 /* ... item meta key */
629 lua_pop( L, 1 ); /* pop "meta" */
630 /* ... item -> back to original stack */
633 /*****************************************************************************
635 ****************************************************************************/
637 * Playlist item table should be on top of the stack when this is called
639 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
640 int *pi_options, char ***pppsz_options )
642 lua_getfield( L, -1, "options" );
643 if( lua_istable( L, -1 ) )
646 while( lua_next( L, -2 ) )
648 if( lua_isstring( L, -1 ) )
650 char *psz_option = strdup( lua_tostring( L, -1 ) );
651 msg_Dbg( p_this, "Option: %s", psz_option );
652 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
657 msg_Warn( p_this, "Option should be a string" );
659 lua_pop( L, 1 ); /* pop option */
662 lua_pop( L, 1 ); /* pop "options" */
665 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
666 playlist_t *p_playlist,
667 input_item_t *p_parent, vlc_bool_t b_play )
672 if( lua_istable( L, -1 ) )
676 while( lua_next( L, -2 ) )
678 /* playlist key item */
679 /* <Parse playlist item> */
680 if( lua_istable( L, -1 ) )
682 lua_getfield( L, -1, "path" );
683 /* playlist key item path */
684 if( lua_isstring( L, -1 ) )
686 const char *psz_path = NULL;
687 const char *psz_name = NULL;
688 char **ppsz_options = NULL;
690 mtime_t i_duration = -1;
691 input_item_t *p_input;
693 /* Read path and name */
694 psz_path = lua_tostring( L, -1 );
695 msg_Dbg( p_this, "Path: %s", psz_path );
696 lua_getfield( L, -2, "name" );
697 /* playlist key item path name */
698 if( lua_isstring( L, -1 ) )
700 psz_name = lua_tostring( L, -1 );
701 msg_Dbg( p_this, "Name: %s", psz_name );
705 if( !lua_isnil( L, -1 ) )
706 msg_Warn( p_this, "Playlist item name should be a string." );
711 lua_getfield( L, -3, "duration" );
712 /* playlist key item path name duration */
713 if( lua_isnumber( L, -1 ) )
715 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
717 else if( !lua_isnil( L, -1 ) )
719 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
721 lua_pop( L, 1 ); /* pop "duration" */
723 /* playlist key item path name */
725 /* Read options: item must be on top of stack */
726 lua_pushvalue( L, -3 );
727 /* playlist key item path name item */
728 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
730 /* Create input item */
731 p_input = input_ItemNewExt( p_playlist, psz_path,
733 (const char **)ppsz_options,
735 lua_pop( L, 3 ); /* pop "path name item" */
736 /* playlist key item */
738 /* Read meta data: item must be on top of stack */
739 vlclua_read_meta_data( p_this, L, p_input );
741 /* Read custom meta data: item must be on top of stack*/
742 vlclua_read_custom_meta_data( p_this, L, p_input );
744 /* Append item to playlist */
745 if( p_parent ) /* Add to node */
746 input_ItemAddSubItem( p_parent, p_input );
747 else if( b_play ) /* Play */
748 playlist_AddInput( p_playlist, p_input,
749 PLAYLIST_APPEND | PLAYLIST_GO,
750 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
752 playlist_AddInput( p_playlist, p_input,
753 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
754 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
755 i_count ++; /* increment counter */
756 vlc_gc_decref( p_input );
757 while( i_options > 0 )
758 free( ppsz_options[--i_options] );
759 free( ppsz_options );
763 lua_pop( L, 1 ); /* pop "path" */
765 "Playlist item's path should be a string" );
767 /* playlist key item */
771 msg_Warn( p_this, "Playlist item should be a table" );
773 /* <Parse playlist item> */
774 lua_pop( L, 1 ); /* pop the value, keep the key for
775 * the next lua_next() call */
782 msg_Warn( p_this, "Playlist should be a table." );