]> git.sesse.net Git - vlc/blob - modules/control/rc.c
6d5fe5d76e454bb2d9053caf3a27daed2d038417
[vlc] / modules / control / rc.c
1 /*****************************************************************************
2  * rc.c : remote control stdin/stdout module for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 - 2005 VideoLAN
5  * $Id$
6  *
7  * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37 #include <vlc/aout.h>
38 #include <vlc/vout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43
44 #ifdef HAVE_SYS_TIME_H
45 #    include <sys/time.h>
46 #endif
47 #include <sys/types.h>
48
49 #include "vlc_error.h"
50 #include "network.h"
51
52 #if defined(PF_UNIX) && !defined(PF_LOCAL)
53 #    define PF_LOCAL PF_UNIX
54 #endif
55 #if defined(AF_UNIX) && !defined(AF_LOCAL)
56 #    define AF_LOCAL AF_UNIX
57 #endif
58
59 #ifdef PF_LOCAL
60 #    include <sys/un.h>
61 #endif
62
63 #define MAX_LINE_LENGTH 256
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int  Activate     ( vlc_object_t * );
69 static void Deactivate   ( vlc_object_t * );
70 static void Run          ( intf_thread_t * );
71
72 static vlc_bool_t ReadCommand( intf_thread_t *, char *, int * );
73
74 static playlist_item_t *parse_MRL( intf_thread_t *, char * );
75
76 static int  Input        ( vlc_object_t *, char const *,
77                            vlc_value_t, vlc_value_t, void * );
78 static int  Playlist     ( vlc_object_t *, char const *,
79                            vlc_value_t, vlc_value_t, void * );
80 static int  Other        ( vlc_object_t *, char const *,
81                            vlc_value_t, vlc_value_t, void * );
82 static int  Quit         ( vlc_object_t *, char const *,
83                            vlc_value_t, vlc_value_t, void * );
84 static int  Intf         ( vlc_object_t *, char const *,
85                            vlc_value_t, vlc_value_t, void * );
86 static int  Volume       ( vlc_object_t *, char const *,
87                            vlc_value_t, vlc_value_t, void * );
88 static int  VolumeMove   ( vlc_object_t *, char const *,
89                            vlc_value_t, vlc_value_t, void * );
90 static int  AudioConfig  ( vlc_object_t *, char const *,
91                            vlc_value_t, vlc_value_t, void * );
92
93 struct intf_sys_t
94 {
95     int i_socket_listen;
96     int i_socket;
97     char *psz_unix_path;
98     vlc_bool_t b_extend;
99
100 #ifdef WIN32
101     HANDLE hConsoleIn;
102     vlc_bool_t b_quiet;
103 #endif
104 };
105
106 #ifdef HAVE_VARIADIC_MACROS
107 #   define msg_rc( psz_format, args... ) \
108       __msg_rc( p_intf, psz_format, ## args )
109 #endif
110
111 void __msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
112 {
113     va_list args;
114     va_start( args, psz_fmt );
115     if( p_intf->p_sys->i_socket == -1 ) vprintf( psz_fmt, args );
116     else
117     { net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, psz_fmt, args );
118       net_Printf( VLC_OBJECT(p_intf), p_intf->p_sys->i_socket, NULL, "\r" ); }
119     va_end( args );
120 }
121
122 /*****************************************************************************
123  * Module descriptor
124  *****************************************************************************/
125 #define POS_TEXT N_("Show stream position")
126 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
127                         "stream from time to time." )
128
129 #define TTY_TEXT N_("Fake TTY")
130 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
131
132 #define UNIX_TEXT N_("UNIX socket command input")
133 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
134                          "stdin." )
135
136 #define HOST_TEXT N_("TCP command input")
137 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
138             "You can set the address and port the interface will bind to." )
139 #define EXTEND_TEXT N_("Extended help")
140 #define EXTEND_LONGTEXT N_("List additional commands.")
141
142 #ifdef WIN32
143 #define QUIET_TEXT N_("Do not open a DOS command box interface")
144 #define QUIET_LONGTEXT N_( \
145     "By default the rc interface plugin will start a DOS command box. " \
146     "Enabling the quiet mode will not bring this command box but can also " \
147     "be pretty annoying when you want to stop VLC and no video window is " \
148     "open." )
149 #endif
150
151 vlc_module_begin();
152     set_category( CAT_INTERFACE );
153     set_subcategory( SUBCAT_INTERFACE_GENERAL );
154     set_description( _("Remote control interface") );
155     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
156 #ifdef HAVE_ISATTY
157     add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
158 #endif
159     add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, VLC_TRUE );
160     add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
161
162 #ifdef WIN32
163     add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, VLC_FALSE );
164 #endif
165     add_bool( "rc-extend", 0, NULL, EXTEND_TEXT, EXTEND_LONGTEXT, VLC_FALSE );
166
167     set_capability( "interface", 20 );
168     set_callbacks( Activate, Deactivate );
169 vlc_module_end();
170
171 /*****************************************************************************
172  * Activate: initialize and create stuff
173  *****************************************************************************/
174 static int Activate( vlc_object_t *p_this )
175 {
176     intf_thread_t *p_intf = (intf_thread_t*)p_this;
177     playlist_t *p_playlist;
178     char *psz_host, *psz_unix_path;
179     int i_socket = -1;
180
181 #if defined(HAVE_ISATTY) && !defined(WIN32)
182     /* Check that stdin is a TTY */
183     if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
184     {
185         msg_Warn( p_intf, "fd 0 is not a TTY" );
186         return VLC_EGENERIC;
187     }
188 #endif
189
190     psz_unix_path = config_GetPsz( p_intf, "rc-unix" );
191     if( psz_unix_path )
192     {
193 #ifndef PF_LOCAL
194         msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
195         free( psz_unix_path );
196         return VLC_EGENERIC;
197 #else
198         struct sockaddr_un addr;
199         int i_ret;
200
201         memset( &addr, 0, sizeof(struct sockaddr_un) );
202
203         msg_Dbg( p_intf, "trying UNIX socket" );
204
205         if( (i_socket = socket( PF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
206         {
207             msg_Warn( p_intf, "can't open socket: %s", strerror(errno) );
208             free( psz_unix_path );
209             return VLC_EGENERIC;
210         }
211
212         addr.sun_family = AF_LOCAL;
213         strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
214         addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
215
216         if( (i_ret = bind( i_socket, (struct sockaddr*)&addr,
217                            sizeof(struct sockaddr_un) ) ) < 0 )
218         {
219             msg_Warn( p_intf, "couldn't bind socket to address: %s",
220                       strerror(errno) );
221             free( psz_unix_path );
222             net_Close( i_socket );
223             return VLC_EGENERIC;
224         }
225
226         if( ( i_ret = listen( i_socket, 1 ) ) < 0 )
227         {
228             msg_Warn( p_intf, "can't listen on socket: %s", strerror(errno));
229             free( psz_unix_path );
230             net_Close( i_socket );
231             return VLC_EGENERIC;
232         }
233 #endif
234     }
235
236     if( ( i_socket == -1) &&
237         ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
238     {
239         vlc_url_t url;
240
241         vlc_UrlParse( &url, psz_host, 0 );
242
243         msg_Dbg( p_intf, "base %s port %d", url.psz_host, url.i_port );
244
245         if( (i_socket = net_ListenTCP(p_this, url.psz_host, url.i_port)) == -1)
246         {
247             msg_Warn( p_intf, "can't listen to %s port %i",
248                       url.psz_host, url.i_port );
249             vlc_UrlClean( &url );
250             free( psz_host );
251             return VLC_EGENERIC;
252         }
253
254         vlc_UrlClean( &url );
255         free( psz_host );
256     }
257
258     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
259     if( !p_intf->p_sys )
260     {
261         msg_Err( p_intf, "no memory" );
262         return VLC_ENOMEM;
263     }
264
265     p_intf->p_sys->i_socket_listen = i_socket;
266     p_intf->p_sys->i_socket = -1;
267     p_intf->p_sys->psz_unix_path = psz_unix_path;
268
269     /* Non-buffered stdout */
270     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
271
272     p_intf->pf_run = Run;
273
274 #ifdef WIN32
275     p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
276     if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
277 #else
278     CONSOLE_INTRO_MSG;
279 #endif
280
281     /* Force "no-view" mode */
282     p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
283                                                  FIND_ANYWHERE );
284     if( p_playlist )
285     {
286         vlc_mutex_lock( &p_playlist->object_lock );
287         p_playlist->status.i_view = -1;
288         vlc_mutex_unlock( &p_playlist->object_lock );
289         vlc_object_release( p_playlist );
290     }
291
292     msg_rc( _("Remote control interface initialized, `h' for help\n") );
293     return VLC_SUCCESS;
294 }
295
296 /*****************************************************************************
297  * Deactivate: uninitialize and cleanup
298  *****************************************************************************/
299 static void Deactivate( vlc_object_t *p_this )
300 {
301     intf_thread_t *p_intf = (intf_thread_t*)p_this;
302
303     if( p_intf->p_sys->i_socket_listen != -1 )
304         net_Close( p_intf->p_sys->i_socket_listen );
305     if( p_intf->p_sys->i_socket != -1 )
306         net_Close( p_intf->p_sys->i_socket );
307     if( p_intf->p_sys->psz_unix_path != NULL )
308     {
309 #ifdef PF_LOCAL
310         unlink( p_intf->p_sys->psz_unix_path );
311 #endif
312         free( p_intf->p_sys->psz_unix_path );
313     }
314     free( p_intf->p_sys );
315 }
316
317 /*****************************************************************************
318  * Run: rc thread
319  *****************************************************************************
320  * This part of the interface is in a separate thread so that we can call
321  * exec() from within it without annoying the rest of the program.
322  *****************************************************************************/
323 static void Run( intf_thread_t *p_intf )
324 {
325     input_thread_t * p_input;
326     playlist_t *     p_playlist;
327
328     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
329     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
330
331     int        i_size = 0;
332     int        i_oldpos = 0;
333     int        i_newpos;
334
335     p_buffer[0] = 0;
336     p_input = NULL;
337     p_playlist = NULL;
338  
339     p_intf->p_sys->b_extend = config_GetInt( p_intf, "rc-extend" );
340     /* Register commands that will be cleaned up upon object destruction */
341     var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
342     var_AddCallback( p_intf, "quit", Quit, NULL );
343     var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
344     var_AddCallback( p_intf, "intf", Intf, NULL );
345
346     var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
347     var_AddCallback( p_intf, "add", Playlist, NULL );
348     var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
349     var_AddCallback( p_intf, "playlist", Playlist, NULL );
350     var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
351     var_AddCallback( p_intf, "play", Playlist, NULL );
352     var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
353     var_AddCallback( p_intf, "stop", Playlist, NULL );
354     var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
355     var_AddCallback( p_intf, "prev", Playlist, NULL );
356     var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
357     var_AddCallback( p_intf, "next", Playlist, NULL );
358     var_Create( p_intf, "goto", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
359     var_AddCallback( p_intf, "goto", Playlist, NULL );
360  
361     /* marquee on the fly items */
362     var_Create( p_intf, "marq-marquee", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
363     var_AddCallback( p_intf, "marq-marquee", Other, NULL );
364     var_Create( p_intf, "marq-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
365     var_AddCallback( p_intf, "marq-x", Other, NULL );
366     var_Create( p_intf, "marq-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
367     var_AddCallback( p_intf, "marq-y", Other, NULL );
368     var_Create( p_intf, "marq-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
369     var_AddCallback( p_intf, "marq-position", Other, NULL );
370     var_Create( p_intf, "marq-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
371     var_AddCallback( p_intf, "marq-color", Other, NULL );
372     var_Create( p_intf, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
373     var_AddCallback( p_intf, "marq-opacity", Other, NULL );
374     var_Create( p_intf, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
375     var_AddCallback( p_intf, "marq-timeout", Other, NULL );
376     var_Create( p_intf, "marq-size", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
377     var_AddCallback( p_intf, "marq-size", Other, NULL );
378
379     var_Create( p_intf, "mosaic-alpha", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
380     var_AddCallback( p_intf, "mosaic-alpha", Other, NULL );
381     var_Create( p_intf, "mosaic-height", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
382     var_AddCallback( p_intf, "mosaic-height", Other, NULL );
383     var_Create( p_intf, "mosaic-width", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
384     var_AddCallback( p_intf, "mosaic-width", Other, NULL );
385     var_Create( p_intf, "mosaic-xoffset", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
386     var_AddCallback( p_intf, "mosaic-xoffset", Other, NULL );
387     var_Create( p_intf, "mosaic-yoffset", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
388     var_AddCallback( p_intf, "mosaic-yoffset", Other, NULL );
389     var_Create( p_intf, "mosaic-vborder", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
390     var_AddCallback( p_intf, "mosaic-vborder", Other, NULL );
391     var_Create( p_intf, "mosaic-hborder", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
392     var_AddCallback( p_intf, "mosaic-hborder", Other, NULL );
393     var_Create( p_intf, "mosaic-position",
394                      VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
395     var_AddCallback( p_intf, "mosaic-position", Other, NULL );
396     var_Create( p_intf, "mosaic-rows", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
397     var_AddCallback( p_intf, "mosaic-rows", Other, NULL );
398     var_Create( p_intf, "mosaic-cols", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
399     var_AddCallback( p_intf, "mosaic-cols", Other, NULL );
400     var_Create( p_intf, "mosaic-keep-aspect-ratio",
401                      VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
402     var_AddCallback( p_intf, "mosaic-keep-aspect-ratio", Other, NULL );
403
404     /* time on the fly items */
405     var_Create( p_intf, "time-format", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
406     var_AddCallback( p_intf, "time-format", Other, NULL );
407     var_Create( p_intf, "time-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
408     var_AddCallback( p_intf, "time-x", Other, NULL );
409     var_Create( p_intf, "time-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
410     var_AddCallback( p_intf, "time-y", Other, NULL );
411     var_Create( p_intf, "time-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
412     var_AddCallback( p_intf, "time-position", Other, NULL );
413     var_Create( p_intf, "time-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
414     var_AddCallback( p_intf, "time-color", Other, NULL );
415     var_Create( p_intf, "time-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
416     var_AddCallback( p_intf, "time-opacity", Other, NULL );
417     var_Create( p_intf, "time-size", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
418     var_AddCallback( p_intf, "time-size", Other, NULL );
419     
420     var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
421     var_AddCallback( p_intf, "pause", Input, NULL );
422     var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
423     var_AddCallback( p_intf, "seek", Input, NULL );
424     var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
425     var_AddCallback( p_intf, "title", Input, NULL );
426     var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
427     var_AddCallback( p_intf, "title_n", Input, NULL );
428     var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
429     var_AddCallback( p_intf, "title_p", Input, NULL );
430     var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
431     var_AddCallback( p_intf, "chapter", Input, NULL );
432     var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
433     var_AddCallback( p_intf, "chapter_n", Input, NULL );
434     var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
435     var_AddCallback( p_intf, "chapter_p", Input, NULL );
436
437     var_Create( p_intf, "fastforward", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
438     var_AddCallback( p_intf, "fastforward", Input, NULL );
439     var_Create( p_intf, "rewind", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
440     var_AddCallback( p_intf, "rewind", Input, NULL );
441
442     var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
443     var_AddCallback( p_intf, "volume", Volume, NULL );
444     var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
445     var_AddCallback( p_intf, "volup", VolumeMove, NULL );
446     var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
447     var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
448     var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
449     var_AddCallback( p_intf, "adev", AudioConfig, NULL );
450     var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
451     var_AddCallback( p_intf, "achan", AudioConfig, NULL );
452
453 #ifdef WIN32
454     /* Get the file descriptor of the console input */
455     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
456     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
457     {
458         msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
459         p_intf->b_die = VLC_TRUE;
460     }
461 #endif
462
463     while( !p_intf->b_die )
464     {
465         char *psz_cmd, *psz_arg;
466         vlc_bool_t b_complete;
467
468         if( p_intf->p_sys->i_socket_listen != - 1 &&
469             p_intf->p_sys->i_socket == -1 )
470         {
471             p_intf->p_sys->i_socket =
472                 net_Accept( p_intf, p_intf->p_sys->i_socket_listen, 0 );
473         }
474
475         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
476
477         /* Manage the input part */
478         if( p_input == NULL )
479         {
480             if( p_playlist )
481             {
482                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
483                                                        FIND_CHILD );
484             }
485             else
486             {
487                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
488                                                    FIND_ANYWHERE );
489                 if( p_input )
490                 {
491                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
492                                                            FIND_PARENT );
493                 }
494             }
495         }
496         else if( p_input->b_dead )
497         {
498             vlc_object_release( p_input );
499             p_input = NULL;
500         }
501
502         if( p_input && b_showpos )
503         {
504             i_newpos = 100 * var_GetFloat( p_input, "position" );
505             if( i_oldpos != i_newpos )
506             {
507                 i_oldpos = i_newpos;
508                 msg_rc( "pos: %d%%\n", i_newpos );
509             }
510         }
511
512         /* Is there something to do? */
513         if( !b_complete ) continue;
514
515
516         /* Skip heading spaces */
517         psz_cmd = p_buffer;
518         while( *psz_cmd == ' ' )
519         {
520             psz_cmd++;
521         }
522
523         /* Split psz_cmd at the first space and make sure that
524          * psz_arg is valid */
525         psz_arg = strchr( psz_cmd, ' ' );
526         if( psz_arg )
527         {
528             *psz_arg++ = 0;
529             while( *psz_arg == ' ' )
530             {
531                 psz_arg++;
532             }
533         }
534         else
535         {
536             psz_arg = "";
537         }
538
539         /* If the user typed a registered local command, try it */
540         if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
541         {
542             vlc_value_t val;
543             int i_ret;
544
545             val.psz_string = psz_arg;
546             i_ret = var_Set( p_intf, psz_cmd, val );
547             msg_rc( "%s: returned %i (%s)\n",
548                     psz_cmd, i_ret, vlc_error( i_ret ) );
549         }
550         /* Or maybe it's a global command */
551         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
552         {
553             vlc_value_t val;
554             int i_ret;
555
556             val.psz_string = psz_arg;
557             /* FIXME: it's a global command, but we should pass the
558              * local object as an argument, not p_intf->p_libvlc. */
559             i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
560             if( i_ret != 0 )
561             {
562                 msg_rc( "%s: returned %i (%s)\n",
563                          psz_cmd, i_ret, vlc_error( i_ret ) );
564             }
565         }
566         else if( !strcmp( psz_cmd, "logout" ) )
567         {
568             /* Close connection */
569             if( p_intf->p_sys->i_socket != -1 )
570             {
571                 net_Close( p_intf->p_sys->i_socket );
572             }
573             p_intf->p_sys->i_socket = -1;
574         }
575         else if( !strcmp( psz_cmd, "info" ) )
576         {
577             if( p_input )
578             {
579                 int i, j;
580                 vlc_mutex_lock( &p_input->input.p_item->lock );
581                 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
582                 {
583                     info_category_t *p_category =
584                         p_input->input.p_item->pp_categories[i];
585
586                     msg_rc( "+----[ %s ]\n", p_category->psz_name );
587                     msg_rc( "| \n" );
588                     for ( j = 0; j < p_category->i_infos; j++ )
589                     {
590                         info_t *p_info = p_category->pp_infos[j];
591                         msg_rc( "| %s: %s\n", p_info->psz_name,
592                                 p_info->psz_value );
593                     }
594                     msg_rc( "| \n" );
595                 }
596                 msg_rc( "+----[ end of stream info ]\n" );
597                 vlc_mutex_unlock( &p_input->input.p_item->lock );
598             }
599             else
600             {
601                 msg_rc( "no input\n" );
602             }
603         }
604         else if( !strcmp( psz_cmd, "is_playing" ) )
605         {
606             if( ! p_input )
607             {
608                 msg_rc( "0\n" );
609             }
610             else
611             {
612                 msg_rc( "1\n" );
613             }
614         }
615         else if( !strcmp( psz_cmd, "get_time" ) )
616         {
617             if( ! p_input )
618             {
619                 msg_rc("0\n");
620             }
621             else
622             {
623                 vlc_value_t time;
624                 var_Get( p_input, "time", &time );
625                 msg_rc( "%i\n", time.i_time / 1000000);
626             }
627         }
628         else if( !strcmp( psz_cmd, "get_length" ) )
629         {
630             if( ! p_input )
631             {
632                 msg_rc("0\n");
633             }
634             else
635             {
636                 vlc_value_t time;
637                 var_Get( p_input, "length", &time );
638                 msg_rc( "%i\n", time.i_time / 1000000);
639             }
640         }
641         else if( !strcmp( psz_cmd, "get_title" ) )
642         {
643             if( ! p_input )
644             {
645                 msg_rc("\n");
646             }
647             else
648             {
649                 msg_rc( "%s\n", p_input->input.p_item->psz_name );
650             }
651         }
652         else switch( psz_cmd[0] )
653         {
654         case 'f':
655         case 'F':
656             if( p_input )
657             {
658                 vout_thread_t *p_vout;
659                 p_vout = vlc_object_find( p_input,
660                                           VLC_OBJECT_VOUT, FIND_CHILD );
661
662                 if( p_vout )
663                 {
664                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
665                     vlc_object_release( p_vout );
666                 }
667             }
668             break;
669
670         case 's':
671         case 'S':
672             ;
673             break;
674
675         case '?':
676         case 'h':
677         case 'H':
678             msg_rc(_("+----[ Remote control commands ]\n"));
679             msg_rc(  "| \n");
680             msg_rc(_("| add XYZ  . . . . . . . . . . add XYZ to playlist\n"));
681             msg_rc(_("| playlist . . .  show items currently in playlist\n"));
682             msg_rc(_("| play . . . . . . . . . . . . . . . . play stream\n"));
683             msg_rc(_("| stop . . . . . . . . . . . . . . . . stop stream\n"));
684             msg_rc(_("| next . . . . . . . . . . . .  next playlist item\n"));
685             msg_rc(_("| prev . . . . . . . . . .  previous playlist item\n"));
686             msg_rc(_("| goto . . . . . . . . . . . .  goto item at index\n"));
687             msg_rc(_("| title [X]  . . . . set/get title in current item\n"));
688             msg_rc(_("| title_n  . . . . . .  next title in current item\n"));
689             msg_rc(_("| title_p  . . . .  previous title in current item\n"));
690             msg_rc(_("| chapter [X]  . . set/get chapter in current item\n"));
691             msg_rc(_("| chapter_n  . . . .  next chapter in current item\n"));
692             msg_rc(_("| chapter_p  . .  previous chapter in current item\n"));
693             msg_rc(  "| \n");
694             msg_rc(_("| seek X . seek in seconds, for instance `seek 12'\n"));
695             msg_rc(_("| pause  . . . . . . . . . . . . . .  toggle pause\n"));
696             msg_rc(_("| fastforward  . . . . . .  .  set to maximum rate\n"));
697             msg_rc(_("| rewind  . . . . . . . . . .  set to minimum rate\n"));
698             msg_rc(_("| f  . . . . . . . . . . . . . . toggle fullscreen\n"));
699             msg_rc(_("| info . . .  information about the current stream\n"));
700             msg_rc(  "| \n");
701             msg_rc(_("| volume [X] . . . . . . . .  set/get audio volume\n"));
702             msg_rc(_("| volup [X]  . . . . .  raise audio volume X steps\n"));
703             msg_rc(_("| voldown [X]  . . . .  lower audio volume X steps\n"));
704             msg_rc(_("| adev [X] . . . . . . . . .  set/get audio device\n"));
705             msg_rc(_("| achan [X]. . . . . . . .  set/get audio channels\n"));
706             msg_rc(  "| \n");
707             
708             if (p_intf->p_sys->b_extend)
709             {
710                 msg_rc(_("| marq-marquee STRING  . . overlay STRING in video\n"));
711                 msg_rc(_("| marq-x X . . . . .  offset of marquee, from left\n"));
712                 msg_rc(_("| marq-y Y . . . . . . offset of marquee, from top\n"));
713                 msg_rc(_("| marq-position #. . .  .relative position control\n"));
714                 msg_rc(_("| marq-color # . . . .  font color of marquee, RGB\n"));
715                 msg_rc(_("| marq-opacity # . . . . . . . .opacity of marquee\n"));
716                 msg_rc(_("| marq-timeout T. . . .  timeout of marquee, in ms\n"));
717                 msg_rc(_("| marq-size # . . . font size of marquee, in pixels\n"));
718                 msg_rc(  "| \n");
719                 msg_rc(_("| time-format STRING . . . overlay STRING in video\n"));
720                 msg_rc(_("| time-x X . . . . .offset of timestamp, from left\n"));
721                 msg_rc(_("| time-y Y . . . . . offset of timestamp, from top\n"));
722                 msg_rc(_("| time-position #. . .  .relative position control\n"));
723                 msg_rc(_("| time-color # . . .  font color of timestamp, RGB\n"));
724                 msg_rc(_("| time-opacity # . . . . . . .opacity of timestamp\n"));
725                 msg_rc(_("| time-size # . .font size of timestamp, in pixels\n"));
726                 msg_rc(  "| \n");
727             }    
728             msg_rc(_("| help . . . . . . . . . . . . . this help message\n"));
729             msg_rc(_("| logout . . . . .  exit (if in socket connection)\n"));
730             msg_rc(_("| quit . . . . . . . . . . . . . . . . .  quit vlc\n"));
731             msg_rc(  "| \n");
732             msg_rc(_("+----[ end of help ]\n"));
733             break;
734
735         case '\0':
736             /* Ignore empty lines */
737             break;
738
739         default:
740             msg_rc(_("unknown command `%s', type `help' for help\n"), psz_cmd);
741             break;
742         }
743
744         /* Command processed */
745         i_size = 0; p_buffer[0] = 0;
746     }
747
748     if( p_input )
749     {
750         vlc_object_release( p_input );
751         p_input = NULL;
752     }
753
754     if( p_playlist )
755     {
756         vlc_object_release( p_playlist );
757         p_playlist = NULL;
758     }
759 }
760
761 static int Input( vlc_object_t *p_this, char const *psz_cmd,
762                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
763 {
764     intf_thread_t *p_intf = (intf_thread_t*)p_this;
765     input_thread_t *p_input;
766     vlc_value_t     val;
767
768     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
769     if( !p_input ) return VLC_ENOOBJ;
770
771     /* Parse commands that only require an input */
772     if( !strcmp( psz_cmd, "pause" ) )
773     {
774         val.i_int = PAUSE_S;
775
776         var_Set( p_input, "state", val );
777         vlc_object_release( p_input );
778         return VLC_SUCCESS;
779     }
780     else if( !strcmp( psz_cmd, "seek" ) )
781     {
782         if( strlen( newval.psz_string ) > 0 &&
783             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
784         {
785             val.f_float = (float)atoi( newval.psz_string ) / 100.0;
786             var_Set( p_input, "position", val );
787         }
788         else
789         {
790             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
791             var_Set( p_input, "time", val );
792         }
793         vlc_object_release( p_input );
794         return VLC_SUCCESS;
795     }
796     else if ( !strcmp( psz_cmd, "fastforward" ) )
797     {
798         val.i_int = INPUT_RATE_MAX;
799
800         var_Set( p_input, "rate", val );
801         vlc_object_release( p_input );
802         return VLC_SUCCESS;
803     }
804     else if ( !strcmp( psz_cmd, "rewind" ) )
805     {
806         val.i_int = INPUT_RATE_MIN;
807
808         var_Set( p_input, "rate", val );
809         vlc_object_release( p_input );
810         return VLC_SUCCESS;
811     }
812     else if( !strcmp( psz_cmd, "chapter" ) ||
813              !strcmp( psz_cmd, "chapter_n" ) ||
814              !strcmp( psz_cmd, "chapter_p" ) )
815     {
816         if( !strcmp( psz_cmd, "chapter" ) )
817         {
818             if ( *newval.psz_string )
819             {
820                 /* Set. */
821                 val.i_int = atoi( newval.psz_string );
822                 var_Set( p_input, "chapter", val );
823             }
824             else
825             {
826                 vlc_value_t val_list;
827
828                 /* Get. */
829                 var_Get( p_input, "chapter", &val );
830                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
831                             &val_list, NULL );
832                 msg_rc( "Currently playing chapter %d/%d\n",
833                         val.i_int, val_list.p_list->i_count );
834                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
835                             &val_list, NULL );
836             }
837         }
838         else if( !strcmp( psz_cmd, "chapter_n" ) )
839         {
840             val.b_bool = VLC_TRUE;
841             var_Set( p_input, "next-chapter", val );
842         }
843         else if( !strcmp( psz_cmd, "chapter_p" ) )
844         {
845             val.b_bool = VLC_TRUE;
846             var_Set( p_input, "prev-chapter", val );
847         }
848
849         vlc_object_release( p_input );
850         return VLC_SUCCESS;
851     }
852     else if( !strcmp( psz_cmd, "title" ) ||
853              !strcmp( psz_cmd, "title_n" ) ||
854              !strcmp( psz_cmd, "title_p" ) )
855     {
856         if( !strcmp( psz_cmd, "title" ) )
857         {
858             if ( *newval.psz_string )
859             {
860                 /* Set. */
861                 val.i_int = atoi( newval.psz_string );
862                 var_Set( p_input, "title", val );
863             }
864             else
865             {
866                 vlc_value_t val_list;
867
868                 /* Get. */
869                 var_Get( p_input, "title", &val );
870                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
871                             &val_list, NULL );
872                 msg_rc( "Currently playing title %d/%d\n",
873                         val.i_int, val_list.p_list->i_count );
874                 var_Change( p_this, "title", VLC_VAR_FREELIST,
875                             &val_list, NULL );
876             }
877         }
878         else if( !strcmp( psz_cmd, "title_n" ) )
879         {
880             val.b_bool = VLC_TRUE;
881             var_Set( p_input, "next-title", val );
882         }
883         else if( !strcmp( psz_cmd, "title_p" ) )
884         {
885             val.b_bool = VLC_TRUE;
886             var_Set( p_input, "prev-title", val );
887         }
888
889         vlc_object_release( p_input );
890         return VLC_SUCCESS;
891     }
892
893     /* Never reached. */
894     return VLC_EGENERIC;
895 }
896
897 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
898                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
899 {
900     vlc_value_t val;
901     intf_thread_t *p_intf = (intf_thread_t*)p_this;
902     playlist_t *p_playlist;
903
904     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
905                                            FIND_ANYWHERE );
906     if( !p_playlist )
907     {
908         return VLC_ENOOBJ;
909     }
910
911     /* Parse commands that require a playlist */
912     if( !strcmp( psz_cmd, "prev" ) )
913     {
914         playlist_Prev( p_playlist );
915     }
916     else if( !strcmp( psz_cmd, "next" ) )
917     {
918         playlist_Next( p_playlist );
919     }
920     else if( !strcmp( psz_cmd, "play" ) )
921     {
922         playlist_Play( p_playlist );
923     }
924     else if (!strcmp( psz_cmd, "goto" ) )
925     {
926         if( strlen( newval.psz_string ) > 0) 
927         {
928             val.i_int = atoi( newval.psz_string );
929             playlist_Goto( p_playlist, val.i_int); 
930         }
931     }
932     else if( !strcmp( psz_cmd, "stop" ) )
933     {
934         playlist_Stop( p_playlist );
935     }
936     else if( !strcmp( psz_cmd, "add" ) &&
937              newval.psz_string && *newval.psz_string )
938     {
939         playlist_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
940
941         if( p_item )
942         {
943             msg_rc( "trying to add %s to playlist\n", newval.psz_string );
944             playlist_AddItem( p_playlist, p_item,
945                               PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
946         }
947     }
948     else if( !strcmp( psz_cmd, "playlist" ) )
949     {
950         int i;
951         for ( i = 0; i < p_playlist->i_size; i++ )
952         {
953             msg_rc( "|%s%s   %s|%s|\n", i == p_playlist->i_index ? "*" : " ",
954                     p_playlist->pp_items[i]->input.psz_name,
955                     p_playlist->pp_items[i]->input.psz_uri,
956                     p_playlist->pp_items[i]->i_parents > 0 ?
957                     p_playlist->pp_items[i]->pp_parents[0]->p_parent->input.psz_name : "" );
958         }
959         if ( i == 0 )
960         {
961             msg_rc( "| no entries\n" );
962         }
963     }
964  
965     /*
966      * sanity check
967      */
968     else
969     {
970         msg_rc( "unknown command!\n" );
971     }
972
973     vlc_object_release( p_playlist );
974     return VLC_SUCCESS;
975 }
976
977 static int Other( vlc_object_t *p_this, char const *psz_cmd,
978                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
979 {
980     intf_thread_t *p_intf = (intf_thread_t*)p_this;
981     vlc_object_t *p_pl;
982     vlc_value_t     val;
983     vlc_object_t *p_inp;
984
985     p_pl = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
986                                            FIND_ANYWHERE );
987     if( !p_pl )
988     {
989         return VLC_ENOOBJ;
990     }
991     
992     p_inp = vlc_object_find( p_this, VLC_OBJECT_INPUT,
993                                            FIND_ANYWHERE );
994     if( !p_inp )
995     {
996         return VLC_ENOOBJ;
997     }
998
999     /* Parse miscellaneous commands */
1000     if( !strcmp( psz_cmd, "marq-marquee" ) )
1001     {
1002         if( strlen( newval.psz_string ) > 0 )
1003         {
1004             val.psz_string = newval.psz_string;
1005             var_Set( p_inp->p_libvlc, "marq-marquee", val );
1006         }
1007         else 
1008         {
1009                 val.psz_string = "";
1010                 var_Set( p_inp->p_libvlc, "marq-marquee", val);
1011         }
1012     }
1013     else if( !strcmp( psz_cmd, "marq-x" ) )
1014     {
1015         if( strlen( newval.psz_string ) > 0) 
1016         {
1017             val.i_int = atoi( newval.psz_string );
1018             var_Set( p_inp->p_libvlc, "marq-x", val );
1019         }
1020     }
1021     else if( !strcmp( psz_cmd, "marq-y" ) )
1022     {
1023         if( strlen( newval.psz_string ) > 0) 
1024         {
1025             val.i_int = atoi( newval.psz_string );
1026             var_Set( p_inp->p_libvlc, "marq-y", val );
1027         }
1028     }
1029     else if( !strcmp( psz_cmd, "marq-position" ) )
1030     {
1031         if( strlen( newval.psz_string ) > 0) 
1032         {
1033             val.i_int = atoi( newval.psz_string );
1034             var_Set( p_inp->p_libvlc, "marq-position", val );
1035         }
1036     }
1037     else if( !strcmp( psz_cmd, "marq-color" ) )
1038     {
1039         if( strlen( newval.psz_string ) > 0) 
1040         {
1041             val.i_int = strtol( newval.psz_string, NULL, 0 );
1042             var_Set( p_inp->p_libvlc, "marq-color", val );
1043         }
1044     }
1045     else if( !strcmp( psz_cmd, "marq-opacity" ) )
1046     {
1047         if( strlen( newval.psz_string ) > 0) 
1048         {
1049             val.i_int = strtol( newval.psz_string, NULL, 0 );
1050             var_Set( p_inp->p_libvlc, "marq-opacity", val );
1051         }
1052     }
1053     else if( !strcmp( psz_cmd, "marq-size" ) )
1054     {
1055         if( strlen( newval.psz_string ) > 0) 
1056         {
1057             val.i_int = atoi( newval.psz_string );
1058             var_Set( p_inp->p_libvlc, "marq-size", val );
1059         }
1060     }
1061     else if( !strcmp( psz_cmd, "marq-timeout" ) )
1062     {
1063         if( strlen( newval.psz_string ) > 0) 
1064         {
1065             val.i_int = atoi( newval.psz_string );
1066             var_Set( p_inp, "marq-timeout", val );
1067         }
1068     }
1069     else if( !strcmp( psz_cmd, "mosaic-alpha" ) )
1070     {
1071         if( strlen( newval.psz_string) > 0)
1072         {
1073             val.i_int = atoi( newval.psz_string );
1074             var_Set( p_inp->p_libvlc, "mosaic-alpha", val );
1075         }
1076     }
1077     else if( !strcmp( psz_cmd, "mosaic-height" ) )
1078     {
1079         if( strlen( newval.psz_string) > 0)
1080         {
1081             val.i_int = atoi( newval.psz_string );
1082             var_Set( p_inp->p_libvlc, "mosaic-height", val );
1083         }
1084     }
1085     else if( !strcmp( psz_cmd, "mosaic-width" ) )
1086     {
1087         if( strlen( newval.psz_string) > 0)
1088         {
1089             val.i_int = atoi( newval.psz_string );
1090             var_Set( p_inp->p_libvlc, "mosaic-width", val );
1091         }
1092     }
1093     else if( !strcmp( psz_cmd, "mosaic-xoffset" ) )
1094     {
1095         if( strlen( newval.psz_string) > 0)
1096         {
1097             val.i_int = atoi( newval.psz_string );
1098             var_Set( p_inp->p_libvlc, "mosaic-xoffset", val );
1099         }
1100     }
1101     else if( !strcmp( psz_cmd, "mosaic-yoffset" ) )
1102     {
1103         if( strlen( newval.psz_string) > 0)
1104         {
1105             val.i_int = atoi( newval.psz_string );
1106             var_Set( p_inp->p_libvlc, "mosaic-yoffset", val );
1107         }
1108     }
1109     else if( !strcmp( psz_cmd, "mosaic-vborder" ) )
1110     {
1111         if( strlen( newval.psz_string) > 0)
1112         {
1113             val.i_int = atoi( newval.psz_string );
1114             var_Set( p_inp->p_libvlc, "mosaic-vborder", val );
1115         }
1116     }
1117     else if( !strcmp( psz_cmd, "mosaic-hborder" ) )
1118     {
1119         if( strlen( newval.psz_string) > 0)
1120         {
1121             val.i_int = atoi( newval.psz_string );
1122             var_Set( p_inp->p_libvlc, "mosaic-hborder", val );
1123         }
1124     }
1125     else if( !strcmp( psz_cmd, "mosaic-position" ) )
1126     {
1127         if( strlen( newval.psz_string) > 0)
1128         {
1129             val.i_int = atoi( newval.psz_string );
1130             var_Set( p_inp->p_libvlc, "mosaic-position", val );
1131         }
1132     }
1133     else if( !strcmp( psz_cmd, "mosaic-rows" ) )
1134     {
1135         if( strlen( newval.psz_string) > 0)
1136         {
1137             val.i_int = atoi( newval.psz_string );
1138             var_Set( p_inp->p_libvlc, "mosaic-rows", val );
1139         }
1140     }
1141     else if( !strcmp( psz_cmd, "mosaic-cols" ) )
1142     {
1143         if( strlen( newval.psz_string) > 0)
1144         {
1145             val.i_int = atoi( newval.psz_string );
1146             var_Set( p_inp->p_libvlc, "mosaic-cols", val );
1147         }
1148     }
1149     else if( !strcmp( psz_cmd, "mosaic-keep-aspect-ratio" ) )
1150     {
1151         if( strlen( newval.psz_string) > 0)
1152         {
1153             val.i_int = atoi( newval.psz_string );
1154             var_Set( p_inp->p_libvlc, "mosaic-keep-aspect-ratio", val );
1155         }
1156     }
1157     else if( !strcmp( psz_cmd, "time-format" ) )
1158     {
1159         if( strlen( newval.psz_string ) > 0 )
1160         {
1161             val.psz_string = newval.psz_string;
1162             var_Set( p_inp->p_libvlc, "time-format", val );
1163         }
1164         else 
1165         {
1166                 val.psz_string = "";
1167                 var_Set( p_inp->p_libvlc, "time-format", val);
1168         }
1169     }
1170     else if( !strcmp( psz_cmd, "time-x" ) )
1171     {
1172         if( strlen( newval.psz_string ) > 0) 
1173         {
1174             val.i_int = atoi( newval.psz_string );
1175             var_Set( p_inp->p_libvlc, "time-x", val );
1176         }
1177     }
1178     else if( !strcmp( psz_cmd, "time-y" ) )
1179     {
1180         if( strlen( newval.psz_string ) > 0) 
1181         {
1182             val.i_int = atoi( newval.psz_string );
1183             var_Set( p_inp->p_libvlc, "time-y", val );
1184         }
1185     }
1186     else if( !strcmp( psz_cmd, "time-position" ) )
1187     {
1188         if( strlen( newval.psz_string ) > 0) 
1189         {
1190             val.i_int = atoi( newval.psz_string );
1191             var_Set( p_inp->p_libvlc, "time-position", val );
1192         }
1193     }
1194     else if( !strcmp( psz_cmd, "time-color" ) )
1195     {
1196         if( strlen( newval.psz_string ) > 0) 
1197         {
1198             val.i_int = strtol( newval.psz_string, NULL, 0 );
1199             var_Set( p_inp->p_libvlc, "time-color", val );
1200         }
1201     }
1202     else if( !strcmp( psz_cmd, "time-opacity" ) )
1203     {
1204         if( strlen( newval.psz_string ) > 0) 
1205         {
1206             val.i_int = strtol( newval.psz_string, NULL, 0 );
1207             var_Set( p_inp->p_libvlc, "time-opacity", val );
1208         }
1209     }
1210     else if( !strcmp( psz_cmd, "time-size" ) )
1211     {
1212         if( strlen( newval.psz_string ) > 0) 
1213         {
1214             val.i_int = atoi( newval.psz_string );
1215             var_Set( p_inp->p_libvlc, "time-size", val );
1216         }
1217     }
1218
1219     /*
1220      * sanity check
1221      */
1222     else
1223     {
1224         msg_rc( "unknown command!\n" );
1225     }
1226
1227     vlc_object_release( p_pl );
1228     vlc_object_release( p_inp );
1229     return VLC_SUCCESS;
1230 }
1231
1232 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1233                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1234 {
1235     p_this->p_vlc->b_die = VLC_TRUE;
1236     return VLC_SUCCESS;
1237 }
1238
1239 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1240                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1241 {
1242     intf_thread_t *p_newintf;
1243
1244     p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
1245
1246     if( p_newintf )
1247     {
1248         p_newintf->b_block = VLC_FALSE;
1249         if( intf_RunThread( p_newintf ) )
1250         {
1251             vlc_object_detach( p_newintf );
1252             intf_Destroy( p_newintf );
1253         }
1254     }
1255
1256     return VLC_SUCCESS;
1257 }
1258
1259 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1260                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
1261 {
1262     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1263     int i_error;
1264
1265     if ( *newval.psz_string )
1266     {
1267         /* Set. */
1268         audio_volume_t i_volume = atoi( newval.psz_string );
1269         if ( i_volume > AOUT_VOLUME_MAX )
1270         {
1271             msg_rc( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
1272                     AOUT_VOLUME_MAX );
1273             i_error = VLC_EBADVAR;
1274         }
1275         else i_error = aout_VolumeSet( p_this, i_volume );
1276     }
1277     else
1278     {
1279         /* Get. */
1280         audio_volume_t i_volume;
1281         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1282         {
1283             i_error = VLC_EGENERIC;
1284         }
1285         else
1286         {
1287             msg_rc( "Volume is %d\n", i_volume );
1288             i_error = VLC_SUCCESS;
1289         }
1290     }
1291
1292     return i_error;
1293 }
1294
1295 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1296                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1297 {
1298     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1299     audio_volume_t i_volume;
1300     int i_nb_steps = atoi(newval.psz_string);
1301     int i_error = VLC_SUCCESS;
1302
1303     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
1304     {
1305         i_nb_steps = 1;
1306     }
1307
1308     if ( !strcmp(psz_cmd, "volup") )
1309     {
1310         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1311             i_error = VLC_EGENERIC;
1312     }
1313     else
1314     {
1315         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1316             i_error = VLC_EGENERIC;
1317     }
1318
1319     if ( !i_error ) msg_rc( "Volume is %d\n", i_volume );
1320     return i_error;
1321 }
1322
1323 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1324                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1325 {
1326     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1327     aout_instance_t * p_aout;
1328     const char * psz_variable;
1329     vlc_value_t val_name;
1330     int i_error;
1331
1332     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1333     if ( p_aout == NULL ) return VLC_ENOOBJ;
1334
1335     if ( !strcmp( psz_cmd, "adev" ) )
1336     {
1337         psz_variable = "audio-device";
1338     }
1339     else
1340     {
1341         psz_variable = "audio-channels";
1342     }
1343
1344     /* Get the descriptive name of the variable */
1345     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1346                  &val_name, NULL );
1347     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1348
1349     if ( !*newval.psz_string )
1350     {
1351         /* Retrieve all registered ***. */
1352         vlc_value_t val, text;
1353         int i, i_value;
1354
1355         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1356         {
1357             vlc_object_release( (vlc_object_t *)p_aout );
1358             return VLC_EGENERIC;
1359         }
1360         i_value = val.i_int;
1361
1362         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1363                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1364         {
1365             vlc_object_release( (vlc_object_t *)p_aout );
1366             return VLC_EGENERIC;
1367         }
1368
1369         msg_rc( "+----[ %s ]\n", val_name.psz_string );
1370         for ( i = 0; i < val.p_list->i_count; i++ )
1371         {
1372             if ( i_value == val.p_list->p_values[i].i_int )
1373                 msg_rc( "| %i - %s *\n", val.p_list->p_values[i].i_int,
1374                         text.p_list->p_values[i].psz_string );
1375             else
1376                 msg_rc( "| %i - %s\n", val.p_list->p_values[i].i_int,
1377                         text.p_list->p_values[i].psz_string );
1378         }
1379         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1380                     &val, &text );
1381         msg_rc( "+----[ end of %s ]\n", val_name.psz_string );
1382
1383         if( val_name.psz_string ) free( val_name.psz_string );
1384         i_error = VLC_SUCCESS;
1385     }
1386     else
1387     {
1388         vlc_value_t val;
1389         val.i_int = atoi( newval.psz_string );
1390
1391         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1392     }
1393     vlc_object_release( (vlc_object_t *)p_aout );
1394
1395     return i_error;
1396 }
1397
1398 #ifdef WIN32
1399 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1400 {
1401     INPUT_RECORD input_record;
1402     DWORD i_dw;
1403
1404     /* On Win32, select() only works on socket descriptors */
1405     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1406                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1407     {
1408         while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1409                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1410                                  1, &i_dw ) )
1411         {
1412             if( input_record.EventType != KEY_EVENT ||
1413                 !input_record.Event.KeyEvent.bKeyDown ||
1414                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1415                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1416                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1417                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1418             {
1419                 /* nothing interesting */
1420                 continue;
1421             }
1422
1423             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1424
1425             /* Echo out the command */
1426             putc( p_buffer[ *pi_size ], stdout );
1427
1428             /* Handle special keys */
1429             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1430             {
1431                 putc( '\n', stdout );
1432                 break;
1433             }
1434             switch( p_buffer[ *pi_size ] )
1435             {
1436             case '\b':
1437                 if( *pi_size )
1438                 {
1439                     *pi_size -= 2;
1440                     putc( ' ', stdout );
1441                     putc( '\b', stdout );
1442                 }
1443                 break;
1444             case '\r':
1445                 (*pi_size) --;
1446                 break;
1447             }
1448
1449             (*pi_size)++;
1450         }
1451
1452         if( *pi_size == MAX_LINE_LENGTH ||
1453             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1454         {
1455             p_buffer[ *pi_size ] = 0;
1456             return VLC_TRUE;
1457         }
1458     }
1459
1460     return VLC_FALSE;
1461 }
1462 #endif
1463
1464 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1465 {
1466     int i_read = 0;
1467
1468 #ifdef WIN32
1469     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1470         return ReadWin32( p_intf, p_buffer, pi_size );
1471     else if( p_intf->p_sys->i_socket == -1 )
1472     {
1473         msleep( INTF_IDLE_SLEEP );
1474         return VLC_FALSE;
1475     }
1476 #endif
1477
1478     while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1479            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
1480                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
1481                        p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
1482     {
1483         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1484             break;
1485
1486         (*pi_size)++;
1487     }
1488
1489     /* Connection closed */
1490     if( i_read == -1 )
1491     {
1492         p_intf->p_sys->i_socket = -1;
1493         p_buffer[ *pi_size ] = 0;
1494         return VLC_TRUE;
1495     }
1496
1497     if( *pi_size == MAX_LINE_LENGTH ||
1498         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1499     {
1500         p_buffer[ *pi_size ] = 0;
1501         return VLC_TRUE;
1502     }
1503
1504     return VLC_FALSE;
1505 }
1506
1507 /*****************************************************************************
1508  * parse_MRL: build a playlist item from a full mrl
1509  *****************************************************************************
1510  * MRL format: "simplified-mrl [:option-name[=option-value]]"
1511  * We don't check for '"' or '\'', we just assume that a ':' that follows a
1512  * space is a new option. Should be good enough for our purpose.
1513  *****************************************************************************/
1514 static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
1515 {
1516 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
1517 #define SKIPTRAILINGSPACE( p, d ) \
1518     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1519
1520     playlist_item_t *p_item = NULL;
1521     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
1522     char **ppsz_options = NULL;
1523     int i, i_options = 0;
1524
1525     if( !psz_mrl ) return 0;
1526
1527     psz_mrl = psz_orig = strdup( psz_mrl );
1528     while( *psz_mrl )
1529     {
1530         SKIPSPACE( psz_mrl );
1531         psz_item = psz_mrl;
1532
1533         for( ; *psz_mrl; psz_mrl++ )
1534         {
1535             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
1536             {
1537                 /* We have a complete item */
1538                 break;
1539             }
1540             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
1541                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
1542             {
1543                 /* We have a complete item */
1544                 break;
1545             }
1546         }
1547
1548         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
1549         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
1550
1551         /* Remove '"' and '\'' if necessary */
1552         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
1553         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1554         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
1555         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1556
1557         if( !psz_item_mrl ) psz_item_mrl = psz_item;
1558         else if( *psz_item )
1559         {
1560             i_options++;
1561             ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
1562             ppsz_options[i_options - 1] = &psz_item[1];
1563         }
1564
1565         if( *psz_mrl ) SKIPSPACE( psz_mrl );
1566     }
1567
1568     /* Now create a playlist item */
1569     if( psz_item_mrl )
1570     {
1571         p_item = playlist_ItemNew( p_intf, psz_item_mrl, psz_item_mrl );
1572         for( i = 0; i < i_options; i++ )
1573         {
1574             playlist_ItemAddOption( p_item, ppsz_options[i] );
1575         }
1576     }
1577
1578     if( i_options ) free( ppsz_options );
1579     free( psz_orig );
1580
1581     return p_item;
1582 }