]> git.sesse.net Git - vlc/blob - modules/control/rc.c
Use net_Accept's timeout param instead of a call to msleep.
[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,
505                             INTF_IDLE_SLEEP );
506             if( p_intf->p_sys->i_socket == -1 ) continue;
507         }
508
509         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
510
511         /* Manage the input part */
512         if( p_input == NULL )
513         {
514             if( p_playlist )
515             {
516                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
517                                                        FIND_CHILD );
518             }
519             else
520             {
521                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
522                                                    FIND_ANYWHERE );
523                 if( p_input )
524                 {
525                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
526                                                            FIND_PARENT );
527                 }
528             }
529             /* New input has been registered */
530             if( p_input )
531             {
532                 if( !p_input->b_dead || !p_input->b_die )
533                 {
534                     msg_rc( STATUS_CHANGE "( new input: %s )",
535                             input_GetItem(p_input)->psz_uri );
536                     msg_rc( STATUS_CHANGE "( audio volume: %d )",
537                             config_GetInt( p_intf, "volume" ));
538                 }
539                 var_AddCallback( p_input, "state", StateChanged, p_intf );
540                 var_AddCallback( p_input, "rate-faster", RateChanged, p_intf );
541                 var_AddCallback( p_input, "rate-slower", RateChanged, p_intf );
542                 var_AddCallback( p_input, "rate", RateChanged, p_intf );
543                 var_AddCallback( p_input, "time-offset", TimeOffsetChanged,
544                                  p_intf );
545             }
546         }
547         else if( p_input->b_dead )
548         {
549             var_DelCallback( p_input, "state", StateChanged, p_intf );
550             var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
551             var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
552             var_DelCallback( p_input, "rate", RateChanged, p_intf );
553             var_DelCallback( p_input, "time-offset", TimeOffsetChanged,
554                              p_intf );
555             vlc_object_release( p_input );
556             p_input = NULL;
557
558             if( p_playlist )
559             {
560                 vlc_mutex_lock( &p_playlist->object_lock );
561                 p_intf->p_sys->i_last_state = (int) PLAYLIST_STOPPED;
562                 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
563                 vlc_mutex_unlock( &p_playlist->object_lock );
564             }
565         }
566
567         if( (p_input != NULL) && !p_input->b_dead && !p_input->b_die &&
568             (p_playlist != NULL) )
569         {
570             vlc_mutex_lock( &p_playlist->object_lock );
571             if( (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
572                 (p_playlist->status.i_status == PLAYLIST_STOPPED) )
573             {
574                 p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
575                 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
576             }
577             else if(
578                 (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
579                 (p_playlist->status.i_status == PLAYLIST_RUNNING) )
580             {
581                 p_intf->p_sys->i_last_state = p_playlist->status.i_status;
582                 msg_rc( STATUS_CHANGE "( play state: 1 )" );
583             }
584             else if(
585                 (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
586                 (p_playlist->status.i_status == PLAYLIST_PAUSED) )
587             {
588                 p_intf->p_sys->i_last_state = p_playlist->status.i_status;
589                 msg_rc( STATUS_CHANGE "( pause state: 2 )" );
590             }
591             vlc_mutex_unlock( &p_playlist->object_lock );
592         }
593
594         if( p_input && b_showpos )
595         {
596             i_newpos = 100 * var_GetFloat( p_input, "position" );
597             if( i_oldpos != i_newpos )
598             {
599                 i_oldpos = i_newpos;
600                 msg_rc( "pos: %d%%", i_newpos );
601             }
602         }
603
604         /* Is there something to do? */
605         if( !b_complete ) continue;
606
607
608         /* Skip heading spaces */
609         psz_cmd = p_buffer;
610         while( *psz_cmd == ' ' )
611         {
612             psz_cmd++;
613         }
614
615         /* Split psz_cmd at the first space and make sure that
616          * psz_arg is valid */
617         psz_arg = strchr( psz_cmd, ' ' );
618         if( psz_arg )
619         {
620             *psz_arg++ = 0;
621             while( *psz_arg == ' ' )
622             {
623                 psz_arg++;
624             }
625         }
626         else
627         {
628             psz_arg = "";
629         }
630
631         /* If the user typed a registered local command, try it */
632         if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
633         {
634             vlc_value_t val;
635             int i_ret;
636
637             val.psz_string = psz_arg;
638             i_ret = var_Set( p_intf, psz_cmd, val );
639             msg_rc( "%s: returned %i (%s)",
640                     psz_cmd, i_ret, vlc_error( i_ret ) );
641         }
642         /* Or maybe it's a global command */
643         else if( var_Type( p_intf->p_libvlc_global, psz_cmd ) & VLC_VAR_ISCOMMAND )
644         {
645             vlc_value_t val;
646             int i_ret;
647
648             val.psz_string = psz_arg;
649             /* FIXME: it's a global command, but we should pass the
650              * local object as an argument, not p_intf->p_libvlc_global. */
651             i_ret = var_Set( p_intf->p_libvlc_global, psz_cmd, val );
652             if( i_ret != 0 )
653             {
654                 msg_rc( "%s: returned %i (%s)",
655                          psz_cmd, i_ret, vlc_error( i_ret ) );
656             }
657         }
658         else if( !strcmp( psz_cmd, "logout" ) )
659         {
660             /* Close connection */
661             if( p_intf->p_sys->i_socket != -1 )
662             {
663                 net_Close( p_intf->p_sys->i_socket );
664             }
665             p_intf->p_sys->i_socket = -1;
666         }
667         else if( !strcmp( psz_cmd, "info" ) )
668         {
669             if( p_input )
670             {
671                 int i, j;
672                 vlc_mutex_lock( &input_GetItem(p_input)->lock );
673                 for ( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
674                 {
675                     info_category_t *p_category = input_GetItem(p_input)
676                                                         ->pp_categories[i];
677
678                     msg_rc( "+----[ %s ]", p_category->psz_name );
679                     msg_rc( "| " );
680                     for ( j = 0; j < p_category->i_infos; j++ )
681                     {
682                         info_t *p_info = p_category->pp_infos[j];
683                         msg_rc( "| %s: %s", p_info->psz_name,
684                                 p_info->psz_value );
685                     }
686                     msg_rc( "| " );
687                 }
688                 msg_rc( "+----[ end of stream info ]" );
689                 vlc_mutex_unlock( &input_GetItem(p_input)->lock );
690             }
691             else
692             {
693                 msg_rc( "no input" );
694             }
695         }
696         else if( !strcmp( psz_cmd, "is_playing" ) )
697         {
698             if( ! p_input )
699             {
700                 msg_rc( "0" );
701             }
702             else
703             {
704                 msg_rc( "1" );
705             }
706         }
707         else if( !strcmp( psz_cmd, "get_time" ) )
708         {
709             if( ! p_input )
710             {
711                 msg_rc("0");
712             }
713             else
714             {
715                 vlc_value_t time;
716                 var_Get( p_input, "time", &time );
717                 msg_rc( "%i", time.i_time / 1000000);
718             }
719         }
720         else if( !strcmp( psz_cmd, "get_length" ) )
721         {
722             if( ! p_input )
723             {
724                 msg_rc("0");
725             }
726             else
727             {
728                 vlc_value_t time;
729                 var_Get( p_input, "length", &time );
730                 msg_rc( "%i", time.i_time / 1000000);
731             }
732         }
733         else if( !strcmp( psz_cmd, "get_title" ) )
734         {
735             if( ! p_input )
736             {
737                 msg_rc("");
738             }
739             else
740             {
741                 msg_rc( "%s", input_GetItem(p_input)->psz_name );
742             }
743         }
744         else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
745                  || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
746         {
747             if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "H", 1 ) )
748                  b_longhelp = VLC_TRUE;
749             else b_longhelp = VLC_FALSE;
750
751             Help( p_intf, b_longhelp );
752         }
753         else if( !strcmp( psz_cmd, "check-updates" ) )
754         {
755             checkUpdates( p_intf, psz_arg );
756         }
757         else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
758         {
759             var_SetInteger( p_intf->p_libvlc, "key-pressed",
760                             config_GetInt( p_intf, psz_arg ) );
761         }
762         else switch( psz_cmd[0] )
763         {
764         case 'f':
765         case 'F':
766             if( p_input )
767             {
768                 vout_thread_t *p_vout;
769                 p_vout = vlc_object_find( p_input,
770                                           VLC_OBJECT_VOUT, FIND_CHILD );
771
772                 if( p_vout )
773                 {
774                     vlc_value_t val;
775                     vlc_bool_t b_update = VLC_FALSE;
776                     var_Get( p_vout, "fullscreen", &val );
777                     val.b_bool = !val.b_bool;
778                     if( !strncmp( psz_arg, "on", 2 )
779                         && ( val.b_bool == VLC_TRUE ) )
780                     {
781                         b_update = VLC_TRUE;
782                         val.b_bool = VLC_TRUE;
783                     }
784                     else if( !strncmp( psz_arg, "off", 3 )
785                              && ( val.b_bool == VLC_FALSE ) )
786                     {
787                         b_update = VLC_TRUE;
788                         val.b_bool = VLC_FALSE;
789                     }
790                     else if( strncmp( psz_arg, "off", 3 )
791                              && strncmp( psz_arg, "on", 2 ) )
792                         b_update = VLC_TRUE;
793                     if( b_update ) var_Set( p_vout, "fullscreen", val );
794                     vlc_object_release( p_vout );
795                 }
796             }
797             break;
798
799         case 's':
800         case 'S':
801             ;
802             break;
803
804         case '\0':
805             /* Ignore empty lines */
806             break;
807
808         default:
809             msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
810             break;
811         }
812
813         /* Command processed */
814         i_size = 0; p_buffer[0] = 0;
815     }
816
817     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
818     msg_rc( STATUS_CHANGE "( quit )" );
819
820     if( p_input )
821     {
822         var_DelCallback( p_input, "state", StateChanged, p_intf );
823         var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
824         var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
825         var_DelCallback( p_input, "rate", RateChanged, p_intf );
826         var_DelCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
827         vlc_object_release( p_input );
828         p_input = NULL;
829     }
830
831     if( p_playlist )
832     {
833         vlc_object_release( p_playlist );
834         p_playlist = NULL;
835     }
836
837     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
838 }
839
840 static void Help( intf_thread_t *p_intf, vlc_bool_t b_longhelp)
841 {
842     msg_rc(_("+----[ Remote control commands ]"));
843     msg_rc(  "| ");
844     msg_rc(_("| add XYZ  . . . . . . . . . . add XYZ to playlist"));
845     msg_rc(_("| enqueue XYZ  . . . . . . . queue XYZ to playlist"));
846     msg_rc(_("| playlist . . .  show items currently in playlist"));
847     msg_rc(_("| play . . . . . . . . . . . . . . . . play stream"));
848     msg_rc(_("| stop . . . . . . . . . . . . . . . . stop stream"));
849     msg_rc(_("| next . . . . . . . . . . . .  next playlist item"));
850     msg_rc(_("| prev . . . . . . . . . .  previous playlist item"));
851     msg_rc(_("| goto . . . . . . . . . . . .  goto item at index"));
852     msg_rc(_("| repeat [on|off] . .  toggle playlist item repeat"));
853     msg_rc(_("| loop [on|off] . . . .  toggle playlist item loop"));
854     msg_rc(_("| clear . . . . . . . . . . .   clear the playlist"));
855     msg_rc(_("| status . . . . . . . . . current playlist status"));
856     msg_rc(_("| title [X]  . . . . set/get title in current item"));
857     msg_rc(_("| title_n  . . . . . .  next title in current item"));
858     msg_rc(_("| title_p  . . . .  previous title in current item"));
859     msg_rc(_("| chapter [X]  . . set/get chapter in current item"));
860     msg_rc(_("| chapter_n  . . . .  next chapter in current item"));
861     msg_rc(_("| chapter_p  . .  previous chapter in current item"));
862     msg_rc(  "| ");
863     msg_rc(_("| seek X . seek in seconds, for instance `seek 12'"));
864     msg_rc(_("| pause  . . . . . . . . . . . . . .  toggle pause"));
865     msg_rc(_("| fastforward  . . . . . .  .  set to maximum rate"));
866     msg_rc(_("| rewind  . . . . . . . . . .  set to minimum rate"));
867     msg_rc(_("| faster . . . . . . . .  faster playing of stream"));
868     msg_rc(_("| slower . . . . . . . .  slower playing of stream"));
869     msg_rc(_("| normal . . . . . . . .  normal playing of stream"));
870     msg_rc(_("| f [on|off] . . . . . . . . . . toggle fullscreen"));
871     msg_rc(_("| info . . .  information about the current stream"));
872     msg_rc(_("| get_time . . seconds elapsed since stream's beginning"));
873     msg_rc(_("| is_playing . .  1 if a stream plays, 0 otherwise"));
874     msg_rc(_("| get_title . . .  the title of the current stream"));
875     msg_rc(_("| get_length . .  the length of the current stream"));
876     msg_rc(  "| ");
877     msg_rc(_("| volume [X] . . . . . . . .  set/get audio volume"));
878     msg_rc(_("| volup [X]  . . . . .  raise audio volume X steps"));
879     msg_rc(_("| voldown [X]  . . . .  lower audio volume X steps"));
880     msg_rc(_("| adev [X] . . . . . . . . .  set/get audio device"));
881     msg_rc(_("| achan [X]. . . . . . . .  set/get audio channels"));
882     msg_rc(_("| atrack [X] . . . . . . . . . set/get audio track"));
883     msg_rc(_("| vtrack [X] . . . . . . . . . set/get video track"));
884     msg_rc(_("| vratio [X]  . . . . . set/get video aspect ratio"));
885     msg_rc(_("| vcrop [X]  . . . . . . . . .  set/get video crop"));
886     msg_rc(_("| vzoom [X]  . . . . . . . . .  set/get video zoom"));
887     msg_rc(_("| strack [X] . . . . . . . set/get subtitles track"));
888     msg_rc(_("| key [hotkey name]  . . . . simulate hotkey press"));
889     msg_rc(_("| menu [on|off|up|down|left|right|select] use menu"));
890     msg_rc(  "| ");
891
892     if (b_longhelp)
893     {
894         msg_rc(_("| marq-marquee STRING  . . overlay STRING in video"));
895         msg_rc(_("| marq-x X . . . . . . . . . . . .offset from left"));
896         msg_rc(_("| marq-y Y . . . . . . . . . . . . offset from top"));
897         msg_rc(_("| marq-position #. . .  .relative position control"));
898         msg_rc(_("| marq-color # . . . . . . . . . . font color, RGB"));
899         msg_rc(_("| marq-opacity # . . . . . . . . . . . . . opacity"));
900         msg_rc(_("| marq-timeout T. . . . . . . . . . timeout, in ms"));
901         msg_rc(_("| marq-size # . . . . . . . . font size, in pixels"));
902         msg_rc(  "| ");
903         msg_rc(_("| logo-file STRING . . .the overlay file path/name"));
904         msg_rc(_("| logo-x X . . . . . . . . . . . .offset from left"));
905         msg_rc(_("| logo-y Y . . . . . . . . . . . . offset from top"));
906         msg_rc(_("| logo-position #. . . . . . . . relative position"));
907         msg_rc(_("| logo-transparency #. . . . . . . . .transparency"));
908         msg_rc(  "| ");
909         msg_rc(_("| mosaic-alpha # . . . . . . . . . . . . . . alpha"));
910         msg_rc(_("| mosaic-height #. . . . . . . . . . . . . .height"));
911         msg_rc(_("| mosaic-width # . . . . . . . . . . . . . . width"));
912         msg_rc(_("| mosaic-xoffset # . . . .top left corner position"));
913         msg_rc(_("| mosaic-yoffset # . . . .top left corner position"));
914         msg_rc(_("| mosaic-offsets x,y(,x,y)*. . . . list of offsets"));
915         msg_rc(_("| mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
916         msg_rc(_("| mosaic-vborder # . . . . . . . . vertical border"));
917         msg_rc(_("| mosaic-hborder # . . . . . . . horizontal border"));
918         msg_rc(_("| mosaic-position {0=auto,1=fixed} . . . .position"));
919         msg_rc(_("| mosaic-rows #. . . . . . . . . . .number of rows"));
920         msg_rc(_("| mosaic-cols #. . . . . . . . . . .number of cols"));
921         msg_rc(_("| mosaic-order id(,id)* . . . . order of pictures "));
922         msg_rc(_("| mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
923         msg_rc(  "| ");
924         msg_rc(_("| check-updates [newer] [equal] [older]\n"
925                  "|               [undef] [info] [source] [binary] [plugin]"));
926         msg_rc(  "| ");
927     }
928     msg_rc(_("| help . . . . . . . . . . . . . this help message"));
929     msg_rc(_("| longhelp . . . . . . . . . a longer help message"));
930     msg_rc(_("| logout . . . . .  exit (if in socket connection)"));
931     msg_rc(_("| quit . . . . . . . . . . . . . . . . .  quit vlc"));
932     msg_rc(  "| ");
933     msg_rc(_("+----[ end of help ]"));
934 }
935
936 /********************************************************************
937  * Status callback routines
938  ********************************************************************/
939 static int TimeOffsetChanged( vlc_object_t *p_this, char const *psz_cmd,
940     vlc_value_t oldval, vlc_value_t newval, void *p_data )
941 {
942     intf_thread_t *p_intf = (intf_thread_t*)p_data;
943     input_thread_t *p_input = NULL;
944
945     vlc_mutex_lock( &p_intf->p_sys->status_lock );
946     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
947     if( p_input )
948     {
949         msg_rc( STATUS_CHANGE "( time-offset: %d )",
950                 var_GetInteger( p_input, "time-offset" ) );
951         vlc_object_release( p_input );
952     }
953     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
954     return VLC_SUCCESS;
955 }
956
957 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
958     vlc_value_t oldval, vlc_value_t newval, void *p_data )
959 {
960     intf_thread_t *p_intf = (intf_thread_t*)p_data;
961
962     vlc_mutex_lock( &p_intf->p_sys->status_lock );
963     msg_rc( STATUS_CHANGE "( audio volume: %d )",
964             config_GetInt( p_this, "volume") );
965     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
966     return VLC_SUCCESS;
967 }
968
969 static int StateChanged( vlc_object_t *p_this, char const *psz_cmd,
970     vlc_value_t oldval, vlc_value_t newval, void *p_data )
971 {
972     intf_thread_t *p_intf = (intf_thread_t*)p_data;
973     playlist_t    *p_playlist = NULL;
974     input_thread_t *p_input = NULL;
975
976     vlc_mutex_lock( &p_intf->p_sys->status_lock );
977     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
978     if( p_input )
979     {
980         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
981         if( p_playlist )
982         {
983             char cmd[5] = "";
984             switch( p_playlist->status.i_status )
985             {
986             case PLAYLIST_STOPPED:
987                 strncpy( &cmd[0], "stop", 4);
988                 cmd[4] = '\0';
989                 break;
990             case PLAYLIST_RUNNING:
991                 strncpy( &cmd[0], "play", 4);
992                 cmd[4] = '\0';
993                 break;
994             case PLAYLIST_PAUSED:
995                 strncpy( &cmd[0], "pause", 5);
996                 cmd[5] = '\0';
997                 break;
998             } /* var_GetInteger( p_input, "state" )  */
999             msg_rc( STATUS_CHANGE "( %s state: %d )", &cmd[0], newval.i_int );
1000             vlc_object_release( p_playlist );
1001         }
1002         vlc_object_release( p_input );
1003     }
1004     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
1005     return VLC_SUCCESS;
1006 }
1007
1008 static int RateChanged( vlc_object_t *p_this, char const *psz_cmd,
1009     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1010 {
1011     intf_thread_t *p_intf = (intf_thread_t*)p_data;
1012     input_thread_t *p_input = NULL;
1013
1014     vlc_mutex_lock( &p_intf->p_sys->status_lock );
1015     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1016     if( p_input )
1017     {
1018         msg_rc( STATUS_CHANGE "( new rate: %d )",
1019                 var_GetInteger( p_input, "rate" ) );
1020         vlc_object_release( p_input );
1021     }
1022     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
1023     return VLC_SUCCESS;
1024 }
1025
1026 /********************************************************************
1027  * Command routines
1028  ********************************************************************/
1029 static int Input( vlc_object_t *p_this, char const *psz_cmd,
1030                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
1031 {
1032     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1033     input_thread_t *p_input;
1034     vlc_value_t     val;
1035
1036     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1037     if( !p_input ) return VLC_ENOOBJ;
1038
1039     var_Get( p_input, "state", &val );
1040     if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1041         ( strcmp( psz_cmd, "pause" ) != 0 ) )
1042     {
1043         msg_rc( _("Press menu select or pause to continue.") );
1044         vlc_object_release( p_input );
1045         return VLC_EGENERIC;
1046     }
1047
1048     /* Parse commands that only require an input */
1049     if( !strcmp( psz_cmd, "pause" ) )
1050     {
1051         val.i_int = config_GetInt( p_intf, "key-play-pause" );
1052         var_Set( p_intf->p_libvlc, "key-pressed", val );
1053         vlc_object_release( p_input );
1054         return VLC_SUCCESS;
1055     }
1056     else if( !strcmp( psz_cmd, "seek" ) )
1057     {
1058         if( strlen( newval.psz_string ) > 0 &&
1059             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
1060         {
1061             val.f_float = (float)atof( newval.psz_string ) / 100.0;
1062             var_Set( p_input, "position", val );
1063         }
1064         else
1065         {
1066             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
1067             var_Set( p_input, "time", val );
1068         }
1069         vlc_object_release( p_input );
1070         return VLC_SUCCESS;
1071     }
1072     else if ( !strcmp( psz_cmd, "fastforward" ) )
1073     {
1074         val.i_int = config_GetInt( p_intf, "key-jump+extrashort" );
1075         var_Set( p_intf->p_libvlc, "key-pressed", val );
1076         vlc_object_release( p_input );
1077         return VLC_SUCCESS;
1078     }
1079     else if ( !strcmp( psz_cmd, "rewind" ) )
1080     {
1081         val.i_int = config_GetInt( p_intf, "key-jump-extrashort" );
1082         var_Set( p_intf->p_libvlc, "key-pressed", val );
1083         vlc_object_release( p_input );
1084         return VLC_SUCCESS;
1085     }
1086     else if ( !strcmp( psz_cmd, "faster" ) )
1087     {
1088         val.b_bool = VLC_TRUE;
1089         var_Set( p_input, "rate-faster", val );
1090         vlc_object_release( p_input );
1091         return VLC_SUCCESS;
1092     }
1093     else if ( !strcmp( psz_cmd, "slower" ) )
1094     {
1095         val.b_bool = VLC_TRUE;
1096         var_Set( p_input, "rate-slower", val );
1097         vlc_object_release( p_input );
1098         return VLC_SUCCESS;
1099     }
1100     else if ( !strcmp( psz_cmd, "normal" ) )
1101     {
1102         val.i_int = INPUT_RATE_DEFAULT;
1103         var_Set( p_input, "rate", val );
1104         vlc_object_release( p_input );
1105         return VLC_SUCCESS;
1106     }
1107     else if( !strcmp( psz_cmd, "chapter" ) ||
1108              !strcmp( psz_cmd, "chapter_n" ) ||
1109              !strcmp( psz_cmd, "chapter_p" ) )
1110     {
1111         if( !strcmp( psz_cmd, "chapter" ) )
1112         {
1113             if ( *newval.psz_string )
1114             {
1115                 /* Set. */
1116                 val.i_int = atoi( newval.psz_string );
1117                 var_Set( p_input, "chapter", val );
1118             }
1119             else
1120             {
1121                 vlc_value_t val_list;
1122
1123                 /* Get. */
1124                 var_Get( p_input, "chapter", &val );
1125                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
1126                             &val_list, NULL );
1127                 msg_rc( "Currently playing chapter %d/%d.",
1128                         val.i_int, val_list.p_list->i_count );
1129                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
1130                             &val_list, NULL );
1131             }
1132         }
1133         else if( !strcmp( psz_cmd, "chapter_n" ) )
1134         {
1135             val.b_bool = VLC_TRUE;
1136             var_Set( p_input, "next-chapter", val );
1137         }
1138         else if( !strcmp( psz_cmd, "chapter_p" ) )
1139         {
1140             val.b_bool = VLC_TRUE;
1141             var_Set( p_input, "prev-chapter", val );
1142         }
1143         vlc_object_release( p_input );
1144         return VLC_SUCCESS;
1145     }
1146     else if( !strcmp( psz_cmd, "title" ) ||
1147              !strcmp( psz_cmd, "title_n" ) ||
1148              !strcmp( psz_cmd, "title_p" ) )
1149     {
1150         if( !strcmp( psz_cmd, "title" ) )
1151         {
1152             if ( *newval.psz_string )
1153             {
1154                 /* Set. */
1155                 val.i_int = atoi( newval.psz_string );
1156                 var_Set( p_input, "title", val );
1157             }
1158             else
1159             {
1160                 vlc_value_t val_list;
1161
1162                 /* Get. */
1163                 var_Get( p_input, "title", &val );
1164                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
1165                             &val_list, NULL );
1166                 msg_rc( "Currently playing title %d/%d.",
1167                         val.i_int, val_list.p_list->i_count );
1168                 var_Change( p_this, "title", VLC_VAR_FREELIST,
1169                             &val_list, NULL );
1170             }
1171         }
1172         else if( !strcmp( psz_cmd, "title_n" ) )
1173         {
1174             val.b_bool = VLC_TRUE;
1175             var_Set( p_input, "next-title", val );
1176         }
1177         else if( !strcmp( psz_cmd, "title_p" ) )
1178         {
1179             val.b_bool = VLC_TRUE;
1180             var_Set( p_input, "prev-title", val );
1181         }
1182
1183         vlc_object_release( p_input );
1184         return VLC_SUCCESS;
1185     }
1186     else if(    !strcmp( psz_cmd, "atrack" )
1187              || !strcmp( psz_cmd, "vtrack" )
1188              || !strcmp( psz_cmd, "strack" ) )
1189     {
1190         const char *psz_variable;
1191         vlc_value_t val_name;
1192         int i_error;
1193
1194         if( !strcmp( psz_cmd, "atrack" ) )
1195         {
1196             psz_variable = "audio-es";
1197         }
1198         else if( !strcmp( psz_cmd, "vtrack" ) )
1199         {
1200             psz_variable = "video-es";
1201         }
1202         else
1203         {
1204             psz_variable = "spu-es";
1205         }
1206
1207         /* Get the descriptive name of the variable */
1208         var_Change( p_input, psz_variable, VLC_VAR_GETTEXT,
1209                      &val_name, NULL );
1210         if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1211
1212         if( newval.psz_string && *newval.psz_string )
1213         {
1214             /* set */
1215             vlc_value_t val;
1216             val.i_int = atoi( newval.psz_string );
1217
1218             i_error = var_Set( p_input, psz_variable, val );
1219         }
1220         else
1221         {
1222             /* get */
1223             vlc_value_t val, text;
1224             int i, i_value;
1225
1226             if ( var_Get( p_input, psz_variable, &val ) < 0 )
1227             {
1228                 vlc_object_release( p_input );
1229                 return VLC_EGENERIC;
1230             }
1231             i_value = val.i_int;
1232
1233             if ( var_Change( p_input, psz_variable,
1234                              VLC_VAR_GETLIST, &val, &text ) < 0 )
1235             {
1236                 vlc_object_release( p_input );
1237                 return VLC_EGENERIC;
1238             }
1239
1240             msg_rc( "+----[ %s ]", val_name.psz_string );
1241             for ( i = 0; i < val.p_list->i_count; i++ )
1242             {
1243                 if ( i_value == val.p_list->p_values[i].i_int )
1244                     msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1245                             text.p_list->p_values[i].psz_string );
1246                 else
1247                     msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1248                             text.p_list->p_values[i].psz_string );
1249             }
1250             var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1251                         &val, &text );
1252             msg_rc( "+----[ end of %s ]", val_name.psz_string );
1253
1254             if( val_name.psz_string ) free( val_name.psz_string );
1255
1256             i_error = VLC_SUCCESS;
1257         }
1258         vlc_object_release( p_input );
1259         return i_error;
1260     }
1261
1262     /* Never reached. */
1263     vlc_object_release( p_input );
1264     return VLC_EGENERIC;
1265 }
1266
1267 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1268                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1269 {
1270     vlc_value_t val;
1271
1272     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1273     playlist_t *p_playlist = pl_Yield( p_this );
1274
1275     PL_LOCK;
1276     if( p_playlist->p_input )
1277     {
1278         var_Get( p_playlist->p_input, "state", &val );
1279         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1280         {
1281             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1282             vlc_object_release( p_playlist );
1283             PL_UNLOCK;
1284             return VLC_EGENERIC;
1285         }
1286     }
1287     PL_UNLOCK;
1288
1289     /* Parse commands that require a playlist */
1290     if( !strcmp( psz_cmd, "prev" ) )
1291     {
1292         playlist_Prev( p_playlist );
1293     }
1294     else if( !strcmp( psz_cmd, "next" ) )
1295     {
1296         playlist_Next( p_playlist );
1297     }
1298     else if( !strcmp( psz_cmd, "play" ) )
1299     {
1300         msg_Warn( p_playlist, "play" );
1301         playlist_Play( p_playlist );
1302     }
1303     else if( !strcmp( psz_cmd, "repeat" ) )
1304     {
1305         vlc_bool_t b_update = VLC_TRUE;
1306
1307         var_Get( p_playlist, "repeat", &val );
1308
1309         if( strlen( newval.psz_string ) > 0 )
1310         {
1311             if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == VLC_TRUE ) ) ||
1312                  ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == VLC_FALSE ) ) )
1313             {
1314                 b_update = VLC_FALSE;
1315             }
1316         }
1317
1318         if ( b_update )
1319         {
1320             val.b_bool = !val.b_bool;
1321             var_Set( p_playlist, "repeat", val );
1322         }
1323         msg_rc( "Setting repeat to %d", val.b_bool );
1324     }
1325     else if( !strcmp( psz_cmd, "loop" ) )
1326     {
1327         vlc_bool_t b_update = VLC_TRUE;
1328
1329         var_Get( p_playlist, "loop", &val );
1330
1331         if( strlen( newval.psz_string ) > 0 )
1332         {
1333             if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == VLC_TRUE ) ) ||
1334                  ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == VLC_FALSE ) ) )
1335             {
1336                 b_update = VLC_FALSE;
1337             }
1338         }
1339
1340         if ( b_update )
1341         {
1342             val.b_bool = !val.b_bool;
1343             var_Set( p_playlist, "loop", val );
1344         }
1345         msg_rc( "Setting loop to %d", val.b_bool );
1346     }
1347     else if (!strcmp( psz_cmd, "goto" ) )
1348     {
1349         msg_rc( _("goto is deprecated" ) );
1350         msg_Err( p_playlist, "goto is deprecated" );
1351     }
1352     else if( !strcmp( psz_cmd, "stop" ) )
1353     {
1354         playlist_Stop( p_playlist );
1355     }
1356     else if( !strcmp( psz_cmd, "clear" ) )
1357     {
1358         playlist_Stop( p_playlist );
1359         playlist_Clear( p_playlist, VLC_FALSE );
1360     }
1361     else if( !strcmp( psz_cmd, "add" ) &&
1362              newval.psz_string && *newval.psz_string )
1363     {
1364         input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1365
1366         if( p_item )
1367         {
1368             msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1369             playlist_AddInput( p_playlist, p_item,
1370                      PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END, VLC_TRUE );
1371         }
1372     }
1373     else if( !strcmp( psz_cmd, "enqueue" ) &&
1374              newval.psz_string && *newval.psz_string )
1375     {
1376         input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1377
1378         if( p_item )
1379         {
1380             msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1381             playlist_AddInput( p_playlist, p_item,
1382                                PLAYLIST_APPEND, PLAYLIST_END, VLC_TRUE );
1383         }
1384     }
1385     else if( !strcmp( psz_cmd, "playlist" ) )
1386     {
1387         msg_Dbg( p_playlist, "Dumping category" );
1388         playlist_NodeDump( p_playlist, p_playlist->p_root_category, 0 );
1389         msg_Dbg( p_playlist, "Dumping Onelevel" );
1390         playlist_NodeDump( p_playlist, p_playlist->p_root_onelevel, 0 );
1391     }
1392     else if( !strcmp( psz_cmd, "sort" ))
1393     {
1394         playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_onelevel,
1395                                     SORT_ARTIST, ORDER_NORMAL );
1396     }
1397     else if( !strcmp( psz_cmd, "status" ) )
1398     {
1399         if( p_playlist->p_input )
1400         {
1401             /* Replay the current state of the system. */
1402             msg_rc( STATUS_CHANGE "( new input: %s )",
1403                     input_GetItem(p_playlist->p_input)->psz_uri );
1404             msg_rc( STATUS_CHANGE "( audio volume: %d )",
1405                     config_GetInt( p_intf, "volume" ));
1406
1407             vlc_mutex_lock( &p_playlist->object_lock );
1408             switch( p_playlist->status.i_status )
1409             {
1410                 case PLAYLIST_STOPPED:
1411                     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
1412                     break;
1413                 case PLAYLIST_RUNNING:
1414                     msg_rc( STATUS_CHANGE "( play state: 1 )" );
1415                     break;
1416                 case PLAYLIST_PAUSED:
1417                     msg_rc( STATUS_CHANGE "( pause state: 2 )" );
1418                     break;
1419                 default:
1420                     msg_rc( STATUS_CHANGE "( state unknown )" );
1421                     break;
1422             }
1423             vlc_mutex_unlock( &p_playlist->object_lock );
1424         }
1425     }
1426
1427     /*
1428      * sanity check
1429      */
1430     else
1431     {
1432         msg_rc( "unknown command!" );
1433     }
1434
1435     vlc_object_release( p_playlist );
1436     return VLC_SUCCESS;
1437 }
1438
1439 static int Other( vlc_object_t *p_this, char const *psz_cmd,
1440                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1441 {
1442     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1443     vlc_object_t  *p_playlist;
1444     vlc_value_t    val;
1445     vlc_object_t  *p_input;
1446
1447     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1448     if( !p_playlist )
1449     {
1450         return VLC_ENOOBJ;
1451     }
1452
1453     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1454     if( !p_input )
1455     {
1456         vlc_object_release( p_playlist );
1457         return VLC_ENOOBJ;
1458     }
1459
1460     if( p_input )
1461     {
1462         var_Get( p_input, "state", &val );
1463         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1464         {
1465             msg_rc( _("Type 'pause' to continue.") );
1466             vlc_object_release( p_playlist );
1467             vlc_object_release( p_input );
1468             return VLC_EGENERIC;
1469         }
1470     }
1471
1472     /* Parse miscellaneous commands */
1473     {
1474         static const char vars[] =
1475             "marq-x\0" "marq-y\0" "marq-position\0" "marq-color\0"
1476             "marq-opacity\0" "marq-size\0" "marq-timeout\0"
1477             "mosaic-alpha\0" "mosaic-height\0" "mosaic-width\0"
1478             "mosaic-xoffset\0" "mosaic-yoffset\0" "mosaic-align\0"
1479             "mosaic-vborder\0" "mosaic-hborder\0" "mosaic-position\0"
1480             "mosaic-rows\0" "mosaic-cols\0" "mosaic-order\0"
1481             "mosaic-offsets\0" "mosaic-keep-aspect-ratio\0"
1482             "logo-file\0" "logo-x\0" "logo-y\0" "logo-position\0"
1483             "logo-transparency\0";
1484         const char *psz_name = NULL;
1485
1486         if( newval.psz_string )
1487         for( psz_name = vars; *psz_name; psz_name += strlen( psz_name ) + 1 )
1488         {
1489             if( strcmp( psz_name, psz_cmd ) == 0 )
1490             {
1491                 int i_type = var_Type( p_input->p_libvlc_global, psz_name );
1492
1493                 if( i_type & VLC_VAR_INTEGER )
1494                 {
1495                     var_SetInteger( p_input->p_libvlc_global, psz_name,
1496                                     atoi( newval.psz_string ) );
1497                     break;
1498                 }
1499                 else if( i_type & VLC_VAR_STRING )
1500                 {
1501                     var_SetString( p_input->p_libvlc_global, psz_name,
1502                                    newval.psz_string );
1503                     break;
1504                 }
1505             }
1506         }
1507
1508         if( *psz_name == '\0' )
1509             msg_rc( "Unknown command!" );
1510     }
1511
1512     vlc_object_release( p_playlist );
1513     vlc_object_release( p_input );
1514     return VLC_SUCCESS;
1515 }
1516
1517 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1518                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1519 {
1520     playlist_t *p_playlist;
1521
1522     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1523     if( p_playlist )
1524     {
1525         playlist_Stop( p_playlist );
1526         vlc_object_release( p_playlist );
1527     }
1528     p_this->p_libvlc->b_die = VLC_TRUE;
1529     return VLC_SUCCESS;
1530 }
1531
1532 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1533                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1534 {
1535     intf_thread_t *p_newintf = NULL;
1536
1537     p_newintf = intf_Create( p_this->p_libvlc, newval.psz_string, 0, NULL );
1538     if( p_newintf )
1539     {
1540         p_newintf->b_block = VLC_FALSE;
1541         if( intf_RunThread( p_newintf ) )
1542         {
1543             vlc_object_detach( p_newintf );
1544             intf_Destroy( p_newintf );
1545         }
1546     }
1547
1548     return VLC_SUCCESS;
1549 }
1550
1551 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1552                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
1553 {
1554     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1555     input_thread_t *p_input = NULL;
1556     int i_error = VLC_EGENERIC;
1557
1558     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1559     if( !p_input )
1560         return VLC_ENOOBJ;
1561
1562     if( p_input )
1563     {
1564         vlc_value_t val;
1565
1566         var_Get( p_input, "state", &val );
1567         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1568         {
1569             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1570             vlc_object_release( p_input );
1571             return VLC_EGENERIC;
1572         }
1573         vlc_object_release( p_input );
1574     }
1575
1576     if ( *newval.psz_string )
1577     {
1578         /* Set. */
1579         audio_volume_t i_volume = atoi( newval.psz_string );
1580         if ( (i_volume > (audio_volume_t)AOUT_VOLUME_MAX) )
1581         {
1582             msg_rc( "Volume must be in the range %d-%d.", AOUT_VOLUME_MIN,
1583                     AOUT_VOLUME_MAX );
1584             i_error = VLC_EBADVAR;
1585         }
1586         else
1587         {
1588             if( i_volume == AOUT_VOLUME_MIN )
1589             {
1590                 vlc_value_t keyval;
1591
1592                 keyval.i_int = config_GetInt( p_intf, "key-vol-mute" );
1593                 var_Set( p_intf->p_libvlc, "key-pressed", keyval );
1594             }
1595             i_error = aout_VolumeSet( p_this, i_volume );
1596             osd_Volume( p_this );
1597             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1598         }
1599     }
1600     else
1601     {
1602         /* Get. */
1603         audio_volume_t i_volume;
1604         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1605         {
1606             i_error = VLC_EGENERIC;
1607         }
1608         else
1609         {
1610             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1611             i_error = VLC_SUCCESS;
1612         }
1613     }
1614
1615     return i_error;
1616 }
1617
1618 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1619                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1620 {
1621     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1622     audio_volume_t i_volume;
1623     input_thread_t *p_input = NULL;
1624     int i_nb_steps = atoi(newval.psz_string);
1625     int i_error = VLC_SUCCESS;
1626     int i_volume_step = 0;
1627
1628     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1629     if( !p_input )
1630         return VLC_ENOOBJ;
1631
1632     if( p_input )
1633     {
1634         vlc_value_t val;
1635
1636         var_Get( p_input, "state", &val );
1637         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1638         {
1639             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1640             vlc_object_release( p_input );
1641             return VLC_EGENERIC;
1642         }
1643         vlc_object_release( p_input );
1644     }
1645
1646     i_volume_step = config_GetInt( p_intf->p_libvlc, "volume-step" );
1647     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/i_volume_step) )
1648     {
1649         i_nb_steps = 1;
1650     }
1651
1652     if ( !strcmp(psz_cmd, "volup") )
1653     {
1654         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1655             i_error = VLC_EGENERIC;
1656     }
1657     else
1658     {
1659         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1660             i_error = VLC_EGENERIC;
1661     }
1662     osd_Volume( p_this );
1663
1664     if ( !i_error ) msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1665     return i_error;
1666 }
1667
1668
1669 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1670                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1671 {
1672     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1673     input_thread_t *p_input = NULL;
1674     vout_thread_t * p_vout;
1675     const char * psz_variable;
1676     vlc_value_t val_name;
1677     int i_error;
1678
1679     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1680     if( !p_input )
1681         return VLC_ENOOBJ;
1682
1683     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
1684     vlc_object_release( p_input );
1685     if( !p_vout )
1686         return VLC_ENOOBJ;
1687
1688     if( !strcmp( psz_cmd, "vcrop" ) )
1689     {
1690         psz_variable = "crop";
1691     }
1692     else if( !strcmp( psz_cmd, "vratio" ) )
1693     {
1694         psz_variable = "aspect-ratio";
1695     }
1696     else /* if( !strcmp( psz_cmd, "vzoom" ) ) */
1697     {
1698         psz_variable = "zoom";
1699     }
1700
1701
1702     /* Get the descriptive name of the variable */
1703     var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT,
1704                  &val_name, NULL );
1705     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1706
1707     if( newval.psz_string && *newval.psz_string )
1708     {
1709         /* set */
1710         if( !strcmp( psz_variable, "zoom" ) )
1711         {
1712             vlc_value_t val;
1713             val.f_float = atof( newval.psz_string );
1714             i_error = var_Set( p_vout, psz_variable, val );
1715         }
1716         else
1717         {
1718             i_error = var_Set( p_vout, psz_variable, newval );
1719         }
1720     }
1721     else
1722     {
1723         /* get */
1724         vlc_value_t val, text;
1725         int i;
1726         float f_value = 0.;
1727         char *psz_value = NULL;
1728
1729         if ( var_Get( p_vout, psz_variable, &val ) < 0 )
1730         {
1731             vlc_object_release( p_vout );
1732             return VLC_EGENERIC;
1733         }
1734         if( !strcmp( psz_variable, "zoom" ) )
1735         {
1736             f_value = val.f_float;
1737         }
1738         else
1739         {
1740             psz_value = strdup( val.psz_string );
1741         }
1742
1743         if ( var_Change( p_vout, psz_variable,
1744                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1745         {
1746             vlc_object_release( p_vout );
1747             return VLC_EGENERIC;
1748         }
1749
1750         msg_rc( "+----[ %s ]", val_name.psz_string );
1751         if( !strcmp( psz_variable, "zoom" ) )
1752         {
1753             for ( i = 0; i < val.p_list->i_count; i++ )
1754             {
1755                 if ( f_value == val.p_list->p_values[i].f_float )
1756                     msg_rc( "| %f - %s *", val.p_list->p_values[i].f_float,
1757                             text.p_list->p_values[i].psz_string );
1758                 else
1759                     msg_rc( "| %f - %s", val.p_list->p_values[i].f_float,
1760                             text.p_list->p_values[i].psz_string );
1761             }
1762         }
1763         else
1764         {
1765             for ( i = 0; i < val.p_list->i_count; i++ )
1766             {
1767                 if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1768                     msg_rc( "| %s - %s *", val.p_list->p_values[i].psz_string,
1769                             text.p_list->p_values[i].psz_string );
1770                 else
1771                     msg_rc( "| %s - %s", val.p_list->p_values[i].psz_string,
1772                             text.p_list->p_values[i].psz_string );
1773             }
1774             free( psz_value );
1775         }
1776         var_Change( p_vout, psz_variable, VLC_VAR_FREELIST,
1777                     &val, &text );
1778         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1779
1780         if( val_name.psz_string ) free( val_name.psz_string );
1781
1782         i_error = VLC_SUCCESS;
1783     }
1784     vlc_object_release( p_vout );
1785     return i_error;
1786 }
1787
1788 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1789                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1790 {
1791     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1792     input_thread_t *p_input = NULL;
1793     aout_instance_t * p_aout;
1794     const char * psz_variable;
1795     vlc_value_t val_name;
1796     int i_error;
1797
1798     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1799     if( !p_input )
1800         return VLC_ENOOBJ;
1801
1802     if( p_input )
1803     {
1804         vlc_value_t val;
1805
1806         var_Get( p_input, "state", &val );
1807         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )        {
1808             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1809             vlc_object_release( p_input );
1810             return VLC_EGENERIC;
1811         }
1812         vlc_object_release( p_input );
1813     }
1814
1815     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1816     if ( p_aout == NULL ) return VLC_ENOOBJ;
1817
1818     if ( !strcmp( psz_cmd, "adev" ) )
1819     {
1820         psz_variable = "audio-device";
1821     }
1822     else
1823     {
1824         psz_variable = "audio-channels";
1825     }
1826
1827     /* Get the descriptive name of the variable */
1828     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1829                  &val_name, NULL );
1830     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1831
1832     if ( !*newval.psz_string )
1833     {
1834         /* Retrieve all registered ***. */
1835         vlc_value_t val, text;
1836         int i, i_value;
1837
1838         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1839         {
1840             vlc_object_release( (vlc_object_t *)p_aout );
1841             return VLC_EGENERIC;
1842         }
1843         i_value = val.i_int;
1844
1845         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1846                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1847         {
1848             vlc_object_release( (vlc_object_t *)p_aout );
1849             return VLC_EGENERIC;
1850         }
1851
1852         msg_rc( "+----[ %s ]", val_name.psz_string );
1853         for ( i = 0; i < val.p_list->i_count; i++ )
1854         {
1855             if ( i_value == val.p_list->p_values[i].i_int )
1856                 msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1857                         text.p_list->p_values[i].psz_string );
1858             else
1859                 msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1860                         text.p_list->p_values[i].psz_string );
1861         }
1862         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1863                     &val, &text );
1864         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1865
1866         if( val_name.psz_string ) free( val_name.psz_string );
1867         i_error = VLC_SUCCESS;
1868     }
1869     else
1870     {
1871         vlc_value_t val;
1872         val.i_int = atoi( newval.psz_string );
1873
1874         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1875     }
1876     vlc_object_release( (vlc_object_t *)p_aout );
1877
1878     return i_error;
1879 }
1880
1881 /* OSD menu commands */
1882 static int Menu( vlc_object_t *p_this, char const *psz_cmd,
1883     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1884 {
1885     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1886     playlist_t    *p_playlist = NULL;
1887     vlc_value_t val;
1888     int i_error = VLC_EGENERIC;
1889
1890     if ( !*newval.psz_string )
1891     {
1892         msg_rc( _("Please provide one of the following parameters:") );
1893         msg_rc( "[on|off|up|down|left|right|select]" );
1894         return i_error;
1895     }
1896
1897     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1898     if( !p_playlist )
1899         return VLC_ENOOBJ;
1900
1901     if( p_playlist->p_input )
1902     {
1903         var_Get( p_playlist->p_input, "state", &val );
1904         if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1905             ( strcmp( newval.psz_string, "select" ) != 0 ) )
1906         {
1907             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1908             vlc_object_release( p_playlist );
1909             return VLC_EGENERIC;
1910         }
1911     }
1912     vlc_object_release( p_playlist );
1913
1914     val.psz_string = strdup( newval.psz_string );
1915     if( !strcmp( val.psz_string, "on" ) || !strcmp( val.psz_string, "show" ))
1916         osd_MenuShow( p_this );
1917     else if( !strcmp( val.psz_string, "off" ) || !strcmp( val.psz_string, "hide" ) )
1918         osd_MenuHide( p_this );
1919     else if( !strcmp( val.psz_string, "up" ) )
1920         osd_MenuUp( p_this );
1921     else if( !strcmp( val.psz_string, "down" ) )
1922         osd_MenuDown( p_this );
1923     else if( !strcmp( val.psz_string, "left" ) )
1924         osd_MenuPrev( p_this );
1925     else if( !strcmp( val.psz_string, "right" ) )
1926         osd_MenuNext( p_this );
1927     else if( !strcmp( val.psz_string, "select" ) )
1928         osd_MenuActivate( p_this );
1929     else
1930     {
1931         msg_rc( _("Please provide one of the following parameters:") );
1932         msg_rc( "[on|off|up|down|left|right|select]" );
1933         if( val.psz_string ) free( val.psz_string );
1934             return i_error;
1935     }
1936
1937     i_error = VLC_SUCCESS;
1938     if( val.psz_string ) free( val.psz_string );
1939     return i_error;
1940 }
1941
1942 #ifdef WIN32
1943 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1944 {
1945     INPUT_RECORD input_record;
1946     DWORD i_dw;
1947
1948     /* On Win32, select() only works on socket descriptors */
1949     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1950                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1951     {
1952         while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
1953                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1954                                  1, &i_dw ) )
1955         {
1956             if( input_record.EventType != KEY_EVENT ||
1957                 !input_record.Event.KeyEvent.bKeyDown ||
1958                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1959                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1960                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1961                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1962             {
1963                 /* nothing interesting */
1964                 continue;
1965             }
1966
1967             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1968
1969             /* Echo out the command */
1970             putc( p_buffer[ *pi_size ], stdout );
1971
1972             /* Handle special keys */
1973             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1974             {
1975                 putc( '\n', stdout );
1976                 break;
1977             }
1978             switch( p_buffer[ *pi_size ] )
1979             {
1980             case '\b':
1981                 if( *pi_size )
1982                 {
1983                     *pi_size -= 2;
1984                     putc( ' ', stdout );
1985                     putc( '\b', stdout );
1986                 }
1987                 break;
1988             case '\r':
1989                 (*pi_size) --;
1990                 break;
1991             }
1992
1993             (*pi_size)++;
1994         }
1995
1996         if( *pi_size == MAX_LINE_LENGTH ||
1997             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1998         {
1999             p_buffer[ *pi_size ] = 0;
2000             return VLC_TRUE;
2001         }
2002     }
2003
2004     return VLC_FALSE;
2005 }
2006 #endif
2007
2008 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2009 {
2010     int i_read = 0;
2011
2012 #ifdef WIN32
2013     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
2014         return ReadWin32( p_intf, p_buffer, pi_size );
2015     else if( p_intf->p_sys->i_socket == -1 )
2016     {
2017         msleep( INTF_IDLE_SLEEP );
2018         return VLC_FALSE;
2019     }
2020 #endif
2021
2022     while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
2023            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
2024                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
2025                   (uint8_t *)p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
2026     {
2027         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2028             break;
2029
2030         (*pi_size)++;
2031     }
2032
2033     /* Connection closed */
2034     if( i_read == -1 )
2035     {
2036         p_intf->p_sys->i_socket = -1;
2037         p_buffer[ *pi_size ] = 0;
2038         return VLC_TRUE;
2039     }
2040
2041     if( *pi_size == MAX_LINE_LENGTH ||
2042         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2043     {
2044         p_buffer[ *pi_size ] = 0;
2045         return VLC_TRUE;
2046     }
2047
2048     return VLC_FALSE;
2049 }
2050
2051 /*****************************************************************************
2052  * parse_MRL: build a input item from a full mrl
2053  *****************************************************************************
2054  * MRL format: "simplified-mrl [:option-name[=option-value]]"
2055  * We don't check for '"' or '\'', we just assume that a ':' that follows a
2056  * space is a new option. Should be good enough for our purpose.
2057  *****************************************************************************/
2058 static input_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
2059 {
2060 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
2061 #define SKIPTRAILINGSPACE( p, d ) \
2062     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
2063
2064     input_item_t *p_item = NULL;
2065     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
2066     char **ppsz_options = NULL;
2067     int i, i_options = 0;
2068
2069     if( !psz_mrl ) return 0;
2070
2071     psz_mrl = psz_orig = strdup( psz_mrl );
2072     while( *psz_mrl )
2073     {
2074         SKIPSPACE( psz_mrl );
2075         psz_item = psz_mrl;
2076
2077         for( ; *psz_mrl; psz_mrl++ )
2078         {
2079             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
2080             {
2081                 /* We have a complete item */
2082                 break;
2083             }
2084             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
2085                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
2086             {
2087                 /* We have a complete item */
2088                 break;
2089             }
2090         }
2091
2092         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
2093         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
2094
2095         /* Remove '"' and '\'' if necessary */
2096         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
2097         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2098         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
2099         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2100
2101         if( !psz_item_mrl ) psz_item_mrl = psz_item;
2102         else if( *psz_item )
2103         {
2104             i_options++;
2105             ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
2106             ppsz_options[i_options - 1] = &psz_item[1];
2107         }
2108
2109         if( *psz_mrl ) SKIPSPACE( psz_mrl );
2110     }
2111
2112     /* Now create a playlist item */
2113     if( psz_item_mrl )
2114     {
2115         p_item = input_ItemNew( p_intf, psz_item_mrl, NULL );
2116         for( i = 0; i < i_options; i++ )
2117         {
2118             input_ItemAddOption( p_item, ppsz_options[i] );
2119         }
2120     }
2121
2122     if( i_options ) free( ppsz_options );
2123     free( psz_orig );
2124
2125     return p_item;
2126 }
2127
2128 /*****************************************************************************
2129  * checkUpdates : check for updates
2130  ****************************************************************************/
2131 static void checkUpdates( intf_thread_t *p_intf, char *psz_arg )
2132 {
2133     update_iterator_t *p_uit;
2134     update_t *p_u = update_New( p_intf );
2135     if( p_u == NULL ) return;
2136     p_uit = update_iterator_New( p_u );
2137     if( p_uit )
2138     {
2139         int s = 0, t = 0;
2140
2141         if( strstr( psz_arg, "newer" ) )
2142             s |= UPDATE_RELEASE_STATUS_NEWER;
2143         if( strstr( psz_arg, "equal" ) )
2144             s |= UPDATE_RELEASE_STATUS_EQUAL;
2145         if( strstr( psz_arg, "older" ) )
2146             s |= UPDATE_RELEASE_STATUS_OLDER;
2147         if( s ) p_uit->i_rs = s;
2148         else p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
2149
2150         if( strstr( psz_arg, "undef" ) )
2151             t |= UPDATE_FILE_TYPE_UNDEF;
2152         if( strstr( psz_arg, "info" ) )
2153             t |= UPDATE_FILE_TYPE_INFO;
2154         if( strstr( psz_arg, "source" ) )
2155             t |= UPDATE_FILE_TYPE_SOURCE;
2156         if( strstr( psz_arg, "binary" ) )
2157             t |= UPDATE_FILE_TYPE_BINARY;
2158         if( strstr( psz_arg, "plugin" ) )
2159             t |= UPDATE_FILE_TYPE_PLUGIN;
2160         if( t ) p_uit->i_t = t;
2161
2162         update_Check( p_u, VLC_FALSE );
2163         update_iterator_Action( p_uit, UPDATE_MIRROR );
2164         msg_rc( "\nUsing mirror: %s (%s) [%s]",
2165                 p_uit->mirror.psz_name,
2166                 p_uit->mirror.psz_location,
2167                 p_uit->mirror.psz_type );
2168         while( (s = update_iterator_Action( p_uit, UPDATE_FILE )) != UPDATE_FAIL )
2169         {
2170             char *psz_tmp;
2171             if( s & UPDATE_RELEASE )
2172             {
2173                 switch( p_uit->release.i_status )
2174                 {
2175                     case UPDATE_RELEASE_STATUS_OLDER:
2176                         psz_tmp = strdup( "older" );
2177                         break;
2178                     case UPDATE_RELEASE_STATUS_EQUAL:
2179                         psz_tmp = strdup( "equal" );
2180                         break;
2181                     case UPDATE_RELEASE_STATUS_NEWER:
2182                         psz_tmp = strdup( "newer" );
2183                         break;
2184                     default:
2185                         psz_tmp = strdup( "?!?" );
2186                         break;
2187                 }
2188                 msg_rc( "\n+----[ VLC %s %s (%s) ] ",
2189                         p_uit->release.psz_version,
2190                         p_uit->release.psz_svn_revision,
2191                         psz_tmp );
2192                 free( psz_tmp );
2193             }
2194             switch( p_uit->file.i_type )
2195             {
2196                 case UPDATE_FILE_TYPE_UNDEF:
2197                     psz_tmp = strdup( "undef" );
2198                     break;
2199                 case UPDATE_FILE_TYPE_INFO:
2200                     psz_tmp = strdup( "info" );
2201                     break;
2202                 case UPDATE_FILE_TYPE_SOURCE:
2203                     psz_tmp = strdup( "source" );
2204                     break;
2205                 case UPDATE_FILE_TYPE_BINARY:
2206                     psz_tmp = strdup( "binary" );
2207                     break;
2208                 case UPDATE_FILE_TYPE_PLUGIN:
2209                     psz_tmp = strdup( "plugin" );
2210                     break;
2211                 default:
2212                     psz_tmp = strdup( "?!?" );
2213                     break;
2214             }
2215             msg_rc( "| %s (%s)", p_uit->file.psz_description, psz_tmp );
2216             free( psz_tmp );
2217             if( p_uit->file.l_size )
2218             {
2219                 if( p_uit->file.l_size > 1024 * 1024 * 1024 )
2220                     asprintf( &psz_tmp, "(%ld GB)",
2221                               p_uit->file.l_size / (1024*1024*1024) );
2222                 if( p_uit->file.l_size > 1024 * 1024 )
2223                     asprintf( &psz_tmp, "(%ld MB)",
2224                               p_uit->file.l_size / (1024*1024) );
2225                 else if( p_uit->file.l_size > 1024 )
2226                     asprintf( &psz_tmp, "(%ld kB)",
2227                               p_uit->file.l_size / 1024 );
2228                 else
2229                     asprintf( &psz_tmp, "(%ld B)", p_uit->file.l_size );
2230             }
2231             else
2232             {
2233                 psz_tmp = strdup( "" );
2234             }
2235             msg_rc( "| %s %s", p_uit->file.psz_url, psz_tmp );
2236             msg_rc( "+----" );
2237             free( psz_tmp );
2238         }
2239         msg_rc( "" );
2240         update_iterator_Delete( p_uit );
2241     }
2242     update_Delete( p_u );
2243 }