vlc.c 26.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*****************************************************************************
 * vlc.c: Generic lua inteface functions
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
 *          Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

32
33
34
35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vlc/vlc.h>
#include <vlc_meta.h>
#include <vlc_charset.h>
#include <vlc_aout.h>

#include <lua.h>        /* Low level lua C API */
#include <lauxlib.h>    /* Higher level C API */
#include <lualib.h>     /* Lua libs */

#include "vlc.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

#define INTF_TEXT N_("Lua interface")
#define INTF_LONGTEXT N_("Lua interface module to load")

#define CONFIG_TEXT N_("Lua inteface configuration")
#define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")

vlc_module_begin();
58
59
60
61
62
63
    add_submodule();
        add_shortcut( "luameta" );
        set_shortname( N_( "Lua Meta" ) );
        set_description( _("Fetch metadata using lua scripts") );
        set_capability( "meta fetcher", 10 );
        set_callbacks( E_(FindMeta), NULL );
64
65
66
67
68
69
70
71
72
73
74
    add_submodule();
        set_shortname( N_( "Lua Art" ) );
        set_description( _("Fetch artwork using lua scripts") );
        set_capability( "art finder", 10 );
        set_callbacks( E_(FindArt), NULL );
    add_submodule();
        add_shortcut( "luaplaylist" );
        set_category( CAT_INPUT );
        set_subcategory( SUBCAT_INPUT_DEMUX );
        set_shortname( _("Lua Playlist") );
        set_description( _("Lua Playlist Parser Interface") );
75
        set_capability( "demux2", 2 );
76
77
78
79
80
81
82
83
84
        set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
    add_submodule();
        add_shortcut( "luaintf" );
        add_shortcut( "luarc" );
        /* add_shortcut( "rc" ); */
        add_shortcut( "luahotkeys" );
        /* add_shortcut( "hotkeys" ); */
        add_shortcut( "luatelnet" );
        /* add_shortcut( "telnet" ); */
