rc.c 64.3 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-2009 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 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <errno.h>                                                 /* ENOMEM */
34
#include <signal.h>
35
#include <assert.h>
36
#include <math.h>
37

38 39
#include <vlc_common.h>
#include <vlc_plugin.h>
40
#include <vlc_interface.h>
41
#include <vlc_input.h>
42
#include <vlc_aout.h>
43 44
#include <vlc_vout.h>
#include <vlc_playlist.h>
45
#include <vlc_keys.h>
46 47

#include <sys/types.h>
48
#include <unistd.h>
49

50

51
#include <vlc_network.h>
52
#include <vlc_url.h>
53
#include <vlc_charset.h>
54

55 56
#if defined(PF_UNIX) && !defined(PF_LOCAL)
#    define PF_LOCAL PF_UNIX
57 58
#endif

59
#if defined(AF_LOCAL) && ! defined(_WIN32)
60 61
#    include <sys/un.h>
#endif
62

63
#define MAX_LINE_LENGTH 1024
64
#define STATUS_CHANGE "status change: "
65

66 67
/* input_state_e from <vlc_input.h> */
static const char *ppsz_input_state[] = {
68 69 70 71 72 73
    [INIT_S] = N_("Initializing"),
    [OPENING_S] = N_("Opening"),
    [PLAYING_S] = N_("Play"),
    [PAUSE_S] = N_("Pause"),
    [END_S] = N_("End"),
    [ERROR_S] = N_("Error"),
74
};
75

76 77 78 79
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate     ( vlc_object_t * );
80
static void Deactivate   ( vlc_object_t * );
81
static void *Run         ( void * );
82

83
static void Help         ( intf_thread_t * );
84
static void RegisterCallbacks( intf_thread_t * );
85

86
static bool ReadCommand( intf_thread_t *, char *, int * );
87

88
static input_item_t *parse_MRL( const char * );
89

90 91
static int  Input        ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
92 93 94 95 96 97 98 99 100 101
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 * );
102 103
static int  VideoConfig  ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
104 105 106
static int  AudioDevice  ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );
static int  AudioChannel ( vlc_object_t *, char const *,
107
                           vlc_value_t, vlc_value_t, void * );
108 109 110 111
static int  Statistics   ( vlc_object_t *, char const *,
                           vlc_value_t, vlc_value_t, void * );

static int updateStatistics( intf_thread_t *, input_item_t *);
112 113

/* Status Callbacks */
114 115 116 117
static int VolumeChanged( vlc_object_t *, char const *,
                          vlc_value_t, vlc_value_t, void * );
static int InputEvent( 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
    vlc_thread_t thread;
125

126 127
    /* status changes */
    vlc_mutex_t       status_lock;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
128
    int               i_last_state;
129
    playlist_t        *p_playlist;
130
    input_thread_t    *p_input;
131
    bool              b_input_buffering;
132

133
#ifdef _WIN32
134
    HANDLE hConsoleIn;
135
    bool b_quiet;
136 137 138
#endif
};

139
VLC_FORMAT(2, 3)
140
static void msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
141 142
{
    va_list args;
143 144 145
    char fmt_eol[strlen (psz_fmt) + 3];

    snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
146
    va_start( args, psz_fmt );
147

148
    if( p_intf->p_sys->i_socket == -1 )
149
        utf8_vfprintf( stdout, fmt_eol, args );
150
    else
151
        net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, fmt_eol, args );
152 153
    va_end( args );
}
154
#define msg_rc( ... ) msg_rc( p_intf, __VA_ARGS__ )
155

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

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

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

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

174
#ifdef _WIN32
175 176 177 178 179 180 181 182
#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

183 184 185 186 187
vlc_module_begin ()
    set_shortname( N_("RC"))
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_description( N_("Remote control interface") )
188
    add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
189

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

200
    set_capability( "interface", 20 )
201

202
    set_callbacks( Activate, Deactivate )
203
#ifdef _WIN32
204 205
    add_shortcut( "rc" )
