]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlc.c
Use config_Get*Dir
[vlc] / modules / misc / lua / vlc.c
1 /*****************************************************************************
2  * vlc.c: Generic lua inteface functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc/vlc.h>
37 #include <vlc_meta.h>
38 #include <vlc_charset.h>
39 #include <vlc_aout.h>
40
41 #include <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43 #include <lualib.h>     /* Lua libs */
44
45 #include "vlc.h"
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50
51 #define INTF_TEXT N_("Lua interface")
52 #define INTF_LONGTEXT N_("Lua interface module to load")
53
54 #define CONFIG_TEXT N_("Lua inteface configuration")
55 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
56
57 vlc_module_begin();
58     add_submodule();
59         set_shortname( N_( "Lua Art" ) );
60         set_description( _("Fetch artwork using lua scripts") );
61         set_capability( "art finder", 10 );
62         set_callbacks( E_(FindArt), NULL );
63     add_submodule();
64         add_shortcut( "luaplaylist" );
65         set_category( CAT_INPUT );
66         set_subcategory( SUBCAT_INPUT_DEMUX );
67         set_shortname( _("Lua Playlist") );
68         set_description( _("Lua Playlist Parser Interface") );
69         set_capability( "demux", 2 );
70         set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
71     add_submodule();
72         add_shortcut( "luaintf" );
73         add_shortcut( "luarc" );
74         /* add_shortcut( "rc" ); */
75         add_shortcut( "luahotkeys" );
76         /* add_shortcut( "hotkeys" ); */
77         add_shortcut( "luatelnet" );
78         /* add_shortcut( "telnet" ); */
79         add_shortcut( "luahttp" );
80         /* add_shortcut( "http" ); */
81         set_description( _("Lua Interface Module") );
82         set_capability( "interface", 0 );
83         add_string( "lua-intf", "dummy", NULL,
84                     INTF_TEXT, INTF_LONGTEXT, false );
85         add_string( "lua-config", "", NULL,
86                     CONFIG_TEXT, CONFIG_LONGTEXT, false );
87         set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Internal lua<->vlc utils
92  *****************************************************************************/
93 vlc_object_t * vlclua_get_this( lua_State *L )
94 {
95     vlc_object_t * p_this;
96     lua_getglobal( L, "vlc" );
97     lua_getfield( L, -1, "private" );
98     p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
99     lua_pop( L, 2 );
100     return p_this;
101 }
102
103 /*****************************************************************************
104  * VLC error code translation
105  *****************************************************************************/
106 int vlclua_push_ret( lua_State *L, int i_error )
107 {
108     lua_pushnumber( L, i_error );
109     lua_pushstring( L, vlc_error( i_error ) );
110     return 2;
111 }
112
113 /*****************************************************************************
114  * Get the VLC version string
115  *****************************************************************************/
116 int vlclua_version( lua_State *L )
117 {
118     lua_pushstring( L, VLC_Version() );
119     return 1;
120 }
121
122 /*****************************************************************************
123  * Get the VLC copyright
124  *****************************************************************************/
125 int vlclua_copyright( lua_State *L )
126 {
127     lua_pushstring( L, COPYRIGHT_MESSAGE );
128     return 1;
129 }
130
131 /*****************************************************************************
132  * Get the VLC license msg/disclaimer
133  *****************************************************************************/
134 int vlclua_license( lua_State *L )
135 {
136     lua_pushstring( L, LICENSE_MSG );
137     return 1;
138 }
139
140 /*****************************************************************************
141  * Quit VLC
142  *****************************************************************************/
143 int vlclua_quit( lua_State *L )
144 {
145     vlc_object_t *p_this = vlclua_get_this( L );
146     /* The rc.c code also stops the playlist ... not sure if this is needed
147      * though. */
148     vlc_object_kill( p_this->p_libvlc );
149     return 0;
150 }
151
152 /*****************************************************************************
153  * Global properties getters
154  *****************************************************************************/
155 int vlclua_datadir( lua_State *L )
156 {
157     lua_pushstring( L, config_GetDataDir() );
158     return 1;
159 }
160 int vlclua_homedir( lua_State *L )
161 {
162     lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
163     return 1;
164 }
165 int vlclua_configdir( lua_State *L )
166 {
167     char *dir = config_GetConfigDir();
168     lua_pushstring( L, dir );
169     free( dir );
170     return 1;
171 }
172 int vlclua_cachedir( lua_State *L )
173 {
174     char *dir = config_GetCacheDir();
175     lua_pushstring( L, dir );
176     free( dir );
177     return 1;
178 }
179 int vlclua_datadir_list( lua_State *L )
180 {
181     const char *psz_dirname = luaL_checkstring( L, 1 );
182     vlc_object_t *p_this = vlclua_get_this( L );
183     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
184     char **ppsz_dir = ppsz_dir_list;
185     int i = 1;
186
187     if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
188         return 0;
189     lua_newtable( L );
190     for( ; *ppsz_dir; ppsz_dir++ )
191     {
192         lua_pushstring( L, *ppsz_dir );
193         lua_rawseti( L, -2, i );
194         i ++;
195     }
196     return 1;
197 }
198
199 /*****************************************************************************
200  * Volume related
201  *****************************************************************************/
202 int vlclua_volume_set( lua_State *L )
203 {
204     vlc_object_t *p_this = vlclua_get_this( L );
205     int i_volume = luaL_checkint( L, 1 );
206     /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
207     return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
208 }
209
210 int vlclua_volume_get( lua_State *L )
211 {
212     vlc_object_t *p_this = vlclua_get_this( L );
213     audio_volume_t i_volume;
214     if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
215         lua_pushnumber( L, i_volume );
216     else
217         lua_pushnil( L );
218     return 1;
219 }
220
221 int vlclua_volume_up( lua_State *L )
222 {
223     audio_volume_t i_volume;
224     aout_VolumeUp( vlclua_get_this( L ),
225                    luaL_optint( L, 1, 1 ),
226                    &i_volume );
227     lua_pushnumber( L, i_volume );
228     return 1;
229 }
230
231 int vlclua_volume_down( lua_State *L )
232 {
233     audio_volume_t i_volume;
234     aout_VolumeDown( vlclua_get_this( L ),
235                      luaL_optint( L, 1, 1 ),
236                      &i_volume );
237     lua_pushnumber( L, i_volume );
238     return 1;
239 }
240
241 /*****************************************************************************
242  * Stream handling
243  *****************************************************************************/
244 int vlclua_stream_new( lua_State *L )
245 {
246     vlc_object_t * p_this = vlclua_get_this( L );
247     stream_t * p_stream;
248     const char * psz_url;
249     psz_url = luaL_checkstring( L, -1 );
250     p_stream = stream_UrlNew( p_this, psz_url );
251     if( !p_stream )
252         return luaL_error( L, "Error when opening url: `%s'", psz_url );
253     lua_pushlightuserdata( L, p_stream );
254     return 1;
255 }
256
257 int vlclua_stream_read( lua_State *L )
258 {
259     stream_t * p_stream;
260     int n;
261     uint8_t *p_read;
262     int i_read;
263     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
264     n = luaL_checkint( L, 2 );
265     p_read = malloc( n );
266     if( !p_read ) return vlclua_error( L );
267     i_read = stream_Read( p_stream, p_read, n );
268     lua_pushlstring( L, (const char *)p_read, i_read );
269     free( p_read );
270     return 1;
271 }
272
273 int vlclua_stream_readline( lua_State *L )
274 {
275     stream_t * p_stream;
276     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
277     char *psz_line = stream_ReadLine( p_stream );
278     if( psz_line )
279     {
280         lua_pushstring( L, psz_line );
281         free( psz_line );
282     }
283     else
284         lua_pushnil( L );
285     return 1;
286 }
287
288 int vlclua_stream_delete( lua_State *L )
289 {
290     stream_t * p_stream;
291     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
292     stream_Delete( p_stream );
293     return 0;
294 }
295
296 /*****************************************************************************
297  * String transformations
298  *****************************************************************************/
299 int vlclua_decode_uri( lua_State *L )
300 {
301     int i_top = lua_gettop( L );
302     int i;
303     for( i = 1; i <= i_top; i++ )
304     {
305         const char *psz_cstring = luaL_checkstring( L, 1 );
306         char *psz_string = strdup( psz_cstring );
307         lua_remove( L, 1 ); /* remove elements to prevent being limited by
308                              * the stack's size (this function will work with
309                              * up to (stack size - 1) arguments */
310         decode_URI( psz_string );
311         lua_pushstring( L, psz_string );
312         free( psz_string );
313     }
314     return i_top;
315 }
316
317 int vlclua_resolve_xml_special_chars( lua_State *L )
318 {
319     int i_top = lua_gettop( L );
320     int i;
321     for( i = 1; i <= i_top; i++ )
322     {
323         const char *psz_cstring = luaL_checkstring( L, 1 );
324         char *psz_string = strdup( psz_cstring );
325         lua_remove( L, 1 ); /* remove elements to prevent being limited by
326                              * the stack's size (this function will work with
327                              * up to (stack size - 1) arguments */
328         resolve_xml_special_chars( psz_string );
329         lua_pushstring( L, psz_string );
330         free( psz_string );
331     }
332     return i_top;
333 }
334
335 int vlclua_convert_xml_special_chars( lua_State *L )
336 {
337     int i_top = lua_gettop( L );
338     int i;
339     for( i = 1; i <= i_top; i++ )
340     {
341         char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
342         lua_remove( L, 1 );
343         lua_pushstring( L, psz_string );
344         free( psz_string );
345     }
346     return i_top;
347 }
348
349 /*****************************************************************************
350  * Messaging facilities
351  *****************************************************************************/
352 int vlclua_msg_dbg( lua_State *L )
353 {
354     int i_top = lua_gettop( L );
355     vlc_object_t *p_this = vlclua_get_this( L );
356     int i;
357     for( i = 1; i <= i_top; i++ )
358         msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
359     return 0;
360 }
361 int vlclua_msg_warn( lua_State *L )
362 {
363     int i_top = lua_gettop( L );
364     vlc_object_t *p_this = vlclua_get_this( L );
365     int i;
366     for( i = 1; i <= i_top; i++ )
367         msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
368     return 0;
369 }
370 int vlclua_msg_err( lua_State *L )
371 {
372     int i_top = lua_gettop( L );
373     vlc_object_t *p_this = vlclua_get_this( L );
374     int i;
375     for( i = 1; i <= i_top; i++ )
376         msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
377     return 0;
378 }
379 int vlclua_msg_info( lua_State *L )
380 {
381     int i_top = lua_gettop( L );
382     vlc_object_t *p_this = vlclua_get_this( L );
383     int i;
384     for( i = 1; i <= i_top; i++ )
385         msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
386     return 0;
387 }
388
389 /*****************************************************************************
390  *
391  *****************************************************************************/
392 static int file_select( const char *file )
393 {
394     int i = strlen( file );
395     return i > 4 && !strcmp( file+i-4, ".lua" );
396 }
397
398 static int file_compare( const char **a, const char **b )
399 {
400     return strcmp( *a, *b );
401 }
402
403 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
404                      char **ppsz_dir_list )
405 {
406     int i = 0;
407     char *datadir = config_GetUserDataDir();
408     if( datadir == NULL )
409         return VLC_ENOMEM;
410
411     if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
412                    datadir, luadirname ) < 0 )
413     {
414         free( datadir );
415         return VLC_ENOMEM;
416     }
417     free( datadir );
418     i++;
419
420 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
421     {
422         const char *psz_vlcpath = config_GetDataDir();
423         if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
424                       psz_vlcpath, luadirname )  < 0 )
425             return VLC_ENOMEM;
426         i++;
427 #       ifdef WIN32
428         if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
429                       psz_vlcpath, luadirname )  < 0 )
430             return VLC_ENOMEM;
431         i++;
432 #       else
433          if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
434                       psz_vlcpath, luadirname )  < 0 )
435             return VLC_ENOMEM;
436         i++;
437 #       endif
438
439     }
440 #   else
441     if( asprintf( &ppsz_dir_list[i],
442                   "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
443         return VLC_ENOMEM;
444
445 #   ifdef HAVE_SYS_STAT_H
446     {
447         struct stat stat_info;
448         if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
449             || !S_ISDIR( stat_info.st_mode ) )
450         {
451             free(ppsz_dir_list[i]);
452             if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
453                           config_GetDataDir (), luadirname ) < 0 )
454                 return VLC_ENOMEM;
455         }
456     }
457 #   endif
458     i++;
459 #   endif
460     return VLC_SUCCESS;
461 }
462
463 /*****************************************************************************
464  * Will execute func on all scripts in luadirname, and stop if func returns
465  * success.
466  *****************************************************************************/
467 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
468                                   const char * luadirname,
469                                   int (*func)(vlc_object_t *, const char *, lua_State *, void *),
470                                   lua_State * L,
471                                   void * user_data)
472 {
473     int i_ret = VLC_EGENERIC;
474
475     char **ppsz_filelist = NULL;
476     char **ppsz_fileend  = NULL;
477     char **ppsz_file;
478
479     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
480     char **ppsz_dir;
481
482     i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
483     if( i_ret != VLC_SUCCESS )
484         return i_ret;
485     i_ret = VLC_EGENERIC;
486
487
488     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
489     {
490         int i_files;
491
492         if( ppsz_filelist )
493         {
494             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
495                  ppsz_file++ )
496                 free( *ppsz_file );
497             free( ppsz_filelist );
498             ppsz_filelist = NULL;
499         }
500
501         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
502         i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
503                                 file_compare );
504         if( i_files < 1 ) continue;
505         ppsz_fileend = ppsz_filelist + i_files;
506
507         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
508         {
509             char  *psz_filename;
510             if( asprintf( &psz_filename,
511                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
512                 return VLC_ENOMEM;
513             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
514
515             i_ret = func( p_this, psz_filename, L, user_data );
516
517             free( psz_filename );
518
519             if( i_ret == VLC_SUCCESS ) break;
520         }
521         if( i_ret == VLC_SUCCESS ) break;
522     }
523
524     if( ppsz_filelist )
525     {
526         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
527              ppsz_file++ )
528             free( *ppsz_file );
529         free( ppsz_filelist );
530     }
531     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
532         free( *ppsz_dir );
533
534     return i_ret;
535 }
536
537
538 /*****************************************************************************
539  * Meta data setters utility.
540  * Playlist item table should be on top of the stack when these are called
541  *****************************************************************************/
542 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
543                               input_item_t *p_input )
544 {
545 #define TRY_META( a, b )                                        \
546     lua_getfield( L, -1, a );                                   \
547     if( lua_isstring( L, -1 ) )                                 \
548     {                                                           \
549         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
550         EnsureUTF8( psz_value );                                \
551         msg_Dbg( p_this, #b ": %s", psz_value );                \
552         input_item_Set ## b ( p_input, psz_value );             \
553         free( psz_value );                                      \
554     }                                                           \
555     lua_pop( L, 1 ); /* pop a */
556     TRY_META( "title", Title );
557     TRY_META( "artist", Artist );
558     TRY_META( "genre", Genre );
559     TRY_META( "copyright", Copyright );
560     TRY_META( "album", Album );
561     TRY_META( "tracknum", TrackNum );
562     TRY_META( "description", Description );
563     TRY_META( "rating", Rating );
564     TRY_META( "date", Date );
565     TRY_META( "setting", Setting );
566     TRY_META( "url", URL );
567     TRY_META( "language", Language );
568     TRY_META( "nowplaying", NowPlaying );
569     TRY_META( "publisher", Publisher );
570     TRY_META( "encodedby", EncodedBy );
571     TRY_META( "arturl", ArtURL );
572     TRY_META( "trackid", TrackID );
573 }
574
575 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
576                                      input_item_t *p_input )
577 {
578     /* ... item */
579     lua_getfield( L, -1, "meta" );
580     /* ... item meta */
581     if( lua_istable( L, -1 ) )
582     {
583         lua_pushnil( L );
584         /* ... item meta nil */
585         while( lua_next( L, -2 ) )
586         {
587             /* ... item meta key value */
588             if( !lua_isstring( L, -2 ) )
589             {
590                 msg_Warn( p_this, "Custom meta data category name must be "
591                                    "a string" );
592             }
593             else if( !lua_istable( L, -1 ) )
594             {
595                 msg_Warn( p_this, "Custom meta data category contents "
596                                    "must be a table" );
597             }
598             else
599             {
600                 const char *psz_meta_category = lua_tostring( L, -2 );
601                 msg_Dbg( p_this, "Found custom meta data category: %s",
602                          psz_meta_category );
603                 lua_pushnil( L );
604                 /* ... item meta key value nil */
605                 while( lua_next( L, -2 ) )
606                 {
607                     /* ... item meta key value key2 value2 */
608                     if( !lua_isstring( L, -2 ) )
609                     {
610                         msg_Warn( p_this, "Custom meta category item name "
611                                            "must be a string." );
612                     }
613                     else if( !lua_isstring( L, -1 ) )
614                     {
615                         msg_Warn( p_this, "Custom meta category item value "
616                                            "must be a string." );
617                     }
618                     else
619                     {
620                         const char *psz_meta_name =
621                             lua_tostring( L, -2 );
622                         const char *psz_meta_value =
623                             lua_tostring( L, -1 );
624                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
625                                  psz_meta_category, psz_meta_name,
626                                  psz_meta_value );
627                         input_ItemAddInfo( p_input, psz_meta_category,
628                                            psz_meta_name, psz_meta_value );
629                     }
630                     lua_pop( L, 1 ); /* pop item */
631                     /* ... item meta key value key2 */
632                 }
633                 /* ... item meta key value */
634             }
635             lua_pop( L, 1 ); /* pop category */
636             /* ... item meta key */
637         }
638         /* ... item meta */
639     }
640     lua_pop( L, 1 ); /* pop "meta" */
641     /* ... item -> back to original stack */
642 }
643
644 /*****************************************************************************
645  * Playlist utilities
646  ****************************************************************************/
647 /**
648  * Playlist item table should be on top of the stack when this is called
649  */
650 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
651                             int *pi_options, char ***pppsz_options )
652 {
653     lua_getfield( L, -1, "options" );
654     if( lua_istable( L, -1 ) )
655     {
656         lua_pushnil( L );
657         while( lua_next( L, -2 ) )
658         {
659             if( lua_isstring( L, -1 ) )
660             {
661                 char *psz_option = strdup( lua_tostring( L, -1 ) );
662                 msg_Dbg( p_this, "Option: %s", psz_option );
663                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
664                              psz_option );
665             }
666             else
667             {
668                 msg_Warn( p_this, "Option should be a string" );
669             }
670             lua_pop( L, 1 ); /* pop option */
671         }
672     }
673     lua_pop( L, 1 ); /* pop "options" */
674 }
675
676 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
677                                     playlist_t *p_playlist,
678                                     input_item_t *p_parent, bool b_play )
679 {
680     int i_count = 0;
681
682     /* playlist */
683     if( lua_istable( L, -1 ) )
684     {
685         lua_pushnil( L );
686         /* playlist nil */
687         while( lua_next( L, -2 ) )
688         {
689             /* playlist key item */
690             /* <Parse playlist item> */
691             if( lua_istable( L, -1 ) )
692             {
693                 lua_getfield( L, -1, "path" );
694                 /* playlist key item path */
695                 if( lua_isstring( L, -1 ) )
696                 {
697                     const char   *psz_path     = NULL;
698                     const char   *psz_name     = NULL;
699                     char        **ppsz_options = NULL;
700                     int           i_options    = 0;
701                     mtime_t       i_duration   = -1;
702                     input_item_t *p_input;
703
704                     /* Read path and name */
705                     psz_path = lua_tostring( L, -1 );
706                     msg_Dbg( p_this, "Path: %s", psz_path );
707                     lua_getfield( L, -2, "name" );
708                     /* playlist key item path name */
709                     if( lua_isstring( L, -1 ) )
710                     {
711                         psz_name = lua_tostring( L, -1 );
712                         msg_Dbg( p_this, "Name: %s", psz_name );
713                     }
714                     else
715                     {
716                         if( !lua_isnil( L, -1 ) )
717                             msg_Warn( p_this, "Playlist item name should be a string." );
718                         psz_name = psz_path;
719                     }
720
721                     /* Read duration */
722                     lua_getfield( L, -3, "duration" );
723                     /* playlist key item path name duration */
724                     if( lua_isnumber( L, -1 ) )
725                     {
726                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
727                     }
728                     else if( !lua_isnil( L, -1 ) )
729                     {
730                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
731                     }
732                     lua_pop( L, 1 ); /* pop "duration" */
733
734                     /* playlist key item path name */
735
736                     /* Read options: item must be on top of stack */
737                     lua_pushvalue( L, -3 );
738                     /* playlist key item path name item */
739                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
740
741                     /* Create input item */
742                     p_input = input_ItemNewExt( p_playlist, psz_path,
743                                                 psz_name, i_options,
744                                                 (const char **)ppsz_options,
745                                                 i_duration );
746                     lua_pop( L, 3 ); /* pop "path name item" */
747                     /* playlist key item */
748
749                     /* Read meta data: item must be on top of stack */
750                     vlclua_read_meta_data( p_this, L, p_input );
751
752                     /* Read custom meta data: item must be on top of stack*/
753                     vlclua_read_custom_meta_data( p_this, L, p_input );
754
755                     /* Append item to playlist */
756                     if( p_parent ) /* Add to node */
757                         input_ItemAddSubItem( p_parent, p_input );
758                     else /* Play or Enqueue (preparse) */
759                         /* FIXME: playlist_AddInput() can fail */
760                         playlist_AddInput( p_playlist, p_input,
761                                PLAYLIST_APPEND | 
762                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
763                                PLAYLIST_END, true, false );
764                     i_count ++; /* increment counter */
765                     vlc_gc_decref( p_input );
766                     while( i_options > 0 )
767                         free( ppsz_options[--i_options] );
768                     free( ppsz_options );
769                 }
770                 else
771                 {
772                     lua_pop( L, 1 ); /* pop "path" */
773                     msg_Warn( p_this,
774                              "Playlist item's path should be a string" );
775                 }
776                 /* playlist key item */
777             }
778             else
779             {
780                 msg_Warn( p_this, "Playlist item should be a table" );
781             }
782             /* <Parse playlist item> */
783             lua_pop( L, 1 ); /* pop the value, keep the key for
784                               * the next lua_next() call */
785             /* playlist key */
786         }
787         /* playlist */
788     }
789     else
790     {
791         msg_Warn( p_this, "Playlist should be a table." );
792     }
793     return i_count;
794 }