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 /*****************************************************************************
122 * Get the VLC license msg/disclaimer
123 *****************************************************************************/
124 int vlclua_license( lua_State *L )
126 lua_pushstring( L, LICENSE_MSG );
130 /*****************************************************************************
132 *****************************************************************************/
133 int vlclua_quit( lua_State *L )
135 vlc_object_t *p_this = vlclua_get_this( L );
136 /* The rc.c code also stops the playlist ... not sure if this is needed
138 vlc_object_kill( p_this->p_libvlc );
142 /*****************************************************************************
144 *****************************************************************************/
145 int vlclua_volume_set( lua_State *L )
147 vlc_object_t *p_this = vlclua_get_this( L );
148 int i_volume = luaL_checkint( L, 1 );
149 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
150 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
153 int vlclua_volume_get( lua_State *L )
155 vlc_object_t *p_this = vlclua_get_this( L );
156 audio_volume_t i_volume;
157 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
158 lua_pushnumber( L, i_volume );
164 int vlclua_volume_up( lua_State *L )
166 audio_volume_t i_volume;
167 aout_VolumeUp( vlclua_get_this( L ),
168 luaL_optint( L, 1, 1 ),
170 lua_pushnumber( L, i_volume );
174 int vlclua_volume_down( lua_State *L )
176 audio_volume_t i_volume;
177 aout_VolumeDown( vlclua_get_this( L ),
178 luaL_optint( L, 1, 1 ),
180 lua_pushnumber( L, i_volume );
184 /*****************************************************************************
186 *****************************************************************************/
187 int vlclua_stream_new( lua_State *L )
189 vlc_object_t * p_this = vlclua_get_this( L );
191 const char * psz_url;
192 psz_url = luaL_checkstring( L, -1 );
193 p_stream = stream_UrlNew( p_this, psz_url );
195 return luaL_error( L, "Error when opening url: `%s'", psz_url );
196 lua_pushlightuserdata( L, p_stream );
200 int vlclua_stream_read( lua_State *L )
206 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
207 n = luaL_checkint( L, 2 );
208 p_read = malloc( n );
209 if( !p_read ) return vlclua_error( L );
210 i_read = stream_Read( p_stream, p_read, n );
211 lua_pushlstring( L, (const char *)p_read, i_read );
216 int vlclua_stream_readline( lua_State *L )
219 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
220 char *psz_line = stream_ReadLine( p_stream );
223 lua_pushstring( L, psz_line );
231 int vlclua_stream_delete( lua_State *L )
234 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
235 stream_Delete( p_stream );
239 /*****************************************************************************
240 * String transformations
241 *****************************************************************************/
242 int vlclua_decode_uri( lua_State *L )
244 int i_top = lua_gettop( L );
246 for( i = 1; i <= i_top; i++ )
248 const char *psz_cstring = luaL_checkstring( L, 1 );
249 char *psz_string = strdup( psz_cstring );
250 lua_remove( L, 1 ); /* remove elements to prevent being limited by
251 * the stack's size (this function will work with
252 * up to (stack size - 1) arguments */
253 decode_URI( psz_string );
254 lua_pushstring( L, psz_string );
260 int vlclua_resolve_xml_special_chars( lua_State *L )
262 int i_top = lua_gettop( L );
264 for( i = 1; i <= i_top; i++ )
266 const char *psz_cstring = luaL_checkstring( L, 1 );
267 char *psz_string = strdup( psz_cstring );
268 lua_remove( L, 1 ); /* remove elements to prevent being limited by
269 * the stack's size (this function will work with
270 * up to (stack size - 1) arguments */
271 resolve_xml_special_chars( psz_string );
272 lua_pushstring( L, psz_string );
278 /*****************************************************************************
279 * Messaging facilities
280 *****************************************************************************/
281 int vlclua_msg_dbg( 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_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
290 int vlclua_msg_warn( 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_Warn( p_this, "%s", luaL_checkstring( L, i ) );
299 int vlclua_msg_err( 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_Err( p_this, "%s", luaL_checkstring( L, i ) );
308 int vlclua_msg_info( lua_State *L )
310 int i_top = lua_gettop( L );
311 vlc_object_t *p_this = vlclua_get_this( L );
313 for( i = 1; i <= i_top; i++ )
314 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
318 /*****************************************************************************
320 *****************************************************************************/
321 static int file_select( const char *file )
323 int i = strlen( file );
324 return i > 4 && !strcmp( file+i-4, ".lua" );
327 static int file_compare( const char **a, const char **b )
329 return strcmp( *a, *b );
332 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
333 char **ppsz_dir_list )
335 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
336 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
339 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
341 const char *psz_vlcpath = config_GetDataDir();
342 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
343 psz_vlcpath, luadirname ) < 0 )
346 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
347 psz_vlcpath, luadirname ) < 0 )
351 if( asprintf( &ppsz_dir_list[1],
352 "share" DIR_SEP "%s", luadirname ) < 0 )
355 # ifdef HAVE_SYS_STAT_H
357 struct stat stat_info;
358 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
359 || !S_ISDIR( stat_info.st_mode ) )
361 free(ppsz_dir_list[1]);
362 if( asprintf( &ppsz_dir_list[1],
363 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
372 /*****************************************************************************
373 * Will execute func on all scripts in luadirname, and stop if func returns
375 *****************************************************************************/
376 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
377 const char * luadirname,
378 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
382 int i_ret = VLC_EGENERIC;
385 char **ppsz_filelist = NULL;
386 char **ppsz_fileend = NULL;
389 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
392 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
393 if( i_ret != VLC_SUCCESS )
395 i_ret = VLC_EGENERIC;
398 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
404 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
407 free( ppsz_filelist );
408 ppsz_filelist = NULL;
416 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
417 dir = utf8_opendir( *ppsz_dir );
420 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
422 if( i_files < 1 ) continue;
423 ppsz_fileend = ppsz_filelist + i_files;
425 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
428 if( asprintf( &psz_filename,
429 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
431 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
433 i_ret = func( p_this, psz_filename, L, user_data );
435 free( psz_filename );
437 if( i_ret == VLC_SUCCESS ) break;
439 if( i_ret == VLC_SUCCESS ) break;
444 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
447 free( ppsz_filelist );
449 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
452 if( dir ) closedir( dir );
458 /*****************************************************************************
459 * Meta data setters utility.
460 * Playlist item table should be on top of the stack when these are called
461 *****************************************************************************/
462 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
463 input_item_t *p_input )
465 #define TRY_META( a, b ) \
466 lua_getfield( L, -1, a ); \
467 if( lua_isstring( L, -1 ) ) \
469 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
470 EnsureUTF8( psz_value ); \
471 msg_Dbg( p_this, #b ": %s", psz_value ); \
472 input_item_Set ## b ( p_input, psz_value ); \
475 lua_pop( L, 1 ); /* pop a */
476 TRY_META( "title", Title );
477 TRY_META( "artist", Artist );
478 TRY_META( "genre", Genre );
479 TRY_META( "copyright", Copyright );
480 TRY_META( "album", Album );
481 TRY_META( "tracknum", TrackNum );
482 TRY_META( "description", Description );
483 TRY_META( "rating", Rating );
484 TRY_META( "date", Date );
485 TRY_META( "setting", Setting );
486 TRY_META( "url", URL );
487 TRY_META( "language", Language );
488 TRY_META( "nowplaying", NowPlaying );
489 TRY_META( "publisher", Publisher );
490 TRY_META( "encodedby", EncodedBy );
491 TRY_META( "arturl", ArtURL );
492 TRY_META( "trackid", TrackID );
495 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
496 input_item_t *p_input )
499 lua_getfield( L, -1, "meta" );
501 if( lua_istable( L, -1 ) )
504 /* ... item meta nil */
505 while( lua_next( L, -2 ) )
507 /* ... item meta key value */
508 if( !lua_isstring( L, -2 ) )
510 msg_Warn( p_this, "Custom meta data category name must be "
513 else if( !lua_istable( L, -1 ) )
515 msg_Warn( p_this, "Custom meta data category contents "
520 const char *psz_meta_category = lua_tostring( L, -2 );
521 msg_Dbg( p_this, "Found custom meta data category: %s",
524 /* ... item meta key value nil */
525 while( lua_next( L, -2 ) )
527 /* ... item meta key value key2 value2 */
528 if( !lua_isstring( L, -2 ) )
530 msg_Warn( p_this, "Custom meta category item name "
531 "must be a string." );
533 else if( !lua_isstring( L, -1 ) )
535 msg_Warn( p_this, "Custom meta category item value "
536 "must be a string." );
540 const char *psz_meta_name =
541 lua_tostring( L, -2 );
542 const char *psz_meta_value =
543 lua_tostring( L, -1 );
544 msg_Dbg( p_this, "Custom meta %s, %s: %s",
545 psz_meta_category, psz_meta_name,
547 input_ItemAddInfo( p_input, psz_meta_category,
548 psz_meta_name, psz_meta_value );
550 lua_pop( L, 1 ); /* pop item */
551 /* ... item meta key value key2 */
553 /* ... item meta key value */
555 lua_pop( L, 1 ); /* pop category */
556 /* ... item meta key */
560 lua_pop( L, 1 ); /* pop "meta" */
561 /* ... item -> back to original stack */
564 /*****************************************************************************
566 ****************************************************************************/
568 * Playlist item table should be on top of the stack when this is called
570 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
571 int *pi_options, char ***pppsz_options )
573 lua_getfield( L, -1, "options" );
574 if( lua_istable( L, -1 ) )
577 while( lua_next( L, -2 ) )
579 if( lua_isstring( L, -1 ) )
581 char *psz_option = strdup( lua_tostring( L, -1 ) );
582 msg_Dbg( p_this, "Option: %s", psz_option );
583 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
588 msg_Warn( p_this, "Option should be a string" );
590 lua_pop( L, 1 ); /* pop option */
593 lua_pop( L, 1 ); /* pop "options" */
596 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
597 playlist_t *p_playlist,
598 input_item_t *p_parent, vlc_bool_t b_play )
603 if( lua_istable( L, -1 ) )
607 while( lua_next( L, -2 ) )
609 /* playlist key item */
610 /* <Parse playlist item> */
611 if( lua_istable( L, -1 ) )
613 lua_getfield( L, -1, "path" );
614 /* playlist key item path */
615 if( lua_isstring( L, -1 ) )
617 const char *psz_path = NULL;
618 const char *psz_name = NULL;
619 char **ppsz_options = NULL;
621 mtime_t i_duration = -1;
622 input_item_t *p_input;
624 /* Read path and name */
625 psz_path = lua_tostring( L, -1 );
626 msg_Dbg( p_this, "Path: %s", psz_path );
627 lua_getfield( L, -2, "name" );
628 /* playlist key item path name */
629 if( lua_isstring( L, -1 ) )
631 psz_name = lua_tostring( L, -1 );
632 msg_Dbg( p_this, "Name: %s", psz_name );
636 if( !lua_isnil( L, -1 ) )
637 msg_Warn( p_this, "Playlist item name should be a string." );
642 lua_getfield( L, -3, "duration" );
643 /* playlist key item path name duration */
644 if( lua_isnumber( L, -1 ) )
646 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
648 else if( !lua_isnil( L, -1 ) )
650 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
652 lua_pop( L, 1 ); /* pop "duration" */
654 /* playlist key item path name */
656 /* Read options: item must be on top of stack */
657 lua_pushvalue( L, -3 );
658 /* playlist key item path name item */
659 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
661 /* Create input item */
662 p_input = input_ItemNewExt( p_playlist, psz_path,
664 (const char **)ppsz_options,
666 lua_pop( L, 3 ); /* pop "path name item" */
667 /* playlist key item */
669 /* Read meta data: item must be on top of stack */
670 vlclua_read_meta_data( p_this, L, p_input );
672 /* Read custom meta data: item must be on top of stack*/
673 vlclua_read_custom_meta_data( p_this, L, p_input );
675 /* Append item to playlist */
676 if( p_parent ) /* Add to node */
677 input_ItemAddSubItem( p_parent, p_input );
678 else if( b_play ) /* Play */
679 playlist_AddInput( p_playlist, p_input,
680 PLAYLIST_APPEND | PLAYLIST_GO,
681 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
683 playlist_AddInput( p_playlist, p_input,
684 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
685 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
686 i_count ++; /* increment counter */
688 while( i_options > 0 )
689 free( ppsz_options[--i_options] );
690 free( ppsz_options );
694 lua_pop( L, 1 ); /* pop "path" */
696 "Playlist item's path should be a string" );
698 /* playlist key item */
702 msg_Warn( p_this, "Playlist item should be a table" );
704 /* <Parse playlist item> */
705 lua_pop( L, 1 ); /* pop the value, keep the key for
706 * the next lua_next() call */
713 msg_Warn( p_this, "Playlist should be a table." );