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