rc.c 73.2 KB
Newer Older
1
/*****************************************************************************
Anil Daoud's avatar
Anil Daoud committed
2
 * rc.c : remote control stdin/stdout module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2004-2007 the VideoLAN team
5
 * $Id$
6
 *
7
 * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
8
 *         Jean-Paul Saman <jpsaman #_at_# m2x _replaceWith#dot_ nl>
9 10 11 12 13
 *
 * 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.
14
 *
15 16 17 18 19 20 21
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34

35 36
#include <errno.h>                                                 /* ENOMEM */
#include <ctype.h>
37
#include <signal.h>
38

Clément Stenac's avatar
Clément Stenac committed
39 40 41
#include <vlc_interface.h>
#include <vlc_aout.h>
#include <vlc_vout.h>
42
#include <vlc_osd.h>
Clément Stenac's avatar
Clément Stenac committed
43
#include <vlc_playlist.h>
44 45 46 47 48 49 50 51 52 53

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#include <sys/types.h>

Clément Stenac's avatar
Clément Stenac committed
54
#include <vlc_network.h>
55
#include "vlc_url.h"
56

Clément Stenac's avatar
Clément Stenac committed
57
#include <vlc_charset.h>
58

59 60 61 62
#if defined(AF_UNIX) && !defined(AF_LOCAL)
#    define AF_LOCAL AF_UNIX
#endif

63
#if defined(AF_LOCAL) && ! defined(WIN32)
64 65
#    include <sys/un.h>
#endif
66

67
#define MAX_LINE_LENGTH 256
68
#define STATUS_CHANGE "status change: "
69

70 71
static const char *ppsz_input_state[] = { N_("Initializing"), N_("Opening"), N_("Buffer"), N_("Play"), N_("Pause"), N_("Stop"), N_("Error") };

72 73 74 75
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate     ( vlc_object_t * );
76 77 78
static void Deactivate   ( vlc_object_t * );
static void Run          ( intf_thread_t * );

79
static void Help         ( intf_thread_t *, bool );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
80
static void RegisterCallbacks( intf_thread_t * );
81

82
static bool ReadCommand( intf_thread_t *, char *, int * );
83

84
static input_item_t *parse_MRL( intf_thread_t *, char * );
85

86 87
static int  Input        ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
Gildas Bazin's avatar
 
Gildas Bazin committed
88 89 90 91 92 93 94 95 96 97
static int  Playlist     ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  Quit         ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  Intf         ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  Volume       ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  VolumeMove   ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
98 99
static int  VideoConfig  ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101
static int  AudioConfig  ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
102 103
static int  Menu         ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
104 105 106 107
static int  Statistics   ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );

static int updateStatistics( intf_thread_t *, input_item_t *);
108 109 110 111 112 113 114 115 116 117

/* Status Callbacks */
static int TimeOffsetChanged( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t , void * );
static int VolumeChanged    ( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );
static int StateChanged     ( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );
static int RateChanged      ( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );
118

119 120
struct intf_sys_t
{
121
    int *pi_socket_listen;
122
    int i_socket;
123
    char *psz_unix_path;
124

125 126 127
    /* status changes */
    vlc_mutex_t       status_lock;
    playlist_status_t i_last_state;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
128

129 130
#ifdef WIN32
    HANDLE hConsoleIn;
131
    bool b_quiet;
132 133 134
#endif
};

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
135
#define msg_rc( ... ) __msg_rc( p_intf, __VA_ARGS__ )
136

137
static void __msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
138 139 140
{
    va_list args;
    va_start( args, psz_fmt );
141

142
    if( p_intf->p_sys->i_socket == -1 )
143
    {
144
        utf8_vfprintf( stdout, psz_fmt, args );
145
        printf( "\r\n" );
146
    }
147
    else
148 149
    {
        net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, psz_fmt, args );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
150
        net_Write( p_intf, p_intf->p_sys->i_socket, NULL, (uint8_t*)"\r\n", 2 );
151
    }
152 153 154
    va_end( args );
}

155 156 157
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
158
#define POS_TEXT N_("Show stream position")
159 160
#define POS_LONGTEXT N_("Show the current position in seconds within the " \
                        "stream from time to time." )
161

Christophe Massiot's avatar
Christophe Massiot committed
162
#define TTY_TEXT N_("Fake TTY")
Anil Daoud's avatar
Anil Daoud committed
163
#define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
164

165
#define UNIX_TEXT N_("UNIX socket command input")
166 167
#define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
                         "stdin." )
168

169
#define HOST_TEXT N_("TCP command input")
170
#define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
171 172
            "You can set the address and port the interface will bind to." )

173 174 175 176 177 178 179 180 181
#ifdef WIN32
#define QUIET_TEXT N_("Do not open a DOS command box interface")
#define QUIET_LONGTEXT N_( \
    "By default the rc interface plugin will start a DOS command box. " \
    "Enabling the quiet mode will not bring this command box but can also " \
    "be pretty annoying when you want to stop VLC and no video window is " \
    "open." )
#endif

182
vlc_module_begin();
183
    set_shortname( N_("RC"));
184
    set_category( CAT_INTERFACE );
Clément Stenac's avatar
Clément Stenac committed
185
    set_subcategory( SUBCAT_INTERFACE_MAIN );
186
    set_description( N_("Remote control interface") );
187
    add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, true );
188 189

#ifdef WIN32
190
    add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, false );
191 192
#else
#if defined (HAVE_ISATTY)
193
    add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, true );
194
#endif
195
    add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, true );
196
#endif
197
    add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, true );
198

199
    set_capability( "interface", 20 );
200

201
    set_callbacks( Activate, Deactivate );
202 203 204 205 206 207 208 209
vlc_module_end();

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
210
    char *psz_host, *psz_unix_path;
211
    int  *pi_socket = NULL;
212

213 214
#ifndef WIN32
#if defined(HAVE_ISATTY)
215
    /* Check that stdin is a TTY */
216
    if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
217 218
    {
        msg_Warn( p_intf, "fd 0 is not a TTY" );
219
        return VLC_EGENERIC;
220 221 222
    }
#endif

223 224
    psz_unix_path = config_GetPsz( p_intf, "rc-unix" );
    if( psz_unix_path )
