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