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