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 *****************************************************************************/
38 #include <vlc_charset.h>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
43 #include <lualib.h> /* Lua libs */
47 /*****************************************************************************
49 *****************************************************************************/
51 #define INTF_TEXT N_("Lua interface")
52 #define INTF_LONGTEXT N_("Lua interface module to load")
54 #define CONFIG_TEXT N_("Lua inteface configuration")
55 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
59 set_shortname( N_( "Lua Art" ) );
60 set_description( _("Fetch artwork using lua scripts") );
61 set_capability( "art finder", 10 );
62 set_callbacks( E_(FindArt), NULL );
64 add_shortcut( "luaplaylist" );
65 set_category( CAT_INPUT );
66 set_subcategory( SUBCAT_INPUT_DEMUX );
67 set_shortname( _("Lua Playlist") );
68 set_description( _("Lua Playlist Parser Interface") );
69 set_capability( "demux", 2 );
70 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
72 add_shortcut( "luaintf" );
73 add_shortcut( "luarc" );
74 /* add_shortcut( "rc" ); */
75 add_shortcut( "luahotkeys" );
76 /* add_shortcut( "hotkeys" ); */
77 add_shortcut( "luatelnet" );
78 /* add_shortcut( "telnet" ); */
79 add_shortcut( "luahttp" );
80 /* add_shortcut( "http" ); */
81 set_description( _("Lua Interface Module") );
82 set_capability( "interface", 0 );
83 add_string( "lua-intf", "dummy", NULL,
84 INTF_TEXT, INTF_LONGTEXT, false );
85 add_string( "lua-config", "", NULL,
86 CONFIG_TEXT, CONFIG_LONGTEXT, false );
87 set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
90 /*****************************************************************************
91 * Internal lua<->vlc utils
92 *****************************************************************************/
93 vlc_object_t * vlclua_get_this( lua_State *L )
95 vlc_object_t * p_this;
96 lua_getglobal( L, "vlc" );
97 lua_getfield( L, -1, "private" );
98 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
103 /*****************************************************************************
104 * VLC error code translation
105 *****************************************************************************/
106 int vlclua_push_ret( lua_State *L, int i_error )
108 lua_pushnumber( L, i_error );
109 lua_pushstring( L, vlc_error( i_error ) );
113 /*****************************************************************************
114 * Get the VLC version string
115 *****************************************************************************/
116 int vlclua_version( lua_State *L )
118 lua_pushstring( L, VLC_Version() );
122 /*****************************************************************************
123 * Get the VLC copyright
124 *****************************************************************************/
125 int vlclua_copyright( lua_State *L )
127 lua_pushstring( L, COPYRIGHT_MESSAGE );
131 /*****************************************************************************
132 * Get the VLC license msg/disclaimer
133 *****************************************************************************/
134 int vlclua_license( lua_State *L )
136 lua_pushstring( L, LICENSE_MSG );
140 /*****************************************************************************
142 *****************************************************************************/
143 int vlclua_quit( lua_State *L )
145 vlc_object_t *p_this = vlclua_get_this( L );
146 /* The rc.c code also stops the playlist ... not sure if this is needed
148 vlc_object_kill( p_this->p_libvlc );
152 /*****************************************************************************
153 * Global properties getters
154 *****************************************************************************/
155 int vlclua_datadir( lua_State *L )
157 lua_pushstring( L, config_GetDataDir() );
160 int vlclua_homedir( lua_State *L )
162 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
165 int vlclua_configdir( lua_State *L )
167 char *dir = config_GetConfigDir();
168 lua_pushstring( L, dir );
172 int vlclua_cachedir( lua_State *L )
174 char *dir = config_GetCacheDir();
175 lua_pushstring( L, dir );
179 int vlclua_datadir_list( lua_State *L )
181 const char *psz_dirname = luaL_checkstring( L, 1 );
182 vlc_object_t *p_this = vlclua_get_this( L );
183 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
184 char **ppsz_dir = ppsz_dir_list;
187 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
190 for( ; *ppsz_dir; ppsz_dir++ )
192 lua_pushstring( L, *ppsz_dir );
193 lua_rawseti( L, -2, i );
199 /*****************************************************************************
201 *****************************************************************************/
202 int vlclua_volume_set( lua_State *L )
204 vlc_object_t *p_this = vlclua_get_this( L );
205 int i_volume = luaL_checkint( L, 1 );
206 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
207 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
210 int vlclua_volume_get( lua_State *L )
212 vlc_object_t *p_this = vlclua_get_this( L );
213 audio_volume_t i_volume;
214 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
215 lua_pushnumber( L, i_volume );
221 int vlclua_volume_up( lua_State *L )
223 audio_volume_t i_volume;
224 aout_VolumeUp( vlclua_get_this( L ),
225 luaL_optint( L, 1, 1 ),
227 lua_pushnumber( L, i_volume );
231 int vlclua_volume_down( lua_State *L )
233 audio_volume_t i_volume;
234 aout_VolumeDown( vlclua_get_this( L ),
235 luaL_optint( L, 1, 1 ),
237 lua_pushnumber( L, i_volume );
241 /*****************************************************************************
243 *****************************************************************************/
244 int vlclua_stream_new( lua_State *L )
246 vlc_object_t * p_this = vlclua_get_this( L );
248 const char * psz_url;
249 psz_url = luaL_checkstring( L, -1 );
250 p_stream = stream_UrlNew( p_this, psz_url );
252 return luaL_error( L, "Error when opening url: `%s'", psz_url );
253 lua_pushlightuserdata( L, p_stream );
257 int vlclua_stream_read( lua_State *L )
263 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
264 n = luaL_checkint( L, 2 );
265 p_read = malloc( n );
266 if( !p_read ) return vlclua_error( L );
267 i_read = stream_Read( p_stream, p_read, n );
268 lua_pushlstring( L, (const char *)p_read, i_read );
273 int vlclua_stream_readline( lua_State *L )
276 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
277 char *psz_line = stream_ReadLine( p_stream );
280 lua_pushstring( L, psz_line );
288 int vlclua_stream_delete( lua_State *L )
291 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
292 stream_Delete( p_stream );
296 /*****************************************************************************
297 * String transformations
298 *****************************************************************************/
299 int vlclua_decode_uri( lua_State *L )
301 int i_top = lua_gettop( L );
303 for( i = 1; i <= i_top; i++ )
305 const char *psz_cstring = luaL_checkstring( L, 1 );
306 char *psz_string = strdup( psz_cstring );
307 lua_remove( L, 1 ); /* remove elements to prevent being limited by
308 * the stack's size (this function will work with
309 * up to (stack size - 1) arguments */
310 decode_URI( psz_string );
311 lua_pushstring( L, psz_string );
317 int vlclua_resolve_xml_special_chars( lua_State *L )
319 int i_top = lua_gettop( L );
321 for( i = 1; i <= i_top; i++ )
323 const char *psz_cstring = luaL_checkstring( L, 1 );
324 char *psz_string = strdup( psz_cstring );
325 lua_remove( L, 1 ); /* remove elements to prevent being limited by
326 * the stack's size (this function will work with
327 * up to (stack size - 1) arguments */
328 resolve_xml_special_chars( psz_string );
329 lua_pushstring( L, psz_string );
335 int vlclua_convert_xml_special_chars( lua_State *L )
337 int i_top = lua_gettop( L );
339 for( i = 1; i <= i_top; i++ )
341 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
343 lua_pushstring( L, psz_string );
349 /*****************************************************************************
350 * Messaging facilities
351 *****************************************************************************/
352 int vlclua_msg_dbg( lua_State *L )
354 int i_top = lua_gettop( L );
355 vlc_object_t *p_this = vlclua_get_this( L );
357 for( i = 1; i <= i_top; i++ )
358 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
361 int vlclua_msg_warn( lua_State *L )
363 int i_top = lua_gettop( L );
364 vlc_object_t *p_this = vlclua_get_this( L );
366 for( i = 1; i <= i_top; i++ )
367 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
370 int vlclua_msg_err( lua_State *L )
372 int i_top = lua_gettop( L );
373 vlc_object_t *p_this = vlclua_get_this( L );
375 for( i = 1; i <= i_top; i++ )
376 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
379 int vlclua_msg_info( lua_State *L )
381 int i_top = lua_gettop( L );
382 vlc_object_t *p_this = vlclua_get_this( L );
384 for( i = 1; i <= i_top; i++ )
385 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
389 /*****************************************************************************
391 *****************************************************************************/
392 static int file_select( const char *file )
394 int i = strlen( file );
395 return i > 4 && !strcmp( file+i-4, ".lua" );
398 static int file_compare( const char **a, const char **b )
400 return strcmp( *a, *b );
403 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
404 char **ppsz_dir_list )
407 char *datadir = config_GetUserDataDir();
408 if( datadir == NULL )
411 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
412 datadir, luadirname ) < 0 )
420 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
422 const char *psz_vlcpath = config_GetDataDir();
423 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
424 psz_vlcpath, luadirname ) < 0 )
428 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
429 psz_vlcpath, luadirname ) < 0 )
433 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
434 psz_vlcpath, luadirname ) < 0 )
441 if( asprintf( &ppsz_dir_list[i],
442 "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
445 # ifdef HAVE_SYS_STAT_H
447 struct stat stat_info;
448 if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
449 || !S_ISDIR( stat_info.st_mode ) )
451 free(ppsz_dir_list[i]);
452 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
453 config_GetDataDir (), luadirname ) < 0 )
463 /*****************************************************************************
464 * Will execute func on all scripts in luadirname, and stop if func returns
466 *****************************************************************************/
467 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
468 const char * luadirname,
469 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
473 int i_ret = VLC_EGENERIC;
475 char **ppsz_filelist = NULL;
476 char **ppsz_fileend = NULL;
479 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
482 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
483 if( i_ret != VLC_SUCCESS )
485 i_ret = VLC_EGENERIC;
488 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
494 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
497 free( ppsz_filelist );
498 ppsz_filelist = NULL;
501 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
502 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
504 if( i_files < 1 ) continue;
505 ppsz_fileend = ppsz_filelist + i_files;
507 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
510 if( asprintf( &psz_filename,
511 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
513 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
515 i_ret = func( p_this, psz_filename, L, user_data );
517 free( psz_filename );
519 if( i_ret == VLC_SUCCESS ) break;
521 if( i_ret == VLC_SUCCESS ) break;
526 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
529 free( ppsz_filelist );
531 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
538 /*****************************************************************************
539 * Meta data setters utility.
540 * Playlist item table should be on top of the stack when these are called
541 *****************************************************************************/
542 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
543 input_item_t *p_input )
545 #define TRY_META( a, b ) \
546 lua_getfield( L, -1, a ); \
547 if( lua_isstring( L, -1 ) ) \
549 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
550 EnsureUTF8( psz_value ); \
551 msg_Dbg( p_this, #b ": %s", psz_value ); \
552 input_item_Set ## b ( p_input, psz_value ); \
555 lua_pop( L, 1 ); /* pop a */
556 TRY_META( "title", Title );
557 TRY_META( "artist", Artist );
558 TRY_META( "genre", Genre );
559 TRY_META( "copyright", Copyright );
560 TRY_META( "album", Album );
561 TRY_META( "tracknum", TrackNum );
562 TRY_META( "description", Description );
563 TRY_META( "rating", Rating );
564 TRY_META( "date", Date );
565 TRY_META( "setting", Setting );
566 TRY_META( "url", URL );
567 TRY_META( "language", Language );
568 TRY_META( "nowplaying", NowPlaying );
569 TRY_META( "publisher", Publisher );
570 TRY_META( "encodedby", EncodedBy );
571 TRY_META( "arturl", ArtURL );
572 TRY_META( "trackid", TrackID );
575 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
576 input_item_t *p_input )
579 lua_getfield( L, -1, "meta" );
581 if( lua_istable( L, -1 ) )
584 /* ... item meta nil */
585 while( lua_next( L, -2 ) )
587 /* ... item meta key value */
588 if( !lua_isstring( L, -2 ) )
590 msg_Warn( p_this, "Custom meta data category name must be "
593 else if( !lua_istable( L, -1 ) )
595 msg_Warn( p_this, "Custom meta data category contents "
600 const char *psz_meta_category = lua_tostring( L, -2 );
601 msg_Dbg( p_this, "Found custom meta data category: %s",
604 /* ... item meta key value nil */
605 while( lua_next( L, -2 ) )
607 /* ... item meta key value key2 value2 */
608 if( !lua_isstring( L, -2 ) )
610 msg_Warn( p_this, "Custom meta category item name "
611 "must be a string." );
613 else if( !lua_isstring( L, -1 ) )
615 msg_Warn( p_this, "Custom meta category item value "
616 "must be a string." );
620 const char *psz_meta_name =
621 lua_tostring( L, -2 );
622 const char *psz_meta_value =
623 lua_tostring( L, -1 );
624 msg_Dbg( p_this, "Custom meta %s, %s: %s",
625 psz_meta_category, psz_meta_name,
627 input_ItemAddInfo( p_input, psz_meta_category,
628 psz_meta_name, psz_meta_value );
630 lua_pop( L, 1 ); /* pop item */
631 /* ... item meta key value key2 */
633 /* ... item meta key value */
635 lua_pop( L, 1 ); /* pop category */
636 /* ... item meta key */
640 lua_pop( L, 1 ); /* pop "meta" */
641 /* ... item -> back to original stack */
644 /*****************************************************************************
646 ****************************************************************************/
648 * Playlist item table should be on top of the stack when this is called
650 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
651 int *pi_options, char ***pppsz_options )
653 lua_getfield( L, -1, "options" );
654 if( lua_istable( L, -1 ) )
657 while( lua_next( L, -2 ) )
659 if( lua_isstring( L, -1 ) )
661 char *psz_option = strdup( lua_tostring( L, -1 ) );
662 msg_Dbg( p_this, "Option: %s", psz_option );
663 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
668 msg_Warn( p_this, "Option should be a string" );
670 lua_pop( L, 1 ); /* pop option */
673 lua_pop( L, 1 ); /* pop "options" */
676 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
677 playlist_t *p_playlist,
678 input_item_t *p_parent, bool b_play )
683 if( lua_istable( L, -1 ) )
687 while( lua_next( L, -2 ) )
689 /* playlist key item */
690 /* <Parse playlist item> */
691 if( lua_istable( L, -1 ) )
693 lua_getfield( L, -1, "path" );
694 /* playlist key item path */
695 if( lua_isstring( L, -1 ) )
697 const char *psz_path = NULL;
698 const char *psz_name = NULL;
699 char **ppsz_options = NULL;
701 mtime_t i_duration = -1;
702 input_item_t *p_input;
704 /* Read path and name */
705 psz_path = lua_tostring( L, -1 );
706 msg_Dbg( p_this, "Path: %s", psz_path );
707 lua_getfield( L, -2, "name" );
708 /* playlist key item path name */
709 if( lua_isstring( L, -1 ) )
711 psz_name = lua_tostring( L, -1 );
712 msg_Dbg( p_this, "Name: %s", psz_name );
716 if( !lua_isnil( L, -1 ) )
717 msg_Warn( p_this, "Playlist item name should be a string." );
722 lua_getfield( L, -3, "duration" );
723 /* playlist key item path name duration */
724 if( lua_isnumber( L, -1 ) )
726 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
728 else if( !lua_isnil( L, -1 ) )
730 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
732 lua_pop( L, 1 ); /* pop "duration" */
734 /* playlist key item path name */
736 /* Read options: item must be on top of stack */
737 lua_pushvalue( L, -3 );
738 /* playlist key item path name item */
739 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
741 /* Create input item */
742 p_input = input_ItemNewExt( p_playlist, psz_path,
744 (const char **)ppsz_options,
746 lua_pop( L, 3 ); /* pop "path name item" */
747 /* playlist key item */
749 /* Read meta data: item must be on top of stack */
750 vlclua_read_meta_data( p_this, L, p_input );
752 /* Read custom meta data: item must be on top of stack*/
753 vlclua_read_custom_meta_data( p_this, L, p_input );
755 /* Append item to playlist */
756 if( p_parent ) /* Add to node */
757 input_ItemAddSubItem( p_parent, p_input );
758 else /* Play or Enqueue (preparse) */
759 /* FIXME: playlist_AddInput() can fail */
760 playlist_AddInput( p_playlist, p_input,
762 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
763 PLAYLIST_END, true, false );
764 i_count ++; /* increment counter */
765 vlc_gc_decref( p_input );
766 while( i_options > 0 )
767 free( ppsz_options[--i_options] );
768 free( ppsz_options );
772 lua_pop( L, 1 ); /* pop "path" */
774 "Playlist item's path should be a string" );
776 /* playlist key item */
780 msg_Warn( p_this, "Playlist item should be a table" );
782 /* <Parse playlist item> */
783 lua_pop( L, 1 ); /* pop the value, keep the key for
784 * the next lua_next() call */
791 msg_Warn( p_this, "Playlist should be a table." );