]> git.sesse.net Git - vlc/blob - modules/control/rc/rc.c
1028c56c7bd0114d7e2bf80a64eb29be7043aa72
[vlc] / modules / control / rc / rc.c
1 /*****************************************************************************
2  * rc.c : remote control stdin/stdout plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: rc.c,v 1.26 2003/02/17 16:56:02 gbazin Exp $
6  *
7  * Authors: 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 "error.h"
50
51 #define MAX_LINE_LENGTH 256
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Activate     ( vlc_object_t * );
57 static void Run          ( intf_thread_t *p_intf );
58
59 static int  Input        ( vlc_object_t *, char const *,
60                            vlc_value_t, vlc_value_t, void * );
61 static int  Playlist     ( vlc_object_t *, char const *,
62                            vlc_value_t, vlc_value_t, void * );
63 static int  Quit         ( vlc_object_t *, char const *,
64                            vlc_value_t, vlc_value_t, void * );
65 static int  Intf         ( vlc_object_t *, char const *,
66                            vlc_value_t, vlc_value_t, void * );
67 static int  Volume       ( vlc_object_t *, char const *,
68                            vlc_value_t, vlc_value_t, void * );
69 static int  VolumeMove   ( vlc_object_t *, char const *,
70                            vlc_value_t, vlc_value_t, void * );
71 static int  AudioConfig  ( vlc_object_t *, char const *,
72                            vlc_value_t, vlc_value_t, void * );
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define POS_TEXT N_("show stream position")
78 #define POS_LONGTEXT N_("Show the current position in seconds within the stream from time to time.")
79
80 #define TTY_TEXT N_("fake TTY")
81 #define TTY_LONGTEXT N_("Force the rc plugin to use stdin as if it was a TTY.")
82
83 vlc_module_begin();
84     add_category_hint( N_("Remote control"), NULL );
85     add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT );
86 #ifdef HAVE_ISATTY
87     add_bool( "fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT );
88 #endif
89     set_description( _("remote control interface module") );
90     set_capability( "interface", 20 );
91     set_callbacks( Activate, NULL );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Activate: initialize and create stuff
96  *****************************************************************************/
97 static int Activate( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t*)p_this;
100
101 #if defined(HAVE_ISATTY) && !defined(WIN32)
102     /* Check that stdin is a TTY */
103     if( !config_GetInt( p_intf, "fake-tty" ) && !isatty( 0 ) )
104     {
105         msg_Warn( p_intf, "fd 0 is not a TTY" );
106         return VLC_EGENERIC;
107     }
108 #endif
109
110     /* Non-buffered stdout */
111     setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
112
113     p_intf->pf_run = Run;
114
115     CONSOLE_INTRO_MSG;
116
117     printf( "remote control interface initialized, `h' for help\n" );
118     return VLC_SUCCESS;
119 }
120
121 /*****************************************************************************
122  * Run: rc thread
123  *****************************************************************************
124  * This part of the interface is in a separate thread so that we can call
125  * exec() from within it without annoying the rest of the program.
126  *****************************************************************************/
127 static void Run( intf_thread_t *p_intf )
128 {
129     input_thread_t * p_input;
130     playlist_t *     p_playlist;
131
132     char       p_buffer[ MAX_LINE_LENGTH + 1 ];
133     vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
134     input_info_category_t * p_category;
135     input_info_t * p_info;
136
137     int        i_dummy;
138     off_t      i_oldpos = 0;
139     off_t      i_newpos;
140
141     double     f_ratio = 1.0;
142
143     p_input = NULL;
144     p_playlist = NULL;
145
146 #ifdef WIN32
147     HANDLE hConsoleIn;
148     INPUT_RECORD input_record;
149     DWORD i_dummy2;
150 #endif
151
152     /* Register commands that will be cleaned up upon object destruction */
153     var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
154     var_AddCallback( p_intf, "quit", Quit, NULL );
155     var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
156     var_AddCallback( p_intf, "intf", Intf, NULL );
157
158     var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
159     var_AddCallback( p_intf, "add", Playlist, NULL );
160     var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
161     var_AddCallback( p_intf, "playlist", Playlist, NULL );
162     var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
163     var_AddCallback( p_intf, "play", Playlist, NULL );
164     var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
165     var_AddCallback( p_intf, "stop", Playlist, NULL );
166     var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
167     var_AddCallback( p_intf, "prev", Playlist, NULL );
168     var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
169     var_AddCallback( p_intf, "next", Playlist, NULL );
170
171     var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
172     var_AddCallback( p_intf, "pause", Input, NULL );
173     var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
174     var_AddCallback( p_intf, "seek", Input, NULL );
175     var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
176     var_AddCallback( p_intf, "title", Input, NULL );
177     var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
178     var_AddCallback( p_intf, "title_n", Input, NULL );
179     var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
180     var_AddCallback( p_intf, "title_p", Input, NULL );
181     var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
182     var_AddCallback( p_intf, "chapter", Input, NULL );
183     var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
184     var_AddCallback( p_intf, "chapter_n", Input, NULL );
185     var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
186     var_AddCallback( p_intf, "chapter_p", Input, NULL );
187
188     var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189     var_AddCallback( p_intf, "volume", Volume, NULL );
190     var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191     var_AddCallback( p_intf, "volup", VolumeMove, NULL );
192     var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193     var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
194     var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
195     var_AddCallback( p_intf, "adev", AudioConfig, NULL );
196     var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
197     var_AddCallback( p_intf, "achan", AudioConfig, NULL );
198
199 #ifdef WIN32
200     /* Get the file descriptor of the console input */
201     hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
202     if( hConsoleIn == INVALID_HANDLE_VALUE )
203     {
204         msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" ); 
205         p_intf->b_die = VLC_TRUE;
206     }
207 #endif
208
209     while( !p_intf->b_die )
210     {
211         vlc_bool_t     b_complete = VLC_FALSE;
212
213 #ifndef WIN32
214         fd_set         fds;
215         struct timeval tv;
216
217         /* Check stdin */
218         tv.tv_sec = 0;
219         tv.tv_usec = (long)INTF_IDLE_SLEEP;
220         FD_ZERO( &fds );
221         FD_SET( STDIN_FILENO, &fds );
222
223         i_dummy = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
224 #else
225         /* On Win32, select() only works on socket descriptors */
226         i_dummy = ( WaitForSingleObject( hConsoleIn, INTF_IDLE_SLEEP/1000 )
227                     == WAIT_OBJECT_0 );
228 #endif
229         if( i_dummy > 0 )
230         {
231             int i_size = 0;
232
233             while( !p_intf->b_die
234                     && i_size < MAX_LINE_LENGTH
235 #ifndef WIN32
236                     && read( STDIN_FILENO, p_buffer + i_size, 1 ) > 0
237 #else
238                     && ReadConsoleInput( hConsoleIn, &input_record, 1,
239                                          &i_dummy2 )
240 #endif
241                    )
242             {
243 #ifdef WIN32
244                 if( input_record.EventType != KEY_EVENT ||
245                     !input_record.Event.KeyEvent.bKeyDown )
246                 {
247                     /* nothing interesting */
248                     continue;
249                 }
250
251                 p_buffer[ i_size ] =
252                     input_record.Event.KeyEvent.uChar.AsciiChar;
253 #endif
254
255                 if( p_buffer[ i_size ] == '\r' || p_buffer[ i_size ] == '\n' )
256                 {
257                     break;
258                 }
259
260                 i_size++;
261             }
262
263             if( i_size == MAX_LINE_LENGTH
264                  || p_buffer[ i_size ] == '\r'
265                  || p_buffer[ i_size ] == '\n' )
266             {
267                 p_buffer[ i_size ] = 0;
268                 b_complete = VLC_TRUE;
269             }
270         }
271
272         /* Manage the input part */
273         if( p_input == NULL )
274         {
275             if( p_playlist )
276             {
277                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
278                                                        FIND_CHILD );
279             }
280             else
281             {
282                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
283                                                    FIND_ANYWHERE );
284                 if( p_input )
285                 {
286                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
287                                                            FIND_PARENT );
288                 }
289             }
290         }
291         else if( p_input->b_dead )
292         {
293             vlc_object_release( p_input );
294             p_input = NULL;
295         }
296
297         if( p_input && b_showpos )
298         {
299             /* Get position */
300             vlc_mutex_lock( &p_input->stream.stream_lock );
301             if( !p_input->b_die && p_input->stream.i_mux_rate )
302             {
303 #define A p_input->stream.p_selected_area
304                 f_ratio = 1.0 / ( 50 * p_input->stream.i_mux_rate );
305                 i_newpos = A->i_tell * f_ratio;
306
307                 if( i_oldpos != i_newpos )
308                 {
309                     i_oldpos = i_newpos;
310                     printf( "pos: %li s / %li s\n", (long int)i_newpos,
311                             (long int)(f_ratio * A->i_size) );
312                 }
313 #undef S
314             }
315             vlc_mutex_unlock( &p_input->stream.stream_lock );
316         }
317
318         /* Is there something to do? */
319         if( b_complete )
320         {
321             char *psz_cmd, *psz_arg;
322
323             /* Skip heading spaces */
324             psz_cmd = p_buffer;
325             while( *psz_cmd == ' ' )
326             {
327                 psz_cmd++;
328             }
329
330             /* Split psz_cmd at the first space and make sure that
331              * psz_arg is valid */
332             psz_arg = strchr( psz_cmd, ' ' );
333             if( psz_arg )
334             {
335                 *psz_arg++ = 0;
336                 while( *psz_arg == ' ' )
337                 {
338                     psz_arg++;
339                 }
340             }
341             else
342             {
343                 psz_arg = "";
344             }
345
346             /* If the user typed a registered local command, try it */
347             if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
348             {
349                 vlc_value_t val;
350                 int i_ret;
351
352                 val.psz_string = psz_arg;
353                 i_ret = var_Set( p_intf, psz_cmd, val );
354                 printf( "%s: returned %i (%s)\n",
355                         psz_cmd, i_ret, vlc_error( i_ret ) );
356             }
357             /* Or maybe it's a global command */
358             else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
359             {
360                 vlc_value_t val;
361                 int i_ret;
362
363                 val.psz_string = psz_arg;
364                 /* FIXME: it's a global command, but we should pass the
365                  * local object as an argument, not p_intf->p_libvlc. */
366                 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
367                 printf( "%s: returned %i (%s)\n",
368                         psz_cmd, i_ret, vlc_error( i_ret ) );
369             }
370             else if( !strcmp( psz_cmd, "info" ) )
371             {
372                 if ( p_input )
373                 {
374                     vlc_mutex_lock( &p_input->stream.stream_lock );
375                     p_category = p_input->stream.p_info;
376                     while ( p_category )
377                     {
378                         printf( "+----[ %s ]\n", p_category->psz_name );
379                         printf( "| \n" );
380                         p_info = p_category->p_info;
381                         while ( p_info )
382                         {
383                             printf( "| %s: %s\n", p_info->psz_name,
384                                     p_info->psz_value );
385                             p_info = p_info->p_next;
386                         }
387                         p_category = p_category->p_next;
388                         printf( "| \n" );
389                     }
390                     printf( "+----[ end of stream info ]\n" );
391                     vlc_mutex_unlock( &p_input->stream.stream_lock );
392                 }
393                 else
394                 {
395                     printf( "no input\n" );
396                 }
397             }
398             else switch( psz_cmd[0] )
399             {
400             case 'f':
401             case 'F':
402                 if( p_input )
403                 {
404                     vout_thread_t *p_vout;
405                     p_vout = vlc_object_find( p_input,
406                                               VLC_OBJECT_VOUT, FIND_CHILD );
407
408                     if( p_vout )
409                     {
410                         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
411                         vlc_object_release( p_vout );
412                     }
413                 }
414                 break;
415
416             case 's':
417             case 'S':
418                 ;
419                 break;
420
421             case '?':
422             case 'h':
423             case 'H':
424                 printf("+----[ remote control commands ]\n");
425                 printf("| \n");
426                 printf("| add XYZ  . . . . . . . . . . add XYZ to playlist\n");
427                 printf("| playlist . . .  show items currently in playlist\n");
428                 printf("| play . . . . . . . . . . . . . . . . play stream\n");
429                 printf("| stop . . . . . . . . . . . . . . . . stop stream\n");
430                 printf("| next . . . . . . . . . . . .  next playlist item\n");
431                 printf("| prev . . . . . . . . . .  previous playlist item\n");
432                 printf("| title [X]  . . . . set/get title in current item\n");
433                 printf("| title_n  . . . . . .  next title in current item\n");
434                 printf("| title_p  . . . .  previous title in current item\n");
435                 printf("| chapter [X]  . . set/get chapter in current item\n");
436                 printf("| chapter_n  . . . .  next chapter in current item\n");
437                 printf("| chapter_p  . .  previous chapter in current item\n");
438                 printf("| \n");
439                 printf("| seek X . seek in seconds, for instance `seek 12'\n");
440                 printf("| pause  . . . . . . . . . . . . . .  toggle pause\n");
441                 printf("| f  . . . . . . . . . . . . . . toggle fullscreen\n");
442                 printf("| info . . .  information about the current stream\n");
443                 printf("| \n");
444                 printf("| volume [X] . . . . . . . .  set/get audio volume\n");
445                 printf("| volup [X]  . . . . .  raise audio volume X steps\n");
446                 printf("| voldown [X]  . . . .  lower audio volume X steps\n");
447                 printf("| adev [X] . . . . . . . . .  set/get audio device\n");
448                 printf("| achan [X]. . . . . . . .  set/get audio channels\n");
449                 printf("| \n");
450                 printf("| help . . . . . . . . . . . . . this help message\n");
451                 printf("| quit . . . . . . . . . . . . . . . . .  quit vlc\n");
452                 printf("| \n");
453                 printf("+----[ end of help ]\n");
454                 break;
455             case '\0':
456                 /* Ignore empty lines */
457                 break;
458             default:
459                 printf( "unknown command `%s', type `help' for help\n", psz_cmd );
460                 break;
461             }
462         }
463     }
464
465     if( p_input )
466     {
467         vlc_object_release( p_input );
468         p_input = NULL;
469     }
470
471     if( p_playlist )
472     {
473         vlc_object_release( p_playlist );
474         p_playlist = NULL;
475     }
476 }
477
478 static int Input( vlc_object_t *p_this, char const *psz_cmd,
479                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
480 {
481     input_thread_t * p_input;
482
483     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
484
485     if( !p_input )
486     {
487         return VLC_ENOOBJ;
488     }
489
490     /* Parse commands that only require an input */
491     if( !strcmp( psz_cmd, "pause" ) )
492     {
493         input_SetStatus( p_input, INPUT_STATUS_PAUSE );
494         vlc_object_release( p_input );
495         return VLC_SUCCESS;
496     }
497     else if( !strcmp( psz_cmd, "seek" ) )
498     {
499         input_Seek( p_input, atoi( newval.psz_string ),
500                     INPUT_SEEK_SECONDS | INPUT_SEEK_SET );
501     }
502     else if( !strcmp( psz_cmd, "chapter" ) ||
503              !strcmp( psz_cmd, "chapter_n" ) ||
504              !strcmp( psz_cmd, "chapter_p" ) )
505     {
506         unsigned int i_chapter = 0;
507
508         if( !strcmp( psz_cmd, "chapter" ) )
509         {
510             if ( *newval.psz_string )
511             {
512                 /* Set. */
513                 i_chapter = atoi( newval.psz_string );
514             }
515             else
516             {
517                 /* Get. */
518                 vlc_mutex_lock( &p_input->stream.stream_lock );
519                 printf( "Currently playing chapter %d/%d\n",
520                         p_input->stream.p_selected_area->i_part,
521                         p_input->stream.p_selected_area->i_part_nb - 1 );
522                 vlc_mutex_unlock( &p_input->stream.stream_lock );
523
524                 vlc_object_release( p_input );
525                 return VLC_SUCCESS;
526             }
527         }
528         else if( !strcmp( psz_cmd, "chapter_n" ) )
529         {
530             vlc_mutex_lock( &p_input->stream.stream_lock );
531             i_chapter = p_input->stream.p_selected_area->i_part + 1;
532             vlc_mutex_unlock( &p_input->stream.stream_lock );
533         }
534         else if( !strcmp( psz_cmd, "chapter_p" ) )
535         {
536             vlc_mutex_lock( &p_input->stream.stream_lock );
537             i_chapter = p_input->stream.p_selected_area->i_part - 1;
538             vlc_mutex_unlock( &p_input->stream.stream_lock );
539         }
540
541         vlc_mutex_lock( &p_input->stream.stream_lock );
542         if( ( i_chapter > 0 ) && ( i_chapter <
543             p_input->stream.p_selected_area->i_part_nb ) )
544         {
545             input_area_t *p_area = p_input->stream.p_selected_area;
546             p_input->stream.p_selected_area->i_part = i_chapter;
547             vlc_mutex_unlock( &p_input->stream.stream_lock );
548             input_ChangeArea( p_input, p_area );
549             input_SetStatus( p_input, INPUT_STATUS_PLAY );
550             vlc_mutex_lock( &p_input->stream.stream_lock );
551         }
552         vlc_mutex_unlock( &p_input->stream.stream_lock );
553
554         vlc_object_release( p_input );
555         return VLC_SUCCESS;
556     }
557     else if( !strcmp( psz_cmd, "title" ) ||
558              !strcmp( psz_cmd, "title_n" ) ||
559              !strcmp( psz_cmd, "title_p" ) )
560     {
561         unsigned int i_title = 0;
562
563         if( !strcmp( psz_cmd, "title" ) )
564         {
565             if ( *newval.psz_string )
566             {
567                 /* Set. */
568                 i_title = atoi( newval.psz_string );
569             }
570             else
571             {
572                 /* Get. */
573                 vlc_mutex_lock( &p_input->stream.stream_lock );
574                 printf( "Currently playing title %d/%d\n",
575                         p_input->stream.p_selected_area->i_id,
576                         p_input->stream.i_area_nb - 1 );
577                 vlc_mutex_unlock( &p_input->stream.stream_lock );
578
579                 vlc_object_release( p_input );
580                 return VLC_SUCCESS;
581             }
582         }
583         else if( !strcmp( psz_cmd, "title_n" ) )
584         {
585             vlc_mutex_lock( &p_input->stream.stream_lock );
586             i_title = p_input->stream.p_selected_area->i_id + 1;
587             vlc_mutex_unlock( &p_input->stream.stream_lock );
588         }
589         else if( !strcmp( psz_cmd, "title_p" ) )
590         {
591             vlc_mutex_lock( &p_input->stream.stream_lock );
592             i_title = p_input->stream.p_selected_area->i_id - 1;
593             vlc_mutex_unlock( &p_input->stream.stream_lock );
594         }
595
596         vlc_mutex_lock( &p_input->stream.stream_lock );
597         if( ( i_title > 0 ) && ( i_title < p_input->stream.i_area_nb ) )
598         {
599             input_area_t *p_area = p_input->stream.pp_areas[i_title];
600             vlc_mutex_unlock( &p_input->stream.stream_lock );
601             input_ChangeArea( p_input, p_area );
602             input_SetStatus( p_input, INPUT_STATUS_PLAY );
603             vlc_mutex_lock( &p_input->stream.stream_lock );
604         }
605         vlc_mutex_unlock( &p_input->stream.stream_lock );
606
607         vlc_object_release( p_input );
608         return VLC_SUCCESS;
609     }
610
611     /* Never reached. */
612     return VLC_EGENERIC;
613 }
614
615 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
616                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
617 {
618     playlist_t *     p_playlist;
619
620     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
621                                            FIND_ANYWHERE );
622     if( !p_playlist )
623     {
624         return VLC_ENOOBJ;
625     }
626
627     /* Parse commands that require a playlist */
628     if( !strcmp( psz_cmd, "prev" ) )
629     {
630         playlist_Prev( p_playlist );
631     }
632     else if( !strcmp( psz_cmd, "next" ) )
633     {
634         playlist_Next( p_playlist );
635     }
636     else if( !strcmp( psz_cmd, "play" ) )
637     {
638         playlist_Play( p_playlist );
639     }
640     else if( !strcmp( psz_cmd, "stop" ) )
641     {
642         playlist_Stop( p_playlist );
643     }
644     else if( !strcmp( psz_cmd, "add" ) )
645     {
646         printf( "trying to add %s to playlist\n", newval.psz_string );
647         playlist_Add( p_playlist, newval.psz_string,
648                       PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
649     }
650     else if( !strcmp( psz_cmd, "playlist" ) )
651     {
652         int i;
653         for ( i = 0; i < p_playlist->i_size; i++ )
654         {
655             printf( "|%s%s   %s|\n", i == p_playlist->i_index?"*":" ",
656                     p_playlist->pp_items[i]->psz_name,
657                     p_playlist->pp_items[i]->psz_uri );
658         }
659         if ( i == 0 )
660         {
661             printf( "| no entries\n" );
662         }
663     }
664     /*
665      * sanity check
666      */
667     else
668     {
669         printf( "unknown command!\n" );
670     }
671
672     vlc_object_release( p_playlist );
673     return VLC_SUCCESS;
674 }
675
676 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
677                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
678 {
679     p_this->p_vlc->b_die = VLC_TRUE;
680     return VLC_SUCCESS;
681 }
682
683 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
684                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
685 {
686     intf_thread_t *p_newintf;
687
688     p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
689
690     if( p_newintf )
691     {
692         p_newintf->b_block = VLC_FALSE;
693         if( intf_RunThread( p_newintf ) )
694         {
695             vlc_object_detach( p_newintf );
696             intf_Destroy( p_newintf );
697         }
698     }
699
700     return VLC_SUCCESS;
701 }
702
703 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
704                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
705 {
706     int i_error;
707
708     if ( *newval.psz_string )
709     {
710         /* Set. */
711         audio_volume_t i_volume = atoi( newval.psz_string );
712         if ( i_volume > AOUT_VOLUME_MAX )
713         {
714             printf( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
715                     AOUT_VOLUME_MAX );
716             i_error = VLC_EBADVAR;
717         }
718         else i_error = aout_VolumeSet( p_this, i_volume );
719     }
720     else
721     {
722         /* Get. */
723         audio_volume_t i_volume;
724         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
725         {
726             i_error = VLC_EGENERIC;
727         }
728         else
729         {
730             printf( "Volume is %d\n", i_volume );
731             i_error = VLC_SUCCESS;
732         }
733     }
734
735     return i_error;
736 }
737
738 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
739                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
740 {
741     audio_volume_t i_volume;
742     int i_nb_steps = atoi(newval.psz_string);
743     int i_error = VLC_SUCCESS;
744
745     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
746     {
747         i_nb_steps = 1;
748     }
749
750     if ( !strcmp(psz_cmd, "volup") )
751     {
752         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
753             i_error = VLC_EGENERIC;
754     }
755     else
756     {
757         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
758             i_error = VLC_EGENERIC;
759     }
760
761     if ( !i_error ) printf( "Volume is %d\n", i_volume );
762     return i_error;
763 }
764
765 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
766                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
767 {
768     aout_instance_t * p_aout;
769     const char * psz_variable;
770     const char * psz_name;
771     int i_error;
772
773     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
774     if ( p_aout == NULL ) return VLC_ENOOBJ;
775
776     if ( !strcmp( psz_cmd, "adev" ) )
777     {
778         psz_variable = "audio-device";
779         psz_name = "audio devices";
780     }
781     else
782     {
783         psz_variable = "audio-channels";
784         psz_name = "audio channels";
785     }
786
787     if ( !*newval.psz_string )
788     {
789         /* Retrieve all registered ***. */
790         vlc_value_t val;
791         int i;
792         char * psz_value;
793
794         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
795         {
796             vlc_object_release( (vlc_object_t *)p_aout );
797             return VLC_EGENERIC;
798         }
799         psz_value = val.psz_string;
800
801         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
802                          VLC_VAR_GETLIST, &val ) < 0 )
803         {
804             free( psz_value );
805             vlc_object_release( (vlc_object_t *)p_aout );
806             return VLC_EGENERIC;
807         }
808
809         printf( "+----[ %s ]\n", psz_name );
810         for ( i = 0; i < val.p_list->i_count; i++ )
811         {
812             if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
813                 printf( "| %s *\n", val.p_list->p_values[i].psz_string );
814             else
815                 printf( "| %s\n", val.p_list->p_values[i].psz_string );
816         }
817         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
818                     &val );
819         printf( "+----[ end of %s ]\n", psz_name );
820
821         free( psz_value );
822         i_error = VLC_SUCCESS;
823     }
824     else
825     {
826         vlc_value_t val;
827         val.psz_string = newval.psz_string;
828
829         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
830     }
831     vlc_object_release( (vlc_object_t *)p_aout );
832
833     return i_error;
834 }