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