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