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