225
    {
226
        int i_socket;
227

228
#ifndef AF_LOCAL
229
        msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
230
        free( psz_unix_path );
231 232 233 234 235 236 237 238
        return VLC_EGENERIC;
#else
        struct sockaddr_un addr;

        memset( &addr, 0, sizeof(struct sockaddr_un) );

        msg_Dbg( p_intf, "trying UNIX socket" );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
239
        if( (i_socket = socket( AF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
240
        {
241
            msg_Warn( p_intf, "can't open socket: %m" );
242
            free( psz_unix_path );
243 244 245
            return VLC_EGENERIC;
        }

246 247 248
        addr.sun_family = AF_LOCAL;
        strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
        addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
249

250 251 252 253
        if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
         && (errno == EADDRINUSE)
         && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
         && (errno == ECONNREFUSED))
254
        {
255 256 257 258
            msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
            unlink (psz_unix_path);

            if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
259
            {
260 261
                msg_Err (p_intf, "cannot bind UNIX socket at %s: %m",
                         psz_unix_path);
262 263
                free (psz_unix_path);
                net_Close (i_socket);
264 265
                return VLC_EGENERIC;
            }
266 267
        }

268
        if( listen( i_socket, 1 ) )
269
        {
270
            msg_Warn( p_intf, "can't listen on socket: %m");
271 272
            free( psz_unix_path );
            net_Close( i_socket );
273 274
            return VLC_EGENERIC;
        }
275 276 277 278 279 280 281 282 283 284 285

        /* FIXME: we need a core function to merge listening sockets sets */
        pi_socket = calloc( 2, sizeof( int ) );
        if( pi_socket == NULL )
        {
            free( psz_unix_path );
            net_Close( i_socket );
            return VLC_ENOMEM;
        }
        pi_socket[0] = i_socket;
        pi_socket[1] = -1;
286
#endif /* AF_LOCAL */
287
    }
288
#endif /* !WIN32 */
289

290
    if( ( pi_socket == NULL ) &&
291
        ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
292 293 294
    {
        vlc_url_t url;

295 296
        vlc_UrlParse( &url, psz_host, 0 );

297
        msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
298

299 300
        pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
        if( pi_socket == NULL )
301
        {
302 303
            msg_Warn( p_intf, "can't listen to %s port %i",
                      url.psz_host, url.i_port );
304 305 306 307 308 309
            vlc_UrlClean( &url );
            free( psz_host );
            return VLC_EGENERIC;
        }

        vlc_UrlClean( &url );
310
        free( psz_host );
311 312 313 314 315 316 317 318 319
    }

    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( !p_intf->p_sys )
    {
        msg_Err( p_intf, "no memory" );
        return VLC_ENOMEM;
    }

320
    p_intf->p_sys->pi_socket_listen = pi_socket;
321
    p_intf->p_sys->i_socket = -1;
322
    p_intf->p_sys->psz_unix_path = psz_unix_path;
323
    vlc_mutex_init( &p_intf->p_sys->status_lock );
324
    p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
325

326 327 328 329 330
    /* Non-buffered stdout */
    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );

    p_intf->pf_run = Run;

331
#ifdef WIN32
332 333
    p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
    if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
334
#else
Gildas Bazin's avatar
 
Gildas Bazin committed
335
    CONSOLE_INTRO_MSG;
336
#endif
337

338
    msg_rc( _("Remote control interface initialized. Type `help' for help.") );
339
    return VLC_SUCCESS;
340 341
}

342 343 344 345 346 347 348
/*****************************************************************************
 * Deactivate: uninitialize and cleanup
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

349
    net_ListenClose( p_intf->p_sys->pi_socket_listen );
350 351
    if( p_intf->p_sys->i_socket != -1 )
        net_Close( p_intf->p_sys->i_socket );
352 353
    if( p_intf->p_sys->psz_unix_path != NULL )
    {
354
#if defined(AF_LOCAL) && !defined(WIN32)
355 356 357 358
        unlink( p_intf->p_sys->psz_unix_path );
#endif
        free( p_intf->p_sys->psz_unix_path );
    }
359
    vlc_mutex_destroy( &p_intf->p_sys->status_lock );
360 361 362
    free( p_intf->p_sys );
}

363
/*****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
364
 * RegisterCallbacks: Register callbacks to dynamic variables
365
 *****************************************************************************/
Jean-Paul Saman's avatar
Jean-Paul Saman committed
366
static void RegisterCallbacks( intf_thread_t *p_intf )
367
{
368
    /* Register commands that will be cleaned up upon object destruction */
369 370 371 372 373 374 375 376 377
#define ADD( name, type, target )                                   \
    var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
    var_AddCallback( p_intf, name, target, NULL );
    ADD( "quit", VOID, Quit )
    ADD( "intf", STRING, Intf )

    ADD( "add", STRING, Playlist )
    ADD( "repeat", STRING, Playlist )
    ADD( "loop", STRING, Playlist )
Rafaël Carré's avatar
Rafaël Carré committed
378
    ADD( "random", STRING, Playlist )
379 380 381 382 383 384 385 386 387 388
    ADD( "enqueue", STRING, Playlist )
    ADD( "playlist", VOID, Playlist )
    ADD( "sort", VOID, Playlist )
    ADD( "play", VOID, Playlist )
    ADD( "stop", VOID, Playlist )
    ADD( "clear", VOID, Playlist )
    ADD( "prev", VOID, Playlist )
    ADD( "next", VOID, Playlist )
    ADD( "goto", INTEGER, Playlist )
    ADD( "status", INTEGER, Playlist )
389

390
    /* OSD menu commands */
391
    ADD(  "menu", STRING, Menu )
392 393

    /* DVD commands */
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    ADD( "pause", VOID, Input )
    ADD( "seek", INTEGER, Input )
    ADD( "title", STRING, Input )
    ADD( "title_n", VOID, Input )
    ADD( "title_p", VOID, Input )
    ADD( "chapter", STRING, Input )
    ADD( "chapter_n", VOID, Input )
    ADD( "chapter_p", VOID, Input )

    ADD( "fastforward", VOID, Input )
    ADD( "rewind", VOID, Input )
    ADD( "faster", VOID, Input )
    ADD( "slower", VOID, Input )
    ADD( "normal", VOID, Input )

    ADD( "atrack", STRING, Input )
    ADD( "vtrack", STRING, Input )
    ADD( "strack", STRING, Input )
412

413
    /* video commands */
414 415 416
    ADD( "vratio", STRING, VideoConfig )
    ADD( "vcrop", STRING, VideoConfig )
    ADD( "vzoom", STRING, VideoConfig )
417
    ADD( "snapshot", VOID, VideoConfig )
418

419
    /* audio commands */
420 421 422 423 424 425
    ADD( "volume", STRING, Volume )
    ADD( "volup", STRING, VolumeMove )
    ADD( "voldown", STRING, VolumeMove )
    ADD( "adev", STRING, AudioConfig )
    ADD( "achan", STRING, AudioConfig )

426 427 428
    /* misc menu commands */
    ADD( "stats", BOOL, Statistics )

429
#undef ADD
Jean-Paul Saman's avatar
Jean-Paul Saman committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443
}

/*****************************************************************************
 * Run: rc thread
 *****************************************************************************
 * This part of the interface is in a separate thread so that we can call
 * exec() from within it without annoying the rest of the program.
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    input_thread_t * p_input;
    playlist_t *     p_playlist;

    char       p_buffer[ MAX_LINE_LENGTH + 1 ];
444 445
    bool b_showpos = config_GetInt( p_intf, "rc-show-pos" );
    bool b_longhelp = false;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
446 447 448 449 450 451 452 453 454 455 456

    int        i_size = 0;
    int        i_oldpos = 0;
    int        i_newpos;

    p_buffer[0] = 0;
    p_input = NULL;
    p_playlist = NULL;

    /* Register commands that will be cleaned up upon object destruction */
    RegisterCallbacks( p_intf );
457

458 459
    /* status callbacks */
    /* Listen to audio volume updates */
460
    var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
461

Gildas Bazin's avatar
 
Gildas Bazin committed
462 463
#ifdef WIN32
    /* Get the file descriptor of the console input */
464 465
    p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
    if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
Gildas Bazin's avatar
 
Gildas Bazin committed
466
    {
467
        msg_Err( p_intf, "couldn't find user input handle" );
468
        vlc_object_kill( p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
469 470 471
    }
#endif

472
    while( !intf_ShouldDie( p_intf ) )
473
    {
474
        char *psz_cmd, *psz_arg;
475
        bool b_complete;
476

477
        if( p_intf->p_sys->pi_socket_listen != NULL &&
478
            p_intf->p_sys->i_socket == -1 )
479
        {
480
            p_intf->p_sys->i_socket =
481 482 483
                net_Accept( p_intf, p_intf->p_sys->pi_socket_listen,
                            INTF_IDLE_SLEEP );
            if( p_intf->p_sys->i_socket == -1 ) continue;
484 485
        }

486 487
        b_complete = ReadCommand( p_intf, p_buffer, &i_size );

488 489 490
        /* Manage the input part */
        if( p_input == NULL )
        {
491 492 493 494 495 496 497 498 499
            if( p_playlist )
            {
                p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
                                                       FIND_CHILD );
            }
            else
            {
                p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
                                                   FIND_ANYWHERE );
500
                if( p_input )
501
                {
502
                    p_playlist = pl_Yield( p_input );
503 504
                }
            }
505 506 507 508 509
            /* New input has been registered */
            if( p_input )
            {
                if( !p_input->b_dead || !p_input->b_die )
                {
510
                    char *psz_uri =
511
                            input_item_GetURI( input_GetItem( p_input ) );
512 513
                    msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
                    free( psz_uri );
Antoine Cellerier's avatar
Antoine Cellerier committed
514 515
                    msg_rc( STATUS_CHANGE "( audio volume: %d )",
                            config_GetInt( p_intf, "volume" ));
516 517 518 519 520
                }
                var_AddCallback( p_input, "state", StateChanged, p_intf );
                var_AddCallback( p_input, "rate-faster", RateChanged, p_intf );
                var_AddCallback( p_input, "rate-slower", RateChanged, p_intf );
                var_AddCallback( p_input, "rate", RateChanged, p_intf );
Antoine Cellerier's avatar
Antoine Cellerier committed
521 522
                var_AddCallback( p_input, "time-offset", TimeOffsetChanged,
                                 p_intf );
523
            }
524 525 526
        }
        else if( p_input->b_dead )
        {
527 528 529 530
            var_DelCallback( p_input, "state", StateChanged, p_intf );
            var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
            var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
            var_DelCallback( p_input, "rate", RateChanged, p_intf );
Antoine Cellerier's avatar
Antoine Cellerier committed
531 532
            var_DelCallback( p_input, "time-offset", TimeOffsetChanged,
                             p_intf );
533 534
            vlc_object_release( p_input );
            p_input = NULL;
535 536 537

            if( p_playlist )
            {
538
                vlc_object_lock( p_playlist );
539
                p_intf->p_sys->i_last_state = (int) PLAYLIST_STOPPED;
540
                msg_rc( STATUS_CHANGE "( stop state: 0 )" );
541
                vlc_object_unlock( p_playlist );
542 543
            }
        }
544

545 546 547
        if( (p_input != NULL) && !p_input->b_dead && !p_input->b_die &&
            (p_playlist != NULL) )
        {
548
            vlc_object_lock( p_playlist );
549 550 551 552
            if( (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
                (p_playlist->status.i_status == PLAYLIST_STOPPED) )
            {
                p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
553
                msg_rc( STATUS_CHANGE "( stop state: 5 )" );
554
            }
Antoine Cellerier's avatar
Antoine Cellerier committed
555 556
            else if(
                (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
557 558 559
                (p_playlist->status.i_status == PLAYLIST_RUNNING) )
            {
                p_intf->p_sys->i_last_state = p_playlist->status.i_status;
560
                 msg_rc( STATUS_CHANGE "( play state: 3 )" );
561
            }
Antoine Cellerier's avatar
Antoine Cellerier committed
562 563
            else if(
                (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
564 565 566
                (p_playlist->status.i_status == PLAYLIST_PAUSED) )
            {
                p_intf->p_sys->i_last_state = p_playlist->status.i_status;
567
                msg_rc( STATUS_CHANGE "( pause state: 4 )" );
568
            }
569
            vlc_object_unlock( p_playlist );
570 571
        }

572
        if( p_input && b_showpos )
573
        {
574 575
            i_newpos = 100 * var_GetFloat( p_input, "position" );
            if( i_oldpos != i_newpos )
576
            {
577
                i_oldpos = i_newpos;
578
                msg_rc( "pos: %d%%", i_newpos );
579 580 581 582
            }
        }

        /* Is there something to do? */
583 584 585 586 587
        if( !b_complete ) continue;

        /* Skip heading spaces */
        psz_cmd = p_buffer;
        while( *psz_cmd == ' ' )
588
        {
589 590
            psz_cmd++;
        }
591

592 593 594 595 596 597 598
        /* Split psz_cmd at the first space and make sure that
         * psz_arg is valid */
        psz_arg = strchr( psz_cmd, ' ' );
        if( psz_arg )
        {
            *psz_arg++ = 0;
            while( *psz_arg == ' ' )
599
            {
600
                psz_arg++;
601
            }
602 603 604
        }
        else
        {
Rafaël Carré's avatar
Rafaël Carré committed
605
            psz_arg = (char*)"";
606 607
        }

608 609 610 611 612 613 614
        /* module specfic commands: @<module name> <command> <args...> */
        if( *psz_cmd == '@' && *psz_arg )
        {
            /* Parse miscellaneous commands */
            char *psz_alias = psz_cmd + 1;
            char *psz_mycmd = strdup( psz_arg );
            char *psz_myarg = strchr( psz_mycmd, ' ' );
615
            char *psz_msg;
616

Antoine Cellerier's avatar
Antoine Cellerier committed
617 618 619 620 621 622 623 624
            if( !psz_myarg )
            {
                msg_rc( "Not enough parameters." );
            }
            else
            {
                *psz_myarg = '\0';
                psz_myarg ++;
625

Antoine Cellerier's avatar
Antoine Cellerier committed
626 627
                var_Command( p_intf, psz_alias, psz_mycmd, psz_myarg,
                             &psz_msg );
628

Antoine Cellerier's avatar
Antoine Cellerier committed
629 630 631 632 633
                if( psz_msg )
                {
                    msg_rc( psz_msg );
                    free( psz_msg );
                }
634 635 636
            }
            free( psz_mycmd );
        }
637
        /* If the user typed a registered local command, try it */
638
        else if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
639 640 641
        {
            vlc_value_t val;
            int i_ret;
642

643 644
            val.psz_string = psz_arg;
            i_ret = var_Set( p_intf, psz_cmd, val );
645
            msg_rc( "%s: returned %i (%s)",
646 647 648
                    psz_cmd, i_ret, vlc_error( i_ret ) );
        }
        /* Or maybe it's a global command */
649
        else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
650 651 652 653 654 655
        {
            vlc_value_t val;
            int i_ret;

            val.psz_string = psz_arg;
            /* FIXME: it's a global command, but we should pass the
656 657
             * local object as an argument, not p_intf->p_libvlc. */
            i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
658 659
            if( i_ret != 0 )
            {
660
                msg_rc( "%s: returned %i (%s)",
661 662
                         psz_cmd, i_ret, vlc_error( i_ret ) );
            }
663
        }
664 665 666 667 668 669 670 671 672
        else if( !strcmp( psz_cmd, "logout" ) )
        {
            /* Close connection */
            if( p_intf->p_sys->i_socket != -1 )
            {
                net_Close( p_intf->p_sys->i_socket );
            }
            p_intf->p_sys->i_socket = -1;
        }
673 674 675
        else if( !strcmp( psz_cmd, "info" ) )
        {
            if( p_input )
676
            {
677
                int i, j;
Clément Stenac's avatar
Clément Stenac committed
678 679
                vlc_mutex_lock( &input_GetItem(p_input)->lock );
                for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
680
                {
Clément Stenac's avatar
Clément Stenac committed
681 682
                    info_category_t *p_category = input_GetItem(p_input)
                                                        ->pp_categories[i];
683

684 685
                    msg_rc( "+----[ %s ]", p_category->psz_name );
                    msg_rc( "| " );
686 687 688
                    for ( j = 0; j < p_category->i_infos; j++ )
                    {
                        info_t *p_info = p_category->pp_infos[j];
689
                        msg_rc( "| %s: %s", p_info->psz_name,
690 691
                                p_info->psz_value );
                    }
692
                    msg_rc( "| " );
693
                }
694
                msg_rc( "+----[ end of stream info ]" );
Clément Stenac's avatar
Clément Stenac committed
695
                vlc_mutex_unlock( &input_GetItem(p_input)->lock );
696
            }
697
            else
698
            {
699
                msg_rc( "no input" );
Sam Hocevar's avatar
Sam Hocevar committed
700
            }
701
        }
702 703 704 705
        else if( !strcmp( psz_cmd, "is_playing" ) )
        {
            if( ! p_input )
            {
706
                msg_rc( "0" );
707 708 709
            }
            else
            {
710
                msg_rc( "1" );
711 712 713 714 715 716
            }
        }
        else if( !strcmp( psz_cmd, "get_time" ) )
        {
            if( ! p_input )
            {
717
                msg_rc("0");
718 719 720 721 722
            }
            else
            {
                vlc_value_t time;
                var_Get( p_input, "time", &time );
723
                msg_rc( "%i", time.i_time / 1000000);
724 725 726 727 728 729
            }
        }
        else if( !strcmp( psz_cmd, "get_length" ) )
        {
            if( ! p_input )
            {
730
                msg_rc("0");
731 732 733 734 735
            }
            else
            {
                vlc_value_t time;
                var_Get( p_input, "length", &time );
736
                msg_rc( "%i", time.i_time / 1000000);
737 738 739 740 741 742
            }
        }
        else if( !strcmp( psz_cmd, "get_title" ) )
        {
            if( ! p_input )
            {
743
                msg_rc("");
744 745 746
            }
            else
            {
Clément Stenac's avatar
Clément Stenac committed
747
                msg_rc( "%s", input_GetItem(p_input)->psz_name );
748 749
            }
        }
750 751
        else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
                 || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
752
        {
753
            if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "H", 1 ) )
754 755
                 b_longhelp = true;
            else b_longhelp = false;
756 757

            Help( p_intf, b_longhelp );
758
        }
759 760 761 762 763
        else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
        {
            var_SetInteger( p_intf->p_libvlc, "key-pressed",
                            config_GetInt( p_intf, psz_arg ) );
        }
764 765 766 767 768 769 770 771 772 773 774 775
        else switch( psz_cmd[0] )
        {
        case 'f':
        case 'F':
            if( p_input )
            {
                vout_thread_t *p_vout;
                p_vout = vlc_object_find( p_input,
                                          VLC_OBJECT_VOUT, FIND_CHILD );

                if( p_vout )
                {
776
                    vlc_value_t val;
777
                    bool b_update = false;
778 779
                    var_Get( p_vout, "fullscreen", &val );
                    val.b_bool = !val.b_bool;
Antoine Cellerier's avatar
Antoine Cellerier committed
780
                    if( !strncmp( psz_arg, "on", 2 )
781
                        && ( val.b_bool == true ) )
782
                    {
783 784
                        b_update = true;
                        val.b_bool = true;
785
                    }
Antoine Cellerier's avatar
Antoine Cellerier committed
786
                    else if( !strncmp( psz_arg, "off", 3 )
787
                             && ( val.b_bool == false ) )
788
                    {
789 790
                        b_update = true;
                        val.b_bool = false;
791
                    }
Antoine Cellerier's avatar
Antoine Cellerier committed
792 793
                    else if( strncmp( psz_arg, "off", 3 )
                             && strncmp( psz_arg, "on", 2 ) )
794
                        b_update = true;
795
                    if( b_update ) var_Set( p_vout, "fullscreen", val );
796 797 798 799 800 801 802 803
                    vlc_object_release( p_vout );
                }
            }
            break;

        case 's':
        case 'S':
            ;
804 805 806 807 808 809 810
            break;

        case '\0':
            /* Ignore empty lines */
            break;

        default:
811
            msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
812
            break;
813
        }
814 815 816

        /* Command processed */
        i_size = 0; p_buffer[0] = 0;
817 818
    }

819 820
    msg_rc( STATUS_CHANGE "( stop state: 0 )" );
    msg_rc( STATUS_CHANGE "( quit )" );
821

822 823
    if( p_input )
    {
824 825 826 827 828
        var_DelCallback( p_input, "state", StateChanged, p_intf );
        var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
        var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
        var_DelCallback( p_input, "rate", RateChanged, p_intf );
        var_DelCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
829 830
        vlc_object_release( p_input );
        p_input = NULL;
831
    }
832

833 834 835 836
    if( p_playlist )
    {
        vlc_object_release( p_playlist );
        p_playlist = NULL;
837
    }
838

839
    var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
840 841
}

