]> git.sesse.net Git - vlc/blob - modules/control/rc.c
rc.c -- Add hooks to control marquee string and offsets
[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, "marquee", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
315     var_AddCallback( p_intf, "marquee", Input, NULL );
316     var_Create( p_intf, "marq-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
317     var_AddCallback( p_intf, "marq-x", Input, NULL );
318     var_Create( p_intf, "marq-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
319     var_AddCallback( p_intf, "marq-y", Input, NULL );
320
321     var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
322     var_AddCallback( p_intf, "pause", Input, NULL );
323     var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
324     var_AddCallback( p_intf, "seek", Input, NULL );
325     var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
326     var_AddCallback( p_intf, "title", Input, NULL );
327     var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
328     var_AddCallback( p_intf, "title_n", Input, NULL );
329     var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
330     var_AddCallback( p_intf, "title_p", Input, NULL );
331     var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
332     var_AddCallback( p_intf, "chapter", Input, NULL );
333     var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
334     var_AddCallback( p_intf, "chapter_n", Input, NULL );
335     var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
336     var_AddCallback( p_intf, "chapter_p", Input, NULL );
337
338     var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
339     var_AddCallback( p_intf, "volume", Volume, NULL );
340     var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
341     var_AddCallback( p_intf, "volup", VolumeMove, NULL );
342     var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
343     var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
344     var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
345     var_AddCallback( p_intf, "adev", AudioConfig, NULL );
346     var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
347     var_AddCallback( p_intf, "achan", AudioConfig, NULL );
348
349 #ifdef WIN32
350     /* Get the file descriptor of the console input */
351     p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
352     if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
353     {
354         msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
355         p_intf->b_die = VLC_TRUE;
356     }
357 #endif
358
359     while( !p_intf->b_die )
360     {
361         char *psz_cmd, *psz_arg;
362         vlc_bool_t b_complete;
363
364         if( p_intf->p_sys->i_socket_listen != - 1 &&
365             p_intf->p_sys->i_socket == -1 )
366         {
367             p_intf->p_sys->i_socket =
368                 net_Accept( p_intf, p_intf->p_sys->i_socket_listen, 0 );
369         }
370
371         b_complete = ReadCommand( p_intf, p_buffer, &i_size );
372
373         /* Manage the input part */
374         if( p_input == NULL )
375         {
376             if( p_playlist )
377             {
378                 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
379                                                        FIND_CHILD );
380             }
381             else
382             {
383                 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
384                                                    FIND_ANYWHERE );
385                 if( p_input )
386                 {
387                     p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
388                                                            FIND_PARENT );
389                 }
390             }
391         }
392         else if( p_input->b_dead )
393         {
394             vlc_object_release( p_input );
395             p_input = NULL;
396         }
397
398         if( p_input && b_showpos )
399         {
400             i_newpos = 100 * var_GetFloat( p_input, "position" );
401             if( i_oldpos != i_newpos )
402             {
403                 i_oldpos = i_newpos;
404                 printf( "pos: %d%%\n", i_newpos );
405             }
406         }
407
408         /* Is there something to do? */
409         if( !b_complete ) continue;
410
411
412         /* Skip heading spaces */
413         psz_cmd = p_buffer;
414         while( *psz_cmd == ' ' )
415         {
416             psz_cmd++;
417         }
418
419         /* Split psz_cmd at the first space and make sure that
420          * psz_arg is valid */
421         psz_arg = strchr( psz_cmd, ' ' );
422         if( psz_arg )
423         {
424             *psz_arg++ = 0;
425             while( *psz_arg == ' ' )
426             {
427                 psz_arg++;
428             }
429         }
430         else
431         {
432             psz_arg = "";
433         }
434
435         /* If the user typed a registered local command, try it */
436         if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
437         {
438             vlc_value_t val;
439             int i_ret;
440
441             val.psz_string = psz_arg;
442             i_ret = var_Set( p_intf, psz_cmd, val );
443             printf( _("%s: returned %i (%s)\n"),
444                     psz_cmd, i_ret, vlc_error( i_ret ) );
445         }
446         /* Or maybe it's a global command */
447         else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
448         {
449             vlc_value_t val;
450             int i_ret;
451
452             val.psz_string = psz_arg;
453             /* FIXME: it's a global command, but we should pass the
454              * local object as an argument, not p_intf->p_libvlc. */
455             i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
456             if( i_ret != 0 )
457             {
458                 printf( _("%s: returned %i (%s)\n"),
459                          psz_cmd, i_ret, vlc_error( i_ret ) );
460             }
461         }
462         else if( !strcmp( psz_cmd, "info" ) )
463         {
464             if( p_input )
465             {
466                 int i, j;
467                 vlc_mutex_lock( &p_input->input.p_item->lock );
468                 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
469                 {
470                     info_category_t *p_category =
471                         p_input->input.p_item->pp_categories[i];
472
473                     printf( "+----[ %s ]\n", p_category->psz_name );
474                     printf( "| \n" );
475                     for ( j = 0; j < p_category->i_infos; j++ )
476                     {
477                         info_t *p_info = p_category->pp_infos[j];
478                         printf( "| %s: %s\n", p_info->psz_name,
479                                 p_info->psz_value );
480                     }
481                     printf( "| \n" );
482                 }
483                 printf( _("+----[ end of stream info ]\n") );
484                 vlc_mutex_unlock( &p_input->input.p_item->lock );
485             }
486             else
487             {
488                 printf( _("no input\n") );
489             }
490         }
491         else if( !strcmp( psz_cmd, "is_playing" ) )
492         {
493             if( ! p_input )
494             {
495                 printf( "0\n" );
496             }
497             else
498             {
499                 printf( "1\n" );
500             }
501         }
502         else if( !strcmp( psz_cmd, "get_time" ) )
503         {
504             if( ! p_input )
505             {
506                 printf("0\n");
507             }
508             else
509             {
510                 vlc_value_t time;
511                 var_Get( p_input, "time", &time );
512                 printf( "%i\n", time.i_time / 1000000);
513             }
514         }
515         else if( !strcmp( psz_cmd, "get_length" ) )
516         {
517             if( ! p_input )
518             {
519                 printf("0\n");
520             }
521             else
522             {
523                 vlc_value_t time;
524                 var_Get( p_input, "length", &time );
525                 printf( "%i\n", time.i_time / 1000000);
526             }
527         }
528         else if( !strcmp( psz_cmd, "get_title" ) )
529         {
530             if( ! p_input )
531             {
532                 printf("\n");
533             }
534             else
535             {
536                 printf( "%s\n", p_input->input.p_item->psz_name );
537             }
538         }
539         else switch( psz_cmd[0] )
540         {
541         case 'f':
542         case 'F':
543             if( p_input )
544             {
545                 vout_thread_t *p_vout;
546                 p_vout = vlc_object_find( p_input,
547                                           VLC_OBJECT_VOUT, FIND_CHILD );
548
549                 if( p_vout )
550                 {
551                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
552                     vlc_object_release( p_vout );
553                 }
554             }
555             break;
556
557         case 's':
558         case 'S':
559             ;
560             break;
561
562         case '?':
563         case 'h':
564         case 'H':
565             printf(_("+----[ Remote control commands ]\n"));
566             printf("| \n");
567             printf(_("| add XYZ  . . . . . . . . . . add XYZ to playlist\n"));
568             printf(_("| playlist . . .  show items currently in playlist\n"));
569             printf(_("| play . . . . . . . . . . . . . . . . play stream\n"));
570             printf(_("| stop . . . . . . . . . . . . . . . . stop stream\n"));
571             printf(_("| next . . . . . . . . . . . .  next playlist item\n"));
572             printf(_("| prev . . . . . . . . . .  previous playlist item\n"));
573             printf(_("| title [X]  . . . . set/get title in current item\n"));
574             printf(_("| title_n  . . . . . .  next title in current item\n"));
575             printf(_("| title_p  . . . .  previous title in current item\n"));
576             printf(_("| chapter [X]  . . set/get chapter in current item\n"));
577             printf(_("| chapter_n  . . . .  next chapter in current item\n"));
578             printf(_("| chapter_p  . .  previous chapter in current item\n"));
579             printf("| \n");
580             printf(_("| marquee STRING . . . . . overlay STRING in video\n"));
581             printf(_("| marq-x X . . . . . .offset of marquee, from left\n"));
582             printf(_("| marq-y Y . . . . . . offset of marquee, from top\n"));
583             printf("| \n");
584             printf(_("| seek X . seek in seconds, for instance `seek 12'\n"));
585             printf(_("| pause  . . . . . . . . . . . . . .  toggle pause\n"));
586             printf(_("| f  . . . . . . . . . . . . . . toggle fullscreen\n"));
587             printf(_("| info . . .  information about the current stream\n"));
588             printf("| \n");
589             printf(_("| volume [X] . . . . . . . .  set/get audio volume\n"));
590             printf(_("| volup [X]  . . . . .  raise audio volume X steps\n"));
591             printf(_("| voldown [X]  . . . .  lower audio volume X steps\n"));
592             printf(_("| adev [X] . . . . . . . . .  set/get audio device\n"));
593             printf(_("| achan [X]. . . . . . . .  set/get audio channels\n"));
594             printf("| \n");
595             printf(_("| help . . . . . . . . . . . . . this help message\n"));
596             printf(_("| quit . . . . . . . . . . . . . . . . .  quit vlc\n"));
597             printf("| \n");
598             printf(_("+----[ end of help ]\n"));
599             break;
600
601         case '\0':
602             /* Ignore empty lines */
603             break;
604
605         default:
606             printf(_("unknown command `%s', type `help' for help\n"), psz_cmd);
607             break;
608         }
609
610         /* Command processed */
611         i_size = 0; p_buffer[0] = 0;
612     }
613
614     if( p_input )
615     {
616         vlc_object_release( p_input );
617         p_input = NULL;
618     }
619
620     if( p_playlist )
621     {
622         vlc_object_release( p_playlist );
623         p_playlist = NULL;
624     }
625 }
626
627 static int Input( vlc_object_t *p_this, char const *psz_cmd,
628                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
629 {
630     intf_thread_t *p_intf = (intf_thread_t*)p_this;
631     input_thread_t *p_input;
632     vlc_value_t     val;
633
634     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
635     if( !p_input ) return VLC_ENOOBJ;
636
637     /* Parse commands that only require an input */
638     if( !strcmp( psz_cmd, "pause" ) )
639     {
640         val.i_int = PAUSE_S;
641
642         var_Set( p_input, "state", val );
643         vlc_object_release( p_input );
644         return VLC_SUCCESS;
645     }
646     else if( !strcmp( psz_cmd, "seek" ) )
647     {
648         if( strlen( newval.psz_string ) > 0 &&
649             newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
650         {
651             val.f_float = (float)atoi( newval.psz_string ) / 100.0;
652             var_Set( p_input, "position", val );
653         }
654         else
655         {
656             val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
657             var_Set( p_input, "time", val );
658         }
659         vlc_object_release( p_input );
660         return VLC_SUCCESS;
661     }
662     else if( !strcmp( psz_cmd, "marquee" ) )
663     {
664         if( strlen( newval.psz_string ) > 0 )
665         {
666             val.psz_string = newval.psz_string;
667             /* check for the playlist structure */
668             vlc_object_t *p_pl =
669                   vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
670             if( !p_pl )
671                 {
672                   return VLC_ENOOBJ;
673                 }
674             var_Set( p_pl, "marquee", val );
675             vlc_object_release( p_pl );
676         }
677         vlc_object_release( p_input );
678         return VLC_SUCCESS;
679     }
680     else if( !strcmp( psz_cmd, "marq-x" ) )
681     {
682         if( strlen( newval.psz_string ) > 0) 
683         {
684             vlc_object_t *p_pl =
685                   vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
686             if( !p_pl )
687                 {
688                   return VLC_ENOOBJ;
689                 }
690             val.i_int = atoi( newval.psz_string );
691             var_Set( p_pl, "marq-x", val );
692              vlc_object_release( p_pl );
693         }
694         vlc_object_release( p_input );
695         return VLC_SUCCESS;
696     }
697     else if( !strcmp( psz_cmd, "marq-y" ) )
698     {
699         if( strlen( newval.psz_string ) > 0) 
700         {
701             vlc_object_t *p_pl =
702                   vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
703             if( !p_pl )
704                 {
705                   return VLC_ENOOBJ;
706                 }
707             val.i_int = atoi( newval.psz_string );
708             var_Set( p_pl, "marq-y", val );
709              vlc_object_release( p_pl );
710         }
711         vlc_object_release( p_input );
712         return VLC_SUCCESS;
713     }
714     
715     else if( !strcmp( psz_cmd, "chapter" ) ||
716              !strcmp( psz_cmd, "chapter_n" ) ||
717              !strcmp( psz_cmd, "chapter_p" ) )
718     {
719         if( !strcmp( psz_cmd, "chapter" ) )
720         {
721             if ( *newval.psz_string )
722             {
723                 /* Set. */
724                 val.i_int = atoi( newval.psz_string );
725                 var_Set( p_input, "chapter", val );
726             }
727             else
728             {
729                 vlc_value_t val_list;
730
731                 /* Get. */
732                 var_Get( p_input, "chapter", &val );
733                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
734                             &val_list, NULL );
735                 printf( _("Currently playing chapter %d/%d\n"),
736                         val.i_int, val_list.p_list->i_count );
737                 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
738                             &val_list, NULL );
739             }
740         }
741         else if( !strcmp( psz_cmd, "chapter_n" ) )
742         {
743             val.b_bool = VLC_TRUE;
744             var_Set( p_input, "next-chapter", val );
745         }
746         else if( !strcmp( psz_cmd, "chapter_p" ) )
747         {
748             val.b_bool = VLC_TRUE;
749             var_Set( p_input, "prev-chapter", val );
750         }
751
752         vlc_object_release( p_input );
753         return VLC_SUCCESS;
754     }
755     else if( !strcmp( psz_cmd, "title" ) ||
756              !strcmp( psz_cmd, "title_n" ) ||
757              !strcmp( psz_cmd, "title_p" ) )
758     {
759         if( !strcmp( psz_cmd, "title" ) )
760         {
761             if ( *newval.psz_string )
762             {
763                 /* Set. */
764                 val.i_int = atoi( newval.psz_string );
765                 var_Set( p_input, "title", val );
766             }
767             else
768             {
769                 vlc_value_t val_list;
770
771                 /* Get. */
772                 var_Get( p_input, "title", &val );
773                 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
774                             &val_list, NULL );
775                 printf( _("Currently playing title %d/%d\n"),
776                         val.i_int, val_list.p_list->i_count );
777                 var_Change( p_this, "title", VLC_VAR_FREELIST,
778                             &val_list, NULL );
779             }
780         }
781         else if( !strcmp( psz_cmd, "title_n" ) )
782         {
783             val.b_bool = VLC_TRUE;
784             var_Set( p_input, "next-title", val );
785         }
786         else if( !strcmp( psz_cmd, "title_p" ) )
787         {
788             val.b_bool = VLC_TRUE;
789             var_Set( p_input, "prev-title", val );
790         }
791
792         vlc_object_release( p_input );
793         return VLC_SUCCESS;
794     }
795
796     /* Never reached. */
797     return VLC_EGENERIC;
798 }
799
800 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
801                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
802 {
803     intf_thread_t *p_intf = (intf_thread_t*)p_this;
804     playlist_t *p_playlist;
805
806     p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
807                                            FIND_ANYWHERE );
808     if( !p_playlist )
809     {
810         return VLC_ENOOBJ;
811     }
812
813     /* Parse commands that require a playlist */
814     if( !strcmp( psz_cmd, "prev" ) )
815     {
816         playlist_Prev( p_playlist );
817     }
818     else if( !strcmp( psz_cmd, "next" ) )
819     {
820         playlist_Next( p_playlist );
821     }
822     else if( !strcmp( psz_cmd, "play" ) )
823     {
824         playlist_Play( p_playlist );
825     }
826     else if( !strcmp( psz_cmd, "stop" ) )
827     {
828         playlist_Stop( p_playlist );
829     }
830     else if( !strcmp( psz_cmd, "add" ) )
831     {
832         printf( _("trying to add %s to playlist\n"), newval.psz_string );
833         playlist_Add( p_playlist, newval.psz_string, newval.psz_string,
834                       PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
835     }
836     else if( !strcmp( psz_cmd, "playlist" ) )
837     {
838         int i;
839         for ( i = 0; i < p_playlist->i_size; i++ )
840         {
841             printf( "|%s%s   %s|\n", i == p_playlist->i_index?"*":" ",
842                     p_playlist->pp_items[i]->input.psz_name,
843                     p_playlist->pp_items[i]->input.psz_uri );
844         }
845         if ( i == 0 )
846         {
847             printf( _("| no entries\n") );
848         }
849     }
850     /*
851      * sanity check
852      */
853     else
854     {
855         printf( _("unknown command!\n") );
856     }
857
858     vlc_object_release( p_playlist );
859     return VLC_SUCCESS;
860 }
861
862 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
863                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
864 {
865     p_this->p_vlc->b_die = VLC_TRUE;
866     return VLC_SUCCESS;
867 }
868
869 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
870                  vlc_value_t oldval, vlc_value_t newval, void *p_data )
871 {
872     intf_thread_t *p_newintf;
873
874     p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
875
876     if( p_newintf )
877     {
878         p_newintf->b_block = VLC_FALSE;
879         if( intf_RunThread( p_newintf ) )
880         {
881             vlc_object_detach( p_newintf );
882             intf_Destroy( p_newintf );
883         }
884     }
885
886     return VLC_SUCCESS;
887 }
888
889 static int Volume( 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     int i_error;
894
895     if ( *newval.psz_string )
896     {
897         /* Set. */
898         audio_volume_t i_volume = atoi( newval.psz_string );
899         if ( i_volume > AOUT_VOLUME_MAX )
900         {
901             printf( _("Volume must be in the range %d-%d\n"), AOUT_VOLUME_MIN,
902                     AOUT_VOLUME_MAX );
903             i_error = VLC_EBADVAR;
904         }
905         else i_error = aout_VolumeSet( p_this, i_volume );
906     }
907     else
908     {
909         /* Get. */
910         audio_volume_t i_volume;
911         if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
912         {
913             i_error = VLC_EGENERIC;
914         }
915         else
916         {
917             printf( _("Volume is %d\n"), i_volume );
918             i_error = VLC_SUCCESS;
919         }
920     }
921
922     return i_error;
923 }
924
925 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
926                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
927 {
928     intf_thread_t *p_intf = (intf_thread_t*)p_this;
929     audio_volume_t i_volume;
930     int i_nb_steps = atoi(newval.psz_string);
931     int i_error = VLC_SUCCESS;
932
933     if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
934     {
935         i_nb_steps = 1;
936     }
937
938     if ( !strcmp(psz_cmd, "volup") )
939     {
940         if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
941             i_error = VLC_EGENERIC;
942     }
943     else
944     {
945         if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
946             i_error = VLC_EGENERIC;
947     }
948
949     if ( !i_error ) printf( _("Volume is %d\n"), i_volume );
950     return i_error;
951 }
952
953 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
954                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
955 {
956     intf_thread_t *p_intf = (intf_thread_t*)p_this;
957     aout_instance_t * p_aout;
958     const char * psz_variable;
959     vlc_value_t val_name;
960     int i_error;
961
962     p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
963     if ( p_aout == NULL ) return VLC_ENOOBJ;
964
965     if ( !strcmp( psz_cmd, "adev" ) )
966     {
967         psz_variable = "audio-device";
968     }
969     else
970     {
971         psz_variable = "audio-channels";
972     }
973
974     /* Get the descriptive name of the variable */
975     var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
976                  &val_name, NULL );
977     if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
978
979     if ( !*newval.psz_string )
980     {
981         /* Retrieve all registered ***. */
982         vlc_value_t val, text;
983         int i, i_value;
984
985         if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
986         {
987             vlc_object_release( (vlc_object_t *)p_aout );
988             return VLC_EGENERIC;
989         }
990         i_value = val.i_int;
991
992         if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
993                          VLC_VAR_GETLIST, &val, &text ) < 0 )
994         {
995             vlc_object_release( (vlc_object_t *)p_aout );
996             return VLC_EGENERIC;
997         }
998
999         printf( "+----[ %s ]\n", val_name.psz_string );
1000         for ( i = 0; i < val.p_list->i_count; i++ )
1001         {
1002             if ( i_value == val.p_list->p_values[i].i_int )
1003                 printf( "| %i - %s *\n", val.p_list->p_values[i].i_int,
1004                         text.p_list->p_values[i].psz_string );
1005             else
1006                 printf( "| %i - %s\n", val.p_list->p_values[i].i_int,
1007                         text.p_list->p_values[i].psz_string );
1008         }
1009         var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1010                     &val, &text );
1011         printf( _("+----[ end of %s ]\n"), val_name.psz_string );
1012
1013         if( val_name.psz_string ) free( val_name.psz_string );
1014         i_error = VLC_SUCCESS;
1015     }
1016     else
1017     {
1018         vlc_value_t val;
1019         val.i_int = atoi( newval.psz_string );
1020
1021         i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1022     }
1023     vlc_object_release( (vlc_object_t *)p_aout );
1024
1025     return i_error;
1026 }
1027
1028 #ifdef WIN32
1029 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1030 {
1031     INPUT_RECORD input_record;
1032     DWORD i_dw;
1033
1034     /* On Win32, select() only works on socket descriptors */
1035     while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1036                                 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1037     {
1038         while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1039                ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1040                                  1, &i_dw ) )
1041         {
1042             if( input_record.EventType != KEY_EVENT ||
1043                 !input_record.Event.KeyEvent.bKeyDown ||
1044                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1045                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1046                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1047                 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1048             {
1049                 /* nothing interesting */
1050                 continue;
1051             }
1052
1053             p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1054
1055             /* Echo out the command */
1056             putc( p_buffer[ *pi_size ], stdout );
1057
1058             /* Handle special keys */
1059             if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1060             {
1061                 putc( '\n', stdout );
1062                 break;
1063             }
1064             switch( p_buffer[ *pi_size ] )
1065             {
1066             case '\b':
1067                 if( *pi_size )
1068                 {
1069                     *pi_size -= 2;
1070                     putc( ' ', stdout );
1071                     putc( '\b', stdout );
1072                 }
1073                 break;
1074             case '\r':
1075                 (*pi_size) --;
1076                 break;
1077             }
1078
1079             (*pi_size)++;
1080         }
1081
1082         if( *pi_size == MAX_LINE_LENGTH ||
1083             p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1084         {
1085             p_buffer[ *pi_size ] = 0;
1086             return VLC_TRUE;
1087         }
1088     }
1089
1090     return VLC_FALSE;
1091 }
1092 #endif
1093
1094 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1095 {
1096     int i_read = 0;
1097
1098 #ifdef WIN32
1099     if( p_intf->p_sys->i_socket == -1 )
1100         return ReadWin32( p_intf, p_buffer, pi_size );
1101 #endif
1102
1103     while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1104            (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
1105                        STDIN_FILENO : p_intf->p_sys->i_socket,
1106                        p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
1107     {
1108         if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1109             break;
1110
1111         (*pi_size)++;
1112     }
1113
1114     /* Connection closed */
1115     if( i_read == -1 )
1116     {
1117         p_intf->p_sys->i_socket = -1;
1118         p_buffer[ *pi_size ] = 0;
1119         return VLC_TRUE;
1120     }
1121
1122     if( *pi_size == MAX_LINE_LENGTH ||
1123         p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1124     {
1125         p_buffer[ *pi_size ] = 0;
1126         return VLC_TRUE;
1127     }
1128
1129     return VLC_FALSE;
1130 }