#endif
206
vlc_module_end ()
207 208 209 210 211 212

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
213
    /* FIXME: This function is full of memory leaks and bugs in error paths. */
214
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
215
    playlist_t *p_playlist = pl_Get( p_intf );
216
    char *psz_host, *psz_unix_path = NULL;
217
    int  *pi_socket = NULL;
218

219
#ifndef _WIN32
220
#if defined(HAVE_ISATTY)
221
    /* Check that stdin is a TTY */
222
    if( !var_InheritBool( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
223 224
    {
        msg_Warn( p_intf, "fd 0 is not a TTY" );
225
        return VLC_EGENERIC;
226 227 228
    }
#endif

229
    psz_unix_path = var_InheritString( p_intf, "rc-unix" );
230
    if( psz_unix_path )
231
    {
232
        int i_socket;
233

234
#ifndef AF_LOCAL
235
        msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
236
        free( psz_unix_path );
237 238 239 240 241 242 243 244
        return VLC_EGENERIC;
#else
        struct sockaddr_un addr;

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

        msg_Dbg( p_intf, "trying UNIX socket" );

245
        if( (i_socket = vlc_socket( PF_LOCAL, SOCK_STREAM, 0, false ) ) < 0 )
246
        {
247
            msg_Warn( p_intf, "can't open socket: %s", vlc_strerror_c(errno) );
248
            free( psz_unix_path );
249 250 251
            return VLC_EGENERIC;
        }

252 253 254
        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';
255

256 257 258 259
        if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
         && (errno == EADDRINUSE)
         && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
         && (errno == ECONNREFUSED))
260
        {
261 262 263 264
            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)))
265
            {
266 267
                msg_Err (p_intf, "cannot bind UNIX socket at %s: %s",
                         psz_unix_path, vlc_strerror_c(errno));
268 269
                free (psz_unix_path);
                net_Close (i_socket);
270 271
                return VLC_EGENERIC;
            }
272 273
        }

274
        if( listen( i_socket, 1 ) )
275
        {
276 277
            msg_Warn (p_intf, "can't listen on socket: %s",
                      vlc_strerror_c(errno));
278 279
            free( psz_unix_path );
            net_Close( i_socket );
280 281
            return VLC_EGENERIC;
        }
282 283 284 285 286 287 288 289 290 291 292

        /* 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;
293
#endif /* AF_LOCAL */
294
    }
295
#endif /* !_WIN32 */
296

297
    if( ( pi_socket == NULL ) &&
298
        ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
299 300 301
    {
        vlc_url_t url;

302 303
        vlc_UrlParse( &url, psz_host, 0 );

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

306 307
        pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
        if( pi_socket == NULL )
308
        {
309 310
            msg_Warn( p_intf, "can't listen to %s port %i",
                      url.psz_host, url.i_port );
311 312 313 314 315 316
            vlc_UrlClean( &url );
            free( psz_host );
            return VLC_EGENERIC;
        }

        vlc_UrlClean( &url );
317
        free( psz_host );
318 319
    }

320 321
    intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
    if( unlikely(p_sys == NULL) )
322
    {
323
        net_ListenClose( pi_socket );
324
        free( psz_unix_path );
325
        return VLC_ENOMEM;
326
    }
327

328 329 330 331 332 333 334 335 336
    p_intf->p_sys = p_sys;
    p_sys->pi_socket_listen = pi_socket;
    p_sys->i_socket = -1;
    p_sys->psz_unix_path = psz_unix_path;
    vlc_mutex_init( &p_sys->status_lock );
    p_sys->i_last_state = PLAYLIST_STOPPED;
    p_sys->b_input_buffering = false;
    p_sys->p_playlist = p_playlist;
    p_sys->p_input = NULL;
337

338 339 340
    /* Non-buffered stdout */
    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );

341
#ifdef _WIN32
342 343
    p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
    if( !p_sys->b_quiet )
344
#endif
345 346 347
    {
        CONSOLE_INTRO_MSG;
    }
348

349
    if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
350 351
        abort();

352
    msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
