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