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