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 copyright
123 *****************************************************************************/
124 int vlclua_copyright( lua_State *L )
126 lua_pushstring( L, COPYRIGHT_MESSAGE );
130 /*****************************************************************************
131 * Get the VLC license msg/disclaimer
132 *****************************************************************************/
133 int vlclua_license( lua_State *L )
135 lua_pushstring( L, LICENSE_MSG );
139 /*****************************************************************************
141 *****************************************************************************/
142 int vlclua_quit( lua_State *L )
144 vlc_object_t *p_this = vlclua_get_this( L );
145 /* The rc.c code also stops the playlist ... not sure if this is needed
147 vlc_object_kill( p_this->p_libvlc );
151 /*****************************************************************************
152 * Global properties getters
153 *****************************************************************************/
154 int vlclua_datadir( lua_State *L )
156 lua_pushstring( L, config_GetDataDir() );
159 int vlclua_homedir( lua_State *L )
161 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
164 int vlclua_configdir( lua_State *L )
166 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
169 int vlclua_cachedir( lua_State *L )
171 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
174 int vlclua_datadir_list( lua_State *L )
176 const char *psz_dirname = luaL_checkstring( L, 1 );
177 vlc_object_t *p_this = vlclua_get_this( L );
178 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
179 char **ppsz_dir = ppsz_dir_list;
182 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
185 for( ; *ppsz_dir; ppsz_dir++ )
187 lua_pushstring( L, *ppsz_dir );
188 lua_rawseti( L, -2, i );
194 /*****************************************************************************
196 *****************************************************************************/
197 int vlclua_volume_set( lua_State *L )
199 vlc_object_t *p_this = vlclua_get_this( L );
200 int i_volume = luaL_checkint( L, 1 );
201 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
202 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
205 int vlclua_volume_get( lua_State *L )
207 vlc_object_t *p_this = vlclua_get_this( L );
208 audio_volume_t i_volume;
209 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
210 lua_pushnumber( L, i_volume );
216 int vlclua_volume_up( lua_State *L )
218 audio_volume_t i_volume;
219 aout_VolumeUp( vlclua_get_this( L ),
220 luaL_optint( L, 1, 1 ),
222 lua_pushnumber( L, i_volume );
226 int vlclua_volume_down( lua_State *L )
228 audio_volume_t i_volume;
229 aout_VolumeDown( vlclua_get_this( L ),
230 luaL_optint( L, 1, 1 ),
232 lua_pushnumber( L, i_volume );
236 /*****************************************************************************
238 *****************************************************************************/
239 int vlclua_stream_new( lua_State *L )
241 vlc_object_t * p_this = vlclua_get_this( L );
243 const char * psz_url;
244 psz_url = luaL_checkstring( L, -1 );
245 p_stream = stream_UrlNew( p_this, psz_url );
247 return luaL_error( L, "Error when opening url: `%s'", psz_url );
248 lua_pushlightuserdata( L, p_stream );
252 int vlclua_stream_read( lua_State *L )
258 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
259 n = luaL_checkint( L, 2 );
260 p_read = malloc( n );
261 if( !p_read ) return vlclua_error( L );
262 i_read = stream_Read( p_stream, p_read, n );
263 lua_pushlstring( L, (const char *)p_read, i_read );
268 int vlclua_stream_readline( lua_State *L )
271 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
272 char *psz_line = stream_ReadLine( p_stream );
275 lua_pushstring( L, psz_line );
283 int vlclua_stream_delete( lua_State *L )
286 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
287 stream_Delete( p_stream );
291 /*****************************************************************************
292 * String transformations
293 *****************************************************************************/
294 int vlclua_decode_uri( lua_State *L )
296 int i_top = lua_gettop( L );
298 for( i = 1; i <= i_top; i++ )
300 const char *psz_cstring = luaL_checkstring( L, 1 );
301 char *psz_string = strdup( psz_cstring );
302 lua_remove( L, 1 ); /* remove elements to prevent being limited by
303 * the stack's size (this function will work with
304 * up to (stack size - 1) arguments */
305 decode_URI( psz_string );
306 lua_pushstring( L, psz_string );
312 int vlclua_resolve_xml_special_chars( lua_State *L )
314 int i_top = lua_gettop( L );
316 for( i = 1; i <= i_top; i++ )
318 const char *psz_cstring = luaL_checkstring( L, 1 );
319 char *psz_string = strdup( psz_cstring );
320 lua_remove( L, 1 ); /* remove elements to prevent being limited by
321 * the stack's size (this function will work with
322 * up to (stack size - 1) arguments */
323 resolve_xml_special_chars( psz_string );
324 lua_pushstring( L, psz_string );
330 int vlclua_convert_xml_special_chars( lua_State *L )
332 int i_top = lua_gettop( L );
334 for( i = 1; i <= i_top; i++ )
336 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
338 lua_pushstring( L, psz_string );
344 /*****************************************************************************
345 * Messaging facilities
346 *****************************************************************************/
347 int vlclua_msg_dbg( lua_State *L )
349 int i_top = lua_gettop( L );
350 vlc_object_t *p_this = vlclua_get_this( L );
352 for( i = 1; i <= i_top; i++ )
353 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
356 int vlclua_msg_warn( lua_State *L )
358 int i_top = lua_gettop( L );
359 vlc_object_t *p_this = vlclua_get_this( L );
361 for( i = 1; i <= i_top; i++ )
362 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
365 int vlclua_msg_err( lua_State *L )
367 int i_top = lua_gettop( L );
368 vlc_object_t *p_this = vlclua_get_this( L );
370 for( i = 1; i <= i_top; i++ )
371 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
374 int vlclua_msg_info( lua_State *L )
376 int i_top = lua_gettop( L );
377 vlc_object_t *p_this = vlclua_get_this( L );
379 for( i = 1; i <= i_top; i++ )
380 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
384 /*****************************************************************************
386 *****************************************************************************/
387 static int file_select( const char *file )
389 int i = strlen( file );
390 return i > 4 && !strcmp( file+i-4, ".lua" );
393 static int file_compare( const char **a, const char **b )
395 return strcmp( *a, *b );
398 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
399 char **ppsz_dir_list )
401 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
402 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
405 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
407 const char *psz_vlcpath = config_GetDataDir();
408 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
409 psz_vlcpath, luadirname ) < 0 )
412 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
413 psz_vlcpath, luadirname ) < 0 )
417 if( asprintf( &ppsz_dir_list[1],
418 "share" DIR_SEP "%s", luadirname ) < 0 )
421 # ifdef HAVE_SYS_STAT_H
423 struct stat stat_info;
424 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
425 || !S_ISDIR( stat_info.st_mode ) )
427 free(ppsz_dir_list[1]);
428 if( asprintf( &ppsz_dir_list[1],
429 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
438 /*****************************************************************************
439 * Will execute func on all scripts in luadirname, and stop if func returns
441 *****************************************************************************/
442 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
443 const char * luadirname,
444 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
448 int i_ret = VLC_EGENERIC;
451 char **ppsz_filelist = NULL;
452 char **ppsz_fileend = NULL;
455 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
458 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
459 if( i_ret != VLC_SUCCESS )
461 i_ret = VLC_EGENERIC;
464 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
470 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
473 free( ppsz_filelist );
474 ppsz_filelist = NULL;
482 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
483 dir = utf8_opendir( *ppsz_dir );
486 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
488 if( i_files < 1 ) continue;
489 ppsz_fileend = ppsz_filelist + i_files;
491 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
494 if( asprintf( &psz_filename,
495 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
497 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
499 i_ret = func( p_this, psz_filename, L, user_data );
501 free( psz_filename );
503 if( i_ret == VLC_SUCCESS ) break;
505 if( i_ret == VLC_SUCCESS ) break;
510 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
513 free( ppsz_filelist );
515 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
518 if( dir ) closedir( dir );
524 /*****************************************************************************
525 * Meta data setters utility.
526 * Playlist item table should be on top of the stack when these are called
527 *****************************************************************************/
528 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
529 input_item_t *p_input )
531 #define TRY_META( a, b ) \
532 lua_getfield( L, -1, a ); \
533 if( lua_isstring( L, -1 ) ) \
535 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
536 EnsureUTF8( psz_value ); \
537 msg_Dbg( p_this, #b ": %s", psz_value ); \
538 input_item_Set ## b ( p_input, psz_value ); \
541 lua_pop( L, 1 ); /* pop a */
542 TRY_META( "title", Title );
543 TRY_META( "artist", Artist );
544 TRY_META( "genre", Genre );
545 TRY_META( "copyright", Copyright );
546 TRY_META( "album", Album );
547 TRY_META( "tracknum", TrackNum );
548 TRY_META( "description", Description );
549 TRY_META( "rating", Rating );
550 TRY_META( "date", Date );
551 TRY_META( "setting", Setting );
552 TRY_META( "url", URL );
553 TRY_META( "language", Language );
554 TRY_META( "nowplaying", NowPlaying );
555 TRY_META( "publisher", Publisher );
556 TRY_META( "encodedby", EncodedBy );
557 TRY_META( "arturl", ArtURL );
558 TRY_META( "trackid", TrackID );
561 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
562 input_item_t *p_input )
565 lua_getfield( L, -1, "meta" );
567 if( lua_istable( L, -1 ) )
570 /* ... item meta nil */
571 while( lua_next( L, -2 ) )
573 /* ... item meta key value */
574 if( !lua_isstring( L, -2 ) )
576 msg_Warn( p_this, "Custom meta data category name must be "
579 else if( !lua_istable( L, -1 ) )
581 msg_Warn( p_this, "Custom meta data category contents "
586 const char *psz_meta_category = lua_tostring( L, -2 );
587 msg_Dbg( p_this, "Found custom meta data category: %s",
590 /* ... item meta key value nil */
591 while( lua_next( L, -2 ) )
593 /* ... item meta key value key2 value2 */
594 if( !lua_isstring( L, -2 ) )
596 msg_Warn( p_this, "Custom meta category item name "
597 "must be a string." );
599 else if( !lua_isstring( L, -1 ) )
601 msg_Warn( p_this, "Custom meta category item value "
602 "must be a string." );
606 const char *psz_meta_name =
607 lua_tostring( L, -2 );
608 const char *psz_meta_value =
609 lua_tostring( L, -1 );
610 msg_Dbg( p_this, "Custom meta %s, %s: %s",
611 psz_meta_category, psz_meta_name,
613 input_ItemAddInfo( p_input, psz_meta_category,
614 psz_meta_name, psz_meta_value );
616 lua_pop( L, 1 ); /* pop item */
617 /* ... item meta key value key2 */
619 /* ... item meta key value */
621 lua_pop( L, 1 ); /* pop category */
622 /* ... item meta key */
626 lua_pop( L, 1 ); /* pop "meta" */
627 /* ... item -> back to original stack */
630 /*****************************************************************************
632 ****************************************************************************/
634 * Playlist item table should be on top of the stack when this is called
636 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
637 int *pi_options, char ***pppsz_options )
639 lua_getfield( L, -1, "options" );
640 if( lua_istable( L, -1 ) )
643 while( lua_next( L, -2 ) )
645 if( lua_isstring( L, -1 ) )
647 char *psz_option = strdup( lua_tostring( L, -1 ) );
648 msg_Dbg( p_this, "Option: %s", psz_option );
649 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
654 msg_Warn( p_this, "Option should be a string" );
656 lua_pop( L, 1 ); /* pop option */
659 lua_pop( L, 1 ); /* pop "options" */
662 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
663 playlist_t *p_playlist,
664 input_item_t *p_parent, vlc_bool_t b_play )
669 if( lua_istable( L, -1 ) )
673 while( lua_next( L, -2 ) )
675 /* playlist key item */
676 /* <Parse playlist item> */
677 if( lua_istable( L, -1 ) )
679 lua_getfield( L, -1, "path" );
680 /* playlist key item path */
681 if( lua_isstring( L, -1 ) )
683 const char *psz_path = NULL;
684 const char *psz_name = NULL;
685 char **ppsz_options = NULL;
687 mtime_t i_duration = -1;
688 input_item_t *p_input;
690 /* Read path and name */
691 psz_path = lua_tostring( L, -1 );
692 msg_Dbg( p_this, "Path: %s", psz_path );
693 lua_getfield( L, -2, "name" );
694 /* playlist key item path name */
695 if( lua_isstring( L, -1 ) )
697 psz_name = lua_tostring( L, -1 );
698 msg_Dbg( p_this, "Name: %s", psz_name );
702 if( !lua_isnil( L, -1 ) )
703 msg_Warn( p_this, "Playlist item name should be a string." );
708 lua_getfield( L, -3, "duration" );
709 /* playlist key item path name duration */
710 if( lua_isnumber( L, -1 ) )
712 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
714 else if( !lua_isnil( L, -1 ) )
716 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
718 lua_pop( L, 1 ); /* pop "duration" */
720 /* playlist key item path name */
722 /* Read options: item must be on top of stack */
723 lua_pushvalue( L, -3 );
724 /* playlist key item path name item */
725 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
727 /* Create input item */
728 p_input = input_ItemNewExt( p_playlist, psz_path,
730 (const char **)ppsz_options,
732 lua_pop( L, 3 ); /* pop "path name item" */
733 /* playlist key item */
735 /* Read meta data: item must be on top of stack */
736 vlclua_read_meta_data( p_this, L, p_input );
738 /* Read custom meta data: item must be on top of stack*/
739 vlclua_read_custom_meta_data( p_this, L, p_input );
741 /* Append item to playlist */
742 if( p_parent ) /* Add to node */
743 input_ItemAddSubItem( p_parent, p_input );
744 else if( b_play ) /* Play */
745 playlist_AddInput( p_playlist, p_input,
746 PLAYLIST_APPEND | PLAYLIST_GO,
747 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
749 playlist_AddInput( p_playlist, p_input,
750 PLAYLIST_APPEND | PLAYLIST_PREPARSE,
751 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
752 i_count ++; /* increment counter */
754 while( i_options > 0 )
755 free( ppsz_options[--i_options] );
756 free( ppsz_options );
760 lua_pop( L, 1 ); /* pop "path" */
762 "Playlist item's path should be a string" );
764 /* playlist key item */
768 msg_Warn( p_this, "Playlist item should be a table" );
770 /* <Parse playlist item> */
771 lua_pop( L, 1 ); /* pop the value, keep the key for
772 * the next lua_next() call */
779 msg_Warn( p_this, "Playlist should be a table." );