842
static void Help( intf_thread_t *p_intf, bool b_longhelp)
843
{
844 845
    msg_rc(_("+----[ Remote control commands ]"));
    msg_rc(  "| ");
846 847 848 849 850 851 852 853 854
    msg_rc(_("| add XYZ  . . . . . . . . . . . . add XYZ to playlist"));
    msg_rc(_("| enqueue XYZ  . . . . . . . . . queue XYZ to playlist"));
    msg_rc(_("| playlist . . . . .  show items currently in playlist"));
    msg_rc(_("| play . . . . . . . . . . . . . . . . . . play stream"));
    msg_rc(_("| stop . . . . . . . . . . . . . . . . . . stop stream"));
    msg_rc(_("| next . . . . . . . . . . . . . .  next playlist item"));
    msg_rc(_("| prev . . . . . . . . . . . .  previous playlist item"));
    msg_rc(_("| goto . . . . . . . . . . . . . .  goto item at index"));
    msg_rc(_("| repeat [on|off] . . . .  toggle playlist item repeat"));
Rafaël Carré's avatar
Rafaël Carré committed
855 856 857
    msg_rc(_("| loop [on|off] . . . . . . . . . toggle playlist loop"));
    msg_rc(_("| random [on|off] . . . . . . .  toggle random jumping"));
    msg_rc(_("| clear . . . . . . . . . . . . . . clear the playlist"));
858 859 860 861 862 863 864
    msg_rc(_("| status . . . . . . . . . . . current playlist status"));
    msg_rc(_("| title [X]  . . . . . . set/get title in current item"));
    msg_rc(_("| title_n  . . . . . . . .  next title in current item"));
    msg_rc(_("| title_p  . . . . . .  previous title in current item"));
    msg_rc(_("| chapter [X]  . . . . set/get chapter in current item"));
    msg_rc(_("| chapter_n  . . . . . .  next chapter in current item"));
    msg_rc(_("| chapter_p  . . . .  previous chapter in current item"));
865
    msg_rc(  "| ");
866 867 868 869 870 871 872 873 874
    msg_rc(_("| seek X . . . seek in seconds, for instance `seek 12'"));
    msg_rc(_("| pause  . . . . . . . . . . . . . . . .  toggle pause"));
    msg_rc(_("| fastforward  . . . . . . . .  .  set to maximum rate"));
    msg_rc(_("| rewind  . . . . . . . . . . . .  set to minimum rate"));
    msg_rc(_("| faster . . . . . . . . . .  faster playing of stream"));
    msg_rc(_("| slower . . . . . . . . . .  slower playing of stream"));
    msg_rc(_("| normal . . . . . . . . . .  normal playing of stream"));
    msg_rc(_("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
    msg_rc(_("| info . . . . .  information about the current stream"));
875
    msg_rc(_("| stats  . . . . . . . .  show statistical information"));
876
    msg_rc(_("| get_time . . seconds elapsed since stream's beginning"));
877 878 879
    msg_rc(_("| is_playing . . . .  1 if a stream plays, 0 otherwise"));
    msg_rc(_("| get_title . . . . .  the title of the current stream"));
    msg_rc(_("| get_length . . . .  the length of the current stream"));
880
    msg_rc(  "| ");
881 882 883 884 885 886 887 888 889 890
    msg_rc(_("| volume [X] . . . . . . . . . .  set/get audio volume"));
    msg_rc(_("| volup [X]  . . . . . . .  raise audio volume X steps"));
    msg_rc(_("| voldown [X]  . . . . . .  lower audio volume X steps"));
    msg_rc(_("| adev [X] . . . . . . . . . . .  set/get audio device"));
    msg_rc(_("| achan [X]. . . . . . . . . .  set/get audio channels"));
    msg_rc(_("| atrack [X] . . . . . . . . . . . set/get audio track"));
    msg_rc(_("| vtrack [X] . . . . . . . . . . . set/get video track"));
    msg_rc(_("| vratio [X]  . . . . . . . set/get video aspect ratio"));
    msg_rc(_("| vcrop [X]  . . . . . . . . . . .  set/get video crop"));
    msg_rc(_("| vzoom [X]  . . . . . . . . . . .  set/get video zoom"));
891
    msg_rc(_("| snapshot . . . . . . . . . . . . take video snapshot"));
892 893 894
    msg_rc(_("| strack [X] . . . . . . . . . set/get subtitles track"));
    msg_rc(_("| key [hotkey name] . . . . . .  simulate hotkey press"));
    msg_rc(_("| menu . . [on|off|up|down|left|right|select] use menu"));
895
    msg_rc(  "| ");
896

897 898
    if (b_longhelp)
    {
899 900 901 902 903 904 905 906
        msg_rc(_("| @name marq-marquee  STRING  . . overlay STRING in video"));
        msg_rc(_("| @name marq-x X . . . . . . . . . . . .offset from left"));
        msg_rc(_("| @name marq-y Y . . . . . . . . . . . . offset from top"));
        msg_rc(_("| @name marq-position #. . .  .relative position control"));
        msg_rc(_("| @name marq-color # . . . . . . . . . . font color, RGB"));
        msg_rc(_("| @name marq-opacity # . . . . . . . . . . . . . opacity"));
        msg_rc(_("| @name marq-timeout T. . . . . . . . . . timeout, in ms"));
        msg_rc(_("| @name marq-size # . . . . . . . . font size, in pixels"));
907
        msg_rc(  "| ");
908 909 910 911 912
        msg_rc(_("| @name logo-file STRING . . .the overlay file path/name"));
        msg_rc(_("| @name logo-x X . . . . . . . . . . . .offset from left"));
        msg_rc(_("| @name logo-y Y . . . . . . . . . . . . offset from top"));
        msg_rc(_("| @name logo-position #. . . . . . . . relative position"));
        msg_rc(_("| @name logo-transparency #. . . . . . . . .transparency"));
913
        msg_rc(  "| ");
914 915 916 917 918 919 920 921 922 923 924 925 926 927
        msg_rc(_("| @name mosaic-alpha # . . . . . . . . . . . . . . alpha"));
        msg_rc(_("| @name mosaic-height #. . . . . . . . . . . . . .height"));
        msg_rc(_("| @name mosaic-width # . . . . . . . . . . . . . . width"));
        msg_rc(_("| @name mosaic-xoffset # . . . .top left corner position"));
        msg_rc(_("| @name mosaic-yoffset # . . . .top left corner position"));
        msg_rc(_("| @name mosaic-offsets x,y(,x,y)*. . . . list of offsets"));
        msg_rc(_("| @name mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
        msg_rc(_("| @name mosaic-vborder # . . . . . . . . vertical border"));
        msg_rc(_("| @name mosaic-hborder # . . . . . . . horizontal border"));
        msg_rc(_("| @name mosaic-position {0=auto,1=fixed} . . . .position"));
        msg_rc(_("| @name mosaic-rows #. . . . . . . . . . .number of rows"));
        msg_rc(_("| @name mosaic-cols #. . . . . . . . . . .number of cols"));
        msg_rc(_("| @name mosaic-order id(,id)* . . . . order of pictures "));
        msg_rc(_("| @name mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
928
        msg_rc(  "| ");
929
    }
930 931 932 933
    msg_rc(_("| help . . . . . . . . . . . . . . . this help message"));
    msg_rc(_("| longhelp . . . . . . . . . . . a longer help message"));
    msg_rc(_("| logout . . . . . . .  exit (if in socket connection)"));
    msg_rc(_("| quit . . . . . . . . . . . . . . . . . . .  quit vlc"));
934 935
    msg_rc(  "| ");
    msg_rc(_("+----[ end of help ]"));
936 937 938 939 940 941 942 943
}

/********************************************************************
 * Status callback routines
 ********************************************************************/
static int TimeOffsetChanged( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
944 945
    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
946 947
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
    input_thread_t *p_input = NULL;
948

949 950 951 952
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( p_input )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
953 954
        msg_rc( STATUS_CHANGE "( time-offset: %d )",
                var_GetInteger( p_input, "time-offset" ) );
955 956 957 958 959 960 961 962 963
        vlc_object_release( p_input );
    }
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
}

static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
964
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
965
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
966

967
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
Antoine Cellerier's avatar
Antoine Cellerier committed
968 969
    msg_rc( STATUS_CHANGE "( audio volume: %d )",
            config_GetInt( p_this, "volume") );
970 971 972 973 974 975 976
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
}

static int StateChanged( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
977
    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
978 979 980
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
    playlist_t    *p_playlist = NULL;
    input_thread_t *p_input = NULL;
981

982 983 984 985
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( p_input )
    {
986 987 988
        p_playlist = pl_Yield( p_input );
        char cmd[6];
        switch( p_playlist->status.i_status )
989
        {
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
        case PLAYLIST_STOPPED:
            strcpy( cmd, "stop" );
            break;
        case PLAYLIST_RUNNING:
            strcpy( cmd, "play" );
            break;
        case PLAYLIST_PAUSED:
            strcpy( cmd, "pause" );
            break;
        default:
            cmd[0] = '\0';
        } /* var_GetInteger( p_input, "state" )  */
        msg_rc( STATUS_CHANGE "( %s state: %d ): %s",
                              &cmd[0], newval.i_int,
                              ppsz_input_state[ newval.i_int ] );
        vlc_object_release( p_playlist );
1006 1007 1008 1009
        vlc_object_release( p_input );
    }
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
1010 1011
}

1012 1013 1014
static int RateChanged( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
1015 1016
    VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
1017 1018
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
    input_thread_t *p_input = NULL;
1019

1020 1021 1022 1023
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( p_input )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
1024 1025
        msg_rc( STATUS_CHANGE "( new rate: %d )",
                var_GetInteger( p_input, "rate" ) );
1026 1027 1028 1029 1030 1031 1032 1033 1034
        vlc_object_release( p_input );
    }
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
}

/********************************************************************
 * Command routines
 ********************************************************************/
1035 1036
static int Input( vlc_object_t *p_this, char const *psz_cmd,
                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1037
{
Rafaël Carré's avatar
Rafaël Carré committed
1038
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1039 1040
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    input_thread_t *p_input;
1041
    vlc_value_t     val;
1042 1043

    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1044
    if( !p_input ) return VLC_ENOOBJ;
1045

1046 1047 1048 1049
    var_Get( p_input, "state", &val );
    if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
        ( strcmp( psz_cmd, "pause" ) != 0 ) )
    {
1050
        msg_rc( _("Press menu select or pause to continue.") );
1051 1052 1053
        vlc_object_release( p_input );
        return VLC_EGENERIC;
    }
1054

1055 1056 1057
    /* Parse commands that only require an input */
    if( !strcmp( psz_cmd, "pause" ) )
    {
1058
        val.i_int = config_GetInt( p_intf, "key-play-pause" );
1059
        var_Set( p_intf->p_libvlc, "key-pressed", val );
1060 1061 1062
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
1063 1064
    else if( !strcmp( psz_cmd, "seek" ) )
    {
1065 1066 1067
        if( strlen( newval.psz_string ) > 0 &&
            newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
        {
1068
            val.f_float = (float)atof( newval.psz_string ) / 100.0;
1069
            var_Set( p_input, "position", val );
1070 1071 1072
        }
        else
        {
1073
            val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
1074
            var_Set( p_input, "time", val );
1075 1076 1077
        }
        vlc_object_release( p_input );
        return VLC_SUCCESS;
1078
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1079 1080
    else if ( !strcmp( psz_cmd, "fastforward" ) )
    {
1081
        val.i_int = config_GetInt( p_intf, "key-jump+extrashort" );
1082
        var_Set( p_intf->p_libvlc, "key-pressed", val );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1083 1084 1085 1086 1087
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    else if ( !strcmp( psz_cmd, "rewind" ) )
    {
1088
        val.i_int = config_GetInt( p_intf, "key-jump-extrashort" );
1089
        var_Set( p_intf->p_libvlc, "key-pressed", val );
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    else if ( !strcmp( psz_cmd, "faster" ) )
    {
        var_Set( p_input, "rate-faster", val );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    else if ( !strcmp( psz_cmd, "slower" ) )
    {
        var_Set( p_input, "rate-slower", val );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    else if ( !strcmp( psz_cmd, "normal" ) )
    {
        val.i_int = INPUT_RATE_DEFAULT;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1108 1109 1110 1111
        var_Set( p_input, "rate", val );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1112 1113 1114 1115 1116 1117
    else if( !strcmp( psz_cmd, "chapter" ) ||
             !strcmp( psz_cmd, "chapter_n" ) ||
             !strcmp( psz_cmd, "chapter_p" ) )
    {
        if( !strcmp( psz_cmd, "chapter" ) )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1118
            if ( *newval.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
1119 1120
            {
                /* Set. */
1121 1122
                val.i_int = atoi( newval.psz_string );
                var_Set( p_input, "chapter", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1123 1124 1125
            }
            else
            {
1126 1127
                vlc_value_t val_list;

Gildas Bazin's avatar
 
Gildas Bazin committed
1128
                /* Get. */
1129
                var_Get( p_input, "chapter", &val );
1130 1131
                var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
                            &val_list, NULL );
1132
                msg_rc( "Currently playing chapter %d/%d.",
1133 1134 1135
                        val.i_int, val_list.p_list->i_count );
                var_Change( p_this, "chapter", VLC_VAR_FREELIST,
                            &val_list, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
1136 1137 1138 1139
            }
        }
        else if( !strcmp( psz_cmd, "chapter_n" ) )
        {
1140
            val.b_bool = true;
1141
            var_Set( p_input, "next-chapter", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1142 1143 1144
        }
        else if( !strcmp( psz_cmd, "chapter_p" ) )
        {
1145
            val.b_bool = true;
1146
            var_Set( p_input, "prev-chapter", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        }
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    else if( !strcmp( psz_cmd, "title" ) ||
             !strcmp( psz_cmd, "title_n" ) ||
             !strcmp( psz_cmd, "title_p" ) )
    {
        if( !strcmp( psz_cmd, "title" ) )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1157
            if ( *newval.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
1158 1159
            {
                /* Set. */
1160 1161
                val.i_int = atoi( newval.psz_string );
                var_Set( p_input, "title", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1162 1163 1164
            }
            else
            {
1165 1166
                vlc_value_t val_list;

Gildas Bazin's avatar
 
Gildas Bazin committed
1167
                /* Get. */
1168
                var_Get( p_input, "title", &val );
1169 1170
                var_Change( p_input, "title", VLC_VAR_GETCHOICES,
                            &val_list, NULL );
1171
                msg_rc( "Currently playing title %d/%d.",
1172 1173 1174
                        val.i_int, val_list.p_list->i_count );
                var_Change( p_this, "title", VLC_VAR_FREELIST,
                            &val_list, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
1175 1176 1177 1178
            }
        }
        else if( !strcmp( psz_cmd, "title_n" ) )
        {
1179
            val.b_bool = true;
1180
            var_Set( p_input, "next-title", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1181 1182 1183
        }
        else if( !strcmp( psz_cmd, "title_p" ) )
        {
1184
            val.b_bool = true;
1185
            var_Set( p_input, "prev-title", val );
Gildas Bazin's avatar
 
Gildas Bazin committed
1186 1187 1188 1189 1190
        }

        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
1191 1192 1193 1194
    else if(    !strcmp( psz_cmd, "atrack" )
             || !strcmp( psz_cmd, "vtrack" )
             || !strcmp( psz_cmd, "strack" ) )
    {
Clément Stenac's avatar
Clément Stenac committed
1195
        const char *psz_variable;
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
        vlc_value_t val_name;
        int i_error;

        if( !strcmp( psz_cmd, "atrack" ) )
        {
            psz_variable = "audio-es";
        }
        else if( !strcmp( psz_cmd, "vtrack" ) )
        {
            psz_variable = "video-es";
        }
        else
        {
            psz_variable = "spu-es";
        }

        /* Get the descriptive name of the variable */
        var_Change( p_input, psz_variable, VLC_VAR_GETTEXT,
                     &val_name, NULL );
        if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);

        if( newval.psz_string && *newval.psz_string )
        {
            /* set */
            vlc_value_t val;
            val.i_int = atoi( newval.psz_string );

            i_error = var_Set( p_input, psz_variable, val );
        }
        else
        {
            /* get */
            vlc_value_t val, text;
            int i, i_value;

            if ( var_Get( p_input, psz_variable, &val ) < 0 )
            {
                vlc_object_release( p_input );
                return VLC_EGENERIC;
            }
            i_value = val.i_int;

            if ( var_Change( p_input, psz_variable,
                             VLC_VAR_GETLIST, &val, &text ) < 0 )
            {
                vlc_object_release( p_input );
                return VLC_EGENERIC;
            }

            msg_rc( "+----[ %s ]", val_name.psz_string );
            for ( i = 0; i < val.p_list->i_count; i++ )
            {
                if ( i_value == val.p_list->p_values[i].i_int )
                    msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
                            text.p_list->p_values[i].psz_string );
                else
                    msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
                            text.p_list->p_values[i].psz_string );
            }
            var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
                        &val, &text );
            msg_rc( "+----[ end of %s ]", val_name.psz_string );

1259
            free( val_name.psz_string );
1260 1261 1262 1263 1264 1265

            i_error = VLC_SUCCESS;
        }
        vlc_object_release( p_input );
        return i_error;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1266

1267
    /* Never reached. */
1268
    vlc_object_release( p_input );
1269
    return VLC_EGENERIC;
1270
}
1271

1272 1273 1274
static void print_playlist( intf_thread_t *p_intf, playlist_item_t *p_item, int i_level )
{
    int i;
1275
    char psz_buffer[MSTRTIME_MAX_SIZE];
1276 1277
    for( i = 0; i< p_item->i_children; i++ )
    {
1278 1279 1280 1281 1282 1283 1284
        if( p_item->pp_children[i]->p_input->i_duration != -1 )
        {
            secstotimestr( psz_buffer, p_item->pp_children[i]->p_input->i_duration / 1000000 );
            msg_rc( "|%*s- %s (%s)", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name, psz_buffer );
        }
        else
            msg_rc( "|%*s- %s", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name );
1285 1286 1287 1288 1289 1290

        if( p_item->pp_children[i]->i_children >= 0 )
            print_playlist( p_intf, p_item->pp_children[i], i_level + 1 );
    }
}

1291 1292 1293
static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
1294
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1295 1296
    vlc_value_t val;

1297
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
1298
    playlist_t *p_playlist = pl_Yield( p_this );
1299

1300
    PL_LOCK;
1301 1302 1303
    if( p_playlist->p_input )
    {
        var_Get( p_playlist->p_input, "state", &val );
1304 1305
        if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
        {
1306
            msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1307
            vlc_object_release( p_playlist );
1308
            PL_UNLOCK;
1309 1310
            return VLC_EGENERIC;
        }
1311
    }
1312
    PL_UNLOCK;
1313

1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    /* Parse commands that require a playlist */
    if( !strcmp( psz_cmd, "prev" ) )
    {
        playlist_Prev( p_playlist );
    }
    else if( !strcmp( psz_cmd, "next" ) )
    {
        playlist_Next( p_playlist );
    }
    else if( !strcmp( psz_cmd, "play" ) )
    {
1325 1326
        msg_Warn( p_playlist, "play" );
        playlist_Play( p_playlist );
1327
    }
1328 1329
    else if( !strcmp( psz_cmd, "repeat" ) )
    {
1330
        bool b_update = true;
1331 1332 1333 1334 1335

        var_Get( p_playlist, "repeat", &val );

        if( strlen( newval.psz_string ) > 0 )
        {
1336 1337
            if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == true ) ) ||
                 ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == false ) ) )
1338
            {
1339
                b_update = false;
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
            }
        }

        if ( b_update )
        {
            val.b_bool = !val.b_bool;
            var_Set( p_playlist, "repeat", val );
        }
        msg_rc( "Setting repeat to %d", val.b_bool );
    }
    else if( !strcmp( psz_cmd, "loop" ) )
    {
1352
        bool b_update = true;
1353 1354 1355 1356 1357

        var_Get( p_playlist, "loop", &val );

        if( strlen( newval.psz_string ) > 0 )
        {
1358 1359
            if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == true ) ) ||
                 ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == false ) ) )
1360
            {
1361
                b_update = false;
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
            }
        }

        if ( b_update )
        {
            val.b_bool = !val.b_bool;
            var_Set( p_playlist, "loop", val );
        }
        msg_rc( "Setting loop to %d", val.b_bool );
    }
Rafaël Carré's avatar
Rafaël Carré committed
1372 1373
    else if( !strcmp( psz_cmd, "random" ) )
    {
1374
        bool b_update = true;
Rafaël Carré's avatar
Rafaël Carré committed
1375 1376 1377 1378 1379

        var_Get( p_playlist, "random", &val );

        if( strlen( newval.psz_string ) > 0 )
        {
1380 1381
            if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == true ) ) ||
                 ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == false ) ) )
Rafaël Carré's avatar
Rafaël Carré committed
1382
            {
1383
                b_update = false;
Rafaël Carré's avatar
Rafaël Carré committed
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
            }
        }

        if ( b_update )
        {
            val.b_bool = !val.b_bool;
            var_Set( p_playlist, "random", val );
        }
        msg_rc( "Setting random to %d", val.b_bool );
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1394 1395
    else if (!strcmp( psz_cmd, "goto" ) )
    {
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        int i_pos = atoi( newval.psz_string );
        /* The playlist stores 2 times the same item: onelevel & category */
        int i_size = p_playlist->items.i_size / 2;

        if( i_pos <= 0 )
            msg_rc( _("Error: `goto' needs an argument greater than zero.") );
        else if( i_pos <= i_size )
        {
            playlist_item_t *p_item, *p_parent;
            p_item = p_parent = p_playlist->items.p_elems[i_pos*2-1];
            while( p_parent->p_parent )
                p_parent = p_parent->p_parent;
1408
            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
1409 1410 1411 1412
                    p_parent, p_item );
        }
        else
            msg_rc( _("Playlist has only %d elements"), i_size );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1413
    }
1414 1415 1416 1417
    else if( !strcmp( psz_cmd, "stop" ) )
    {
        playlist_Stop( p_playlist );
    }
1418 1419 1420
    else if( !strcmp( psz_cmd, "clear" ) )
    {
        playlist_Stop( p_playlist );
1421
        playlist_Clear( p_playlist, false );
1422
    }
1423 1424
    else if( !strcmp( psz_cmd, "add" ) &&
             newval.psz_string && *newval.psz_string )
1425
    {
1426
        input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1427 1428 1429

        if( p_item )
        {
1430
            msg_rc( "Trying to add %s to playlist.", newval.psz_string );
Rafaël Carré's avatar
Rafaël Carré committed
1431
            int i_ret =playlist_AddInput( p_playlist, p_item,
1432
                     PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END, true,
Rafaël Carré's avatar
Rafaël Carré committed
1433 1434 1435
                     false );
            vlc_gc_decref( p_item );
            if( i_ret != VLC_SUCCESS )
1436 1437 1438
            {
                return VLC_EGENERIC;
            }
1439
        }
1440
    }
1441 1442 1443
    else if( !strcmp( psz_cmd, "enqueue" ) &&
             newval.psz_string && *newval.psz_string )
    {
1444
        input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1445 1446 1447 1448

        if( p_item )
        {
            msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1449
            if( playlist_AddInput( p_playlist, p_item,
1450 1451
                               PLAYLIST_APPEND, PLAYLIST_END, true,
                               false ) != VLC_SUCCESS )
1452 1453 1454
            {
                return VLC_EGENERIC;
            }
1455 1456
        }
    }
1457
    else if( !strcmp( psz_cmd, "playlist" ) )
1458 1459
    {
        msg_rc( "+----[ Playlist ]" );
1460
        print_playlist( p_intf, p_playlist->p_root_category, 0 );
1461 1462
        msg_rc( "+----[ End of playlist ]" );
    }
1463

Clément Stenac's avatar
Clément Stenac committed
1464 1465
    else if( !strcmp( psz_cmd, "sort" ))
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
1466
        playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_onelevel,
Clément Stenac's avatar
Clément Stenac committed
1467 1468
                                    SORT_ARTIST, ORDER_NORMAL );
    }
