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