]> git.sesse.net Git - vlc/blob - modules/control/rc.c
Remove skins2 dependency on libxml2 for Win32
[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 <ctype.h>
38 #include <signal.h>
39 #include <assert.h>
40
41 #include <vlc_interface.h>
42 #include <vlc_aout.h>
43 #include <vlc_vout.h>
44 #include <vlc_osd.h>
45 #include <vlc_playlist.h>
46 #include <vlc_keys.h>
47
48 #ifdef HAVE_UNISTD_H
49 #    include <unistd.h>
50 #endif
51
52 #ifdef HAVE_SYS_TIME_H
53 #    include <sys/time.h>
54 #endif
55 #include <sys/types.h>
56
57 #include <vlc_network.h>
58 #include <vlc_url.h>
59
60 #include <vlc_charset.h>
61
62 #if defined(PF_UNIX) && !defined(PF_LOCAL)
63 #    define PF_LOCAL PF_UNIX
64 #endif
65
66 #if defined(AF_LOCAL) && ! defined(WIN32)
67 #    include <sys/un.h>
68 #endif
69
70 #define MAX_LINE_LENGTH 1024
71 #define STATUS_CHANGE "status change: "
72
73 /* input_state_e from <vlc_input.h> */
74 static const char *ppsz_input_state[] = {
75     [INIT_S] = N_("Initializing"),
76     [OPENING_S] = N_("Opening"),
77     [PLAYING_S] = N_("Play"),
78     [PAUSE_S] = N_("Pause"),
79     [END_S] = N_("End"),
80     [ERROR_S] = N_("Error"),
81 };
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  Activate     ( vlc_object_t * );
87 static void Deactivate   ( vlc_object_t * );
88 static void Run          ( intf_thread_t * );
89
90 static void Help         ( intf_thread_t *, bool );
91 static void RegisterCallbacks( intf_thread_t * );
92
93 static bool ReadCommand( intf_thread_t *, char *, int * );
94
95 static input_item_t *parse_MRL( intf_thread_t *, char * );
96
97 static int  Input        ( vlc_object_t *, char const *,
98                            vlc_value_t, vlc_value_t, void * );
99 static int  Playlist     ( vlc_object_t *, char const *,
100                            vlc_value_t, vlc_value_t, void * );
101 static int  Quit         ( vlc_object_t *, char const *,
102                            vlc_value_t, vlc_value_t, void * );
103 static int  Intf         ( vlc_object_t *, char const *,
104                            vlc_value_t, vlc_value_t, void * );
105 static int  Volume       ( vlc_object_t *, char const *,
106                            vlc_value_t, vlc_value_t, void * );
107 static int  VolumeMove   ( vlc_object_t *, char const *,
108                            vlc_value_t, vlc_value_t, void * );
109 static int  VideoConfig  ( vlc_object_t *, char const *,
110                            vlc_value_t, vlc_value_t, void * );
111 static int  AudioConfig  ( vlc_object_t *, char const *,
112                            vlc_value_t, vlc_value_t, void * );
113 static int  Menu         ( vlc_object_t *, char const *,
114                            vlc_value_t, vlc_value_t, void * );
115 static int  Statistics   ( vlc_object_t *, char const *,
116                            vlc_value_t, vlc_value_t, void * );
117
118 static int updateStatistics( intf_thread_t *, input_item_t *);
119
120 /* Status Callbacks */
121 static int VolumeChanged( vlc_object_t *, char const *,
122                           vlc_value_t, vlc_value_t, void * );
123 static int InputEvent( vlc_object_t *, char const *,
124                        vlc_value_t, vlc_value_t, void * );
125
126 struct intf_sys_t
127 {
128     int *pi_socket_listen;
129     int i_socket;
130     char *psz_unix_path;
131
132     /* status changes */
133     vlc_mutex_t       status_lock;
134     playlist_status_t i_last_state;
135     playlist_t        *p_playlist;
136     bool              b_input_buffering;
137
138 #ifdef WIN32
139     HANDLE hConsoleIn;
140     bool b_quiet;
141 #endif
142 };
143
144 #define msg_rc( ... ) __msg_rc( p_intf, __VA_ARGS__ )
145
146 LIBVLC_FORMAT(2, 3)
147 static void __msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
148 {
149     va_list args;
150     char fmt_eol[strlen (psz_fmt) + 3];
151
152     snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
153     va_start( args, psz_fmt );
154
155     if( p_intf->p_sys->i_socket == -1 )
156         utf8_vfprintf( stdout, fmt_eol, args );
157     else
158         net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, fmt_eol, args );
159     va_end( args );
160 }
161
162 /*****************************************************************************
163  * Module descriptor
164  *****************************************************************************/
165 #define POS_TEXT N_("Show stream position")
166 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
167                         "stream from time to time." )
168
169 #define TTY_TEXT N_("Fake TTY")
170 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
171
172 #define UNIX_TEXT N_("UNIX socket command input")
173 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
174                          "stdin." )
175
176 #define HOST_TEXT N_("TCP command input")
177 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
178             "You can set the address and port the interface will bind to." )
179
180 #ifdef WIN32
181 #define QUIET_TEXT N_("Do not open a DOS command box interface")
182 #define QUIET_LONGTEXT N_( \
183     "By default the rc interface plugin will start a DOS command box. " \
184     "Enabling the quiet mode will not bring this command box but can also " \
185     "be pretty annoying when you want to stop VLC and no video window is " \
186     "open." )
187 #endif
188
189 vlc_module_begin ()
190     set_shortname( N_("RC"))
191     set_category( CAT_INTERFACE )
192     set_subcategory( SUBCAT_INTERFACE_MAIN )
193     set_description( N_("Remote control interface") )
194     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, true )
195
196 #ifdef WIN32
197     add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, false )
198 #else
199 #if defined (HAVE_ISATTY)
200     add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, true )
201 #endif
202     add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, true )
203 #endif
204     add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, true )
205
206     set_capability( "interface", 20 )
207
208     set_callbacks( Activate, Deactivate )
209 vlc_module_end ()
210
211 /*****************************************************************************
212  * Activate: initialize and create stuff
213  *****************************************************************************/
214 static int Activate( vlc_object_t *p_this )
215 {
216     intf_thread_t *p_intf = (intf_thread_t*)p_this;
217     char *psz_host, *psz_unix_path = NULL;
218     int  *pi_socket = NULL;
219
220 #ifndef WIN32
221 #if defined(HAVE_ISATTY)
222     /* Check that stdin is a TTY */
223     if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
224     {
225         msg_Warn( p_intf, "fd 0 is not a TTY" );
226         return VLC_EGENERIC;
227     }
228 #endif
229
230     psz_unix_path = config_GetPsz( p_intf, "rc-unix" );
231     if( psz_unix_path )
232     {
233         int i_socket;
234
235 #ifndef AF_LOCAL
236         msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
237         free( psz_unix_path );
238         return VLC_EGENERIC;
239 #else
240         struct sockaddr_un addr;
241
242         memset( &addr, 0, sizeof(struct sockaddr_un) );
243
244         msg_Dbg( p_intf, "trying UNIX socket" );
245
246         if( (i_socket = socket( PF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
247         {
248             msg_Warn( p_intf, "can't open socket: %m" );
249             free( psz_unix_path );
250             return VLC_EGENERIC;
251         }
252
253         addr.sun_family = AF_LOCAL;
254         strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
255         addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
256
257         if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
258          && (errno == EADDRINUSE)
259          && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
260          && (errno == ECONNREFUSED))
261         {
262             msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
263             unlink (psz_unix_path);
264
265             if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
266             {
267                 msg_Err (p_intf, "cannot bind UNIX socket at %s: %m",
268                          psz_unix_path);
269                 free (psz_unix_path);
270                 net_Close (i_socket);
271                 return VLC_EGENERIC;
272             }
273         }
274
275         if( listen( i_socket, 1 ) )
276         {
277             msg_Warn( p_intf, "can't listen on socket: %m");
278             free( psz_unix_path );
279             net_Close( i_socket );
280             return VLC_EGENERIC;
281         }
282
283         /* FIXME: we need a core function to merge listening sockets sets */
284         pi_socket = calloc( 2, sizeof( int ) );
285         if( pi_socket == NULL )
286         {
287             free( psz_unix_path );
288             net_Close( i_socket );
289             return VLC_ENOMEM;
290         }
291         pi_socket[0] = i_socket;
292         pi_socket[1] = -1;
293 #endif /* AF_LOCAL */
294     }
295 #endif /* !WIN32 */
296
297     if( ( pi_socket == NULL ) &&
298         ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
299     {
300         vlc_url_t url;
301
302         vlc_UrlParse( &url, psz_host, 0 );
303
304         msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
305
306         pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
307         if( pi_socket == NULL )
308         {
309             msg_Warn( p_intf, "can't listen to %s port %i",
310                       url.psz_host, url.i_port );
311             vlc_UrlClean( &url );
312             free( psz_host );
313             return VLC_EGENERIC;
314         }
315
316         vlc_UrlClean( &url );
317         free( psz_host );
318     }
319
320     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
321     if( !p_intf->p_sys )
322         return VLC_ENOMEM;
323
324     p_intf->p_sys->pi_socket_listen = pi_socket;
325     p_intf->p_sys->i_socket = -1;
326     p_intf->p_sys->psz_unix_path = psz_unix_path;
327     vlc_mutex_init( &p_intf->p_sys->status_lock );
328     p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
329     p_intf->p_sys->b_input_buffering = false;
330
331     /* Non-buffered stdout */
332     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
333
334     p_intf->pf_run = Run;
335
336 #ifdef WIN32
337     p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
338     if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
339 #else
340     CONSOLE_INTRO_MSG;
341 #endif
342
343     msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * Deactivate: uninitialize and cleanup
349  *****************************************************************************/
350 static void Deactivate( vlc_object_t *p_this )
351 {
352     intf_thread_t *p_intf = (intf_thread_t*)p_this;
353
354     net_ListenClose( p_intf->p_sys->pi_socket_listen );
355     if( p_intf->p_sys->i_socket != -1 )
356         net_Close( p_intf->p_sys->i_socket );
357     if( p_intf->p_sys->psz_unix_path != NULL )
358     {
359 #if defined(AF_LOCAL) && !defined(WIN32)
360         unlink( p_intf->p_sys->psz_unix_path );
361 #endif
362         free( p_intf->p_sys->psz_unix_path );
363     }
364     vlc_mutex_destroy( &p_intf->p_sys->status_lock );
365     free( p_intf->p_sys );
366 }
367
368 /*****************************************************************************
369  * RegisterCallbacks: Register callbacks to dynamic variables
370  *****************************************************************************/
371 static void RegisterCallbacks( intf_thread_t *p_intf )
372 {
373     /* Register commands that will be cleaned up upon object destruction */
374 #define ADD( name, type, target )                                   \
375     var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
376     var_AddCallback( p_intf, name, target, NULL );
377     ADD( "quit", VOID, Quit )
378     ADD( "intf", STRING, Intf )
379
380     ADD( "add", STRING, Playlist )
381     ADD( "repeat", STRING, Playlist )
382     ADD( "loop", STRING, Playlist )
383     ADD( "random", STRING, Playlist )
384     ADD( "enqueue", STRING, Playlist )
385     ADD( "playlist", VOID, Playlist )
386     ADD( "sort", VOID, Playlist )
387     ADD( "play", VOID, Playlist )
388     ADD( "stop", VOID, Playlist )
389     ADD( "clear", VOID, Playlist )
390     ADD( "prev", VOID, Playlist )
391     ADD( "next", VOID, Playlist )
392     ADD( "goto", INTEGER, Playlist )
393     ADD( "status", INTEGER, Playlist )
394
395     /* OSD menu commands */
396     ADD(  "menu", STRING, Menu )
397
398     /* DVD commands */
399     ADD( "pause", VOID, Input )
400     ADD( "seek", INTEGER, Input )
401     ADD( "title", STRING, Input )
402     ADD( "title_n", VOID, Input )
403     ADD( "title_p", VOID, Input )
404     ADD( "chapter", STRING, Input )
405     ADD( "chapter_n", VOID, Input )
406     ADD( "chapter_p", VOID, Input )
407
408     ADD( "fastforward", VOID, Input )
409     ADD( "rewind", VOID, Input )
410     ADD( "faster", VOID, Input )
411     ADD( "slower", VOID, Input )
412     ADD( "normal", VOID, Input )
413     ADD( "frame", VOID, Input )
414
415     ADD( "atrack", STRING, Input )
416     ADD( "vtrack", STRING, Input )
417     ADD( "strack", STRING, Input )
418
419     /* video commands */
420     ADD( "vratio", STRING, VideoConfig )
421     ADD( "vcrop", STRING, VideoConfig )
422     ADD( "vzoom", STRING, VideoConfig )
423     ADD( "snapshot", VOID, VideoConfig )
424
425     /* audio commands */
426     ADD( "volume", STRING, Volume )
427     ADD( "volup", STRING, VolumeMove )
428     ADD( "voldown", STRING, VolumeMove )
429     ADD( "adev", STRING, AudioConfig )
430     ADD( "achan", STRING, AudioConfig )
431
432     /* misc menu commands */
433     ADD( "stats", BOOL, Statistics )
434
435 #undef ADD
436 }
437
438 /*****************************************************************************
439  * Run: rc thread
440  *****************************************************************************
441  * This part of the interface is in a separate thread so that we can call
442  * exec() from within it without annoying the rest of the program.
443  *****************************************************************************/
444 static void Run( intf_thread_t *p_intf )
445 {
446     input_thread_t * p_input = NULL;
447     playlist_t *     p_playlist = pl_Hold( p_intf );
448
449     char p_buffer[ MAX_LINE_LENGTH + 1 ];
450     bool b_showpos = config_GetInt( p_intf, "rc-show-pos" );
451     bool b_longhelp = false;
452
453     int  i_size = 0;
454     int  i_oldpos = 0;
455     int  i_newpos;
456     int  canc = vlc_savecancel();
457
458     p_buffer[0] = 0;
459
460     /* Register commands that will be cleaned up upon object destruction */
461     p_intf->p_sys->p_playlist = p_playlist;
462     RegisterCallbacks( p_intf );
463
464     /* status callbacks */
465     /* Listen to audio volume updates */
466     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
467
468 #ifdef WIN32
469     /* Get the file descriptor of the console input */
470     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
471     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
472     {
473         msg_Err( p_intf, "couldn't find user input handle" );
474         vlc_object_kill( p_intf );
475     }
476 #endif
477
478     while( vlc_object_alive( p_intf ) )
479     {
480         char *psz_cmd, *psz_arg;
481         bool b_complete;
482
483         if( p_intf->p_sys->pi_socket_listen != NULL &&
484             p_intf->p_sys->i_socket == -1 )
485         {
486             p_intf->p_sys->i_socket =
487                 net_Accept( p_intf, p_intf->p_sys->pi_socket_listen,
488                             INTF_IDLE_SLEEP );
489             if( p_intf->p_sys->i_socket == -1 ) continue;
490         }
491
492         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
493
494         /* Manage the input part */
495         if( p_input == NULL )
496         {
497             p_input = playlist_CurrentInput( p_playlist );
498             /* New input has been registered */
499             if( p_input )
500             {
501                 if( !p_input->b_dead || vlc_object_alive (p_input) )
502                 {
503                     char *psz_uri =
504                             input_item_GetURI( input_GetItem( p_input ) );
505                     msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
506                     free( psz_uri );
507                     msg_rc( STATUS_CHANGE "( audio volume: %d )",
508                             config_GetInt( p_intf, "volume" ));
509                 }
510                 var_AddCallback( p_input, "intf-event", InputEvent, p_intf );
511             }
512         }
513         else if( p_input->b_dead )
514         {
515             var_DelCallback( p_input, "intf-event", InputEvent, p_intf );
516             vlc_object_release( p_input );
517             p_input = NULL;
518
519             if( p_playlist )
520             {
521                 PL_LOCK;
522                 p_intf->p_sys->i_last_state = (int) PLAYLIST_STOPPED;
523                 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
524                 PL_UNLOCK;
525             }
526         }
527
528         if( (p_input != NULL) && !p_input->b_dead && vlc_object_alive (p_input) &&
529             (p_playlist != NULL) )
530         {
531             PL_LOCK;
532             int status = playlist_Status( p_playlist );
533             if( p_intf->p_sys->i_last_state != status )
534             {
535                 if( status == PLAYLIST_STOPPED )
536                 {
537                     p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
538                     msg_rc( STATUS_CHANGE "( stop state: 5 )" );
539                 }
540                 else if( status == PLAYLIST_RUNNING )
541                 {
542                     p_intf->p_sys->i_last_state = PLAYLIST_RUNNING;
543                     msg_rc( STATUS_CHANGE "( play state: 3 )" );
544                 }
545                 else if( status == PLAYLIST_PAUSED )
546                 {
547                     p_intf->p_sys->i_last_state = PLAYLIST_PAUSED;
548                     msg_rc( STATUS_CHANGE "( pause state: 4 )" );
549                 }
550             }
551             PL_UNLOCK;
552         }
553
554         if( p_input && b_showpos )
555         {
556             i_newpos = 100 * var_GetFloat( p_input, "position" );
557             if( i_oldpos != i_newpos )
558             {
559                 i_oldpos = i_newpos;
560                 msg_rc( "pos: %d%%", i_newpos );
561             }
562         }
563
564         /* Is there something to do? */
565         if( !b_complete ) continue;
566
567         /* Skip heading spaces */
568         psz_cmd = p_buffer;
569         while( *psz_cmd == ' ' )
570         {
571             psz_cmd++;
572         }
573
574         /* Split psz_cmd at the first space and make sure that
575          * psz_arg is valid */
576         psz_arg = strchr( psz_cmd, ' ' );
577         if( psz_arg )
578         {
579             *psz_arg++ = 0;
580             while( *psz_arg == ' ' )
581             {
582                 psz_arg++;
583             }
584         }
585         else
586         {
587             psz_arg = (char*)"";
588         }
589
590         /* module specfic commands: @<module name> <command> <args...> */
591         if( *psz_cmd == '@' && *psz_arg )
592         {
593             /* Parse miscellaneous commands */
594             char *psz_alias = psz_cmd + 1;
595             char *psz_mycmd = strdup( psz_arg );
596             char *psz_myarg = strchr( psz_mycmd, ' ' );
597             char *psz_msg;
598
599             if( !psz_myarg )
600             {
601                 msg_rc( "Not enough parameters." );
602             }
603             else
604             {
605                 *psz_myarg = '\0';
606                 psz_myarg ++;
607
608                 var_Command( p_intf, psz_alias, psz_mycmd, psz_myarg,
609                              &psz_msg );
610
611                 if( psz_msg )
612                 {
613                     msg_rc( "%s", psz_msg );
614                     free( psz_msg );
615                 }
616             }
617             free( psz_mycmd );
618         }
619         /* If the user typed a registered local command, try it */
620         else if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
621         {
622             vlc_value_t val;
623             int i_ret;
624
625             val.psz_string = psz_arg;
626             i_ret = var_Set( p_intf, psz_cmd, val );
627             msg_rc( "%s: returned %i (%s)",
628                     psz_cmd, i_ret, vlc_error( i_ret ) );
629         }
630         /* Or maybe it's a global command */
631         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
632         {
633             vlc_value_t val;
634             int i_ret;
635
636             val.psz_string = psz_arg;
637             /* FIXME: it's a global command, but we should pass the
638              * local object as an argument, not p_intf->p_libvlc. */
639             i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
640             if( i_ret != 0 )
641             {
642                 msg_rc( "%s: returned %i (%s)",
643                          psz_cmd, i_ret, vlc_error( i_ret ) );
644             }
645         }
646         else if( !strcmp( psz_cmd, "logout" ) )
647         {
648             /* Close connection */
649             if( p_intf->p_sys->i_socket != -1 )
650             {
651                 net_Close( p_intf->p_sys->i_socket );
652             }
653             p_intf->p_sys->i_socket = -1;
654         }
655         else if( !strcmp( psz_cmd, "info" ) )
656         {
657             if( p_input )
658             {
659                 int i, j;
660                 vlc_mutex_lock( &input_GetItem(p_input)->lock );
661                 for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
662                 {
663                     info_category_t *p_category = input_GetItem(p_input)
664                                                         ->pp_categories[i];
665
666                     msg_rc( "+----[ %s ]", p_category->psz_name );
667                     msg_rc( "| " );
668                     for ( j = 0; j < p_category->i_infos; j++ )
669                     {
670                         info_t *p_info = p_category->pp_infos[j];
671                         msg_rc( "| %s: %s", p_info->psz_name,
672                                 p_info->psz_value );
673                     }
674                     msg_rc( "| " );
675                 }
676                 msg_rc( "+----[ end of stream info ]" );
677                 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
678             }
679             else
680             {
681                 msg_rc( "no input" );
682             }
683         }
684         else if( !strcmp( psz_cmd, "is_playing" ) )
685         {
686             if( ! p_input )
687             {
688                 msg_rc( "0" );
689             }
690             else
691             {
692                 msg_rc( "1" );
693             }
694         }
695         else if( !strcmp( psz_cmd, "get_time" ) )
696         {
697             if( ! p_input )
698             {
699                 msg_rc("0");
700             }
701             else
702             {
703                 vlc_value_t time;
704                 var_Get( p_input, "time", &time );
705                 msg_rc( "%"PRIu64, time.i_time / 1000000);
706             }
707         }
708         else if( !strcmp( psz_cmd, "get_length" ) )
709         {
710             if( ! p_input )
711             {
712                 msg_rc("0");
713             }
714             else
715             {
716                 vlc_value_t time;
717                 var_Get( p_input, "length", &time );
718                 msg_rc( "%"PRIu64, time.i_time / 1000000);
719             }
720         }
721         else if( !strcmp( psz_cmd, "get_title" ) )
722         {
723             if( ! p_input )
724             {
725                 msg_rc("%s", "");
726             }
727             else
728             {
729                 msg_rc( "%s", input_GetItem(p_input)->psz_name );
730             }
731         }
732         else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
733                  || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
734         {
735             if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "H", 1 ) )
736                  b_longhelp = true;
737             else b_longhelp = false;
738
739             Help( p_intf, b_longhelp );
740         }
741         else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
742         {
743             var_SetInteger( p_intf->p_libvlc, "key-pressed",
744                             config_GetInt( p_intf, psz_arg ) );
745         }
746         else switch( psz_cmd[0] )
747         {
748         case 'f':
749         case 'F':
750             if( p_input )
751             {
752                 vout_thread_t *p_vout;
753                 p_vout = input_GetVout( p_input );
754
755                 if( p_vout )
756                 {
757                     vlc_value_t val;
758                     bool b_update = false;
759                     var_Get( p_vout, "fullscreen", &val );
760                     val.b_bool = !val.b_bool;
761                     if( !strncmp( psz_arg, "on", 2 )
762                         && ( val.b_bool == true ) )
763                     {
764                         b_update = true;
765                         val.b_bool = true;
766                     }
767                     else if( !strncmp( psz_arg, "off", 3 )
768                              && ( val.b_bool == false ) )
769                     {
770                         b_update = true;
771                         val.b_bool = false;
772                     }
773                     else if( strncmp( psz_arg, "off", 3 )
774                              && strncmp( psz_arg, "on", 2 ) )
775                         b_update = true;
776                     if( b_update ) var_Set( p_vout, "fullscreen", val );
777                     vlc_object_release( p_vout );
778                 }
779             }
780             break;
781
782         case 's':
783         case 'S':
784             ;
785             break;
786
787         case '\0':
788             /* Ignore empty lines */
789             break;
790
791         default:
792             msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
793             break;
794         }
795
796         /* Command processed */
797         i_size = 0; p_buffer[0] = 0;
798     }
799
800     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
801     msg_rc( STATUS_CHANGE "( quit )" );
802
803     if( p_input )
804     {
805         var_DelCallback( p_input, "intf-event", InputEvent, p_intf );
806         vlc_object_release( p_input );
807     }
808
809     pl_Release( p_intf );
810
811     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
812     vlc_restorecancel( canc );
813 }
814
815 static void Help( intf_thread_t *p_intf, bool b_longhelp)
816 {
817     msg_rc("%s", _("+----[ Remote control commands ]"));
818     msg_rc(  "| ");
819     msg_rc("%s", _("| add XYZ  . . . . . . . . . . . . add XYZ to playlist"));
820     msg_rc("%s", _("| enqueue XYZ  . . . . . . . . . queue XYZ to playlist"));
821     msg_rc("%s", _("| playlist . . . . .  show items currently in playlist"));
822     msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
823     msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
824     msg_rc("%s", _("| next . . . . . . . . . . . . . .  next playlist item"));
825     msg_rc("%s", _("| prev . . . . . . . . . . . .  previous playlist item"));
826     msg_rc("%s", _("| goto . . . . . . . . . . . . . .  goto item at index"));
827     msg_rc("%s", _("| repeat [on|off] . . . .  toggle playlist item repeat"));
828     msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
829     msg_rc("%s", _("| random [on|off] . . . . . . .  toggle random jumping"));
830     msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
831     msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
832     msg_rc("%s", _("| title [X]  . . . . . . set/get title in current item"));
833     msg_rc("%s", _("| title_n  . . . . . . . .  next title in current item"));
834     msg_rc("%s", _("| title_p  . . . . . .  previous title in current item"));
835     msg_rc("%s", _("| chapter [X]  . . . . set/get chapter in current item"));
836     msg_rc("%s", _("| chapter_n  . . . . . .  next chapter in current item"));
837     msg_rc("%s", _("| chapter_p  . . . .  previous chapter in current item"));
838     msg_rc(  "| ");
839     msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
840     msg_rc("%s", _("| pause  . . . . . . . . . . . . . . . .  toggle pause"));
841     msg_rc("%s", _("| fastforward  . . . . . . . .  .  set to maximum rate"));
842     msg_rc("%s", _("| rewind  . . . . . . . . . . . .  set to minimum rate"));
843     msg_rc("%s", _("| faster . . . . . . . . . .  faster playing of stream"));
844     msg_rc("%s", _("| slower . . . . . . . . . .  slower playing of stream"));
845     msg_rc("%s", _("| normal . . . . . . . . . .  normal playing of stream"));
846     msg_rc("%s", _("| frame. . . . . . . . . .  play frame by frame"));
847     msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
848     msg_rc("%s", _("| info . . . . .  information about the current stream"));
849     msg_rc("%s", _("| stats  . . . . . . . .  show statistical information"));
850     msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
851     msg_rc("%s", _("| is_playing . . . .  1 if a stream plays, 0 otherwise"));
852     msg_rc("%s", _("| get_title . . . . .  the title of the current stream"));
853     msg_rc("%s", _("| get_length . . . .  the length of the current stream"));
854     msg_rc(  "| ");
855     msg_rc("%s", _("| volume [X] . . . . . . . . . .  set/get audio volume"));
856     msg_rc("%s", _("| volup [X]  . . . . . . .  raise audio volume X steps"));
857     msg_rc("%s", _("| voldown [X]  . . . . . .  lower audio volume X steps"));
858     msg_rc("%s", _("| adev [X] . . . . . . . . . . .  set/get audio device"));
859     msg_rc("%s", _("| achan [X]. . . . . . . . . .  set/get audio channels"));
860     msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
861     msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
862     msg_rc("%s", _("| vratio [X]  . . . . . . . set/get video aspect ratio"));
863     msg_rc("%s", _("| vcrop [X]  . . . . . . . . . . .  set/get video crop"));
864     msg_rc("%s", _("| vzoom [X]  . . . . . . . . . . .  set/get video zoom"));
865     msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
866     msg_rc("%s", _("| strack [X] . . . . . . . . . set/get subtitles track"));
867     msg_rc("%s", _("| key [hotkey name] . . . . . .  simulate hotkey press"));
868     msg_rc("%s", _("| menu . . [on|off|up|down|left|right|select] use menu"));
869     msg_rc(  "| ");
870
871     if (b_longhelp)
872     {
873         msg_rc("%s", _("| @name marq-marquee  STRING  . . overlay STRING in video"));
874         msg_rc("%s", _("| @name marq-x X . . . . . . . . . . . .offset from left"));
875         msg_rc("%s", _("| @name marq-y Y . . . . . . . . . . . . offset from top"));
876         msg_rc("%s", _("| @name marq-position #. . .  .relative position control"));
877         msg_rc("%s", _("| @name marq-color # . . . . . . . . . . font color, RGB"));
878         msg_rc("%s", _("| @name marq-opacity # . . . . . . . . . . . . . opacity"));
879         msg_rc("%s", _("| @name marq-timeout T. . . . . . . . . . timeout, in ms"));
880         msg_rc("%s", _("| @name marq-size # . . . . . . . . font size, in pixels"));
881         msg_rc(  "| ");
882         msg_rc("%s", _("| @name logo-file STRING . . .the overlay file path/name"));
883         msg_rc("%s", _("| @name logo-x X . . . . . . . . . . . .offset from left"));
884         msg_rc("%s", _("| @name logo-y Y . . . . . . . . . . . . offset from top"));
885         msg_rc("%s", _("| @name logo-position #. . . . . . . . relative position"));
886         msg_rc("%s", _("| @name logo-transparency #. . . . . . . . .transparency"));
887         msg_rc(  "| ");
888         msg_rc("%s", _("| @name mosaic-alpha # . . . . . . . . . . . . . . alpha"));
889         msg_rc("%s", _("| @name mosaic-height #. . . . . . . . . . . . . .height"));
890         msg_rc("%s", _("| @name mosaic-width # . . . . . . . . . . . . . . width"));
891         msg_rc("%s", _("| @name mosaic-xoffset # . . . .top left corner position"));
892         msg_rc("%s", _("| @name mosaic-yoffset # . . . .top left corner position"));
893         msg_rc("%s", _("| @name mosaic-offsets x,y(,x,y)*. . . . list of offsets"));
894         msg_rc("%s", _("| @name mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
895         msg_rc("%s", _("| @name mosaic-vborder # . . . . . . . . vertical border"));
896         msg_rc("%s", _("| @name mosaic-hborder # . . . . . . . horizontal border"));
897         msg_rc("%s", _("| @name mosaic-position {0=auto,1=fixed} . . . .position"));
898         msg_rc("%s", _("| @name mosaic-rows #. . . . . . . . . . .number of rows"));
899         msg_rc("%s", _("| @name mosaic-cols #. . . . . . . . . . .number of cols"));
900         msg_rc("%s", _("| @name mosaic-order id(,id)* . . . . order of pictures "));
901         msg_rc("%s", _("| @name mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
902         msg_rc(  "| ");
903     }
904     msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
905     msg_rc("%s", _("| longhelp . . . . . . . . . . . a longer help message"));
906     msg_rc("%s", _("| logout . . . . . . .  exit (if in socket connection)"));
907     msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . .  quit vlc"));
908     msg_rc(  "| ");
909     msg_rc("%s", _("+----[ end of help ]"));
910 }
911
912 /********************************************************************
913  * Status callback routines
914  ********************************************************************/
915 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
916     vlc_value_t oldval, vlc_value_t newval, void *p_data )
917 {
918     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
919     intf_thread_t *p_intf = (intf_thread_t*)p_data;
920
921     vlc_mutex_lock( &p_intf->p_sys->status_lock );
922     msg_rc( STATUS_CHANGE "( audio volume: %d )",
923             config_GetInt( p_this, "volume") );
924     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
925     return VLC_SUCCESS;
926 }
927
928 static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
929 {
930     playlist_t *p_playlist = p_intf->p_sys->p_playlist;
931
932     PL_LOCK;
933     const int i_status = playlist_Status( p_playlist );
934     PL_UNLOCK;
935
936     /* */
937     char *psz_cmd;
938     switch( i_status )
939     {
940     case PLAYLIST_STOPPED:
941         psz_cmd = "stop";
942         break;
943     case PLAYLIST_RUNNING:
944         psz_cmd = "play";
945         break;
946     case PLAYLIST_PAUSED:
947         psz_cmd = "pause";
948         break;
949     default:
950         psz_cmd = "";
951         break;
952     }
953
954     /* */
955     const int i_state = var_GetInteger( p_input, "state" );
956
957     vlc_mutex_lock( &p_intf->p_sys->status_lock );
958     msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
959             i_state, ppsz_input_state[i_state] );
960     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
961 }
962 static void RateChanged( intf_thread_t *p_intf,
963                          input_thread_t *p_input )
964 {
965     vlc_mutex_lock( &p_intf->p_sys->status_lock );
966     msg_rc( STATUS_CHANGE "( new rate: %d )",
967             var_GetInteger( p_input, "rate" ) );
968     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
969 }
970 static void PositionChanged( intf_thread_t *p_intf,
971                              input_thread_t *p_input )
972 {
973     vlc_mutex_lock( &p_intf->p_sys->status_lock );
974     if( p_intf->p_sys->b_input_buffering )
975         msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
976                 (var_GetTime( p_input, "time" )/1000000) );
977     p_intf->p_sys->b_input_buffering = false;
978     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
979 }
980 static void CacheChanged( intf_thread_t *p_intf )
981 {
982     vlc_mutex_lock( &p_intf->p_sys->status_lock );
983     p_intf->p_sys->b_input_buffering = true;
984     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
985 }
986
987 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
988                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
989 {
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             int i_rate = var_GetInteger( p_input, "rate" );
1062             i_rate = (i_rate < 0) ? -i_rate : i_rate * 2;
1063             var_SetInteger( p_input, "rate", i_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             int i_rate = var_GetInteger( p_input, "rate" );
1076             i_rate = (i_rate > 0) ? -i_rate : i_rate / 2;
1077             var_SetInteger( p_input, "rate", i_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;
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         i_error = var_SetBool( p_vout, psz_variable, true );
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         i_error = VLC_SUCCESS;
1709     }
1710     vlc_object_release( p_vout );
1711     return i_error;
1712 }
1713
1714 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1715                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1716 {
1717     VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1718     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1719     input_thread_t *p_input =
1720         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1721     aout_instance_t * p_aout;
1722     const char * psz_variable;
1723     vlc_value_t val_name;
1724     int i_error;
1725
1726     if( !p_input )
1727         return VLC_ENOOBJ;
1728
1729     int state = var_GetInteger( p_input, "state" );
1730     if( state == PAUSE_S )
1731     {
1732         msg_rc( "%s", _("Type 'menu select' or 'pause' to continue.") );
1733         return VLC_EGENERIC;
1734     }
1735
1736     p_aout = input_GetAout( p_input );
1737     vlc_object_release( p_input );
1738     if ( p_aout == NULL )
1739          return VLC_ENOOBJ;
1740
1741     if ( !strcmp( psz_cmd, "adev" ) )
1742     {
1743         psz_variable = "audio-device";
1744     }
1745     else
1746     {
1747         psz_variable = "audio-channels";
1748     }
1749
1750     /* Get the descriptive name of the variable */
1751     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1752                  &val_name, NULL );
1753     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1754
1755     if ( !*newval.psz_string )
1756     {
1757         /* Retrieve all registered ***. */
1758         vlc_value_t val, text;
1759         int i, i_value;
1760
1761         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1762         {
1763             vlc_object_release( (vlc_object_t *)p_aout );
1764             return VLC_EGENERIC;
1765         }
1766         i_value = val.i_int;
1767
1768         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1769                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1770         {
1771             vlc_object_release( (vlc_object_t *)p_aout );
1772             return VLC_EGENERIC;
1773         }
1774
1775         msg_rc( "+----[ %s ]", val_name.psz_string );
1776         for ( i = 0; i < val.p_list->i_count; i++ )
1777         {
1778             if ( i_value == val.p_list->p_values[i].i_int )
1779                 msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1780                         text.p_list->p_values[i].psz_string );
1781             else
1782                 msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1783                         text.p_list->p_values[i].psz_string );
1784         }
1785         var_FreeList( &val, &text );
1786         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1787
1788         free( val_name.psz_string );
1789         i_error = VLC_SUCCESS;
1790     }
1791     else
1792     {
1793         vlc_value_t val;
1794         val.i_int = atoi( newval.psz_string );
1795
1796         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1797     }
1798     vlc_object_release( (vlc_object_t *)p_aout );
1799
1800     return i_error;
1801 }
1802
1803 /* OSD menu commands */
1804 static int Menu( vlc_object_t *p_this, char const *psz_cmd,
1805     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1806 {
1807     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1808     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1809     playlist_t    *p_playlist = p_intf->p_sys->p_playlist;
1810     int i_error = VLC_SUCCESS;
1811     vlc_value_t val;
1812
1813     if ( !*newval.psz_string )
1814     {
1815         msg_rc( "%s", _("Please provide one of the following parameters:") );
1816         msg_rc( "[on|off|up|down|left|right|select]" );
1817         return VLC_EGENERIC;
1818     }
1819
1820     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1821
1822     if( p_input )
1823     {
1824         var_Get( p_input, "state", &val );
1825         vlc_object_release( p_input );
1826
1827         if( ( val.i_int == PAUSE_S ) &&
1828             ( strcmp( newval.psz_string, "select" ) != 0 ) )
1829         {
1830             msg_rc( "%s", _("Type 'menu select' or 'pause' to continue.") );
1831             return VLC_EGENERIC;
1832         }
1833     }
1834
1835     val.psz_string = strdup( newval.psz_string );
1836     if( !val.psz_string )
1837         return VLC_ENOMEM;
1838     if( !strcmp( val.psz_string, "on" ) || !strcmp( val.psz_string, "show" ))
1839         osd_MenuShow( p_this );
1840     else if( !strcmp( val.psz_string, "off" )
1841           || !strcmp( val.psz_string, "hide" ) )
1842         osd_MenuHide( p_this );
1843     else if( !strcmp( val.psz_string, "up" ) )
1844         osd_MenuUp( p_this );
1845     else if( !strcmp( val.psz_string, "down" ) )
1846         osd_MenuDown( p_this );
1847     else if( !strcmp( val.psz_string, "left" ) )
1848         osd_MenuPrev( p_this );
1849     else if( !strcmp( val.psz_string, "right" ) )
1850         osd_MenuNext( p_this );
1851     else if( !strcmp( val.psz_string, "select" ) )
1852         osd_MenuActivate( p_this );
1853     else
1854     {
1855         msg_rc( "%s", _("Please provide one of the following parameters:") );
1856         msg_rc( "[on|off|up|down|left|right|select]" );
1857         i_error = VLC_EGENERIC;
1858     }
1859
1860     free( val.psz_string );
1861     return i_error;
1862 }
1863
1864 static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
1865     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1866 {
1867     VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1868     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1869     input_thread_t *p_input =
1870         playlist_CurrentInput( p_intf->p_sys->p_playlist );
1871
1872     if( !p_input )
1873         return VLC_ENOOBJ;
1874
1875     if( !strcmp( psz_cmd, "stats" ) )
1876     {
1877         vlc_mutex_lock( &input_GetItem(p_input)->lock );
1878         updateStatistics( p_intf, input_GetItem(p_input) );
1879         vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1880     }
1881     /*
1882      * sanity check
1883      */
1884     else
1885     {
1886         msg_rc("%s", _("Unknown command!") );
1887     }
1888
1889     vlc_object_release( p_input );
1890     return VLC_SUCCESS;
1891 }
1892
1893 static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
1894 {
1895     if( !p_item ) return VLC_EGENERIC;
1896
1897     vlc_mutex_lock( &p_item->p_stats->lock );
1898     msg_rc( "+----[ begin of statistical info ]" );
1899
1900     /* Input */
1901     msg_rc("%s", _("+-[Incoming]"));
1902     msg_rc(_("| input bytes read : %8.0f kB"),
1903             (float)(p_item->p_stats->i_read_bytes)/1000 );
1904     msg_rc(_("| input bitrate    :   %6.0f kb/s"),
1905             (float)(p_item->p_stats->f_input_bitrate)*8000 );
1906     msg_rc(_("| demux bytes read : %8.0f kB"),
1907             (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
1908     msg_rc(_("| demux bitrate    :   %6.0f kb/s"),
1909             (float)(p_item->p_stats->f_demux_bitrate)*8000 );
1910     msg_rc("|");
1911     /* Video */
1912     msg_rc("%s", _("+-[Video Decoding]"));
1913     msg_rc(_("| video decoded    :    %5i"),
1914             p_item->p_stats->i_decoded_video );
1915     msg_rc(_("| frames displayed :    %5i"),
1916             p_item->p_stats->i_displayed_pictures );
1917     msg_rc(_("| frames lost      :    %5i"),
1918             p_item->p_stats->i_lost_pictures );
1919     msg_rc("|");
1920     /* Audio*/
1921     msg_rc("%s", _("+-[Audio Decoding]"));
1922     msg_rc(_("| audio decoded    :    %5i"),
1923             p_item->p_stats->i_decoded_audio );
1924     msg_rc(_("| buffers played   :    %5i"),
1925             p_item->p_stats->i_played_abuffers );
1926     msg_rc(_("| buffers lost     :    %5i"),
1927             p_item->p_stats->i_lost_abuffers );
1928     msg_rc("|");
1929     /* Sout */
1930     msg_rc("%s", _("+-[Streaming]"));
1931     msg_rc(_("| packets sent     :    %5i"), p_item->p_stats->i_sent_packets );
1932     msg_rc(_("| bytes sent       : %8.0f kB"),
1933             (float)(p_item->p_stats->i_sent_bytes)/1000 );
1934     msg_rc(_("| sending bitrate  :   %6.0f kb/s"),
1935             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
1936     msg_rc("|");
1937     msg_rc( "+----[ end of statistical info ]" );
1938     vlc_mutex_unlock( &p_item->p_stats->lock );
1939
1940     return VLC_SUCCESS;
1941 }
1942
1943 #ifdef WIN32
1944 bool ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1945 {
1946     INPUT_RECORD input_record;
1947     DWORD i_dw;
1948
1949     /* On Win32, select() only works on socket descriptors */
1950     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1951                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1952     {
1953         while( vlc_object_alive( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
1954                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1955                                  1, &i_dw ) )
1956         {
1957             if( input_record.EventType != KEY_EVENT ||
1958                 !input_record.Event.KeyEvent.bKeyDown ||
1959                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1960                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1961                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1962                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1963             {
1964                 /* nothing interesting */
1965                 continue;
1966             }
1967
1968             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1969
1970             /* Echo out the command */
1971             putc( p_buffer[ *pi_size ], stdout );
1972
1973             /* Handle special keys */
1974             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1975             {
1976                 putc( '\n', stdout );
1977                 break;
1978             }
1979             switch( p_buffer[ *pi_size ] )
1980             {
1981             case '\b':
1982                 if( *pi_size )
1983                 {
1984                     *pi_size -= 2;
1985                     putc( ' ', stdout );
1986                     putc( '\b', stdout );
1987                 }
1988                 break;
1989             case '\r':
1990                 (*pi_size) --;
1991                 break;
1992             }
1993
1994             (*pi_size)++;
1995         }
1996
1997         if( *pi_size == MAX_LINE_LENGTH ||
1998             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1999         {
2000             p_buffer[ *pi_size ] = 0;
2001             return true;
2002         }
2003     }
2004
2005     return false;
2006 }
2007 #endif
2008
2009 bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2010 {
2011     int i_read = 0;
2012
2013 #ifdef WIN32
2014     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
2015         return ReadWin32( p_intf, p_buffer, pi_size );
2016     else if( p_intf->p_sys->i_socket == -1 )
2017     {
2018         msleep( INTF_IDLE_SLEEP );
2019         return false;
2020     }
2021 #endif
2022
2023     while( vlc_object_alive( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
2024            (i_read = net_Read( p_intf, p_intf->p_sys->i_socket == -1 ?
2025                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
2026                   (uint8_t *)p_buffer + *pi_size, 1, false ) ) > 0 )
2027     {
2028         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2029             break;
2030
2031         (*pi_size)++;
2032     }
2033
2034     /* Connection closed */
2035     if( i_read <= 0 )
2036     {
2037         if( p_intf->p_sys->i_socket != -1 )
2038         {
2039             net_Close( p_intf->p_sys->i_socket );
2040             p_intf->p_sys->i_socket = -1;
2041         }
2042         else
2043         {
2044             /* Standard input closed: exit */
2045             vlc_value_t empty;
2046             Quit( VLC_OBJECT(p_intf), NULL, empty, empty, NULL );
2047         }
2048
2049         p_buffer[ *pi_size ] = 0;
2050         return true;
2051     }
2052
2053     if( *pi_size == MAX_LINE_LENGTH ||
2054         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2055     {
2056         p_buffer[ *pi_size ] = 0;
2057         return true;
2058     }
2059
2060     return false;
2061 }
2062
2063 /*****************************************************************************
2064  * parse_MRL: build a input item from a full mrl
2065  *****************************************************************************
2066  * MRL format: "simplified-mrl [:option-name[=option-value]]"
2067  * We don't check for '"' or '\'', we just assume that a ':' that follows a
2068  * space is a new option. Should be good enough for our purpose.
2069  *****************************************************************************/
2070 static input_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
2071 {
2072 #define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
2073 #define SKIPTRAILINGSPACE( p, d ) \
2074     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
2075
2076     input_item_t *p_item = NULL;
2077     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
2078     char **ppsz_options = NULL;
2079     int i, i_options = 0;
2080
2081     if( !psz_mrl ) return 0;
2082
2083     psz_mrl = psz_orig = strdup( psz_mrl );
2084     while( *psz_mrl )
2085     {
2086         SKIPSPACE( psz_mrl );
2087         psz_item = psz_mrl;
2088
2089         for( ; *psz_mrl; psz_mrl++ )
2090         {
2091             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
2092             {
2093                 /* We have a complete item */
2094                 break;
2095             }
2096             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
2097                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
2098             {
2099                 /* We have a complete item */
2100                 break;
2101             }
2102         }
2103
2104         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
2105         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
2106
2107         /* Remove '"' and '\'' if necessary */
2108         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
2109         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2110         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
2111         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2112
2113         if( !psz_item_mrl ) psz_item_mrl = psz_item;
2114         else if( *psz_item )
2115         {
2116             i_options++;
2117             ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
2118             ppsz_options[i_options - 1] = &psz_item[1];
2119         }
2120
2121         if( *psz_mrl ) SKIPSPACE( psz_mrl );
2122     }
2123
2124     /* Now create a playlist item */
2125     if( psz_item_mrl )
2126     {
2127         p_item = input_item_New( p_intf, psz_item_mrl, NULL );
2128         for( i = 0; i < i_options; i++ )
2129         {
2130             input_item_AddOption( p_item, ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
2131         }
2132     }
2133
2134     if( i_options ) free( ppsz_options );
2135     free( psz_orig );
2136
2137     return p_item;
2138 }