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