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