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