1469
    else if( !strcmp( psz_cmd, "status" ) )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1470
    {
1471
        if( p_playlist->p_input )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1472
        {
1473
            /* Replay the current state of the system. */
1474 1475 1476 1477
            char *psz_uri =
                    input_item_GetURI( input_GetItem( p_playlist->p_input ) );
            msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
            free( psz_uri );
Antoine Cellerier's avatar
Antoine Cellerier committed
1478 1479
            msg_rc( STATUS_CHANGE "( audio volume: %d )",
                    config_GetInt( p_intf, "volume" ));
1480

1481
            PL_LOCK;
1482 1483 1484
            switch( p_playlist->status.i_status )
            {
                case PLAYLIST_STOPPED:
1485
                    msg_rc( STATUS_CHANGE "( stop state: 5 )" );
1486 1487
                    break;
                case PLAYLIST_RUNNING:
1488
                    msg_rc( STATUS_CHANGE "( play state: 3 )" );
1489 1490
                    break;
                case PLAYLIST_PAUSED:
1491
                    msg_rc( STATUS_CHANGE "( pause state: 4 )" );
1492 1493
                    break;
                default:
1494
                    msg_rc( STATUS_CHANGE "( unknown state: -1 )" );
1495 1496
                    break;
            }
1497
            PL_UNLOCK;
1498 1499
        }
    }
