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 add_shortcut( "luameta" );
60 set_shortname( N_( "Lua Meta" ) );
61 set_description( _("Fetch metadata using lua scripts") );
62 set_capability( "meta fetcher", 10 );
63 set_callbacks( E_(FindMeta), NULL );
65 set_shortname( N_( "Lua Art" ) );
66 set_description( _("Fetch artwork using lua scripts") );
67 set_capability( "art finder", 10 );
68 set_callbacks( E_(FindArt), NULL );
70 add_shortcut( "luaplaylist" );
71 set_category( CAT_INPUT );
72 set_subcategory( SUBCAT_INPUT_DEMUX );
73 set_shortname( _("Lua Playlist") );
74 set_description( _("Lua Playlist Parser Interface") );
75 set_capability( "demux2", 2 );
76 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
78 add_shortcut( "luaintf" );
79 add_shortcut( "luarc" );
80 /* add_shortcut( "rc" ); */
81 add_shortcut( "luahotkeys" );
82 /* add_shortcut( "hotkeys" ); */
83 add_shortcut( "luatelnet" );
84 /* add_shortcut( "telnet" ); */
85 add_shortcut( "luahttp" );
86 /* add_shortcut( "http" ); */
87 set_description( _("Lua Interface Module") );
88 set_capability( "interface", 0 );
89 add_string( "lua-intf", "dummy", NULL,
90 INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
91 add_string( "lua-config", "", NULL,
92 CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
93 set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
96 /*****************************************************************************
97 * Internal lua<->vlc utils
98 *****************************************************************************/
99 vlc_object_t * vlclua_get_this( lua_State *L )
101 vlc_object_t * p_this;
102 lua_getglobal( L, "vlc" );
103 lua_getfield( L, -1, "private" );
104 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
109 /*****************************************************************************
110 * VLC error code translation
111 *****************************************************************************/
112 int vlclua_push_ret( lua_State *L, int i_error )
114 lua_pushnumber( L, i_error );
115 lua_pushstring( L, vlc_error( i_error ) );
119 /*****************************************************************************
120 * Get the VLC version string
121 *****************************************************************************/
122 int vlclua_version( lua_State *L )
124 lua_pushstring( L, VLC_Version() );
128 /*****************************************************************************
129 * Get the VLC copyright
130 *****************************************************************************/
131 int vlclua_copyright( lua_State *L )
133 lua_pushstring( L, COPYRIGHT_MESSAGE );
137 /*****************************************************************************
138 * Get the VLC license msg/disclaimer
139 *****************************************************************************/
140 int vlclua_license( lua_State *L )
142 lua_pushstring( L, LICENSE_MSG );
146 /*****************************************************************************
148 *****************************************************************************/
149 int vlclua_quit( lua_State *L )
151 vlc_object_t *p_this = vlclua_get_this( L );
152 /* The rc.c code also stops the playlist ... not sure if this is needed
154 vlc_object_kill( p_this->p_libvlc );
158 /*****************************************************************************
159 * Global properties getters
160 *****************************************************************************/
161 int vlclua_datadir( lua_State *L )
163 lua_pushstring( L, config_GetDataDir() );
166 int vlclua_homedir( lua_State *L )
168 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
171 int vlclua_configdir( lua_State *L )
173 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
176 int vlclua_cachedir( lua_State *L )
178 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
181 int vlclua_datadir_list( lua_State *L )
183 const char *psz_dirname = luaL_checkstring( L, 1 );
184 vlc_object_t *p_this = vlclua_get_this( L );
185 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
186 char **ppsz_dir = ppsz_dir_list;
189 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
192 for( ; *ppsz_dir; ppsz_dir++ )
194 lua_pushstring( L, *ppsz_dir );
195 lua_rawseti( L, -2, i );
201 /*****************************************************************************
203 *****************************************************************************/
204 int vlclua_volume_set( lua_State *L )
206 vlc_object_t *p_this = vlclua_get_this( L );
207 int i_volume = luaL_checkint( L, 1 );
208 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
209 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
212 int vlclua_volume_get( lua_State *L )
214 vlc_object_t *p_this = vlclua_get_this( L );
215 audio_volume_t i_volume;
216 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
217 lua_pushnumber( L, i_volume );
223 int vlclua_volume_up( lua_State *L )
225 audio_volume_t i_volume;
226 aout_VolumeUp( vlclua_get_this( L ),
227 luaL_optint( L, 1, 1 ),
229 lua_pushnumber( L, i_volume );
233 int vlclua_volume_down( lua_State *L )
235 audio_volume_t i_volume;
236 aout_VolumeDown( vlclua_get_this( L ),
237 luaL_optint( L, 1, 1 ),
239 lua_pushnumber( L, i_volume );
243 /*****************************************************************************
245 *****************************************************************************/
246 int vlclua_stream_new( lua_State *L )
248 vlc_object_t * p_this = vlclua_get_this( L );
250 const char * psz_url;
251 psz_url = luaL_checkstring( L, -1 );
252 p_stream = stream_UrlNew( p_this, psz_url );
254 return luaL_error( L, "Error when opening url: `%s'", psz_url );
255 lua_pushlightuserdata( L, p_stream );
259 int vlclua_stream_read( lua_State *L )
265 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
266 n = luaL_checkint( L, 2 );
267 p_read = malloc( n );
268 if( !p_read ) return vlclua_error( L );
269 i_read = stream_Read( p_stream, p_read, n );
270 lua_pushlstring( L, (const char *)p_read, i_read );
275 int vlclua_stream_readline( lua_State *L )
278 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
279 char *psz_line = stream_ReadLine( p_stream );
282 lua_pushstring( L, psz_line );
290 int vlclua_stream_delete( lua_State *L )
293 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
294 stream_Delete( p_stream );
298 /*****************************************************************************
299 * String transformations
300 *****************************************************************************/
301 int vlclua_decode_uri( lua_State *L )
303 int i_top = lua_gettop( L );
305 for( i = 1; i <= i_top; i++ )
307 const char *psz_cstring = luaL_checkstring( L, 1 );
308 char *psz_string = strdup( psz_cstring );
309 lua_remove( L, 1 ); /* remove elements to prevent being limited by
310 * the stack's size (this function will work with
311 * up to (stack size - 1) arguments */
312 decode_URI( psz_string );
313 lua_pushstring( L, psz_string );
319 int vlclua_resolve_xml_special_chars( lua_State *L )
321 int i_top = lua_gettop( L );
323 for( i = 1; i <= i_top; i++ )
325 const char *psz_cstring = luaL_checkstring( L, 1 );
326 char *psz_string = strdup( psz_cstring );
327 lua_remove( L, 1 ); /* remove elements to prevent being limited by
328 * the stack's size (this function will work with
329 * up to (stack size - 1) arguments */
330 resolve_xml_special_chars( psz_string );
331 lua_pushstring( L, psz_string );
337 int vlclua_convert_xml_special_chars( lua_State *L )
339 int i_top = lua_gettop( L );
341 for( i = 1; i <= i_top; i++ )
343 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
345 lua_pushstring( L, psz_string );
351 /*****************************************************************************
352 * Messaging facilities
353 *****************************************************************************/
354 int vlclua_msg_dbg( lua_State *L )
356 int i_top = lua_gettop( L );
357 vlc_object_t *p_this = vlclua_get_this( L );
359 for( i = 1; i <= i_top; i++ )
360 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
363 int vlclua_msg_warn( lua_State *L )
365 int i_top = lua_gettop( L );
366 vlc_object_t *p_this = vlclua_get_this( L );
368 for( i = 1; i <= i_top; i++ )
369 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
372 int vlclua_msg_err( lua_State *L )
374 int i_top = lua_gettop( L );
375 vlc_object_t *p_this = vlclua_get_this( L );
377 for( i = 1; i <= i_top; i++ )
378 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
381 int vlclua_msg_info( lua_State *L )
383 int i_top = lua_gettop( L );
384 vlc_object_t *p_this = vlclua_get_this( L );
386 for( i = 1; i <= i_top; i++ )
387 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
391 /*****************************************************************************
393 *****************************************************************************/
394 static int file_select( const char *file )
396 int i = strlen( file );
397 return i > 4 && !strcmp( file+i-4, ".lua" );
400 static int file_compare( const char **a, const char **b )
402 return strcmp( *a, *b );
405 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
406 char **ppsz_dir_list )
408 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
409 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
412 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
414 const char *psz_vlcpath = config_GetDataDir();
415 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
416 psz_vlcpath, luadirname ) < 0 )
419 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
420 psz_vlcpath, luadirname ) < 0 )
424 if( asprintf( &ppsz_dir_list[1],
425 "share" DIR_SEP "%s", luadirname ) < 0 )
428 # ifdef HAVE_SYS_STAT_H
430 struct stat stat_info;
431 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
432 || !S_ISDIR( stat_info.st_mode ) )
434 free(ppsz_dir_list[1]);
435 if( asprintf( &ppsz_dir_list[1],
436 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
445 /*****************************************************************************
446 * Will execute func on all scripts in luadirname, and stop if func returns
448 *****************************************************************************/
449 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
450 const char * luadirname,
451 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
455 int i_ret = VLC_EGENERIC;
458 char **ppsz_filelist = NULL;
459 char **ppsz_fileend = NULL;
462 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
465 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
466 if( i_ret != VLC_SUCCESS )
468 i_ret = VLC_EGENERIC;
471 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
477 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
480 free( ppsz_filelist );
481 ppsz_filelist = NULL;
489 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
490 dir = utf8_opendir( *ppsz_dir );
493 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
495 if( i_files < 1 ) continue;
496 ppsz_fileend = ppsz_filelist + i_files;
498 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
501 if( asprintf( &psz_filename,
502 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
504 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
506 i_ret = func( p_this, psz_filename, L, user_data );
508 free( psz_filename );
510 if( i_ret == VLC_SUCCESS ) break;
512 if( i_ret == VLC_SUCCESS ) break;
517 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
520 free( ppsz_filelist );
522 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
525 if( dir ) closedir( dir );
531 /*****************************************************************************
532 * Meta data setters utility.
533 * Playlist item table should be on top of the stack when these are called
534 *****************************************************************************/
535 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
536 input_item_t *p_input )
538 #define TRY_META( a, b ) \
539 lua_getfield( L, -1, a ); \
540 if( lua_isstring( L, -1 ) ) \
542 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
543 EnsureUTF8( psz_value ); \
544 msg_Dbg( p_this, #b ": %s", psz_value ); \
545 input_item_Set ## b ( p_input, psz_value ); \
548 lua_pop( L, 1 ); /* pop a */
549 TRY_META( "title", Title );
550 TRY_META( "artist", Artist );
551 TRY_META( "genre", Genre );
552 TRY_META( "copyright", Copyright );
553 TRY_META( "album", Album );
554 TRY_META( "tracknum", TrackNum );
555 TRY_META( "description", Description );
556 TRY_META( "rating", Rating );
557 TRY_META( "date", Date );
558 TRY_META( "setting", Setting );
559 TRY_META( "url", URL );
560 TRY_META( "language", Language );
561 TRY_META( "nowplaying", NowPlaying );
562 TRY_META( "publisher", Publisher );
563 TRY_META( "encodedby", EncodedBy );
564 TRY_META( "arturl", ArtURL );
565 TRY_META( "trackid", TrackID );
568 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
569 input_item_t *p_input )
572 lua_getfield( L, -1, "meta" );
574 if( lua_istable( L, -1 ) )
577 /* ... item meta nil */
578 while( lua_next( L, -2 ) )
580 /* ... item meta key value */
581 if( !lua_isstring( L, -2 ) )
583 msg_Warn( p_this, "Custom meta data category name must be "
586 else if( !lua_istable( L, -1 ) )
588 msg_Warn( p_this, "Custom meta data category contents "
593 const char *psz_meta_category = lua_tostring( L, -2 );
594 msg_Dbg( p_this, "Found custom meta data category: %s",
597 /* ... item meta key value nil */
598 while( lua_next( L, -2 ) )
600 /* ... item meta key value key2 value2 */
601 if( !lua_isstring( L, -2 ) )
603 msg_Warn( p_this, "Custom meta category item name "
604 "must be a string." );
606 else if( !lua_isstring( L, -1 ) )
608 msg_Warn( p_this, "Custom meta category item value "
609 "must be a string." );
613 const char *psz_meta_name =
614 lua_tostring( L, -2 );
615 const char *psz_meta_value =
616 lua_tostring( L, -1 );
617 msg_Dbg( p_this, "Custom meta %s, %s: %s",
618 psz_meta_category, psz_meta_name,
620 input_ItemAddInfo( p_input, psz_meta_category,
621 psz_meta_name, psz_meta_value );
623 lua_pop( L, 1 ); /* pop item */
624 /* ... item meta key value key2 */
626 /* ... item meta key value */
628 lua_pop( L, 1 ); /* pop category */
629 /* ... item meta key */
633 lua_pop( L, 1 ); /* pop "meta" */
634 /* ... item -> back to original stack */
637 /*****************************************************************************
639 ****************************************************************************/
641 * Playlist item table should be on top of the stack when this is called
643 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
644 int *pi_options, char ***pppsz_options )
646 lua_getfield( L, -1, "options" );
647 if( lua_istable( L, -1 ) )
650 while( lua_next( L, -2 ) )
652 if( lua_isstring( L, -1 ) )
654 char *psz_option = strdup( lua_tostring( L, -1 ) );
655 msg_Dbg( p_this, "Option: %s", psz_option );
656 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
661 msg_Warn( p_this, "Option should be a string" );
663 lua_pop( L, 1 ); /* pop option */
666 lua_pop( L, 1 ); /* pop "options" */
669 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
670 playlist_t *p_playlist,
671 input_item_t *p_parent, vlc_bool_t b_play )
676 if( lua_istable( L, -1 ) )
680 while( lua_next( L, -2 ) )
682 /* playlist key item */
683 /* <Parse playlist item> */
684 if( lua_istable( L, -1 ) )
686 lua_getfield( L, -1, "path" );
687 /* playlist key item path */
688 if( lua_isstring( L, -1 ) )
690 const char *psz_path = NULL;
691 const char *psz_name = NULL;
692 char **ppsz_options = NULL;
694 mtime_t i_duration = -1;
695 input_item_t *p_input;
697 /* Read path and name */
698 psz_path = lua_tostring( L, -1 );
699 msg_Dbg( p_this, "Path: %s", psz_path );
700 lua_getfield( L, -2, "name" );
701 /* playlist key item path name */
702 if( lua_isstring( L, -1 ) )
704 psz_name = lua_tostring( L, -1 );
705 msg_Dbg( p_this, "Name: %s", psz_name );
709 if( !lua_isnil( L, -1 ) )
710 msg_Warn( p_this, "Playlist item name should be a string." );
715 lua_getfield( L, -3, "duration" );
716 /* playlist key item path name duration */
717 if( lua_isnumber( L, -1 ) )
719 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
721 else if( !lua_isnil( L, -1 ) )
723 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
725 lua_pop( L, 1 ); /* pop "duration" */
727 /* playlist key item path name */
729 /* Read options: item must be on top of stack */
730 lua_pushvalue( L, -3 );
731 /* playlist key item path name item */
732 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
734 /* Create input item */
735 p_input = input_ItemNewExt( p_playlist, psz_path,
737 (const char **)ppsz_options,
739 lua_pop( L, 3 ); /* pop "path name item" */
740 /* playlist key item */
742 /* Read meta data: item must be on top of stack */
743 vlclua_read_meta_data( p_this, L, p_input );
745 /* Read custom meta data: item must be on top of stack*/
746 vlclua_read_custom_meta_data( p_this, L, p_input );
748 /* Append item to playlist */
749 if( p_parent ) /* Add to node */
750 input_ItemAddSubItem( p_parent, p_input );
751 else if( b_play ) /* Play */
752 playlist_AddInput( p_playlist, p_input,
753 PLAYLIST_APPEND | PLAYLIST_GO,
754 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
756 playlist_AddInput( p_playlist, p_input,
757 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
758 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
759 i_count ++; /* increment counter */
760 vlc_gc_decref( p_input );
761 while( i_options > 0 )
762 free( ppsz_options[--i_options] );
763 free( ppsz_options );
767 lua_pop( L, 1 ); /* pop "path" */
769 "Playlist item's path should be a string" );
771 /* playlist key item */
775 msg_Warn( p_this, "Playlist item should be a table" );
777 /* <Parse playlist item> */
778 lua_pop( L, 1 ); /* pop the value, keep the key for
779 * the next lua_next() call */
786 msg_Warn( p_this, "Playlist should be a table." );