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