1500

1501 1502 1503 1504 1505
    /*
     * sanity check
     */
    else
    {
1506
        msg_rc( "unknown command!" );
1507 1508 1509 1510 1511 1512
    }

    vlc_object_release( p_playlist );
    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1513 1514
static int Quit( vlc_object_t *p_this, char const *psz_cmd,
                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1515
{
Rafaël Carré's avatar
Rafaël Carré committed
1516 1517
    VLC_UNUSED(p_data); VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
1518
    playlist_t *p_playlist;
1519

1520 1521 1522 1523
    p_playlist = pl_Yield( p_this );
    playlist_Stop( p_playlist );
    vlc_object_release( p_playlist );
    
1524
    vlc_object_kill( p_this->p_libvlc );
1525 1526 1527
    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1528 1529
static int Intf( vlc_object_t *p_this, char const *psz_cmd,
                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1530
{
Rafaël Carré's avatar
Rafaël Carré committed
1531
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1532
    intf_thread_t *p_newintf = NULL;
1533

1534
    p_newintf = intf_Create( p_this->p_libvlc, newval.psz_string );
1535 1536 1537 1538 1539
    if( p_newintf )
    {
        if( intf_RunThread( p_newintf ) )
        {
            vlc_object_detach( p_newintf );
1540
            vlc_object_release( p_newintf );
1541 1542 1543 1544
        }
    }

    return VLC_SUCCESS;
1545 1546
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1547 1548
static int Volume( vlc_object_t *p_this, char const *psz_cmd,
                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
1549
{
Rafaël Carré's avatar
Rafaël Carré committed
1550
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1551
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1552
    input_thread_t *p_input = NULL;
1553
    int i_error = VLC_EGENERIC;
1554

Jean-Paul Saman's avatar
Jean-Paul Saman committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( !p_input )
        return VLC_ENOOBJ;

    if( p_input )
    {
        vlc_value_t val;

        var_Get( p_input, "state", &val );
        if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
        {
1566
            msg_rc( _("Type 'menu select' or 'pause' to continue.") );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1567 1568 1569 1570 1571 1572
            vlc_object_release( p_input );
            return VLC_EGENERIC;
        }
        vlc_object_release( p_input );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1573
    if ( *newval.psz_string )
1574 1575
    {
        /* Set. */
Gildas Bazin's avatar
 
Gildas Bazin committed
1576
        audio_volume_t i_volume = atoi( newval.psz_string );
1577
        if ( (i_volume > (audio_volume_t)AOUT_VOLUME_MAX) )
1578
        {
1579
            msg_rc( "Volume must be in the range %d-%d.", AOUT_VOLUME_MIN,
1580 1581 1582
                    AOUT_VOLUME_MAX );
            i_error = VLC_EBADVAR;
        }
1583 1584 1585 1586 1587 1588 1589
        else
        {
            if( i_volume == AOUT_VOLUME_MIN )
            {
                vlc_value_t keyval;

                keyval.i_int = config_GetInt( p_intf, "key-vol-mute" );
1590
                var_Set( p_intf->p_libvlc, "key-pressed", keyval );
1591
            }
1592
            i_error = aout_VolumeSet( p_this, i_volume );
1593
            osd_Volume( p_this );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1594
            msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1595
        }
1596 1597 1598 1599 1600
    }
    else
    {
        /* Get. */
        audio_volume_t i_volume;
1601
        if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1602 1603 1604 1605 1606
        {
            i_error = VLC_EGENERIC;
        }
        else
        {
1607
            msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1608 1609 1610 1611 1612 1613 1614
            i_error = VLC_SUCCESS;
        }
    }

    return i_error;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1615 1616
static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
1617
{
Rafaël Carré's avatar
Rafaël Carré committed
1618
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1619
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
1620
    audio_volume_t i_volume;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1621
    input_thread_t *p_input = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
1622
    int i_nb_steps = atoi(newval.psz_string);
1623
    int i_error = VLC_SUCCESS;
1624
    int i_volume_step = 0;
1625

Jean-Paul Saman's avatar
Jean-Paul Saman committed
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( !p_input )
        return VLC_ENOOBJ;

    if( p_input )
    {
        vlc_value_t val;

        var_Get( p_input, "state", &val );
        if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
        {
1637
            msg_rc( _("Type 'menu select' or 'pause' to continue.") );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1638 1639 1640 1641 1642 1643
            vlc_object_release( p_input );
            return VLC_EGENERIC;
        }
        vlc_object_release( p_input );
    }

1644
    i_volume_step = config_GetInt( p_intf->p_libvlc, "volume-step" );
1645
    if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/i_volume_step) )
1646 1647 1648 1649 1650 1651
    {
        i_nb_steps = 1;
    }

    if ( !strcmp(psz_cmd, "volup") )
    {
1652
        if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1653 1654 1655 1656
            i_error = VLC_EGENERIC;
    }
    else
    {
1657
        if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1658 1659
            i_error = VLC_EGENERIC;
    }
1660
    osd_Volume( p_this );
1661

1662
    if ( !i_error ) msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1663 1664 1665
    return i_error;
}

1666 1667 1668 1669

static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
1670
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1671 1672 1673
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    input_thread_t *p_input = NULL;
    vout_thread_t * p_vout;
1674
    const char * psz_variable = NULL;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    int i_error;

    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( !p_input )
        return VLC_ENOOBJ;

    p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
    vlc_object_release( p_input );
    if( !p_vout )
        return VLC_ENOOBJ;

    if( !strcmp( psz_cmd, "vcrop" ) )
    {
        psz_variable = "crop";
    }
    else if( !strcmp( psz_cmd, "vratio" ) )
    {
        psz_variable = "aspect-ratio";
    }
1694
    else if( !strcmp( psz_cmd, "vzoom" ) )
1695 1696 1697
    {
        psz_variable = "zoom";
    }
1698 1699 1700 1701
    else if( !strcmp( psz_cmd, "snapshot" ) )
    {
        psz_variable = "video-snapshot";
    }
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716

    if( newval.psz_string && *newval.psz_string )
    {
        /* set */
        if( !strcmp( psz_variable, "zoom" ) )
        {
            vlc_value_t val;
            val.f_float = atof( newval.psz_string );
            i_error = var_Set( p_vout, psz_variable, val );
        }
        else
        {
            i_error = var_Set( p_vout, psz_variable, newval );
        }
    }
1717 1718
    else  if( !strcmp( psz_cmd, "snapshot" ) )
    {
1719
        vlc_value_t val;
1720
        val.b_bool = true;
1721
        i_error = var_Set( p_vout, psz_variable, val );
1722
    }
1723 1724 1725
    else
    {
        /* get */
1726
        vlc_value_t val_name;
1727 1728
        vlc_value_t val, text;
        int i;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1729
        float f_value = 0.;
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
        char *psz_value = NULL;

        if ( var_Get( p_vout, psz_variable, &val ) < 0 )
        {
            vlc_object_release( p_vout );
            return VLC_EGENERIC;
        }
        if( !strcmp( psz_variable, "zoom" ) )
        {
            f_value = val.f_float;
        }
        else
        {
            psz_value = strdup( val.psz_string );
        }

        if ( var_Change( p_vout, psz_variable,
                         VLC_VAR_GETLIST, &val, &text ) < 0 )
        {
            vlc_object_release( p_vout );
            return VLC_EGENERIC;
        }

1753 1754 1755 1756 1757
        /* Get the descriptive name of the variable */
        var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT,
                    &val_name, NULL );
        if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
        msg_rc( "+----[ %s ]", val_name.psz_string );
        if( !strcmp( psz_variable, "zoom" ) )
        {
            for ( i = 0; i < val.p_list->i_count; i++ )
            {
                if ( f_value == val.p_list->p_values[i].f_float )
                    msg_rc( "| %f - %s *", val.p_list->p_values[i].f_float,
                            text.p_list->p_values[i].psz_string );
                else
                    msg_rc( "| %f - %s", val.p_list->p_values[i].f_float,
                            text.p_list->p_values[i].psz_string );
            }
        }
        else
        {
            for ( i = 0; i < val.p_list->i_count; i++ )
            {
                if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
                    msg_rc( "| %s - %s *", val.p_list->p_values[i].psz_string,
                            text.p_list->p_values[i].psz_string );
                else
                    msg_rc( "| %s - %s", val.p_list->p_values[i].psz_string,
                            text.p_list->p_values[i].psz_string );
            }
            free( psz_value );
        }
        var_Change( p_vout, psz_variable, VLC_VAR_FREELIST,
                    &val, &text );
        msg_rc( "+----[ end of %s ]", val_name.psz_string );

1788
        free( val_name.psz_string );
1789 1790 1791 1792 1793 1794 1795

        i_error = VLC_SUCCESS;
    }
    vlc_object_release( p_vout );
    return i_error;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1796 1797
static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1798
{
Rafaël Carré's avatar
Rafaël Carré committed
1799
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1800
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1801
    input_thread_t *p_input = NULL;
1802 1803
    aout_instance_t * p_aout;
    const char * psz_variable;
Gildas Bazin's avatar
 
Gildas Bazin committed
1804
    vlc_value_t val_name;
1805 1806
    int i_error;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( !p_input )
        return VLC_ENOOBJ;

    if( p_input )
    {
        vlc_value_t val;

        var_Get( p_input, "state", &val );
        if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )        {
1817
            msg_rc( _("Type 'menu select' or 'pause' to continue.") );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1818 1819 1820 1821 1822 1823
            vlc_object_release( p_input );
            return VLC_EGENERIC;
        }
        vlc_object_release( p_input );
    }

1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
    p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
    if ( p_aout == NULL ) return VLC_ENOOBJ;

    if ( !strcmp( psz_cmd, "adev" ) )
    {
        psz_variable = "audio-device";
    }
    else
    {
        psz_variable = "audio-channels";
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1836 1837 1838 1839 1840
    /* Get the descriptive name of the variable */
    var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
                 &val_name, NULL );
    if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);

Gildas Bazin's avatar
 
Gildas Bazin committed
1841
    if ( !*newval.psz_string )
1842 1843
    {
        /* Retrieve all registered ***. */
Gildas Bazin's avatar
 
Gildas Bazin committed
1844 1845
        vlc_value_t val, text;
        int i, i_value;
1846 1847 1848 1849 1850 1851

        if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
        {
            vlc_object_release( (vlc_object_t *)p_aout );
            return VLC_EGENERIC;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
1852
        i_value = val.i_int;
1853 1854

        if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
Gildas Bazin's avatar
 
Gildas Bazin committed
1855
                         VLC_VAR_GETLIST, &val, &text ) < 0 )
1856 1857 1858 1859 1860
        {
            vlc_object_release( (vlc_object_t *)p_aout );
            return VLC_EGENERIC;
        }

1861
        msg_rc( "+----[ %s ]", val_name.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
1862
        for ( i = 0; i < val.p_list->i_count; i++ )
1863
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1864
            if ( i_value == val.p_list->p_values[i].i_int )
1865
                msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1866
                        text.p_list->p_values[i].psz_string );
1867
            else
1868
                msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1869
                        text.p_list->p_values[i].psz_string );
1870 1871
        }
        var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
Gildas Bazin's avatar
 
Gildas Bazin committed
1872
                    &val, &text );
