oldrc.c 64.2 KB
Newer Older
1
/*****************************************************************************
2
 * oldrc.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
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
39 40
#include <vlc_common.h>
#include <vlc_plugin.h>
41
#include <vlc_interface.h>
42
#include <vlc_input.h>
43
#include <vlc_aout.h>
44 45
#include <vlc_vout.h>
#include <vlc_playlist.h>
46
#include <vlc_keys.h>
47 48

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

51

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

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

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

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

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

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

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

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

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

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

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

/* Status Callbacks */
115 116 117 118
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 * );
119

120 121
struct intf_sys_t
{
122
    int *pi_socket_listen;
123
    int i_socket;
124
    char *psz_unix_path;
125
    vlc_thread_t thread;
126

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

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

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

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

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

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

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

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

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

175
#ifdef _WIN32
176 177 178 179 180 181
#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." )
182
#if !VLC_WINSTORE_APP
183
#include "intromsg.h"
184
#endif
185
#endif
186

187 188 189 190 191
vlc_module_begin ()
    set_shortname( N_("RC"))
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_description( N_("Remote control interface") )
192
    add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
193

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

204
    set_capability( "interface", 20 )
205

206
    set_callbacks( Activate, Deactivate )
207
#ifdef _WIN32
208 209
    add_shortcut( "rc" )
#endif
210
vlc_module_end ()
211 212 213 214 215 216

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

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

233
    psz_unix_path = var_InheritString( p_intf, "rc-unix" );
234
    if( psz_unix_path )
235
    {
236
        int i_socket;
237

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

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

        msg_Dbg( p_intf, "trying UNIX socket" );

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

256 257 258
        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';
259

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

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

        /* 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;
297
#endif /* AF_LOCAL */
298
    }
299
#endif /* !_WIN32 */
300

301
    if( ( pi_socket == NULL ) &&
302
        ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
303 304 305
    {
        vlc_url_t url;

306
        vlc_UrlParse( &url, psz_host );
307

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

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

        vlc_UrlClean( &url );
321
        free( psz_host );
322 323
    }

324 325
    intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
    if( unlikely(p_sys == NULL) )
326
    {
327
        net_ListenClose( pi_socket );
328
        free( psz_unix_path );
329
        return VLC_ENOMEM;
330
    }
331

332 333 334 335 336 337 338 339 340
    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;
341

342 343 344
    /* Non-buffered stdout */
    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );

345 346 347
#if VLC_WINSTORE_APP
    p_sys->b_quiet = true;
#elif defined(_WIN32)
348 349
    p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
    if( !p_sys->b_quiet )
350
        intf_consoleIntroMsg( p_intf );
351
#endif
352

353
    if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
354 355
        abort();

356
    msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
357 358 359

    /* Listen to audio volume updates */
    var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
360
    return VLC_SUCCESS;
361 362
}

363 364 365 366 367 368
/*****************************************************************************
 * Deactivate: uninitialize and cleanup
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
369 370
    intf_sys_t *p_sys = p_intf->p_sys;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
371
    vlc_cancel( p_sys->thread );
372 373
    var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
    vlc_join( p_sys->thread, NULL );
374

375 376 377 378 379
    if( p_sys->p_input != NULL )
    {
        var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
        vlc_object_release( p_sys->p_input );
    }
380

381 382 383 384
    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 )
385
    {
386
#if defined(AF_LOCAL) && !defined(_WIN32)
387
        unlink( p_sys->psz_unix_path );
388
#endif
389
        free( p_sys->psz_unix_path );
390
    }
391 392
    vlc_mutex_destroy( &p_sys->status_lock );
    free( p_sys );
393 394
}

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

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

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

443
    /* video commands */
444 445 446
    ADD( "vratio", STRING, VideoConfig )
    ADD( "vcrop", STRING, VideoConfig )
    ADD( "vzoom", STRING, VideoConfig )
447
    ADD( "snapshot", VOID, VideoConfig )
448

449
    /* audio commands */
450 451 452
    ADD( "volume", STRING, Volume )
    ADD( "volup", STRING, VolumeMove )
    ADD( "voldown", STRING, VolumeMove )
