1 /*****************************************************************************
2 * vlclua.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 *****************************************************************************/
35 #include <lua.h> /* Low level lua C API */
36 #include <lauxlib.h> /* Higher level C API */
37 #include <lualib.h> /* Lua libs */
41 /*****************************************************************************
43 *****************************************************************************/
46 add_shortcut( "luameta" );
47 set_shortname( N_( "Lua Meta" ) );
48 set_description( _("Fetch metadata using lua scripts") );
49 set_capability( "meta fetcher", 10 );
50 set_callbacks( E_(FindMeta), NULL );
52 set_shortname( N_( "Lua Art" ) );
53 set_description( _("Fetch artwork using lua scripts") );
54 set_capability( "art finder", 10 );
55 set_callbacks( E_(FindArt), NULL );
57 add_shortcut( "luaplaylist" );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_DEMUX );
60 set_shortname( _("Lua Playlist") );
61 set_description( _("Lua Playlist Parser Interface") );
62 set_capability( "demux2", 9 );
63 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
66 /*****************************************************************************
68 *****************************************************************************/
69 vlc_object_t * vlclua_get_this( lua_State *p_state )
71 vlc_object_t * p_this;
72 lua_getglobal( p_state, "vlc" );
73 lua_getfield( p_state, lua_gettop( p_state ), "private" );
74 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
75 lua_pop( p_state, 2 );
79 int vlclua_stream_new( lua_State *p_state )
81 vlc_object_t * p_this = vlclua_get_this( p_state );
82 int i = lua_gettop( p_state );
86 psz_url = lua_tostring( p_state, 1 );
87 lua_pop( p_state, i );
88 p_stream = stream_UrlNew( p_this, psz_url );
89 if( !p_stream ) return 0;
90 lua_pushlightuserdata( p_state, p_stream );
94 int vlclua_stream_read( lua_State *p_state )
96 int i = lua_gettop( p_state );
102 p_stream = (stream_t *)lua_topointer( p_state, 1 );
103 n = lua_tonumber( p_state, 2 );
104 lua_pop( p_state, i );
105 p_read = malloc( n );
106 if( !p_read ) return 0;
107 i_read = stream_Read( p_stream, p_read, n );
108 lua_pushlstring( p_state, (const char *)p_read, i_read );
113 int vlclua_stream_readline( lua_State *p_state )
115 int i = lua_gettop( p_state );
118 p_stream = (stream_t *)lua_topointer( p_state, 1 );
119 lua_pop( p_state, i );
120 char *psz_line = stream_ReadLine( p_stream );
123 lua_pushstring( p_state, psz_line );
128 lua_pushnil( p_state );
133 int vlclua_stream_delete( lua_State *p_state )
135 int i = lua_gettop( p_state );
138 p_stream = (stream_t *)lua_topointer( p_state, 1 );
139 lua_pop( p_state, i );
140 stream_Delete( p_stream );
144 int vlclua_decode_uri( lua_State *p_state )
146 int i = lua_gettop( p_state );
148 const char *psz_cstring = lua_tostring( p_state, 1 );
149 if( !psz_cstring ) return 0;
150 char *psz_string = strdup( psz_cstring );
151 lua_pop( p_state, i );
152 decode_URI( psz_string );
153 lua_pushstring( p_state, psz_string );
158 int vlclua_resolve_xml_special_chars( lua_State *p_state )
160 int i = lua_gettop( p_state );
162 const char *psz_cstring = lua_tostring( p_state, 1 );
163 if( !psz_cstring ) return 0;
164 char *psz_string = strdup( psz_cstring );
165 lua_pop( p_state, i );
166 resolve_xml_special_chars( psz_string );
167 lua_pushstring( p_state, psz_string );
172 int vlclua_msg_dbg( lua_State *p_state )
174 vlc_object_t *p_this = vlclua_get_this( p_state );
175 int i = lua_gettop( p_state );
177 const char *psz_cstring = lua_tostring( p_state, 1 );
178 if( !psz_cstring ) return 0;
179 msg_Dbg( p_this, "%s", psz_cstring );
182 int vlclua_msg_warn( lua_State *p_state )
184 vlc_object_t *p_this = vlclua_get_this( p_state );
185 int i = lua_gettop( p_state );
187 const char *psz_cstring = lua_tostring( p_state, 1 );
188 if( !psz_cstring ) return 0;
189 msg_Warn( p_this, "%s", psz_cstring );
192 int vlclua_msg_err( lua_State *p_state )
194 vlc_object_t *p_this = vlclua_get_this( p_state );
195 int i = lua_gettop( p_state );
197 const char *psz_cstring = lua_tostring( p_state, 1 );
198 if( !psz_cstring ) return 0;
199 msg_Err( p_this, "%s", psz_cstring );
202 int vlclua_msg_info( lua_State *p_state )
204 vlc_object_t *p_this = vlclua_get_this( p_state );
205 int i = lua_gettop( p_state );
207 const char *psz_cstring = lua_tostring( p_state, 1 );
208 if( !psz_cstring ) return 0;
209 msg_Info( p_this, "%s", psz_cstring );
213 /*****************************************************************************
215 *****************************************************************************/
216 static int file_select( const char *file )
218 int i = strlen( file );
219 return i > 4 && !strcmp( file+i-4, ".lua" );
222 static int file_compare( const char **a, const char **b )
224 return strcmp( *a, *b );
228 /*****************************************************************************
229 * Will execute func on all scripts in luadirname, and stop if func returns
231 *****************************************************************************/
232 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
233 const char * luadirname,
234 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
238 int i_ret = VLC_EGENERIC;
241 char **ppsz_filelist = NULL;
242 char **ppsz_fileend = NULL;
245 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
248 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
249 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
252 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
254 const char *psz_vlcpath = config_GetDataDir();
255 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
258 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
262 if( asprintf( &ppsz_dir_list[1],
263 "share" DIR_SEP "%s", luadirname ) < 0 )
266 # ifdef HAVE_SYS_STAT_H
268 struct stat stat_info;
269 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
270 || !S_ISDIR( stat_info.st_mode ) )
272 free(ppsz_dir_list[1]);
273 if( asprintf( &ppsz_dir_list[1],
274 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
281 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
287 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
290 free( ppsz_filelist );
291 ppsz_filelist = NULL;
299 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
300 dir = utf8_opendir( *ppsz_dir );
303 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
304 if( i_files < 1 ) continue;
305 ppsz_fileend = ppsz_filelist + i_files;
307 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
310 if( asprintf( &psz_filename,
311 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
313 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
315 i_ret = func( p_this, psz_filename, p_state, user_data );
317 free( psz_filename );
319 if( i_ret == VLC_SUCCESS ) break;
321 if( i_ret == VLC_SUCCESS ) break;
326 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
329 free( ppsz_filelist );
331 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
334 if( dir ) closedir( dir );
340 /*****************************************************************************
341 * Meta data setters utility.
342 *****************************************************************************/
343 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *p_state,
344 int o, int t, input_item_t *p_input )
346 const char *psz_value;
347 #define TRY_META( a, b ) \
348 lua_getfield( p_state, o, a ); \
349 if( lua_isstring( p_state, t ) ) \
351 psz_value = lua_tostring( p_state, t ); \
352 msg_Dbg( p_this, #b ": %s", psz_value ); \
353 input_item_Set ## b ( p_input, psz_value ); \
355 lua_pop( p_state, 1 ); /* pop a */
356 TRY_META( "title", Title );
357 TRY_META( "artist", Artist );
358 TRY_META( "genre", Genre );
359 TRY_META( "copyright", Copyright );
360 TRY_META( "album", Album );
361 TRY_META( "tracknum", TrackNum );
362 TRY_META( "description", Description );
363 TRY_META( "rating", Rating );
364 TRY_META( "date", Date );
365 TRY_META( "setting", Setting );
366 TRY_META( "url", URL );
367 TRY_META( "language", Language );
368 TRY_META( "nowplaying", NowPlaying );
369 TRY_META( "publisher", Publisher );
370 TRY_META( "encodedby", EncodedBy );
371 TRY_META( "arturl", ArtURL );
372 TRY_META( "trackid", TrackID );
375 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *p_state,
376 int o, int t, input_item_t *p_input )
378 lua_getfield( p_state, o, "meta" );
379 if( lua_istable( p_state, t ) )
381 lua_pushnil( p_state );
382 while( lua_next( p_state, t ) )
384 if( !lua_isstring( p_state, t+1 ) )
386 msg_Warn( p_this, "Custom meta data category name must be "
389 else if( !lua_istable( p_state, t+2 ) )
391 msg_Warn( p_this, "Custom meta data category contents "
396 const char *psz_meta_category = lua_tostring( p_state, t+1 );
397 msg_Dbg( p_this, "Found custom meta data category: %s",
399 lua_pushnil( p_state );
400 while( lua_next( p_state, t+2 ) )
402 if( !lua_isstring( p_state, t+3 ) )
404 msg_Warn( p_this, "Custom meta category item name "
405 "must be a string." );
407 else if( !lua_isstring( p_state, t+4 ) )
409 msg_Warn( p_this, "Custom meta category item value "
410 "must be a string." );
414 const char *psz_meta_name =
415 lua_tostring( p_state, t+3 );
416 const char *psz_meta_value =
417 lua_tostring( p_state, t+4 );
418 msg_Dbg( p_this, "Custom meta %s, %s: %s",
419 psz_meta_category, psz_meta_name,
421 input_ItemAddInfo( p_input, psz_meta_category,
422 psz_meta_name, psz_meta_value );
424 lua_pop( p_state, 1 ); /* pop item */
427 lua_pop( p_state, 1 ); /* pop category */
430 lua_pop( p_state, 1 ); /* pop "meta" */