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