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 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
170 int vlclua_cachedir( lua_State *L )
172 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
175 int vlclua_datadir_list( lua_State *L )
177 const char *psz_dirname = luaL_checkstring( L, 1 );
178 vlc_object_t *p_this = vlclua_get_this( L );
179 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
180 char **ppsz_dir = ppsz_dir_list;
183 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
186 for( ; *ppsz_dir; ppsz_dir++ )
188 lua_pushstring( L, *ppsz_dir );
189 lua_rawseti( L, -2, i );
195 /*****************************************************************************
197 *****************************************************************************/
198 int vlclua_volume_set( lua_State *L )
200 vlc_object_t *p_this = vlclua_get_this( L );
201 int i_volume = luaL_checkint( L, 1 );
202 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
203 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
206 int vlclua_volume_get( lua_State *L )
208 vlc_object_t *p_this = vlclua_get_this( L );
209 audio_volume_t i_volume;
210 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
211 lua_pushnumber( L, i_volume );
217 int vlclua_volume_up( lua_State *L )
219 audio_volume_t i_volume;
220 aout_VolumeUp( vlclua_get_this( L ),
221 luaL_optint( L, 1, 1 ),
223 lua_pushnumber( L, i_volume );
227 int vlclua_volume_down( lua_State *L )
229 audio_volume_t i_volume;
230 aout_VolumeDown( vlclua_get_this( L ),
231 luaL_optint( L, 1, 1 ),
233 lua_pushnumber( L, i_volume );
237 /*****************************************************************************
239 *****************************************************************************/
240 int vlclua_stream_new( lua_State *L )
242 vlc_object_t * p_this = vlclua_get_this( L );
244 const char * psz_url;
245 psz_url = luaL_checkstring( L, -1 );
246 p_stream = stream_UrlNew( p_this, psz_url );
248 return luaL_error( L, "Error when opening url: `%s'", psz_url );
249 lua_pushlightuserdata( L, p_stream );
253 int vlclua_stream_read( lua_State *L )
259 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
260 n = luaL_checkint( L, 2 );
261 p_read = malloc( n );
262 if( !p_read ) return vlclua_error( L );
263 i_read = stream_Read( p_stream, p_read, n );
264 lua_pushlstring( L, (const char *)p_read, i_read );
269 int vlclua_stream_readline( lua_State *L )
272 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
273 char *psz_line = stream_ReadLine( p_stream );
276 lua_pushstring( L, psz_line );
284 int vlclua_stream_delete( lua_State *L )
287 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
288 stream_Delete( p_stream );
292 /*****************************************************************************
293 * String transformations
294 *****************************************************************************/
295 int vlclua_decode_uri( lua_State *L )
297 int i_top = lua_gettop( L );
299 for( i = 1; i <= i_top; i++ )
301 const char *psz_cstring = luaL_checkstring( L, 1 );
302 char *psz_string = strdup( psz_cstring );
303 lua_remove( L, 1 ); /* remove elements to prevent being limited by
304 * the stack's size (this function will work with
305 * up to (stack size - 1) arguments */
306 decode_URI( psz_string );
307 lua_pushstring( L, psz_string );
313 int vlclua_resolve_xml_special_chars( lua_State *L )
315 int i_top = lua_gettop( L );
317 for( i = 1; i <= i_top; i++ )
319 const char *psz_cstring = luaL_checkstring( L, 1 );
320 char *psz_string = strdup( psz_cstring );
321 lua_remove( L, 1 ); /* remove elements to prevent being limited by
322 * the stack's size (this function will work with
323 * up to (stack size - 1) arguments */
324 resolve_xml_special_chars( psz_string );
325 lua_pushstring( L, psz_string );
331 int vlclua_convert_xml_special_chars( lua_State *L )
333 int i_top = lua_gettop( L );
335 for( i = 1; i <= i_top; i++ )
337 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
339 lua_pushstring( L, psz_string );
345 /*****************************************************************************
346 * Messaging facilities
347 *****************************************************************************/
348 int vlclua_msg_dbg( lua_State *L )
350 int i_top = lua_gettop( L );
351 vlc_object_t *p_this = vlclua_get_this( L );
353 for( i = 1; i <= i_top; i++ )
354 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
357 int vlclua_msg_warn( lua_State *L )
359 int i_top = lua_gettop( L );
360 vlc_object_t *p_this = vlclua_get_this( L );
362 for( i = 1; i <= i_top; i++ )
363 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
366 int vlclua_msg_err( lua_State *L )
368 int i_top = lua_gettop( L );
369 vlc_object_t *p_this = vlclua_get_this( L );
371 for( i = 1; i <= i_top; i++ )
372 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
375 int vlclua_msg_info( lua_State *L )
377 int i_top = lua_gettop( L );
378 vlc_object_t *p_this = vlclua_get_this( L );
380 for( i = 1; i <= i_top; i++ )
381 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
385 /*****************************************************************************
387 *****************************************************************************/
388 static int file_select( const char *file )
390 int i = strlen( file );
391 return i > 4 && !strcmp( file+i-4, ".lua" );
394 static int file_compare( const char **a, const char **b )
396 return strcmp( *a, *b );
399 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
400 char **ppsz_dir_list )
403 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
404 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[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
412 psz_vlcpath, luadirname ) < 0 )
416 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
417 psz_vlcpath, luadirname ) < 0 )
421 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
422 psz_vlcpath, luadirname ) < 0 )
429 if( asprintf( &ppsz_dir_list[i],
430 "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
433 # ifdef HAVE_SYS_STAT_H
435 struct stat stat_info;
436 if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
437 || !S_ISDIR( stat_info.st_mode ) )
439 free(ppsz_dir_list[i]);
440 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
441 config_GetDataDir (), luadirname ) < 0 )
451 /*****************************************************************************
452 * Will execute func on all scripts in luadirname, and stop if func returns
454 *****************************************************************************/
455 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
456 const char * luadirname,
457 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
461 int i_ret = VLC_EGENERIC;
464 char **ppsz_filelist = NULL;
465 char **ppsz_fileend = NULL;
468 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
471 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
472 if( i_ret != VLC_SUCCESS )
474 i_ret = VLC_EGENERIC;
477 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
483 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
486 free( ppsz_filelist );
487 ppsz_filelist = NULL;
495 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
496 dir = utf8_opendir( *ppsz_dir );
499 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
501 if( i_files < 1 ) continue;
502 ppsz_fileend = ppsz_filelist + i_files;
504 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
507 if( asprintf( &psz_filename,
508 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
510 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
512 i_ret = func( p_this, psz_filename, L, user_data );
514 free( psz_filename );
516 if( i_ret == VLC_SUCCESS ) break;
518 if( i_ret == VLC_SUCCESS ) break;
523 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
526 free( ppsz_filelist );
528 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
531 if( dir ) closedir( dir );
537 /*****************************************************************************
538 * Meta data setters utility.
539 * Playlist item table should be on top of the stack when these are called
540 *****************************************************************************/
541 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
542 input_item_t *p_input )
544 #define TRY_META( a, b ) \
545 lua_getfield( L, -1, a ); \
546 if( lua_isstring( L, -1 ) ) \
548 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
549 EnsureUTF8( psz_value ); \
550 msg_Dbg( p_this, #b ": %s", psz_value ); \
551 input_item_Set ## b ( p_input, psz_value ); \
554 lua_pop( L, 1 ); /* pop a */
555 TRY_META( "title", Title );
556 TRY_META( "artist", Artist );
557 TRY_META( "genre", Genre );
558 TRY_META( "copyright", Copyright );
559 TRY_META( "album", Album );
560 TRY_META( "tracknum", TrackNum );
561 TRY_META( "description", Description );
562 TRY_META( "rating", Rating );
563 TRY_META( "date", Date );
564 TRY_META( "setting", Setting );
565 TRY_META( "url", URL );
566 TRY_META( "language", Language );
567 TRY_META( "nowplaying", NowPlaying );
568 TRY_META( "publisher", Publisher );
569 TRY_META( "encodedby", EncodedBy );
570 TRY_META( "arturl", ArtURL );
571 TRY_META( "trackid", TrackID );
574 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
575 input_item_t *p_input )
578 lua_getfield( L, -1, "meta" );
580 if( lua_istable( L, -1 ) )
583 /* ... item meta nil */
584 while( lua_next( L, -2 ) )
586 /* ... item meta key value */
587 if( !lua_isstring( L, -2 ) )
589 msg_Warn( p_this, "Custom meta data category name must be "
592 else if( !lua_istable( L, -1 ) )
594 msg_Warn( p_this, "Custom meta data category contents "
599 const char *psz_meta_category = lua_tostring( L, -2 );
600 msg_Dbg( p_this, "Found custom meta data category: %s",
603 /* ... item meta key value nil */
604 while( lua_next( L, -2 ) )
606 /* ... item meta key value key2 value2 */
607 if( !lua_isstring( L, -2 ) )
609 msg_Warn( p_this, "Custom meta category item name "
610 "must be a string." );
612 else if( !lua_isstring( L, -1 ) )
614 msg_Warn( p_this, "Custom meta category item value "
615 "must be a string." );
619 const char *psz_meta_name =
620 lua_tostring( L, -2 );
621 const char *psz_meta_value =
622 lua_tostring( L, -1 );
623 msg_Dbg( p_this, "Custom meta %s, %s: %s",
624 psz_meta_category, psz_meta_name,
626 input_ItemAddInfo( p_input, psz_meta_category,
627 psz_meta_name, psz_meta_value );
629 lua_pop( L, 1 ); /* pop item */
630 /* ... item meta key value key2 */
632 /* ... item meta key value */
634 lua_pop( L, 1 ); /* pop category */
635 /* ... item meta key */
639 lua_pop( L, 1 ); /* pop "meta" */
640 /* ... item -> back to original stack */
643 /*****************************************************************************
645 ****************************************************************************/
647 * Playlist item table should be on top of the stack when this is called
649 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
650 int *pi_options, char ***pppsz_options )
652 lua_getfield( L, -1, "options" );
653 if( lua_istable( L, -1 ) )
656 while( lua_next( L, -2 ) )
658 if( lua_isstring( L, -1 ) )
660 char *psz_option = strdup( lua_tostring( L, -1 ) );
661 msg_Dbg( p_this, "Option: %s", psz_option );
662 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
667 msg_Warn( p_this, "Option should be a string" );
669 lua_pop( L, 1 ); /* pop option */
672 lua_pop( L, 1 ); /* pop "options" */
675 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
676 playlist_t *p_playlist,
677 input_item_t *p_parent, bool b_play )
682 if( lua_istable( L, -1 ) )
686 while( lua_next( L, -2 ) )
688 /* playlist key item */
689 /* <Parse playlist item> */
690 if( lua_istable( L, -1 ) )
692 lua_getfield( L, -1, "path" );
693 /* playlist key item path */
694 if( lua_isstring( L, -1 ) )
696 const char *psz_path = NULL;
697 const char *psz_name = NULL;
698 char **ppsz_options = NULL;
700 mtime_t i_duration = -1;
701 input_item_t *p_input;
703 /* Read path and name */
704 psz_path = lua_tostring( L, -1 );
705 msg_Dbg( p_this, "Path: %s", psz_path );
706 lua_getfield( L, -2, "name" );
707 /* playlist key item path name */
708 if( lua_isstring( L, -1 ) )
710 psz_name = lua_tostring( L, -1 );
711 msg_Dbg( p_this, "Name: %s", psz_name );
715 if( !lua_isnil( L, -1 ) )
716 msg_Warn( p_this, "Playlist item name should be a string." );
721 lua_getfield( L, -3, "duration" );
722 /* playlist key item path name duration */
723 if( lua_isnumber( L, -1 ) )
725 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
727 else if( !lua_isnil( L, -1 ) )
729 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
731 lua_pop( L, 1 ); /* pop "duration" */
733 /* playlist key item path name */
735 /* Read options: item must be on top of stack */
736 lua_pushvalue( L, -3 );
737 /* playlist key item path name item */
738 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
740 /* Create input item */
741 p_input = input_ItemNewExt( p_playlist, psz_path,
743 (const char **)ppsz_options,
745 lua_pop( L, 3 ); /* pop "path name item" */
746 /* playlist key item */
748 /* Read meta data: item must be on top of stack */
749 vlclua_read_meta_data( p_this, L, p_input );
751 /* Read custom meta data: item must be on top of stack*/
752 vlclua_read_custom_meta_data( p_this, L, p_input );
754 /* Append item to playlist */
755 if( p_parent ) /* Add to node */
756 input_ItemAddSubItem( p_parent, p_input );
757 else /* Play or Enqueue (preparse) */
758 /* FIXME: playlist_AddInput() can fail */
759 playlist_AddInput( p_playlist, p_input,
761 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
762 PLAYLIST_END, true, false );
763 i_count ++; /* increment counter */
764 vlc_gc_decref( p_input );
765 while( i_options > 0 )
766 free( ppsz_options[--i_options] );
767 free( ppsz_options );
771 lua_pop( L, 1 ); /* pop "path" */
773 "Playlist item's path should be a string" );
775 /* playlist key item */
779 msg_Warn( p_this, "Playlist item should be a table" );
781 /* <Parse playlist item> */
782 lua_pop( L, 1 ); /* pop the value, keep the key for
783 * the next lua_next() call */
790 msg_Warn( p_this, "Playlist should be a table." );