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