]> git.sesse.net Git - vlc/blob - modules/control/rc.c
Some infrastructure work for playlist autoload/autosave
[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, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
364     var_AddCallback( p_intf, "play", Playlist, NULL );
365     var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
366     var_AddCallback( p_intf, "stop", Playlist, NULL );
367     var_Create( p_intf, "clear", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
368     var_AddCallback( p_intf, "clear", Playlist, NULL );
369     var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
370     var_AddCallback( p_intf, "prev", Playlist, NULL );
371     var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
372     var_AddCallback( p_intf, "next", Playlist, NULL );
373     var_Create( p_intf, "goto", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
374     var_AddCallback( p_intf, "goto", Playlist, NULL );
375     var_Create( p_intf, "status", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
376     var_AddCallback( p_intf, "status", Playlist, NULL );
377
378     /* marquee on the fly items */
379     var_Create( p_intf, "marq-marquee", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
380     var_AddCallback( p_intf, "marq-marquee", Other, NULL );
381     var_Create( p_intf, "marq-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
382     var_AddCallback( p_intf, "marq-x", Other, NULL );
383     var_Create( p_intf, "marq-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
384     var_AddCallback( p_intf, "marq-y", Other, NULL );
385     var_Create( p_intf, "marq-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
386     var_AddCallback( p_intf, "marq-position", Other, NULL );
387     var_Create( p_intf, "marq-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
388     var_AddCallback( p_intf, "marq-color", Other, NULL );
389     var_Create( p_intf, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
390     var_AddCallback( p_intf, "marq-opacity", Other, NULL );
391     var_Create( p_intf, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
392     var_AddCallback( p_intf, "marq-timeout", Other, NULL );
393     var_Create( p_intf, "marq-size", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
394     var_AddCallback( p_intf, "marq-size", Other, NULL );
395
396     var_Create( p_intf, "mosaic-alpha", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
397     var_AddCallback( p_intf, "mosaic-alpha", Other, NULL );
398     var_Create( p_intf, "mosaic-height", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
399     var_AddCallback( p_intf, "mosaic-height", Other, NULL );
400     var_Create( p_intf, "mosaic-width", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
401     var_AddCallback( p_intf, "mosaic-width", Other, NULL );
402     var_Create( p_intf, "mosaic-xoffset", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
403     var_AddCallback( p_intf, "mosaic-xoffset", Other, NULL );
404     var_Create( p_intf, "mosaic-yoffset", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
405     var_AddCallback( p_intf, "mosaic-yoffset", Other, NULL );
406     var_Create( p_intf, "mosaic-align", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
407     var_AddCallback( p_intf, "mosaic-align", Other, NULL );
408     var_Create( p_intf, "mosaic-vborder", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
409     var_AddCallback( p_intf, "mosaic-vborder", Other, NULL );
410     var_Create( p_intf, "mosaic-hborder", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
411     var_AddCallback( p_intf, "mosaic-hborder", Other, NULL );
412     var_Create( p_intf, "mosaic-position",
413                      VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
414     var_AddCallback( p_intf, "mosaic-position", Other, NULL );
415     var_Create( p_intf, "mosaic-rows", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
416     var_AddCallback( p_intf, "mosaic-rows", Other, NULL );
417     var_Create( p_intf, "mosaic-cols", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
418     var_AddCallback( p_intf, "mosaic-cols", Other, NULL );
419     var_Create( p_intf, "mosaic-order", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
420     var_AddCallback( p_intf, "mosaic-order", Other, NULL );
421     var_Create( p_intf, "mosaic-keep-aspect-ratio",
422                      VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
423     var_AddCallback( p_intf, "mosaic-keep-aspect-ratio", Other, NULL );
424
425     /* time on the fly items */
426     var_Create( p_intf, "time-format", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
427     var_AddCallback( p_intf, "time-format", Other, NULL );
428     var_Create( p_intf, "time-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
429     var_AddCallback( p_intf, "time-x", Other, NULL );
430     var_Create( p_intf, "time-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
431     var_AddCallback( p_intf, "time-y", Other, NULL );
432     var_Create( p_intf, "time-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
433     var_AddCallback( p_intf, "time-position", Other, NULL );
434     var_Create( p_intf, "time-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
435     var_AddCallback( p_intf, "time-color", Other, NULL );
436     var_Create( p_intf, "time-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
437     var_AddCallback( p_intf, "time-opacity", Other, NULL );
438     var_Create( p_intf, "time-size", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
439     var_AddCallback( p_intf, "time-size", Other, NULL );
440
441     /* logo on the fly items */
442     var_Create( p_intf, "logo-file", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
443     var_AddCallback( p_intf, "logo-file", Other, NULL );
444     var_Create( p_intf, "logo-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
445     var_AddCallback( p_intf, "logo-x", Other, NULL );
446     var_Create( p_intf, "logo-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
447     var_AddCallback( p_intf, "logo-y", Other, NULL );
448     var_Create( p_intf, "logo-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
449     var_AddCallback( p_intf, "logo-position", Other, NULL );
450     var_Create( p_intf, "logo-transparency", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
451     var_AddCallback( p_intf, "logo-transparency", Other, NULL );
452
453     /* OSD menu commands */
454     var_Create( p_intf, "menu", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
455     var_AddCallback( p_intf, "menu", Menu, NULL );
456
457     /* DVD commands */
458     var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
459     var_AddCallback( p_intf, "pause", Input, NULL );
460     var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
461     var_AddCallback( p_intf, "seek", Input, NULL );
462     var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
463     var_AddCallback( p_intf, "title", Input, NULL );
464     var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
465     var_AddCallback( p_intf, "title_n", Input, NULL );
466     var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
467     var_AddCallback( p_intf, "title_p", Input, NULL );
468     var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
469     var_AddCallback( p_intf, "chapter", Input, NULL );
470     var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
471     var_AddCallback( p_intf, "chapter_n", Input, NULL );
472     var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
473     var_AddCallback( p_intf, "chapter_p", Input, NULL );
474
475     var_Create( p_intf, "fastforward", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
476     var_AddCallback( p_intf, "fastforward", Input, NULL );
477     var_Create( p_intf, "rewind", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
478     var_AddCallback( p_intf, "rewind", Input, NULL );
479     var_Create( p_intf, "faster", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
480     var_AddCallback( p_intf, "faster", Input, NULL );
481     var_Create( p_intf, "slower", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
482     var_AddCallback( p_intf, "slower", Input, NULL );
483     var_Create( p_intf, "normal", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
484     var_AddCallback( p_intf, "normal", Input, NULL );
485
486     /* audio commands */
487     var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
488     var_AddCallback( p_intf, "volume", Volume, NULL );
489     var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
490     var_AddCallback( p_intf, "volup", VolumeMove, NULL );
491     var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
492     var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
493     var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
494     var_AddCallback( p_intf, "adev", AudioConfig, NULL );
495     var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
496     var_AddCallback( p_intf, "achan", AudioConfig, NULL );
497 }
498
499 /*****************************************************************************
500  * Run: rc thread
501  *****************************************************************************
502  * This part of the interface is in a separate thread so that we can call
503  * exec() from within it without annoying the rest of the program.
504  *****************************************************************************/
505 static void Run( intf_thread_t *p_intf )
506 {
507     input_thread_t * p_input;
508     playlist_t *     p_playlist;
509
510     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
511     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
512     vlc_bool_t b_longhelp = VLC_FALSE;
513
514     int        i_size = 0;
515     int        i_oldpos = 0;
516     int        i_newpos;
517
518     p_buffer[0] = 0;
519     p_input = NULL;
520     p_playlist = NULL;
521
522     /* Register commands that will be cleaned up upon object destruction */
523     RegisterCallbacks( p_intf );
524
525     /* status callbacks */
526     /* Listen to audio volume updates */
527     var_AddCallback( p_intf->p_vlc, "volume-change", VolumeChanged, p_intf );
528
529 #ifdef WIN32
530     /* Get the file descriptor of the console input */
531     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
532     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
533     {
534         msg_Err( p_intf, "couldn't find user input handle" );
535         p_intf->b_die = VLC_TRUE;
536     }
537 #endif
538
539     while( !p_intf->b_die )
540     {
541         char *psz_cmd, *psz_arg;
542         vlc_bool_t b_complete;
543
544         if( p_intf->p_sys->pi_socket_listen != NULL &&
545             p_intf->p_sys->i_socket == -1 )
546         {
547             p_intf->p_sys->i_socket =
548                 net_Accept( p_intf, p_intf->p_sys->pi_socket_listen, 0 );
549         }
550
551         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
552
553         /* Manage the input part */
554         if( p_input == NULL )
555         {
556             if( p_playlist )
557             {
558                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
559                                                        FIND_CHILD );
560             }
561             else
562             {
563                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
564                                                    FIND_ANYWHERE );
565                 if( p_input )
566                 {
567                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
568                                                            FIND_PARENT );
569                 }
570             }
571             /* New input has been registered */
572             if( p_input )
573             {
574                 if( !p_input->b_dead || !p_input->b_die )
575                 {
576                     msg_rc( STATUS_CHANGE "( new input: %s )", p_input->input.p_item->psz_uri );
577                     msg_rc( STATUS_CHANGE "( audio volume: %d )", config_GetInt( p_intf, "volume" ));
578                 }
579                 var_AddCallback( p_input, "state", StateChanged, p_intf );
580                 var_AddCallback( p_input, "rate-faster", RateChanged, p_intf );
581                 var_AddCallback( p_input, "rate-slower", RateChanged, p_intf );
582                 var_AddCallback( p_input, "rate", RateChanged, p_intf );
583                 var_AddCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
584             }
585         }
586         else if( p_input->b_dead )
587         {
588             var_DelCallback( p_input, "state", StateChanged, p_intf );
589             var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
590             var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
591             var_DelCallback( p_input, "rate", RateChanged, p_intf );
592             var_DelCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
593             vlc_object_release( p_input );
594             p_input = NULL;
595
596             if( p_playlist )
597             {
598                 vlc_mutex_lock( &p_playlist->object_lock );
599                 p_intf->p_sys->i_last_state = (int) PLAYLIST_STOPPED;
600                 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
601                 vlc_mutex_unlock( &p_playlist->object_lock );
602             }
603         }
604
605         if( (p_input != NULL) && !p_input->b_dead && !p_input->b_die &&
606             (p_playlist != NULL) )
607         {
608             vlc_mutex_lock( &p_playlist->object_lock );
609             if( (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
610                 (p_playlist->status.i_status == PLAYLIST_STOPPED) )
611             {
612                 p_intf->p_sys->i_last_state = PLAYLIST_STOPPED;
613                 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
614             }
615             else if( (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
616                 (p_playlist->status.i_status == PLAYLIST_RUNNING) )
617             {
618                 p_intf->p_sys->i_last_state = p_playlist->status.i_status;
619                 msg_rc( STATUS_CHANGE "( play state: 1 )" );
620             }
621             else if( (p_intf->p_sys->i_last_state != p_playlist->status.i_status) &&
622                 (p_playlist->status.i_status == PLAYLIST_PAUSED) )
623             {
624                 p_intf->p_sys->i_last_state = p_playlist->status.i_status;
625                 msg_rc( STATUS_CHANGE "( pause state: 2 )" );
626             }
627             vlc_mutex_unlock( &p_playlist->object_lock );
628         }
629
630         if( p_input && b_showpos )
631         {
632             i_newpos = 100 * var_GetFloat( p_input, "position" );
633             if( i_oldpos != i_newpos )
634             {
635                 i_oldpos = i_newpos;
636                 msg_rc( "pos: %d%%", i_newpos );
637             }
638         }
639
640         /* Is there something to do? */
641         if( !b_complete ) continue;
642
643
644         /* Skip heading spaces */
645         psz_cmd = p_buffer;
646         while( *psz_cmd == ' ' )
647         {
648             psz_cmd++;
649         }
650
651         /* Split psz_cmd at the first space and make sure that
652          * psz_arg is valid */
653         psz_arg = strchr( psz_cmd, ' ' );
654         if( psz_arg )
655         {
656             *psz_arg++ = 0;
657             while( *psz_arg == ' ' )
658             {
659                 psz_arg++;
660             }
661         }
662         else
663         {
664             psz_arg = "";
665         }
666
667         /* If the user typed a registered local command, try it */
668         if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
669         {
670             vlc_value_t val;
671             int i_ret;
672
673             val.psz_string = psz_arg;
674             i_ret = var_Set( p_intf, psz_cmd, val );
675             msg_rc( "%s: returned %i (%s)",
676                     psz_cmd, i_ret, vlc_error( i_ret ) );
677         }
678         /* Or maybe it's a global command */
679         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
680         {
681             vlc_value_t val;
682             int i_ret;
683
684             val.psz_string = psz_arg;
685             /* FIXME: it's a global command, but we should pass the
686              * local object as an argument, not p_intf->p_libvlc. */
687             i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
688             if( i_ret != 0 )
689             {
690                 msg_rc( "%s: returned %i (%s)",
691                          psz_cmd, i_ret, vlc_error( i_ret ) );
692             }
693         }
694         else if( !strcmp( psz_cmd, "logout" ) )
695         {
696             /* Close connection */
697             if( p_intf->p_sys->i_socket != -1 )
698             {
699                 net_Close( p_intf->p_sys->i_socket );
700             }
701             p_intf->p_sys->i_socket = -1;
702         }
703         else if( !strcmp( psz_cmd, "info" ) )
704         {
705             if( p_input )
706             {
707                 int i, j;
708                 vlc_mutex_lock( &p_input->input.p_item->lock );
709                 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
710                 {
711                     info_category_t *p_category =
712                         p_input->input.p_item->pp_categories[i];
713
714                     msg_rc( "+----[ %s ]", p_category->psz_name );
715                     msg_rc( "| " );
716                     for ( j = 0; j < p_category->i_infos; j++ )
717                     {
718                         info_t *p_info = p_category->pp_infos[j];
719                         msg_rc( "| %s: %s", p_info->psz_name,
720                                 p_info->psz_value );
721                     }
722                     msg_rc( "| " );
723                 }
724                 msg_rc( "+----[ end of stream info ]" );
725                 vlc_mutex_unlock( &p_input->input.p_item->lock );
726             }
727             else
728             {
729                 msg_rc( "no input" );
730             }
731         }
732         else if( !strcmp( psz_cmd, "is_playing" ) )
733         {
734             if( ! p_input )
735             {
736                 msg_rc( "0" );
737             }
738             else
739             {
740                 msg_rc( "1" );
741             }
742         }
743         else if( !strcmp( psz_cmd, "get_time" ) )
744         {
745             if( ! p_input )
746             {
747                 msg_rc("0");
748             }
749             else
750             {
751                 vlc_value_t time;
752                 var_Get( p_input, "time", &time );
753                 msg_rc( "%i", time.i_time / 1000000);
754             }
755         }
756         else if( !strcmp( psz_cmd, "get_length" ) )
757         {
758             if( ! p_input )
759             {
760                 msg_rc("0");
761             }
762             else
763             {
764                 vlc_value_t time;
765                 var_Get( p_input, "length", &time );
766                 msg_rc( "%i", time.i_time / 1000000);
767             }
768         }
769         else if( !strcmp( psz_cmd, "get_title" ) )
770         {
771             if( ! p_input )
772             {
773                 msg_rc("");
774             }
775             else
776             {
777                 msg_rc( "%s", p_input->input.p_item->psz_name );
778             }
779         }
780         else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
781                  || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
782         {
783             if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "H", 1 ) )
784                  b_longhelp = VLC_TRUE;
785             else b_longhelp = VLC_FALSE;
786
787             Help( p_intf, b_longhelp );
788         }
789         else if( !strcmp( psz_cmd, "check-updates" ) )
790         {
791             checkUpdates( p_intf, psz_arg );
792         }
793         else switch( psz_cmd[0] )
794         {
795         case 'f':
796         case 'F':
797             if( p_input )
798             {
799                 vout_thread_t *p_vout;
800                 p_vout = vlc_object_find( p_input,
801                                           VLC_OBJECT_VOUT, FIND_CHILD );
802
803                 if( p_vout )
804                 {
805                     vlc_value_t val;
806                     vlc_bool_t b_update = VLC_FALSE;
807                     var_Get( p_vout, "fullscreen", &val );
808                     val.b_bool = !val.b_bool;
809                     if( !strncmp(psz_arg, "on", 2) && (val.b_bool == VLC_TRUE) )
810                     {
811                         b_update = VLC_TRUE;
812                         val.b_bool = VLC_TRUE;
813                     }
814                     else if( !strncmp(psz_arg, "off", 3)  && (val.b_bool == VLC_FALSE) )
815                     {
816                         b_update = VLC_TRUE;
817                         val.b_bool = VLC_FALSE;
818                     }
819                     else if( strncmp(psz_arg, "off", 3) && strncmp(psz_arg, "on", 2) )
820                         b_update = VLC_TRUE;
821                     if( b_update ) var_Set( p_vout, "fullscreen", val );
822                     vlc_object_release( p_vout );
823                 }
824             }
825             break;
826
827         case 's':
828         case 'S':
829             ;
830             break;
831
832         case '\0':
833             /* Ignore empty lines */
834             break;
835
836         default:
837             msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
838             break;
839         }
840
841         /* Command processed */
842         i_size = 0; p_buffer[0] = 0;
843     }
844
845     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
846     msg_rc( STATUS_CHANGE "( quit )" );
847
848     if( p_input )
849     {
850         var_DelCallback( p_input, "state", StateChanged, p_intf );
851         var_DelCallback( p_input, "rate-faster", RateChanged, p_intf );
852         var_DelCallback( p_input, "rate-slower", RateChanged, p_intf );
853         var_DelCallback( p_input, "rate", RateChanged, p_intf );
854         var_DelCallback( p_input, "time-offset", TimeOffsetChanged, p_intf );
855         vlc_object_release( p_input );
856         p_input = NULL;
857     }
858
859     if( p_playlist )
860     {
861         vlc_object_release( p_playlist );
862         p_playlist = NULL;
863     }
864
865     var_DelCallback( p_intf->p_vlc, "volume-change", VolumeChanged, p_intf );
866 }
867
868 static void Help( intf_thread_t *p_intf, vlc_bool_t b_longhelp)
869 {
870     msg_rc(_("+----[ Remote control commands ]"));
871     msg_rc(  "| ");
872     msg_rc(_("| add XYZ  . . . . . . . . . . add XYZ to playlist"));
873     msg_rc(_("| playlist . . .  show items currently in playlist"));
874     msg_rc(_("| play . . . . . . . . . . . . . . . . play stream"));
875     msg_rc(_("| stop . . . . . . . . . . . . . . . . stop stream"));
876     msg_rc(_("| next . . . . . . . . . . . .  next playlist item"));
877     msg_rc(_("| prev . . . . . . . . . .  previous playlist item"));
878     msg_rc(_("| goto . . . . . . . . . . . .  goto item at index"));
879     msg_rc(_("| clear . . . . . . . . . . .   clear the playlist"));
880     msg_rc(_("| status . . . . . . . . . current playlist status"));
881     msg_rc(_("| title [X]  . . . . set/get title in current item"));
882     msg_rc(_("| title_n  . . . . . .  next title in current item"));
883     msg_rc(_("| title_p  . . . .  previous title in current item"));
884     msg_rc(_("| chapter [X]  . . set/get chapter in current item"));
885     msg_rc(_("| chapter_n  . . . .  next chapter in current item"));
886     msg_rc(_("| chapter_p  . .  previous chapter in current item"));
887     msg_rc(  "| ");
888     msg_rc(_("| seek X . seek in seconds, for instance `seek 12'"));
889     msg_rc(_("| pause  . . . . . . . . . . . . . .  toggle pause"));
890     msg_rc(_("| fastforward  . . . . . .  .  set to maximum rate"));
891     msg_rc(_("| rewind  . . . . . . . . . .  set to minimum rate"));
892     msg_rc(_("| faster . . . . . . . .  faster playing of stream"));
893     msg_rc(_("| slower . . . . . . . .  slower playing of stream"));
894     msg_rc(_("| normal . . . . . . . .  normal playing of stream"));
895     msg_rc(_("| f [on|off] . . . . . . . . . . toggle fullscreen"));
896     msg_rc(_("| info . . .  information about the current stream"));
897     msg_rc(_("| get_time . . seconds elapsed since stream's beginning"));
898     msg_rc(_("| is_playing . .  1 if a stream plays, 0 otherwise"));
899     msg_rc(_("| get_title . . .  the title of the current stream"));
900     msg_rc(_("| get_length . .  the length of the current stream"));
901     msg_rc(  "| ");
902     msg_rc(_("| volume [X] . . . . . . . .  set/get audio volume"));
903     msg_rc(_("| volup [X]  . . . . .  raise audio volume X steps"));
904     msg_rc(_("| voldown [X]  . . . .  lower audio volume X steps"));
905     msg_rc(_("| adev [X] . . . . . . . . .  set/get audio device"));
906     msg_rc(_("| achan [X]. . . . . . . .  set/get audio channels"));
907     msg_rc(_("| menu [on|off|up|down|left|right|select] use menu"));
908     msg_rc(  "| ");
909
910     if (b_longhelp)
911     {
912         msg_rc(_("| marq-marquee STRING  . . overlay STRING in video"));
913         msg_rc(_("| marq-x X . . . . . . . . . . . .offset from left"));
914         msg_rc(_("| marq-y Y . . . . . . . . . . . . offset from top"));
915         msg_rc(_("| marq-position #. . .  .relative position control"));
916         msg_rc(_("| marq-color # . . . . . . . . . . font color, RGB"));
917         msg_rc(_("| marq-opacity # . . . . . . . . . . . . . opacity"));
918         msg_rc(_("| marq-timeout T. . . . . . . . . . timeout, in ms"));
919         msg_rc(_("| marq-size # . . . . . . . . font size, in pixels"));
920         msg_rc(  "| ");
921         msg_rc(_("| time-format STRING . . . overlay STRING in video"));
922         msg_rc(_("| time-x X . . . . . . . . . . . .offset from left"));
923         msg_rc(_("| time-y Y . . . . . . . . . . . . offset from top"));
924         msg_rc(_("| time-position #. . . . . . . . relative position"));
925         msg_rc(_("| time-color # . . . . . . . . . . font color, RGB"));
926         msg_rc(_("| time-opacity # . . . . . . . . . . . . . opacity"));
927         msg_rc(_("| time-size # . . . . . . . . font size, in pixels"));
928         msg_rc(  "| ");
929         msg_rc(_("| logo-file STRING . . .the overlay file path/name"));
930         msg_rc(_("| logo-x X . . . . . . . . . . . .offset from left"));
931         msg_rc(_("| logo-y Y . . . . . . . . . . . . offset from top"));
932         msg_rc(_("| logo-position #. . . . . . . . relative position"));
933         msg_rc(_("| logo-transparency #. . . . . . . . .transparency"));
934         msg_rc(  "| ");
935         msg_rc(_("| mosaic-alpha # . . . . . . . . . . . . . . alpha"));
936         msg_rc(_("| mosaic-height #. . . . . . . . . . . . . .height"));
937         msg_rc(_("| mosaic-width # . . . . . . . . . . . . . . width"));
938         msg_rc(_("| mosaic-xoffset # . . . .top left corner position"));
939         msg_rc(_("| mosaic-yoffset # . . . .top left corner position"));
940         msg_rc(_("| mosaic-align 0..2,4..6,8..10. . .mosaic alignment"));
941         msg_rc(_("| mosaic-vborder # . . . . . . . . vertical border"));
942         msg_rc(_("| mosaic-hborder # . . . . . . . horizontal border"));
943         msg_rc(_("| mosaic-position {0=auto,1=fixed} . . . .position"));
944         msg_rc(_("| mosaic-rows #. . . . . . . . . . .number of rows"));
945         msg_rc(_("| mosaic-cols #. . . . . . . . . . .number of cols"));
946         msg_rc(_("| mosaic-order id(,id)* . . . . order of pictures "));
947         msg_rc(_("| mosaic-keep-aspect-ratio {0,1} . . .aspect ratio"));
948         msg_rc(  "| ");
949         msg_rc(_("| check-updates [newer] [equal] [older]\n"
950                  "|               [undef] [info] [source] [binary] [plugin]"));
951         msg_rc(  "| ");
952     }
953     msg_rc(_("| help . . . . . . . . . . . . . this help message"));
954     msg_rc(_("| longhelp . . . . . . . . . a longer help message"));
955     msg_rc(_("| logout . . . . .  exit (if in socket connection)"));
956     msg_rc(_("| quit . . . . . . . . . . . . . . . . .  quit vlc"));
957     msg_rc(  "| ");
958     msg_rc(_("+----[ end of help ]"));
959 }
960
961 /********************************************************************
962  * Status callback routines
963  ********************************************************************/
964 static int TimeOffsetChanged( vlc_object_t *p_this, char const *psz_cmd,
965     vlc_value_t oldval, vlc_value_t newval, void *p_data )
966 {
967     intf_thread_t *p_intf = (intf_thread_t*)p_data;
968     input_thread_t *p_input = NULL;
969
970     vlc_mutex_lock( &p_intf->p_sys->status_lock );
971     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
972     if( p_input )
973     {
974         msg_rc( STATUS_CHANGE "( time-offset: %d )", var_GetInteger( p_input, "time-offset" ) );
975         vlc_object_release( p_input );
976     }
977     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
978     return VLC_SUCCESS;
979 }
980
981 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
982     vlc_value_t oldval, vlc_value_t newval, void *p_data )
983 {
984     intf_thread_t *p_intf = (intf_thread_t*)p_data;
985
986     vlc_mutex_lock( &p_intf->p_sys->status_lock );
987     msg_rc( STATUS_CHANGE "( audio volume: %d )", config_GetInt( p_this, "volume") );
988     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
989     return VLC_SUCCESS;
990 }
991
992 static int StateChanged( vlc_object_t *p_this, char const *psz_cmd,
993     vlc_value_t oldval, vlc_value_t newval, void *p_data )
994 {
995     intf_thread_t *p_intf = (intf_thread_t*)p_data;
996     playlist_t    *p_playlist = NULL;
997     input_thread_t *p_input = NULL;
998
999     vlc_mutex_lock( &p_intf->p_sys->status_lock );
1000     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1001     if( p_input )
1002     {
1003         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT );
1004         if( p_playlist )
1005         {
1006             char cmd[5] = "";
1007             switch( p_playlist->status.i_status )
1008             {
1009             case PLAYLIST_STOPPED:
1010                 strncpy( &cmd[0], "stop", 4);
1011                 cmd[4] = '\0';
1012                 break;
1013             case PLAYLIST_RUNNING:
1014                 strncpy( &cmd[0], "play", 4);
1015                 cmd[4] = '\0';
1016                 break;
1017             case PLAYLIST_PAUSED:
1018                 strncpy( &cmd[0], "pause", 5);
1019                 cmd[5] = '\0';
1020                 break;
1021             } /* var_GetInteger( p_input, "state" )  */
1022             msg_rc( STATUS_CHANGE "( %s state: %d )", &cmd[0], newval.i_int );
1023             vlc_object_release( p_playlist );
1024         }
1025         vlc_object_release( p_input );
1026     }
1027     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
1028     return VLC_SUCCESS;
1029 }
1030
1031 static int RateChanged( vlc_object_t *p_this, char const *psz_cmd,
1032     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1033 {
1034     intf_thread_t *p_intf = (intf_thread_t*)p_data;
1035     input_thread_t *p_input = NULL;
1036
1037     vlc_mutex_lock( &p_intf->p_sys->status_lock );
1038     p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1039     if( p_input )
1040     {
1041         msg_rc( STATUS_CHANGE "( new rate: %d )", var_GetInteger( p_input, "rate" ) );
1042         vlc_object_release( p_input );
1043     }
1044     vlc_mutex_unlock( &p_intf->p_sys->status_lock );
1045     return VLC_SUCCESS;
1046 }
1047
1048 /********************************************************************
1049  * Command routines
1050  ********************************************************************/
1051 static int Input( vlc_object_t *p_this, char const *psz_cmd,
1052                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
1053 {
1054     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1055     input_thread_t *p_input;
1056     vlc_value_t     val;
1057
1058     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1059     if( !p_input ) return VLC_ENOOBJ;
1060
1061     var_Get( p_input, "state", &val );
1062     if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1063         ( strcmp( psz_cmd, "pause" ) != 0 ) )
1064     {
1065         msg_rc( _("Press menu select or pause to continue.") );
1066         vlc_object_release( p_input );
1067         return VLC_EGENERIC;
1068     }
1069
1070     /* Parse commands that only require an input */
1071     if( !strcmp( psz_cmd, "pause" ) )
1072     {
1073         val.i_int = config_GetInt( p_intf, "key-play-pause" );
1074         var_Set( p_intf->p_vlc, "key-pressed", val );
1075         vlc_object_release( p_input );
1076         return VLC_SUCCESS;
1077     }
1078     else if( !strcmp( psz_cmd, "seek" ) )
1079     {
1080         if( strlen( newval.psz_string ) > 0 &&
1081             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
1082         {
1083             val.f_float = (float)atoi( newval.psz_string ) / 100.0;
1084             var_Set( p_input, "position", val );
1085         }
1086         else
1087         {
1088             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
1089             var_Set( p_input, "time", val );
1090         }
1091         vlc_object_release( p_input );
1092         return VLC_SUCCESS;
1093     }
1094     else if ( !strcmp( psz_cmd, "fastforward" ) )
1095     {
1096         val.i_int = config_GetInt( p_intf, "key-jump+extrashort" );
1097         var_Set( p_intf->p_vlc, "key-pressed", val );
1098         vlc_object_release( p_input );
1099         return VLC_SUCCESS;
1100     }
1101     else if ( !strcmp( psz_cmd, "rewind" ) )
1102     {
1103         val.i_int = config_GetInt( p_intf, "key-jump-extrashort" );
1104         var_Set( p_intf->p_vlc, "key-pressed", val );
1105         vlc_object_release( p_input );
1106         return VLC_SUCCESS;
1107     }
1108     else if ( !strcmp( psz_cmd, "faster" ) )
1109     {
1110         val.b_bool = VLC_TRUE;
1111         var_Set( p_input, "rate-faster", val );
1112         vlc_object_release( p_input );
1113         return VLC_SUCCESS;
1114     }
1115     else if ( !strcmp( psz_cmd, "slower" ) )
1116     {
1117         val.b_bool = VLC_TRUE;
1118         var_Set( p_input, "rate-slower", val );
1119         vlc_object_release( p_input );
1120         return VLC_SUCCESS;
1121     }
1122     else if ( !strcmp( psz_cmd, "normal" ) )
1123     {
1124         val.i_int = INPUT_RATE_DEFAULT;
1125         var_Set( p_input, "rate", val );
1126         vlc_object_release( p_input );
1127         return VLC_SUCCESS;
1128     }
1129     else if( !strcmp( psz_cmd, "chapter" ) ||
1130              !strcmp( psz_cmd, "chapter_n" ) ||
1131              !strcmp( psz_cmd, "chapter_p" ) )
1132     {
1133         if( !strcmp( psz_cmd, "chapter" ) )
1134         {
1135             if ( *newval.psz_string )
1136             {
1137                 /* Set. */
1138                 val.i_int = atoi( newval.psz_string );
1139                 var_Set( p_input, "chapter", val );
1140             }
1141             else
1142             {
1143                 vlc_value_t val_list;
1144
1145                 /* Get. */
1146                 var_Get( p_input, "chapter", &val );
1147                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
1148                             &val_list, NULL );
1149                 msg_rc( "Currently playing chapter %d/%d.",
1150                         val.i_int, val_list.p_list->i_count );
1151                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
1152                             &val_list, NULL );
1153             }
1154         }
1155         else if( !strcmp( psz_cmd, "chapter_n" ) )
1156         {
1157             val.b_bool = VLC_TRUE;
1158             var_Set( p_input, "next-chapter", val );
1159         }
1160         else if( !strcmp( psz_cmd, "chapter_p" ) )
1161         {
1162             val.b_bool = VLC_TRUE;
1163             var_Set( p_input, "prev-chapter", val );
1164         }
1165         vlc_object_release( p_input );
1166         return VLC_SUCCESS;
1167     }
1168     else if( !strcmp( psz_cmd, "title" ) ||
1169              !strcmp( psz_cmd, "title_n" ) ||
1170              !strcmp( psz_cmd, "title_p" ) )
1171     {
1172         if( !strcmp( psz_cmd, "title" ) )
1173         {
1174             if ( *newval.psz_string )
1175             {
1176                 /* Set. */
1177                 val.i_int = atoi( newval.psz_string );
1178                 var_Set( p_input, "title", val );
1179             }
1180             else
1181             {
1182                 vlc_value_t val_list;
1183
1184                 /* Get. */
1185                 var_Get( p_input, "title", &val );
1186                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
1187                             &val_list, NULL );
1188                 msg_rc( "Currently playing title %d/%d.",
1189                         val.i_int, val_list.p_list->i_count );
1190                 var_Change( p_this, "title", VLC_VAR_FREELIST,
1191                             &val_list, NULL );
1192             }
1193         }
1194         else if( !strcmp( psz_cmd, "title_n" ) )
1195         {
1196             val.b_bool = VLC_TRUE;
1197             var_Set( p_input, "next-title", val );
1198         }
1199         else if( !strcmp( psz_cmd, "title_p" ) )
1200         {
1201             val.b_bool = VLC_TRUE;
1202             var_Set( p_input, "prev-title", val );
1203         }
1204
1205         vlc_object_release( p_input );
1206         return VLC_SUCCESS;
1207     }
1208
1209     /* Never reached. */
1210     vlc_object_release( p_input );
1211     return VLC_EGENERIC;
1212 }
1213
1214 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1215                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1216 {
1217     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1218     playlist_t *p_playlist;
1219
1220     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
1221                                            FIND_ANYWHERE );
1222     if( !p_playlist )
1223     {
1224         msg_Err( p_this, "no playlist" );
1225         return VLC_ENOOBJ;
1226     }
1227
1228     if( p_playlist->p_input )
1229     {
1230         vlc_value_t val;
1231         var_Get( p_playlist->p_input, "state", &val );
1232         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1233         {
1234             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1235             vlc_object_release( p_playlist );
1236             return VLC_EGENERIC;
1237         }
1238     }
1239
1240     /* Parse commands that require a playlist */
1241     if( !strcmp( psz_cmd, "prev" ) )
1242     {
1243         playlist_Prev( p_playlist );
1244     }
1245     else if( !strcmp( psz_cmd, "next" ) )
1246     {
1247         playlist_Next( p_playlist );
1248     }
1249     else if( !strcmp( psz_cmd, "play" ) )
1250     {
1251         msg_Warn( p_playlist, "play" );
1252         playlist_Play( p_playlist );
1253     }
1254     else if (!strcmp( psz_cmd, "goto" ) )
1255     {
1256         msg_Err( p_playlist, "goto is deprecated" );
1257     }
1258     else if( !strcmp( psz_cmd, "stop" ) )
1259     {
1260         playlist_Stop( p_playlist );
1261     }
1262     else if( !strcmp( psz_cmd, "clear" ) )
1263     {
1264         playlist_Stop( p_playlist );
1265         vlc_mutex_lock( &p_playlist->object_lock );
1266         playlist_Clear( p_playlist );
1267         vlc_mutex_unlock( &p_playlist->object_lock );
1268     }
1269     else if( !strcmp( psz_cmd, "add" ) &&
1270              newval.psz_string && *newval.psz_string )
1271     {
1272         input_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
1273
1274         if( p_item )
1275         {
1276             msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1277             playlist_PlaylistAddInput( p_playlist, p_item,
1278                               PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
1279         }
1280     }
1281     else if( !strcmp( psz_cmd, "playlist" ) )
1282     {
1283         playlist_Import( p_playlist, "/home/zorglub/vlc-trunk/foo.xspf",
1284                          p_playlist->p_ml_category, VLC_FALSE );
1285         playlist_NodeDump( p_playlist, p_playlist->p_root_category, 0 );
1286         playlist_NodeDump( p_playlist, p_playlist->p_root_onelevel, 0 );
1287     }
1288     else if( !strcmp( psz_cmd, "status" ) )
1289     {
1290         if( p_playlist->p_input )
1291         {
1292             /* Replay the current state of the system. */
1293             msg_rc( STATUS_CHANGE "( new input: %s )", p_playlist->p_input->input.p_item->psz_uri );
1294             msg_rc( STATUS_CHANGE "( audio volume: %d )", config_GetInt( p_intf, "volume" ));
1295
1296             vlc_mutex_lock( &p_playlist->object_lock );
1297             switch( p_playlist->status.i_status )
1298             {
1299                 case PLAYLIST_STOPPED:
1300                     msg_rc( STATUS_CHANGE "( stop state: 0 )" );
1301                     break;
1302                 case PLAYLIST_RUNNING:
1303                     msg_rc( STATUS_CHANGE "( play state: 1 )" );
1304                     break;
1305                 case PLAYLIST_PAUSED:
1306                     msg_rc( STATUS_CHANGE "( pause state: 2 )" );
1307                     break;
1308                 default:
1309                     msg_rc( STATUS_CHANGE "( state unknown )" );
1310                     break;
1311             }
1312             vlc_mutex_unlock( &p_playlist->object_lock );
1313         }
1314     }
1315
1316     /*
1317      * sanity check
1318      */
1319     else
1320     {
1321         msg_rc( "unknown command!" );
1322     }
1323
1324     vlc_object_release( p_playlist );
1325     return VLC_SUCCESS;
1326 }
1327
1328 static int Other( vlc_object_t *p_this, char const *psz_cmd,
1329                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1330 {
1331     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1332     vlc_object_t  *p_playlist;
1333     vlc_value_t    val;
1334     vlc_object_t  *p_input;
1335
1336     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1337     if( !p_playlist )
1338     {
1339         return VLC_ENOOBJ;
1340     }
1341
1342     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1343     if( !p_input )
1344     {
1345         vlc_object_release( p_playlist );
1346         return VLC_ENOOBJ;
1347     }
1348
1349     if( p_input )
1350     {
1351         var_Get( p_input, "state", &val );
1352         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1353         {
1354             msg_rc( _("Type 'pause' to continue.") );
1355             vlc_object_release( p_playlist );
1356             vlc_object_release( p_input );
1357             return VLC_EGENERIC;
1358         }
1359     }
1360
1361     /* Parse miscellaneous commands */
1362     if( !strcmp( psz_cmd, "marq-marquee" ) )
1363     {
1364         if( strlen( newval.psz_string ) > 0 )
1365         {
1366             val.psz_string = newval.psz_string;
1367             var_Set( p_input->p_libvlc, "marq-marquee", val );
1368         }
1369         else
1370         {
1371                 val.psz_string = "";
1372                 var_Set( p_input->p_libvlc, "marq-marquee", val);
1373         }
1374     }
1375     else if( !strcmp( psz_cmd, "marq-x" ) )
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-x", val );
1381         }
1382     }
1383     else if( !strcmp( psz_cmd, "marq-y" ) )
1384     {
1385         if( strlen( newval.psz_string ) > 0)
1386         {
1387             val.i_int = atoi( newval.psz_string );
1388             var_Set( p_input->p_libvlc, "marq-y", val );
1389         }
1390     }
1391     else if( !strcmp( psz_cmd, "marq-position" ) )
1392     {
1393         if( strlen( newval.psz_string ) > 0)
1394         {
1395             val.i_int = atoi( newval.psz_string );
1396             var_Set( p_input->p_libvlc, "marq-position", val );
1397         }
1398     }
1399     else if( !strcmp( psz_cmd, "marq-color" ) )
1400     {
1401         if( strlen( newval.psz_string ) > 0)
1402         {
1403             val.i_int = strtol( newval.psz_string, NULL, 0 );
1404             var_Set( p_input->p_libvlc, "marq-color", val );
1405         }
1406     }
1407     else if( !strcmp( psz_cmd, "marq-opacity" ) )
1408     {
1409         if( strlen( newval.psz_string ) > 0)
1410         {
1411             val.i_int = strtol( newval.psz_string, NULL, 0 );
1412             var_Set( p_input->p_libvlc, "marq-opacity", val );
1413         }
1414     }
1415     else if( !strcmp( psz_cmd, "marq-size" ) )
1416     {
1417         if( strlen( newval.psz_string ) > 0)
1418         {
1419             val.i_int = atoi( newval.psz_string );
1420             var_Set( p_input->p_libvlc, "marq-size", val );
1421         }
1422     }
1423     else if( !strcmp( psz_cmd, "marq-timeout" ) )
1424     {
1425         if( strlen( newval.psz_string ) > 0)
1426         {
1427             val.i_int = atoi( newval.psz_string );
1428             var_Set( p_input, "marq-timeout", val );
1429         }
1430     }
1431     else if( !strcmp( psz_cmd, "mosaic-alpha" ) )
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-alpha", val );
1437         }
1438     }
1439     else if( !strcmp( psz_cmd, "mosaic-height" ) )
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-height", val );
1445         }
1446     }
1447     else if( !strcmp( psz_cmd, "mosaic-width" ) )
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-width", val );
1453         }
1454     }
1455     else if( !strcmp( psz_cmd, "mosaic-xoffset" ) )
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-xoffset", val );
1461         }
1462     }
1463     else if( !strcmp( psz_cmd, "mosaic-yoffset" ) )
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-yoffset", val );
1469         }
1470     }
1471     else if( !strcmp( psz_cmd, "mosaic-align" ) )
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-align", val );
1477         }
1478     }
1479     else if( !strcmp( psz_cmd, "mosaic-vborder" ) )
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-vborder", val );
1485         }
1486     }
1487     else if( !strcmp( psz_cmd, "mosaic-hborder" ) )
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-hborder", val );
1493         }
1494     }
1495     else if( !strcmp( psz_cmd, "mosaic-position" ) )
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-position", val );
1501         }
1502     }
1503     else if( !strcmp( psz_cmd, "mosaic-rows" ) )
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-rows", val );
1509         }
1510     }
1511     else if( !strcmp( psz_cmd, "mosaic-cols" ) )
1512     {
1513         if( strlen( newval.psz_string ) > 0)
1514         {
1515             val.i_int = atoi( newval.psz_string );
1516             var_Set( p_input->p_libvlc, "mosaic-cols", val );
1517         }
1518     }
1519     else if( !strcmp( psz_cmd, "mosaic-order" ) )
1520     {
1521         if( strlen( newval.psz_string ) > 0)
1522         {
1523             val.psz_string = newval.psz_string;
1524             var_Set( p_input->p_libvlc, "mosaic-order", val );
1525         }
1526     }
1527     else if( !strcmp( psz_cmd, "mosaic-keep-aspect-ratio" ) )
1528     {
1529         if( strlen( newval.psz_string ) > 0)
1530         {
1531             val.i_int = atoi( newval.psz_string );
1532             var_Set( p_input->p_libvlc, "mosaic-keep-aspect-ratio", val );
1533         }
1534     }
1535     else if( !strcmp( psz_cmd, "time-format" ) )
1536     {
1537         if( strlen( newval.psz_string ) > 0 )
1538         {
1539             val.psz_string = newval.psz_string;
1540             var_Set( p_input->p_libvlc, "time-format", val );
1541         }
1542         else
1543         {
1544             val.psz_string = "";
1545             var_Set( p_input->p_libvlc, "time-format", val);
1546         }
1547     }
1548     else if( !strcmp( psz_cmd, "time-x" ) )
1549     {
1550         if( strlen( newval.psz_string ) > 0)
1551         {
1552             val.i_int = atoi( newval.psz_string );
1553             var_Set( p_input->p_libvlc, "time-x", val );
1554         }
1555     }
1556     else if( !strcmp( psz_cmd, "time-y" ) )
1557     {
1558         if( strlen( newval.psz_string ) > 0)
1559         {
1560             val.i_int = atoi( newval.psz_string );
1561             var_Set( p_input->p_libvlc, "time-y", val );
1562         }
1563     }
1564     else if( !strcmp( psz_cmd, "time-position" ) )
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-position", val );
1570         }
1571     }
1572     else if( !strcmp( psz_cmd, "time-color" ) )
1573     {
1574         if( strlen( newval.psz_string ) > 0)
1575         {
1576             val.i_int = strtol( newval.psz_string, NULL, 0 );
1577             var_Set( p_input->p_libvlc, "time-color", val );
1578         }
1579     }
1580     else if( !strcmp( psz_cmd, "time-opacity" ) )
1581     {
1582         if( strlen( newval.psz_string ) > 0)
1583         {
1584             val.i_int = strtol( newval.psz_string, NULL, 0 );
1585             var_Set( p_input->p_libvlc, "time-opacity", val );
1586         }
1587     }
1588     else if( !strcmp( psz_cmd, "time-size" ) )
1589     {
1590         if( strlen( newval.psz_string ) > 0)
1591         {
1592             val.i_int = atoi( newval.psz_string );
1593             var_Set( p_input->p_libvlc, "time-size", val );
1594         }
1595     }
1596     else if( !strcmp( psz_cmd, "logo-file" ) )
1597     {
1598         if( strlen( newval.psz_string ) > 0 )
1599         {
1600             val.psz_string = newval.psz_string;
1601             var_Set( p_input->p_libvlc, "logo-file", val );
1602         }
1603     }
1604     else if( !strcmp( psz_cmd, "logo-x" ) )
1605     {
1606         if( strlen( newval.psz_string ) > 0)
1607         {
1608             val.i_int = atoi( newval.psz_string );
1609             var_Set( p_input->p_libvlc, "logo-x", val );
1610         }
1611     }
1612     else if( !strcmp( psz_cmd, "logo-y" ) )
1613     {
1614         if( strlen( newval.psz_string ) > 0)
1615         {
1616             val.i_int = atoi( newval.psz_string );
1617             var_Set( p_input->p_libvlc, "logo-y", val );
1618         }
1619     }
1620     else if( !strcmp( psz_cmd, "logo-position" ) )
1621     {
1622         if( strlen( newval.psz_string ) > 0)
1623         {
1624             val.i_int = atoi( newval.psz_string );
1625             var_Set( p_input->p_libvlc, "logo-position", val );
1626         }
1627     }
1628     else if( !strcmp( psz_cmd, "logo-transparency" ) )
1629     {
1630         if( strlen( newval.psz_string ) > 0)
1631         {
1632             val.i_int = strtol( newval.psz_string, NULL, 0 );
1633             var_Set( p_input->p_libvlc, "logo-transparency", val );
1634         }
1635     }
1636
1637     /*
1638      * sanity check
1639      */
1640     else
1641     {
1642         msg_rc( "Unknown command!" );
1643     }
1644
1645     vlc_object_release( p_playlist );
1646     vlc_object_release( p_input );
1647     return VLC_SUCCESS;
1648 }
1649
1650 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1651                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1652 {
1653     playlist_t *p_playlist;
1654
1655     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1656     if( p_playlist )
1657     {
1658         playlist_Stop( p_playlist );
1659         vlc_object_release( p_playlist );
1660     }
1661     p_this->p_vlc->b_die = VLC_TRUE;
1662     return VLC_SUCCESS;
1663 }
1664
1665 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1666                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
1667 {
1668     intf_thread_t *p_newintf = NULL;
1669
1670     p_newintf = intf_Create( p_this->p_vlc, newval.psz_string, 0, NULL );
1671     if( p_newintf )
1672     {
1673         p_newintf->b_block = VLC_FALSE;
1674         if( intf_RunThread( p_newintf ) )
1675         {
1676             vlc_object_detach( p_newintf );
1677             intf_Destroy( p_newintf );
1678         }
1679     }
1680
1681     return VLC_SUCCESS;
1682 }
1683
1684 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1685                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
1686 {
1687     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1688     input_thread_t *p_input = NULL;
1689     int i_error = VLC_EGENERIC;
1690
1691     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1692     if( !p_input )
1693         return VLC_ENOOBJ;
1694
1695     if( p_input )
1696     {
1697         vlc_value_t val;
1698
1699         var_Get( p_input, "state", &val );
1700         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1701         {
1702             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1703             vlc_object_release( p_input );
1704             return VLC_EGENERIC;
1705         }
1706         vlc_object_release( p_input );
1707     }
1708
1709     if ( *newval.psz_string )
1710     {
1711         /* Set. */
1712         audio_volume_t i_volume = atoi( newval.psz_string );
1713         if ( (i_volume > (audio_volume_t)AOUT_VOLUME_MAX) )
1714         {
1715             msg_rc( "Volume must be in the range %d-%d.", AOUT_VOLUME_MIN,
1716                     AOUT_VOLUME_MAX );
1717             i_error = VLC_EBADVAR;
1718         }
1719         else
1720         {
1721             if( i_volume == AOUT_VOLUME_MIN )
1722             {
1723                 vlc_value_t keyval;
1724
1725                 keyval.i_int = config_GetInt( p_intf, "key-vol-mute" );
1726                 var_Set( p_intf->p_vlc, "key-pressed", keyval );
1727             }
1728             i_error = aout_VolumeSet( p_this, i_volume );
1729             osd_Volume( p_this );
1730             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1731         }
1732     }
1733     else
1734     {
1735         /* Get. */
1736         audio_volume_t i_volume;
1737         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1738         {
1739             i_error = VLC_EGENERIC;
1740         }
1741         else
1742         {
1743             msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1744             i_error = VLC_SUCCESS;
1745         }
1746     }
1747
1748     return i_error;
1749 }
1750
1751 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1752                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1753 {
1754     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1755     audio_volume_t i_volume;
1756     input_thread_t *p_input = NULL;
1757     int i_nb_steps = atoi(newval.psz_string);
1758     int i_error = VLC_SUCCESS;
1759     int i_volume_step = 0;
1760
1761     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1762     if( !p_input )
1763         return VLC_ENOOBJ;
1764
1765     if( p_input )
1766     {
1767         vlc_value_t val;
1768
1769         var_Get( p_input, "state", &val );
1770         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )
1771         {
1772             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1773             vlc_object_release( p_input );
1774             return VLC_EGENERIC;
1775         }
1776         vlc_object_release( p_input );
1777     }
1778
1779     i_volume_step = config_GetInt( p_intf->p_vlc, "volume-step" );
1780     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/i_volume_step) )
1781     {
1782         i_nb_steps = 1;
1783     }
1784
1785     if ( !strcmp(psz_cmd, "volup") )
1786     {
1787         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1788             i_error = VLC_EGENERIC;
1789     }
1790     else
1791     {
1792         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1793             i_error = VLC_EGENERIC;
1794     }
1795     osd_Volume( p_this );
1796
1797     if ( !i_error ) msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1798     return i_error;
1799 }
1800
1801 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1802                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1803 {
1804     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1805     input_thread_t *p_input = NULL;
1806     aout_instance_t * p_aout;
1807     const char * psz_variable;
1808     vlc_value_t val_name;
1809     int i_error;
1810
1811     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1812     if( !p_input )
1813         return VLC_ENOOBJ;
1814
1815     if( p_input )
1816     {
1817         vlc_value_t val;
1818
1819         var_Get( p_input, "state", &val );
1820         if( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) )        {
1821             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1822             vlc_object_release( p_input );
1823             return VLC_EGENERIC;
1824         }
1825         vlc_object_release( p_input );
1826     }
1827
1828     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1829     if ( p_aout == NULL ) return VLC_ENOOBJ;
1830
1831     if ( !strcmp( psz_cmd, "adev" ) )
1832     {
1833         psz_variable = "audio-device";
1834     }
1835     else
1836     {
1837         psz_variable = "audio-channels";
1838     }
1839
1840     /* Get the descriptive name of the variable */
1841     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1842                  &val_name, NULL );
1843     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1844
1845     if ( !*newval.psz_string )
1846     {
1847         /* Retrieve all registered ***. */
1848         vlc_value_t val, text;
1849         int i, i_value;
1850
1851         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1852         {
1853             vlc_object_release( (vlc_object_t *)p_aout );
1854             return VLC_EGENERIC;
1855         }
1856         i_value = val.i_int;
1857
1858         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1859                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1860         {
1861             vlc_object_release( (vlc_object_t *)p_aout );
1862             return VLC_EGENERIC;
1863         }
1864
1865         msg_rc( "+----[ %s ]", val_name.psz_string );
1866         for ( i = 0; i < val.p_list->i_count; i++ )
1867         {
1868             if ( i_value == val.p_list->p_values[i].i_int )
1869                 msg_rc( "| %i - %s *", val.p_list->p_values[i].i_int,
1870                         text.p_list->p_values[i].psz_string );
1871             else
1872                 msg_rc( "| %i - %s", val.p_list->p_values[i].i_int,
1873                         text.p_list->p_values[i].psz_string );
1874         }
1875         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1876                     &val, &text );
1877         msg_rc( "+----[ end of %s ]", val_name.psz_string );
1878
1879         if( val_name.psz_string ) free( val_name.psz_string );
1880         i_error = VLC_SUCCESS;
1881     }
1882     else
1883     {
1884         vlc_value_t val;
1885         val.i_int = atoi( newval.psz_string );
1886
1887         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1888     }
1889     vlc_object_release( (vlc_object_t *)p_aout );
1890
1891     return i_error;
1892 }
1893
1894 /* OSD menu commands */
1895 static int Menu( vlc_object_t *p_this, char const *psz_cmd,
1896     vlc_value_t oldval, vlc_value_t newval, void *p_data )
1897 {
1898     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1899     playlist_t    *p_playlist = NULL;
1900     vlc_value_t val;
1901     int i_error = VLC_EGENERIC;
1902
1903     if ( !*newval.psz_string )
1904     {
1905         msg_rc( _("Please provide one of the following parameters:") );
1906         msg_rc( "[on|off|up|down|left|right|select]" );
1907         return i_error;
1908     }
1909
1910     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1911     if( !p_playlist )
1912         return VLC_ENOOBJ;
1913
1914     if( p_playlist->p_input )
1915     {
1916         var_Get( p_playlist->p_input, "state", &val );
1917         if( ( ( val.i_int == PAUSE_S ) || ( val.i_int == PLAYLIST_PAUSED ) ) &&
1918             ( strcmp( newval.psz_string, "select" ) != 0 ) )
1919         {
1920             msg_rc( _("Type 'menu select' or 'pause' to continue.") );
1921             vlc_object_release( p_playlist );
1922             return VLC_EGENERIC;
1923         }
1924     }
1925     vlc_object_release( p_playlist );
1926
1927     val.psz_string = strdup( newval.psz_string );
1928     if( !strcmp( val.psz_string, "on" ) || !strcmp( val.psz_string, "show" ))
1929         osd_MenuShow( p_this );
1930     else if( !strcmp( val.psz_string, "off" ) || !strcmp( val.psz_string, "hide" ) )
1931         osd_MenuHide( p_this );
1932     else if( !strcmp( val.psz_string, "up" ) )
1933         osd_MenuUp( p_this );
1934     else if( !strcmp( val.psz_string, "down" ) )
1935         osd_MenuDown( p_this );
1936     else if( !strcmp( val.psz_string, "left" ) )
1937         osd_MenuPrev( p_this );
1938     else if( !strcmp( val.psz_string, "right" ) )
1939         osd_MenuNext( p_this );
1940     else if( !strcmp( val.psz_string, "select" ) )
1941         osd_MenuActivate( p_this );
1942     else
1943     {
1944         msg_rc( _("Please provide one of the following parameters:") );
1945         msg_rc( "[on|off|up|down|left|right|select]" );
1946         if( val.psz_string ) free( val.psz_string );
1947             return i_error;
1948     }
1949
1950     i_error = VLC_SUCCESS;
1951     if( val.psz_string ) free( val.psz_string );
1952     return i_error;
1953 }
1954
1955 #ifdef WIN32
1956 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1957 {
1958     INPUT_RECORD input_record;
1959     DWORD i_dw;
1960
1961     /* On Win32, select() only works on socket descriptors */
1962     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1963                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1964     {
1965         while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1966                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1967                                  1, &i_dw ) )
1968         {
1969             if( input_record.EventType != KEY_EVENT ||
1970                 !input_record.Event.KeyEvent.bKeyDown ||
1971                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1972                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1973                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1974                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1975             {
1976                 /* nothing interesting */
1977                 continue;
1978             }
1979
1980             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1981
1982             /* Echo out the command */
1983             putc( p_buffer[ *pi_size ], stdout );
1984
1985             /* Handle special keys */
1986             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1987             {
1988                 putc( '\n', stdout );
1989                 break;
1990             }
1991             switch( p_buffer[ *pi_size ] )
1992             {
1993             case '\b':
1994                 if( *pi_size )
1995                 {
1996                     *pi_size -= 2;
1997                     putc( ' ', stdout );
1998                     putc( '\b', stdout );
1999                 }
2000                 break;
2001             case '\r':
2002                 (*pi_size) --;
2003                 break;
2004             }
2005
2006             (*pi_size)++;
2007         }
2008
2009         if( *pi_size == MAX_LINE_LENGTH ||
2010             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2011         {
2012             p_buffer[ *pi_size ] = 0;
2013             return VLC_TRUE;
2014         }
2015     }
2016
2017     return VLC_FALSE;
2018 }
2019 #endif
2020
2021 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
2022 {
2023     int i_read = 0;
2024
2025 #ifdef WIN32
2026     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
2027         return ReadWin32( p_intf, p_buffer, pi_size );
2028     else if( p_intf->p_sys->i_socket == -1 )
2029     {
2030         msleep( INTF_IDLE_SLEEP );
2031         return VLC_FALSE;
2032     }
2033 #endif
2034
2035     while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
2036            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
2037                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
2038                   (uint8_t *)p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
2039     {
2040         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2041             break;
2042
2043         (*pi_size)++;
2044     }
2045
2046     /* Connection closed */
2047     if( i_read == -1 )
2048     {
2049         p_intf->p_sys->i_socket = -1;
2050         p_buffer[ *pi_size ] = 0;
2051         return VLC_TRUE;
2052     }
2053
2054     if( *pi_size == MAX_LINE_LENGTH ||
2055         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
2056     {
2057         p_buffer[ *pi_size ] = 0;
2058         return VLC_TRUE;
2059     }
2060
2061     return VLC_FALSE;
2062 }
2063
2064 /*****************************************************************************
2065  * parse_MRL: build a input item from a full mrl
2066  *****************************************************************************
2067  * MRL format: "simplified-mrl [:option-name[=option-value]]"
2068  * We don't check for '"' or '\'', we just assume that a ':' that follows a
2069  * space is a new option. Should be good enough for our purpose.
2070  *****************************************************************************/
2071 static input_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
2072 {
2073 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
2074 #define SKIPTRAILINGSPACE( p, d ) \
2075     { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
2076
2077     input_item_t *p_item = NULL;
2078     char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
2079     char **ppsz_options = NULL;
2080     int i, i_options = 0;
2081
2082     if( !psz_mrl ) return 0;
2083
2084     psz_mrl = psz_orig = strdup( psz_mrl );
2085     while( *psz_mrl )
2086     {
2087         SKIPSPACE( psz_mrl );
2088         psz_item = psz_mrl;
2089
2090         for( ; *psz_mrl; psz_mrl++ )
2091         {
2092             if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
2093             {
2094                 /* We have a complete item */
2095                 break;
2096             }
2097             if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
2098                 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
2099             {
2100                 /* We have a complete item */
2101                 break;
2102             }
2103         }
2104
2105         if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
2106         SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
2107
2108         /* Remove '"' and '\'' if necessary */
2109         if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
2110         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2111         if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
2112         { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
2113
2114         if( !psz_item_mrl ) psz_item_mrl = psz_item;
2115         else if( *psz_item )
2116         {
2117             i_options++;
2118             ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
2119             ppsz_options[i_options - 1] = &psz_item[1];
2120         }
2121
2122         if( *psz_mrl ) SKIPSPACE( psz_mrl );
2123     }
2124
2125     /* Now create a playlist item */
2126     if( psz_item_mrl )
2127     {
2128         p_item = input_ItemNew( p_intf, psz_item_mrl, psz_item_mrl );
2129         for( i = 0; i < i_options; i++ )
2130         {
2131             vlc_input_item_AddOption( p_item, ppsz_options[i] );
2132         }
2133     }
2134
2135     if( i_options ) free( ppsz_options );
2136     free( psz_orig );
2137
2138     return p_item;
2139 }
2140
2141 /*****************************************************************************
2142  * checkUpdates : check for updates
2143  ****************************************************************************/
2144 static void checkUpdates( intf_thread_t *p_intf, char *psz_arg )
2145 {
2146     update_iterator_t *p_uit;
2147     update_t *p_u = update_New( p_intf );
2148     if( p_u == NULL ) return;
2149     p_uit = update_iterator_New( p_u );
2150     if( p_uit )
2151     {
2152         int s = 0, t = 0;
2153
2154         if( strstr( psz_arg, "newer" ) )
2155             s |= UPDATE_RELEASE_STATUS_NEWER;
2156         if( strstr( psz_arg, "equal" ) )
2157             s |= UPDATE_RELEASE_STATUS_EQUAL;
2158         if( strstr( psz_arg, "older" ) )
2159             s |= UPDATE_RELEASE_STATUS_OLDER;
2160         if( s ) p_uit->i_rs = s;
2161         else p_uit->i_rs = UPDATE_RELEASE_STATUS_NEWER;
2162
2163         if( strstr( psz_arg, "undef" ) )
2164             t |= UPDATE_FILE_TYPE_UNDEF;
2165         if( strstr( psz_arg, "info" ) )
2166             t |= UPDATE_FILE_TYPE_INFO;
2167         if( strstr( psz_arg, "source" ) )
2168             t |= UPDATE_FILE_TYPE_SOURCE;
2169         if( strstr( psz_arg, "binary" ) )
2170             t |= UPDATE_FILE_TYPE_BINARY;
2171         if( strstr( psz_arg, "plugin" ) )
2172             t |= UPDATE_FILE_TYPE_PLUGIN;
2173         if( t ) p_uit->i_t = t;
2174
2175         update_Check( p_u, VLC_FALSE );
2176         update_iterator_Action( p_uit, UPDATE_MIRROR );
2177         msg_rc( "\nUsing mirror: %s (%s) [%s]",
2178                 p_uit->mirror.psz_name,
2179                 p_uit->mirror.psz_location,
2180                 p_uit->mirror.psz_type );
2181         while( (s = update_iterator_Action( p_uit, UPDATE_FILE )) != UPDATE_FAIL )
2182         {
2183             char *psz_tmp;
2184             if( s & UPDATE_RELEASE )
2185             {
2186                 switch( p_uit->release.i_status )
2187                 {
2188                     case UPDATE_RELEASE_STATUS_OLDER:
2189                         psz_tmp = strdup( "older" );
2190                         break;
2191                     case UPDATE_RELEASE_STATUS_EQUAL:
2192                         psz_tmp = strdup( "equal" );
2193                         break;
2194                     case UPDATE_RELEASE_STATUS_NEWER:
2195                         psz_tmp = strdup( "newer" );
2196                         break;
2197                     default:
2198                         psz_tmp = strdup( "?!?" );
2199                         break;
2200                 }
2201                 msg_rc( "\n+----[ VLC %s %s (%s) ] ",
2202                         p_uit->release.psz_version,
2203                         p_uit->release.psz_svn_revision,
2204                         psz_tmp );
2205                 free( psz_tmp );
2206             }
2207             switch( p_uit->file.i_type )
2208             {
2209                 case UPDATE_FILE_TYPE_UNDEF:
2210                     psz_tmp = strdup( "undef" );
2211                     break;
2212                 case UPDATE_FILE_TYPE_INFO:
2213                     psz_tmp = strdup( "info" );
2214                     break;
2215                 case UPDATE_FILE_TYPE_SOURCE:
2216                     psz_tmp = strdup( "source" );
2217                     break;
2218                 case UPDATE_FILE_TYPE_BINARY:
2219                     psz_tmp = strdup( "binary" );
2220                     break;
2221                 case UPDATE_FILE_TYPE_PLUGIN:
2222                     psz_tmp = strdup( "plugin" );
2223                     break;
2224                 default:
2225                     psz_tmp = strdup( "?!?" );
2226                     break;
2227             }
2228             msg_rc( "| %s (%s)", p_uit->file.psz_description, psz_tmp );
2229             free( psz_tmp );
2230             if( p_uit->file.l_size )
2231             {
2232                 if( p_uit->file.l_size > 1024 * 1024 * 1024 )
2233                     asprintf( &psz_tmp, "(%ld GB)",
2234                               p_uit->file.l_size / (1024*1024*1024) );
2235                 if( p_uit->file.l_size > 1024 * 1024 )
2236                     asprintf( &psz_tmp, "(%ld MB)",
2237                               p_uit->file.l_size / (1024*1024) );
2238                 else if( p_uit->file.l_size > 1024 )
2239                     asprintf( &psz_tmp, "(%ld kB)",
2240                               p_uit->file.l_size / 1024 );
2241                 else
2242                     asprintf( &psz_tmp, "(%ld B)", p_uit->file.l_size );
2243             }
2244             else
2245             {
2246                 psz_tmp = strdup( "" );
2247             }
2248             msg_rc( "| %s %s", p_uit->file.psz_url, psz_tmp );
2249             msg_rc( "+----" );
2250             free( psz_tmp );
2251         }
2252         msg_rc( "" );
2253         update_iterator_Delete( p_uit );
2254     }
2255     update_Delete( p_u );
2256 }