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