1873
        msg_rc( "+----[ end of %s ]", val_name.psz_string );
1874

1875
        free( val_name.psz_string );
1876 1877 1878 1879 1880
        i_error = VLC_SUCCESS;
    }
    else
    {
        vlc_value_t val;
Gildas Bazin's avatar
 
Gildas Bazin committed
1881
        val.i_int = atoi( newval.psz_string );
1882 1883 1884 1885 1886 1887 1888

        i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
    }
    vlc_object_release( (vlc_object_t *)p_aout );

    return i_error;
}
1889

1890 1891 1892 1893
/* OSD menu commands */
static int Menu( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
1894
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1895 1896
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    playlist_t    *p_playlist = NULL;
1897
    int i_error = VLC_SUCCESS;
1898 1899 1900 1901
    vlc_value_t val;

    if ( !*newval.psz_string )
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
1902
        msg_rc( _("Please provide one of the following parameters:") );
1903
        msg_rc( "[on|off|up|down|left|right|select]" );
1904
        return VLC_EGENERIC;
1905
    }
1906

1907
    p_playlist = pl_Yield( p_this );
1908 1909 1910 1911 1912 1913 1914

    if( p_playlist->p_input )
    {
        var_Get( p_playlist->p_input, "state", &val );
        if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
            ( strcmp( newval.psz_string, "select" ) != 0 ) )
        {
1915
            msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1916 1917 1918 1919
            vlc_object_release( p_playlist );
            return VLC_EGENERIC;
        }
    }
