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 set_shortname( N_( "Lua Art" ) );
60 set_description( _("Fetch artwork using lua scripts") );
61 set_capability( "art finder", 10 );
62 set_callbacks( E_(FindArt), NULL );
64 add_shortcut( "luaplaylist" );
65 set_category( CAT_INPUT );
66 set_subcategory( SUBCAT_INPUT_DEMUX );
67 set_shortname( _("Lua Playlist") );
68 set_description( _("Lua Playlist Parser Interface") );
69 set_capability( "demux", 2 );
70 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
72 add_shortcut( "luaintf" );
73 add_shortcut( "luarc" );
74 /* add_shortcut( "rc" ); */
75 add_shortcut( "luahotkeys" );
76 /* add_shortcut( "hotkeys" ); */
77 add_shortcut( "luatelnet" );
78 /* add_shortcut( "telnet" ); */
79 add_shortcut( "luahttp" );
80 /* add_shortcut( "http" ); */
81 set_description( _("Lua Interface Module") );
82 set_capability( "interface", 0 );
83 add_string( "lua-intf", "dummy", NULL,
84 INTF_TEXT, INTF_LONGTEXT, false );
85 add_string( "lua-config", "", NULL,
86 CONFIG_TEXT, CONFIG_LONGTEXT, false );
87 set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
90 /*****************************************************************************
91 * Internal lua<->vlc utils
92 *****************************************************************************/
93 vlc_object_t * vlclua_get_this( lua_State *L )
95 vlc_object_t * p_this;
96 lua_getglobal( L, "vlc" );
97 lua_getfield( L, -1, "private" );
98 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
103 /*****************************************************************************
104 * VLC error code translation
105 *****************************************************************************/
106 int vlclua_push_ret( lua_State *L, int i_error )
108 lua_pushnumber( L, i_error );
109 lua_pushstring( L, vlc_error( i_error ) );
113 /*****************************************************************************
114 * Get the VLC version string
115 *****************************************************************************/
116 int vlclua_version( lua_State *L )
118 lua_pushstring( L, VLC_Version() );
122 /*****************************************************************************
123 * Get the VLC copyright
124 *****************************************************************************/
125 int vlclua_copyright( lua_State *L )
127 lua_pushstring( L, COPYRIGHT_MESSAGE );
131 /*****************************************************************************
132 * Get the VLC license msg/disclaimer
133 *****************************************************************************/
134 int vlclua_license( lua_State *L )
136 lua_pushstring( L, LICENSE_MSG );
140 /*****************************************************************************
142 *****************************************************************************/
143 int vlclua_quit( lua_State *L )
145 vlc_object_t *p_this = vlclua_get_this( L );
146 /* The rc.c code also stops the playlist ... not sure if this is needed
148 vlc_object_kill( p_this->p_libvlc );
152 /*****************************************************************************
153 * Global properties getters
154 *****************************************************************************/
155 int vlclua_datadir( lua_State *L )
157 lua_pushstring( L, config_GetDataDir() );
160 int vlclua_homedir( lua_State *L )
162 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
165 int vlclua_configdir( lua_State *L )
167 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
170 int vlclua_cachedir( lua_State *L )
172 lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
175 int vlclua_datadir_list( lua_State *L )
177 const char *psz_dirname = luaL_checkstring( L, 1 );
178 vlc_object_t *p_this = vlclua_get_this( L );
179 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
180 char **ppsz_dir = ppsz_dir_list;
183 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
186 for( ; *ppsz_dir; ppsz_dir++ )
188 lua_pushstring( L, *ppsz_dir );
189 lua_rawseti( L, -2, i );
195 /*****************************************************************************
197 *****************************************************************************/
198 int vlclua_volume_set( lua_State *L )
200 vlc_object_t *p_this = vlclua_get_this( L );
201 int i_volume = luaL_checkint( L, 1 );
202 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
203 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
206 int vlclua_volume_get( lua_State *L )
208 vlc_object_t *p_this = vlclua_get_this( L );
209 audio_volume_t i_volume;
210 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
211 lua_pushnumber( L, i_volume );
217 int vlclua_volume_up( lua_State *L )
219 audio_volume_t i_volume;
220 aout_VolumeUp( vlclua_get_this( L ),
221 luaL_optint( L, 1, 1 ),
223 lua_pushnumber( L, i_volume );
227 int vlclua_volume_down( lua_State *L )
229 audio_volume_t i_volume;
230 aout_VolumeDown( vlclua_get_this( L ),
231 luaL_optint( L, 1, 1 ),
233 lua_pushnumber( L, i_volume );
237 /*****************************************************************************
239 *****************************************************************************/
240 int vlclua_stream_new( lua_State *L )
242 vlc_object_t * p_this = vlclua_get_this( L );
244 const char * psz_url;
245 psz_url = luaL_checkstring( L, -1 );
246 p_stream = stream_UrlNew( p_this, psz_url );
248 return luaL_error( L, "Error when opening url: `%s'", psz_url );
249 lua_pushlightuserdata( L, p_stream );
253 int vlclua_stream_read( lua_State *L )
259 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
260 n = luaL_checkint( L, 2 );
261 p_read = malloc( n );
262 if( !p_read ) return vlclua_error( L );
263 i_read = stream_Read( p_stream, p_read, n );
264 lua_pushlstring( L, (const char *)p_read, i_read );
269 int vlclua_stream_readline( lua_State *L )
272 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
273 char *psz_line = stream_ReadLine( p_stream );
276 lua_pushstring( L, psz_line );
284 int vlclua_stream_delete( lua_State *L )
287 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
288 stream_Delete( p_stream );
292 /*****************************************************************************
293 * String transformations
294 *****************************************************************************/
295 int vlclua_decode_uri( lua_State *L )
297 int i_top = lua_gettop( L );
299 for( i = 1; i <= i_top; i++ )
301 const char *psz_cstring = luaL_checkstring( L, 1 );
302 char *psz_string = strdup( psz_cstring );
303 lua_remove( L, 1 ); /* remove elements to prevent being limited by
304 * the stack's size (this function will work with
305 * up to (stack size - 1) arguments */
306 decode_URI( psz_string );
307 lua_pushstring( L, psz_string );
313 int vlclua_resolve_xml_special_chars( lua_State *L )
315 int i_top = lua_gettop( L );
317 for( i = 1; i <= i_top; i++ )
319 const char *psz_cstring = luaL_checkstring( L, 1 );
320 char *psz_string = strdup( psz_cstring );
321 lua_remove( L, 1 ); /* remove elements to prevent being limited by
322 * the stack's size (this function will work with
323 * up to (stack size - 1) arguments */
324 resolve_xml_special_chars( psz_string );
325 lua_pushstring( L, psz_string );
331 int vlclua_convert_xml_special_chars( lua_State *L )
333 int i_top = lua_gettop( L );
335 for( i = 1; i <= i_top; i++ )
337 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
339 lua_pushstring( L, psz_string );
345 /*****************************************************************************
346 * Messaging facilities
347 *****************************************************************************/
348 int vlclua_msg_dbg( lua_State *L )
350 int i_top = lua_gettop( L );
351 vlc_object_t *p_this = vlclua_get_this( L );
353 for( i = 1; i <= i_top; i++ )
354 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
357 int vlclua_msg_warn( lua_State *L )
359 int i_top = lua_gettop( L );
360 vlc_object_t *p_this = vlclua_get_this( L );
362 for( i = 1; i <= i_top; i++ )
363 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
366 int vlclua_msg_err( lua_State *L )
368 int i_top = lua_gettop( L );
369 vlc_object_t *p_this = vlclua_get_this( L );
371 for( i = 1; i <= i_top; i++ )
372 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
375 int vlclua_msg_info( lua_State *L )
377 int i_top = lua_gettop( L );
378 vlc_object_t *p_this = vlclua_get_this( L );
380 for( i = 1; i <= i_top; i++ )
381 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
385 /*****************************************************************************
387 *****************************************************************************/
388 static int file_select( const char *file )
390 int i = strlen( file );
391 return i > 4 && !strcmp( file+i-4, ".lua" );
394 static int file_compare( const char **a, const char **b )
396 return strcmp( *a, *b );
399 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
400 char **ppsz_dir_list )
403 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
404 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
408 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
410 const char *psz_vlcpath = config_GetDataDir();
411 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
412 psz_vlcpath, luadirname ) < 0 )
416 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
417 psz_vlcpath, luadirname ) < 0 )
421 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
422 psz_vlcpath, luadirname ) < 0 )
429 if( asprintf( &ppsz_dir_list[i],
430 "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
433 # ifdef HAVE_SYS_STAT_H
435 struct stat stat_info;
436 if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
437 || !S_ISDIR( stat_info.st_mode ) )
439 free(ppsz_dir_list[i]);
440 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
441 config_GetDataDir (), luadirname ) < 0 )
451 /*****************************************************************************
452 * Will execute func on all scripts in luadirname, and stop if func returns
454 *****************************************************************************/
455 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
456 const char * luadirname,
457 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
461 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;
489 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
490 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
492 if( i_files < 1 ) continue;
493 ppsz_fileend = ppsz_filelist + i_files;
495 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
498 if( asprintf( &psz_filename,
499 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
501 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
503 i_ret = func( p_this, psz_filename, L, user_data );
505 free( psz_filename );
507 if( i_ret == VLC_SUCCESS ) break;
509 if( i_ret == VLC_SUCCESS ) break;
514 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
517 free( ppsz_filelist );
519 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
526 /*****************************************************************************
527 * Meta data setters utility.
528 * Playlist item table should be on top of the stack when these are called
529 *****************************************************************************/
530 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
531 input_item_t *p_input )
533 #define TRY_META( a, b ) \
534 lua_getfield( L, -1, a ); \
535 if( lua_isstring( L, -1 ) ) \
537 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
538 EnsureUTF8( psz_value ); \
539 msg_Dbg( p_this, #b ": %s", psz_value ); \
540 input_item_Set ## b ( p_input, psz_value ); \
543 lua_pop( L, 1 ); /* pop a */
544 TRY_META( "title", Title );
545 TRY_META( "artist", Artist );
546 TRY_META( "genre", Genre );
547 TRY_META( "copyright", Copyright );
548 TRY_META( "album", Album );
549 TRY_META( "tracknum", TrackNum );
550 TRY_META( "description", Description );
551 TRY_META( "rating", Rating );
552 TRY_META( "date", Date );
553 TRY_META( "setting", Setting );
554 TRY_META( "url", URL );
555 TRY_META( "language", Language );
556 TRY_META( "nowplaying", NowPlaying );
557 TRY_META( "publisher", Publisher );
558 TRY_META( "encodedby", EncodedBy );
559 TRY_META( "arturl", ArtURL );
560 TRY_META( "trackid", TrackID );
563 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
564 input_item_t *p_input )
567 lua_getfield( L, -1, "meta" );
569 if( lua_istable( L, -1 ) )
572 /* ... item meta nil */
573 while( lua_next( L, -2 ) )
575 /* ... item meta key value */
576 if( !lua_isstring( L, -2 ) )
578 msg_Warn( p_this, "Custom meta data category name must be "
581 else if( !lua_istable( L, -1 ) )
583 msg_Warn( p_this, "Custom meta data category contents "
588 const char *psz_meta_category = lua_tostring( L, -2 );
589 msg_Dbg( p_this, "Found custom meta data category: %s",
592 /* ... item meta key value nil */
593 while( lua_next( L, -2 ) )
595 /* ... item meta key value key2 value2 */
596 if( !lua_isstring( L, -2 ) )
598 msg_Warn( p_this, "Custom meta category item name "
599 "must be a string." );
601 else if( !lua_isstring( L, -1 ) )
603 msg_Warn( p_this, "Custom meta category item value "
604 "must be a string." );
608 const char *psz_meta_name =
609 lua_tostring( L, -2 );
610 const char *psz_meta_value =
611 lua_tostring( L, -1 );
612 msg_Dbg( p_this, "Custom meta %s, %s: %s",
613 psz_meta_category, psz_meta_name,
615 input_ItemAddInfo( p_input, psz_meta_category,
616 psz_meta_name, psz_meta_value );
618 lua_pop( L, 1 ); /* pop item */
619 /* ... item meta key value key2 */
621 /* ... item meta key value */
623 lua_pop( L, 1 ); /* pop category */
624 /* ... item meta key */
628 lua_pop( L, 1 ); /* pop "meta" */
629 /* ... item -> back to original stack */
632 /*****************************************************************************
634 ****************************************************************************/
636 * Playlist item table should be on top of the stack when this is called
638 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
639 int *pi_options, char ***pppsz_options )
641 lua_getfield( L, -1, "options" );
642 if( lua_istable( L, -1 ) )
645 while( lua_next( L, -2 ) )
647 if( lua_isstring( L, -1 ) )
649 char *psz_option = strdup( lua_tostring( L, -1 ) );
650 msg_Dbg( p_this, "Option: %s", psz_option );
651 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
656 msg_Warn( p_this, "Option should be a string" );
658 lua_pop( L, 1 ); /* pop option */
661 lua_pop( L, 1 ); /* pop "options" */
664 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
665 playlist_t *p_playlist,
666 input_item_t *p_parent, bool b_play )
671 if( lua_istable( L, -1 ) )
675 while( lua_next( L, -2 ) )
677 /* playlist key item */
678 /* <Parse playlist item> */
679 if( lua_istable( L, -1 ) )
681 lua_getfield( L, -1, "path" );
682 /* playlist key item path */
683 if( lua_isstring( L, -1 ) )
685 const char *psz_path = NULL;
686 const char *psz_name = NULL;
687 char **ppsz_options = NULL;
689 mtime_t i_duration = -1;
690 input_item_t *p_input;
692 /* Read path and name */
693 psz_path = lua_tostring( L, -1 );
694 msg_Dbg( p_this, "Path: %s", psz_path );
695 lua_getfield( L, -2, "name" );
696 /* playlist key item path name */
697 if( lua_isstring( L, -1 ) )
699 psz_name = lua_tostring( L, -1 );
700 msg_Dbg( p_this, "Name: %s", psz_name );
704 if( !lua_isnil( L, -1 ) )
705 msg_Warn( p_this, "Playlist item name should be a string." );
710 lua_getfield( L, -3, "duration" );
711 /* playlist key item path name duration */
712 if( lua_isnumber( L, -1 ) )
714 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
716 else if( !lua_isnil( L, -1 ) )
718 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
720 lua_pop( L, 1 ); /* pop "duration" */
722 /* playlist key item path name */
724 /* Read options: item must be on top of stack */
725 lua_pushvalue( L, -3 );
726 /* playlist key item path name item */
727 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
729 /* Create input item */
730 p_input = input_ItemNewExt( p_playlist, psz_path,
732 (const char **)ppsz_options,
734 lua_pop( L, 3 ); /* pop "path name item" */
735 /* playlist key item */
737 /* Read meta data: item must be on top of stack */
738 vlclua_read_meta_data( p_this, L, p_input );
740 /* Read custom meta data: item must be on top of stack*/
741 vlclua_read_custom_meta_data( p_this, L, p_input );
743 /* Append item to playlist */
744 if( p_parent ) /* Add to node */
745 input_ItemAddSubItem( p_parent, p_input );
746 else /* Play or Enqueue (preparse) */
747 /* FIXME: playlist_AddInput() can fail */
748 playlist_AddInput( p_playlist, p_input,
750 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
751 PLAYLIST_END, true, false );
752 i_count ++; /* increment counter */
753 vlc_gc_decref( p_input );
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." );