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