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