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