453 454
    ADD( "adev", STRING, AudioDevice )
    ADD( "achan", STRING, AudioChannel )
455

456 457 458
    /* misc menu commands */
    ADD( "stats", BOOL, Statistics )

459
#undef ADD
460 461 462 463 464 465 466 467
}

/*****************************************************************************
 * 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.
 *****************************************************************************/
468
static void *Run( void *data )
469
{
470
    intf_thread_t *p_intf = data;
471
    intf_sys_t *p_sys = p_intf->p_sys;
472

473
    char p_buffer[ MAX_LINE_LENGTH + 1 ];
474
    bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
475

476 477 478
    int  i_size = 0;
    int  i_oldpos = 0;
    int  i_newpos;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
479
    int  canc = vlc_savecancel( );
480 481 482

    p_buffer[0] = 0;

483
#if defined(_WIN32) && !VLC_WINSTORE_APP
484
    /* Get the file descriptor of the console input */
485 486
    p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
    if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
487
    {
488
        msg_Err( p_intf, "couldn't find user input handle" );
489
        return NULL;
490 491 492
    }
#endif

493 494 495 496 497
    /* Register commands that will be cleaned up upon object destruction */
    RegisterCallbacks( p_intf );

    /* status callbacks */

498
    for( ;; )
499
    {
500
        char *psz_cmd, *psz_arg;
501
        bool b_complete;
502

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
503 504
        vlc_restorecancel( canc );

505
        if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
506
        {
507 508 509
            p_sys->i_socket =
                net_Accept( p_intf, p_sys->pi_socket_listen );
            if( p_sys->i_socket == -1 ) continue;
510 511
        }

512
        b_complete = ReadCommand( p_intf, p_buffer, &i_size );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
513
        canc = vlc_savecancel( );
514

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

526
                var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
527
            }
528
        }
529 530 531 532 533

        int state;
        if( p_sys->p_input != NULL
         && ((state = var_GetInteger( p_sys->p_input, "state")) == ERROR_S
          || state == END_S) )
534
        {
535 536 537
            var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
            vlc_object_release( p_sys->p_input );
            p_sys->p_input = NULL;
538

539 540
            p_sys->i_last_state = PLAYLIST_STOPPED;
            msg_rc( STATUS_CHANGE "( stop state: 0 )" );
541
        }
542

543
        if( p_sys->p_input != NULL )
544
        {
545 546
            playlist_t *p_playlist = p_sys->p_playlist;

547
            PL_LOCK;
548
            int status = playlist_Status( p_playlist );
Jakob Leben's avatar
Jakob Leben committed
549 550
            PL_UNLOCK;

551
            if( p_sys->i_last_state != status )
552
            {
Rémi Duraffort's avatar
Rémi Duraffort committed
553 554
                if( status == PLAYLIST_STOPPED )
                {
555
                    p_sys->i_last_state = PLAYLIST_STOPPED;
Rémi Duraffort's avatar
Rémi Duraffort committed
556 557 558 559
                    msg_rc( STATUS_CHANGE "( stop state: 5 )" );
                }
                else if( status == PLAYLIST_RUNNING )
                {
560
                    p_sys->i_last_state = PLAYLIST_RUNNING;
Rémi Duraffort's avatar
Rémi Duraffort committed
561 562 563 564
                    msg_rc( STATUS_CHANGE "( play state: 3 )" );
                }
                else if( status == PLAYLIST_PAUSED )
                {
565
                    p_sys->i_last_state = PLAYLIST_PAUSED;
Rémi Duraffort's avatar
Rémi Duraffort committed
566 567
                    msg_rc( STATUS_CHANGE "( pause state: 4 )" );
                }
568
            }
569 570
        }

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

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

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

591 592 593 594 595 596 597
        /* 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 == ' ' )
598
            {
599
                psz_arg++;
600
            }
601 602 603
        }
        else
        {
Rafaël Carré's avatar
Rafaël Carré committed
604
            psz_arg = (char*)"";
605 606 607
        }

        /* If the user typed a registered local command, try it */
608
        if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
