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