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 )
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 "share" DIR_SEP "lua" DIR_SEP "%s",
423 psz_vlcpath, luadirname ) < 0 )
428 if( asprintf( &ppsz_dir_list[i],
429 "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
432 # ifdef HAVE_SYS_STAT_H
434 struct stat stat_info;
435 if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
436 || !S_ISDIR( stat_info.st_mode ) )
438 free(ppsz_dir_list[i]);
439 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
440 config_GetDataDir (), luadirname ) < 0 )
450 /*****************************************************************************
451 * Will execute func on all scripts in luadirname, and stop if func returns
453 *****************************************************************************/
454 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
455 const char * luadirname,
456 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
460 int i_ret = VLC_EGENERIC;
463 char **ppsz_filelist = NULL;
464 char **ppsz_fileend = NULL;
467 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
470 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
471 if( i_ret != VLC_SUCCESS )
473 i_ret = VLC_EGENERIC;
476 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
482 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
485 free( ppsz_filelist );
486 ppsz_filelist = NULL;
494 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
495 dir = utf8_opendir( *ppsz_dir );
498 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
500 if( i_files < 1 ) continue;
501 ppsz_fileend = ppsz_filelist + i_files;
503 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
506 if( asprintf( &psz_filename,
507 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
509 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
511 i_ret = func( p_this, psz_filename, L, user_data );
513 free( psz_filename );
515 if( i_ret == VLC_SUCCESS ) break;
517 if( i_ret == VLC_SUCCESS ) break;
522 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
525 free( ppsz_filelist );
527 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
530 if( dir ) closedir( dir );
536 /*****************************************************************************
537 * Meta data setters utility.
538 * Playlist item table should be on top of the stack when these are called
539 *****************************************************************************/
540 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
541 input_item_t *p_input )
543 #define TRY_META( a, b ) \
544 lua_getfield( L, -1, a ); \
545 if( lua_isstring( L, -1 ) ) \
547 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
548 EnsureUTF8( psz_value ); \
549 msg_Dbg( p_this, #b ": %s", psz_value ); \
550 input_item_Set ## b ( p_input, psz_value ); \
553 lua_pop( L, 1 ); /* pop a */
554 TRY_META( "title", Title );
555 TRY_META( "artist", Artist );
556 TRY_META( "genre", Genre );
557 TRY_META( "copyright", Copyright );
558 TRY_META( "album", Album );
559 TRY_META( "tracknum", TrackNum );
560 TRY_META( "description", Description );
561 TRY_META( "rating", Rating );
562 TRY_META( "date", Date );
563 TRY_META( "setting", Setting );
564 TRY_META( "url", URL );
565 TRY_META( "language", Language );
566 TRY_META( "nowplaying", NowPlaying );
567 TRY_META( "publisher", Publisher );
568 TRY_META( "encodedby", EncodedBy );
569 TRY_META( "arturl", ArtURL );
570 TRY_META( "trackid", TrackID );
573 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
574 input_item_t *p_input )
577 lua_getfield( L, -1, "meta" );
579 if( lua_istable( L, -1 ) )
582 /* ... item meta nil */
583 while( lua_next( L, -2 ) )
585 /* ... item meta key value */
586 if( !lua_isstring( L, -2 ) )
588 msg_Warn( p_this, "Custom meta data category name must be "
591 else if( !lua_istable( L, -1 ) )
593 msg_Warn( p_this, "Custom meta data category contents "
598 const char *psz_meta_category = lua_tostring( L, -2 );
599 msg_Dbg( p_this, "Found custom meta data category: %s",
602 /* ... item meta key value nil */
603 while( lua_next( L, -2 ) )
605 /* ... item meta key value key2 value2 */
606 if( !lua_isstring( L, -2 ) )
608 msg_Warn( p_this, "Custom meta category item name "
609 "must be a string." );
611 else if( !lua_isstring( L, -1 ) )
613 msg_Warn( p_this, "Custom meta category item value "
614 "must be a string." );
618 const char *psz_meta_name =
619 lua_tostring( L, -2 );
620 const char *psz_meta_value =
621 lua_tostring( L, -1 );
622 msg_Dbg( p_this, "Custom meta %s, %s: %s",
623 psz_meta_category, psz_meta_name,
625 input_ItemAddInfo( p_input, psz_meta_category,
626 psz_meta_name, psz_meta_value );
628 lua_pop( L, 1 ); /* pop item */
629 /* ... item meta key value key2 */
631 /* ... item meta key value */
633 lua_pop( L, 1 ); /* pop category */
634 /* ... item meta key */
638 lua_pop( L, 1 ); /* pop "meta" */
639 /* ... item -> back to original stack */
642 /*****************************************************************************
644 ****************************************************************************/
646 * Playlist item table should be on top of the stack when this is called
648 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
649 int *pi_options, char ***pppsz_options )
651 lua_getfield( L, -1, "options" );
652 if( lua_istable( L, -1 ) )
655 while( lua_next( L, -2 ) )
657 if( lua_isstring( L, -1 ) )
659 char *psz_option = strdup( lua_tostring( L, -1 ) );
660 msg_Dbg( p_this, "Option: %s", psz_option );
661 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
666 msg_Warn( p_this, "Option should be a string" );
668 lua_pop( L, 1 ); /* pop option */
671 lua_pop( L, 1 ); /* pop "options" */
674 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
675 playlist_t *p_playlist,
676 input_item_t *p_parent, vlc_bool_t b_play )
681 if( lua_istable( L, -1 ) )
685 while( lua_next( L, -2 ) )
687 /* playlist key item */
688 /* <Parse playlist item> */
689 if( lua_istable( L, -1 ) )
691 lua_getfield( L, -1, "path" );
692 /* playlist key item path */
693 if( lua_isstring( L, -1 ) )
695 const char *psz_path = NULL;
696 const char *psz_name = NULL;
697 char **ppsz_options = NULL;
699 mtime_t i_duration = -1;
700 input_item_t *p_input;
702 /* Read path and name */
703 psz_path = lua_tostring( L, -1 );
704 msg_Dbg( p_this, "Path: %s", psz_path );
705 lua_getfield( L, -2, "name" );
706 /* playlist key item path name */
707 if( lua_isstring( L, -1 ) )
709 psz_name = lua_tostring( L, -1 );
710 msg_Dbg( p_this, "Name: %s", psz_name );
714 if( !lua_isnil( L, -1 ) )
715 msg_Warn( p_this, "Playlist item name should be a string." );
720 lua_getfield( L, -3, "duration" );
721 /* playlist key item path name duration */
722 if( lua_isnumber( L, -1 ) )
724 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
726 else if( !lua_isnil( L, -1 ) )
728 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
730 lua_pop( L, 1 ); /* pop "duration" */
732 /* playlist key item path name */
734 /* Read options: item must be on top of stack */
735 lua_pushvalue( L, -3 );
736 /* playlist key item path name item */
737 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
739 /* Create input item */
740 p_input = input_ItemNewExt( p_playlist, psz_path,
742 (const char **)ppsz_options,
744 lua_pop( L, 3 ); /* pop "path name item" */
745 /* playlist key item */
747 /* Read meta data: item must be on top of stack */
748 vlclua_read_meta_data( p_this, L, p_input );
750 /* Read custom meta data: item must be on top of stack*/
751 vlclua_read_custom_meta_data( p_this, L, p_input );
753 /* Append item to playlist */
754 if( p_parent ) /* Add to node */
755 input_ItemAddSubItem( p_parent, p_input );
756 else /* Play or Enqueue (preparse) */
757 /* FIXME: playlist_AddInput() can fail */
758 playlist_AddInput( p_playlist, p_input,
760 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
761 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
762 i_count ++; /* increment counter */
763 vlc_gc_decref( p_input );
764 while( i_options > 0 )
765 free( ppsz_options[--i_options] );
766 free( ppsz_options );
770 lua_pop( L, 1 ); /* pop "path" */
772 "Playlist item's path should be a string" );
774 /* playlist key item */
778 msg_Warn( p_this, "Playlist item should be a table" );
780 /* <Parse playlist item> */
781 lua_pop( L, 1 ); /* pop the value, keep the key for
782 * the next lua_next() call */
789 msg_Warn( p_this, "Playlist should be a table." );