1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc_input.h>
31 #include <vlc_playlist.h>
33 #include <vlc_stream.h>
34 #include <vlc_charset.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
40 #include <lua.h> /* Low level lua C API */
41 #include <lauxlib.h> /* Higher level C API */
42 #include <lualib.h> /* Lua libs */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int FindArt( vlc_object_t * );
48 static int FindMeta( vlc_object_t *p_this );
50 /*****************************************************************************
52 *****************************************************************************/
55 set_shortname( N_( "Lua Meta" ) );
56 set_description( _("Fetch Artwork using lua scripts") );
57 set_capability( "meta fetcher", 10 );
58 set_callbacks( FindMeta, NULL );
60 set_capability( "art finder", 10 );
61 set_callbacks( FindArt, NULL );
64 /*****************************************************************************
66 *****************************************************************************/
67 static vlc_object_t * vlclua_get_this( lua_State *p_state )
69 vlc_object_t * p_this;
70 lua_getglobal( p_state, "vlc" );
71 lua_getfield( p_state, lua_gettop( p_state ), "private" );
72 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
73 lua_pop( p_state, 2 );
77 static int vlclua_stream_new( lua_State *p_state )
79 vlc_object_t * p_this = vlclua_get_this( p_state );
80 int i = lua_gettop( p_state );
84 psz_url = lua_tostring( p_state, 1 );
85 lua_pop( p_state, i );
86 p_stream = stream_UrlNew( p_this, psz_url );
87 lua_pushlightuserdata( p_state, p_stream );
91 static int vlclua_stream_read( lua_State *p_state )
93 int i = lua_gettop( p_state );
99 p_stream = (stream_t *)lua_topointer( p_state, 1 );
100 n = lua_tonumber( p_state, 2 );
101 lua_pop( p_state, i );
102 p_read = malloc( n );
103 if( !p_read ) return 0;
104 i_read = stream_Read( p_stream, p_read, n );
105 lua_pushlstring( p_state, (const char *)p_read, i_read );
110 static int vlclua_stream_readline( lua_State *p_state )
112 int i = lua_gettop( p_state );
115 p_stream = (stream_t *)lua_topointer( p_state, 1 );
116 lua_pop( p_state, i );
117 char *psz_line = stream_ReadLine( p_stream );
120 lua_pushstring( p_state, psz_line );
125 lua_pushnil( p_state );
130 static int vlclua_stream_delete( lua_State *p_state )
132 int i = lua_gettop( p_state );
135 p_stream = (stream_t *)lua_topointer( p_state, 1 );
136 lua_pop( p_state, i );
137 stream_Delete( p_stream );
141 static int vlclua_msg_dbg( lua_State *p_state )
143 vlc_object_t *p_this = vlclua_get_this( p_state );
144 int i = lua_gettop( p_state );
146 const char *psz_cstring = lua_tostring( p_state, 1 );
147 if( !psz_cstring ) return 0;
148 msg_Dbg( p_this, "%s", psz_cstring );
151 static int vlclua_msg_warn( lua_State *p_state )
153 vlc_object_t *p_this = vlclua_get_this( p_state );
154 int i = lua_gettop( p_state );
156 const char *psz_cstring = lua_tostring( p_state, 1 );
157 if( !psz_cstring ) return 0;
158 msg_Warn( p_this, "%s", psz_cstring );
161 static int vlclua_msg_err( lua_State *p_state )
163 vlc_object_t *p_this = vlclua_get_this( p_state );
164 int i = lua_gettop( p_state );
166 const char *psz_cstring = lua_tostring( p_state, 1 );
167 if( !psz_cstring ) return 0;
168 msg_Err( p_this, "%s", psz_cstring );
171 static int vlclua_msg_info( 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_Info( p_this, "%s", psz_cstring );
182 /* Functions to register */
183 static luaL_Reg p_reg[] =
185 { "stream_new", vlclua_stream_new },
186 { "stream_read", vlclua_stream_read },
187 { "stream_readline", vlclua_stream_readline },
188 { "stream_delete", vlclua_stream_delete },
189 { "msg_dbg", vlclua_msg_dbg },
190 { "msg_warn", vlclua_msg_warn },
191 { "msg_err", vlclua_msg_err },
192 { "msg_info", vlclua_msg_info },
195 /*****************************************************************************
197 *****************************************************************************/
198 static int file_select( const char *file )
200 int i = strlen( file );
201 return i > 4 && !strcmp( file+i-4, ".lua" );
204 static int file_compare( const char **a, const char **b )
206 return strcmp( *a, *b );
209 /*****************************************************************************
211 *****************************************************************************/
212 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
214 lua_State * p_state = luaL_newstate();
217 msg_Err( p_this, "Could not create new Lua State" );
221 /* Load Lua libraries */
222 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
224 luaL_register( p_state, "vlc", p_reg );
226 lua_pushlightuserdata( p_state, p_this );
227 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
229 lua_pushstring( p_state, p_item->psz_name );
230 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
232 lua_pushstring( p_state, p_item->p_meta->psz_title );
233 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
235 lua_pushstring( p_state, p_item->p_meta->psz_album );
236 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
238 lua_pushstring( p_state, p_item->p_meta->psz_arturl );
239 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
240 /* XXX: all should be passed */
245 /*****************************************************************************
246 * Will execute func on all scripts in luadirname, and stop if func returns
248 *****************************************************************************/
249 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
250 const char * luadirname,
251 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
255 int i_ret = VLC_EGENERIC;
258 char **ppsz_filelist = NULL;
259 char **ppsz_fileend = NULL;
262 char *ppsz_dir_list[] = { NULL, NULL, NULL };
265 ppsz_dir_list[0] = malloc( strlen( p_this->p_libvlc->psz_homedir )
266 + strlen( "/"CONFIG_DIR"/" ) + strlen( luadirname ) + 1 );
267 sprintf( ppsz_dir_list[0], "%s/"CONFIG_DIR"/%s",
268 p_this->p_libvlc->psz_homedir, luadirname );
270 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
272 const char *psz_vlcpath = config_GetDataDir( p_this );
273 ppsz_dir_list[1] = malloc( strlen( psz_vlcpath ) + strlen( luadirname ) + 1 );
274 if( !ppsz_dir_list[1] ) return VLC_ENOMEM;
275 sprintf( ppsz_dir_list[1], "%s/%s", psz_vlcpath, luadirname );
279 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
285 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
288 free( ppsz_filelist );
289 ppsz_filelist = NULL;
297 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
298 dir = utf8_opendir( *ppsz_dir );
301 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
302 if( i_files < 1 ) continue;
303 ppsz_fileend = ppsz_filelist + i_files;
305 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
308 asprintf( &psz_filename, "%s/%s", *ppsz_dir, *ppsz_file );
309 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
311 i_ret = func( p_this, psz_filename, p_state, user_data );
313 free( psz_filename );
315 if( i_ret == VLC_SUCCESS ) break;
317 if( i_ret == VLC_SUCCESS ) break;
322 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
325 free( ppsz_filelist );
327 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
330 if( dir ) closedir( dir );
335 /*****************************************************************************
336 * Meta data setters utility.
337 *****************************************************************************/
338 static inline void read_meta_data( vlc_object_t *p_this,
339 lua_State *p_state, int o, int t,
340 input_item_t *p_input )
342 const char *psz_value;
343 #define TRY_META( a, b ) \
344 lua_getfield( p_state, o, a ); \
345 if( lua_isstring( p_state, t ) ) \
347 psz_value = lua_tostring( p_state, t ); \
348 msg_Dbg( p_this, #b ": %s", psz_value ); \
349 vlc_meta_Set ## b ( p_input->p_meta, psz_value ); \
351 lua_pop( p_state, 1 ); /* pop a */
352 TRY_META( "title", Title );
353 TRY_META( "artist", Artist );
354 TRY_META( "genre", Genre );
355 TRY_META( "copyright", Copyright );
356 TRY_META( "album", Album );
357 TRY_META( "tracknum", Tracknum );
358 TRY_META( "description", Description );
359 TRY_META( "rating", Rating );
360 TRY_META( "date", Date );
361 TRY_META( "setting", Setting );
362 TRY_META( "url", URL );
363 TRY_META( "language", Language );
364 TRY_META( "nowplaying", NowPlaying );
365 TRY_META( "publisher", Publisher );
366 TRY_META( "encodedby", EncodedBy );
367 TRY_META( "arturl", ArtURL );
368 TRY_META( "trackid", TrackID );
371 static inline void read_custom_meta_data( vlc_object_t *p_this,
372 lua_State *p_state, int o, int t,
373 input_item_t *p_input )
375 lua_getfield( p_state, o, "meta" );
376 if( lua_istable( p_state, t ) )
378 lua_pushnil( p_state );
379 while( lua_next( p_state, t ) )
381 if( !lua_isstring( p_state, t+1 ) )
383 msg_Warn( p_this, "Custom meta data category name must be "
386 else if( !lua_istable( p_state, t+2 ) )
388 msg_Warn( p_this, "Custom meta data category contents "
393 const char *psz_meta_category = lua_tostring( p_state, t+1 );
394 msg_Dbg( p_this, "Found custom meta data category: %s",
396 lua_pushnil( p_state );
397 while( lua_next( p_state, t+2 ) )
399 if( !lua_isstring( p_state, t+3 ) )
401 msg_Warn( p_this, "Custom meta category item name "
402 "must be a string." );
404 else if( !lua_isstring( p_state, t+4 ) )
406 msg_Warn( p_this, "Custom meta category item value "
407 "must be a string." );
411 const char *psz_meta_name =
412 lua_tostring( p_state, t+3 );
413 const char *psz_meta_value =
414 lua_tostring( p_state, t+4 );
415 msg_Dbg( p_this, "Custom meta %s, %s: %s",
416 psz_meta_category, psz_meta_name,
418 input_ItemAddInfo( p_input, psz_meta_category,
419 psz_meta_name, psz_meta_value );
421 lua_pop( p_state, 1 ); /* pop item */
424 lua_pop( p_state, 1 ); /* pop category */
427 lua_pop( p_state, 1 ); /* pop "meta" */
430 /*****************************************************************************
431 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
432 * pointed by psz_filename.
433 *****************************************************************************/
434 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
435 lua_State * p_state, void * user_data )
437 int i_ret = VLC_EGENERIC;
438 input_item_t * p_input = user_data;
441 /* Ugly hack to delete previous versions of the fetchart()
443 lua_pushnil( p_state );
444 lua_setglobal( p_state, "fetch_art" );
446 /* Load and run the script(s) */
447 if( luaL_dofile( p_state, psz_filename ) )
449 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
450 lua_tostring( p_state, lua_gettop( p_state ) ) );
451 lua_pop( p_state, 1 );
455 lua_getglobal( p_state, "fetch_art" );
457 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
459 msg_Warn( p_this, "Error while runing script %s, "
460 "function fetch_art() not found", psz_filename );
461 lua_pop( p_state, 1 );
465 if( lua_pcall( p_state, 0, 1, 0 ) )
467 msg_Warn( p_this, "Error while runing script %s, "
468 "function fetch_art(): %s", psz_filename,
469 lua_tostring( p_state, lua_gettop( p_state ) ) );
470 lua_pop( p_state, 1 );
474 if((s = lua_gettop( p_state )))
476 const char * psz_value;
478 if( lua_isstring( p_state, s ) )
480 psz_value = lua_tostring( p_state, s );
481 if( psz_value && *psz_value != 0 )
483 msg_Dbg( p_this, "setting arturl: %s", psz_value );
484 vlc_meta_SetArtURL ( p_input->p_meta, psz_value );
490 msg_Err( p_this, "Lua playlist script %s: "
491 "didn't return a string", psz_filename );
496 msg_Err( p_this, "Script went completely foobar" );
502 /*****************************************************************************
503 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
504 * pointed by psz_filename.
505 *****************************************************************************/
506 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
507 lua_State * p_state, void * user_data )
509 input_item_t * p_input = user_data;
512 /* Ugly hack to delete previous versions of the fetchmeta()
514 lua_pushnil( p_state );
515 lua_setglobal( p_state, "fetch_meta" );
517 /* Load and run the script(s) */
518 if( luaL_dofile( p_state, psz_filename ) )
520 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
521 lua_tostring( p_state, lua_gettop( p_state ) ) );
522 lua_pop( p_state, 1 );
526 lua_getglobal( p_state, "fetch_meta" );
528 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
530 msg_Warn( p_this, "Error while runing script %s, "
531 "function fetch_meta() not found", psz_filename );
532 lua_pop( p_state, 1 );
536 if( lua_pcall( p_state, 0, 1, 0 ) )
538 msg_Warn( p_this, "Error while runing script %s, "
539 "function fetch_meta(): %s", psz_filename,
540 lua_tostring( p_state, lua_gettop( p_state ) ) );
541 lua_pop( p_state, 1 );
546 if((t = lua_gettop( p_state )))
548 if( lua_istable( p_state, t ) )
550 read_meta_data( p_this, p_state, t, t+1, p_input );
551 read_custom_meta_data( p_this, p_state, t, t+1, p_input );
555 msg_Err( p_this, "Lua playlist script %s: "
556 "didn't return a table", psz_filename );
561 msg_Err( p_this, "Script went completely foobar" );
564 /* We tell the batch thing to continue, hence all script
565 * will get the change to add its meta. This behaviour could
570 /*****************************************************************************
571 * Module entry point for meta.
572 *****************************************************************************/
573 static int FindMeta( vlc_object_t *p_this )
575 meta_engine_t *p_me = (meta_engine_t *)p_this;
576 input_item_t *p_item = p_me->p_item;
577 lua_State *p_state = vlclua_meta_init( p_this, p_item );
579 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
580 lua_close( p_state );
584 /*****************************************************************************
585 * Module entry point for art.
586 *****************************************************************************/
587 static int FindArt( vlc_object_t *p_this )
589 playlist_t *p_playlist = (playlist_t *)p_this;
590 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
591 lua_State *p_state = vlclua_meta_init( p_this, p_item );
593 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
594 lua_close( p_state );