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