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