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