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