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