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