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>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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>
Clément Stenac's avatar
Clément Stenac committed
41
#include <vlc_interface.h>
42
#include <vlc_input.h>
43
#include <vlc_aout.h>
Clément Stenac's avatar
Clément Stenac committed
44 45
#include <vlc_vout.h>
#include <vlc_playlist.h>
46
#include <vlc_actions.h>
47 48

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

51

Clément Stenac's avatar
Clément Stenac committed
52
#include <vlc_network.h>
53
#include <vlc_url.h>
Clément Stenac's avatar
Clément Stenac committed
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 * );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
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;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
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 309 310 311 312 313 314 315 316 317 318 319
        if( url.psz_host == NULL )
        {
            vlc_UrlClean( &url );
            char *psz_backward_compat_host;
            if( asprintf( &psz_backward_compat_host, "//%s", psz_host ) < 0 )
            {
                free( psz_host );
                return VLC_EGENERIC;
            }
            free( psz_host );
            psz_host = psz_backward_compat_host;
            vlc_UrlParse( &url, psz_host );
        }
320

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

323 324
        pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
        if( pi_socket == NULL )
325
        {
326 327
            msg_Warn( p_intf, "can't listen to %s port %i",
                      url.psz_host, url.i_port );
328 329 330 331 332 333
            vlc_UrlClean( &url );
            free( psz_host );
            return VLC_EGENERIC;
        }

        vlc_UrlClean( &url );
334
        free( psz_host );
335 336
    }

337 338
    intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
    if( unlikely(p_sys == NULL) )
339
    {
340
        net_ListenClose( pi_socket );
341
        free( psz_unix_path );
342
        return VLC_ENOMEM;
343
    }
344

345 346 347 348 349 350 351 352 353
    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;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
354

355 356 357
    /* Non-buffered stdout */
    setvbuf( stdout, (char *)NULL, _IOLBF, 0 );

358 359 360
#if VLC_WINSTORE_APP
    p_sys->b_quiet = true;
#elif defined(_WIN32)
361 362
    p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
    if( !p_sys->b_quiet )
363
        intf_consoleIntroMsg( p_intf );
364
#endif
365

366
    if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
367 368
        abort();

369
    msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
370 371 372

    /* Listen to audio volume updates */
    var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
373
    return VLC_SUCCESS;
374 375
}

376 377 378 379 380 381
/*****************************************************************************
 * Deactivate: uninitialize and cleanup
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;
382 383
    intf_sys_t *p_sys = p_intf->p_sys;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
384
    vlc_cancel( p_sys->thread );
385 386
    var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
    vlc_join( p_sys->thread, NULL );
387

388 389 390 391 392
    if( p_sys->p_input != NULL )
    {
        var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
        vlc_object_release( p_sys->p_input );
    }
393

394 395 396 397
    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 )
398
    {
399
#if defined(AF_LOCAL) && !defined(_WIN32)
400
        unlink( p_sys->psz_unix_path );
401
#endif
402
        free( p_sys->psz_unix_path );
403
    }
404 405
    vlc_mutex_destroy( &p_sys->status_lock );
    free( p_sys );
406 407
}

408
/*****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
409
 * RegisterCallbacks: Register callbacks to dynamic variables
410
 *****************************************************************************/
Jean-Paul Saman's avatar
Jean-Paul Saman committed
411
static void RegisterCallbacks( intf_thread_t *p_intf )
412
{
413
    /* Register commands that will be cleaned up upon object destruction */
414 415 416 417 418 419 420 421 422
#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
423
    ADD( "random", STRING, Playlist )
424 425 426 427 428 429 430 431 432 433
    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 )
434

435
    /* DVD commands */
436 437 438 439 440 441 442 443 444 445 446 447 448 449
    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 )
450
    ADD( "frame", VOID, Input )
451 452 453 454

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

456
    /* video commands */
