]> git.sesse.net Git - vlc/blob - modules/control/rc.c
Old RC: fix exit
[vlc] / modules / control / rc.c
1 /*****************************************************************************
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  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35
36 #include <errno.h>                                                 /* ENOMEM */
37 #include <signal.h>
38 #include <assert.h>
39 #include <math.h>
40
41 #include <vlc_interface.h>
42 #include <vlc_aout.h>
43 #include <vlc_vout.h>
44 #include <vlc_playlist.h>
45 #include <vlc_keys.h>
46
47 #ifdef HAVE_UNISTD_H
48 #    include <unistd.h>
49 #endif
50 #include <sys/types.h>
51
52 #include <vlc_network.h>
53 #include <vlc_url.h>
54
55 #include <vlc_charset.h>
56
57 #if defined(PF_UNIX) && !defined(PF_LOCAL)
58 #    define PF_LOCAL PF_UNIX
59 #endif
60
61 #if defined(AF_LOCAL) && ! defined(WIN32)
62 #    include <sys/un.h>
63 #endif
64
65 #define MAX_LINE_LENGTH 1024
66 #define STATUS_CHANGE "status change: "
67
68 /* input_state_e from <vlc_input.h> */
69 static const char *ppsz_input_state[] = {
70     [INIT_S] = N_("Initializing"),
71     [OPENING_S] = N_("Opening"),
72     [PLAYING_S] = N_("Play"),
73     [PAUSE_S] = N_("Pause"),
74     [END_S] = N_("End"),
75     [ERROR_S] = N_("Error"),
76 };
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int  Activate     ( vlc_object_t * );
82 static void Deactivate   ( vlc_object_t * );
83 static void *Run         ( void * );
84
85 static void Help         ( intf_thread_t *, bool );
86 static void RegisterCallbacks( intf_thread_t * );
87
88 static bool ReadCommand( intf_thread_t *, char *, int * );
89
90 static input_item_t *parse_MRL( const char * );
91
92 static int  Input        ( vlc_object_t *, char const *,
93                            vlc_value_t, vlc_value_t, void * );
94 static int  Playlist     ( vlc_object_t *, char const *,
95                            vlc_value_t, vlc_value_t, void * );
96 static int  Quit         ( vlc_object_t *, char const *,
97                            vlc_value_t, vlc_value_t, void * );
98 static int  Intf         ( vlc_object_t *, char const *,
99                            vlc_value_t, vlc_value_t, void * );
100 static int  Volume       ( vlc_object_t *, char const *,
101                            vlc_value_t, vlc_value_t, void * );
102 static int  VolumeMove   ( vlc_object_t *, char const *,
103                            vlc_value_t, vlc_value_t, void * );
104 static int  VideoConfig  ( vlc_object_t *, char const *,
105                            vlc_value_t, vlc_value_t, void * );
106 static int  AudioDevice  ( vlc_object_t *, char const *,
107                            vlc_value_t, vlc_value_t, void * );
108 static int  AudioChannel ( vlc_object_t *, char const *,
109                            vlc_value_t, vlc_value_t, void * );
110 static int  Statistics   ( vlc_object_t *, char const *,
111                            vlc_value_t, vlc_value_t, void * );
112
113 static int updateStatistics( intf_thread_t *, input_item_t *);
114
115 /* Status Callbacks */
116 static int VolumeChanged( vlc_object_t *, char const *,
117                           vlc_value_t, vlc_value_t, void * );
118 static int InputEvent( vlc_object_t *, char const *,
119                        vlc_value_t, vlc_value_t, void * );
120
121 struct intf_sys_t
122 {
123     int *pi_socket_listen;
124     int i_socket;
125     char *psz_unix_path;
126     vlc_thread_t thread;
127
128     /* status changes */
129     vlc_mutex_t       status_lock;
130     int               i_last_state;
131     playlist_t        *p_playlist;
132     input_thread_t    *p_input;
133     bool              b_input_buffering;
134
135 #ifdef WIN32
136     HANDLE hConsoleIn;
137     bool b_quiet;
138 #endif
139 };
140
141 VLC_FORMAT(2, 3)
142 static void msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
143 {
144     va_list args;
145     char fmt_eol[strlen (psz_fmt) + 3];
146
147     snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
148     va_start( args, psz_fmt );
149
150     if( p_intf->p_sys->i_socket == -1 )
151         utf8_vfprintf( stdout, fmt_eol, args );
152     else
153         net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, fmt_eol, args );
154     va_end( args );
155 }
156 #define msg_rc( ... ) msg_rc( p_intf, __VA_ARGS__ )
157
158 /*****************************************************************************
159  * Module descriptor
160  *****************************************************************************/
161 #define POS_TEXT N_("Show stream position")
162 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
163                         "stream from time to time." )
164
165 #define TTY_TEXT N_("Fake TTY")
166 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
167
168 #define UNIX_TEXT N_("UNIX socket command input")
169 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
170                          "stdin." )
171
172 #define HOST_TEXT N_("TCP command input")
173 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
174             "You can set the address and port the interface will bind to." )
175
176 #ifdef WIN32
177 #define QUIET_TEXT N_("Do not open a DOS command box interface")
178 #define QUIET_LONGTEXT N_( \
179     "By default the rc interface plugin will start a DOS command box. " \
180     "Enabling the quiet mode will not bring this command box but can also " \
181     "be pretty annoying when you want to stop VLC and no video window is " \
182     "open." )
183 #endif
184
185 vlc_module_begin ()
186     set_shortname( N_("RC"))
187     set_category( CAT_INTERFACE )
188     set_subcategory( SUBCAT_INTERFACE_MAIN )
189     set_description( N_("Remote control interface") )
190     add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
191
192 #ifdef WIN32
193     add_bool( "rc-quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false )
194 #else
195 #if defined (HAVE_ISATTY)
196     add_bool( "rc-fake-tty", false, TTY_TEXT, TTY_LONGTEXT, true )
197 #endif
198     add_string( "rc-unix", NULL, UNIX_TEXT, UNIX_LONGTEXT, true )
199 #endif
200     add_string( "rc-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
201
202     set_capability( "interface", 20 )
203
204     set_callbacks( Activate, Deactivate )
205 #ifdef WIN32
206     add_shortcut( "rc" )
207 #endif
208 vlc_module_end ()
209
210 /*****************************************************************************
211  * Activate: initialize and create stuff
212  *****************************************************************************/
213 static int Activate( vlc_object_t *p_this )
214 {
215     /* FIXME: This function is full of memory leaks and bugs in error paths. */
216     intf_thread_t *p_intf = (intf_thread_t*)p_this;
217     playlist_t *p_playlist = pl_Get( p_intf );
218     char *psz_host, *psz_unix_path = NULL;
219     int  *pi_socket = NULL;
220
221 #ifndef WIN32
222 #if defined(HAVE_ISATTY)
223     /* Check that stdin is a TTY */
224     if( !var_InheritBool( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
225     {
226         msg_Warn( p_intf, "fd 0 is not a TTY" );
227         return VLC_EGENERIC;
228     }
229 #endif
230
231     psz_unix_path = var_InheritString( p_intf, "rc-unix" );
232     if( psz_unix_path )
233     {
234         int i_socket;
235
236 #ifndef AF_LOCAL
237         msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
238         free( psz_unix_path );
239         return VLC_EGENERIC;
240 #else
241         struct sockaddr_un addr;
242
243         memset( &addr, 0, sizeof(struct sockaddr_un) );
244
245         msg_Dbg( p_intf, "trying UNIX socket" );
246
247         if( (i_socket = vlc_socket( PF_LOCAL, SOCK_STREAM, 0, false ) ) < 0 )
248         {
249             msg_Warn( p_intf, "can't open socket: %m" );
250             free( psz_unix_path );
251             return VLC_EGENERIC;
252         }
253
254         addr.sun_family = AF_LOCAL;
255         strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
256         addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
257
258         if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
259          && (errno == EADDRINUSE)
260          && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
261          && (errno == ECONNREFUSED))
262         {
263             msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
264             unlink (psz_unix_path);
265
266             if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
267             {
268                 msg_Err (p_intf, "cannot bind UNIX socket at %s: %m",
269                          psz_unix_path);
270                 free (psz_unix_path);
271                 net_Close (i_socket);
272                 return VLC_EGENERIC;
273             }
274         }
275
276         if( listen( i_socket, 1 ) )
277         {
278             msg_Warn( p_intf, "can't listen on socket: %m");
279             free( psz_unix_path );
280             net_Close( i_socket );
281             return VLC_EGENERIC;
282         }
283
284         /* FIXME: we need a core function to merge listening sockets sets */
285         pi_socket = calloc( 2, sizeof( int ) );
286         if( pi_socket == NULL )
287         {
288             free( psz_unix_path );
289             net_Close( i_socket );
290             return VLC_ENOMEM;
291         }
292         pi_socket[0] = i_socket;
293         pi_socket[1] = -1;
294 #endif /* AF_LOCAL */
295     }
296 #endif /* !WIN32 */
297
298     if( ( pi_socket == NULL ) &&
299         ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
300     {
301         vlc_url_t url;
302
303         vlc_UrlParse( &url, psz_host, 0 );
304
305         msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
306
307         pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
308         if( pi_socket == NULL )
309         {
310             msg_Warn( p_intf, "can't listen to %s port %i",
311                       url.psz_host, url.i_port );
312             vlc_UrlClean( &url );
313             free( psz_host );
314             return VLC_EGENERIC;
315         }
316
317         vlc_UrlClean( &url );
318         free( psz_host );
319     }
320
321     intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
322     if( unlikely(p_sys == NULL) )
323         return VLC_ENOMEM;
324
325     p_intf->p_sys = p_sys;
326     p_sys->pi_socket_listen = pi_socket;
327     p_sys->i_socket = -1;
328     p_sys->psz_unix_path = psz_unix_path;
329     vlc_mutex_init( &p_sys->status_lock );
330     p_sys->i_last_state = PLAYLIST_STOPPED;
331     p_sys->b_input_buffering = false;
332     p_sys->p_playlist = p_playlist;
333     p_sys->p_input = NULL;
334
335     /* Non-buffered stdout */
336     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
337
338 #ifdef WIN32
339     p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
340     if( !p_sys->b_quiet )
341 #endif
342     {
343         CONSOLE_INTRO_MSG;
344     }
345
346     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
347         abort();
348
349     msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
350
351     /* Listen to audio volume updates */
352     var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
353     return VLC_SUCCESS;
354 }
355
356 /*****************************************************************************
357  * Deactivate: uninitialize and cleanup
358  *****************************************************************************/
359 static void Deactivate( vlc_object_t *p_this )
360 {
361     intf_thread_t *p_intf = (intf_thread_t*)p_this;
362     intf_sys_t *p_sys = p_intf->p_sys;
363
364     vlc_cancel( p_sys->thread );
365     var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
366     vlc_join( p_sys->thread, NULL );
367
368     if( p_sys->p_input != NULL )
369     {
370         var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
371         vlc_object_release( p_sys->p_input );
372     }
373
374     net_ListenClose( p_sys->pi_socket_listen );
375     if( p_sys->i_socket != -1 )
376         net_Close( p_sys->i_socket );
377     if( p_sys->psz_unix_path != NULL )
378     {
379 #if defined(AF_LOCAL) && !defined(WIN32)
380         unlink( p_sys->psz_unix_path );
381 #endif
382         free( p_sys->psz_unix_path );
383     }
384     vlc_mutex_destroy( &p_sys->status_lock );
385     free( p_sys );
386 }
387
388 /*****************************************************************************
389  * RegisterCallbacks: Register callbacks to dynamic variables
390  *****************************************************************************/
391 static void RegisterCallbacks( intf_thread_t *p_intf )
392 {
393     /* Register commands that will be cleaned up upon object destruction */
394 #define ADD( name, type, target )                                   \
395     var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
396     var_AddCallback( p_intf, name, target, NULL );
397     ADD( "quit", VOID, Quit )
398     ADD( "intf", STRING, Intf )
399
400     ADD( "add", STRING, Playlist )
401     ADD( "repeat", STRING, Playlist )
402     ADD( "loop", STRING, Playlist )
403     ADD( "random", STRING, Playlist )
404     ADD( "enqueue", STRING, Playlist )
405     ADD( "playlist", VOID, Playlist )
406     ADD( "sort", VOID, Playlist )
407     ADD( "play", VOID, Playlist )
408     ADD( "stop", VOID, Playlist )
409     ADD( "clear", VOID, Playlist )
410     ADD( "prev", VOID, Playlist )
411     ADD( "next", VOID, Playlist )
412     ADD( "goto", INTEGER, Playlist )
413     ADD( "status", INTEGER, Playlist )
414
415     /* DVD commands */
416     ADD( "pause", VOID, Input )
417     ADD( "seek", INTEGER, Input )
418     ADD( "title", STRING, Input )
419     ADD( "title_n", VOID, Input )
420     ADD( "title_p", VOID, Input )
421     ADD( "chapter", STRING, Input )
422     ADD( "chapter_n", VOID, Input )
423     ADD( "chapter_p", VOID, Input )
424
425     ADD( "fastforward", VOID, Input )
426     ADD( "rewind", VOID, Input )
427     ADD( "faster", VOID, Input )
428     ADD( "slower", VOID, Input )
429     ADD( "normal", VOID, Input )
430     ADD( "frame", VOID, Input )
431
432     ADD( "atrack", STRING, Input )
433     ADD( "vtrack", STRING, Input )
434     ADD( "strack", STRING, Input )
435
436     /* video commands */
437     ADD( "vratio", STRING, VideoConfig )
438     ADD( "vcrop", STRING, VideoConfig )
439     ADD( "vzoom", STRING, VideoConfig )
440     ADD( "snapshot", VOID, VideoConfig )
441
442     /* audio commands */
443     ADD( "volume", STRING, Volume )
444     ADD( "volup", STRING, VolumeMove )
445     ADD( "voldown", STRING, VolumeMove )
446     ADD( "adev", STRING, AudioDevice )
447     ADD( "achan", STRING, AudioChannel )
448
449     /* misc menu commands */
450     ADD( "stats", BOOL, Statistics )
451
452 #undef ADD
453 }
454
455 /*****************************************************************************
456  * Run: rc thread
457  *****************************************************************************
458  * This part of the interface is in a separate thread so that we can call
459  * exec() from within it without annoying the rest of the program.
460  *****************************************************************************/
461 static void *Run( void *data )
462 {
463     intf_thread_t *p_intf = data;
464     intf_sys_t *p_sys = p_intf->p_sys;
465
466     char p_buffer[ MAX_LINE_LENGTH + 1 ];
467     bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
468     bool b_longhelp = false;
469
470     int  i_size = 0;
471     int  i_oldpos = 0;
472     int  i_newpos;
473     int  canc = vlc_savecancel( );
474
475     p_buffer[0] = 0;
476
477 #ifdef WIN32
478     /* Get the file descriptor of the console input */
479     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
480     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
481     {
482         msg_Err( p_intf, "couldn't find user input handle" );
483         return;
484     }
485 #endif
486
487     /* Register commands that will be cleaned up upon object destruction */
488     RegisterCallbacks( p_intf );
489
490     /* status callbacks */
491
492     for( ;; )
493     {
494         char *psz_cmd, *psz_arg;
495         bool b_complete;
496
497         vlc_restorecancel( canc );
498
499         if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
500         {
501             p_sys->i_socket =
502                 net_Accept( p_intf, p_sys->pi_socket_listen );
503             if( p_sys->i_socket == -1 ) continue;
504         }
505
506         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
507         canc = vlc_savecancel( );
508
509         /* Manage the input part */
510         if( p_sys->p_input == NULL )
511         {
512             p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
513             /* New input has been registered */
514             if( p_sys->p_input )
515             {
516                 char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
517                 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
518                 free( psz_uri );
519
520                 var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
521             }
522         }
523 #warning This is not reliable...
524         else if( p_sys->p_input->b_dead )
525         {
526             var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
527             vlc_object_release( p_sys->p_input );
528             p_sys->p_input = NULL;
529
530             p_sys->i_last_state = PLAYLIST_STOPPED;
531             msg_rc( STATUS_CHANGE "( stop state: 0 )" );
532         }
533
534         if( p_sys->p_input != NULL )
535         {
536             playlist_t *p_playlist = p_sys->p_playlist;
537
538             PL_LOCK;
539             int status = playlist_Status( p_playlist );
540             PL_UNLOCK;
541
542             if( p_sys->i_last_state != status )
543             {
544                 if( status == PLAYLIST_STOPPED )
545                 {
546                     p_sys->i_last_state = PLAYLIST_STOPPED;
547                     msg_rc( STATUS_CHANGE "( stop state: 5 )" );
548                 }
549                 else if( status == PLAYLIST_RUNNING )
550                 {
551                     p_sys->i_last_state = PLAYLIST_RUNNING;
552                     msg_rc( STATUS_CHANGE "( play state: 3 )" );
553                 }
554                 else if( status == PLAYLIST_PAUSED )
555                 {
556                     p_sys->i_last_state = PLAYLIST_PAUSED;
557                     msg_rc( STATUS_CHANGE "( pause state: 4 )" );
558                 }
559             }
560         }
561
562         if( p_sys->p_input && b_showpos )
563         {
564             i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
565             if( i_oldpos != i_newpos )
566             {
567                 i_oldpos = i_newpos;
568                 msg_rc( "pos: %d%%", i_newpos );
569             }
570         }
571
572         /* Is there something to do? */
573         if( !b_complete ) continue;
574
575         /* Skip heading spaces */
576         psz_cmd = p_buffer;
577         while( *psz_cmd == ' ' )
578         {
579             psz_cmd++;
580         }
581
582         /* Split psz_cmd at the first space and make sure that
583          * psz_arg is valid */
584         psz_arg = strchr( psz_cmd, ' ' );
585         if( psz_arg )
586         {
587             *psz_arg++ = 0;
588             while( *psz_arg == ' ' )
589             {
590                 psz_arg++;
591             }
592         }
593         else
594         {
595             psz_arg = (char*)"";
596         }
597
598         /* module specfic commands: @<module name> <command> <args...> */
599         if( *psz_cmd == '@' && *psz_arg )
600         {
601             /* Parse miscellaneous commands */
602             char *psz_alias = psz_cmd + 1;
603             char *psz_mycmd = strdup( psz_arg );
604             char *psz_myarg = strchr( psz_mycmd, ' ' );
605             char *psz_msg;
606
607             if( !psz_myarg )
608             {
609                 msg_rc( "Not enough parameters." );
610             }
611             else
612             {
613                 *psz_myarg = '\0';
614                 psz_myarg ++;
615
616                 var_Command( p_intf, psz_alias, psz_mycmd, psz_myarg,
617                              &psz_msg );
618
619                 if( psz_msg )
620                 {
621                     msg_rc( "%s", psz_msg );
622                     free( psz_msg );
623                 }
624             }
625             free( psz_mycmd );
626         }
627         /* If the user typed a registered local command, try it */
628         else if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
629         {
630             vlc_value_t val;
631             int i_ret;
632             val.psz_string = psz_arg;
633
634             if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
635                 i_ret = var_TriggerCallback( p_intf, psz_cmd );
636             else
637                 i_ret = var_Set( p_intf, psz_cmd, val );
638             msg_rc( "%s: returned %i (%s)",
639                     psz_cmd, i_ret, vlc_error( i_ret ) );
640         }
641         /* Or maybe it's a global command */
642         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
643         {
644             vlc_value_t val;
645             int i_ret;
646
647             val.psz_string = psz_arg;
648             /* FIXME: it's a global command, but we should pass the
649              * local object as an argument, not p_intf->p_libvlc. */
650             if ((var_Type( p_intf->p_libvlc, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
651                 i_ret = var_TriggerCallback( p_intf, psz_cmd );
652             else
653                 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
654             if( i_ret != 0 )
655             {
656                 msg_rc( "%s: returned %i (%s)",
657                          psz_cmd, i_ret, vlc_error( i_ret ) );
658             }
659         }
660         else if( !strcmp( psz_cmd, "logout" ) )
661         {
662             /* Close connection */
663             if( p_sys->i_socket != -1 )
664             {
665                 net_Close( p_sys->i_socket );
666                 p_sys->i_socket = -1;
667             }
668         }
669         else if( !strcmp( psz_cmd, "info" ) )
670         {
671             if( p_sys->p_input )
672             {
673                 int i, j;
674                 vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
675                 for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
676                 {
677                     info_category_t *p_category = input_GetItem(p_sys->p_input)
678                                                         ->pp_categories[i];
679
680                     msg_rc( "+----[ %s ]", p_category->psz_name );
681                     msg_rc( "| " );
682                     for ( j = 0; j < p_category->i_infos; j++ )
683                     {
684                         info_t *p_info = p_category->pp_infos[j];
685                         msg_rc( "| %s: %s", p_info->psz_name,
686                                 p_info->psz_value );
687                     }
688                     msg_rc( "| " );
689                 }
690                 msg_rc( "+----[ end of stream info ]" );
691                 vlc_mutex_unlock( &input_GetItem(p_sys->p_input)->lock );
692             }
693             else
694             {
695                 msg_rc( "no input" );
696             }
697         }
698         else if( !strcmp( psz_cmd, "is_playing" ) )
699         {
700             if( p_sys->p_input == NULL )
701             {
702                 msg_rc( "0" );
703             }
704             else
705             {
706                 msg_rc( "1" );
707             }
708         }
709         else if( !strcmp( psz_cmd, "get_time" ) )
710         {
711             if( p_sys->p_input == NULL )
712             {
713                 msg_rc("0");
714             }
715             else
716             {
717                 vlc_value_t time;
718                 var_Get( p_sys->p_input, "time", &time );
719                 msg_rc( "%"PRIu64, time.i_time / 1000000);
720             }
721         }
722         else if( !strcmp( psz_cmd, "get_length" ) )
723         {
724             if( p_sys->p_input == NULL )
725             {
726                 msg_rc("0");
727             }
728             else
729             {
730                 vlc_value_t time;
731                 var_Get( p_sys->p_input, "length", &time );
732                 msg_rc( "%"PRIu64, time.i_time / 1000000);
733             }
734         }
735         else if( !strcmp( psz_cmd, "get_title" ) )
736         {
737             if( p_sys->p_input == NULL )
738             {
739                 msg_rc("%s", "");
740             }
741             else
742             {
743                 msg_rc( "%s", input_GetItem(p_sys->p_input)->psz_name );
744             }
745         }
746         else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
747                  || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
748         {
749             if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "H", 1 ) )
750                  b_longhelp = true;
751             else b_longhelp = false;
752
753             Help( p_intf, b_longhelp );
754         }
755         else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
756         {
757             var_SetInteger( p_intf->p_libvlc, "key-action",
758                             vlc_GetActionId( psz_arg ) );
759         }
760         else switch( psz_cmd[0] )
761         {
762         case 'f':
763         case 'F':
764         {
765             bool fs;
766
767             if( !strncasecmp( psz_arg, "on", 2 ) )
768                 var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
769             else if( !strncasecmp( psz_arg, "off", 3 ) )
770                 var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
771             else
772                 fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
773
774             if( p_sys->p_input == NULL )
775             {
776                 vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
777                 if( p_vout )
778                 {
779                     var_SetBool( p_vout, "fullscreen", fs );
780                     vlc_object_release( p_vout );
781                 }
782             }
783             break;
784         }
785         case 's':
786         case 'S':
787             ;
788             break;
789
790         case '\0':
791             /* Ignore empty lines */
792             break;
793
794         default:
795             msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
796             break;
797         }
798
799         /* Command processed */
800         i_size = 0; p_buffer[0] = 0;
801     }
802
803     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
804     msg_rc( STATUS_CHANGE "( quit )" );
805
806     vlc_restorecancel( canc );
807
808     return NULL;
809 }
810
811 static void Help( intf_thread_t *p_intf, bool b_longhelp)
812 {
813     msg_rc("%s", _("+----[ Remote control commands ]"));
814     msg_rc(  "| ");
815     msg_rc("%s", _("| add XYZ  . . . . . . . . . . . . add XYZ to playlist"));
816     msg_rc("%s", _("| enqueue XYZ  . . . . . . . . . queue XYZ to playlist"));
817     msg_rc("%s", _("| playlist . . . . .  show items currently in playlist"));
818     msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
819     msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
820     msg_rc("%s", _("| next . . . . . . . . . . . . . .  next playlist item"));
821     msg_rc("%s", _("| prev . . . . . . . . . . . .  previous playlist item"));
822     msg_rc("%s", _("| goto . . . . . . . . . . . . . .  goto item at index"));
823     msg_rc("%s", _("| repeat [on|off] . . . .  toggle playlist item repeat"));
824     msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
825     msg_rc("%s", _("| random [on|off] . . . . . . .  toggle random jumping"));
826     msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
827     msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
828     msg_rc("%s", _("| title [X]  . . . . . . set/get title in current item"));
829     msg_rc("%s", _("| title_n  . . . . . . . .  next title in current item"));
830     msg_rc("%s", _("| title_p  . . . . . .  previous title in current item"));
831     msg_rc("%s", _("| chapter [X]  . . . . set/get chapter in current item"));
832     msg_rc("%s", _("| chapter_n  . . . . . .  next chapter in current item"));
833     msg_rc("%s", _("| chapter_p  . . . .  previous chapter in current item"));
834     msg_rc(  "| ");
835     msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
836     msg_rc("%s", _("| pause  . . . . . . . . . . . . . . . .  toggle pause"));
837     msg_rc("%s", _("| fastforward  . . . . . . . .  .  set to maximum rate"));
838     msg_rc("%s", _("| rewind  . . . . . . . . . . . .  set to minimum rate"));
839     msg_rc("%s", _("| faster . . . . . . . . . .  faster playing of stream"));
840     msg_rc("%s", _("| slower . . . . . . . . . .  slower playing of stream"));
841     msg_rc("%s", _("| normal . . . . . . . . . .  normal playing of stream"));
842     msg_rc("%s", _("| frame. . . . . . . . . .  play frame by frame"));
843     msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
844     msg_rc("%s", _("| info . . . . .  information about the current stream"));
845     msg_rc("%s", _("| stats  . . . . . . . .  show statistical information"));
846     msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
847     msg_rc("%s", _("| is_playing . . . .  1 if a stream plays, 0 otherwise"));
848     msg_rc("%s", _("| get_title . . . . .  the title of the current stream"));
849     msg_rc("%s", _("| get_length . . . .  the length of the current stream"));
850     msg_rc(  "| ");
851     msg_rc("%s", _("| volume [X] . . . . . . . . . .  set/get audio volume"));
852     msg_rc("%s", _("| volup [X]  . . . . . . .  raise audio volume X steps"));
853     msg_rc("%s", _("| voldown [X]  . . . . . .  lower audio volume X steps"));
854     msg_rc("%s", _("| adev [device]  . . . . . . . .  set/get audio device"));
855     msg_rc("%s", _("| achan [X]. . . . . . . . . .  set/get audio channels"));
856     msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
857     msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
858     msg_rc("%s", _("| vratio [X]  . . . . . . . set/get video aspect ratio"));
859     msg_rc("%s", _("| vcrop [X]  . . . . . . . . . . .  set/get video crop"));
860     msg_rc("%s", _("| vzoom [X]  . . . . . . . . . . .  set/get video zoom"));
861     msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
862     msg_rc("%s", _("| strack [X] . . . . . . . . .  set/get subtitle track"));
863     msg_rc("%s", _("| key [hotkey name] . . . . . .  simulate hotkey press"));
864     msg_rc("%s", _("| menu . . [on|off|up|down|left|right|select] use menu"));
865     msg_rc(  "| ");
866
867     if (b_longhelp)
868     {
869         msg_rc("%s", _("| @name marq-marquee  STRING  . . overlay STRING in video"));
870         msg_rc("%s", _("| @name marq-x X . . . . . . . . . . . .offset from left"));
871         msg_rc("%s", _("| @name marq-y Y . . . . . . . . . . . . offset from top"));
872         msg_rc("%s", _("| @name marq-position #. . .  .relative position control"));
873         msg_rc("%s", _("| @name marq-color # . . . . . . . . . . font color, RGB"));
874         msg_rc("%s", _("| @name marq-opacity # . . . . . . . . . . . . . opacity"));
875         msg_rc("%s", _("| @name marq-timeout T. . . . . . . . . . timeout, in ms"));
876         msg_rc("%s", _("| @name marq-size # . . . . . . . . font size, in pixels"));
877         msg_rc(  "| ");
878         msg_rc("%s", _("| @name logo-file STRING . . .the overlay file path/name"));
879         msg_rc("%s", _("| @name logo-x X . . . . . . . . . . . .offset from left"));
880         msg_rc("%s", _("| @name logo-y Y . . . . . . . . . . . . offset from top"));
881         msg_rc("%s", _("| @name logo-position #. . . . . . . . relative position"));
882         msg_rc("%s", _("| @name logo-transparency #. . . . . . . . .transparency"));
883         msg_rc(  "| ");
884         msg_rc("%s", _("| @name mosaic-alpha # . . . . . . . . . . . . . . alpha"));
885         msg_rc("%s", _("| @name mosaic-height #. . . . . . . . . . . . . .height"));
886         msg_rc("%s", _("| @name mosaic-width # . . . . . . . . . . . . . . width"));
887         msg_rc("%s", _("| @name mosaic-xoffset # . . . .top left corner position"));
888         msg_rc("%s", _("| @name mosaic-yoffset # . . . .top left corner position"));
889         msg_rc("%s", _("| @name mosaic-offsets x,y(,x,y)*. . . . list of offsets"));
890         msg_rc("%s", _("| @name mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
891         msg_rc("%s", _("| @name mosaic-vborder # . . . . . . . . vertical border"));
892         msg_rc("%s", _("| @name mosaic-hborder # . . . . . . . horizontal border"));
893         msg_rc("%s", _("| @name mosaic-position {0=auto,1=fixed} . . . .position"));
894         msg_rc("%s", _("| @name mosaic-rows #. . . . . . . . . . .number of rows"));
895         msg_rc("%s", _("| @name mosaic-cols #. . . . . . . . . . .number of cols"));
896         msg_rc("%s", _("| @name mosaic-order id(,id)* . . . . order of pictures "));
897         msg_rc("%s", _("| @name mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
898         msg_rc(  "| ");
899     }
900     msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
901     msg_rc("%s", _("| longhelp . . . . . . . . . . . a longer help message"));
902     msg_rc("%s", _("| logout . . . . . . .  exit (if in socket connection)"));
903     msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . .  quit vlc"));
904     msg_rc(  "| ");
905     msg_rc("%s", _("+----[ end of help ]"));
906 }
907
908 /********************************************************************
909  * Status callback routines
910  ********************************************************************/
911 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
912     vlc_value_t oldval, vlc_value_t newval, void *p_data )
913 {
914     (void) p_this;
915     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
916     intf_thread_t *p_intf = (intf_thread_t*)p_data;
917
918     vlc_mutex_lock( &p_intf->p_sys->status_lock );
919     msg_rc( STATUS_CHANGE "( audio volume: %ld )",
920             lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
921     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
922     return VLC_SUCCESS;
923 }
924
925 static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
926 {
927     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
928
929     PL_LOCK;
930     const int i_status = playlist_Status( p_playlist );
931     PL_UNLOCK;
932
933     /* */
934     const char *psz_cmd;
935     switch( i_status )
936     {
937     case PLAYLIST_STOPPED:
938         psz_cmd = "stop";
939         break;
940     case PLAYLIST_RUNNING:
941         psz_cmd = "play";
942         break;
943     case PLAYLIST_PAUSED:
944         psz_cmd = "pause";
945         break;
946     default:
947         psz_cmd = "";
948         break;
949     }
950
951     /* */
952     const int i_state = var_GetInteger( p_input, "state" );
953
954     vlc_mutex_lock( &p_intf->p_sys->status_lock );
955     msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
956             i_state, ppsz_input_state[i_state] );
957     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
958 }
959 static void RateChanged( intf_thread_t *p_intf,
960                          input_thread_t *p_input )
961 {
962     vlc_mutex_lock( &p_intf->p_sys->status_lock );
963     msg_rc( STATUS_CHANGE "( new rate: %.3f )",
964             var_GetFloat( p_input, "rate" ) );
965     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
966 }
967 static void PositionChanged( intf_thread_t *p_intf,
968                              input_thread_t *p_input )
969 {
970     vlc_mutex_lock( &p_intf->p_sys->status_lock );
971     if( p_intf->p_sys->b_input_buffering )
972         msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
973                 (var_GetTime( p_input, "time" )/1000000) );
974     p_intf->p_sys->b_input_buffering = false;
975     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
976 }
977 static void CacheChanged( intf_thread_t *p_intf )
978 {
979     vlc_mutex_lock( &p_intf->p_sys->status_lock );
980     p_intf->p_sys->b_input_buffering = true;
981     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
982 }
983
984 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
985                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
986 {
987     VLC_UNUSED(psz_cmd);
988     VLC_UNUSED(oldval);
989     input_thread_t *p_input = (input_thread_t*)p_this;
990     intf_thread_t *p_intf = p_data;
991
992     switch( newval.i_int )
993     {
994     case INPUT_EVENT_STATE:
995     case INPUT_EVENT_DEAD:
996         StateChanged( p_intf, p_input );
997         break;
998     case INPUT_EVENT_RATE:
999         RateChanged( p_intf, p_input );
1000         break;
1001     case INPUT_EVENT_POSITION:
1002         PositionChanged( p_intf, p_input );
1003         break;
1004     case INPUT_EVENT_CACHE:
1005         CacheChanged( p_intf );
1006         break;
1007     default:
1008         break;
1009     }
1010     return VLC_SUCCESS;
1011 }
1012
1013 /********************************************************************
1014  * Command routines
1015  ********************************************************************/
1016 static int Input( vlc_object_t *p_this, char const *psz_cmd,
1017                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
1018 {
1019     VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1020     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1021     input_thread_t *p_input =
1022         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1023     int i_error = VLC_EGENERIC;
1024
1025     if( !p_input )
1026         return VLC_ENOOBJ;
1027
1028     int state = var_GetInteger( p_input, "state" );
1029     if( ( state == PAUSE_S ) &&
1030         ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
1031     {
1032         msg_rc( "%s", _("Press menu select or pause to continue.") );
1033     }
1034     else
1035     /* Parse commands that only require an input */
1036     if( !strcmp( psz_cmd, "pause" ) )
1037     {
1038         playlist_Pause( p_intf->p_sys->p_playlist );
1039         i_error = VLC_SUCCESS;
1040     }
1041     else if( !strcmp( psz_cmd, "seek" ) )
1042     {
1043         if( strlen( newval.psz_string ) > 0 &&
1044             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
1045         {
1046             float f = atof( newval.psz_string ) / 100.0;
1047             var_SetFloat( p_input, "position", f );
1048         }
1049         else
1050         {
1051             mtime_t t = ((int64_t)atoi( newval.psz_string )) * CLOCK_FREQ;
1052             var_SetTime( p_input, "time", t );
1053         }
1054         i_error = VLC_SUCCESS;
1055     }
1056     else if ( !strcmp( psz_cmd, "fastforward" ) )
1057     {
1058         if( var_GetBool( p_input, "can-rate" ) )
1059         {
1060             float f_rate = var_GetFloat( p_input, "rate" );
1061             f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
1062             var_SetFloat( p_input, "rate", f_rate );
1063         }
1064         else
1065         {
1066             var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
1067         }
1068         i_error = VLC_SUCCESS;
1069     }
1070     else if ( !strcmp( psz_cmd, "rewind" ) )
1071     {
1072         if( var_GetBool( p_input, "can-rewind" ) )
1073         {
1074             float f_rate = var_GetFloat( p_input, "rate" );
1075             f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
1076             var_SetFloat( p_input, "rate", f_rate );
1077         }
1078         else
1079         {
1080             var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1081         }
1082         i_error = VLC_SUCCESS;
1083     }
1084     else if ( !strcmp( psz_cmd, "faster" ) )
1085     {
1086         var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
1087         i_error = VLC_SUCCESS;
1088     }
1089     else if ( !strcmp( psz_cmd, "slower" ) )
1090     {
1091         var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
1092         i_error = VLC_SUCCESS;
1093     }
1094     else if ( !strcmp( psz_cmd, "normal" ) )
1095     {
1096         var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
1097         i_error = VLC_SUCCESS;
1098     }
1099     else if ( !strcmp( psz_cmd, "frame" ) )
1100     {
1101         var_TriggerCallback( p_input, "frame-next" );
1102         i_error = VLC_SUCCESS;
1103     }
1104     else if( !strcmp( psz_cmd, "chapter" ) ||
1105              !strcmp( psz_cmd, "chapter_n" ) ||
1106              !strcmp( psz_cmd, "chapter_p" ) )
1107     {
1108         if( !strcmp( psz_cmd, "chapter" ) )
1109         {
1110             if ( *newval.psz_string )
1111             {
1112                 /* Set. */
1113                 var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1114             }
1115             else
1116             {
1117                 /* Get. */
1118                 int i_chap = var_GetInteger( p_input, "chapter" );
1119                 int i_chapter_count = var_CountChoices( p_input, "chapter" );
1120                 msg_rc( "Currently playing chapter %d/%d.", i_chap,
1121                         i_chapter_count );
1122             }
1123         }
1124         else if( !strcmp( psz_cmd, "chapter_n" ) )
1125             var_TriggerCallback( p_input, "next-chapter" );
1126         else if( !strcmp( psz_cmd, "chapter_p" ) )
1127             var_TriggerCallback( p_input, "prev-chapter" );
1128         i_error = VLC_SUCCESS;
1129     }
1130     else if( !strcmp( psz_cmd, "title" ) ||
1131              !strcmp( psz_cmd, "title_n" ) ||
1132              !strcmp( psz_cmd, "title_p" ) )
1133     {
1134         if( !strcmp( psz_cmd, "title" ) )
1135         {
1136             if ( *newval.psz_string )
1137                 /* Set. */
1138                 var_SetInteger( p_input, "title", atoi( newval.psz_string ) );
1139             else
1140             {
1141                 /* Get. */
1142                 int i_title = var_GetInteger( p_input, "title" );
1143                 int i_title_count = var_CountChoices( p_input, "title" );
1144                 msg_rc( "Currently playing title %d/%d.", i_title,
1145                         i_title_count );
1146             }
1147         }
1148         else if( !strcmp( psz_cmd, "title_n" ) )
1149             var_TriggerCallback( p_input, "next-title" );
1150         else if( !strcmp( psz_cmd, "title_p" ) )
1151             var_TriggerCallback( p_input, "prev-title" );
1152
1153         i_error = VLC_SUCCESS;
1154     }
1155     else if(    !strcmp( psz_cmd, "atrack" )
1156              || !strcmp( psz_cmd, "vtrack" )
1157              || !strcmp( psz_cmd, "strack" ) )
1158     {
1159         const char *psz_variable;
1160         vlc_value_t val_name;
1161
1162         if( !strcmp( psz_cmd, "atrack" ) )
1163         {
1164             psz_variable = "audio-es";
1165         }
1166         else if( !strcmp( psz_cmd, "vtrack" ) )
1167         {
1168             psz_variable = "video-es";
1169         }
1170         else
1171         {
1172             psz_variable = "spu-es";
1173         }
1174
1175         /* Get the descriptive name of the variable */
1176         var_Change( p_input, psz_variable, VLC_VAR_GETTEXT,
1177                      &val_name, NULL );
1178         if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1179
1180         if( newval.psz_string && *newval.psz_string )
1181         {
1182             /* set */
1183             i_error = var_SetInteger( p_input, psz_variable,
1184                                       atoi( newval.psz_string ) );
1185         }
1186         else
1187         {
1188             /* get */
1189             vlc_value_t val, text;
1190             int i, i_value;
1191
1192             if ( var_Get( p_input, psz_variable, &val ) < 0 )
1193                 goto out;
1194             i_value = val.i_int;
1195
1196             if ( var_Change( p_input, psz_variable,
1197                              VLC_VAR_GETLIST, &val, &text ) < 0 )
1198                 goto out;
1199
1200             msg_rc( "+----[ %s ]", val_name.psz_string );
1201             for ( i = 0; i < val.p_list->i_count; i++ )
1202             {
1203                 if ( i_value == val.p_list->p_values[i].i_int )
1204                     msg_rc( "| %"PRId64" - %s *",
1205                             val.p_list->p_values[i].i_int,
1206                             text.p_list->p_values[i].psz_string );
1207                 else
1208                     msg_rc( "| %"PRId64" - %s",
1209                             val.p_list->p_values[i].i_int,
1210                             text.p_list->p_values[i].psz_string );
1211             }
1212             var_FreeList( &val, &text );
1213             msg_rc( "+----[ end of %s ]", val_name.psz_string );
1214         }
1215         free( val_name.psz_string );
1216     }
1217 out:
1218     vlc_object_release( p_input );
1219     return i_error;
1220 }
1221
1222 static void print_playlist( intf_thread_t *p_intf, playlist_item_t *p_item, int i_level )
1223 {
1224     int i;
1225     char psz_buffer[MSTRTIME_MAX_SIZE];
1226     for( i = 0; i< p_item->i_children; i++ )
1227     {
1228         if( p_item->pp_children[i]->p_input->i_duration != -1 )
1229         {
1230             secstotimestr( psz_buffer, p_item->pp_children[i]->p_input->i_duration / 1000000 );
1231             msg_rc( "|%*s- %s (%s)", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name, psz_buffer );
1232         }
1233         else
1234             msg_rc( "|%*s- %s", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name );
1235
1236         if( p_item->pp_children[i]->i_children >= 0 )
1237             print_playlist( p_intf, p_item->pp_children[i], i_level + 1 );
1238     }
1239 }
1240
1241 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1242                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1243 {
1244     VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1245     vlc_value_t val;
1246
1247     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1248     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1249     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1250
1251     if( p_input )
1252     {
1253         int state = var_GetInteger( p_input, "state" );
1254         vlc_object_release( p_input );
1255
1256         if( state == PAUSE_S )
1257         {
1258             msg_rc( "%s", _("Type 'menu select' or 'pause' to continue.") );
1259             return VLC_EGENERIC;
1260         }
1261     }
1262
1263     /* Parse commands that require a playlist */
1264     if( !strcmp( psz_cmd, "prev" ) )
1265     {
1266         playlist_Prev( p_playlist );
1267     }
1268     else if( !strcmp( psz_cmd, "next" ) )
1269     {
1270         playlist_Next( p_playlist );
1271     }
1272     else if( !strcmp( psz_cmd, "play" ) )
1273     {
1274         msg_Warn( p_playlist, "play" );
1275         playlist_Play( p_playlist );
1276     }
1277     else if( !strcmp( psz_cmd, "repeat" ) )
1278     {
1279         bool b_update = true;
1280
1281         var_Get( p_playlist, "repeat", &val );
1282
1283         if( strlen( newval.psz_string ) > 0 )
1284         {
1285             if ( ( !strncmp( newval.psz_string, "on", 2 )  &&  val.b_bool ) ||
1286                  ( !strncmp( newval.psz_string, "off", 3 ) && !val.b_bool ) )
1287             {
1288                 b_update = false;
1289             }
1290         }
1291
1292         if ( b_update )
1293         {
1294             val.b_bool = !val.b_bool;
1295             var_Set( p_playlist, "repeat", val );
1296         }
1297         msg_rc( "Setting repeat to %d", val.b_bool );
1298     }
1299     else if( !strcmp( psz_cmd, "loop" ) )
1300     {
1301         bool b_update = true;
1302
1303         var_Get( p_playlist, "loop", &val );
1304
1305         if( strlen( newval.psz_string ) > 0 )
1306         {
1307             if ( ( !strncmp( newval.psz_string, "on", 2 )  &&  val.b_bool ) ||
1308                  ( !strncmp( newval.psz_string, "off", 3 ) && !val.b_bool ) )
1309             {
1310                 b_update = false;
1311             }
1312         }
1313
1314         if ( b_update )
1315         {
1316             val.b_bool = !val.b_bool;
1317             var_Set( p_playlist, "loop", val );
1318         }
1319         msg_rc( "Setting loop to %d", val.b_bool );
1320     }
1321     else if( !strcmp( psz_cmd, "random" ) )
1322     {
1323         bool b_update = true;
1324
1325         var_Get( p_playlist, "random", &val );
1326
1327         if( strlen( newval.psz_string ) > 0 )
1328         {
1329             if ( ( !strncmp( newval.psz_string, "on", 2 )  &&  val.b_bool ) ||
1330                  ( !strncmp( newval.psz_string, "off", 3 ) && !val.b_bool ) )
1331             {
1332                 b_update = false;
1333             }
1334         }
1335
1336         if ( b_update )
1337         {
1338             val.b_bool = !val.b_bool;
1339             var_Set( p_playlist, "random", val );
1340         }
1341         msg_rc( "Setting random to %d", val.b_bool );
1342     }
1343     else if (!strcmp( psz_cmd, "goto" ) )
1344     {
1345         PL_LOCK;
1346         unsigned i_pos = atoi( newval.psz_string );
1347         unsigned i_size = p_playlist->items.i_size;
1348
1349         if( i_pos <= 0 )
1350             msg_rc( "%s", _("Error: `goto' needs an argument greater than zero.") );
1351         else if( i_pos <= i_size )
1352         {
1353             playlist_item_t *p_item, *p_parent;
1354             p_item = p_parent = p_playlist->items.p_elems[i_pos-1];
1355             while( p_parent->p_parent )
1356                 p_parent = p_parent->p_parent;
1357             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, pl_Locked,
1358                     p_parent, p_item );
1359         }
1360         else
1361             msg_rc( vlc_ngettext("Playlist has only %u element",
1362                                  "Playlist has only %u elements", i_size),
1363                      i_size );
1364         PL_UNLOCK;
1365     }
1366     else if( !strcmp( psz_cmd, "stop" ) )
1367     {
1368         playlist_Stop( p_playlist );
1369     }
1370     else if( !strcmp( psz_cmd, "clear" ) )
1371     {
1372         playlist_Stop( p_playlist );
1373         playlist_Clear( p_playlist, pl_Unlocked );
1374     }
1375     else if( !strcmp( psz_cmd, "add" ) &&
1376              newval.psz_string && *newval.psz_string )
1377     {
1378         input_item_t *p_item = parse_MRL( newval.psz_string );
1379
1380         if( p_item )
1381         {
1382             msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1383             int i_ret =playlist_AddInput( p_playlist, p_item,
1384                      PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END, true,
1385                      pl_Unlocked );
1386             vlc_gc_decref( p_item );
1387             if( i_ret != VLC_SUCCESS )
1388             {
1389                 return VLC_EGENERIC;
1390             }
1391         }
1392     }
1393     else if( !strcmp( psz_cmd, "enqueue" ) &&
1394              newval.psz_string && *newval.psz_string )
1395     {
1396         input_item_t *p_item = parse_MRL( newval.psz_string );
1397
1398         if( p_item )
1399         {
1400             msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1401             if( playlist_AddInput( p_playlist, p_item,
1402                                PLAYLIST_APPEND, PLAYLIST_END, true,
1403                                pl_Unlocked ) != VLC_SUCCESS )
1404             {
1405                 return VLC_EGENERIC;
1406             }
1407         }
1408     }
1409     else if( !strcmp( psz_cmd, "playlist" ) )
1410     {
1411         msg_rc( "+----[ Playlist ]" );
1412         print_playlist( p_intf, p_playlist->p_root_category, 0 );
1413         msg_rc( "+----[ End of playlist ]" );
1414     }
1415
1416     else if( !strcmp( psz_cmd, "sort" ))
1417     {
1418         PL_LOCK;
1419         playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_onelevel,
1420                                     SORT_ARTIST, ORDER_NORMAL );
1421         PL_UNLOCK;
1422     }
1423     else if( !strcmp( psz_cmd, "status" ) )
1424     {
1425         input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1426         if( p_input )
1427         {
1428             /* Replay the current state of the system. */
1429             char *psz_uri =
1430                     input_item_GetURI( input_GetItem( p_input ) );
1431             vlc_object_release( p_input );
1432             if( likely(psz_uri != NULL) )
1433             {
1434                 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
1435                 free( psz_uri );
1436             }
1437         }
1438
1439         float volume = playlist_VolumeGet( p_playlist );
1440         if( volume >= 0.f )
1441             msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1442                     lroundf(volume * AOUT_VOLUME_DEFAULT) );
1443
1444         int status;
1445         PL_LOCK;
1446         status = playlist_Status(p_playlist);
1447         PL_UNLOCK;
1448         switch( status )
1449         {
1450             case PLAYLIST_STOPPED:
1451                 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
1452                 break;
1453             case PLAYLIST_RUNNING:
1454                 msg_rc( STATUS_CHANGE "( play state: 3 )" );
1455                 break;
1456             case PLAYLIST_PAUSED:
1457                 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
1458                 break;
1459             default:
1460                 msg_rc( STATUS_CHANGE "( unknown state: -1 )" );
1461                 break;
1462         }
1463     }
1464
1465     /*
1466      * sanity check
1467      */
1468     else
1469     {
1470         msg_rc( "unknown command!" );
1471     }
1472
1473     return VLC_SUCCESS;
1474 }
1475
1476 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1477                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1478 {
1479     VLC_UNUSED(p_data); VLC_UNUSED(psz_cmd);
1480     VLC_UNUSED(oldval); VLC_UNUSED(newval);
1481
1482     libvlc_Quit( p_this->p_libvlc );
1483     return VLC_SUCCESS;
1484 }
1485
1486 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1487                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1488 {
1489     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1490
1491     return intf_Create( p_this->p_libvlc, newval.psz_string );
1492 }
1493
1494 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1495                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
1496 {
1497     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1498     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1499     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1500     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
1501     int i_error = VLC_EGENERIC;
1502
1503     if( !p_input )
1504         return VLC_ENOOBJ;
1505
1506     if( p_input )
1507     {
1508         int state = var_GetInteger( p_input, "state" );
1509         vlc_object_release( p_input );
1510         if( state == PAUSE_S )
1511         {
1512             msg_rc( "%s", _("Type 'menu select' or 'pause' to continue.") );
1513             return VLC_EGENERIC;
1514         }
1515     }
1516
1517     if ( *newval.psz_string )
1518     {
1519         /* Set. */
1520         int i_volume = atoi( newval.psz_string );
1521         if( !playlist_VolumeSet( p_playlist,
1522                              i_volume / (float)AOUT_VOLUME_DEFAULT ) )
1523             i_error = VLC_SUCCESS;
1524         playlist_MuteSet( p_playlist, i_volume == 0 );
1525         msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1526     }
1527     else
1528     {
1529         /* Get. */
1530         msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1531                lroundf( playlist_VolumeGet( p_playlist ) * AOUT_VOLUME_DEFAULT ) );
1532         i_error = VLC_SUCCESS;
1533     }
1534
1535     return i_error;
1536 }
1537
1538 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1539                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1540 {
1541     VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1542     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1543     float volume;
1544     input_thread_t *p_input =
1545         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1546     int i_nb_steps = atoi(newval.psz_string);
1547     int i_error = VLC_SUCCESS;
1548
1549     if( !p_input )
1550         return VLC_ENOOBJ;
1551
1552     int state = var_GetInteger( p_input, "state" );
1553     vlc_object_release( p_input );
1554     if( state == PAUSE_S )
1555     {
1556         msg_rc( "%s", _("Type 'menu select' or 'pause' to continue.") );
1557         return VLC_EGENERIC;
1558     }
1559
1560     if( !strcmp(psz_cmd, "voldown") )
1561         i_nb_steps *= -1;
1562     if( playlist_VolumeUp( p_intf->p_sys->p_playlist, i_nb_steps, &volume ) < 0 )
1563         i_error = VLC_EGENERIC;
1564
1565     if ( !i_error )
1566         msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1567                 lroundf( volume * AOUT_VOLUME_DEFAULT ) );
1568     return i_error;
1569 }
1570
1571
1572 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1573                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1574 {
1575     VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1576     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1577     input_thread_t *p_input =
1578         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1579     vout_thread_t * p_vout;
1580     const char * psz_variable = NULL;
1581     int i_error = VLC_SUCCESS;
1582
1583     if( !p_input )
1584         return VLC_ENOOBJ;
1585
1586     p_vout = input_GetVout( p_input );
1587     vlc_object_release( p_input );
1588     if( !p_vout )
1589         return VLC_ENOOBJ;
1590
1591     if( !strcmp( psz_cmd, "vcrop" ) )
1592     {
1593         psz_variable = "crop";
1594     }
1595     else if( !strcmp( psz_cmd, "vratio" ) )
1596     {
1597         psz_variable = "aspect-ratio";
1598     }
1599     else if( !strcmp( psz_cmd, "vzoom" ) )
1600     {
1601         psz_variable = "zoom";
1602     }
1603     else if( !strcmp( psz_cmd, "snapshot" ) )
1604     {
1605         psz_variable = "video-snapshot";
1606     }
1607     else
1608         /* This case can't happen */
1609         assert( 0 );
1610
1611     if( newval.psz_string && *newval.psz_string )
1612     {
1613         /* set */
1614         if( !strcmp( psz_variable, "zoom" ) )
1615         {
1616             vlc_value_t val;
1617             val.f_float = atof( newval.psz_string );
1618             i_error = var_Set( p_vout, psz_variable, val );
1619         }
1620         else
1621         {
1622             i_error = var_Set( p_vout, psz_variable, newval );
1623         }
1624     }
1625     else if( !strcmp( psz_cmd, "snapshot" ) )
1626     {
1627         var_TriggerCallback( p_vout, psz_variable );
1628     }
1629     else
1630     {
1631         /* get */
1632         vlc_value_t val_name;
1633         vlc_value_t val, text;
1634         int i;
1635         float f_value = 0.;
1636         char *psz_value = NULL;
1637
1638         if ( var_Get( p_vout, psz_variable, &val ) < 0 )
1639         {
1640             vlc_object_release( p_vout );
1641             return VLC_EGENERIC;
1642         }
1643         if( !strcmp( psz_variable, "zoom" ) )
1644         {
1645             f_value = val.f_float;
1646         }
1647         else
1648         {
1649             psz_value = val.psz_string;
1650         }
1651
1652         if ( var_Change( p_vout, psz_variable,
1653                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1654         {
1655             vlc_object_release( p_vout );
1656             free( psz_value );
1657             return VLC_EGENERIC;
1658         }
1659
1660         /* Get the descriptive name of the variable */
1661         var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT,
1662                     &val_name, NULL );
1663         if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1664
1665         msg_rc( "+----[ %s ]", val_name.psz_string );
1666         if( !strcmp( psz_variable, "zoom" ) )
1667         {
1668             for ( i = 0; i < val.p_list->i_count; i++ )
1669             {
1670                 if ( f_value == val.p_list->p_values[i].f_float )
1671                     msg_rc( "| %f - %s *", val.p_list->p_values[i].f_float,
1672                             text.p_list->p_values[i].psz_string );
1673                 else
1674                     msg_rc( "| %f - %s", val.p_list->p_values[i].f_float,
1675                             text.p_list->p_values[i].psz_string );
1676             }
1677         }
1678         else
1679         {
1680             for ( i = 0; i < val.p_list->i_count; i++ )
1681             {
1682                 if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1683                     msg_rc( "| %s - %s *", val.p_list->p_values[i].psz_string,
1684                             text.p_list->p_values[i].psz_string );
1685                 else
1686                     msg_rc( "| %s - %s", val.p_list->p_values[i].psz_string,
1687                             text.p_list->p_values[i].psz_string );
1688             }
1689             free( psz_value );
1690         }
1691         var_FreeList( &val, &text );
1692         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1693
1694         free( val_name.psz_string );
1695     }
1696     vlc_object_release( p_vout );
1697     return i_error;
1698 }
1699
1700 static int AudioDevice( vlc_object_t *obj, char const *cmd,
1701                         vlc_value_t old, vlc_value_t cur, void *dummy )
1702 {
1703     intf_thread_t *p_intf = (intf_thread_t *)obj;
1704     audio_output_t *p_aout = playlist_GetAout( pl_Get(p_intf) );
1705     if( p_aout == NULL )
1706         return VLC_ENOOBJ;
1707
1708     if( !*cur.psz_string )
1709     {
1710         char **ids, **names;
1711         int n = aout_DevicesList( p_aout, &ids, &names );
1712         if( n < 0 )
1713             goto out;
1714
1715         char *dev = aout_DeviceGet( p_aout );
1716         const char *devstr = (dev != NULL) ? dev : "";
1717
1718         msg_rc( "+----[ %s ]", cmd );
1719         for ( int i = 0; i < n; i++ )
1720         {
1721             const char *fmt = "| %s - %s";
1722
1723             if( !strcmp(devstr, ids[i]) )
1724                 fmt = "| %s - %s *";
1725             msg_rc( fmt, ids[i], names[i] );
1726             free( names[i] );
1727             free( ids[i] );
1728         }
1729         msg_rc( "+----[ end of %s ]", cmd );
1730
1731         free( dev );
1732         free( names );
1733         free( ids );
1734     }
1735     else
1736         aout_DeviceSet( p_aout, cur.psz_string );
1737 out:
1738     vlc_object_release( p_aout );
1739     (void) old; (void) dummy;
1740     return VLC_SUCCESS;
1741 }
1742
1743 static int AudioChannel( vlc_object_t *obj, char const *cmd,
1744                          vlc_value_t old, vlc_value_t cur, void *dummy )
1745 {
1746     intf_thread_t *p_intf = (intf_thread_t*)obj;
1747     vlc_object_t *p_aout = (vlc_object_t *)playlist_GetAout( pl_Get(p_intf) );
1748     if ( p_aout == NULL )
1749          return VLC_ENOOBJ;
1750
1751     int ret = VLC_SUCCESS;
1752
1753     if ( !*cur.psz_string )
1754     {
1755         /* Retrieve all registered ***. */
1756         vlc_value_t val, text;
1757         if ( var_Change( p_aout, "stereo-mode",
1758                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1759         {
1760             ret = VLC_ENOVAR;
1761             goto out;
1762         }
1763
1764         int i_value = var_GetInteger( p_aout, "stereo-mode" );
1765
1766         msg_rc( "+----[ %s ]", cmd );
1767         for ( int i = 0; i < val.p_list->i_count; i++ )
1768         {
1769             if ( i_value == val.p_list->p_values[i].i_int )
1770                 msg_rc( "| %"PRId64" - %s *", val.p_list->p_values[i].i_int,
1771                         text.p_list->p_values[i].psz_string );
1772             else
1773                 msg_rc( "| %"PRId64" - %s", val.p_list->p_values[i].i_int,
1774                         text.p_list->p_values[i].psz_string );
1775         }
1776         var_FreeList( &val, &text );
1777         msg_rc( "+----[ end of %s ]", cmd );
1778     }
1779     else
1780         ret = var_SetInteger( p_aout, "stereo-mode", atoi( cur.psz_string ) );
1781 out:
1782     vlc_object_release( p_aout );
1783     (void) old; (void) dummy;
1784     return ret;
1785 }
1786
1787 static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
1788     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1789 {
1790     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1791     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1792     input_thread_t *p_input =
1793         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1794
1795     if( !p_input )
1796         return VLC_ENOOBJ;
1797
1798     updateStatistics( p_intf, input_GetItem(p_input) );
1799     vlc_object_release( p_input );
1800     return VLC_SUCCESS;
1801 }
1802
1803 static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
1804 {
1805     if( !p_item ) return VLC_EGENERIC;
1806
1807     vlc_mutex_lock( &p_item->lock );
1808     vlc_mutex_lock( &p_item->p_stats->lock );
1809     msg_rc( "+----[ begin of statistical info ]" );
1810
1811     /* Input */
1812     msg_rc("%s", _("+-[Incoming]"));
1813     msg_rc(_("| input bytes read : %8.0f KiB"),
1814             (float)(p_item->p_stats->i_read_bytes)/1024 );
1815     msg_rc(_("| input bitrate    :   %6.0f kb/s"),
1816             (float)(p_item->p_stats->f_input_bitrate)*8000 );
1817     msg_rc(_("| demux bytes read : %8.0f KiB"),
1818             (float)(p_item->p_stats->i_demux_read_bytes)/1024 );
1819     msg_rc(_("| demux bitrate    :   %6.0f kb/s"),
1820             (float)(p_item->p_stats->f_demux_bitrate)*8000 );
1821     msg_rc(_("| demux corrupted  :    %5"PRIi64),
1822             p_item->p_stats->i_demux_corrupted );
1823     msg_rc(_("| discontinuities  :    %5"PRIi64),
1824             p_item->p_stats->i_demux_discontinuity );
1825     msg_rc("|");
1826     /* Video */
1827     msg_rc("%s", _("+-[Video Decoding]"));
1828     msg_rc(_("| video decoded    :    %5"PRIi64),
1829             p_item->p_stats->i_decoded_video );
1830     msg_rc(_("| frames displayed :    %5"PRIi64),
1831             p_item->p_stats->i_displayed_pictures );
1832     msg_rc(_("| frames lost      :    %5"PRIi64),
1833             p_item->p_stats->i_lost_pictures );
1834     msg_rc("|");
1835     /* Audio*/
1836     msg_rc("%s", _("+-[Audio Decoding]"));
1837     msg_rc(_("| audio decoded    :    %5"PRIi64),
1838             p_item->p_stats->i_decoded_audio );
1839     msg_rc(_("| buffers played   :    %5"PRIi64),
1840             p_item->p_stats->i_played_abuffers );
1841     msg_rc(_("| buffers lost     :    %5"PRIi64),
1842             p_item->p_stats->i_lost_abuffers );
1843     msg_rc("|");
1844     /* Sout */
1845     msg_rc("%s", _("+-[Streaming]"));
1846     msg_rc(_("| packets sent     :    %5"PRIi64),
1847            p_item->p_stats->i_sent_packets );
1848     msg_rc(_("| bytes sent       : %8.0f KiB"),
1849             (float)(p_item->p_stats->i_sent_bytes)/1024 );
1850     msg_rc(_("| sending bitrate  :   %6.0f kb/s"),
1851             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
1852     msg_rc("|");
1853     msg_rc( "+----[ end of statistical info ]" );
1854     vlc_mutex_unlock( &p_item->p_stats->lock );
1855     vlc_mutex_unlock( &p_item->lock );
1856
1857     return VLC_SUCCESS;
1858 }
1859
1860 #ifdef WIN32
1861 static bool ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1862 {
1863     INPUT_RECORD input_record;
1864     DWORD i_dw;
1865
1866     /* On Win32, select() only works on socket descriptors */
1867     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1868                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1869     {
1870         while( *pi_size < MAX_LINE_LENGTH &&
1871                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1872                                  1, &i_dw ) )
1873         {
1874             if( input_record.EventType != KEY_EVENT ||
1875                 !input_record.Event.KeyEvent.bKeyDown ||
1876                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1877                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1878                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1879                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1880             {
1881                 /* nothing interesting */
1882                 continue;
1883             }
1884
1885             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1886
1887             /* Echo out the command */
1888             putc( p_buffer[ *pi_size ], stdout );
1889
1890             /* Handle special keys */
1891             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1892             {
1893                 putc( '\n', stdout );
1894                 break;
1895             }
1896             switch( p_buffer[ *pi_size ] )
1897             {
1898             case '\b':
1899                 if( *pi_size )
1900                 {
1901                     *pi_size -= 2;
1902                     putc( ' ', stdout );
1903                     putc( '\b', stdout );
1904                 }
1905                 break;
1906             case '\r':
1907                 (*pi_size) --;
1908                 break;
1909             }
1910
1911             (*pi_size)++;
1912         }
1913
1914         if( *pi_size == MAX_LINE_LENGTH ||
1915             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1916         {
1917             p_buffer[ *pi_size ] = 0;
1918             return true;
1919         }
1920     }
1921
1922     return false;
1923 }
1924 #endif
1925
1926 bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1927 {
1928     int i_read = 0;
1929
1930 #ifdef WIN32
1931     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1932         return ReadWin32( p_intf, p_buffer, pi_size );
1933     else if( p_intf->p_sys->i_socket == -1 )
1934     {
1935         msleep( INTF_IDLE_SLEEP );
1936         return false;
1937     }
1938 #endif
1939
1940     while( *pi_size < MAX_LINE_LENGTH &&
1941            (i_read = net_Read( p_intf, p_intf->p_sys->i_socket == -1 ?
1942                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
1943                   (uint8_t *)p_buffer + *pi_size, 1, false ) ) > 0 )
1944     {
1945         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1946             break;
1947
1948         (*pi_size)++;
1949     }
1950
1951     /* Connection closed */
1952     if( i_read <= 0 )
1953     {
1954         if( p_intf->p_sys->i_socket != -1 )
1955         {
1956             net_Close( p_intf->p_sys->i_socket );
1957             p_intf->p_sys->i_socket = -1;
1958         }
1959         else
1960         {
1961             /* Standard input closed: exit */
1962             vlc_value_t empty;
1963             Quit( VLC_OBJECT(p_intf), NULL, empty, empty, NULL );
1964         }
1965
1966         p_buffer[ *pi_size ] = 0;
1967         return true;
1968     }
1969
1970     if( *pi_size == MAX_LINE_LENGTH ||
1971         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1972     {
1973         p_buffer[ *pi_size ] = 0;
1974         return true;
1975     }
1976
1977     return false;
1978 }
1979
1980 /*****************************************************************************
1981  * parse_MRL: build a input item from a full mrl
1982  *****************************************************************************
1983  * MRL format: "simplified-mrl [:option-name[=option-value]]"
1984  * We don't check for '"' or '\'', we just assume that a ':' that follows a
1985  * space is a new option. Should be good enough for our purpose.
1986  *****************************************************************************/
1987 static input_item_t *parse_MRL( const char *mrl )
1988 {
1989 #define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
1990 #define SKIPTRAILINGSPACE( p, d ) \
1991     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1992
1993     input_item_t *p_item = NULL;
1994     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig, *psz_mrl;
1995     char **ppsz_options = NULL;
1996     int i, i_options = 0;
1997
1998     if( !mrl ) return 0;
1999
2000     psz_mrl = psz_orig = strdup( mrl );
2001     if( !psz_mrl )
2002         return NULL;
2003     while( *psz_mrl )
2004     {
2005         SKIPSPACE( psz_mrl );
2006         psz_item = psz_mrl;
2007
2008         for( ; *psz_mrl; psz_mrl++ )
2009         {
2010             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
2011             {
2012                 /* We have a complete item */
2013                 break;
2014             }
2015             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
2016                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
2017             {
2018                 /* We have a complete item */
2019                 break;
2020             }
2021         }
2022
2023         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
2024         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
2025
2026         /* Remove '"' and '\'' if necessary */
2027         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
2028         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2029         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
2030         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2031
2032         if( !psz_item_mrl )
2033         {
2034             if( strstr( psz_item, "://" ) != NULL )
2035                 psz_item_mrl = strdup( psz_item );
2036             else
2037                 psz_item_mrl = vlc_path2uri( psz_item, NULL );
2038             if( psz_item_mrl == NULL )
2039             {
2040                 free( psz_orig );
2041                 return NULL;
2042             }
2043         }
2044         else if( *psz_item )
2045         {
2046             i_options++;
2047             ppsz_options = xrealloc( ppsz_options, i_options * sizeof(char *) );
2048             ppsz_options[i_options - 1] = &psz_item[1];
2049         }
2050
2051         if( *psz_mrl ) SKIPSPACE( psz_mrl );
2052     }
2053
2054     /* Now create a playlist item */
2055     if( psz_item_mrl )
2056     {
2057         p_item = input_item_New( psz_item_mrl, NULL );
2058         for( i = 0; i < i_options; i++ )
2059         {
2060             input_item_AddOption( p_item, ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
2061         }
2062         free( psz_item_mrl );
2063     }
2064
2065     if( i_options ) free( ppsz_options );
2066     free( psz_orig );
2067
2068     return p_item;
2069 }