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