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