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