1920
    vlc_object_release( p_playlist );
1921

1922
    val.psz_string = strdup( newval.psz_string );
1923 1924
    if( !val.psz_string )
        return VLC_ENOMEM;
1925 1926
    if( !strcmp( val.psz_string, "on" ) || !strcmp( val.psz_string, "show" ))
        osd_MenuShow( p_this );
1927 1928
    else if( !strcmp( val.psz_string, "off" )
          || !strcmp( val.psz_string, "hide" ) )
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
        osd_MenuHide( p_this );
    else if( !strcmp( val.psz_string, "up" ) )
        osd_MenuUp( p_this );
    else if( !strcmp( val.psz_string, "down" ) )
        osd_MenuDown( p_this );
    else if( !strcmp( val.psz_string, "left" ) )
        osd_MenuPrev( p_this );
    else if( !strcmp( val.psz_string, "right" ) )
        osd_MenuNext( p_this );
    else if( !strcmp( val.psz_string, "select" ) )
        osd_MenuActivate( p_this );
    else
    {
Antoine Cellerier's avatar
Antoine Cellerier committed
1942
        msg_rc( _("Please provide one of the following parameters:") );
1943
        msg_rc( "[on|off|up|down|left|right|select]" );
1944
        i_error = VLC_EGENERIC;
1945 1946
    }

