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