353 354 355

    /* Listen to audio volume updates */
    var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
356
    return VLC_SUCCESS;
357 358
}

359 360 361 362 363 364
/*****************************************************************************
 * Deactivate: uninitialize and cleanup
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
365 366
    intf_sys_t *p_sys = p_intf->p_sys;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
367
    vlc_cancel( p_sys->thread );
368 369
    var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
    vlc_join( p_sys->thread, NULL );
370

371 372 373 374 375
    if( p_sys->p_input != NULL )
    {
        var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
        vlc_object_release( p_sys->p_input );
    }
376

377 378 379 380
    net_ListenClose( p_sys->pi_socket_listen );
    if( p_sys->i_socket != -1 )
        net_Close( p_sys->i_socket );
    if( p_sys->psz_unix_path != NULL )
381
    {
382
#if defined(AF_LOCAL) && !defined(_WIN32)
383
        unlink( p_sys->psz_unix_path );
384
#endif
385
        free( p_sys->psz_unix_path );
386
    }
387 388
    vlc_mutex_destroy( &p_sys->status_lock );
    free( p_sys );
389 390
}

391
/*****************************************************************************
392
 * RegisterCallbacks: Register callbacks to dynamic variables
393
 *****************************************************************************/
394
static void RegisterCallbacks( intf_thread_t *p_intf )
395
{
396
    /* Register commands that will be cleaned up upon object destruction */
397 398 399 400 401 402 403 404 405
#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 )
406
    ADD( "random", STRING, Playlist )
407 408 409 410 411 412 413 414 415 416
    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 )
417

418
    /* DVD commands */
419 420 421 422 423 424 425 426 427 428 429 430 431 432
    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 )
433
    ADD( "frame", VOID, Input )
434 435 436 437

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

439
    /* video commands */
440 441 442
    ADD( "vratio", STRING, VideoConfig )
    ADD( "vcrop", STRING, VideoConfig )
    ADD( "vzoom", STRING, VideoConfig )
443
    ADD( "snapshot", VOID, VideoConfig )
444

445
    /* audio commands */
446 447 448
    ADD( "volume", STRING, Volume )
    ADD( "volup", STRING, VolumeMove )
    ADD( "voldown", STRING, VolumeMove )
449 450
    ADD( "adev", STRING, AudioDevice )
    ADD( "achan", STRING, AudioChannel )
451

452 453 454
    /* misc menu commands */
    ADD( "stats", BOOL, Statistics )

455
#undef ADD
456 457 458 459 460 461 462 463
}

/*****************************************************************************
 * 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.
 *****************************************************************************/
