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