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 *****************************************************************************/
34 #include <vlc_charset.h>
36 #include <lua.h> /* Low level lua C API */
37 #include <lauxlib.h> /* Higher level C API */
38 #include <lualib.h> /* Lua libs */
42 /*****************************************************************************
44 *****************************************************************************/
47 add_shortcut( "luameta" );
48 set_shortname( N_( "Lua Meta" ) );
49 set_description( _("Fetch metadata using lua scripts") );
50 set_capability( "meta fetcher", 10 );
51 set_callbacks( E_(FindMeta), NULL );
53 set_shortname( N_( "Lua Art" ) );
54 set_description( _("Fetch artwork using lua scripts") );
55 set_capability( "art finder", 10 );
56 set_callbacks( E_(FindArt), NULL );
58 add_shortcut( "luaplaylist" );
59 set_category( CAT_INPUT );
60 set_subcategory( SUBCAT_INPUT_DEMUX );
61 set_shortname( _("Lua Playlist") );
62 set_description( _("Lua Playlist Parser Interface") );
63 set_capability( "demux2", 9 );
64 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
67 /*****************************************************************************
69 *****************************************************************************/
70 vlc_object_t * vlclua_get_this( lua_State *p_state )
72 vlc_object_t * p_this;
73 lua_getglobal( p_state, "vlc" );
74 lua_getfield( p_state, lua_gettop( p_state ), "private" );
75 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
76 lua_pop( p_state, 2 );
80 int vlclua_stream_new( lua_State *p_state )
82 vlc_object_t * p_this = vlclua_get_this( p_state );
83 int i = lua_gettop( p_state );
87 psz_url = lua_tostring( p_state, 1 );
88 lua_pop( p_state, i );
89 p_stream = stream_UrlNew( p_this, psz_url );
90 if( !p_stream ) return 0;
91 lua_pushlightuserdata( p_state, p_stream );
95 int vlclua_stream_read( lua_State *p_state )
97 int i = lua_gettop( p_state );
103 p_stream = (stream_t *)lua_topointer( p_state, 1 );
104 n = lua_tonumber( p_state, 2 );
105 lua_pop( p_state, i );
106 p_read = malloc( n );
107 if( !p_read ) return 0;
108 i_read = stream_Read( p_stream, p_read, n );
109 lua_pushlstring( p_state, (const char *)p_read, i_read );
114 int vlclua_stream_readline( lua_State *p_state )
116 int i = lua_gettop( p_state );
119 p_stream = (stream_t *)lua_topointer( p_state, 1 );
120 lua_pop( p_state, i );
121 char *psz_line = stream_ReadLine( p_stream );
124 lua_pushstring( p_state, psz_line );
129 lua_pushnil( p_state );
134 int vlclua_stream_delete( lua_State *p_state )
136 int i = lua_gettop( p_state );
139 p_stream = (stream_t *)lua_topointer( p_state, 1 );
140 lua_pop( p_state, i );
141 stream_Delete( p_stream );
145 int vlclua_decode_uri( lua_State *p_state )
147 int i = lua_gettop( p_state );
149 const char *psz_cstring = lua_tostring( p_state, 1 );
150 if( !psz_cstring ) return 0;
151 char *psz_string = strdup( psz_cstring );
152 lua_pop( p_state, i );
153 decode_URI( psz_string );
154 lua_pushstring( p_state, psz_string );
159 int vlclua_resolve_xml_special_chars( lua_State *p_state )
161 int i = lua_gettop( p_state );
163 const char *psz_cstring = lua_tostring( p_state, 1 );
164 if( !psz_cstring ) return 0;
165 char *psz_string = strdup( psz_cstring );
166 lua_pop( p_state, i );
167 resolve_xml_special_chars( psz_string );
168 lua_pushstring( p_state, psz_string );
173 int vlclua_msg_dbg( lua_State *p_state )
175 vlc_object_t *p_this = vlclua_get_this( p_state );
176 int i = lua_gettop( p_state );
178 const char *psz_cstring = lua_tostring( p_state, 1 );
179 if( !psz_cstring ) return 0;
180 msg_Dbg( p_this, "%s", psz_cstring );
183 int vlclua_msg_warn( lua_State *p_state )
185 vlc_object_t *p_this = vlclua_get_this( p_state );
186 int i = lua_gettop( p_state );
188 const char *psz_cstring = lua_tostring( p_state, 1 );
189 if( !psz_cstring ) return 0;
190 msg_Warn( p_this, "%s", psz_cstring );
193 int vlclua_msg_err( lua_State *p_state )
195 vlc_object_t *p_this = vlclua_get_this( p_state );
196 int i = lua_gettop( p_state );
198 const char *psz_cstring = lua_tostring( p_state, 1 );
199 if( !psz_cstring ) return 0;
200 msg_Err( p_this, "%s", psz_cstring );
203 int vlclua_msg_info( lua_State *p_state )
205 vlc_object_t *p_this = vlclua_get_this( p_state );
206 int i = lua_gettop( p_state );
208 const char *psz_cstring = lua_tostring( p_state, 1 );
209 if( !psz_cstring ) return 0;
210 msg_Info( p_this, "%s", psz_cstring );
214 /*****************************************************************************
216 *****************************************************************************/
217 static int file_select( const char *file )
219 int i = strlen( file );
220 return i > 4 && !strcmp( file+i-4, ".lua" );
223 static int file_compare( const char **a, const char **b )
225 return strcmp( *a, *b );
229 /*****************************************************************************
230 * Will execute func on all scripts in luadirname, and stop if func returns
232 *****************************************************************************/
233 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
234 const char * luadirname,
235 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
239 int i_ret = VLC_EGENERIC;
242 char **ppsz_filelist = NULL;
243 char **ppsz_fileend = NULL;
246 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
249 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
250 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
253 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
255 const char *psz_vlcpath = config_GetDataDir();
256 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
259 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
263 if( asprintf( &ppsz_dir_list[1],
264 "share" DIR_SEP "%s", luadirname ) < 0 )
267 # ifdef HAVE_SYS_STAT_H
269 struct stat stat_info;
270 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
271 || !S_ISDIR( stat_info.st_mode ) )
273 free(ppsz_dir_list[1]);
274 if( asprintf( &ppsz_dir_list[1],
275 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
282 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
288 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
291 free( ppsz_filelist );
292 ppsz_filelist = NULL;
300 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
301 dir = utf8_opendir( *ppsz_dir );
304 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
305 if( i_files < 1 ) continue;
306 ppsz_fileend = ppsz_filelist + i_files;
308 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
311 if( asprintf( &psz_filename,
312 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
314 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
316 i_ret = func( p_this, psz_filename, p_state, user_data );
318 free( psz_filename );
320 if( i_ret == VLC_SUCCESS ) break;
322 if( i_ret == VLC_SUCCESS ) break;
327 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
330 free( ppsz_filelist );
332 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
335 if( dir ) closedir( dir );
341 /*****************************************************************************
342 * Meta data setters utility.
343 *****************************************************************************/
344 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *p_state,
345 int o, int t, input_item_t *p_input )
347 const char *psz_value;
348 #define TRY_META( a, b ) \
349 lua_getfield( p_state, o, a ); \
350 if( lua_isstring( p_state, t ) ) \
352 psz_value = lua_tostring( p_state, t ); \
353 EnsureUTF8( psz_value ); \
354 msg_Dbg( p_this, #b ": %s", psz_value ); \
355 input_item_Set ## b ( p_input, psz_value ); \
357 lua_pop( p_state, 1 ); /* pop a */
358 TRY_META( "title", Title );
359 TRY_META( "artist", Artist );
360 TRY_META( "genre", Genre );
361 TRY_META( "copyright", Copyright );
362 TRY_META( "album", Album );
363 TRY_META( "tracknum", TrackNum );
364 TRY_META( "description", Description );
365 TRY_META( "rating", Rating );
366 TRY_META( "date", Date );
367 TRY_META( "setting", Setting );
368 TRY_META( "url", URL );
369 TRY_META( "language", Language );
370 TRY_META( "nowplaying", NowPlaying );
371 TRY_META( "publisher", Publisher );
372 TRY_META( "encodedby", EncodedBy );
373 TRY_META( "arturl", ArtURL );
374 TRY_META( "trackid", TrackID );
377 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *p_state,
378 int o, int t, input_item_t *p_input )
380 lua_getfield( p_state, o, "meta" );
381 if( lua_istable( p_state, t ) )
383 lua_pushnil( p_state );
384 while( lua_next( p_state, t ) )
386 if( !lua_isstring( p_state, t+1 ) )
388 msg_Warn( p_this, "Custom meta data category name must be "
391 else if( !lua_istable( p_state, t+2 ) )
393 msg_Warn( p_this, "Custom meta data category contents "
398 const char *psz_meta_category = lua_tostring( p_state, t+1 );
399 msg_Dbg( p_this, "Found custom meta data category: %s",
401 lua_pushnil( p_state );
402 while( lua_next( p_state, t+2 ) )
404 if( !lua_isstring( p_state, t+3 ) )
406 msg_Warn( p_this, "Custom meta category item name "
407 "must be a string." );
409 else if( !lua_isstring( p_state, t+4 ) )
411 msg_Warn( p_this, "Custom meta category item value "
412 "must be a string." );
416 const char *psz_meta_name =
417 lua_tostring( p_state, t+3 );
418 const char *psz_meta_value =
419 lua_tostring( p_state, t+4 );
420 msg_Dbg( p_this, "Custom meta %s, %s: %s",
421 psz_meta_category, psz_meta_name,
423 input_ItemAddInfo( p_input, psz_meta_category,
424 psz_meta_name, psz_meta_value );
426 lua_pop( p_state, 1 ); /* pop item */
429 lua_pop( p_state, 1 ); /* pop category */
432 lua_pop( p_state, 1 ); /* pop "meta" */