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