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 *****************************************************************************/
37 #include <vlc_plugin.h>
39 #include <vlc_charset.h>
42 #include <lua.h> /* Low level lua C API */
43 #include <lauxlib.h> /* Higher level C API */
44 #include <lualib.h> /* Lua libs */
48 /*****************************************************************************
50 *****************************************************************************/
52 #define INTF_TEXT N_("Lua interface")
53 #define INTF_LONGTEXT N_("Lua interface module to load")
55 #define CONFIG_TEXT N_("Lua inteface configuration")
56 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
60 set_shortname( N_( "Lua Art" ) );
61 set_description( N_("Fetch artwork using lua scripts") );
62 set_capability( "art finder", 10 );
63 set_callbacks( FindArt, NULL );
65 add_shortcut( "luaplaylist" );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
68 set_shortname( N_("Lua Playlist") );
69 set_description( N_("Lua Playlist Parser Interface") );
70 set_capability( "demux", 2 );
71 set_callbacks( Import_LuaPlaylist, 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 add_shortcut( "luahttp" );
81 /* add_shortcut( "http" ); */
82 set_description( N_("Lua Interface Module") );
83 set_capability( "interface", 0 );
84 add_string( "lua-intf", "dummy", NULL,
85 INTF_TEXT, INTF_LONGTEXT, false );
86 add_string( "lua-config", "", NULL,
87 CONFIG_TEXT, CONFIG_LONGTEXT, false );
88 set_callbacks( Open_LuaIntf, Close_LuaIntf );
91 /*****************************************************************************
92 * Internal lua<->vlc utils
93 *****************************************************************************/
94 vlc_object_t * vlclua_get_this( lua_State *L )
96 vlc_object_t * p_this;
97 lua_getglobal( L, "vlc" );
98 lua_getfield( L, -1, "private" );
99 p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
104 /*****************************************************************************
105 * VLC error code translation
106 *****************************************************************************/
107 int vlclua_push_ret( lua_State *L, int i_error )
109 lua_pushnumber( L, i_error );
110 lua_pushstring( L, vlc_error( i_error ) );
114 /*****************************************************************************
115 * Get the VLC version string
116 *****************************************************************************/
117 int vlclua_version( lua_State *L )
119 lua_pushstring( L, VLC_Version() );
123 /*****************************************************************************
124 * Get the VLC copyright
125 *****************************************************************************/
126 int vlclua_copyright( lua_State *L )
128 lua_pushstring( L, COPYRIGHT_MESSAGE );
132 /*****************************************************************************
133 * Get the VLC license msg/disclaimer
134 *****************************************************************************/
135 int vlclua_license( lua_State *L )
137 lua_pushstring( L, LICENSE_MSG );
141 /*****************************************************************************
143 *****************************************************************************/
144 int vlclua_quit( lua_State *L )
146 vlc_object_t *p_this = vlclua_get_this( L );
147 /* The rc.c code also stops the playlist ... not sure if this is needed
149 vlc_object_kill( p_this->p_libvlc );
153 /*****************************************************************************
154 * Global properties getters
155 *****************************************************************************/
156 int vlclua_datadir( lua_State *L )
158 lua_pushstring( L, config_GetDataDir() );
161 int vlclua_homedir( lua_State *L )
163 lua_pushstring( L, config_GetHomeDir() );
166 int vlclua_configdir( lua_State *L )
168 char *dir = config_GetUserConfDir();
169 lua_pushstring( L, dir );
173 int vlclua_cachedir( lua_State *L )
175 char *dir = config_GetCacheDir();
176 lua_pushstring( L, dir );
180 int vlclua_datadir_list( lua_State *L )
182 const char *psz_dirname = luaL_checkstring( L, 1 );
183 vlc_object_t *p_this = vlclua_get_this( L );
184 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
185 char **ppsz_dir = ppsz_dir_list;
188 if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
191 for( ; *ppsz_dir; ppsz_dir++ )
193 lua_pushstring( L, *ppsz_dir );
194 lua_rawseti( L, -2, i );
200 /*****************************************************************************
202 *****************************************************************************/
203 int vlclua_volume_set( lua_State *L )
205 vlc_object_t *p_this = vlclua_get_this( L );
206 int i_volume = luaL_checkint( L, 1 );
207 /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
208 return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
211 int vlclua_volume_get( lua_State *L )
213 vlc_object_t *p_this = vlclua_get_this( L );
214 audio_volume_t i_volume;
215 if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
216 lua_pushnumber( L, i_volume );
222 int vlclua_volume_up( lua_State *L )
224 audio_volume_t i_volume;
225 aout_VolumeUp( vlclua_get_this( L ),
226 luaL_optint( L, 1, 1 ),
228 lua_pushnumber( L, i_volume );
232 int vlclua_volume_down( lua_State *L )
234 audio_volume_t i_volume;
235 aout_VolumeDown( vlclua_get_this( L ),
236 luaL_optint( L, 1, 1 ),
238 lua_pushnumber( L, i_volume );
242 /*****************************************************************************
244 *****************************************************************************/
245 int vlclua_stream_new( lua_State *L )
247 vlc_object_t * p_this = vlclua_get_this( L );
249 const char * psz_url;
250 psz_url = luaL_checkstring( L, -1 );
251 p_stream = stream_UrlNew( p_this, psz_url );
253 return luaL_error( L, "Error when opening url: `%s'", psz_url );
254 lua_pushlightuserdata( L, p_stream );
258 int vlclua_stream_read( lua_State *L )
264 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
265 n = luaL_checkint( L, 2 );
266 p_read = malloc( n );
267 if( !p_read ) return vlclua_error( L );
268 i_read = stream_Read( p_stream, p_read, n );
269 lua_pushlstring( L, (const char *)p_read, i_read );
274 int vlclua_stream_readline( lua_State *L )
277 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
278 char *psz_line = stream_ReadLine( p_stream );
281 lua_pushstring( L, psz_line );
289 int vlclua_stream_delete( lua_State *L )
292 p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
293 stream_Delete( p_stream );
297 /*****************************************************************************
298 * String transformations
299 *****************************************************************************/
300 int vlclua_decode_uri( lua_State *L )
302 int i_top = lua_gettop( L );
304 for( i = 1; i <= i_top; i++ )
306 const char *psz_cstring = luaL_checkstring( L, 1 );
307 char *psz_string = strdup( psz_cstring );
308 lua_remove( L, 1 ); /* remove elements to prevent being limited by
309 * the stack's size (this function will work with
310 * up to (stack size - 1) arguments */
311 decode_URI( psz_string );
312 lua_pushstring( L, psz_string );
318 int vlclua_resolve_xml_special_chars( lua_State *L )
320 int i_top = lua_gettop( L );
322 for( i = 1; i <= i_top; i++ )
324 const char *psz_cstring = luaL_checkstring( L, 1 );
325 char *psz_string = strdup( psz_cstring );
326 lua_remove( L, 1 ); /* remove elements to prevent being limited by
327 * the stack's size (this function will work with
328 * up to (stack size - 1) arguments */
329 resolve_xml_special_chars( psz_string );
330 lua_pushstring( L, psz_string );
336 int vlclua_convert_xml_special_chars( lua_State *L )
338 int i_top = lua_gettop( L );
340 for( i = 1; i <= i_top; i++ )
342 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
344 lua_pushstring( L, psz_string );
350 /*****************************************************************************
351 * Messaging facilities
352 *****************************************************************************/
353 int vlclua_msg_dbg( lua_State *L )
355 int i_top = lua_gettop( L );
356 vlc_object_t *p_this = vlclua_get_this( L );
358 for( i = 1; i <= i_top; i++ )
359 msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
362 int vlclua_msg_warn( lua_State *L )
364 int i_top = lua_gettop( L );
365 vlc_object_t *p_this = vlclua_get_this( L );
367 for( i = 1; i <= i_top; i++ )
368 msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
371 int vlclua_msg_err( lua_State *L )
373 int i_top = lua_gettop( L );
374 vlc_object_t *p_this = vlclua_get_this( L );
376 for( i = 1; i <= i_top; i++ )
377 msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
380 int vlclua_msg_info( lua_State *L )
382 int i_top = lua_gettop( L );
383 vlc_object_t *p_this = vlclua_get_this( L );
385 for( i = 1; i <= i_top; i++ )
386 msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
390 /*****************************************************************************
392 *****************************************************************************/
393 static int file_select( const char *file )
395 int i = strlen( file );
396 return i > 4 && !strcmp( file+i-4, ".lua" );
399 static int file_compare( const char **a, const char **b )
401 return strcmp( *a, *b );
404 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
405 char **ppsz_dir_list )
408 char *datadir = config_GetUserDataDir();
409 if( datadir == NULL )
412 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
413 datadir, luadirname ) < 0 )
421 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
423 const char *psz_vlcpath = config_GetDataDir();
424 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
425 psz_vlcpath, luadirname ) < 0 )
428 if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
429 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;
469 char **ppsz_filelist = NULL;
470 char **ppsz_fileend = NULL;
473 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
476 i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
477 if( i_ret != VLC_SUCCESS )
479 i_ret = VLC_EGENERIC;
482 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
488 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
491 free( ppsz_filelist );
492 ppsz_filelist = NULL;
495 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
496 i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
498 if( i_files < 1 ) continue;
499 ppsz_fileend = ppsz_filelist + i_files;
501 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
504 if( asprintf( &psz_filename,
505 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
507 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
509 i_ret = func( p_this, psz_filename, L, user_data );
511 free( psz_filename );
513 if( i_ret == VLC_SUCCESS ) break;
515 if( i_ret == VLC_SUCCESS ) break;
520 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
523 free( ppsz_filelist );
525 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
532 /*****************************************************************************
533 * Meta data setters utility.
534 * Playlist item table should be on top of the stack when these are called
535 *****************************************************************************/
536 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
537 input_item_t *p_input )
539 #define TRY_META( a, b ) \
540 lua_getfield( L, -1, a ); \
541 if( lua_isstring( L, -1 ) ) \
543 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
544 EnsureUTF8( psz_value ); \
545 msg_Dbg( p_this, #b ": %s", psz_value ); \
546 input_item_Set ## b ( p_input, psz_value ); \
549 lua_pop( L, 1 ); /* pop a */
550 TRY_META( "title", Title );
551 TRY_META( "artist", Artist );
552 TRY_META( "genre", Genre );
553 TRY_META( "copyright", Copyright );
554 TRY_META( "album", Album );
555 TRY_META( "tracknum", TrackNum );
556 TRY_META( "description", Description );
557 TRY_META( "rating", Rating );
558 TRY_META( "date", Date );
559 TRY_META( "setting", Setting );
560 TRY_META( "url", URL );
561 TRY_META( "language", Language );
562 TRY_META( "nowplaying", NowPlaying );
563 TRY_META( "publisher", Publisher );
564 TRY_META( "encodedby", EncodedBy );
565 TRY_META( "arturl", ArtURL );
566 TRY_META( "trackid", TrackID );
569 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
570 input_item_t *p_input )
573 lua_getfield( L, -1, "meta" );
575 if( lua_istable( L, -1 ) )
578 /* ... item meta nil */
579 while( lua_next( L, -2 ) )
581 /* ... item meta key value */
582 if( !lua_isstring( L, -2 ) )
584 msg_Warn( p_this, "Custom meta data category name must be "
587 else if( !lua_istable( L, -1 ) )
589 msg_Warn( p_this, "Custom meta data category contents "
594 const char *psz_meta_category = lua_tostring( L, -2 );
595 msg_Dbg( p_this, "Found custom meta data category: %s",
598 /* ... item meta key value nil */
599 while( lua_next( L, -2 ) )
601 /* ... item meta key value key2 value2 */
602 if( !lua_isstring( L, -2 ) )
604 msg_Warn( p_this, "Custom meta category item name "
605 "must be a string." );
607 else if( !lua_isstring( L, -1 ) )
609 msg_Warn( p_this, "Custom meta category item value "
610 "must be a string." );
614 const char *psz_meta_name =
615 lua_tostring( L, -2 );
616 const char *psz_meta_value =
617 lua_tostring( L, -1 );
618 msg_Dbg( p_this, "Custom meta %s, %s: %s",
619 psz_meta_category, psz_meta_name,
621 input_ItemAddInfo( p_input, psz_meta_category,
622 psz_meta_name, psz_meta_value );
624 lua_pop( L, 1 ); /* pop item */
625 /* ... item meta key value key2 */
627 /* ... item meta key value */
629 lua_pop( L, 1 ); /* pop category */
630 /* ... item meta key */
634 lua_pop( L, 1 ); /* pop "meta" */
635 /* ... item -> back to original stack */
638 /*****************************************************************************
640 ****************************************************************************/
642 * Playlist item table should be on top of the stack when this is called
644 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
645 int *pi_options, char ***pppsz_options )
647 lua_getfield( L, -1, "options" );
648 if( lua_istable( L, -1 ) )
651 while( lua_next( L, -2 ) )
653 if( lua_isstring( L, -1 ) )
655 char *psz_option = strdup( lua_tostring( L, -1 ) );
656 msg_Dbg( p_this, "Option: %s", psz_option );
657 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
662 msg_Warn( p_this, "Option should be a string" );
664 lua_pop( L, 1 ); /* pop option */
667 lua_pop( L, 1 ); /* pop "options" */
670 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
671 playlist_t *p_playlist,
672 input_item_t *p_parent, bool b_play )
677 if( lua_istable( L, -1 ) )
681 while( lua_next( L, -2 ) )
683 /* playlist key item */
684 /* <Parse playlist item> */
685 if( lua_istable( L, -1 ) )
687 lua_getfield( L, -1, "path" );
688 /* playlist key item path */
689 if( lua_isstring( L, -1 ) )
691 const char *psz_path = NULL;
692 const char *psz_name = NULL;
693 char **ppsz_options = NULL;
695 mtime_t i_duration = -1;
696 input_item_t *p_input;
698 /* Read path and name */
699 psz_path = lua_tostring( L, -1 );
700 msg_Dbg( p_this, "Path: %s", psz_path );
701 lua_getfield( L, -2, "name" );
702 /* playlist key item path name */
703 if( lua_isstring( L, -1 ) )
705 psz_name = lua_tostring( L, -1 );
706 msg_Dbg( p_this, "Name: %s", psz_name );
710 if( !lua_isnil( L, -1 ) )
711 msg_Warn( p_this, "Playlist item name should be a string." );
716 lua_getfield( L, -3, "duration" );
717 /* playlist key item path name duration */
718 if( lua_isnumber( L, -1 ) )
720 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
722 else if( !lua_isnil( L, -1 ) )
724 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
726 lua_pop( L, 1 ); /* pop "duration" */
728 /* playlist key item path name */
730 /* Read options: item must be on top of stack */
731 lua_pushvalue( L, -3 );
732 /* playlist key item path name item */
733 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
735 /* Create input item */
736 p_input = input_ItemNewExt( p_playlist, psz_path,
738 (const char **)ppsz_options,
740 lua_pop( L, 3 ); /* pop "path name item" */
741 /* playlist key item */
743 /* Read meta data: item must be on top of stack */
744 vlclua_read_meta_data( p_this, L, p_input );
746 /* Read custom meta data: item must be on top of stack*/
747 vlclua_read_custom_meta_data( p_this, L, p_input );
749 /* Append item to playlist */
750 if( p_parent ) /* Add to node */
751 input_ItemAddSubItem( p_parent, p_input );
752 else /* Play or Enqueue (preparse) */
753 /* FIXME: playlist_AddInput() can fail */
754 playlist_AddInput( p_playlist, p_input,
756 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
757 PLAYLIST_END, true, false );
758 i_count ++; /* increment counter */
759 vlc_gc_decref( p_input );
760 while( i_options > 0 )
761 free( ppsz_options[--i_options] );
762 free( ppsz_options );
766 lua_pop( L, 1 ); /* pop "path" */
768 "Playlist item's path should be a string" );
770 /* playlist key item */
774 msg_Warn( p_this, "Playlist item should be a table" );
776 /* <Parse playlist item> */
777 lua_pop( L, 1 ); /* pop the value, keep the key for
778 * the next lua_next() call */
785 msg_Warn( p_this, "Playlist should be a table." );