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