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