1947
    free( val.psz_string );
1948 1949 1950
    return i_error;
}

1951 1952 1953
static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
1954
    VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
    input_thread_t *p_input = NULL;
    int i_error;

    p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
    if( !p_input )
        return VLC_ENOOBJ;

    if( !strcmp( psz_cmd, "stats" ) )
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1965 1966 1967
        vlc_mutex_lock( &input_GetItem(p_input)->lock );
        updateStatistics( p_intf, input_GetItem(p_input) );
        vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
    }
    /*
     * sanity check
     */
    else
    {
        msg_rc(_("Unknown command!") );
    }

    vlc_object_release( p_input );
    i_error = VLC_SUCCESS;
    return i_error;
}

static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
{
    if( !p_item ) return VLC_EGENERIC;

    vlc_mutex_lock( &p_item->p_stats->lock );
    msg_rc( "+----[ begin of statistical info ]" );

    /* Input */
    msg_rc(_("+-[Incoming]"));
    msg_rc(_("| input bytes read : %8.0f kB"),
            (float)(p_item->p_stats->i_read_bytes)/1000 );
    msg_rc(_("| input bitrate    :   %6.0f kb/s"),
            (float)(p_item->p_stats->f_input_bitrate)*8000 );
    msg_rc(_("| demux bytes read : %8.0f kB"),
            (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
    msg_rc(_("| demux bitrate    :   %6.0f kb/s"),
            (float)(p_item->p_stats->f_demux_bitrate)*8000 );
    msg_rc("|");
    /* Video */
    msg_rc(_("+-[Video Decoding]"));
    msg_rc(_("| video decoded    :    %5i"),
            p_item->p_stats->i_decoded_video );
    msg_rc(_("| frames displayed :    %5i"),
            p_item->p_stats->i_displayed_pictures );
    msg_rc(_("| frames lost      :    %5i"),
            p_item->p_stats->i_lost_pictures );
    msg_rc("|");
    /* Audio*/
    msg_rc(_("+-[Audio Decoding]"));
    msg_rc(_("| audio decoded    :    %5i"),
            p_item->p_stats->i_decoded_audio );
    msg_rc(_("| buffers played   :    %5i"),
            p_item->p_stats->i_played_abuffers );
    msg_rc(_("| buffers lost     :    %5i"),
            p_item->p_stats->i_lost_abuffers );
    msg_rc("|");
    /* Sout */
    msg_rc(_("+-[Streaming]"));
    msg_rc(_("| packets sent     :    %5i"), p_item->p_stats->i_sent_packets );
    msg_rc(_("| bytes sent       : %8.0f kB"),
            (float)(p_item->p_stats->i_sent_bytes)/1000 );
    msg_rc(_("| sending bitrate  :   %6.0f kb/s"),
            (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
    msg_rc("|");
    msg_rc( "+----[ end of statistical info ]" );
    vlc_mutex_unlock( &p_item->p_stats->lock );

    return VLC_SUCCESS;
}

2032
#ifdef WIN32
2033
bool ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2034 2035 2036 2037 2038 2039 2040 2041
{
    INPUT_RECORD input_record;
    DWORD i_dw;

    /* On Win32, select() only works on socket descriptors */
    while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
                                INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
    {
2042
        while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
2043 2044
               ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
                                 1, &i_dw ) )
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
        {
            if( input_record.EventType != KEY_EVENT ||
                !input_record.Event.KeyEvent.bKeyDown ||
                input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
                input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
                input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
                input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
            {
                /* nothing interesting */
                continue;
            }

            p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;

            /* Echo out the command */
            putc( p_buffer[ *pi_size ], stdout );

            /* Handle special keys */
            if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
            {
                putc( '\n', stdout );
                break;
            }
            switch( p_buffer[ *pi_size ] )
            {
            case '\b':
                if( *pi_size )
                {
                    *pi_size -= 2;
                    putc( ' ', stdout );
                    putc( '\b', stdout );
                }
                break;
            case '\r':
                (*pi_size) --;
                break;
            }

            (*pi_size)++;
        }

        if( *pi_size == MAX_LINE_LENGTH ||
            p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
        {
            p_buffer[ *pi_size ] = 0;
2090
            return true;
2091 2092 2093
        }
    }

2094
    return false;
2095 2096 2097
}
#endif

2098
bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2099 2100 2101 2102
{
    int i_read = 0;

#ifdef WIN32
2103
    if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
2104
        return ReadWin32( p_intf, p_buffer, pi_size );
2105 2106 2107
    else if( p_intf->p_sys->i_socket == -1 )
    {
        msleep( INTF_IDLE_SLEEP );
2108
        return false;
2109
    }
2110 2111
#endif

2112 2113 2114
    while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
           (i_read = net_Read( p_intf, p_intf->p_sys->i_socket == -1 ?
                       0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
2115
                  (uint8_t *)p_buffer + *pi_size, 1, false ) ) > 0 )
2116
    {
2117
        if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2118
            break;
2119 2120

        (*pi_size)++;
2121 2122 2123
    }

    /* Connection closed */
2124
    if( i_read <= 0 )
2125
    {
2126 2127 2128 2129 2130 2131
        if( p_intf->p_sys->i_socket != -1 )
        {
            net_Close( p_intf->p_sys->i_socket );
            p_intf->p_sys->i_socket = -1;
        }
        else
2132
        {
2133
            /* Standard input closed: exit */
2134 2135 2136
            vlc_value_t empty;
            Quit( p_intf, NULL, empty, empty, NULL );
        }
2137

2138
        p_buffer[ *pi_size ] = 0;
2139
        return true;
2140 2141 2142 2143 2144 2145
    }

    if( *pi_size == MAX_LINE_LENGTH ||
        p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
    {
        p_buffer[ *pi_size ] = 0;
2146
        return true;
2147 2148
    }

2149
    return false;
2150
}
2151 2152

/*****************************************************************************
2153
 * parse_MRL: build a input item from a full mrl
2154 2155 2156 2157 2158
 *****************************************************************************
 * MRL format: "simplified-mrl [:option-name[=option-value]]"
 * We don't check for '"' or '\'', we just assume that a ':' that follows a
 * space is a new option. Should be good enough for our purpose.
 *****************************************************************************/
2159
static input_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
2160
{
Rafaël Carré's avatar
Rafaël Carré committed
2161
#define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
2162 2163 2164
#define SKIPTRAILINGSPACE( p, d ) \
    { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }

2165
    input_item_t *p_item = NULL;
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
    char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
    char **ppsz_options = NULL;
    int i, i_options = 0;

    if( !psz_mrl ) return 0;

    psz_mrl = psz_orig = strdup( psz_mrl );
    while( *psz_mrl )
    {
        SKIPSPACE( psz_mrl );
        psz_item = psz_mrl;

        for( ; *psz_mrl; psz_mrl++ )
        {
            if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
            {
                /* We have a complete item */
                break;
            }
            if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
                (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
            {
                /* We have a complete item */
                break;
            }
        }

        if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
        SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );

        /* Remove '"' and '\'' if necessary */
        if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
        { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
        if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
        { psz_item++; psz_item[strlen(psz_item)-1] = 0; }

        if( !psz_item_mrl ) psz_item_mrl = psz_item;
        else if( *psz_item )
        {
            i_options++;
            ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
            ppsz_options[i_options - 1] = &psz_item[1];
        }

        if( *psz_mrl ) SKIPSPACE( psz_mrl );
    }

    /* Now create a playlist item */
    if( psz_item_mrl )
    {
2216
        p_item = input_ItemNew( p_intf, psz_item_mrl, NULL );
2217 2218
        for( i = 0; i < i_options; i++ )
        {
2219
            input_ItemAddOption( p_item, ppsz_options[i] );
2220 2221 2222 2223 2224 2225 2226 2227
        }
    }

    if( i_options ) free( ppsz_options );
    free( psz_orig );

    return p_item;
}