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