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( "demux", 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, false );
91 add_string( "lua-config", "", NULL,
92 CONFIG_TEXT, CONFIG_LONGTEXT, 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 )
409 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
410 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
414 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
416 const char *psz_vlcpath = config_GetDataDir();
417 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
418 psz_vlcpath, luadirname ) < 0 )
422 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
423 psz_vlcpath, luadirname ) < 0 )
427 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
428 psz_vlcpath, luadirname ) < 0 )
435 if( asprintf( &ppsz_dir_list[i],
436 "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
439 # ifdef HAVE_SYS_STAT_H
441 struct stat stat_info;
442 if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
443 || !S_ISDIR( stat_info.st_mode ) )
445 free(ppsz_dir_list[i]);
446 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
447 config_GetDataDir (), luadirname ) < 0 )
457 /*****************************************************************************
458 * Will execute func on all scripts in luadirname, and stop if func returns
460 *****************************************************************************/
461 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
462 const char * luadirname,
463 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
467 int i_ret = VLC_EGENERIC;
470 char **ppsz_filelist = NULL;
471 char **ppsz_fileend = NULL;
474 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
477 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
478 if( i_ret != VLC_SUCCESS )
480 i_ret = VLC_EGENERIC;
483 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
489 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
492 free( ppsz_filelist );
493 ppsz_filelist = NULL;
501 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
502 dir = utf8_opendir( *ppsz_dir );
505 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
507 if( i_files < 1 ) continue;
508 ppsz_fileend = ppsz_filelist + i_files;
510 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
513 if( asprintf( &psz_filename,
514 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
516 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
518 i_ret = func( p_this, psz_filename, L, user_data );
520 free( psz_filename );
522 if( i_ret == VLC_SUCCESS ) break;
524 if( i_ret == VLC_SUCCESS ) break;
529 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
532 free( ppsz_filelist );
534 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
537 if( dir ) closedir( dir );
543 /*****************************************************************************
544 * Meta data setters utility.
545 * Playlist item table should be on top of the stack when these are called
546 *****************************************************************************/
547 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
548 input_item_t *p_input )
550 #define TRY_META( a, b ) \
551 lua_getfield( L, -1, a ); \
552 if( lua_isstring( L, -1 ) ) \
554 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
555 EnsureUTF8( psz_value ); \
556 msg_Dbg( p_this, #b ": %s", psz_value ); \
557 input_item_Set ## b ( p_input, psz_value ); \
560 lua_pop( L, 1 ); /* pop a */
561 TRY_META( "title", Title );
562 TRY_META( "artist", Artist );
563 TRY_META( "genre", Genre );
564 TRY_META( "copyright", Copyright );
565 TRY_META( "album", Album );
566 TRY_META( "tracknum", TrackNum );
567 TRY_META( "description", Description );
568 TRY_META( "rating", Rating );
569 TRY_META( "date", Date );
570 TRY_META( "setting", Setting );
571 TRY_META( "url", URL );
572 TRY_META( "language", Language );
573 TRY_META( "nowplaying", NowPlaying );
574 TRY_META( "publisher", Publisher );
575 TRY_META( "encodedby", EncodedBy );
576 TRY_META( "arturl", ArtURL );
577 TRY_META( "trackid", TrackID );
580 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
581 input_item_t *p_input )
584 lua_getfield( L, -1, "meta" );
586 if( lua_istable( L, -1 ) )
589 /* ... item meta nil */
590 while( lua_next( L, -2 ) )
592 /* ... item meta key value */
593 if( !lua_isstring( L, -2 ) )
595 msg_Warn( p_this, "Custom meta data category name must be "
598 else if( !lua_istable( L, -1 ) )
600 msg_Warn( p_this, "Custom meta data category contents "
605 const char *psz_meta_category = lua_tostring( L, -2 );
606 msg_Dbg( p_this, "Found custom meta data category: %s",
609 /* ... item meta key value nil */
610 while( lua_next( L, -2 ) )
612 /* ... item meta key value key2 value2 */
613 if( !lua_isstring( L, -2 ) )
615 msg_Warn( p_this, "Custom meta category item name "
616 "must be a string." );
618 else if( !lua_isstring( L, -1 ) )
620 msg_Warn( p_this, "Custom meta category item value "
621 "must be a string." );
625 const char *psz_meta_name =
626 lua_tostring( L, -2 );
627 const char *psz_meta_value =
628 lua_tostring( L, -1 );
629 msg_Dbg( p_this, "Custom meta %s, %s: %s",
630 psz_meta_category, psz_meta_name,
632 input_ItemAddInfo( p_input, psz_meta_category,
633 psz_meta_name, psz_meta_value );
635 lua_pop( L, 1 ); /* pop item */
636 /* ... item meta key value key2 */
638 /* ... item meta key value */
640 lua_pop( L, 1 ); /* pop category */
641 /* ... item meta key */
645 lua_pop( L, 1 ); /* pop "meta" */
646 /* ... item -> back to original stack */
649 /*****************************************************************************
651 ****************************************************************************/
653 * Playlist item table should be on top of the stack when this is called
655 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
656 int *pi_options, char ***pppsz_options )
658 lua_getfield( L, -1, "options" );
659 if( lua_istable( L, -1 ) )
662 while( lua_next( L, -2 ) )
664 if( lua_isstring( L, -1 ) )
666 char *psz_option = strdup( lua_tostring( L, -1 ) );
667 msg_Dbg( p_this, "Option: %s", psz_option );
668 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
673 msg_Warn( p_this, "Option should be a string" );
675 lua_pop( L, 1 ); /* pop option */
678 lua_pop( L, 1 ); /* pop "options" */
681 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
682 playlist_t *p_playlist,
683 input_item_t *p_parent, bool b_play )
688 if( lua_istable( L, -1 ) )
692 while( lua_next( L, -2 ) )
694 /* playlist key item */
695 /* <Parse playlist item> */
696 if( lua_istable( L, -1 ) )
698 lua_getfield( L, -1, "path" );
699 /* playlist key item path */
700 if( lua_isstring( L, -1 ) )
702 const char *psz_path = NULL;
703 const char *psz_name = NULL;
704 char **ppsz_options = NULL;
706 mtime_t i_duration = -1;
707 input_item_t *p_input;
709 /* Read path and name */
710 psz_path = lua_tostring( L, -1 );
711 msg_Dbg( p_this, "Path: %s", psz_path );
712 lua_getfield( L, -2, "name" );
713 /* playlist key item path name */
714 if( lua_isstring( L, -1 ) )
716 psz_name = lua_tostring( L, -1 );
717 msg_Dbg( p_this, "Name: %s", psz_name );
721 if( !lua_isnil( L, -1 ) )
722 msg_Warn( p_this, "Playlist item name should be a string." );
727 lua_getfield( L, -3, "duration" );
728 /* playlist key item path name duration */
729 if( lua_isnumber( L, -1 ) )
731 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
733 else if( !lua_isnil( L, -1 ) )
735 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
737 lua_pop( L, 1 ); /* pop "duration" */
739 /* playlist key item path name */
741 /* Read options: item must be on top of stack */
742 lua_pushvalue( L, -3 );
743 /* playlist key item path name item */
744 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
746 /* Create input item */
747 p_input = input_ItemNewExt( p_playlist, psz_path,
749 (const char **)ppsz_options,
751 lua_pop( L, 3 ); /* pop "path name item" */
752 /* playlist key item */
754 /* Read meta data: item must be on top of stack */
755 vlclua_read_meta_data( p_this, L, p_input );
757 /* Read custom meta data: item must be on top of stack*/
758 vlclua_read_custom_meta_data( p_this, L, p_input );
760 /* Append item to playlist */
761 if( p_parent ) /* Add to node */
762 input_ItemAddSubItem( p_parent, p_input );
763 else /* Play or Enqueue (preparse) */
764 /* FIXME: playlist_AddInput() can fail */
765 playlist_AddInput( p_playlist, p_input,
767 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
768 PLAYLIST_END, true, false );
769 i_count ++; /* increment counter */
770 vlc_gc_decref( p_input );
771 while( i_options > 0 )
772 free( ppsz_options[--i_options] );
773 free( ppsz_options );
777 lua_pop( L, 1 ); /* pop "path" */
779 "Playlist item's path should be a string" );
781 /* playlist key item */
785 msg_Warn( p_this, "Playlist item should be a table" );
787 /* <Parse playlist item> */
788 lua_pop( L, 1 ); /* pop the value, keep the key for
789 * the next lua_next() call */
796 msg_Warn( p_this, "Playlist should be a table." );