85
86
        add_shortcut( "luahttp" );
        /* add_shortcut( "http" ); */
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        set_description( _("Lua Interface Module") );
        set_capability( "interface", 0 );
        add_string( "lua-intf", "dummy", NULL,
                    INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
        add_string( "lua-config", "", NULL,
                    CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
        set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
vlc_module_end();

/*****************************************************************************
 * Internal lua<->vlc utils
 *****************************************************************************/
vlc_object_t * vlclua_get_this( lua_State *L )
{
    vlc_object_t * p_this;
    lua_getglobal( L, "vlc" );
    lua_getfield( L, -1, "private" );
    p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
    lua_pop( L, 2 );
    return p_this;
}

/*****************************************************************************
 * VLC error code translation
 *****************************************************************************/
int vlclua_push_ret( lua_State *L, int i_error )
{
    lua_pushnumber( L, i_error );
    lua_pushstring( L, vlc_error( i_error ) );
    return 2;
}

/*****************************************************************************
 * Get the VLC version string
 *****************************************************************************/
int vlclua_version( lua_State *L )
{
    lua_pushstring( L, VLC_Version() );
    return 1;
}

128
129
130
131
132
133
134
135
136
/*****************************************************************************
 * Get the VLC copyright
 *****************************************************************************/
int vlclua_copyright( lua_State *L )
{
    lua_pushstring( L, COPYRIGHT_MESSAGE );
    return 1;
}

dionoea's avatar
dionoea committed
137
138
139
140
141
142
143
144
145
/*****************************************************************************
 * Get the VLC license msg/disclaimer
 *****************************************************************************/
int vlclua_license( lua_State *L )
{
    lua_pushstring( L, LICENSE_MSG );
    return 1;
}

146
147
148
149
150
151
152
153
154
155
156
157
/*****************************************************************************
 * Quit VLC
 *****************************************************************************/
int vlclua_quit( lua_State *L )
{
    vlc_object_t *p_this = vlclua_get_this( L );
    /* The rc.c code also stops the playlist ... not sure if this is needed
     * though. */
    vlc_object_kill( p_this->p_libvlc );
    return 0;
}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
 * Global properties getters
 *****************************************************************************/
int vlclua_datadir( lua_State *L )
{
    lua_pushstring( L, config_GetDataDir() );
    return 1;
}
int vlclua_homedir( lua_State *L )
{
    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
    return 1;
}
int vlclua_configdir( lua_State *L )
{
    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
    return 1;
}
int vlclua_cachedir( lua_State *L )
{
    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
    return 1;
}
int vlclua_datadir_list( lua_State *L )
{
    const char *psz_dirname = luaL_checkstring( L, 1 );
    vlc_object_t *p_this = vlclua_get_this( L );
    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
    char **ppsz_dir = ppsz_dir_list;
    int i = 1;

    if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
        return 0;
    lua_newtable( L );
    for( ; *ppsz_dir; ppsz_dir++ )
    {
        lua_pushstring( L, *ppsz_dir );
        lua_rawseti( L, -2, i );
        i ++;
    }
    return 1;
}

201
202
203
204
205
206
/*****************************************************************************
 * Volume related
 *****************************************************************************/
int vlclua_volume_set( lua_State *L )
{
    vlc_object_t *p_this = vlclua_get_this( L );
207
    int i_volume = luaL_checkint( L, 1 );
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
    return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
}

int vlclua_volume_get( lua_State *L )
{
    vlc_object_t *p_this = vlclua_get_this( L );
    audio_volume_t i_volume;
    if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
        lua_pushnumber( L, i_volume );
    else
        lua_pushnil( L );
    return 1;
}

int vlclua_volume_up( lua_State *L )
{
    audio_volume_t i_volume;
    aout_VolumeUp( vlclua_get_this( L ),
                   luaL_optint( L, 1, 1 ),
                   &i_volume );
    lua_pushnumber( L, i_volume );
    return 1;
}

int vlclua_volume_down( lua_State *L )
{
    audio_volume_t i_volume;
    aout_VolumeDown( vlclua_get_this( L ),
                     luaL_optint( L, 1, 1 ),
                     &i_volume );
    lua_pushnumber( L, i_volume );
    return 1;
}

/*****************************************************************************
 * Stream handling
 *****************************************************************************/
int vlclua_stream_new( lua_State *L )
{
    vlc_object_t * p_this = vlclua_get_this( L );
    stream_t * p_stream;
    const char * psz_url;
    psz_url = luaL_checkstring( L, -1 );
    p_stream = stream_UrlNew( p_this, psz_url );
253
254
    if( !p_stream )
        return luaL_error( L, "Error when opening url: `%s'", psz_url );
255
256
257
258
259
260
261
262
263
264
    lua_pushlightuserdata( L, p_stream );
    return 1;
}

int vlclua_stream_read( lua_State *L )
{
    stream_t * p_stream;
    int n;
    byte_t *p_read;
    int i_read;
265
266
    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
    n = luaL_checkint( L, 2 );
267
268
269
270
271
272
273
274
275
276
277
    p_read = malloc( n );
    if( !p_read ) return vlclua_error( L );
    i_read = stream_Read( p_stream, p_read, n );
    lua_pushlstring( L, (const char *)p_read, i_read );
    free( p_read );
    return 1;
}

int vlclua_stream_readline( lua_State *L )
{
    stream_t * p_stream;
278
    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    char *psz_line = stream_ReadLine( p_stream );
    if( psz_line )
    {
        lua_pushstring( L, psz_line );
        free( psz_line );
    }
    else
        lua_pushnil( L );
    return 1;
}

int vlclua_stream_delete( lua_State *L )
{
    stream_t * p_stream;
293
    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
294
    stream_Delete( p_stream );
295
    return 0;
296
297
298
299
300
301
302
}

/*****************************************************************************
 * String transformations
 *****************************************************************************/
int vlclua_decode_uri( lua_State *L )
{
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    int i_top = lua_gettop( L );
    int i;
    for( i = 1; i <= i_top; i++ )
    {
        const char *psz_cstring = luaL_checkstring( L, 1 );
        char *psz_string = strdup( psz_cstring );
        lua_remove( L, 1 ); /* remove elements to prevent being limited by
                             * the stack's size (this function will work with
                             * up to (stack size - 1) arguments */
        decode_URI( psz_string );
        lua_pushstring( L, psz_string );
        free( psz_string );
    }
    return i_top;
317
318
319
320
}

int vlclua_resolve_xml_special_chars( lua_State *L )
{
321
322
323
324
325
326
327
328
329
330
331
332
333
334
    int i_top = lua_gettop( L );
    int i;
    for( i = 1; i <= i_top; i++ )
    {
        const char *psz_cstring = luaL_checkstring( L, 1 );
        char *psz_string = strdup( psz_cstring );
        lua_remove( L, 1 ); /* remove elements to prevent being limited by
                             * the stack's size (this function will work with
                             * up to (stack size - 1) arguments */
        resolve_xml_special_chars( psz_string );
        lua_pushstring( L, psz_string );
        free( psz_string );
    }
    return i_top;
335
336
}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
int vlclua_convert_xml_special_chars( lua_State *L )
{
    int i_top = lua_gettop( L );
    int i;
    for( i = 1; i <= i_top; i++ )
    {
        char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
        lua_remove( L, 1 );
        lua_pushstring( L, psz_string );
        free( psz_string );
    }
    return i_top;
}

351
352
353
354
355
/*****************************************************************************
 * Messaging facilities
 *****************************************************************************/
int vlclua_msg_dbg( lua_State *L )
{
356
    int i_top = lua_gettop( L );
357
    vlc_object_t *p_this = vlclua_get_this( L );
358
359
360
    int i;
    for( i = 1; i <= i_top; i++ )
        msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
361
362
363
364
    return 0;
}
int vlclua_msg_warn( lua_State *L )
{
365
    int i_top = lua_gettop( L );
366
    vlc_object_t *p_this = vlclua_get_this( L );
367
368
369
    int i;
    for( i = 1; i <= i_top; i++ )
        msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
370
371
372
373
    return 0;
}
int vlclua_msg_err( lua_State *L )
{
374
    int i_top = lua_gettop( L );
375
    vlc_object_t *p_this = vlclua_get_this( L );
376
377
378
    int i;
    for( i = 1; i <= i_top; i++ )
        msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
379
380
381
382
    return 0;
}
int vlclua_msg_info( lua_State *L )
{
383
    int i_top = lua_gettop( L );
384
    vlc_object_t *p_this = vlclua_get_this( L );
385
386
387
    int i;
    for( i = 1; i <= i_top; i++ )
        msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    return 0;
}

/*****************************************************************************
 *
 *****************************************************************************/
static int file_select( const char *file )
{
    int i = strlen( file );
    return i > 4 && !strcmp( file+i-4, ".lua" );
}

static int file_compare( const char **a, const char **b )
{
    return strcmp( *a, *b );
}

int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
                     char **ppsz_dir_list )
{
    if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
                   p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
        return VLC_ENOMEM;

#   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
    {
        const char *psz_vlcpath = config_GetDataDir();
        if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
                      psz_vlcpath, luadirname )  < 0 )
            return VLC_ENOMEM;

        if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
                      psz_vlcpath, luadirname )  < 0 )
            return VLC_ENOMEM;
    }
