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