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