#   else
    if( asprintf( &ppsz_dir_list[1],
                  "share" DIR_SEP "%s", luadirname ) < 0 )
        return VLC_ENOMEM;

#   ifdef HAVE_SYS_STAT_H
    {
        struct stat stat_info;
        if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
            || !S_ISDIR( stat_info.st_mode ) )
        {
            free(ppsz_dir_list[1]);
435
436
            if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
                          config_GetDataDir (), luadirname ) < 0 )
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
                return VLC_ENOMEM;
        }
    }
#   endif
#   endif
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Will execute func on all scripts in luadirname, and stop if func returns
 * success.
 *****************************************************************************/
int vlclua_scripts_batch_execute( vlc_object_t *p_this,
                                  const char * luadirname,
                                  int (*func)(vlc_object_t *, const char *, lua_State *, void *),
                                  lua_State * L,
                                  void * user_data)
{
    int i_ret = VLC_EGENERIC;

    DIR   *dir           = NULL;
    char **ppsz_filelist = NULL;
    char **ppsz_fileend  = NULL;
    char **ppsz_file;

    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
    char **ppsz_dir;

    i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
    if( i_ret != VLC_SUCCESS )
        return i_ret;
    i_ret = VLC_EGENERIC;


    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
    {
        int i_files;

        if( ppsz_filelist )
        {
            for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
                 ppsz_file++ )
                free( *ppsz_file );
            free( ppsz_filelist );
            ppsz_filelist = NULL;
        }

        if( dir )
        {
            closedir( dir );
        }

        msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
        dir = utf8_opendir( *ppsz_dir );

        if( !dir ) continue;
        i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
                                file_compare );
        if( i_files < 1 ) continue;
        ppsz_fileend = ppsz_filelist + i_files;

        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
        {
            char  *psz_filename;
            if( asprintf( &psz_filename,
                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
                return VLC_ENOMEM;
            msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );

            i_ret = func( p_this, psz_filename, L, user_data );

            free( psz_filename );

            if( i_ret == VLC_SUCCESS ) break;
        }
        if( i_ret == VLC_SUCCESS ) break;
    }

    if( ppsz_filelist )
    {
        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
             ppsz_file++ )
            free( *ppsz_file );
        free( ppsz_filelist );
    }
    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
        free( *ppsz_dir );

    if( dir ) closedir( dir );

    return i_ret;
}


