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