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 lua_pushlightuserdata( p_state, p_stream );
93 int vlclua_stream_read( lua_State *p_state )
95 int i = lua_gettop( p_state );
101 p_stream = (stream_t *)lua_topointer( p_state, 1 );
102 n = lua_tonumber( p_state, 2 );
103 lua_pop( p_state, i );
104 p_read = malloc( n );
105 if( !p_read ) return 0;
106 i_read = stream_Read( p_stream, p_read, n );
107 lua_pushlstring( p_state, (const char *)p_read, i_read );
112 int vlclua_stream_readline( lua_State *p_state )
114 int i = lua_gettop( p_state );
117 p_stream = (stream_t *)lua_topointer( p_state, 1 );
118 lua_pop( p_state, i );
119 char *psz_line = stream_ReadLine( p_stream );
122 lua_pushstring( p_state, psz_line );
127 lua_pushnil( p_state );
132 int vlclua_stream_delete( lua_State *p_state )
134 int i = lua_gettop( p_state );
137 p_stream = (stream_t *)lua_topointer( p_state, 1 );
138 lua_pop( p_state, i );
139 stream_Delete( p_stream );
143 int vlclua_decode_uri( lua_State *p_state )
145 int i = lua_gettop( p_state );
147 const char *psz_cstring = lua_tostring( p_state, 1 );
148 if( !psz_cstring ) return 0;
149 char *psz_string = strdup( psz_cstring );
150 lua_pop( p_state, i );
151 decode_URI( psz_string );
152 lua_pushstring( p_state, psz_string );
157 int vlclua_resolve_xml_special_chars( lua_State *p_state )
159 int i = lua_gettop( p_state );
161 const char *psz_cstring = lua_tostring( p_state, 1 );
162 if( !psz_cstring ) return 0;
163 char *psz_string = strdup( psz_cstring );
164 lua_pop( p_state, i );
165 resolve_xml_special_chars( psz_string );
166 lua_pushstring( p_state, psz_string );
171 int vlclua_msg_dbg( lua_State *p_state )
173 vlc_object_t *p_this = vlclua_get_this( p_state );
174 int i = lua_gettop( p_state );
176 const char *psz_cstring = lua_tostring( p_state, 1 );
177 if( !psz_cstring ) return 0;
178 msg_Dbg( p_this, "%s", psz_cstring );
181 int vlclua_msg_warn( lua_State *p_state )
183 vlc_object_t *p_this = vlclua_get_this( p_state );
184 int i = lua_gettop( p_state );
186 const char *psz_cstring = lua_tostring( p_state, 1 );
187 if( !psz_cstring ) return 0;
188 msg_Warn( p_this, "%s", psz_cstring );
191 int vlclua_msg_err( lua_State *p_state )
193 vlc_object_t *p_this = vlclua_get_this( p_state );
194 int i = lua_gettop( p_state );
196 const char *psz_cstring = lua_tostring( p_state, 1 );
197 if( !psz_cstring ) return 0;
198 msg_Err( p_this, "%s", psz_cstring );
201 int vlclua_msg_info( lua_State *p_state )
203 vlc_object_t *p_this = vlclua_get_this( p_state );
204 int i = lua_gettop( p_state );
206 const char *psz_cstring = lua_tostring( p_state, 1 );
207 if( !psz_cstring ) return 0;
208 msg_Info( p_this, "%s", psz_cstring );
212 /*****************************************************************************
214 *****************************************************************************/
215 static int file_select( const char *file )
217 int i = strlen( file );
218 return i > 4 && !strcmp( file+i-4, ".lua" );
221 static int file_compare( const char **a, const char **b )
223 return strcmp( *a, *b );
227 /*****************************************************************************
228 * Will execute func on all scripts in luadirname, and stop if func returns
230 *****************************************************************************/
231 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
232 const char * luadirname,
233 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
237 int i_ret = VLC_EGENERIC;
240 char **ppsz_filelist = NULL;
241 char **ppsz_fileend = NULL;
244 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
247 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
248 p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
251 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
253 const char *psz_vlcpath = config_GetDataDir();
254 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
257 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
261 if( asprintf( &ppsz_dir_list[1],
262 "share" DIR_SEP "%s", luadirname ) < 0 )
265 # ifdef HAVE_SYS_STAT_H
267 struct stat stat_info;
268 if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
269 || !S_ISDIR( stat_info.st_mode ) )
271 free(ppsz_dir_list[1]);
272 if( asprintf( &ppsz_dir_list[1],
273 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
280 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
286 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
289 free( ppsz_filelist );
290 ppsz_filelist = NULL;
298 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
299 dir = utf8_opendir( *ppsz_dir );
302 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
303 if( i_files < 1 ) continue;
304 ppsz_fileend = ppsz_filelist + i_files;
306 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
309 if( asprintf( &psz_filename,
310 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
312 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
314 i_ret = func( p_this, psz_filename, p_state, user_data );
316 free( psz_filename );
318 if( i_ret == VLC_SUCCESS ) break;
320 if( i_ret == VLC_SUCCESS ) break;
325 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
328 free( ppsz_filelist );
330 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
333 if( dir ) closedir( dir );
339 /*****************************************************************************
340 * Meta data setters utility.
341 *****************************************************************************/
342 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *p_state,
343 int o, int t, input_item_t *p_input )
345 const char *psz_value;
346 #define TRY_META( a, b ) \
347 lua_getfield( p_state, o, a ); \
348 if( lua_isstring( p_state, t ) ) \
350 psz_value = lua_tostring( p_state, t ); \
351 msg_Dbg( p_this, #b ": %s", psz_value ); \
352 input_item_Set ## b ( p_input, psz_value ); \
354 lua_pop( p_state, 1 ); /* pop a */
355 TRY_META( "title", Title );
356 TRY_META( "artist", Artist );
357 TRY_META( "genre", Genre );
358 TRY_META( "copyright", Copyright );
359 TRY_META( "album", Album );
360 TRY_META( "tracknum", TrackNum );
361 TRY_META( "description", Description );
362 TRY_META( "rating", Rating );
363 TRY_META( "date", Date );
364 TRY_META( "setting", Setting );
365 TRY_META( "url", URL );
366 TRY_META( "language", Language );
367 TRY_META( "nowplaying", NowPlaying );
368 TRY_META( "publisher", Publisher );
369 TRY_META( "encodedby", EncodedBy );
370 TRY_META( "arturl", ArtURL );
371 TRY_META( "trackid", TrackID );
374 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *p_state,
375 int o, int t, input_item_t *p_input )
377 lua_getfield( p_state, o, "meta" );
378 if( lua_istable( p_state, t ) )
380 lua_pushnil( p_state );
381 while( lua_next( p_state, t ) )
383 if( !lua_isstring( p_state, t+1 ) )
385 msg_Warn( p_this, "Custom meta data category name must be "
388 else if( !lua_istable( p_state, t+2 ) )
390 msg_Warn( p_this, "Custom meta data category contents "
395 const char *psz_meta_category = lua_tostring( p_state, t+1 );
396 msg_Dbg( p_this, "Found custom meta data category: %s",
398 lua_pushnil( p_state );
399 while( lua_next( p_state, t+2 ) )
401 if( !lua_isstring( p_state, t+3 ) )
403 msg_Warn( p_this, "Custom meta category item name "
404 "must be a string." );
406 else if( !lua_isstring( p_state, t+4 ) )
408 msg_Warn( p_this, "Custom meta category item value "
409 "must be a string." );
413 const char *psz_meta_name =
414 lua_tostring( p_state, t+3 );
415 const char *psz_meta_value =
416 lua_tostring( p_state, t+4 );
417 msg_Dbg( p_this, "Custom meta %s, %s: %s",
418 psz_meta_category, psz_meta_name,
420 input_ItemAddInfo( p_input, psz_meta_category,
421 psz_meta_name, psz_meta_value );
423 lua_pop( p_state, 1 ); /* pop item */
426 lua_pop( p_state, 1 ); /* pop category */
429 lua_pop( p_state, 1 ); /* pop "meta" */