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