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