464
static void *Run( void *data )
465
{
466
    intf_thread_t *p_intf = data;
467
    intf_sys_t *p_sys = p_intf->p_sys;
468

469
    char p_buffer[ MAX_LINE_LENGTH + 1 ];
470
    bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
471

472 473 474
    int  i_size = 0;
    int  i_oldpos = 0;
    int  i_newpos;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
475
    int  canc = vlc_savecancel( );
476 477 478

    p_buffer[0] = 0;

479
#ifdef _WIN32
480
    /* Get the file descriptor of the console input */
481 482
    p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
    if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
483
    {
484
        msg_Err( p_intf, "couldn't find user input handle" );
485
        return NULL;
486 487 488
    }
#endif

489 490 491 492 493
    /* Register commands that will be cleaned up upon object destruction */
    RegisterCallbacks( p_intf );

    /* status callbacks */

494
    for( ;; )
495
    {
496
        char *psz_cmd, *psz_arg;
497
        bool b_complete;
498

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
499 500
        vlc_restorecancel( canc );

501
        if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
502
        {
503 504 505
            p_sys->i_socket =
                net_Accept( p_intf, p_sys->pi_socket_listen );
            if( p_sys->i_socket == -1 ) continue;
506 507
        }

508
        b_complete = ReadCommand( p_intf, p_buffer, &i_size );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
509
        canc = vlc_savecancel( );
510

511
        /* Manage the input part */
512
        if( p_sys->p_input == NULL )
513
        {
514
            p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
515
            /* New input has been registered */
516
            if( p_sys->p_input )
517
            {
518
                char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
519 520 521
                msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
                free( psz_uri );

522
                var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
523
            }
524
        }
525 526
#warning This is not reliable...
        else if( p_sys->p_input->b_dead )
527
        {
528 529 530
            var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
            vlc_object_release( p_sys->p_input );
            p_sys->p_input = NULL;
531

532 533
            p_sys->i_last_state = PLAYLIST_STOPPED;
            msg_rc( STATUS_CHANGE "( stop state: 0 )" );
534
        }
535

536
        if( p_sys->p_input != NULL )
537
        {
538 539
            playlist_t *p_playlist = p_sys->p_playlist;

540
            PL_LOCK;
541
            int status = playlist_Status( p_playlist );
Jakob Leben's avatar
Jakob Leben committed
542 543
            PL_UNLOCK;

544
            if( p_sys->i_last_state != status )
545
            {
Rémi Duraffort's avatar
Rémi Duraffort committed
546 547
                if( status == PLAYLIST_STOPPED )
                {
548
                    p_sys->i_last_state = PLAYLIST_STOPPED;
Rémi Duraffort's avatar
Rémi Duraffort committed
549 550 551 552
                    msg_rc( STATUS_CHANGE "( stop state: 5 )" );
                }
                else if( status == PLAYLIST_RUNNING )
                {
553
                    p_sys->i_last_state = PLAYLIST_RUNNING;
Rémi Duraffort's avatar
Rémi Duraffort committed
554 555 556 557
                    msg_rc( STATUS_CHANGE "( play state: 3 )" );
                }
                else if( status == PLAYLIST_PAUSED )
                {
558
                    p_sys->i_last_state = PLAYLIST_PAUSED;
Rémi Duraffort's avatar
Rémi Duraffort committed
559 560
                    msg_rc( STATUS_CHANGE "( pause state: 4 )" );
                }
561
            }
562 563
        }

564
        if( p_sys->p_input && b_showpos )
565
        {
566
            i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
567
            if( i_oldpos != i_newpos )
568
            {
569
                i_oldpos = i_newpos;
570
                msg_rc( "pos: %d%%", i_newpos );
571 572 573 574
            }
        }

        /* Is there something to do? */
575 576 577 578 579
        if( !b_complete ) continue;

        /* Skip heading spaces */
        psz_cmd = p_buffer;
        while( *psz_cmd == ' ' )
580
        {
581 582
            psz_cmd++;
        }
583

584 585 586 587 588 589 590
        /* 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 == ' ' )
591
            {
592
                psz_arg++;
593
            }
594 595 596
        }
        else
        {
Rafaël Carré's avatar
Rafaël Carré committed
597
            psz_arg = (char*)"";
598 599 600
        }

        /* If the user typed a registered local command, try it */
601
        if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
602 603 604 605
        {
            vlc_value_t val;
            int i_ret;
            val.psz_string = psz_arg;
606 607 608 609 610

            if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
                i_ret = var_TriggerCallback( p_intf, psz_cmd );
            else
                i_ret = var_Set( p_intf, psz_cmd, val );
611
            msg_rc( "%s: returned %i (%s)",
612 613 614
                    psz_cmd, i_ret, vlc_error( i_ret ) );
        }
        /* Or maybe it's a global command */
615
        else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
616 617 618 619 620 621
        {
            vlc_value_t val;
            int i_ret;

            val.psz_string = psz_arg;
            /* FIXME: it's a global command, but we should pass the
622
             * local object as an argument, not p_intf->p_libvlc. */
623 624 625 626
            if ((var_Type( p_intf->p_libvlc, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
                i_ret = var_TriggerCallback( p_intf, psz_cmd );
            else
                i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
627 628
            if( i_ret != 0 )
            {
629
                msg_rc( "%s: returned %i (%s)",
630 631
                         psz_cmd, i_ret, vlc_error( i_ret ) );
            }
632
        }
633 634 635
        else if( !strcmp( psz_cmd, "logout" ) )
        {
            /* Close connection */
636
            if( p_sys->i_socket != -1 )
637
            {
638 639
                net_Close( p_sys->i_socket );
                p_sys->i_socket = -1;
640 641
            }
        }
642 643
        else if( !strcmp( psz_cmd, "info" ) )
        {
644
            if( p_sys->p_input )
645
            {
646
                int i, j;
647 648
                vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
                for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
649
                {
650
                    info_category_t *p_category = input_GetItem(p_sys->p_input)
651
                                                        ->pp_categories[i];
652

653 654
                    msg_rc( "+----[ %s ]", p_category->psz_name );
                    msg_rc( "| " );
655 656 657
                    for ( j = 0; j < p_category->i_infos; j++ )
                    {
                        info_t *p_info = p_category->pp_infos[j];
658
                        msg_rc( "| %s: %s", p_info->psz_name,
659 660
                                p_info->psz_value );
                    }
661
                    msg_rc( "| " );
662
                }
663
                msg_rc( "+----[ end of stream info ]" );
664
                vlc_mutex_unlock( &input_GetItem(p_sys->p_input)->lock );
665
            }
666
            else
667
            {
668
                msg_rc( "no input" );
Sam Hocevar's avatar
Sam Hocevar committed
669
            }
670
        }
671 672
        else if( !strcmp( psz_cmd, "is_playing" ) )
        {
673
            if( p_sys->p_input == NULL )
674
            {
675
                msg_rc( "0" );
676 677 678
            }
            else
            {
679
                msg_rc( "1" );
680 681 682 683
            }
        }
        else if( !strcmp( psz_cmd, "get_time" ) )
        {
684
            if( p_sys->p_input == NULL )
685
            {
686
                msg_rc("0");
687 688 689 690
            }
            else
            {
                vlc_value_t time;
691
                var_Get( p_sys->p_input, "time", &time );
692
                msg_rc( "%"PRIu64, time.i_time / 1000000);
693 694 695 696
            }
        }
        else if( !strcmp( psz_cmd, "get_length" ) )
        {
697
            if( p_sys->p_input == NULL )
698
            {
699
                msg_rc("0");
700 701 702 703
            }
            else
            {
                vlc_value_t time;
704
                var_Get( p_sys->p_input, "length", &time );
705
                msg_rc( "%"PRIu64, time.i_time / 1000000);
706 707 708 709
            }
        }
        else if( !strcmp( psz_cmd, "get_title" ) )
        {
710
            if( p_sys->p_input == NULL )
711
            {
712
                msg_rc("%s", "");
713 714 715
            }
            else
            {
716
                msg_rc( "%s", input_GetItem(p_sys->p_input)->psz_name );
717 718
            }
        }
719 720
        else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
                 || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
721
        {
722
            Help( p_intf );
723
        }
724 725
        else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
        {
726 727
            var_SetInteger( p_intf->p_libvlc, "key-action",
                            vlc_GetActionId( psz_arg ) );
728
        }
729 730 731 732
        else switch( psz_cmd[0] )
        {
        case 'f':
        case 'F':
733 734 735 736
        {
            bool fs;

            if( !strncasecmp( psz_arg, "on", 2 ) )
737
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
738
            else if( !strncasecmp( psz_arg, "off", 3 ) )
739
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
740
            else
741
                fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
742

743
            if( p_sys->p_input != NULL )
744
            {
745
                vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
746 747
                if( p_vout )
                {
748
                    var_SetBool( p_vout, "fullscreen", fs );
749 750 751 752
                    vlc_object_release( p_vout );
                }
            }
            break;
753
        }
754 755 756
        case 's':
        case 'S':
            ;
757 758 759 760 761 762 763
            break;

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

        default:
764
            msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
765
            break;
766
        }
767 768 769

        /* Command processed */
        i_size = 0; p_buffer[0] = 0;
770 771
    }

772 773
    msg_rc( STATUS_CHANGE "( stop state: 0 )" );
    msg_rc( STATUS_CHANGE "( quit )" );
774

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
775 776
    vlc_restorecancel( canc );

777
    return NULL;
778 779
}

780
static void Help( intf_thread_t *p_intf)
781
{
782
    msg_rc("%s", _("+----[ Remote control commands ]"));
783
    msg_rc(  "| ");
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    msg_rc("%s", _("| add XYZ  . . . . . . . . . . . . add XYZ to playlist"));
    msg_rc("%s", _("| enqueue XYZ  . . . . . . . . . queue XYZ to playlist"));
    msg_rc("%s", _("| playlist . . . . .  show items currently in playlist"));
    msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
    msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
    msg_rc("%s", _("| next . . . . . . . . . . . . . .  next playlist item"));
    msg_rc("%s", _("| prev . . . . . . . . . . . .  previous playlist item"));
    msg_rc("%s", _("| goto . . . . . . . . . . . . . .  goto item at index"));
    msg_rc("%s", _("| repeat [on|off] . . . .  toggle playlist item repeat"));
    msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
    msg_rc("%s", _("| random [on|off] . . . . . . .  toggle random jumping"));
    msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
    msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
    msg_rc("%s", _("| title [X]  . . . . . . set/get title in current item"));
    msg_rc("%s", _("| title_n  . . . . . . . .  next title in current item"));
    msg_rc("%s", _("| title_p  . . . . . .  previous title in current item"));
    msg_rc("%s", _("| chapter [X]  . . . . set/get chapter in current item"));
    msg_rc("%s", _("| chapter_n  . . . . . .  next chapter in current item"));
    msg_rc("%s", _("| chapter_p  . . . .  previous chapter in current item"));
803
    msg_rc(  "| ");
804 805 806 807 808 809 810
    msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
    msg_rc("%s", _("| pause  . . . . . . . . . . . . . . . .  toggle pause"));
    msg_rc("%s", _("| fastforward  . . . . . . . .  .  set to maximum rate"));
    msg_rc("%s", _("| rewind  . . . . . . . . . . . .  set to minimum rate"));
    msg_rc("%s", _("| faster . . . . . . . . . .  faster playing of stream"));
    msg_rc("%s", _("| slower . . . . . . . . . .  slower playing of stream"));
    msg_rc("%s", _("| normal . . . . . . . . . .  normal playing of stream"));
811
    msg_rc("%s", _("| frame. . . . . . . . . .  play frame by frame"));
812 813 814 815 816 817 818
    msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
    msg_rc("%s", _("| info . . . . .  information about the current stream"));
    msg_rc("%s", _("| stats  . . . . . . . .  show statistical information"));
    msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
    msg_rc("%s", _("| is_playing . . . .  1 if a stream plays, 0 otherwise"));
    msg_rc("%s", _("| get_title . . . . .  the title of the current stream"));
    msg_rc("%s", _("| get_length . . . .  the length of the current stream"));
819
    msg_rc(  "| ");
820 821 822
    msg_rc("%s", _("| volume [X] . . . . . . . . . .  set/get audio volume"));
    msg_rc("%s", _("| volup [X]  . . . . . . .  raise audio volume X steps"));
    msg_rc("%s", _("| voldown [X]  . . . . . .  lower audio volume X steps"));
823
    msg_rc("%s", _("| adev [device]  . . . . . . . .  set/get audio device"));
824 825 826 827 828 829 830
    msg_rc("%s", _("| achan [X]. . . . . . . . . .  set/get audio channels"));
    msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
    msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
    msg_rc("%s", _("| vratio [X]  . . . . . . . set/get video aspect ratio"));
    msg_rc("%s", _("| vcrop [X]  . . . . . . . . . . .  set/get video crop"));
    msg_rc("%s", _("| vzoom [X]  . . . . . . . . . . .  set/get video zoom"));
    msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
831
    msg_rc("%s", _("| strack [X] . . . . . . . . .  set/get subtitle track"));
832
    msg_rc("%s", _("| key [hotkey name] . . . . . .  simulate hotkey press"));
833
    msg_rc(  "| ");
834 835 836
    msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
    msg_rc("%s", _("| logout . . . . . . .  exit (if in socket connection)"));
    msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . .  quit vlc"));
837
    msg_rc(  "| ");
838
    msg_rc("%s", _("+----[ end of help ]"));
839 840 841 842 843 844 845 846
}

/********************************************************************
 * Status callback routines
 ********************************************************************/
static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
    vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
847
    (void) p_this;
Rafaël Carré's avatar
Rafaël Carré committed
848
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
849
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
850

851
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
852 853
    msg_rc( STATUS_CHANGE "( audio volume: %ld )",
            lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
854 855 856 857
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
}

858
static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
859
{
860
    playlist_t *p_playlist = p_intf->p_sys->p_playlist;
861

862
    PL_LOCK;
863
    const int i_status = playlist_Status( p_playlist );
864 865
    PL_UNLOCK;

866
    /* */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
867
    const char *psz_cmd;
868
    switch( i_status )
869
    {
870
    case PLAYLIST_STOPPED:
871
        psz_cmd = "stop";
872 873
        break;
    case PLAYLIST_RUNNING:
874
        psz_cmd = "play";
875 876
        break;
    case PLAYLIST_PAUSED:
877
        psz_cmd = "pause";
878 879
        break;
    default:
880 881 882 883 884 885
        psz_cmd = "";
        break;
    }

    /* */
    const int i_state = var_GetInteger( p_input, "state" );
886

887 888 889 890 891 892 893 894 895
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
            i_state, ppsz_input_state[i_state] );
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
}
static void RateChanged( intf_thread_t *p_intf,
                         input_thread_t *p_input )
{
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
896 897
    msg_rc( STATUS_CHANGE "( new rate: %.3f )",
            var_GetFloat( p_input, "rate" ) );
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
}
static void PositionChanged( intf_thread_t *p_intf,
                             input_thread_t *p_input )
{
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    if( p_intf->p_sys->b_input_buffering )
        msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
                (var_GetTime( p_input, "time" )/1000000) );
    p_intf->p_sys->b_input_buffering = false;
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
}
static void CacheChanged( intf_thread_t *p_intf )
{
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
    p_intf->p_sys->b_input_buffering = true;
914
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
915 916
}

917 918
static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
919
{
Pierre's avatar
Pierre committed
920 921
    VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval);
922 923
    input_thread_t *p_input = (input_thread_t*)p_this;
    intf_thread_t *p_intf = p_data;
924

925
    switch( newval.i_int )
926
    {
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    case INPUT_EVENT_STATE:
    case INPUT_EVENT_DEAD:
        StateChanged( p_intf, p_input );
        break;
    case INPUT_EVENT_RATE:
        RateChanged( p_intf, p_input );
        break;
    case INPUT_EVENT_POSITION:
        PositionChanged( p_intf, p_input );
        break;
    case INPUT_EVENT_CACHE:
        CacheChanged( p_intf );
        break;
    default:
        break;
942 943 944 945 946 947 948
    }
    return VLC_SUCCESS;
}

