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