]> git.sesse.net Git - vlc/blob - modules/control/rc.c
More cleanup
[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, i_overwrite;
224
225         i_overwrite = config_GetInt( p_intf, "rc-overwrite" );
226
227 #if !defined(AF_LOCAL) || defined(WIN32)
228         msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
229         free( psz_unix_path );
230         return VLC_EGENERIC;
231 #else
232         struct sockaddr_un addr;
233
234         memset( &addr, 0, sizeof(struct sockaddr_un) );
235
236         msg_Dbg( p_intf, "trying UNIX socket" );
237
238         if( (i_socket = socket( AF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
239         {
240             msg_Warn( p_intf, "can't open socket: %s", strerror(errno) );
241             free( psz_unix_path );
242             return VLC_EGENERIC;
243         }
244
245         addr.sun_family = AF_LOCAL;
246         strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
247         addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
248
249         if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
250          && (errno == EADDRINUSE)
251          && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
252          && (errno == ECONNREFUSED))
253         {
254             msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
255             unlink (psz_unix_path);
256
257             if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
258             {
259                 msg_Err (p_intf, "cannot bind UNIX socket at %s: %s",
260                          psz_unix_path, strerror (errno));
261                 free (psz_unix_path);
262                 net_Close (i_socket);
263                 return VLC_EGENERIC;
264             }
265         }
266
267         if( listen( i_socket, 1 ) )
268         {
269             msg_Warn( p_intf, "can't listen on socket: %s", strerror(errno));
270             free( psz_unix_path );
271             net_Close( i_socket );
272             return VLC_EGENERIC;
273         }
274
275         /* FIXME: we need a core function to merge listening sockets sets */
276         pi_socket = calloc( 2, sizeof( int ) );
277         if( pi_socket == NULL )
278         {
279             free( psz_unix_path );
280             net_Close( i_socket );
281             return VLC_ENOMEM;
282         }
283         pi_socket[0] = i_socket;
284         pi_socket[1] = -1;
285 #endif
286     }
287
288     if( ( pi_socket == NULL ) &&
289         ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
290     {
291         vlc_url_t url;
292
293         vlc_UrlParse( &url, psz_host, 0 );
294
295         msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
296
297         pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
298         if( pi_socket == NULL )
299         {
300             msg_Warn( p_intf, "can't listen to %s port %i",
301                       url.psz_host, url.i_port );
302             vlc_UrlClean( &url );
303             free( psz_host );
304             return VLC_EGENERIC;
305         }
306
307         vlc_UrlClean( &url );
308         free( psz_host );
309     }
310
311     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
312     if( !p_intf->p_sys )
313     {
314         msg_Err( p_intf, "no memory" );
315         return VLC_ENOMEM;
316     }
317
318     p_intf->p_sys->pi_socket_listen = pi_socket;
319     p_intf->p_sys->i_socket = -1;
320     p_intf->p_sys->psz_unix_path = psz_unix_path;
321     vlc_mutex_init( p_intf, &p_intf->p_sys->status_lock );
322     p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
323
324     /* Non-buffered stdout */
325     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
326
327     p_intf->pf_run = Run;
328
329 #ifdef WIN32
330     p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
331     if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
332 #else
333     CONSOLE_INTRO_MSG;
334 #endif
335
336     msg_rc( _("Remote control interface initialized. Type `help' for help.") );
337     return VLC_SUCCESS;
338 }
339
340 /*****************************************************************************
341  * Deactivate: uninitialize and cleanup
342  *****************************************************************************/
343 static void Deactivate( vlc_object_t *p_this )
344 {
345     intf_thread_t *p_intf = (intf_thread_t*)p_this;
346
347     net_ListenClose( p_intf->p_sys->pi_socket_listen );
348     if( p_intf->p_sys->i_socket != -1 )
349         net_Close( p_intf->p_sys->i_socket );
350     if( p_intf->p_sys->psz_unix_path != NULL )
351     {
352 #if defined(AF_LOCAL) && !defined(WIN32)
353         unlink( p_intf->p_sys->psz_unix_path );
354 #endif
355         free( p_intf->p_sys->psz_unix_path );
356     }
357     vlc_mutex_destroy( &p_intf->p_sys->status_lock );
358     free( p_intf->p_sys );
359 }
360
361 /*****************************************************************************
362  * RegisterCallbacks: Register callbacks to dynamic variables
363  *****************************************************************************/
364 static void RegisterCallbacks( intf_thread_t *p_intf )
365 {
366     /* Register commands that will be cleaned up upon object destruction */
367 #define ADD( name, type, target )                                   \
368     var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
369     var_AddCallback( p_intf, name, target, NULL );
370     ADD( "quit", VOID, Quit )
371     ADD( "intf", STRING, Intf )
372
373     ADD( "add", STRING, Playlist )
374     ADD( "repeat", STRING, Playlist )
375     ADD( "loop", STRING, Playlist )
376     ADD( "enqueue", STRING, Playlist )
377     ADD( "playlist", VOID, Playlist )
378     ADD( "sort", VOID, Playlist )
379     ADD( "play", VOID, Playlist )
380     ADD( "stop", VOID, Playlist )
381     ADD( "clear", VOID, Playlist )
382     ADD( "prev", VOID, Playlist )
383     ADD( "next", VOID, Playlist )
384     ADD( "goto", INTEGER, Playlist )
385     ADD( "status", INTEGER, Playlist )
386
387     /* marquee on the fly items */
388     ADD( "marq-marquee", STRING, Other )
389     ADD( "marq-x", INTEGER, Other )
390     ADD( "marq-y", INTEGER, Other )
391     ADD( "marq-position", INTEGER, Other )
392     ADD( "marq-color", INTEGER, Other )
393     ADD( "marq-opacity", INTEGER, Other )
394     ADD( "marq-timeout", INTEGER, Other )
395     ADD( "marq-size", INTEGER, Other )
396
397     ADD( "mosaic-alpha", INTEGER, Other )
398     ADD( "mosaic-height", INTEGER, Other )
399     ADD( "mosaic-width", INTEGER, Other )
400     ADD( "mosaic-xoffset", INTEGER, Other )
401     ADD( "mosaic-yoffset", INTEGER, Other )
402     ADD( "mosaic-offsets", STRING, Other )
403     ADD( "mosaic-align", INTEGER, Other )
404     ADD( "mosaic-vborder", INTEGER, Other )
405     ADD( "mosaic-hborder", INTEGER, Other )
406     ADD( "mosaic-position", INTEGER, Other )
407     ADD( "mosaic-rows", INTEGER, Other )
408     ADD( "mosaic-cols", INTEGER, Other )
409     ADD( "mosaic-order", STRING, Other )
410     ADD( "mosaic-keep-aspect-ratio", INTEGER, Other )
411
412     /* logo on the fly items */
413     ADD( "logo-file", STRING, Other )
414     ADD( "logo-x", INTEGER, Other )
415     ADD( "logo-y", INTEGER, Other )
416     ADD( "logo-position", INTEGER, Other )
417     ADD( "logo-transparency", INTEGER, Other )
418
419     /* OSD menu commands */
420     ADD(  "menu", STRING, Menu )
421
422     /* DVD commands */
423     ADD( "pause", VOID, Input )
424     ADD( "seek", INTEGER, Input )
425     ADD( "title", STRING, Input )
426     ADD( "title_n", VOID, Input )
427     ADD( "title_p", VOID, Input )
428     ADD( "chapter", STRING, Input )
429     ADD( "chapter_n", VOID, Input )
430     ADD( "chapter_p", VOID, Input )
431
432     ADD( "fastforward", VOID, Input )
433     ADD( "rewind", VOID, Input )
434     ADD( "faster", VOID, Input )
435     ADD( "slower", VOID, Input )
436     ADD( "normal", VOID, Input )
437
438     ADD( "atrack", STRING, Input )
439     ADD( "vtrack", STRING, Input )
440     ADD( "strack", STRING, Input )
441
442     /* video commands */
443     ADD( "vratio", STRING, VideoConfig )
444     ADD( "vcrop", STRING, VideoConfig )
445     ADD( "vzoom", STRING, VideoConfig )
446
447     /* audio commands */
448     ADD( "volume", STRING, Volume )
449     ADD( "volup", STRING, VolumeMove )
450     ADD( "voldown", STRING, VolumeMove )
451     ADD( "adev", STRING, AudioConfig )
452     ADD( "achan", STRING, AudioConfig )
453
454 #undef ADD
455 }
456
457 /*****************************************************************************
458  * Run: rc thread
459  *****************************************************************************
460  * This part of the interface is in a separate thread so that we can call
461  * exec() from within it without annoying the rest of the program.
462  *****************************************************************************/
463 static void Run( intf_thread_t *p_intf )
464 {
465     input_thread_t * p_input;
466     playlist_t *     p_playlist;
467
468     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
469     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
470     vlc_bool_t b_longhelp = VLC_FALSE;
471
472     int        i_size = 0;
473     int        i_oldpos = 0;
474     int        i_newpos;
475
476     p_buffer[0] = 0;
477     p_input = NULL;
478     p_playlist = NULL;
479
480     /* Register commands that will be cleaned up upon object destruction */
481     RegisterCallbacks( p_intf );
482
483     /* status callbacks */
484     /* Listen to audio volume updates */
485     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
486
487 #ifdef WIN32
488     /* Get the file descriptor of the console input */
489     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
490     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
491     {
492         msg_Err( p_intf, "couldn't find user input handle" );
493         p_intf->b_die = VLC_TRUE;
494     }
495 #endif
496
497     while( !intf_ShouldDie( p_intf ) )
498     {
499         char *psz_cmd, *psz_arg;
500         vlc_bool_t b_complete;
501
502         if( p_intf->p_sys->pi_socket_listen != NULL &&
503             p_intf->p_sys->i_socket == -1 )
504         {
505             p_intf->p_sys->i_socket =
506                 net_Accept( p_intf, p_intf->p_sys->pi_socket_listen, 0 );
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 switch( psz_cmd[0] )
758         {
759         case 'f':
760         case 'F':
761             if( p_input )
762             {
763                 vout_thread_t *p_vout;
764                 p_vout = vlc_object_find( p_input,
765                                           VLC_OBJECT_VOUT, FIND_CHILD );
766
767                 if( p_vout )
768                 {
769                     vlc_value_t val;
770                     vlc_bool_t b_update = VLC_FALSE;
771                     var_Get( p_vout, "fullscreen", &val );
772                     val.b_bool = !val.b_bool;
773                     if( !strncmp( psz_arg, "on", 2 )
774                         && ( val.b_bool == VLC_TRUE ) )
775                     {
776                         b_update = VLC_TRUE;
777                         val.b_bool = VLC_TRUE;
778                     }
779                     else if( !strncmp( psz_arg, "off", 3 )
780                              && ( val.b_bool == VLC_FALSE ) )
781                     {
782                         b_update = VLC_TRUE;
783                         val.b_bool = VLC_FALSE;
784                     }
785                     else if( strncmp( psz_arg, "off", 3 )
786                              && strncmp( psz_arg, "on", 2 ) )
787                         b_update = VLC_TRUE;
788                     if( b_update ) var_Set( p_vout, "fullscreen", val );
789                     vlc_object_release( p_vout );
790                 }
791             }
792             break;
793
794         case 's':
795         case 'S':
796             ;
797             break;
798
799         case '\0':
800             /* Ignore empty lines */
801             break;
802
803         default:
804             msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
805             break;
806         }
807
808         /* Command processed */
809         i_size = 0; p_buffer[0] = 0;
810     }
811
812     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
813     msg_rc( STATUS_CHANGE "( quit )" );
814
815     if( p_input )
816     {
817         var_DelCallback( p_input, "state", StateChanged, p_intf );
818         var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
819         var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
820         var_DelCallback( p_input, "rate", RateChanged, p_intf );
821         var_DelCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
822         vlc_object_release( p_input );
823         p_input = NULL;
824     }
825
826     if( p_playlist )
827     {
828         vlc_object_release( p_playlist );
829         p_playlist = NULL;
830     }
831
832     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, p_intf );
833 }
834
835 static void Help( intf_thread_t *p_intf, vlc_bool_t b_longhelp)
836 {
837     msg_rc(_("+----[ Remote control commands ]"));
838     msg_rc(  "| ");
839     msg_rc(_("| add XYZ  . . . . . . . . . . add XYZ to playlist"));
840     msg_rc(_("| enqueue XYZ  . . . . . . . queue XYZ to playlist"));
841     msg_rc(_("| playlist . . .  show items currently in playlist"));
842     msg_rc(_("| play . . . . . . . . . . . . . . . . play stream"));
843     msg_rc(_("| stop . . . . . . . . . . . . . . . . stop stream"));
844     msg_rc(_("| next . . . . . . . . . . . .  next playlist item"));
845     msg_rc(_("| prev . . . . . . . . . .  previous playlist item"));
846     msg_rc(_("| goto . . . . . . . . . . . .  goto item at index"));
847     msg_rc(_("| repeat [on|off] . .  toggle playlist item repeat"));
848     msg_rc(_("| loop [on|off] . . . .  toggle playlist item loop"));
849     msg_rc(_("| clear . . . . . . . . . . .   clear the playlist"));
850     msg_rc(_("| status . . . . . . . . . current playlist status"));
851     msg_rc(_("| title [X]  . . . . set/get title in current item"));
852     msg_rc(_("| title_n  . . . . . .  next title in current item"));
853     msg_rc(_("| title_p  . . . .  previous title in current item"));
854     msg_rc(_("| chapter [X]  . . set/get chapter in current item"));
855     msg_rc(_("| chapter_n  . . . .  next chapter in current item"));
856     msg_rc(_("| chapter_p  . .  previous chapter in current item"));
857     msg_rc(  "| ");
858     msg_rc(_("| seek X . seek in seconds, for instance `seek 12'"));
859     msg_rc(_("| pause  . . . . . . . . . . . . . .  toggle pause"));
860     msg_rc(_("| fastforward  . . . . . .  .  set to maximum rate"));
861     msg_rc(_("| rewind  . . . . . . . . . .  set to minimum rate"));
862     msg_rc(_("| faster . . . . . . . .  faster playing of stream"));
863     msg_rc(_("| slower . . . . . . . .  slower playing of stream"));
864     msg_rc(_("| normal . . . . . . . .  normal playing of stream"));
865     msg_rc(_("| f [on|off] . . . . . . . . . . toggle fullscreen"));
866     msg_rc(_("| info . . .  information about the current stream"));
867     msg_rc(_("| get_time . . seconds elapsed since stream's beginning"));
868     msg_rc(_("| is_playing . .  1 if a stream plays, 0 otherwise"));
869     msg_rc(_("| get_title . . .  the title of the current stream"));
870     msg_rc(_("| get_length . .  the length of the current stream"));
871     msg_rc(  "| ");
872     msg_rc(_("| volume [X] . . . . . . . .  set/get audio volume"));
873     msg_rc(_("| volup [X]  . . . . .  raise audio volume X steps"));
874     msg_rc(_("| voldown [X]  . . . .  lower audio volume X steps"));
875     msg_rc(_("| adev [X] . . . . . . . . .  set/get audio device"));
876     msg_rc(_("| achan [X]. . . . . . . .  set/get audio channels"));
877     msg_rc(_("| atrack [X] . . . . . . . . . set/get audio track"));
878     msg_rc(_("| vtrack [X] . . . . . . . . . set/get video track"));
879     msg_rc(_("| vratio [X]  . . . . . set/get video aspect ratio"));
880     msg_rc(_("| vcrop [X]  . . . . . . . . .  set/get video crop"));
881     msg_rc(_("| vzoom [X]  . . . . . . . . .  set/get video zoom"));
882     msg_rc(_("| strack [X] . . . . . . . set/get subtitles track"));
883     msg_rc(_("| menu [on|off|up|down|left|right|select] use menu"));
884     msg_rc(  "| ");
885
886     if (b_longhelp)
887     {
888         msg_rc(_("| marq-marquee STRING  . . overlay STRING in video"));
889         msg_rc(_("| marq-x X . . . . . . . . . . . .offset from left"));
890         msg_rc(_("| marq-y Y . . . . . . . . . . . . offset from top"));
891         msg_rc(_("| marq-position #. . .  .relative position control"));
892         msg_rc(_("| marq-color # . . . . . . . . . . font color, RGB"));
893         msg_rc(_("| marq-opacity # . . . . . . . . . . . . . opacity"));
894         msg_rc(_("| marq-timeout T. . . . . . . . . . timeout, in ms"));
895         msg_rc(_("| marq-size # . . . . . . . . font size, in pixels"));
896         msg_rc(  "| ");
897         msg_rc(_("| logo-file STRING . . .the overlay file path/name"));
898         msg_rc(_("| logo-x X . . . . . . . . . . . .offset from left"));
899         msg_rc(_("| logo-y Y . . . . . . . . . . . . offset from top"));
900         msg_rc(_("| logo-position #. . . . . . . . relative position"));
901         msg_rc(_("| logo-transparency #. . . . . . . . .transparency"));
902         msg_rc(  "| ");
903         msg_rc(_("| mosaic-alpha # . . . . . . . . . . . . . . alpha"));
904         msg_rc(_("| mosaic-height #. . . . . . . . . . . . . .height"));
905         msg_rc(_("| mosaic-width # . . . . . . . . . . . . . . width"));
906         msg_rc(_("| mosaic-xoffset # . . . .top left corner position"));
907         msg_rc(_("| mosaic-yoffset # . . . .top left corner position"));
908         msg_rc(_("| mosaic-offsets x,y(,x,y)*. . . . list of offsets"));
909         msg_rc(_("| mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
910         msg_rc(_("| mosaic-vborder # . . . . . . . . vertical border"));
911         msg_rc(_("| mosaic-hborder # . . . . . . . horizontal border"));
912         msg_rc(_("| mosaic-position {0=auto,1=fixed} . . . .position"));
913         msg_rc(_("| mosaic-rows #. . . . . . . . . . .number of rows"));
914         msg_rc(_("| mosaic-cols #. . . . . . . . . . .number of cols"));
915         msg_rc(_("| mosaic-order id(,id)* . . . . order of pictures "));
916         msg_rc(_("| mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
917         msg_rc(  "| ");
918         msg_rc(_("| check-updates [newer] [equal] [older]\n"
919                  "|               [undef] [info] [source] [binary] [plugin]"));
920         msg_rc(  "| ");
921     }
922     msg_rc(_("| help . . . . . . . . . . . . . this help message"));
923     msg_rc(_("| longhelp . . . . . . . . . a longer help message"));
924     msg_rc(_("| logout . . . . .  exit (if in socket connection)"));
925     msg_rc(_("| quit . . . . . . . . . . . . . . . . .  quit vlc"));
926     msg_rc(  "| ");
927     msg_rc(_("+----[ end of help ]"));
928 }
929
930 /********************************************************************
931  * Status callback routines
932  ********************************************************************/
933 static int TimeOffsetChanged( vlc_object_t *p_this, char const *psz_cmd,
934     vlc_value_t oldval, vlc_value_t newval, void *p_data )
935 {
936     intf_thread_t *p_intf = (intf_thread_t*)p_data;
937     input_thread_t *p_input = NULL;
938
939     vlc_mutex_lock( &p_intf->p_sys->status_lock );
940     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
941     if( p_input )
942     {
943         msg_rc( STATUS_CHANGE "( time-offset: %d )",
944                 var_GetInteger( p_input, "time-offset" ) );
945         vlc_object_release( p_input );
946     }
947     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
948     return VLC_SUCCESS;
949 }
950
951 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
952     vlc_value_t oldval, vlc_value_t newval, void *p_data )
953 {
954     intf_thread_t *p_intf = (intf_thread_t*)p_data;
955
956     vlc_mutex_lock( &p_intf->p_sys->status_lock );
957     msg_rc( STATUS_CHANGE "( audio volume: %d )",
958             config_GetInt( p_this, "volume") );
959     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
960     return VLC_SUCCESS;
961 }
962
963 static int StateChanged( vlc_object_t *p_this, char const *psz_cmd,
964     vlc_value_t oldval, vlc_value_t newval, void *p_data )
965 {
966     intf_thread_t *p_intf = (intf_thread_t*)p_data;
967     playlist_t    *p_playlist = NULL;
968     input_thread_t *p_input = NULL;
969
970     vlc_mutex_lock( &p_intf->p_sys->status_lock );
971     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
972     if( p_input )
973     {
974         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
975         if( p_playlist )
976         {
977             char cmd[5] = "";
978             switch( p_playlist->status.i_status )
979             {
980             case PLAYLIST_STOPPED:
981                 strncpy( &cmd[0], "stop", 4);
982                 cmd[4] = '\0';
983                 break;
984             case PLAYLIST_RUNNING:
985                 strncpy( &cmd[0], "play", 4);
986                 cmd[4] = '\0';
987                 break;
988             case PLAYLIST_PAUSED:
989                 strncpy( &cmd[0], "pause", 5);
990                 cmd[5] = '\0';
991                 break;
992             } /* var_GetInteger( p_input, "state" )  */
993             msg_rc( STATUS_CHANGE "( %s state: %d )", &cmd[0], newval.i_int );
994             vlc_object_release( p_playlist );
995         }
996         vlc_object_release( p_input );
997     }
998     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
999     return VLC_SUCCESS;
1000 }
1001
1002 static int RateChanged( vlc_object_t *p_this, char const *psz_cmd,
1003     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1004 {
1005     intf_thread_t *p_intf = (intf_thread_t*)p_data;
1006     input_thread_t *p_input = NULL;
1007
1008     vlc_mutex_lock( &p_intf->p_sys->status_lock );
1009     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1010     if( p_input )
1011     {
1012         msg_rc( STATUS_CHANGE "( new rate: %d )",
1013                 var_GetInteger( p_input, "rate" ) );
1014         vlc_object_release( p_input );
1015     }
1016     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
1017     return VLC_SUCCESS;
1018 }
1019
1020 /********************************************************************
1021  * Command routines
1022  ********************************************************************/
1023 static int Input( vlc_object_t *p_this, char const *psz_cmd,
1024                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
1025 {
1026     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1027     input_thread_t *p_input;
1028     vlc_value_t     val;
1029
1030     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1031     if( !p_input ) return VLC_ENOOBJ;
1032
1033     var_Get( p_input, "state", &val );
1034     if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1035         ( strcmp( psz_cmd, "pause" ) != 0 ) )
1036     {
1037         msg_rc( _("Press menu select or pause to continue.") );
1038         vlc_object_release( p_input );
1039         return VLC_EGENERIC;
1040     }
1041
1042     /* Parse commands that only require an input */
1043     if( !strcmp( psz_cmd, "pause" ) )
1044     {
1045         val.i_int = config_GetInt( p_intf, "key-play-pause" );
1046         var_Set( p_intf->p_libvlc, "key-pressed", val );
1047         vlc_object_release( p_input );
1048         return VLC_SUCCESS;
1049     }
1050     else if( !strcmp( psz_cmd, "seek" ) )
1051     {
1052         if( strlen( newval.psz_string ) > 0 &&
1053             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
1054         {
1055             val.f_float = (float)atof( newval.psz_string ) / 100.0;
1056             var_Set( p_input, "position", val );
1057         }
1058         else
1059         {
1060             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
1061             var_Set( p_input, "time", val );
1062         }
1063         vlc_object_release( p_input );
1064         return VLC_SUCCESS;
1065     }
1066     else if ( !strcmp( psz_cmd, "fastforward" ) )
1067     {
1068         val.i_int = config_GetInt( p_intf, "key-jump+extrashort" );
1069         var_Set( p_intf->p_libvlc, "key-pressed", val );
1070         vlc_object_release( p_input );
1071         return VLC_SUCCESS;
1072     }
1073     else if ( !strcmp( psz_cmd, "rewind" ) )
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, "faster" ) )
1081     {
1082         val.b_bool = VLC_TRUE;
1083         var_Set( p_input, "rate-faster", val );
1084         vlc_object_release( p_input );
1085         return VLC_SUCCESS;
1086     }
1087     else if ( !strcmp( psz_cmd, "slower" ) )
1088     {
1089         val.b_bool = VLC_TRUE;
1090         var_Set( p_input, "rate-slower", val );
1091         vlc_object_release( p_input );
1092         return VLC_SUCCESS;
1093     }
1094     else if ( !strcmp( psz_cmd, "normal" ) )
1095     {
1096         val.i_int = INPUT_RATE_DEFAULT;
1097         var_Set( p_input, "rate", val );
1098         vlc_object_release( p_input );
1099         return VLC_SUCCESS;
1100     }
1101     else if( !strcmp( psz_cmd, "chapter" ) ||
1102              !strcmp( psz_cmd, "chapter_n" ) ||
1103              !strcmp( psz_cmd, "chapter_p" ) )
1104     {
1105         if( !strcmp( psz_cmd, "chapter" ) )
1106         {
1107             if ( *newval.psz_string )
1108             {
1109                 /* Set. */
1110                 val.i_int = atoi( newval.psz_string );
1111                 var_Set( p_input, "chapter", val );
1112             }
1113             else
1114             {
1115                 vlc_value_t val_list;
1116
1117                 /* Get. */
1118                 var_Get( p_input, "chapter", &val );
1119                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
1120                             &val_list, NULL );
1121                 msg_rc( "Currently playing chapter %d/%d.",
1122                         val.i_int, val_list.p_list->i_count );
1123                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
1124                             &val_list, NULL );
1125             }
1126         }
1127         else if( !strcmp( psz_cmd, "chapter_n" ) )
1128         {
1129             val.b_bool = VLC_TRUE;
1130             var_Set( p_input, "next-chapter", val );
1131         }
1132         else if( !strcmp( psz_cmd, "chapter_p" ) )
1133         {
1134             val.b_bool = VLC_TRUE;
1135             var_Set( p_input, "prev-chapter", val );
1136         }
1137         vlc_object_release( p_input );
1138         return VLC_SUCCESS;
1139     }
1140     else if( !strcmp( psz_cmd, "title" ) ||
1141              !strcmp( psz_cmd, "title_n" ) ||
1142              !strcmp( psz_cmd, "title_p" ) )
1143     {
1144         if( !strcmp( psz_cmd, "title" ) )
1145         {
1146             if ( *newval.psz_string )
1147             {
1148                 /* Set. */
1149                 val.i_int = atoi( newval.psz_string );
1150                 var_Set( p_input, "title", val );
1151             }
1152             else
1153             {
1154                 vlc_value_t val_list;
1155
1156                 /* Get. */
1157                 var_Get( p_input, "title", &val );
1158                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
1159                             &val_list, NULL );
1160                 msg_rc( "Currently playing title %d/%d.",
1161                         val.i_int, val_list.p_list->i_count );
1162                 var_Change( p_this, "title", VLC_VAR_FREELIST,
1163                             &val_list, NULL );
1164             }
1165         }
1166         else if( !strcmp( psz_cmd, "title_n" ) )
1167         {
1168             val.b_bool = VLC_TRUE;
1169             var_Set( p_input, "next-title", val );
1170         }
1171         else if( !strcmp( psz_cmd, "title_p" ) )
1172         {
1173             val.b_bool = VLC_TRUE;
1174             var_Set( p_input, "prev-title", val );
1175         }
1176
1177         vlc_object_release( p_input );
1178         return VLC_SUCCESS;
1179     }
1180     else if(    !strcmp( psz_cmd, "atrack" )
1181              || !strcmp( psz_cmd, "vtrack" )
1182              || !strcmp( psz_cmd, "strack" ) )
1183     {
1184         const char *psz_variable;
1185         vlc_value_t val_name;
1186         int i_error;
1187
1188         if( !strcmp( psz_cmd, "atrack" ) )
1189         {
1190             psz_variable = "audio-es";
1191         }
1192         else if( !strcmp( psz_cmd, "vtrack" ) )
1193         {
1194             psz_variable = "video-es";
1195         }
1196         else
1197         {
1198             psz_variable = "spu-es";
1199         }
1200
1201         /* Get the descriptive name of the variable */
1202         var_Change( p_input, psz_variable, VLC_VAR_GETTEXT,
1203                      &val_name, NULL );
1204         if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1205
1206         if( newval.psz_string && *newval.psz_string )
1207         {
1208             /* set */
1209             vlc_value_t val;
1210             val.i_int = atoi( newval.psz_string );
1211
1212             i_error = var_Set( p_input, psz_variable, val );
1213         }
1214         else
1215         {
1216             /* get */
1217             vlc_value_t val, text;
1218             int i, i_value;
1219
1220             if ( var_Get( p_input, psz_variable, &val ) < 0 )
1221             {
1222                 vlc_object_release( p_input );
1223                 return VLC_EGENERIC;
1224             }
1225             i_value = val.i_int;
1226
1227             if ( var_Change( p_input, psz_variable,
1228                              VLC_VAR_GETLIST, &val, &text ) < 0 )
1229             {
1230                 vlc_object_release( p_input );
1231                 return VLC_EGENERIC;
1232             }
1233
1234             msg_rc( "+----[ %s ]", val_name.psz_string );
1235             for ( i = 0; i < val.p_list->i_count; i++ )
1236             {
1237                 if ( i_value == val.p_list->p_values[i].i_int )
1238                     msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1239                             text.p_list->p_values[i].psz_string );
1240                 else
1241                     msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1242                             text.p_list->p_values[i].psz_string );
1243             }
1244             var_Change( p_input, psz_variable, VLC_VAR_FREELIST,
1245                         &val, &text );
1246             msg_rc( "+----[ end of %s ]", val_name.psz_string );
1247
1248             if( val_name.psz_string ) free( val_name.psz_string );
1249
1250             i_error = VLC_SUCCESS;
1251         }
1252         vlc_object_release( p_input );
1253         return i_error;
1254     }
1255
1256     /* Never reached. */
1257     vlc_object_release( p_input );
1258     return VLC_EGENERIC;
1259 }
1260
1261 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1262                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1263 {
1264     vlc_value_t val;
1265
1266     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1267     playlist_t *p_playlist = pl_Yield( p_this );
1268
1269     PL_LOCK;
1270     if( p_playlist->p_input )
1271     {
1272         var_Get( p_playlist->p_input, "state", &val );
1273         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1274         {
1275             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1276             vlc_object_release( p_playlist );
1277             PL_UNLOCK;
1278             return VLC_EGENERIC;
1279         }
1280     }
1281     PL_UNLOCK;
1282
1283     /* Parse commands that require a playlist */
1284     if( !strcmp( psz_cmd, "prev" ) )
1285     {
1286         playlist_Prev( p_playlist );
1287     }
1288     else if( !strcmp( psz_cmd, "next" ) )
1289     {
1290         playlist_Next( p_playlist );
1291     }
1292     else if( !strcmp( psz_cmd, "play" ) )
1293     {
1294         msg_Warn( p_playlist, "play" );
1295         playlist_Play( p_playlist );
1296     }
1297     else if( !strcmp( psz_cmd, "repeat" ) )
1298     {
1299         vlc_bool_t b_update = VLC_TRUE;
1300
1301         var_Get( p_playlist, "repeat", &val );
1302
1303         if( strlen( newval.psz_string ) > 0 )
1304         {
1305             if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == VLC_TRUE ) ) ||
1306                  ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == VLC_FALSE ) ) )
1307             {
1308                 b_update = VLC_FALSE;
1309             }
1310         }
1311
1312         if ( b_update )
1313         {
1314             val.b_bool = !val.b_bool;
1315             var_Set( p_playlist, "repeat", val );
1316         }
1317         msg_rc( "Setting repeat to %d", val.b_bool );
1318     }
1319     else if( !strcmp( psz_cmd, "loop" ) )
1320     {
1321         vlc_bool_t b_update = VLC_TRUE;
1322
1323         var_Get( p_playlist, "loop", &val );
1324
1325         if( strlen( newval.psz_string ) > 0 )
1326         {
1327             if ( ( !strncmp( newval.psz_string, "on", 2 ) && ( val.b_bool == VLC_TRUE ) ) ||
1328                  ( !strncmp( newval.psz_string, "off", 3 ) && ( val.b_bool == VLC_FALSE ) ) )
1329             {
1330                 b_update = VLC_FALSE;
1331             }
1332         }
1333
1334         if ( b_update )
1335         {
1336             val.b_bool = !val.b_bool;
1337             var_Set( p_playlist, "loop", val );
1338         }
1339         msg_rc( "Setting loop to %d", val.b_bool );
1340     }
1341     else if (!strcmp( psz_cmd, "goto" ) )
1342     {
1343         msg_rc( _("goto is deprecated" ) );
1344         msg_Err( p_playlist, "goto is deprecated" );
1345     }
1346     else if( !strcmp( psz_cmd, "stop" ) )
1347     {
1348         playlist_Stop( p_playlist );
1349     }
1350     else if( !strcmp( psz_cmd, "clear" ) )
1351     {
1352         playlist_Stop( p_playlist );
1353         playlist_Clear( p_playlist, VLC_FALSE );
1354     }
1355     else if( !strcmp( psz_cmd, "add" ) &&
1356              newval.psz_string && *newval.psz_string )
1357     {
1358         input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1359
1360         if( p_item )
1361         {
1362             msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1363             playlist_AddInput( p_playlist, p_item,
1364                      PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END, VLC_TRUE );
1365         }
1366     }
1367     else if( !strcmp( psz_cmd, "enqueue" ) &&
1368              newval.psz_string && *newval.psz_string )
1369     {
1370         input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1371
1372         if( p_item )
1373         {
1374             msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1375             playlist_AddInput( p_playlist, p_item,
1376                                PLAYLIST_APPEND, PLAYLIST_END, VLC_TRUE );
1377         }
1378     }
1379     else if( !strcmp( psz_cmd, "playlist" ) )
1380     {
1381         msg_Dbg( p_playlist, "Dumping category" );
1382         playlist_NodeDump( p_playlist, p_playlist->p_root_category, 0 );
1383         msg_Dbg( p_playlist, "Dumping Onelevel" );
1384         playlist_NodeDump( p_playlist, p_playlist->p_root_onelevel, 0 );
1385     }
1386     else if( !strcmp( psz_cmd, "sort" ))
1387     {
1388         playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root_onelevel,
1389                                     SORT_ARTIST, ORDER_NORMAL );
1390     }
1391     else if( !strcmp( psz_cmd, "status" ) )
1392     {
1393         if( p_playlist->p_input )
1394         {
1395             /* Replay the current state of the system. */
1396             msg_rc( STATUS_CHANGE "( new input: %s )",
1397                     input_GetItem(p_playlist->p_input)->psz_uri );
1398             msg_rc( STATUS_CHANGE "( audio volume: %d )",
1399                     config_GetInt( p_intf, "volume" ));
1400
1401             vlc_mutex_lock( &p_playlist->object_lock );
1402             switch( p_playlist->status.i_status )
1403             {
1404                 case PLAYLIST_STOPPED:
1405                     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
1406                     break;
1407                 case PLAYLIST_RUNNING:
1408                     msg_rc( STATUS_CHANGE "( play state: 1 )" );
1409                     break;
1410                 case PLAYLIST_PAUSED:
1411                     msg_rc( STATUS_CHANGE "( pause state: 2 )" );
1412                     break;
1413                 default:
1414                     msg_rc( STATUS_CHANGE "( state unknown )" );
1415                     break;
1416             }
1417             vlc_mutex_unlock( &p_playlist->object_lock );
1418         }
1419     }
1420
1421     /*
1422      * sanity check
1423      */
1424     else
1425     {
1426         msg_rc( "unknown command!" );
1427     }
1428
1429     vlc_object_release( p_playlist );
1430     return VLC_SUCCESS;
1431 }
1432
1433 static int Other( vlc_object_t *p_this, char const *psz_cmd,
1434                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1435 {
1436     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1437     vlc_object_t  *p_playlist;
1438     vlc_value_t    val;
1439     vlc_object_t  *p_input;
1440
1441     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1442     if( !p_playlist )
1443     {
1444         return VLC_ENOOBJ;
1445     }
1446
1447     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1448     if( !p_input )
1449     {
1450         vlc_object_release( p_playlist );
1451         return VLC_ENOOBJ;
1452     }
1453
1454     if( p_input )
1455     {
1456         var_Get( p_input, "state", &val );
1457         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1458         {
1459             msg_rc( _("Type 'pause' to continue.") );
1460             vlc_object_release( p_playlist );
1461             vlc_object_release( p_input );
1462             return VLC_EGENERIC;
1463         }
1464     }
1465
1466     /* Parse miscellaneous commands */
1467     {
1468         static const char vars[] =
1469             "marq-x\0" "marq-y\0" "marq-position\0" "marq-color\0"
1470             "marq-opacity\0" "marq-size\0" "marq-timeout\0"
1471             "mosaic-alpha\0" "mosaic-height\0" "mosaic-width\0"
1472             "mosaic-xoffset\0" "mosaic-yoffset\0" "mosaic-align\0"
1473             "mosaic-vborder\0" "mosaic-hborder\0" "mosaic-position\0"
1474             "mosaic-rows\0" "mosaic-cols\0" "mosaic-order\0"
1475             "mosaic-offsets\0" "mosaic-keep-aspect-ratio\0"
1476             "logo-file\0" "logo-x\0" "logo-y\0" "logo-position\0"
1477             "logo-transparency\0";
1478         const char *psz_name = NULL;
1479
1480         if( newval.psz_string )
1481         for( psz_name = vars; *psz_name; psz_name += strlen( psz_name ) + 1 )
1482         {
1483             if( strcmp( psz_name, psz_cmd ) == 0 )
1484             {
1485                 int i_type = var_Type( p_input->p_libvlc_global, psz_name );
1486
1487                 if( i_type & VLC_VAR_INTEGER )
1488                 {
1489                     var_SetInteger( p_input->p_libvlc_global, psz_name,
1490                                     atoi( newval.psz_string ) );
1491                     break;
1492                 }
1493                 else if( i_type & VLC_VAR_STRING )
1494                 {
1495                     var_SetString( p_input->p_libvlc_global, psz_name,
1496                                    newval.psz_string );
1497                     break;
1498                 }
1499             }
1500         }
1501
1502         if( *psz_name == '\0' )
1503             msg_rc( "Unknown command!" );
1504     }
1505
1506     vlc_object_release( p_playlist );
1507     vlc_object_release( p_input );
1508     return VLC_SUCCESS;
1509 }
1510
1511 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1512                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1513 {
1514     playlist_t *p_playlist;
1515
1516     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1517     if( p_playlist )
1518     {
1519         playlist_Stop( p_playlist );
1520         vlc_object_release( p_playlist );
1521     }
1522     p_this->p_libvlc->b_die = VLC_TRUE;
1523     return VLC_SUCCESS;
1524 }
1525
1526 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1527                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1528 {
1529     intf_thread_t *p_newintf = NULL;
1530
1531     p_newintf = intf_Create( p_this->p_libvlc, newval.psz_string, 0, NULL );
1532     if( p_newintf )
1533     {
1534         p_newintf->b_block = VLC_FALSE;
1535         if( intf_RunThread( p_newintf ) )
1536         {
1537             vlc_object_detach( p_newintf );
1538             intf_Destroy( p_newintf );
1539         }
1540     }
1541
1542     return VLC_SUCCESS;
1543 }
1544
1545 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1546                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
1547 {
1548     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1549     input_thread_t *p_input = NULL;
1550     int i_error = VLC_EGENERIC;
1551
1552     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1553     if( !p_input )
1554         return VLC_ENOOBJ;
1555
1556     if( p_input )
1557     {
1558         vlc_value_t val;
1559
1560         var_Get( p_input, "state", &val );
1561         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1562         {
1563             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1564             vlc_object_release( p_input );
1565             return VLC_EGENERIC;
1566         }
1567         vlc_object_release( p_input );
1568     }
1569
1570     if ( *newval.psz_string )
1571     {
1572         /* Set. */
1573         audio_volume_t i_volume = atoi( newval.psz_string );
1574         if ( (i_volume > (audio_volume_t)AOUT_VOLUME_MAX) )
1575         {
1576             msg_rc( "Volume must be in the range %d-%d.", AOUT_VOLUME_MIN,
1577                     AOUT_VOLUME_MAX );
1578             i_error = VLC_EBADVAR;
1579         }
1580         else
1581         {
1582             if( i_volume == AOUT_VOLUME_MIN )
1583             {
1584                 vlc_value_t keyval;
1585
1586                 keyval.i_int = config_GetInt( p_intf, "key-vol-mute" );
1587                 var_Set( p_intf->p_libvlc, "key-pressed", keyval );
1588             }
1589             i_error = aout_VolumeSet( p_this, i_volume );
1590             osd_Volume( p_this );
1591             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1592         }
1593     }
1594     else
1595     {
1596         /* Get. */
1597         audio_volume_t i_volume;
1598         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1599         {
1600             i_error = VLC_EGENERIC;
1601         }
1602         else
1603         {
1604             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1605             i_error = VLC_SUCCESS;
1606         }
1607     }
1608
1609     return i_error;
1610 }
1611
1612 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1613                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1614 {
1615     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1616     audio_volume_t i_volume;
1617     input_thread_t *p_input = NULL;
1618     int i_nb_steps = atoi(newval.psz_string);
1619     int i_error = VLC_SUCCESS;
1620     int i_volume_step = 0;
1621
1622     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1623     if( !p_input )
1624         return VLC_ENOOBJ;
1625
1626     if( p_input )
1627     {
1628         vlc_value_t val;
1629
1630         var_Get( p_input, "state", &val );
1631         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1632         {
1633             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1634             vlc_object_release( p_input );
1635             return VLC_EGENERIC;
1636         }
1637         vlc_object_release( p_input );
1638     }
1639
1640     i_volume_step = config_GetInt( p_intf->p_libvlc, "volume-step" );
1641     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/i_volume_step) )
1642     {
1643         i_nb_steps = 1;
1644     }
1645
1646     if ( !strcmp(psz_cmd, "volup") )
1647     {
1648         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1649             i_error = VLC_EGENERIC;
1650     }
1651     else
1652     {
1653         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1654             i_error = VLC_EGENERIC;
1655     }
1656     osd_Volume( p_this );
1657
1658     if ( !i_error ) msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1659     return i_error;
1660 }
1661
1662
1663 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1664                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1665 {
1666     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1667     input_thread_t *p_input = NULL;
1668     vout_thread_t * p_vout;
1669     const char * psz_variable;
1670     vlc_value_t val_name;
1671     int i_error;
1672
1673     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1674     if( !p_input )
1675         return VLC_ENOOBJ;
1676
1677     p_vout = vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
1678     vlc_object_release( p_input );
1679     if( !p_vout )
1680         return VLC_ENOOBJ;
1681
1682     if( !strcmp( psz_cmd, "vcrop" ) )
1683     {
1684         psz_variable = "crop";
1685     }
1686     else if( !strcmp( psz_cmd, "vratio" ) )
1687     {
1688         psz_variable = "aspect-ratio";
1689     }
1690     else /* if( !strcmp( psz_cmd, "vzoom" ) ) */
1691     {
1692         psz_variable = "zoom";
1693     }
1694
1695
1696     /* Get the descriptive name of the variable */
1697     var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT,
1698                  &val_name, NULL );
1699     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1700
1701     if( newval.psz_string && *newval.psz_string )
1702     {
1703         /* set */
1704         if( !strcmp( psz_variable, "zoom" ) )
1705         {
1706             vlc_value_t val;
1707             val.f_float = atof( newval.psz_string );
1708             i_error = var_Set( p_vout, psz_variable, val );
1709         }
1710         else
1711         {
1712             i_error = var_Set( p_vout, psz_variable, newval );
1713         }
1714     }
1715     else
1716     {
1717         /* get */
1718         vlc_value_t val, text;
1719         int i;
1720         float f_value = 0.;
1721         char *psz_value = NULL;
1722
1723         if ( var_Get( p_vout, psz_variable, &val ) < 0 )
1724         {
1725             vlc_object_release( p_vout );
1726             return VLC_EGENERIC;
1727         }
1728         if( !strcmp( psz_variable, "zoom" ) )
1729         {
1730             f_value = val.f_float;
1731         }
1732         else
1733         {
1734             psz_value = strdup( val.psz_string );
1735         }
1736
1737         if ( var_Change( p_vout, psz_variable,
1738                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1739         {
1740             vlc_object_release( p_vout );
1741             return VLC_EGENERIC;
1742         }
1743
1744         msg_rc( "+----[ %s ]", val_name.psz_string );
1745         if( !strcmp( psz_variable, "zoom" ) )
1746         {
1747             for ( i = 0; i < val.p_list->i_count; i++ )
1748             {
1749                 if ( f_value == val.p_list->p_values[i].f_float )
1750                     msg_rc( "| %f - %s *", val.p_list->p_values[i].f_float,
1751                             text.p_list->p_values[i].psz_string );
1752                 else
1753                     msg_rc( "| %f - %s", val.p_list->p_values[i].f_float,
1754                             text.p_list->p_values[i].psz_string );
1755             }
1756         }
1757         else
1758         {
1759             for ( i = 0; i < val.p_list->i_count; i++ )
1760             {
1761                 if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1762                     msg_rc( "| %s - %s *", val.p_list->p_values[i].psz_string,
1763                             text.p_list->p_values[i].psz_string );
1764                 else
1765                     msg_rc( "| %s - %s", val.p_list->p_values[i].psz_string,
1766                             text.p_list->p_values[i].psz_string );
1767             }
1768             free( psz_value );
1769         }
1770         var_Change( p_vout, psz_variable, VLC_VAR_FREELIST,
1771                     &val, &text );
1772         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1773
1774         if( val_name.psz_string ) free( val_name.psz_string );
1775
1776         i_error = VLC_SUCCESS;
1777     }
1778     vlc_object_release( p_vout );
1779     return i_error;
1780 }
1781
1782 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1783                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1784 {
1785     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1786     input_thread_t *p_input = NULL;
1787     aout_instance_t * p_aout;
1788     const char * psz_variable;
1789     vlc_value_t val_name;
1790     int i_error;
1791
1792     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1793     if( !p_input )
1794         return VLC_ENOOBJ;
1795
1796     if( p_input )
1797     {
1798         vlc_value_t val;
1799
1800         var_Get( p_input, "state", &val );
1801         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )        {
1802             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1803             vlc_object_release( p_input );
1804             return VLC_EGENERIC;
1805         }
1806         vlc_object_release( p_input );
1807     }
1808
1809     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1810     if ( p_aout == NULL ) return VLC_ENOOBJ;
1811
1812     if ( !strcmp( psz_cmd, "adev" ) )
1813     {
1814         psz_variable = "audio-device";
1815     }
1816     else
1817     {
1818         psz_variable = "audio-channels";
1819     }
1820
1821     /* Get the descriptive name of the variable */
1822     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1823                  &val_name, NULL );
1824     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1825
1826     if ( !*newval.psz_string )
1827     {
1828         /* Retrieve all registered ***. */
1829         vlc_value_t val, text;
1830         int i, i_value;
1831
1832         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1833         {
1834             vlc_object_release( (vlc_object_t *)p_aout );
1835             return VLC_EGENERIC;
1836         }
1837         i_value = val.i_int;
1838
1839         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1840                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1841         {
1842             vlc_object_release( (vlc_object_t *)p_aout );
1843             return VLC_EGENERIC;
1844         }
1845
1846         msg_rc( "+----[ %s ]", val_name.psz_string );
1847         for ( i = 0; i < val.p_list->i_count; i++ )
1848         {
1849             if ( i_value == val.p_list->p_values[i].i_int )
1850                 msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1851                         text.p_list->p_values[i].psz_string );
1852             else
1853                 msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1854                         text.p_list->p_values[i].psz_string );
1855         }
1856         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1857                     &val, &text );
1858         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1859
1860         if( val_name.psz_string ) free( val_name.psz_string );
1861         i_error = VLC_SUCCESS;
1862     }
1863     else
1864     {
1865         vlc_value_t val;
1866         val.i_int = atoi( newval.psz_string );
1867
1868         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1869     }
1870     vlc_object_release( (vlc_object_t *)p_aout );
1871
1872     return i_error;
1873 }
1874
1875 /* OSD menu commands */
1876 static int Menu( vlc_object_t *p_this, char const *psz_cmd,
1877     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1878 {
1879     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1880     playlist_t    *p_playlist = NULL;
1881     vlc_value_t val;
1882     int i_error = VLC_EGENERIC;
1883
1884     if ( !*newval.psz_string )
1885     {
1886         msg_rc( _("Please provide one of the following parameters:") );
1887         msg_rc( "[on|off|up|down|left|right|select]" );
1888         return i_error;
1889     }
1890
1891     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1892     if( !p_playlist )
1893         return VLC_ENOOBJ;
1894
1895     if( p_playlist->p_input )
1896     {
1897         var_Get( p_playlist->p_input, "state", &val );
1898         if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1899             ( strcmp( newval.psz_string, "select" ) != 0 ) )
1900         {
1901             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1902             vlc_object_release( p_playlist );
1903             return VLC_EGENERIC;
1904         }
1905     }
1906     vlc_object_release( p_playlist );
1907
1908     val.psz_string = strdup( newval.psz_string );
1909     if( !strcmp( val.psz_string, "on" ) || !strcmp( val.psz_string, "show" ))
1910         osd_MenuShow( p_this );
1911     else if( !strcmp( val.psz_string, "off" ) || !strcmp( val.psz_string, "hide" ) )
1912         osd_MenuHide( p_this );
1913     else if( !strcmp( val.psz_string, "up" ) )
1914         osd_MenuUp( p_this );
1915     else if( !strcmp( val.psz_string, "down" ) )
1916         osd_MenuDown( p_this );
1917     else if( !strcmp( val.psz_string, "left" ) )
1918         osd_MenuPrev( p_this );
1919     else if( !strcmp( val.psz_string, "right" ) )
1920         osd_MenuNext( p_this );
1921     else if( !strcmp( val.psz_string, "select" ) )
1922         osd_MenuActivate( p_this );
1923     else
1924     {
1925         msg_rc( _("Please provide one of the following parameters:") );
1926         msg_rc( "[on|off|up|down|left|right|select]" );
1927         if( val.psz_string ) free( val.psz_string );
1928             return i_error;
1929     }
1930
1931     i_error = VLC_SUCCESS;
1932     if( val.psz_string ) free( val.psz_string );
1933     return i_error;
1934 }
1935
1936 #ifdef WIN32
1937 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1938 {
1939     INPUT_RECORD input_record;
1940     DWORD i_dw;
1941
1942     /* On Win32, select() only works on socket descriptors */
1943     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1944                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1945     {
1946         while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
1947                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1948                                  1, &i_dw ) )
1949         {
1950             if( input_record.EventType != KEY_EVENT ||
1951                 !input_record.Event.KeyEvent.bKeyDown ||
1952                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1953                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1954                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1955                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1956             {
1957                 /* nothing interesting */
1958                 continue;
1959             }
1960
1961             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1962
1963             /* Echo out the command */
1964             putc( p_buffer[ *pi_size ], stdout );
1965
1966             /* Handle special keys */
1967             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1968             {
1969                 putc( '\n', stdout );
1970                 break;
1971             }
1972             switch( p_buffer[ *pi_size ] )
1973             {
1974             case '\b':
1975                 if( *pi_size )
1976                 {
1977                     *pi_size -= 2;
1978                     putc( ' ', stdout );
1979                     putc( '\b', stdout );
1980                 }
1981                 break;
1982             case '\r':
1983                 (*pi_size) --;
1984                 break;
1985             }
1986
1987             (*pi_size)++;
1988         }
1989
1990         if( *pi_size == MAX_LINE_LENGTH ||
1991             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1992         {
1993             p_buffer[ *pi_size ] = 0;
1994             return VLC_TRUE;
1995         }
1996     }
1997
1998     return VLC_FALSE;
1999 }
2000 #endif
2001
2002 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2003 {
2004     int i_read = 0;
2005
2006 #ifdef WIN32
2007     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
2008         return ReadWin32( p_intf, p_buffer, pi_size );
2009     else if( p_intf->p_sys->i_socket == -1 )
2010     {
2011         msleep( INTF_IDLE_SLEEP );
2012         return VLC_FALSE;
2013     }
2014 #endif
2015
2016     while( !intf_ShouldDie( p_intf ) && *pi_size < MAX_LINE_LENGTH &&
2017            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
2018                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
2019                   (uint8_t *)p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
2020     {
2021         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2022             break;
2023
2024         (*pi_size)++;
2025     }
2026
2027     /* Connection closed */
2028     if( i_read == -1 )
2029     {
2030         p_intf->p_sys->i_socket = -1;
2031         p_buffer[ *pi_size ] = 0;
2032         return VLC_TRUE;
2033     }
2034
2035     if( *pi_size == MAX_LINE_LENGTH ||
2036         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2037     {
2038         p_buffer[ *pi_size ] = 0;
2039         return VLC_TRUE;
2040     }
2041
2042     return VLC_FALSE;
2043 }
2044
2045 /*****************************************************************************
2046  * parse_MRL: build a input item from a full mrl
2047  *****************************************************************************
2048  * MRL format: "simplified-mrl [:option-name[=option-value]]"
2049  * We don't check for '"' or '\'', we just assume that a ':' that follows a
2050  * space is a new option. Should be good enough for our purpose.
2051  *****************************************************************************/
2052 static input_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
2053 {
2054 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
2055 #define SKIPTRAILINGSPACE( p, d ) \
2056     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
2057
2058     input_item_t *p_item = NULL;
2059     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
2060     char **ppsz_options = NULL;
2061     int i, i_options = 0;
2062
2063     if( !psz_mrl ) return 0;
2064
2065     psz_mrl = psz_orig = strdup( psz_mrl );
2066     while( *psz_mrl )
2067     {
2068         SKIPSPACE( psz_mrl );
2069         psz_item = psz_mrl;
2070
2071         for( ; *psz_mrl; psz_mrl++ )
2072         {
2073             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
2074             {
2075                 /* We have a complete item */
2076                 break;
2077             }
2078             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
2079                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
2080             {
2081                 /* We have a complete item */
2082                 break;
2083             }
2084         }
2085
2086         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
2087         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
2088
2089         /* Remove '"' and '\'' if necessary */
2090         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
2091         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2092         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
2093         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2094
2095         if( !psz_item_mrl ) psz_item_mrl = psz_item;
2096         else if( *psz_item )
2097         {
2098             i_options++;
2099             ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
2100             ppsz_options[i_options - 1] = &psz_item[1];
2101         }
2102
2103         if( *psz_mrl ) SKIPSPACE( psz_mrl );
2104     }
2105
2106     /* Now create a playlist item */
2107     if( psz_item_mrl )
2108     {
2109         p_item = input_ItemNew( p_intf, psz_item_mrl, NULL );
2110         for( i = 0; i < i_options; i++ )
2111         {
2112             input_ItemAddOption( p_item, ppsz_options[i] );
2113         }
2114     }
2115
2116     if( i_options ) free( ppsz_options );
2117     free( psz_orig );
2118
2119     return p_item;
2120 }
2121
2122 /*****************************************************************************
2123  * checkUpdates : check for updates
2124  ****************************************************************************/
2125 static void checkUpdates( intf_thread_t *p_intf, char *psz_arg )
2126 {
2127     update_iterator_t *p_uit;
2128     update_t *p_u = update_New( p_intf );
2129     if( p_u == NULL ) return;
2130     p_uit = update_iterator_New( p_u );
2131     if( p_uit )
2132     {
2133         int s = 0, t = 0;
2134
2135         if( strstr( psz_arg, "newer" ) )
2136             s |= UPDATE_RELEASE_STATUS_NEWER;
2137         if( strstr( psz_arg, "equal" ) )
2138             s |= UPDATE_RELEASE_STATUS_EQUAL;
2139         if( strstr( psz_arg, "older" ) )
2140             s |= UPDATE_RELEASE_STATUS_OLDER;
2141         if( s ) p_uit->i_rs = s;
2142         else p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
2143
2144         if( strstr( psz_arg, "undef" ) )
2145             t |= UPDATE_FILE_TYPE_UNDEF;
2146         if( strstr( psz_arg, "info" ) )
2147             t |= UPDATE_FILE_TYPE_INFO;
2148         if( strstr( psz_arg, "source" ) )
2149             t |= UPDATE_FILE_TYPE_SOURCE;
2150         if( strstr( psz_arg, "binary" ) )
2151             t |= UPDATE_FILE_TYPE_BINARY;
2152         if( strstr( psz_arg, "plugin" ) )
2153             t |= UPDATE_FILE_TYPE_PLUGIN;
2154         if( t ) p_uit->i_t = t;
2155
2156         update_Check( p_u, VLC_FALSE );
2157         update_iterator_Action( p_uit, UPDATE_MIRROR );
2158         msg_rc( "\nUsing mirror: %s (%s) [%s]",
2159                 p_uit->mirror.psz_name,
2160                 p_uit->mirror.psz_location,
2161                 p_uit->mirror.psz_type );
2162         while( (s = update_iterator_Action( p_uit, UPDATE_FILE )) != UPDATE_FAIL )
2163         {
2164             char *psz_tmp;
2165             if( s & UPDATE_RELEASE )
2166             {
2167                 switch( p_uit->release.i_status )
2168                 {
2169                     case UPDATE_RELEASE_STATUS_OLDER:
2170                         psz_tmp = strdup( "older" );
2171                         break;
2172                     case UPDATE_RELEASE_STATUS_EQUAL:
2173                         psz_tmp = strdup( "equal" );
2174                         break;
2175                     case UPDATE_RELEASE_STATUS_NEWER:
2176                         psz_tmp = strdup( "newer" );
2177                         break;
2178                     default:
2179                         psz_tmp = strdup( "?!?" );
2180                         break;
2181                 }
2182                 msg_rc( "\n+----[ VLC %s %s (%s) ] ",
2183                         p_uit->release.psz_version,
2184                         p_uit->release.psz_svn_revision,
2185                         psz_tmp );
2186                 free( psz_tmp );
2187             }
2188             switch( p_uit->file.i_type )
2189             {
2190                 case UPDATE_FILE_TYPE_UNDEF:
2191                     psz_tmp = strdup( "undef" );
2192                     break;
2193                 case UPDATE_FILE_TYPE_INFO:
2194                     psz_tmp = strdup( "info" );
2195                     break;
2196                 case UPDATE_FILE_TYPE_SOURCE:
2197                     psz_tmp = strdup( "source" );
2198                     break;
2199                 case UPDATE_FILE_TYPE_BINARY:
2200                     psz_tmp = strdup( "binary" );
2201                     break;
2202                 case UPDATE_FILE_TYPE_PLUGIN:
2203                     psz_tmp = strdup( "plugin" );
2204                     break;
2205                 default:
2206                     psz_tmp = strdup( "?!?" );
2207                     break;
2208             }
2209             msg_rc( "| %s (%s)", p_uit->file.psz_description, psz_tmp );
2210             free( psz_tmp );
2211             if( p_uit->file.l_size )
2212             {
2213                 if( p_uit->file.l_size > 1024 * 1024 * 1024 )
2214                     asprintf( &psz_tmp, "(%ld GB)",
2215                               p_uit->file.l_size / (1024*1024*1024) );
2216                 if( p_uit->file.l_size > 1024 * 1024 )
2217                     asprintf( &psz_tmp, "(%ld MB)",
2218                               p_uit->file.l_size / (1024*1024) );
2219                 else if( p_uit->file.l_size > 1024 )
2220                     asprintf( &psz_tmp, "(%ld kB)",
2221                               p_uit->file.l_size / 1024 );
2222                 else
2223                     asprintf( &psz_tmp, "(%ld B)", p_uit->file.l_size );
2224             }
2225             else
2226             {
2227                 psz_tmp = strdup( "" );
2228             }
2229             msg_rc( "| %s %s", p_uit->file.psz_url, psz_tmp );
2230             msg_rc( "+----" );
2231             free( psz_tmp );
2232         }
2233         msg_rc( "" );
2234         update_iterator_Delete( p_uit );
2235     }
2236     update_Delete( p_u );
2237 }