/*****************************************************************************
 * Meta data setters utility.
 * Playlist item table should be on top of the stack when these are called
 *****************************************************************************/
void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
                              input_item_t *p_input )
{
#define TRY_META( a, b )                                        \
    lua_getfield( L, -1, a );                                   \
    if( lua_isstring( L, -1 ) )                                 \
    {                                                           \
        char *psz_value = strdup( lua_tostring( L, -1 ) );      \
        EnsureUTF8( psz_value );                                \
        msg_Dbg( p_this, #b ": %s", psz_value );                \
        input_item_Set ## b ( p_input, psz_value );             \
        free( psz_value );                                      \
    }                                                           \
    lua_pop( L, 1 ); /* pop a */
    TRY_META( "title", Title );
    TRY_META( "artist", Artist );
    TRY_META( "genre", Genre );
    TRY_META( "copyright", Copyright );
    TRY_META( "album", Album );
    TRY_META( "tracknum", TrackNum );
    TRY_META( "description", Description );
    TRY_META( "rating", Rating );
    TRY_META( "date", Date );
    TRY_META( "setting", Setting );
    TRY_META( "url", URL );
    TRY_META( "language", Language );
    TRY_META( "nowplaying", NowPlaying );
    TRY_META( "publisher", Publisher );
    TRY_META( "encodedby", EncodedBy );
    TRY_META( "arturl", ArtURL );
    TRY_META( "trackid", TrackID );
}

void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
                                     input_item_t *p_input )
{
    /* ... item */
    lua_getfield( L, -1, "meta" );
    /* ... item meta */
    if( lua_istable( L, -1 ) )
    {
        lua_pushnil( L );
        /* ... item meta nil */
        while( lua_next( L, -2 ) )
        {
            /* ... item meta key value */
            if( !lua_isstring( L, -2 ) )
            {
                msg_Warn( p_this, "Custom meta data category name must be "
                                   "a string" );
            }
            else if( !lua_istable( L, -1 ) )
            {
                msg_Warn( p_this, "Custom meta data category contents "
                                   "must be a table" );
            }
            else
            {
                const char *psz_meta_category = lua_tostring( L, -2 );
                msg_Dbg( p_this, "Found custom meta data category: %s",
                         psz_meta_category );
                lua_pushnil( L );
                /* ... item meta key value nil */
                while( lua_next( L, -2 ) )
                {
                    /* ... item meta key value key2 value2 */
                    if( !lua_isstring( L, -2 ) )
                    {
                        msg_Warn( p_this, "Custom meta category item name "
                                           "must be a string." );
                    }
                    else if( !lua_isstring( L, -1 ) )
                    {
                        msg_Warn( p_this, "Custom meta category item value "
                                           "must be a string." );
                    }
                    else
                    {
                        const char *psz_meta_name =
                            lua_tostring( L, -2 );
                        const char *psz_meta_value =
                            lua_tostring( L, -1 );
                        msg_Dbg( p_this, "Custom meta %s, %s: %s",
                                 psz_meta_category, psz_meta_name,
                                 psz_meta_value );
                        input_ItemAddInfo( p_input, psz_meta_category,
                                           psz_meta_name, psz_meta_value );
                    }
                    lua_pop( L, 1 ); /* pop item */
                    /* ... item meta key value key2 */
                }
                /* ... item meta key value */
            }
            lua_pop( L, 1 ); /* pop category */
            /* ... item meta key */
        }
        /* ... item meta */
    }
    lua_pop( L, 1 ); /* pop "meta" */
    /* ... item -> back to original stack */
}

/*****************************************************************************
 * Playlist utilities
 ****************************************************************************/
/**
 * Playlist item table should be on top of the stack when this is called
 */
