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