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