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