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