]> git.sesse.net Git - vlc/blob - modules/control/rc.c
* modules/control/rc.c: reverted last commit and only print an additional '\r' in...
[vlc] / modules / control / rc.c
1 /*****************************************************************************
2  * rc.c : remote control stdin/stdout module for vlc
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37 #include <vlc/aout.h>
38 #include <vlc/vout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43
44 #ifdef HAVE_SYS_TIME_H
45 #    include <sys/time.h>
46 #endif
47 #include <sys/types.h>
48
49 #include "vlc_error.h"
50 #include "network.h"
51
52 #if defined(PF_UNIX) && !defined(PF_LOCAL)
53 #    define PF_LOCAL PF_UNIX
54 #endif
55 #if defined(AF_UNIX) && !defined(AF_LOCAL)
56 #    define AF_LOCAL AF_UNIX
57 #endif
58
59 #ifdef PF_LOCAL
60 #    include <sys/un.h>
61 #endif
62
63 #define MAX_LINE_LENGTH 256
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int  Activate     ( vlc_object_t * );
69 static void Deactivate   ( vlc_object_t * );
70 static void Run          ( intf_thread_t * );
71
72 static vlc_bool_t ReadCommand( intf_thread_t *, char *, int * );
73
74 static int  Input        ( vlc_object_t *, char const *,
75                            vlc_value_t, vlc_value_t, void * );
76 static int  Playlist     ( vlc_object_t *, char const *,
77                            vlc_value_t, vlc_value_t, void * );
78 static int  Other        ( vlc_object_t *, char const *,
79                            vlc_value_t, vlc_value_t, void * );
80 static int  Quit         ( vlc_object_t *, char const *,
81                            vlc_value_t, vlc_value_t, void * );
82 static int  Intf         ( vlc_object_t *, char const *,
83                            vlc_value_t, vlc_value_t, void * );
84 static int  Volume       ( vlc_object_t *, char const *,
85                            vlc_value_t, vlc_value_t, void * );
86 static int  VolumeMove   ( vlc_object_t *, char const *,
87                            vlc_value_t, vlc_value_t, void * );
88 static int  AudioConfig  ( vlc_object_t *, char const *,
89                            vlc_value_t, vlc_value_t, void * );
90
91 struct intf_sys_t
92 {
93     int i_socket_listen;
94     int i_socket;
95     char *psz_unix_path;
96     vlc_bool_t b_extend;
97     
98 #ifdef WIN32
99     HANDLE hConsoleIn;
100     vlc_bool_t b_quiet;
101 #endif
102 };
103
104 #ifdef HAVE_VARIADIC_MACROS
105 #   define printf( psz_format, args... ) \
106       Printf( p_intf, psz_format, ## args )
107 #endif
108
109 void Printf( intf_thread_t *p_intf, const char *psz_fmt, ... )
110 {
111     va_list args;
112     va_start( args, psz_fmt );
113     if( p_intf->p_sys->i_socket == -1 ) vprintf( psz_fmt, args );
114     else
115     { net_vaPrintf( p_intf, p_intf->p_sys->i_socket, psz_fmt, args );
116       net_Printf( VLC_OBJECT(p_intf), p_intf->p_sys->i_socket, "\r" ); }
117     va_end( args );
118 }
119
120 /*****************************************************************************
121  * Module descriptor
122  *****************************************************************************/
123 #define POS_TEXT N_("Show stream position")
124 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
125                         "stream from time to time." )
126
127 #define TTY_TEXT N_("Fake TTY")
128 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
129
130 #define UNIX_TEXT N_("UNIX socket command input")
131 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
132                          "stdin." )
133
134 #define HOST_TEXT N_("TCP command input")
135 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
136             "You can set the address and port the interface will bind to." )
137 #define EXTEND_TEXT N_("Extended help")
138 #define EXTEND_LONGTEXT N_("List additional commands.")
139             
140
141 #ifdef WIN32
142 #define QUIET_TEXT N_("Do not open a DOS command box interface")
143 #define QUIET_LONGTEXT N_( \
144     "By default the rc interface plugin will start a DOS command box. " \
145     "Enabling the quiet mode will not bring this command box but can also " \
146     "be pretty annoying when you want to stop VLC and no video window is " \
147     "open." )
148 #endif
149
150 vlc_module_begin();
151     set_description( _("Remote control interface") );
152     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
153 #ifdef HAVE_ISATTY
154     add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
155 #endif
156     add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, VLC_TRUE );
157     add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
158
159 #ifdef WIN32
160     add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, VLC_FALSE );
161 #endif
162     add_bool( "rc-extend", 0, NULL, EXTEND_TEXT, EXTEND_LONGTEXT, VLC_FALSE );
163
164     set_capability( "interface", 20 );
165     set_callbacks( Activate, Deactivate );
166 vlc_module_end();
167
168 /*****************************************************************************
169  * Activate: initialize and create stuff
170  *****************************************************************************/
171 static int Activate( vlc_object_t *p_this )
172 {
173     intf_thread_t *p_intf = (intf_thread_t*)p_this;
174     char *psz_host, *psz_unix_path;
175     int i_socket = -1;
176
177 #if defined(HAVE_ISATTY) && !defined(WIN32)
178     /* Check that stdin is a TTY */
179     if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
180     {
181         msg_Warn( p_intf, "fd 0 is not a TTY" );
182         return VLC_EGENERIC;
183     }
184 #endif
185
186     psz_unix_path = config_GetPsz( p_intf, "rc-unix" );
187     if( psz_unix_path )
188     {
189 #ifndef PF_LOCAL
190         msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
191         free( psz_unix_path );
192         return VLC_EGENERIC;
193 #else
194         struct sockaddr_un addr;
195         int i_ret;
196
197         memset( &addr, 0, sizeof(struct sockaddr_un) );
198
199         msg_Dbg( p_intf, "trying UNIX socket" );
200
201         if( (i_socket = socket( PF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
202         {
203             msg_Warn( p_intf, "can't open socket: %s", strerror(errno) );
204             free( psz_unix_path );
205             return VLC_EGENERIC;
206         }
207
208         addr.sun_family = AF_LOCAL;
209         strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
210         addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
211
212         if( (i_ret = bind( i_socket, (struct sockaddr*)&addr,
213                            sizeof(struct sockaddr_un) ) ) < 0 )
214         {
215             msg_Warn( p_intf, "couldn't bind socket to address: %s",
216                       strerror(errno) );
217             free( psz_unix_path );
218             net_Close( i_socket );
219             return VLC_EGENERIC;
220         }
221
222         if( ( i_ret = listen( i_socket, 1 ) ) < 0 )
223         {
224             msg_Warn( p_intf, "can't listen on socket: %s", strerror(errno));
225             free( psz_unix_path );
226             net_Close( i_socket );
227             return VLC_EGENERIC;
228         }
229 #endif
230     }
231
232     if( ( i_socket == -1) &&
233         ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
234     {
235         vlc_url_t url;
236
237         vlc_UrlParse( &url, psz_host, 0 );
238
239         msg_Dbg( p_intf, "base %s port %d", url.psz_host, url.i_port );
240
241         if( (i_socket = net_ListenTCP(p_this, url.psz_host, url.i_port)) == -1)
242         {
243             msg_Warn( p_intf, "can't listen to %s port %i",
244                       url.psz_host, url.i_port );
245             vlc_UrlClean( &url );
246             free( psz_host );
247             return VLC_EGENERIC;
248         }
249
250         vlc_UrlClean( &url );
251         free( psz_host );
252     }
253
254     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
255     if( !p_intf->p_sys )
256     {
257         msg_Err( p_intf, "no memory" );
258         return VLC_ENOMEM;
259     }
260
261     p_intf->p_sys->i_socket_listen = i_socket;
262     p_intf->p_sys->i_socket = -1;
263     p_intf->p_sys->psz_unix_path = psz_unix_path;
264
265     /* Non-buffered stdout */
266     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
267
268     p_intf->pf_run = Run;
269
270 #ifdef WIN32
271     p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
272     if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
273 #else
274     CONSOLE_INTRO_MSG;
275 #endif
276
277     printf( _("Remote control interface initialized, `h' for help\n") );
278     return VLC_SUCCESS;
279 }
280
281 /*****************************************************************************
282  * Deactivate: uninitialize and cleanup
283  *****************************************************************************/
284 static void Deactivate( vlc_object_t *p_this )
285 {
286     intf_thread_t *p_intf = (intf_thread_t*)p_this;
287
288     if( p_intf->p_sys->i_socket_listen != -1 )
289         net_Close( p_intf->p_sys->i_socket_listen );
290     if( p_intf->p_sys->i_socket != -1 )
291         net_Close( p_intf->p_sys->i_socket );
292     if( p_intf->p_sys->psz_unix_path != NULL )
293     {
294 #ifdef PF_LOCAL
295         unlink( p_intf->p_sys->psz_unix_path );
296 #endif
297         free( p_intf->p_sys->psz_unix_path );
298     }
299     free( p_intf->p_sys );
300 }
301
302 /*****************************************************************************
303  * Run: rc thread
304  *****************************************************************************
305  * This part of the interface is in a separate thread so that we can call
306  * exec() from within it without annoying the rest of the program.
307  *****************************************************************************/
308 static void Run( intf_thread_t *p_intf )
309 {
310     input_thread_t * p_input;
311     playlist_t *     p_playlist;
312
313     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
314     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
315
316     int        i_size = 0;
317     int        i_oldpos = 0;
318     int        i_newpos;
319
320     p_buffer[0] = 0;
321     p_input = NULL;
322     p_playlist = NULL;
323  
324     p_intf->p_sys->b_extend = config_GetInt( p_intf, "rc-extend" );
325     /* Register commands that will be cleaned up upon object destruction */
326     var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
327     var_AddCallback( p_intf, "quit", Quit, NULL );
328     var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
329     var_AddCallback( p_intf, "intf", Intf, NULL );
330
331     var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
332     var_AddCallback( p_intf, "add", Playlist, NULL );
333     var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
334     var_AddCallback( p_intf, "playlist", Playlist, NULL );
335     var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
336     var_AddCallback( p_intf, "play", Playlist, NULL );
337     var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
338     var_AddCallback( p_intf, "stop", Playlist, NULL );
339     var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
340     var_AddCallback( p_intf, "prev", Playlist, NULL );
341     var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
342     var_AddCallback( p_intf, "next", Playlist, NULL );
343   
344     var_Create( p_intf, "marq-marquee", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
345     var_AddCallback( p_intf, "marq-marquee", Other, NULL );
346     var_Create( p_intf, "marq-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
347     var_AddCallback( p_intf, "marq-x", Other, NULL );
348     var_Create( p_intf, "marq-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
349     var_AddCallback( p_intf, "marq-y", Other, NULL );
350     var_Create( p_intf, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
351     var_AddCallback( p_intf, "marq-timeout", Other, NULL );
352
353     var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
354     var_AddCallback( p_intf, "pause", Input, NULL );
355     var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
356     var_AddCallback( p_intf, "seek", Input, NULL );
357     var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
358     var_AddCallback( p_intf, "title", Input, NULL );
359     var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
360     var_AddCallback( p_intf, "title_n", Input, NULL );
361     var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
362     var_AddCallback( p_intf, "title_p", Input, NULL );
363     var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
364     var_AddCallback( p_intf, "chapter", Input, NULL );
365     var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
366     var_AddCallback( p_intf, "chapter_n", Input, NULL );
367     var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
368     var_AddCallback( p_intf, "chapter_p", Input, NULL );
369
370     var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
371     var_AddCallback( p_intf, "volume", Volume, NULL );
372     var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
373     var_AddCallback( p_intf, "volup", VolumeMove, NULL );
374     var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
375     var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
376     var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
377     var_AddCallback( p_intf, "adev", AudioConfig, NULL );
378     var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
379     var_AddCallback( p_intf, "achan", AudioConfig, NULL );
380
381 #ifdef WIN32
382     /* Get the file descriptor of the console input */
383     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
384     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
385     {
386         msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
387         p_intf->b_die = VLC_TRUE;
388     }
389 #endif
390
391     while( !p_intf->b_die )
392     {
393         char *psz_cmd, *psz_arg;
394         vlc_bool_t b_complete;
395
396         if( p_intf->p_sys->i_socket_listen != - 1 &&
397             p_intf->p_sys->i_socket == -1 )
398         {
399             p_intf->p_sys->i_socket =
400                 net_Accept( p_intf, p_intf->p_sys->i_socket_listen, 0 );
401         }
402
403         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
404
405         /* Manage the input part */
406         if( p_input == NULL )
407         {
408             if( p_playlist )
409             {
410                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
411                                                        FIND_CHILD );
412             }
413             else
414             {
415                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
416                                                    FIND_ANYWHERE );
417                 if( p_input )
418                 {
419                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
420                                                            FIND_PARENT );
421                 }
422             }
423         }
424         else if( p_input->b_dead )
425         {
426             vlc_object_release( p_input );
427             p_input = NULL;
428         }
429
430         if( p_input && b_showpos )
431         {
432             i_newpos = 100 * var_GetFloat( p_input, "position" );
433             if( i_oldpos != i_newpos )
434             {
435                 i_oldpos = i_newpos;
436                 printf( "pos: %d%%\n", i_newpos );
437             }
438         }
439
440         /* Is there something to do? */
441         if( !b_complete ) continue;
442
443
444         /* Skip heading spaces */
445         psz_cmd = p_buffer;
446         while( *psz_cmd == ' ' )
447         {
448             psz_cmd++;
449         }
450
451         /* Split psz_cmd at the first space and make sure that
452          * psz_arg is valid */
453         psz_arg = strchr( psz_cmd, ' ' );
454         if( psz_arg )
455         {
456             *psz_arg++ = 0;
457             while( *psz_arg == ' ' )
458             {
459                 psz_arg++;
460             }
461         }
462         else
463         {
464             psz_arg = "";
465         }
466
467         /* If the user typed a registered local command, try it */
468         if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
469         {
470             vlc_value_t val;
471             int i_ret;
472
473             val.psz_string = psz_arg;
474             i_ret = var_Set( p_intf, psz_cmd, val );
475             printf( _("%s: returned %i (%s)\n"),
476                     psz_cmd, i_ret, vlc_error( i_ret ) );
477         }
478         /* Or maybe it's a global command */
479         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
480         {
481             vlc_value_t val;
482             int i_ret;
483
484             val.psz_string = psz_arg;
485             /* FIXME: it's a global command, but we should pass the
486              * local object as an argument, not p_intf->p_libvlc. */
487             i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
488             if( i_ret != 0 )
489             {
490                 printf( _("%s: returned %i (%s)\n"),
491                          psz_cmd, i_ret, vlc_error( i_ret ) );
492             }
493         }
494         else if( !strcmp( psz_cmd, "logout" ) )
495         {
496             /* Close connection */
497             if( p_intf->p_sys->i_socket != -1 )
498             {
499                 net_Close( p_intf->p_sys->i_socket );
500             }
501             p_intf->p_sys->i_socket = -1;
502         }
503         else if( !strcmp( psz_cmd, "info" ) )
504         {
505             if( p_input )
506             {
507                 int i, j;
508                 vlc_mutex_lock( &p_input->input.p_item->lock );
509                 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
510                 {
511                     info_category_t *p_category =
512                         p_input->input.p_item->pp_categories[i];
513
514                     printf( "+----[ %s ]\n", p_category->psz_name );
515                     printf( "| \n" );
516                     for ( j = 0; j < p_category->i_infos; j++ )
517                     {
518                         info_t *p_info = p_category->pp_infos[j];
519                         printf( "| %s: %s\n", p_info->psz_name,
520                                 p_info->psz_value );
521                     }
522                     printf( "| \n" );
523                 }
524                 printf( _("+----[ end of stream info ]\n") );
525                 vlc_mutex_unlock( &p_input->input.p_item->lock );
526             }
527             else
528             {
529                 printf( _("no input\n") );
530             }
531         }
532         else if( !strcmp( psz_cmd, "is_playing" ) )
533         {
534             if( ! p_input )
535             {
536                 printf( "0\n" );
537             }
538             else
539             {
540                 printf( "1\n" );
541             }
542         }
543         else if( !strcmp( psz_cmd, "get_time" ) )
544         {
545             if( ! p_input )
546             {
547                 printf("0\n");
548             }
549             else
550             {
551                 vlc_value_t time;
552                 var_Get( p_input, "time", &time );
553                 printf( "%i\n", time.i_time / 1000000);
554             }
555         }
556         else if( !strcmp( psz_cmd, "get_length" ) )
557         {
558             if( ! p_input )
559             {
560                 printf("0\n");
561             }
562             else
563             {
564                 vlc_value_t time;
565                 var_Get( p_input, "length", &time );
566                 printf( "%i\n", time.i_time / 1000000);
567             }
568         }
569         else if( !strcmp( psz_cmd, "get_title" ) )
570         {
571             if( ! p_input )
572             {
573                 printf("\n");
574             }
575             else
576             {
577                 printf( "%s\n", p_input->input.p_item->psz_name );
578             }
579         }
580         else switch( psz_cmd[0] )
581         {
582         case 'f':
583         case 'F':
584             if( p_input )
585             {
586                 vout_thread_t *p_vout;
587                 p_vout = vlc_object_find( p_input,
588                                           VLC_OBJECT_VOUT, FIND_CHILD );
589
590                 if( p_vout )
591                 {
592                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
593                     vlc_object_release( p_vout );
594                 }
595             }
596             break;
597
598         case 's':
599         case 'S':
600             ;
601             break;
602
603         case '?':
604         case 'h':
605         case 'H':
606             printf(_("+----[ Remote control commands ]\n"));
607             printf("| \n");
608             printf(_("| add XYZ  . . . . . . . . . . add XYZ to playlist\n"));
609             printf(_("| playlist . . .  show items currently in playlist\n"));
610             printf(_("| play . . . . . . . . . . . . . . . . play stream\n"));
611             printf(_("| stop . . . . . . . . . . . . . . . . stop stream\n"));
612             printf(_("| next . . . . . . . . . . . .  next playlist item\n"));
613             printf(_("| prev . . . . . . . . . .  previous playlist item\n"));
614             printf(_("| title [X]  . . . . set/get title in current item\n"));
615             printf(_("| title_n  . . . . . .  next title in current item\n"));
616             printf(_("| title_p  . . . .  previous title in current item\n"));
617             printf(_("| chapter [X]  . . set/get chapter in current item\n"));
618             printf(_("| chapter_n  . . . .  next chapter in current item\n"));
619             printf(_("| chapter_p  . .  previous chapter in current item\n"));
620             printf("| \n");
621             printf(_("| seek X . seek in seconds, for instance `seek 12'\n"));
622             printf(_("| pause  . . . . . . . . . . . . . .  toggle pause\n"));
623             printf(_("| f  . . . . . . . . . . . . . . toggle fullscreen\n"));
624             printf(_("| info . . .  information about the current stream\n"));
625             printf("| \n");
626             printf(_("| volume [X] . . . . . . . .  set/get audio volume\n"));
627             printf(_("| volup [X]  . . . . .  raise audio volume X steps\n"));
628             printf(_("| voldown [X]  . . . .  lower audio volume X steps\n"));
629             printf(_("| adev [X] . . . . . . . . .  set/get audio device\n"));
630             printf(_("| achan [X]. . . . . . . .  set/get audio channels\n"));
631             printf("| \n");
632             if (p_intf->p_sys->b_extend)
633             {
634                    printf(_("| marq-marquee STRING  . . overlay STRING in video\n"));
635                printf(_("| marq-x X . . . . . .offset of marquee, from left\n"));
636                printf(_("| marq-y Y . . . . . . offset of marquee, from top\n"));
637                printf(_("| marq-timeout T. . . . .timeout of marquee, in ms\n"));
638                printf("| \n");
639             }    
640             printf(_("| help . . . . . . . . . . . . . this help message\n"));
641             printf(_("| logout . . . . . .exit (if in socket connection)\n"));
642             printf(_("| quit . . . . . . . . . . . . . . . . .  quit vlc\n"));
643             printf("| \n");
644             printf(_("+----[ end of help ]\n"));
645             break;
646
647         case '\0':
648             /* Ignore empty lines */
649             break;
650
651         default:
652             printf(_("unknown command `%s', type `help' for help\n"), psz_cmd);
653             break;
654         }
655
656         /* Command processed */
657         i_size = 0; p_buffer[0] = 0;
658     }
659
660     if( p_input )
661     {
662         vlc_object_release( p_input );
663         p_input = NULL;
664     }
665
666     if( p_playlist )
667     {
668         vlc_object_release( p_playlist );
669         p_playlist = NULL;
670     }
671 }
672
673 static int Input( vlc_object_t *p_this, char const *psz_cmd,
674                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
675 {
676     intf_thread_t *p_intf = (intf_thread_t*)p_this;
677     input_thread_t *p_input;
678     vlc_value_t     val;
679
680     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
681     if( !p_input ) return VLC_ENOOBJ;
682
683     /* Parse commands that only require an input */
684     if( !strcmp( psz_cmd, "pause" ) )
685     {
686         val.i_int = PAUSE_S;
687
688         var_Set( p_input, "state", val );
689         vlc_object_release( p_input );
690         return VLC_SUCCESS;
691     }
692     else if( !strcmp( psz_cmd, "seek" ) )
693     {
694         if( strlen( newval.psz_string ) > 0 &&
695             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
696         {
697             val.f_float = (float)atoi( newval.psz_string ) / 100.0;
698             var_Set( p_input, "position", val );
699         }
700         else
701         {
702             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
703             var_Set( p_input, "time", val );
704         }
705         vlc_object_release( p_input );
706         return VLC_SUCCESS;
707     }
708    
709     else if( !strcmp( psz_cmd, "chapter" ) ||
710              !strcmp( psz_cmd, "chapter_n" ) ||
711              !strcmp( psz_cmd, "chapter_p" ) )
712     {
713         if( !strcmp( psz_cmd, "chapter" ) )
714         {
715             if ( *newval.psz_string )
716             {
717                 /* Set. */
718                 val.i_int = atoi( newval.psz_string );
719                 var_Set( p_input, "chapter", val );
720             }
721             else
722             {
723                 vlc_value_t val_list;
724
725                 /* Get. */
726                 var_Get( p_input, "chapter", &val );
727                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
728                             &val_list, NULL );
729                 printf( _("Currently playing chapter %d/%d\n"),
730                         val.i_int, val_list.p_list->i_count );
731                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
732                             &val_list, NULL );
733             }
734         }
735         else if( !strcmp( psz_cmd, "chapter_n" ) )
736         {
737             val.b_bool = VLC_TRUE;
738             var_Set( p_input, "next-chapter", val );
739         }
740         else if( !strcmp( psz_cmd, "chapter_p" ) )
741         {
742             val.b_bool = VLC_TRUE;
743             var_Set( p_input, "prev-chapter", val );
744         }
745
746         vlc_object_release( p_input );
747         return VLC_SUCCESS;
748     }
749     else if( !strcmp( psz_cmd, "title" ) ||
750              !strcmp( psz_cmd, "title_n" ) ||
751              !strcmp( psz_cmd, "title_p" ) )
752     {
753         if( !strcmp( psz_cmd, "title" ) )
754         {
755             if ( *newval.psz_string )
756             {
757                 /* Set. */
758                 val.i_int = atoi( newval.psz_string );
759                 var_Set( p_input, "title", val );
760             }
761             else
762             {
763                 vlc_value_t val_list;
764
765                 /* Get. */
766                 var_Get( p_input, "title", &val );
767                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
768                             &val_list, NULL );
769                 printf( _("Currently playing title %d/%d\n"),
770                         val.i_int, val_list.p_list->i_count );
771                 var_Change( p_this, "title", VLC_VAR_FREELIST,
772                             &val_list, NULL );
773             }
774         }
775         else if( !strcmp( psz_cmd, "title_n" ) )
776         {
777             val.b_bool = VLC_TRUE;
778             var_Set( p_input, "next-title", val );
779         }
780         else if( !strcmp( psz_cmd, "title_p" ) )
781         {
782             val.b_bool = VLC_TRUE;
783             var_Set( p_input, "prev-title", val );
784         }
785
786         vlc_object_release( p_input );
787         return VLC_SUCCESS;
788     }
789
790     /* Never reached. */
791     return VLC_EGENERIC;
792 }
793
794 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
795                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
796 {
797     intf_thread_t *p_intf = (intf_thread_t*)p_this;
798     playlist_t *p_playlist;
799
800     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
801                                            FIND_ANYWHERE );
802     if( !p_playlist )
803     {
804         return VLC_ENOOBJ;
805     }
806
807     /* Parse commands that require a playlist */
808     if( !strcmp( psz_cmd, "prev" ) )
809     {
810         playlist_Prev( p_playlist );
811     }
812     else if( !strcmp( psz_cmd, "next" ) )
813     {
814         playlist_Next( p_playlist );
815     }
816     else if( !strcmp( psz_cmd, "play" ) )
817     {
818         playlist_Play( p_playlist );
819     }
820     else if( !strcmp( psz_cmd, "stop" ) )
821     {
822         playlist_Stop( p_playlist );
823     }
824     else if( !strcmp( psz_cmd, "add" ) )
825     {
826         printf( _("trying to add %s to playlist\n"), newval.psz_string );
827         playlist_Add( p_playlist, newval.psz_string, newval.psz_string,
828                       PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
829     }
830     else if( !strcmp( psz_cmd, "playlist" ) )
831     {
832         int i;
833         for ( i = 0; i < p_playlist->i_size; i++ )
834         {
835             printf( "|%s%s   %s|\n", i == p_playlist->i_index?"*":" ",
836                     p_playlist->pp_items[i]->input.psz_name,
837                     p_playlist->pp_items[i]->input.psz_uri );
838         }
839         if ( i == 0 )
840         {
841             printf( _("| no entries\n") );
842         }
843     }
844  
845     /*
846      * sanity check
847      */
848     else
849     {
850         printf( _("unknown command!\n") );
851     }
852
853     vlc_object_release( p_playlist );
854     return VLC_SUCCESS;
855 }
856
857 static int Other( vlc_object_t *p_this, char const *psz_cmd,
858                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
859 {
860     intf_thread_t *p_intf = (intf_thread_t*)p_this;
861     vlc_object_t *p_pl;
862     vlc_value_t     val;
863
864     p_pl = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
865                                            FIND_ANYWHERE );
866     if( !p_pl )
867     {
868         return VLC_ENOOBJ;
869     }
870
871     /* Parse miscellaneous commands */
872     if( !strcmp( psz_cmd, "marq-marquee" ) )
873     {
874         if( strlen( newval.psz_string ) > 0 )
875         {
876             val.psz_string = newval.psz_string;
877             var_Set( p_pl, "marq-marquee", val );
878         }
879         else 
880         {
881                 val.psz_string = "";
882                 var_Set( p_pl, "marq-marquee", val);
883         }
884     }
885     else if( !strcmp( psz_cmd, "marq-x" ) )
886     {
887         if( strlen( newval.psz_string ) > 0) 
888         {
889             val.i_int = atoi( newval.psz_string );
890             var_Set( p_pl, "marq-x", val );
891         }
892     }
893     else if( !strcmp( psz_cmd, "marq-y" ) )
894     {
895         if( strlen( newval.psz_string ) > 0) 
896         {
897             val.i_int = atoi( newval.psz_string );
898             var_Set( p_pl, "marq-y", val );
899         }
900     }
901     else if( !strcmp( psz_cmd, "marq-timeout" ) )
902     {
903         if( strlen( newval.psz_string ) > 0) 
904         {
905             val.i_int = atoi( newval.psz_string );
906             var_Set( p_pl, "marq-timeout", val );
907         }
908     }
909  
910     /*
911      * sanity check
912      */
913     else
914     {
915         printf( _("unknown command!\n") );
916     }
917
918     vlc_object_release( p_pl );
919     return VLC_SUCCESS;
920 }
921
922 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
923                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
924 {
925     p_this->p_vlc->b_die = VLC_TRUE;
926     return VLC_SUCCESS;
927 }
928
929 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
930                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
931 {
932     intf_thread_t *p_newintf;
933
934     p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
935
936     if( p_newintf )
937     {
938         p_newintf->b_block = VLC_FALSE;
939         if( intf_RunThread( p_newintf ) )
940         {
941             vlc_object_detach( p_newintf );
942             intf_Destroy( p_newintf );
943         }
944     }
945
946     return VLC_SUCCESS;
947 }
948
949 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
950                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
951 {
952     intf_thread_t *p_intf = (intf_thread_t*)p_this;
953     int i_error;
954
955     if ( *newval.psz_string )
956     {
957         /* Set. */
958         audio_volume_t i_volume = atoi( newval.psz_string );
959         if ( i_volume > AOUT_VOLUME_MAX )
960         {
961             printf( _("Volume must be in the range %d-%d\n"), AOUT_VOLUME_MIN,
962                     AOUT_VOLUME_MAX );
963             i_error = VLC_EBADVAR;
964         }
965         else i_error = aout_VolumeSet( p_this, i_volume );
966     }
967     else
968     {
969         /* Get. */
970         audio_volume_t i_volume;
971         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
972         {
973             i_error = VLC_EGENERIC;
974         }
975         else
976         {
977             printf( _("Volume is %d\n"), i_volume );
978             i_error = VLC_SUCCESS;
979         }
980     }
981
982     return i_error;
983 }
984
985 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
986                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
987 {
988     intf_thread_t *p_intf = (intf_thread_t*)p_this;
989     audio_volume_t i_volume;
990     int i_nb_steps = atoi(newval.psz_string);
991     int i_error = VLC_SUCCESS;
992
993     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
994     {
995         i_nb_steps = 1;
996     }
997
998     if ( !strcmp(psz_cmd, "volup") )
999     {
1000         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1001             i_error = VLC_EGENERIC;
1002     }
1003     else
1004     {
1005         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1006             i_error = VLC_EGENERIC;
1007     }
1008
1009     if ( !i_error ) printf( _("Volume is %d\n"), i_volume );
1010     return i_error;
1011 }
1012
1013 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1014                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1015 {
1016     intf_thread_t *p_intf = (intf_thread_t*)p_this;
1017     aout_instance_t * p_aout;
1018     const char * psz_variable;
1019     vlc_value_t val_name;
1020     int i_error;
1021
1022     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1023     if ( p_aout == NULL ) return VLC_ENOOBJ;
1024
1025     if ( !strcmp( psz_cmd, "adev" ) )
1026     {
1027         psz_variable = "audio-device";
1028     }
1029     else
1030     {
1031         psz_variable = "audio-channels";
1032     }
1033
1034     /* Get the descriptive name of the variable */
1035     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1036                  &val_name, NULL );
1037     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1038
1039     if ( !*newval.psz_string )
1040     {
1041         /* Retrieve all registered ***. */
1042         vlc_value_t val, text;
1043         int i, i_value;
1044
1045         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1046         {
1047             vlc_object_release( (vlc_object_t *)p_aout );
1048             return VLC_EGENERIC;
1049         }
1050         i_value = val.i_int;
1051
1052         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1053                          VLC_VAR_GETLIST, &val, &text ) < 0 )
1054         {
1055             vlc_object_release( (vlc_object_t *)p_aout );
1056             return VLC_EGENERIC;
1057         }
1058
1059         printf( "+----[ %s ]\n", val_name.psz_string );
1060         for ( i = 0; i < val.p_list->i_count; i++ )
1061         {
1062             if ( i_value == val.p_list->p_values[i].i_int )
1063                 printf( "| %i - %s *\n", val.p_list->p_values[i].i_int,
1064                         text.p_list->p_values[i].psz_string );
1065             else
1066                 printf( "| %i - %s\n", val.p_list->p_values[i].i_int,
1067                         text.p_list->p_values[i].psz_string );
1068         }
1069         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1070                     &val, &text );
1071         printf( _("+----[ end of %s ]\n"), val_name.psz_string );
1072
1073         if( val_name.psz_string ) free( val_name.psz_string );
1074         i_error = VLC_SUCCESS;
1075     }
1076     else
1077     {
1078         vlc_value_t val;
1079         val.i_int = atoi( newval.psz_string );
1080
1081         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1082     }
1083     vlc_object_release( (vlc_object_t *)p_aout );
1084
1085     return i_error;
1086 }
1087
1088 #ifdef WIN32
1089 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1090 {
1091     INPUT_RECORD input_record;
1092     DWORD i_dw;
1093
1094     /* On Win32, select() only works on socket descriptors */
1095     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1096                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1097     {
1098         while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1099                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1100                                  1, &i_dw ) )
1101         {
1102             if( input_record.EventType != KEY_EVENT ||
1103                 !input_record.Event.KeyEvent.bKeyDown ||
1104                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1105                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1106                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1107                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1108             {
1109                 /* nothing interesting */
1110                 continue;
1111             }
1112
1113             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1114
1115             /* Echo out the command */
1116             putc( p_buffer[ *pi_size ], stdout );
1117
1118             /* Handle special keys */
1119             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1120             {
1121                 putc( '\n', stdout );
1122                 break;
1123             }
1124             switch( p_buffer[ *pi_size ] )
1125             {
1126             case '\b':
1127                 if( *pi_size )
1128                 {
1129                     *pi_size -= 2;
1130                     putc( ' ', stdout );
1131                     putc( '\b', stdout );
1132                 }
1133                 break;
1134             case '\r':
1135                 (*pi_size) --;
1136                 break;
1137             }
1138
1139             (*pi_size)++;
1140         }
1141
1142         if( *pi_size == MAX_LINE_LENGTH ||
1143             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1144         {
1145             p_buffer[ *pi_size ] = 0;
1146             return VLC_TRUE;
1147         }
1148     }
1149
1150     return VLC_FALSE;
1151 }
1152 #endif
1153
1154 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1155 {
1156     int i_read = 0;
1157
1158 #ifdef WIN32
1159     if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1160         return ReadWin32( p_intf, p_buffer, pi_size );
1161     else if( p_intf->p_sys->i_socket == -1 )
1162     {
1163         msleep( INTF_IDLE_SLEEP );
1164         return VLC_FALSE;
1165     }
1166 #endif
1167
1168     while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1169            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
1170                        0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket,
1171                        p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
1172     {
1173         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1174             break;
1175
1176         (*pi_size)++;
1177     }
1178
1179     /* Connection closed */
1180     if( i_read == -1 )
1181     {
1182         p_intf->p_sys->i_socket = -1;
1183         p_buffer[ *pi_size ] = 0;
1184         return VLC_TRUE;
1185     }
1186
1187     if( *pi_size == MAX_LINE_LENGTH ||
1188         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1189     {
1190         p_buffer[ *pi_size ] = 0;
1191         return VLC_TRUE;
1192     }
1193
1194     return VLC_FALSE;
1195 }