609
        {
610
            int i_ret = VLC_SUCCESS;
611 612

            if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
613
                var_TriggerCallback( p_intf, psz_cmd );
614
            else
615
                i_ret = var_SetString( p_intf, psz_cmd, psz_arg );
616
            msg_rc( "%s: returned %i (%s)",
617 618 619
                    psz_cmd, i_ret, vlc_error( i_ret ) );
        }
        /* Or maybe it's a global command */
620
        else if( var_Type( p_intf->obj.libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
621
        {
622
            int i_ret = VLC_SUCCESS;
623 624

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

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

            if( !strncasecmp( psz_arg, "on", 2 ) )
728
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
729
            else if( !strncasecmp( psz_arg, "off", 3 ) )
730
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
731
            else
732
                fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
733

734
            if( p_sys->p_input != NULL )
735
            {
736
                vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
737 738
                if( p_vout )
                {
739
                    var_SetBool( p_vout, "fullscreen", fs );
740 741 742 743
                    vlc_object_release( p_vout );
                }
            }
            break;
744
        }
745 746 747
        case 's':
        case 'S':
            ;
748 749 750 751 752 753 754
            break;

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

        default:
755
            msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
756
            break;
757
        }
758 759 760

        /* Command processed */
        i_size = 0; p_buffer[0] = 0;
761 762
    }

763 764
    msg_rc( STATUS_CHANGE "( stop state: 0 )" );
    msg_rc( STATUS_CHANGE "( quit )" );
765

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
766 767
    vlc_restorecancel( canc );

768
    return NULL;
769 770
}

771
static void Help( intf_thread_t *p_intf)
772
{
773
    msg_rc("%s", _("+----[ Remote control commands ]"));
774
    msg_rc(  "| ");
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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"));
794
    msg_rc(  "| ");
795 796 797 798 799 800 801
    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"));
802
    msg_rc("%s", _("| frame. . . . . . . . . .  play frame by frame"));
803 804 805 806 807 808 809
    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"));
810
    msg_rc(  "| ");
811 812 813
    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"));
814
    msg_rc("%s", _("| adev [device]  . . . . . . . .  set/get audio device"));
815 816 817 818 819 820 821
    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"));
822
    msg_rc("%s", _("| strack [X] . . . . . . . . .  set/get subtitle track"));
823
    msg_rc("%s", _("| key [hotkey name] . . . . . .  simulate hotkey press"));
824
    msg_rc(  "| ");
825 826 827
    msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
    msg_rc("%s", _("| logout . . . . . . .  exit (if in socket connection)"));
    msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . .  quit vlc"));
828
    msg_rc(  "| ");
829
    msg_rc("%s", _("+----[ end of help ]"));
830 831 832 833 834 835 836 837
}

/********************************************************************
 * 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
838
    (void) p_this;
Rafaël Carré's avatar
Rafaël Carré committed
839
    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
840
    intf_thread_t *p_intf = (intf_thread_t*)p_data;
841

842
    vlc_mutex_lock( &p_intf->p_sys->status_lock );
843 844
    msg_rc( STATUS_CHANGE "( audio volume: %ld )",
            lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
845 846 847 848
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
    return VLC_SUCCESS;
}

849
static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
850
{
851
    playlist_t *p_playlist = p_intf->p_sys->p_playlist;
852

853
    PL_LOCK;
854
    const int i_status = playlist_Status( p_playlist );
855 856
    PL_UNLOCK;

857
    /* */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
858
    const char *psz_cmd;
859
    switch( i_status )
860
    {
861
    case PLAYLIST_STOPPED:
862
        psz_cmd = "stop";
863 864
        break;
    case PLAYLIST_RUNNING:
865
        psz_cmd = "play";
866 867
        break;
    case PLAYLIST_PAUSED:
868
        psz_cmd = "pause";
869 870
        break;
    default:
871 872 873 874 875 876
        psz_cmd = "";
        break;
    }

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

878 879 880 881 882 883 884 885 886
    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 );
887 888
    msg_rc( STATUS_CHANGE "( new rate: %.3f )",
            var_GetFloat( p_input, "rate" ) );
889 890 891 892 893 894 895 896
    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 )",
897
                (var_GetInteger( p_input, "time" ) / CLOCK_FREQ) );
898 899 900 901 902 903 904
    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;
