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