457 458 459
    ADD( "vratio", STRING, VideoConfig )
    ADD( "vcrop", STRING, VideoConfig )
    ADD( "vzoom", STRING, VideoConfig )
460
    ADD( "snapshot", VOID, VideoConfig )
461

462
    /* audio commands */
463 464 465
    ADD( "volume", STRING, Volume )
    ADD( "volup", STRING, VolumeMove )
    ADD( "voldown", STRING, VolumeMove )
466 467
    ADD( "adev", STRING, AudioDevice )
    ADD( "achan", STRING, AudioChannel )
468

469
    /* misc menu commands */
470
    ADD( "stats", VOID, Statistics )
471

472
#undef ADD
Jean-Paul Saman's avatar
Jean-Paul Saman committed
473 474 475 476 477 478 479 480
}

/*****************************************************************************
 * 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.
 *****************************************************************************/
481
static void *Run( void *data )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
482
{
483
    intf_thread_t *p_intf = data;
484
    intf_sys_t *p_sys = p_intf->p_sys;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
485

486
    char p_buffer[ MAX_LINE_LENGTH + 1 ];
487
    bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
488

489 490 491
    int  i_size = 0;
    int  i_oldpos = 0;
    int  i_newpos;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
492
    int  canc = vlc_savecancel( );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
493 494 495

    p_buffer[0] = 0;

496
#if defined(_WIN32) && !VLC_WINSTORE_APP
497
    /* Get the file descriptor of the console input */
498 499
    p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
    if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
500
    {
501
        msg_Err( p_intf, "couldn't find user input handle" );
502
        return NULL;
503 504 505
    }
#endif

506 507 508 509 510
    /* Register commands that will be cleaned up upon object destruction */
    RegisterCallbacks( p_intf );

    /* status callbacks */

511
    for( ;; )
512
    {
513
        char *psz_cmd, *psz_arg;
514
        bool b_complete;
515

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
516 517
        vlc_restorecancel( canc );

518
        if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
519
        {
520 521 522
            p_sys->i_socket =
                net_Accept( p_intf, p_sys->pi_socket_listen );
            if( p_sys->i_socket == -1 ) continue;
523 524
        }

525
        b_complete = ReadCommand( p_intf, p_buffer, &i_size );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
526
        canc = vlc_savecancel( );
527

528
        /* Manage the input part */
529
        if( p_sys->p_input == NULL )
530
        {
531
            p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
532
            /* New input has been registered */
533
            if( p_sys->p_input )
534
            {
535
                char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
536 537 538
                msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
                free( psz_uri );

539
                var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
540
            }
541
        }
542 543 544 545 546

        int state;
        if( p_sys->p_input != NULL
         && ((state = var_GetInteger( p_sys->p_input, "state")) == ERROR_S
          || state == END_S) )
547
        {
548 549 550
            var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
            vlc_object_release( p_sys->p_input );
            p_sys->p_input = NULL;
551

552 553
            p_sys->i_last_state = PLAYLIST_STOPPED;
            msg_rc( STATUS_CHANGE "( stop state: 0 )" );
554
        }
555

556
        if( p_sys->p_input != NULL )
557
        {
558 559
            playlist_t *p_playlist = p_sys->p_playlist;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
560
            PL_LOCK;
561
            int status = playlist_Status( p_playlist );
Jakob Leben's avatar
Jakob Leben committed
562 563
            PL_UNLOCK;

564
            if( p_sys->i_last_state != status )
565
            {
Rémi Duraffort's avatar
Rémi Duraffort committed
566 567
                if( status == PLAYLIST_STOPPED )
                {
568
                    p_sys->i_last_state = PLAYLIST_STOPPED;
Rémi Duraffort's avatar
Rémi Duraffort committed
569 570 571 572
                    msg_rc( STATUS_CHANGE "( stop state: 5 )" );
                }
                else if( status == PLAYLIST_RUNNING )
                {
573
                    p_sys->i_last_state = PLAYLIST_RUNNING;
Rémi Duraffort's avatar
Rémi Duraffort committed
574 575 576 577
                    msg_rc( STATUS_CHANGE "( play state: 3 )" );
                }
                else if( status == PLAYLIST_PAUSED )
                {
578
                    p_sys->i_last_state = PLAYLIST_PAUSED;
Rémi Duraffort's avatar
Rémi Duraffort committed
579 580
                    msg_rc( STATUS_CHANGE "( pause state: 4 )" );
                }
581
            }
582 583
        }

584
        if( p_sys->p_input && b_showpos )
585
        {
586
            i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
587
            if( i_oldpos != i_newpos )
588
            {
589
                i_oldpos = i_newpos;
590
                msg_rc( "pos: %d%%", i_newpos );
591 592 593 594
            }
        }

        /* Is there something to do? */
595 596 597 598 599
        if( !b_complete ) continue;

        /* Skip heading spaces */
        psz_cmd = p_buffer;
        while( *psz_cmd == ' ' )
600
        {
601 602
            psz_cmd++;
        }
603

604 605 606 607 608 609 610
        /* 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 == ' ' )
611
            {
612
                psz_arg++;
613
            }
614 615 616
        }
        else
        {
Rafaël Carré's avatar
Rafaël Carré committed
617
            psz_arg = (char*)"";
618 619 620
        }

        /* If the user typed a registered local command, try it */
621
        if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
622
        {
623
            int i_ret = VLC_SUCCESS;
624 625

            if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
626
                var_TriggerCallback( p_intf, psz_cmd );
627
            else
628
                i_ret = var_SetString( p_intf, psz_cmd, psz_arg );
629
            msg_rc( "%s: returned %i (%s)",
630 631 632
                    psz_cmd, i_ret, vlc_error( i_ret ) );
        }
        /* Or maybe it's a global command */
633
        else if( var_Type( p_intf->obj.libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
634
        {
635
            int i_ret = VLC_SUCCESS;
636 637

            /* FIXME: it's a global command, but we should pass the
638 639
             * 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)
640
                var_TriggerCallback( p_intf, psz_cmd );
641
            else
642
                i_ret = var_SetString( p_intf->obj.libvlc, psz_cmd, psz_arg );
643 644
            if( i_ret != 0 )
            {
645
                msg_rc( "%s: returned %i (%s)",
646 647
                         psz_cmd, i_ret, vlc_error( i_ret ) );
            }
648
        }
649 650 651
        else if( !strcmp( psz_cmd, "logout" ) )
        {
            /* Close connection */
652
            if( p_sys->i_socket != -1 )
653
            {
654 655
                net_Close( p_sys->i_socket );
                p_sys->i_socket = -1;
656 657
            }
        }
658 659
        else if( !strcmp( psz_cmd, "info" ) )
        {
660
            if( p_sys->p_input )
661
            {
662
                int i, j;
663 664
                vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
                for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
665
                {
666
                    info_category_t *p_category = input_GetItem(p_sys->p_input)
Clément Stenac's avatar
Clément Stenac committed
667
                                                        ->pp_categories[i];
668

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

            if( !strncasecmp( psz_arg, "on", 2 ) )
741
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
742
            else if( !strncasecmp( psz_arg, "off", 3 ) )
743
                var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
744
            else
745
                fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
746

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

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

        default:
768
            msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
769
            break;
770
        }
771 772 773

        /* Command processed */
        i_size = 0; p_buffer[0] = 0;
774 775
    }

776 777
    msg_rc( STATUS_CHANGE "( stop state: 0 )" );
    msg_rc( STATUS_CHANGE "( quit )" );
778

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
779 780
    vlc_restorecancel( canc );

781
    return NULL;
782 783
}

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

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

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

862
static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
863
{
864
    playlist_t *p_playlist = p_intf->p_sys->p_playlist;
865

866
    PL_LOCK;
867
    const int i_status = playlist_Status( p_playlist );
868 869
    PL_UNLOCK;

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