905
    vlc_mutex_unlock( &p_intf->p_sys->status_lock );
906 907
}

908 909
static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
910
{
Pierre's avatar
Pierre committed
911 912
    VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval);
913 914
    input_thread_t *p_input = (input_thread_t*)p_this;
    intf_thread_t *p_intf = p_data;
915

916
    switch( newval.i_int )
917
    {
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
    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;
933 934 935 936 937 938 939
    }
    return VLC_SUCCESS;
}

/********************************************************************
 * Command routines
 ********************************************************************/
940 941
static int Input( vlc_object_t *p_this, char const *psz_cmd,
                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
942
{
Rafaël Carré's avatar
Rafaël Carré committed
943
    VLC_UNUSED(oldval); VLC_UNUSED(p_data);
944
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
945 946 947
    input_thread_t *p_input =
        playlist_CurrentInput( p_intf->p_sys->p_playlist );
    int i_error = VLC_EGENERIC;
948

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
949 950
    if( !p_input )
        return VLC_ENOOBJ;
951

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
952 953
    int state = var_GetInteger( p_input, "state" );
    if( ( state == PAUSE_S ) &&
954
        ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
955
    {
956
        msg_rc( "%s", _("Press pause to continue.") );
957
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
958
    else
959 960 961
    /* Parse commands that only require an input */
    if( !strcmp( psz_cmd, "pause" ) )
    {
962
        playlist_TogglePause( p_intf->p_sys->p_playlist );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
963
        i_error = VLC_SUCCESS;
964
    }
965 966
    else if( !strcmp( psz_cmd, "seek" ) )
    {
967 968 969
        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
970 971
            float f = atof( newval.psz_string ) / 100.0;
            var_SetFloat( p_input, "position", f );
972 973 974
        }
        else
        {
975 976
            mtime_t t = atoi( newval.psz_string );
            var_SetInteger( p_input, "time", CLOCK_FREQ * t );
977
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
978
        i_error = VLC_SUCCESS;
979
    }
980 981
    else if ( !strcmp( psz_cmd, "fastforward" ) )
    {
982
        if( var_GetBool( p_input, "can-rate" ) )
983
        {
984 985 986
            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 );
987 988 989
        }
        else
        {
990
            var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
991
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
992
        i_error = VLC_SUCCESS;
993 994 995
    }
    else if ( !strcmp( psz_cmd, "rewind" ) )
    {
996 997
        if( var_GetBool( p_input, "can-rewind" ) )
        {
998 999 1000
            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 );
1001 1002 1003
        }
        else
        {
1004
            var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1005
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1006
        i_error = VLC_SUCCESS;
1007 1008 1009
    }
    else if ( !strcmp( psz_cmd, "faster" ) )
    {
1010
        var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1011
        i_error = VLC_SUCCESS;
1012 1013 1014
    }
    else if ( !strcmp( psz_cmd, "slower" ) )
    {
1015
        var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1016
        i_error = VLC_SUCCESS;
1017 1018 1019
    }
    else if ( !strcmp( psz_cmd, "normal" ) )
    {
1020
        var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1021
        i_error = VLC_SUCCESS;
1022
    }
1023 1024
    else if ( !strcmp( psz_cmd, "frame" ) )
    {
1025
        var_TriggerCallback( p_input, "frame-next" );
1026 1027
        i_error = VLC_SUCCESS;
    }
1028 1029 1030 1031 1032 1033
    else if( !strcmp( psz_cmd, "chapter" ) ||
             !strcmp( psz_cmd, "chapter_n" ) ||
             !strcmp( psz_cmd, "chapter_p" ) )
    {
        if( !strcmp( psz_cmd, "chapter" ) )
        {
1034
            if ( *newval.psz_string )
1035 1036
            {
                /* Set. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1037
                var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1038 1039 1040 1041
            }
            else
            {
                /* Get. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1042
                int i_chap = var_GetInteger( p_input, "chapter" );
1043
                int i_chapter_count = var_CountChoices( p_input, "chapter" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1044
                msg_rc( "Currently playing chapter %d/%d.", i_chap,
1045
                        i_chapter_count );
Gildas Bazin's avatar