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