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 psz_url = luaL_checkstring( L, -1 );
184 p_stream = stream_UrlNew( p_this, psz_url );
186 return luaL_error( L, "Error when opening url: `%s'", psz_url );
187 lua_pushlightuserdata( L, p_stream );
191 int vlclua_stream_read( lua_State *L )
197 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
198 n = luaL_checkint( L, 2 );
199 p_read = malloc( n );
200 if( !p_read ) return vlclua_error( L );
201 i_read = stream_Read( p_stream, p_read, n );
202 lua_pushlstring( L, (const char *)p_read, i_read );
207 int vlclua_stream_readline( lua_State *L )
210 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
211 char *psz_line = stream_ReadLine( p_stream );
214 lua_pushstring( L, psz_line );
222 int vlclua_stream_delete( lua_State *L )
225 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
226 stream_Delete( p_stream );
230 /*****************************************************************************
231 * String transformations
232 *****************************************************************************/
233 int vlclua_decode_uri( lua_State *L )
235 int i_top = lua_gettop( L );
237 for( i = 1; i <= i_top; i++ )
239 const char *psz_cstring = luaL_checkstring( L, 1 );
240 char *psz_string = strdup( psz_cstring );
241 lua_remove( L, 1 ); /* remove elements to prevent being limited by
242 * the stack's size (this function will work with
243 * up to (stack size - 1) arguments */
244 decode_URI( psz_string );
245 lua_pushstring( L, psz_string );
251 int vlclua_resolve_xml_special_chars( lua_State *L )
253 int i_top = lua_gettop( L );
255 for( i = 1; i <= i_top; i++ )
257 const char *psz_cstring = luaL_checkstring( L, 1 );
258 char *psz_string = strdup( psz_cstring );
259 lua_remove( L, 1 ); /* remove elements to prevent being limited by
260 * the stack's size (this function will work with
261 * up to (stack size - 1) arguments */
262 resolve_xml_special_chars( psz_string );
263 lua_pushstring( L, psz_string );
269 /*****************************************************************************
270 * Messaging facilities
271 *****************************************************************************/
272 int vlclua_msg_dbg( lua_State *L )
274 int i_top = lua_gettop( L );
275 vlc_object_t *p_this = vlclua_get_this( L );
277 for( i = 1; i <= i_top; i++ )
278 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
281 int vlclua_msg_warn( lua_State *L )
283 int i_top = lua_gettop( L );
284 vlc_object_t *p_this = vlclua_get_this( L );
286 for( i = 1; i <= i_top; i++ )
287 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
290 int vlclua_msg_err( lua_State *L )
292 int i_top = lua_gettop( L );
293 vlc_object_t *p_this = vlclua_get_this( L );
295 for( i = 1; i <= i_top; i++ )
296 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
299 int vlclua_msg_info( lua_State *L )
301 int i_top = lua_gettop( L );
302 vlc_object_t *p_this = vlclua_get_this( L );
304 for( i = 1; i <= i_top; i++ )
305 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
309 /*****************************************************************************
311 *****************************************************************************/
312 static int file_select( const char *file )
314 int i = strlen( file );
315 return i > 4 && !strcmp( file+i-4, ".lua" );
318 static int file_compare( const char **a, const char **b )
320 return strcmp( *a, *b );
323 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
324 char **ppsz_dir_list )
326 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
327 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
330 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
332 const char *psz_vlcpath = config_GetDataDir();
333 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
334 psz_vlcpath, luadirname ) < 0 )
337 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
338 psz_vlcpath, luadirname ) < 0 )
342 if( asprintf( &ppsz_dir_list[1],
343 "share" DIR_SEP "%s", luadirname ) < 0 )
346 # ifdef HAVE_SYS_STAT_H
348 struct stat stat_info;
349 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
350 || !S_ISDIR( stat_info.st_mode ) )
352 free(ppsz_dir_list[1]);
353 if( asprintf( &ppsz_dir_list[1],
354 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
363 /*****************************************************************************
364 * Will execute func on all scripts in luadirname, and stop if func returns
366 *****************************************************************************/
367 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
368 const char * luadirname,
369 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
373 int i_ret = VLC_EGENERIC;
376 char **ppsz_filelist = NULL;
377 char **ppsz_fileend = NULL;
380 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
383 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
384 if( i_ret != VLC_SUCCESS )
386 i_ret = VLC_EGENERIC;
389 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
395 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
398 free( ppsz_filelist );
399 ppsz_filelist = NULL;
407 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
408 dir = utf8_opendir( *ppsz_dir );
411 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
413 if( i_files < 1 ) continue;
414 ppsz_fileend = ppsz_filelist + i_files;
416 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
419 if( asprintf( &psz_filename,
420 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
422 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
424 i_ret = func( p_this, psz_filename, L, user_data );
426 free( psz_filename );
428 if( i_ret == VLC_SUCCESS ) break;
430 if( i_ret == VLC_SUCCESS ) break;
435 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
438 free( ppsz_filelist );
440 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
443 if( dir ) closedir( dir );
449 /*****************************************************************************
450 * Meta data setters utility.
451 * Playlist item table should be on top of the stack when these are called
452 *****************************************************************************/
453 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
454 input_item_t *p_input )
456 #define TRY_META( a, b ) \
457 lua_getfield( L, -1, a ); \
458 if( lua_isstring( L, -1 ) ) \
460 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
461 EnsureUTF8( psz_value ); \
462 msg_Dbg( p_this, #b ": %s", psz_value ); \
463 input_item_Set ## b ( p_input, psz_value ); \
466 lua_pop( L, 1 ); /* pop a */
467 TRY_META( "title", Title );
468 TRY_META( "artist", Artist );
469 TRY_META( "genre", Genre );
470 TRY_META( "copyright", Copyright );
471 TRY_META( "album", Album );
472 TRY_META( "tracknum", TrackNum );
473 TRY_META( "description", Description );
474 TRY_META( "rating", Rating );
475 TRY_META( "date", Date );
476 TRY_META( "setting", Setting );
477 TRY_META( "url", URL );
478 TRY_META( "language", Language );
479 TRY_META( "nowplaying", NowPlaying );
480 TRY_META( "publisher", Publisher );
481 TRY_META( "encodedby", EncodedBy );
482 TRY_META( "arturl", ArtURL );
483 TRY_META( "trackid", TrackID );
486 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
487 input_item_t *p_input )
490 lua_getfield( L, -1, "meta" );
492 if( lua_istable( L, -1 ) )
495 /* ... item meta nil */
496 while( lua_next( L, -2 ) )
498 /* ... item meta key value */
499 if( !lua_isstring( L, -2 ) )
501 msg_Warn( p_this, "Custom meta data category name must be "
504 else if( !lua_istable( L, -1 ) )
506 msg_Warn( p_this, "Custom meta data category contents "
511 const char *psz_meta_category = lua_tostring( L, -2 );
512 msg_Dbg( p_this, "Found custom meta data category: %s",
515 /* ... item meta key value nil */
516 while( lua_next( L, -2 ) )
518 /* ... item meta key value key2 value2 */
519 if( !lua_isstring( L, -2 ) )
521 msg_Warn( p_this, "Custom meta category item name "
522 "must be a string." );
524 else if( !lua_isstring( L, -1 ) )
526 msg_Warn( p_this, "Custom meta category item value "
527 "must be a string." );
531 const char *psz_meta_name =
532 lua_tostring( L, -2 );
533 const char *psz_meta_value =
534 lua_tostring( L, -1 );
535 msg_Dbg( p_this, "Custom meta %s, %s: %s",
536 psz_meta_category, psz_meta_name,
538 input_ItemAddInfo( p_input, psz_meta_category,
539 psz_meta_name, psz_meta_value );
541 lua_pop( L, 1 ); /* pop item */
542 /* ... item meta key value key2 */
544 /* ... item meta key value */
546 lua_pop( L, 1 ); /* pop category */
547 /* ... item meta key */
551 lua_pop( L, 1 ); /* pop "meta" */
552 /* ... item -> back to original stack */
555 /*****************************************************************************
557 ****************************************************************************/
559 * Playlist item table should be on top of the stack when this is called
561 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
562 int *pi_options, char ***pppsz_options )
564 lua_getfield( L, -1, "options" );
565 if( lua_istable( L, -1 ) )
568 while( lua_next( L, -2 ) )
570 if( lua_isstring( L, -1 ) )
572 char *psz_option = strdup( lua_tostring( L, -1 ) );
573 msg_Dbg( p_this, "Option: %s", psz_option );
574 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
579 msg_Warn( p_this, "Option should be a string" );
581 lua_pop( L, 1 ); /* pop option */
584 lua_pop( L, 1 ); /* pop "options" */
587 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
588 playlist_t *p_playlist,
589 input_item_t *p_parent, vlc_bool_t b_play )
594 if( lua_istable( L, -1 ) )
598 while( lua_next( L, -2 ) )
600 /* playlist key item */
601 /* <Parse playlist item> */
602 if( lua_istable( L, -1 ) )
604 lua_getfield( L, -1, "path" );
605 /* playlist key item path */
606 if( lua_isstring( L, -1 ) )
608 const char *psz_path = NULL;
609 const char *psz_name = NULL;
610 char **ppsz_options = NULL;
612 mtime_t i_duration = -1;
613 input_item_t *p_input;
615 /* Read path and name */
616 psz_path = lua_tostring( L, -1 );
617 msg_Dbg( p_this, "Path: %s", psz_path );
618 lua_getfield( L, -2, "name" );
619 /* playlist key item path name */
620 if( lua_isstring( L, -1 ) )
622 psz_name = lua_tostring( L, -1 );
623 msg_Dbg( p_this, "Name: %s", psz_name );
627 if( !lua_isnil( L, -1 ) )
628 msg_Warn( p_this, "Playlist item name should be a string." );
633 lua_getfield( L, -3, "duration" );
634 /* playlist key item path name duration */
635 if( lua_isnumber( L, -1 ) )
637 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
639 else if( !lua_isnil( L, -1 ) )
641 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
643 lua_pop( L, 1 ); /* pop "duration" */
645 /* playlist key item path name */
647 /* Read options: item must be on top of stack */
648 lua_pushvalue( L, -3 );
649 /* playlist key item path name item */
650 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
652 /* Create input item */
653 p_input = input_ItemNewExt( p_playlist, psz_path,
655 (const char **)ppsz_options,
657 lua_pop( L, 3 ); /* pop "path name item" */
658 /* playlist key item */
660 /* Read meta data: item must be on top of stack */
661 vlclua_read_meta_data( p_this, L, p_input );
663 /* Read custom meta data: item must be on top of stack*/
664 vlclua_read_custom_meta_data( p_this, L, p_input );
666 /* Append item to playlist */
667 if( p_parent ) /* Add to node */
668 input_ItemAddSubItem( p_parent, p_input );
669 else if( b_play ) /* Play */
670 playlist_AddInput( p_playlist, p_input,
671 PLAYLIST_APPEND | PLAYLIST_GO,
672 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
674 playlist_AddInput( p_playlist, p_input,
675 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
676 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
677 i_count ++; /* increment counter */
679 while( i_options > 0 )
680 free( ppsz_options[--i_options] );
681 free( ppsz_options );
685 lua_pop( L, 1 ); /* pop "path" */
687 "Playlist item's path should be a string" );
689 /* playlist key item */
693 msg_Warn( p_this, "Playlist item should be a table" );
695 /* <Parse playlist item> */
696 lua_pop( L, 1 ); /* pop the value, keep the key for
697 * the next lua_next() call */
704 msg_Warn( p_this, "Playlist should be a table." );