void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
                            int *pi_options, char ***pppsz_options )
{
    lua_getfield( L, -1, "options" );
    if( lua_istable( L, -1 ) )
    {
        lua_pushnil( L );
        while( lua_next( L, -2 ) )
        {
            if( lua_isstring( L, -1 ) )
            {
                char *psz_option = strdup( lua_tostring( L, -1 ) );
                msg_Dbg( p_this, "Option: %s", psz_option );
                INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
                             psz_option );
            }
            else
            {
                msg_Warn( p_this, "Option should be a string" );
            }
            lua_pop( L, 1 ); /* pop option */
        }
    }
    lua_pop( L, 1 ); /* pop "options" */
}

int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                                    playlist_t *p_playlist,
                                    input_item_t *p_parent, vlc_bool_t b_play )
{
    int i_count = 0;

    /* playlist */
    if( lua_istable( L, -1 ) )
    {
        lua_pushnil( L );
        /* playlist nil */
        while( lua_next( L, -2 ) )
        {
            /* playlist key item */
            /* <Parse playlist item> */
            if( lua_istable( L, -1 ) )
            {
                lua_getfield( L, -1, "path" );
                /* playlist key item path */
                if( lua_isstring( L, -1 ) )
                {
                    const char   *psz_path     = NULL;
                    const char   *psz_name     = NULL;
                    char        **ppsz_options = NULL;
                    int           i_options    = 0;
                    mtime_t       i_duration   = -1;
                    input_item_t *p_input;

                    /* Read path and name */
                    psz_path = lua_tostring( L, -1 );
                    msg_Dbg( p_this, "Path: %s", psz_path );
                    lua_getfield( L, -2, "name" );
                    /* playlist key item path name */
                    if( lua_isstring( L, -1 ) )
                    {
                        psz_name = lua_tostring( L, -1 );
                        msg_Dbg( p_this, "Name: %s", psz_name );
                    }
                    else
                    {
                        if( !lua_isnil( L, -1 ) )
                            msg_Warn( p_this, "Playlist item name should be a string." );
                        psz_name = psz_path;
                    }

                    /* Read duration */
                    lua_getfield( L, -3, "duration" );
                    /* playlist key item path name duration */
                    if( lua_isnumber( L, -1 ) )
                    {
                        i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
                    }
                    else if( !lua_isnil( L, -1 ) )
                    {
                        msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
                    }
                    lua_pop( L, 1 ); /* pop "duration" */

                    /* playlist key item path name */

                    /* Read options: item must be on top of stack */
                    lua_pushvalue( L, -3 );
                    /* playlist key item path name item */
                    vlclua_read_options( p_this, L, &i_options, &ppsz_options );

                    /* Create input item */
                    p_input = input_ItemNewExt( p_playlist, psz_path,
                                                psz_name, i_options,
                                                (const char **)ppsz_options,
                                                i_duration );
                    lua_pop( L, 3 ); /* pop "path name item" */
                    /* playlist key item */

                    /* Read meta data: item must be on top of stack */
                    vlclua_read_meta_data( p_this, L, p_input );

                    /* Read custom meta data: item must be on top of stack*/
                    vlclua_read_custom_meta_data( p_this, L, p_input );

                    /* Append item to playlist */
                    if( p_parent ) /* Add to node */
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
750
                        input_ItemAddSubItem( p_parent, p_input );
751
752
753
754
755
756
757
758
759
                    else if( b_play ) /* Play */
                        playlist_AddInput( p_playlist, p_input,
                                           PLAYLIST_APPEND | PLAYLIST_GO,
                                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
                    else /* Enqueue */
                        playlist_AddInput( p_playlist, p_input,
                                           PLAYLIST_APPEND | PLAYLIST_PREPARSE,
                                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
                    i_count ++; /* increment counter */
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
760
                    vlc_gc_decref( p_input );
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
                    while( i_options > 0 )
                        free( ppsz_options[--i_options] );
                    free( ppsz_options );
                }
                else
                {
                    lua_pop( L, 1 ); /* pop "path" */
                    msg_Warn( p_this,
                             "Playlist item's path should be a string" );
                }
                /* playlist key item */
            }
            else
            {
                msg_Warn( p_this, "Playlist item should be a table" );
            }
            /* <Parse playlist item> */
            lua_pop( L, 1 ); /* pop the value, keep the key for
                              * the next lua_next() call */
            /* playlist key */
        }
        /* playlist */
    }
    else
    {
        msg_Warn( p_this, "Playlist should be a table." );
    }
    return i_count;
}