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