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