1 /*****************************************************************************
2 * vlc.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 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_common.h>
39 #include <vlc_plugin.h>
41 #include <vlc_charset.h>
43 #include <vlc_services_discovery.h>
45 #include <lua.h> /* Low level lua C API */
46 #include <lauxlib.h> /* Higher level C API */
47 #include <lualib.h> /* Lua libs */
51 /*****************************************************************************
53 *****************************************************************************/
54 #define INTF_TEXT N_("Lua interface")
55 #define INTF_LONGTEXT N_("Lua interface module to load")
57 #define CONFIG_TEXT N_("Lua interface configuration")
58 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
60 static int vlc_sd_probe_Open( vlc_object_t
* );
63 set_shortname( N_( "Lua Art" ) )
64 set_description( N_("Fetch artwork using lua scripts") )
65 set_capability( "art finder", 10 )
66 set_callbacks( FindArt
, NULL
)
69 set_shortname( N_( "Lua Meta Fetcher" ) )
70 set_description( N_("Fetch meta data using lua scripts") )
71 set_capability( "meta fetcher", 10 )
72 set_callbacks( FetchMeta
, NULL
)
75 set_shortname( N_( "Lua Meta Reader" ) )
76 set_description( N_("Read meta data using lua scripts") )
77 set_capability( "meta reader", 10 )
78 set_callbacks( ReadMeta
, NULL
)
81 add_shortcut( "luaplaylist" )
82 set_category( CAT_INPUT
)
83 set_subcategory( SUBCAT_INPUT_DEMUX
)
84 set_shortname( N_("Lua Playlist") )
85 set_description( N_("Lua Playlist Parser Interface") )
86 set_capability( "demux", 2 )
87 set_callbacks( Import_LuaPlaylist
, Close_LuaPlaylist
)
90 set_description( N_("Lua Interface Module (shortcuts)") )
91 add_shortcut( "luarc" )
93 set_capability( "interface", 25 )
94 set_callbacks( Open_LuaIntf
, Close_LuaIntf
)
97 set_description( N_("Lua Interface Module") )
98 add_shortcut( "luaintf" )
99 add_shortcut( "luahttp" )
100 add_shortcut( "http" )
101 add_shortcut( "luatelnet" )
102 add_shortcut( "telnet" )
103 add_shortcut( "luahotkeys" )
104 /* add_shortcut( "hotkeys" ) */
105 set_capability( "interface", 0 )
106 add_string( "lua-intf", "dummy", NULL
,
107 INTF_TEXT
, INTF_LONGTEXT
, false )
108 add_string( "lua-config", "", NULL
,
109 CONFIG_TEXT
, CONFIG_LONGTEXT
, false )
110 set_callbacks( Open_LuaIntf
, Close_LuaIntf
)
113 set_shortname( N_("Lua Extension") )
114 add_shortcut( "luaextension" )
115 set_capability( "extension", 1 )
116 set_callbacks( Open_Extension
, Close_Extension
)
119 set_description( N_("Lua SD Module") )
120 add_shortcut( "luasd" )
121 set_capability( "services_discovery", 0 )
122 add_string( "lua-sd", "", NULL
, "", "", false )
123 set_callbacks( Open_LuaSD
, Close_LuaSD
)
125 VLC_SD_PROBE_SUBMODULE
129 /*****************************************************************************
131 *****************************************************************************/
132 static int file_select( const char *file
)
134 int i
= strlen( file
);
135 return i
> 4 && !strcmp( file
+i
-4, ".lua" );
138 static int file_compare( const char **a
, const char **b
)
140 return strcmp( *a
, *b
);
143 int vlclua_dir_list( vlc_object_t
*p_this
, const char *luadirname
, char **ppsz_dir_list
)
146 char *datadir
= config_GetUserDir( VLC_DATA_DIR
);
147 if( datadir
== NULL
)
150 if( asprintf( &ppsz_dir_list
[i
], "%s" DIR_SEP
"lua" DIR_SEP
"%s",
151 datadir
, luadirname
) < 0 )
159 char *psz_datapath
= config_GetDataDir( p_this
);
160 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
162 if( asprintf( &ppsz_dir_list
[i
], "%s" DIR_SEP
"lua" DIR_SEP
"%s",
163 psz_datapath
, luadirname
) < 0 )
166 if( asprintf( &ppsz_dir_list
[i
], "%s" DIR_SEP
"share" DIR_SEP
"lua" DIR_SEP
"%s",
167 psz_datapath
, luadirname
) < 0 )
173 if( asprintf( &ppsz_dir_list
[i
], "%s" DIR_SEP
"lua" DIR_SEP
"%s",
174 psz_datapath
, luadirname
) < 0 )
178 free( psz_datapath
);
182 void vlclua_dir_list_free( char **ppsz_dir_list
)
185 for( ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
189 /*****************************************************************************
190 * Will execute func on all scripts in luadirname, and stop if func returns
192 *****************************************************************************/
193 int vlclua_scripts_batch_execute( vlc_object_t
*p_this
,
194 const char * luadirname
,
195 int (*func
)(vlc_object_t
*, const char *, lua_State
*, void *),
199 char *ppsz_dir_list
[] = { NULL
, NULL
, NULL
, NULL
};
201 int i_ret
= vlclua_dir_list( p_this
, luadirname
, ppsz_dir_list
);
202 if( i_ret
!= VLC_SUCCESS
)
204 i_ret
= VLC_EGENERIC
;
206 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
208 char **ppsz_filelist
;
211 msg_Dbg( p_this
, "Trying Lua scripts in %s", *ppsz_dir
);
212 i_files
= utf8_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
217 char **ppsz_file
= ppsz_filelist
;
218 char **ppsz_fileend
= ppsz_filelist
+ i_files
;
220 while( ppsz_file
< ppsz_fileend
)
224 if( asprintf( &psz_filename
,
225 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) == -1 )
227 free( *(ppsz_file
++) );
229 if( likely(psz_filename
!= NULL
) )
231 msg_Dbg( p_this
, "Trying Lua playlist script %s",
233 i_ret
= func( p_this
, psz_filename
, L
, user_data
);
234 free( psz_filename
);
235 if( i_ret
== VLC_SUCCESS
)
240 while( ppsz_file
< ppsz_fileend
)
241 free( *(ppsz_file
++) );
242 free( ppsz_filelist
);
244 if( i_ret
== VLC_SUCCESS
)
247 vlclua_dir_list_free( ppsz_dir_list
);
252 /*****************************************************************************
253 * Meta data setters utility.
254 * Playlist item table should be on top of the stack when these are called
255 *****************************************************************************/
256 void __vlclua_read_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
257 input_item_t
*p_input
)
259 #define TRY_META( a, b ) \
260 lua_getfield( L, -1, a ); \
261 if( lua_isstring( L, -1 ) ) \
263 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
264 EnsureUTF8( psz_value ); \
265 msg_Dbg( p_this, #b ": %s", psz_value ); \
266 input_item_Set ## b ( p_input, psz_value ); \
269 lua_pop( L, 1 ); /* pop a */
270 TRY_META( "title", Title
);
271 TRY_META( "artist", Artist
);
272 TRY_META( "genre", Genre
);
273 TRY_META( "copyright", Copyright
);
274 TRY_META( "album", Album
);
275 TRY_META( "tracknum", TrackNum
);
276 TRY_META( "description", Description
);
277 TRY_META( "rating", Rating
);
278 TRY_META( "date", Date
);
279 TRY_META( "setting", Setting
);
280 TRY_META( "url", URL
);
281 TRY_META( "language", Language
);
282 TRY_META( "nowplaying", NowPlaying
);
283 TRY_META( "publisher", Publisher
);
284 TRY_META( "encodedby", EncodedBy
);
285 TRY_META( "arturl", ArtURL
);
286 TRY_META( "trackid", TrackID
);
289 void __vlclua_read_custom_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
290 input_item_t
*p_input
)
293 lua_getfield( L
, -1, "meta" );
295 if( lua_istable( L
, -1 ) )
298 /* ... item meta nil */
299 while( lua_next( L
, -2 ) )
301 /* ... item meta key value */
302 if( !lua_isstring( L
, -2 ) )
304 msg_Warn( p_this
, "Custom meta data category name must be "
307 else if( !lua_istable( L
, -1 ) )
309 msg_Warn( p_this
, "Custom meta data category contents "
314 const char *psz_meta_category
= lua_tostring( L
, -2 );
315 msg_Dbg( p_this
, "Found custom meta data category: %s",
318 /* ... item meta key value nil */
319 while( lua_next( L
, -2 ) )
321 /* ... item meta key value key2 value2 */
322 if( !lua_isstring( L
, -2 ) )
324 msg_Warn( p_this
, "Custom meta category item name "
325 "must be a string." );
327 else if( !lua_isstring( L
, -1 ) )
329 msg_Warn( p_this
, "Custom meta category item value "
330 "must be a string." );
334 const char *psz_meta_name
=
335 lua_tostring( L
, -2 );
336 const char *psz_meta_value
=
337 lua_tostring( L
, -1 );
338 msg_Dbg( p_this
, "Custom meta %s, %s: %s",
339 psz_meta_category
, psz_meta_name
,
341 input_item_AddInfo( p_input
, psz_meta_category
,
342 psz_meta_name
, "%s", psz_meta_value
);
344 lua_pop( L
, 1 ); /* pop item */
345 /* ... item meta key value key2 */
347 /* ... item meta key value */
349 lua_pop( L
, 1 ); /* pop category */
350 /* ... item meta key */
354 lua_pop( L
, 1 ); /* pop "meta" */
355 /* ... item -> back to original stack */
358 /*****************************************************************************
360 ****************************************************************************/
362 * Playlist item table should be on top of the stack when this is called
364 void __vlclua_read_options( vlc_object_t
*p_this
, lua_State
*L
,
365 int *pi_options
, char ***pppsz_options
)
367 lua_getfield( L
, -1, "options" );
368 if( lua_istable( L
, -1 ) )
371 while( lua_next( L
, -2 ) )
373 if( lua_isstring( L
, -1 ) )
375 char *psz_option
= strdup( lua_tostring( L
, -1 ) );
376 msg_Dbg( p_this
, "Option: %s", psz_option
);
377 INSERT_ELEM( *pppsz_options
, *pi_options
, *pi_options
,
382 msg_Warn( p_this
, "Option should be a string" );
384 lua_pop( L
, 1 ); /* pop option */
387 lua_pop( L
, 1 ); /* pop "options" */
390 int __vlclua_playlist_add_internal( vlc_object_t
*p_this
, lua_State
*L
,
391 playlist_t
*p_playlist
,
392 input_item_t
*p_parent
, bool b_play
)
395 input_item_node_t
*p_parent_node
= NULL
;
397 assert( p_parent
|| p_playlist
);
400 if( lua_istable( L
, -1 ) )
402 if( p_parent
) p_parent_node
= input_item_node_Create( p_parent
);
405 while( lua_next( L
, -2 ) )
407 /* playlist key item */
408 /* <Parse playlist item> */
409 if( lua_istable( L
, -1 ) )
411 lua_getfield( L
, -1, "path" );
412 /* playlist key item path */
413 if( lua_isstring( L
, -1 ) )
415 const char *psz_path
= NULL
;
416 const char *psz_name
= NULL
;
417 char **ppsz_options
= NULL
;
419 mtime_t i_duration
= -1;
420 input_item_t
*p_input
;
422 /* Read path and name */
423 psz_path
= lua_tostring( L
, -1 );
424 msg_Dbg( p_this
, "Path: %s", psz_path
);
425 lua_getfield( L
, -2, "name" );
426 /* playlist key item path name */
427 if( lua_isstring( L
, -1 ) )
429 psz_name
= lua_tostring( L
, -1 );
430 msg_Dbg( p_this
, "Name: %s", psz_name
);
434 if( !lua_isnil( L
, -1 ) )
435 msg_Warn( p_this
, "Playlist item name should be a string." );
440 lua_getfield( L
, -3, "duration" );
441 /* playlist key item path name duration */
442 if( lua_isnumber( L
, -1 ) )
444 i_duration
= (mtime_t
)(lua_tonumber( L
, -1 )*1e6
);
446 else if( !lua_isnil( L
, -1 ) )
448 msg_Warn( p_this
, "Playlist item duration should be a number (in seconds)." );
450 lua_pop( L
, 1 ); /* pop "duration" */
452 /* playlist key item path name */
454 /* Read options: item must be on top of stack */
455 lua_pushvalue( L
, -3 );
456 /* playlist key item path name item */
457 vlclua_read_options( p_this
, L
, &i_options
, &ppsz_options
);
459 /* Create input item */
460 p_input
= input_item_NewExt( p_playlist
, psz_path
,
462 (const char **)ppsz_options
,
463 VLC_INPUT_OPTION_TRUSTED
,
465 lua_pop( L
, 3 ); /* pop "path name item" */
466 /* playlist key item */
468 /* Read meta data: item must be on top of stack */
469 vlclua_read_meta_data( p_this
, L
, p_input
);
471 /* Read custom meta data: item must be on top of stack*/
472 vlclua_read_custom_meta_data( p_this
, L
, p_input
);
474 /* Append item to playlist */
475 if( p_parent
) /* Add to node */
477 input_item_node_AppendItem( p_parent_node
, p_input
);
479 else /* Play or Enqueue (preparse) */
480 /* FIXME: playlist_AddInput() can fail */
481 playlist_AddInput( p_playlist
, p_input
,
483 ( b_play
? PLAYLIST_GO
: PLAYLIST_PREPARSE
),
484 PLAYLIST_END
, true, false );
485 i_count
++; /* increment counter */
486 vlc_gc_decref( p_input
);
487 while( i_options
> 0 )
488 free( ppsz_options
[--i_options
] );
489 free( ppsz_options
);
493 lua_pop( L
, 1 ); /* pop "path" */
495 "Playlist item's path should be a string" );
497 /* playlist key item */
501 msg_Warn( p_this
, "Playlist item should be a table" );
503 /* <Parse playlist item> */
504 lua_pop( L
, 1 ); /* pop the value, keep the key for
505 * the next lua_next() call */
511 if( i_count
) input_item_node_PostAndDelete( p_parent_node
);
512 else input_item_node_Delete( p_parent_node
);
517 msg_Warn( p_this
, "Playlist should be a table." );
522 static int vlc_sd_probe_Open( vlc_object_t
*obj
)
524 vlc_probe_t
*probe
= (vlc_probe_t
*)obj
;
525 char **ppsz_filelist
= NULL
;
526 char **ppsz_fileend
= NULL
;
529 char *ppsz_dir_list
[] = { NULL
, NULL
, NULL
, NULL
};
531 vlclua_dir_list( obj
, "sd", ppsz_dir_list
);
532 for( ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
537 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
540 free( ppsz_filelist
);
541 ppsz_filelist
= NULL
;
543 i_files
= utf8_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
545 if( i_files
< 1 ) continue;
546 ppsz_fileend
= ppsz_filelist
+ i_files
;
547 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
; ppsz_file
++ )
550 if( asprintf( &psz_filename
,
551 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) < 0 )
555 FILE *fd
= utf8_fopen( psz_filename
, "r" );
558 char description
[256];
559 if( fgets( description
, 256, fd
) != NULL
)
561 char *temp
= strchr( description
, '\n' );
564 *(*ppsz_file
+ strlen(*ppsz_file
) - 4 )= '\0';
565 if( asprintf( &psz_name
, "lua{sd=%s,longname=%s}",
566 *ppsz_file
, description
+ 17 ) < 0 )
569 free( psz_filename
);
572 vlc_sd_probe_Add( probe
, psz_name
,
578 free( psz_filename
);
583 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
586 free( ppsz_filelist
);
588 vlclua_dir_list_free( ppsz_dir_list
);
589 return VLC_PROBE_CONTINUE
;
593 for( ppsz_file
= ppsz_filelist
; ppsz_file
< ppsz_fileend
;
596 free( ppsz_filelist
);
598 vlclua_dir_list_free( ppsz_dir_list
);