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