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