/********************************************************************
 * Command routines
 ********************************************************************/
949 950
static int Input( vlc_object_t *p_this, char const *psz_cmd,
                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
951
{
Rafaël Carré's avatar
Rafaël Carré committed
952
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
953
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
954 955 956
    input_thread_t *p_input =
        playlist_CurrentInput( p_intf->p_sys->p_playlist );
    int i_error = VLC_EGENERIC;
957

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
958 959
    if( !p_input )
        return VLC_ENOOBJ;
960

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
961 962
    int state = var_GetInteger( p_input, "state" );
    if( ( state == PAUSE_S ) &&
963
        ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
964
    {
965
        msg_rc( "%s", _("Press pause to continue.") );
966
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
967
    else
968 969 970
    /* Parse commands that only require an input */
    if( !strcmp( psz_cmd, "pause" ) )
    {
971
        playlist_TogglePause( p_intf->p_sys->p_playlist );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
972
        i_error = VLC_SUCCESS;
973
    }
974 975
    else if( !strcmp( psz_cmd, "seek" ) )
    {
976 977 978
        if( strlen( newval.psz_string ) > 0 &&
            newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
979 980
            float f = atof( newval.psz_string ) / 100.0;
            var_SetFloat( p_input, "position", f );
981 982 983
        }
        else
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
984 985
            mtime_t t = ((int64_t)atoi( newval.psz_string )) * CLOCK_FREQ;
            var_SetTime( p_input, "time", t );
986
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
987
        i_error = VLC_SUCCESS;
988
    }
989 990
    else if ( !strcmp( psz_cmd, "fastforward" ) )
    {
991
        if( var_GetBool( p_input, "can-rate" ) )
992
        {
993 994 995
            float f_rate = var_GetFloat( p_input, "rate" );
            f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
            var_SetFloat( p_input, "rate", f_rate );
996 997 998
        }
        else
        {
999
            var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
1000
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1001
        i_error = VLC_SUCCESS;
1002 1003 1004
    }
    else if ( !strcmp( psz_cmd, "rewind" ) )
    {
1005 1006
        if( var_GetBool( p_input, "can-rewind" ) )
        {
1007 1008 1009
            float f_rate = var_GetFloat( p_input, "rate" );
            f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
            var_SetFloat( p_input, "rate", f_rate );
1010 1011 1012
        }
        else
        {
1013
            var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1014
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1015
        i_error = VLC_SUCCESS;
1016 1017 1018
    }
    else if ( !strcmp( psz_cmd, "faster" ) )
    {
1019
        var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1020
        i_error = VLC_SUCCESS;
1021 1022 1023
    }
    else if ( !strcmp( psz_cmd, "slower" ) )
    {
1024
        var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1025
        i_error = VLC_SUCCESS;
1026 1027 1028
    }
    else if ( !strcmp( psz_cmd, "normal" ) )
    {
1029
        var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1030
        i_error = VLC_SUCCESS;
1031
    }
1032 1033 1034 1035 1036
    else if ( !strcmp( psz_cmd, "frame" ) )
    {
	var_TriggerCallback( p_input, "frame-next" );
        i_error = VLC_SUCCESS;
    }
1037 1038 1039 1040 1041 1042
    else if( !strcmp( psz_cmd, "chapter" ) ||
             !strcmp( psz_cmd, "chapter_n" ) ||
             !strcmp( psz_cmd, "chapter_p" ) )
    {
        if( !strcmp( psz_cmd, "chapter" ) )
        {
1043
            if ( *newval.psz_string )
1044 1045
            {
                /* Set. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1046
                var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1047 1048 1049 1050
            }
            else
            {
                /* Get. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1051
                int i_chap = var_GetInteger( p_input, "chapter" );
1052
                int i_chapter_count = var_CountChoices( p_input, "chapter" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1053
                msg_rc( "Currently playing chapter %d/%d.", i_chap,
1054
                        i_chapter_count );
1055 1056 1057
            }
        }
        else if( !strcmp( psz_cmd, "chapter_n" ) )
1058
            var_TriggerCallback( p_input, "next-chapter" );
1059
        else if( !strcmp( psz_cmd, "chapter_p" ) )
1060
            var_TriggerCallback( p_input, "prev-chapter" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1061
        i_error = VLC_SUCCESS;
1062 1063 1064 1065 1066 1067 1068
    }
    else if( !strcmp( psz_cmd, "title" ) ||
             !strcmp( psz_cmd, "title_n" ) ||
             !strcmp( psz_cmd, "title_p" ) )
    {
        if( !strcmp( psz_cmd, "title" ) )
        {
1069
            if ( *newval.psz_string )
1070
                /* Set. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1071
                var_SetInteger( p_input, "title", atoi( newval.psz_string ) );
1072 1073 1074
            else
            {
                /* Get. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1075
                int i_title = var_GetInteger( p_input, "title" );
1076
                int i_title_count = var_CountChoices( p_input, "title" );