1 /*****************************************************************************
2 * rc.c : remote control stdin/stdout module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004 - 2005 VideoLAN
7 * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <errno.h> /* ENOMEM */
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
47 #include <sys/types.h>
49 #include "vlc_error.h"
52 #if defined(PF_UNIX) && !defined(PF_LOCAL)
53 # define PF_LOCAL PF_UNIX
55 #if defined(AF_UNIX) && !defined(AF_LOCAL)
56 # define AF_LOCAL AF_UNIX
63 #define MAX_LINE_LENGTH 256
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Activate ( vlc_object_t * );
69 static void Deactivate ( vlc_object_t * );
70 static void Run ( intf_thread_t * );
72 static vlc_bool_t ReadCommand( intf_thread_t *, char *, int * );
74 static playlist_item_t *parse_MRL( intf_thread_t *, char * );
76 static int Input ( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
78 static int Playlist ( vlc_object_t *, char const *,
79 vlc_value_t, vlc_value_t, void * );
80 static int Other ( vlc_object_t *, char const *,
81 vlc_value_t, vlc_value_t, void * );
82 static int Quit ( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
84 static int Intf ( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
86 static int Volume ( vlc_object_t *, char const *,
87 vlc_value_t, vlc_value_t, void * );
88 static int VolumeMove ( vlc_object_t *, char const *,
89 vlc_value_t, vlc_value_t, void * );
90 static int AudioConfig ( vlc_object_t *, char const *,
91 vlc_value_t, vlc_value_t, void * );
106 #ifdef HAVE_VARIADIC_MACROS
107 # define msg_rc( psz_format, args... ) \
108 __msg_rc( p_intf, psz_format, ## args )
111 void __msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
114 va_start( args, psz_fmt );
115 if( p_intf->p_sys->i_socket == -1 ) vprintf( psz_fmt, args );
117 { net_vaPrintf( p_intf, p_intf->p_sys->i_socket, NULL, psz_fmt, args );
118 net_Printf( VLC_OBJECT(p_intf), p_intf->p_sys->i_socket, NULL, "\r" ); }
122 /*****************************************************************************
124 *****************************************************************************/
125 #define POS_TEXT N_("Show stream position")
126 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
127 "stream from time to time." )
129 #define TTY_TEXT N_("Fake TTY")
130 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
132 #define UNIX_TEXT N_("UNIX socket command input")
133 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
136 #define HOST_TEXT N_("TCP command input")
137 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
138 "You can set the address and port the interface will bind to." )
139 #define EXTEND_TEXT N_("Extended help")
140 #define EXTEND_LONGTEXT N_("List additional commands.")
143 #define QUIET_TEXT N_("Do not open a DOS command box interface")
144 #define QUIET_LONGTEXT N_( \
145 "By default the rc interface plugin will start a DOS command box. " \
146 "Enabling the quiet mode will not bring this command box but can also " \
147 "be pretty annoying when you want to stop VLC and no video window is " \
152 set_category( CAT_INTERFACE );
153 set_subcategory( SUBCAT_INTERFACE_GENERAL );
154 set_description( _("Remote control interface") );
155 add_bool( "rc-show-pos", 0, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
157 add_bool( "rc-fake-tty", 0, NULL, TTY_TEXT, TTY_LONGTEXT, VLC_TRUE );
159 add_string( "rc-unix", 0, NULL, UNIX_TEXT, UNIX_LONGTEXT, VLC_TRUE );
160 add_string( "rc-host", 0, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
163 add_bool( "rc-quiet", 0, NULL, QUIET_TEXT, QUIET_LONGTEXT, VLC_FALSE );
165 add_bool( "rc-extend", 0, NULL, EXTEND_TEXT, EXTEND_LONGTEXT, VLC_FALSE );
167 set_capability( "interface", 20 );
168 set_callbacks( Activate, Deactivate );
171 /*****************************************************************************
172 * Activate: initialize and create stuff
173 *****************************************************************************/
174 static int Activate( vlc_object_t *p_this )
176 intf_thread_t *p_intf = (intf_thread_t*)p_this;
177 playlist_t *p_playlist;
178 char *psz_host, *psz_unix_path;
181 #if defined(HAVE_ISATTY) && !defined(WIN32)
182 /* Check that stdin is a TTY */
183 if( !config_GetInt( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
185 msg_Warn( p_intf, "fd 0 is not a TTY" );
190 psz_unix_path = config_GetPsz( p_intf, "rc-unix" );
194 msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
195 free( psz_unix_path );
198 struct sockaddr_un addr;
201 memset( &addr, 0, sizeof(struct sockaddr_un) );
203 msg_Dbg( p_intf, "trying UNIX socket" );
205 if( (i_socket = socket( PF_LOCAL, SOCK_STREAM, 0 ) ) < 0 )
207 msg_Warn( p_intf, "can't open socket: %s", strerror(errno) );
208 free( psz_unix_path );
212 addr.sun_family = AF_LOCAL;
213 strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
214 addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
216 if( (i_ret = bind( i_socket, (struct sockaddr*)&addr,
217 sizeof(struct sockaddr_un) ) ) < 0 )
219 msg_Warn( p_intf, "couldn't bind socket to address: %s",
221 free( psz_unix_path );
222 net_Close( i_socket );
226 if( ( i_ret = listen( i_socket, 1 ) ) < 0 )
228 msg_Warn( p_intf, "can't listen on socket: %s", strerror(errno));
229 free( psz_unix_path );
230 net_Close( i_socket );
236 if( ( i_socket == -1) &&
237 ( psz_host = config_GetPsz( p_intf, "rc-host" ) ) != NULL )
241 vlc_UrlParse( &url, psz_host, 0 );
243 msg_Dbg( p_intf, "base %s port %d", url.psz_host, url.i_port );
245 if( (i_socket = net_ListenTCP(p_this, url.psz_host, url.i_port)) == -1)
247 msg_Warn( p_intf, "can't listen to %s port %i",
248 url.psz_host, url.i_port );
249 vlc_UrlClean( &url );
254 vlc_UrlClean( &url );
258 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
261 msg_Err( p_intf, "no memory" );
265 p_intf->p_sys->i_socket_listen = i_socket;
266 p_intf->p_sys->i_socket = -1;
267 p_intf->p_sys->psz_unix_path = psz_unix_path;
269 /* Non-buffered stdout */
270 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
272 p_intf->pf_run = Run;
275 p_intf->p_sys->b_quiet = config_GetInt( p_intf, "rc-quiet" );
276 if( !p_intf->p_sys->b_quiet ) { CONSOLE_INTRO_MSG; }
281 /* Force "no-view" mode */
282 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
286 vlc_mutex_lock( &p_playlist->object_lock );
287 p_playlist->status.i_view = -1;
288 vlc_mutex_unlock( &p_playlist->object_lock );
289 vlc_object_release( p_playlist );
292 msg_rc( _("Remote control interface initialized, `h' for help\n") );
296 /*****************************************************************************
297 * Deactivate: uninitialize and cleanup
298 *****************************************************************************/
299 static void Deactivate( vlc_object_t *p_this )
301 intf_thread_t *p_intf = (intf_thread_t*)p_this;
303 if( p_intf->p_sys->i_socket_listen != -1 )
304 net_Close( p_intf->p_sys->i_socket_listen );
305 if( p_intf->p_sys->i_socket != -1 )
306 net_Close( p_intf->p_sys->i_socket );
307 if( p_intf->p_sys->psz_unix_path != NULL )
310 unlink( p_intf->p_sys->psz_unix_path );
312 free( p_intf->p_sys->psz_unix_path );
314 free( p_intf->p_sys );
317 /*****************************************************************************
319 *****************************************************************************
320 * This part of the interface is in a separate thread so that we can call
321 * exec() from within it without annoying the rest of the program.
322 *****************************************************************************/
323 static void Run( intf_thread_t *p_intf )
325 input_thread_t * p_input;
326 playlist_t * p_playlist;
328 char p_buffer[ MAX_LINE_LENGTH + 1 ];
329 vlc_bool_t b_showpos = config_GetInt( p_intf, "rc-show-pos" );
339 p_intf->p_sys->b_extend = config_GetInt( p_intf, "rc-extend" );
340 /* Register commands that will be cleaned up upon object destruction */
341 var_Create( p_intf, "quit", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
342 var_AddCallback( p_intf, "quit", Quit, NULL );
343 var_Create( p_intf, "intf", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
344 var_AddCallback( p_intf, "intf", Intf, NULL );
346 var_Create( p_intf, "add", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
347 var_AddCallback( p_intf, "add", Playlist, NULL );
348 var_Create( p_intf, "playlist", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
349 var_AddCallback( p_intf, "playlist", Playlist, NULL );
350 var_Create( p_intf, "play", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
351 var_AddCallback( p_intf, "play", Playlist, NULL );
352 var_Create( p_intf, "stop", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
353 var_AddCallback( p_intf, "stop", Playlist, NULL );
354 var_Create( p_intf, "prev", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
355 var_AddCallback( p_intf, "prev", Playlist, NULL );
356 var_Create( p_intf, "next", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
357 var_AddCallback( p_intf, "next", Playlist, NULL );
358 var_Create( p_intf, "goto", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
359 var_AddCallback( p_intf, "goto", Playlist, NULL );
361 /* marquee on the fly items */
362 var_Create( p_intf, "marq-marquee", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
363 var_AddCallback( p_intf, "marq-marquee", Other, NULL );
364 var_Create( p_intf, "marq-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
365 var_AddCallback( p_intf, "marq-x", Other, NULL );
366 var_Create( p_intf, "marq-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
367 var_AddCallback( p_intf, "marq-y", Other, NULL );
368 var_Create( p_intf, "marq-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
369 var_AddCallback( p_intf, "marq-position", Other, NULL );
370 var_Create( p_intf, "marq-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
371 var_AddCallback( p_intf, "marq-color", Other, NULL );
372 var_Create( p_intf, "marq-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
373 var_AddCallback( p_intf, "marq-opacity", Other, NULL );
374 var_Create( p_intf, "marq-timeout", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
375 var_AddCallback( p_intf, "marq-timeout", Other, NULL );
377 /* time on the fly items */
378 var_Create( p_intf, "time-format", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
379 var_AddCallback( p_intf, "time-format", Other, NULL );
380 var_Create( p_intf, "time-x", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
381 var_AddCallback( p_intf, "time-x", Other, NULL );
382 var_Create( p_intf, "time-y", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
383 var_AddCallback( p_intf, "time-y", Other, NULL );
384 var_Create( p_intf, "time-position", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
385 var_AddCallback( p_intf, "time-position", Other, NULL );
386 var_Create( p_intf, "time-color", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
387 var_AddCallback( p_intf, "time-color", Other, NULL );
388 var_Create( p_intf, "time-opacity", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
389 var_AddCallback( p_intf, "time-opacity", Other, NULL );
392 var_Create( p_intf, "pause", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
393 var_AddCallback( p_intf, "pause", Input, NULL );
394 var_Create( p_intf, "seek", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
395 var_AddCallback( p_intf, "seek", Input, NULL );
396 var_Create( p_intf, "title", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
397 var_AddCallback( p_intf, "title", Input, NULL );
398 var_Create( p_intf, "title_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
399 var_AddCallback( p_intf, "title_n", Input, NULL );
400 var_Create( p_intf, "title_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
401 var_AddCallback( p_intf, "title_p", Input, NULL );
402 var_Create( p_intf, "chapter", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
403 var_AddCallback( p_intf, "chapter", Input, NULL );
404 var_Create( p_intf, "chapter_n", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
405 var_AddCallback( p_intf, "chapter_n", Input, NULL );
406 var_Create( p_intf, "chapter_p", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
407 var_AddCallback( p_intf, "chapter_p", Input, NULL );
409 var_Create( p_intf, "fastforward", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
410 var_AddCallback( p_intf, "fastforward", Input, NULL );
411 var_Create( p_intf, "rewind", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
412 var_AddCallback( p_intf, "rewind", Input, NULL );
414 var_Create( p_intf, "volume", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
415 var_AddCallback( p_intf, "volume", Volume, NULL );
416 var_Create( p_intf, "volup", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
417 var_AddCallback( p_intf, "volup", VolumeMove, NULL );
418 var_Create( p_intf, "voldown", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
419 var_AddCallback( p_intf, "voldown", VolumeMove, NULL );
420 var_Create( p_intf, "adev", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
421 var_AddCallback( p_intf, "adev", AudioConfig, NULL );
422 var_Create( p_intf, "achan", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
423 var_AddCallback( p_intf, "achan", AudioConfig, NULL );
426 /* Get the file descriptor of the console input */
427 p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
428 if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
430 msg_Err( p_intf, "Couldn't open STD_INPUT_HANDLE" );
431 p_intf->b_die = VLC_TRUE;
435 while( !p_intf->b_die )
437 char *psz_cmd, *psz_arg;
438 vlc_bool_t b_complete;
440 if( p_intf->p_sys->i_socket_listen != - 1 &&
441 p_intf->p_sys->i_socket == -1 )
443 p_intf->p_sys->i_socket =
444 net_Accept( p_intf, p_intf->p_sys->i_socket_listen, 0 );
447 b_complete = ReadCommand( p_intf, p_buffer, &i_size );
449 /* Manage the input part */
450 if( p_input == NULL )
454 p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
459 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
463 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
468 else if( p_input->b_dead )
470 vlc_object_release( p_input );
474 if( p_input && b_showpos )
476 i_newpos = 100 * var_GetFloat( p_input, "position" );
477 if( i_oldpos != i_newpos )
480 msg_rc( "pos: %d%%\n", i_newpos );
484 /* Is there something to do? */
485 if( !b_complete ) continue;
488 /* Skip heading spaces */
490 while( *psz_cmd == ' ' )
495 /* Split psz_cmd at the first space and make sure that
496 * psz_arg is valid */
497 psz_arg = strchr( psz_cmd, ' ' );
501 while( *psz_arg == ' ' )
511 /* If the user typed a registered local command, try it */
512 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
517 val.psz_string = psz_arg;
518 i_ret = var_Set( p_intf, psz_cmd, val );
519 msg_rc( "%s: returned %i (%s)\n",
520 psz_cmd, i_ret, vlc_error( i_ret ) );
522 /* Or maybe it's a global command */
523 else if( var_Type( p_intf->p_libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
528 val.psz_string = psz_arg;
529 /* FIXME: it's a global command, but we should pass the
530 * local object as an argument, not p_intf->p_libvlc. */
531 i_ret = var_Set( p_intf->p_libvlc, psz_cmd, val );
534 msg_rc( "%s: returned %i (%s)\n",
535 psz_cmd, i_ret, vlc_error( i_ret ) );
538 else if( !strcmp( psz_cmd, "logout" ) )
540 /* Close connection */
541 if( p_intf->p_sys->i_socket != -1 )
543 net_Close( p_intf->p_sys->i_socket );
545 p_intf->p_sys->i_socket = -1;
547 else if( !strcmp( psz_cmd, "info" ) )
552 vlc_mutex_lock( &p_input->input.p_item->lock );
553 for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
555 info_category_t *p_category =
556 p_input->input.p_item->pp_categories[i];
558 msg_rc( "+----[ %s ]\n", p_category->psz_name );
560 for ( j = 0; j < p_category->i_infos; j++ )
562 info_t *p_info = p_category->pp_infos[j];
563 msg_rc( "| %s: %s\n", p_info->psz_name,
568 msg_rc( "+----[ end of stream info ]\n" );
569 vlc_mutex_unlock( &p_input->input.p_item->lock );
573 msg_rc( "no input\n" );
576 else if( !strcmp( psz_cmd, "is_playing" ) )
587 else if( !strcmp( psz_cmd, "get_time" ) )
596 var_Get( p_input, "time", &time );
597 msg_rc( "%i\n", time.i_time / 1000000);
600 else if( !strcmp( psz_cmd, "get_length" ) )
609 var_Get( p_input, "length", &time );
610 msg_rc( "%i\n", time.i_time / 1000000);
613 else if( !strcmp( psz_cmd, "get_title" ) )
621 msg_rc( "%s\n", p_input->input.p_item->psz_name );
624 else switch( psz_cmd[0] )
630 vout_thread_t *p_vout;
631 p_vout = vlc_object_find( p_input,
632 VLC_OBJECT_VOUT, FIND_CHILD );
636 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
637 vlc_object_release( p_vout );
650 msg_rc(_("+----[ Remote control commands ]\n"));
652 msg_rc(_("| add XYZ . . . . . . . . . . add XYZ to playlist\n"));
653 msg_rc(_("| playlist . . . show items currently in playlist\n"));
654 msg_rc(_("| play . . . . . . . . . . . . . . . . play stream\n"));
655 msg_rc(_("| stop . . . . . . . . . . . . . . . . stop stream\n"));
656 msg_rc(_("| next . . . . . . . . . . . . next playlist item\n"));
657 msg_rc(_("| prev . . . . . . . . . . previous playlist item\n"));
658 msg_rc(_("| goto . . . . . . . . . . . . goto item at index\n"));
659 msg_rc(_("| title [X] . . . . set/get title in current item\n"));
660 msg_rc(_("| title_n . . . . . . next title in current item\n"));
661 msg_rc(_("| title_p . . . . previous title in current item\n"));
662 msg_rc(_("| chapter [X] . . set/get chapter in current item\n"));
663 msg_rc(_("| chapter_n . . . . next chapter in current item\n"));
664 msg_rc(_("| chapter_p . . previous chapter in current item\n"));
666 msg_rc(_("| seek X . seek in seconds, for instance `seek 12'\n"));
667 msg_rc(_("| pause . . . . . . . . . . . . . . toggle pause\n"));
668 msg_rc(_("| fastforward . . . . . . . set to maximum rate\n"));
669 msg_rc(_("| rewind . . . . . . . . . . set to minimum rate\n"));
670 msg_rc(_("| f . . . . . . . . . . . . . . toggle fullscreen\n"));
671 msg_rc(_("| info . . . information about the current stream\n"));
673 msg_rc(_("| volume [X] . . . . . . . . set/get audio volume\n"));
674 msg_rc(_("| volup [X] . . . . . raise audio volume X steps\n"));
675 msg_rc(_("| voldown [X] . . . . lower audio volume X steps\n"));
676 msg_rc(_("| adev [X] . . . . . . . . . set/get audio device\n"));
677 msg_rc(_("| achan [X]. . . . . . . . set/get audio channels\n"));
680 if (p_intf->p_sys->b_extend)
682 msg_rc(_("| marq-marquee STRING . . overlay STRING in video\n"));
683 msg_rc(_("| marq-x X . . . . . offset of marquee, from left\n"));
684 msg_rc(_("| marq-y Y . . . . . . offset of marquee, from top\n"));
685 msg_rc(_("| marq-position #. . . .relative position control\n"));
686 msg_rc(_("| marq-color # . . . . font color of marquee, RGB\n"));
687 msg_rc(_("| marq-opacity # . . . . . . . .opacity of marquee\n"));
688 msg_rc(_("| marq-timeout T. . . . timeout of marquee, in ms\n"));
690 msg_rc(_("| time-format STRING . . . overlay STRING in video\n"));
691 msg_rc(_("| time-x X . . . . .offset of timestamp, from left\n"));
692 msg_rc(_("| time-y Y . . . . . offset of timestamp, from top\n"));
693 msg_rc(_("| time-position #. . . .relative position control\n"));
694 msg_rc(_("| time-color # . . . font color of timestamp, RGB\n"));
695 msg_rc(_("| time-opacity # . . . . . . .opacity of timestamp\n"));
698 msg_rc(_("| help . . . . . . . . . . . . . this help message\n"));
699 msg_rc(_("| logout . . . . . exit (if in socket connection)\n"));
700 msg_rc(_("| quit . . . . . . . . . . . . . . . . . quit vlc\n"));
702 msg_rc(_("+----[ end of help ]\n"));
706 /* Ignore empty lines */
710 msg_rc(_("unknown command `%s', type `help' for help\n"), psz_cmd);
714 /* Command processed */
715 i_size = 0; p_buffer[0] = 0;
720 vlc_object_release( p_input );
726 vlc_object_release( p_playlist );
731 static int Input( vlc_object_t *p_this, char const *psz_cmd,
732 vlc_value_t oldval, vlc_value_t newval, void *p_data )
734 intf_thread_t *p_intf = (intf_thread_t*)p_this;
735 input_thread_t *p_input;
738 p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
739 if( !p_input ) return VLC_ENOOBJ;
741 /* Parse commands that only require an input */
742 if( !strcmp( psz_cmd, "pause" ) )
746 var_Set( p_input, "state", val );
747 vlc_object_release( p_input );
750 else if( !strcmp( psz_cmd, "seek" ) )
752 if( strlen( newval.psz_string ) > 0 &&
753 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
755 val.f_float = (float)atoi( newval.psz_string ) / 100.0;
756 var_Set( p_input, "position", val );
760 val.i_time = ((int64_t)atoi( newval.psz_string )) * 1000000;
761 var_Set( p_input, "time", val );
763 vlc_object_release( p_input );
766 else if ( !strcmp( psz_cmd, "fastforward" ) )
768 val.i_int = INPUT_RATE_MAX;
770 var_Set( p_input, "rate", val );
771 vlc_object_release( p_input );
774 else if ( !strcmp( psz_cmd, "rewind" ) )
776 val.i_int = INPUT_RATE_MIN;
778 var_Set( p_input, "rate", val );
779 vlc_object_release( p_input );
782 else if( !strcmp( psz_cmd, "chapter" ) ||
783 !strcmp( psz_cmd, "chapter_n" ) ||
784 !strcmp( psz_cmd, "chapter_p" ) )
786 if( !strcmp( psz_cmd, "chapter" ) )
788 if ( *newval.psz_string )
791 val.i_int = atoi( newval.psz_string );
792 var_Set( p_input, "chapter", val );
796 vlc_value_t val_list;
799 var_Get( p_input, "chapter", &val );
800 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
802 msg_rc( "Currently playing chapter %d/%d\n",
803 val.i_int, val_list.p_list->i_count );
804 var_Change( p_this, "chapter", VLC_VAR_FREELIST,
808 else if( !strcmp( psz_cmd, "chapter_n" ) )
810 val.b_bool = VLC_TRUE;
811 var_Set( p_input, "next-chapter", val );
813 else if( !strcmp( psz_cmd, "chapter_p" ) )
815 val.b_bool = VLC_TRUE;
816 var_Set( p_input, "prev-chapter", val );
819 vlc_object_release( p_input );
822 else if( !strcmp( psz_cmd, "title" ) ||
823 !strcmp( psz_cmd, "title_n" ) ||
824 !strcmp( psz_cmd, "title_p" ) )
826 if( !strcmp( psz_cmd, "title" ) )
828 if ( *newval.psz_string )
831 val.i_int = atoi( newval.psz_string );
832 var_Set( p_input, "title", val );
836 vlc_value_t val_list;
839 var_Get( p_input, "title", &val );
840 var_Change( p_input, "title", VLC_VAR_GETCHOICES,
842 msg_rc( "Currently playing title %d/%d\n",
843 val.i_int, val_list.p_list->i_count );
844 var_Change( p_this, "title", VLC_VAR_FREELIST,
848 else if( !strcmp( psz_cmd, "title_n" ) )
850 val.b_bool = VLC_TRUE;
851 var_Set( p_input, "next-title", val );
853 else if( !strcmp( psz_cmd, "title_p" ) )
855 val.b_bool = VLC_TRUE;
856 var_Set( p_input, "prev-title", val );
859 vlc_object_release( p_input );
867 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
868 vlc_value_t oldval, vlc_value_t newval, void *p_data )
871 intf_thread_t *p_intf = (intf_thread_t*)p_this;
872 playlist_t *p_playlist;
874 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
881 /* Parse commands that require a playlist */
882 if( !strcmp( psz_cmd, "prev" ) )
884 playlist_Prev( p_playlist );
886 else if( !strcmp( psz_cmd, "next" ) )
888 playlist_Next( p_playlist );
890 else if( !strcmp( psz_cmd, "play" ) )
892 playlist_Play( p_playlist );
894 else if (!strcmp( psz_cmd, "goto" ) )
896 if( strlen( newval.psz_string ) > 0)
898 val.i_int = atoi( newval.psz_string );
899 playlist_Goto( p_playlist, val.i_int);
902 else if( !strcmp( psz_cmd, "stop" ) )
904 playlist_Stop( p_playlist );
906 else if( !strcmp( psz_cmd, "add" ) &&
907 newval.psz_string && *newval.psz_string )
909 playlist_item_t *p_item = parse_MRL( p_intf, newval.psz_string );
913 msg_rc( "trying to add %s to playlist\n", newval.psz_string );
914 playlist_AddItem( p_playlist, p_item,
915 PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
918 else if( !strcmp( psz_cmd, "playlist" ) )
921 for ( i = 0; i < p_playlist->i_size; i++ )
923 msg_rc( "|%s%s %s|%s|\n", i == p_playlist->i_index ? "*" : " ",
924 p_playlist->pp_items[i]->input.psz_name,
925 p_playlist->pp_items[i]->input.psz_uri,
926 p_playlist->pp_items[i]->i_parents > 0 ?
927 p_playlist->pp_items[i]->pp_parents[0]->p_parent->input.psz_name : "" );
931 msg_rc( "| no entries\n" );
940 msg_rc( "unknown command!\n" );
943 vlc_object_release( p_playlist );
947 static int Other( vlc_object_t *p_this, char const *psz_cmd,
948 vlc_value_t oldval, vlc_value_t newval, void *p_data )
950 intf_thread_t *p_intf = (intf_thread_t*)p_this;
955 p_pl = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
962 p_inp = vlc_object_find( p_this, VLC_OBJECT_INPUT,
969 /* Parse miscellaneous commands */
970 if( !strcmp( psz_cmd, "marq-marquee" ) )
972 if( strlen( newval.psz_string ) > 0 )
974 val.psz_string = newval.psz_string;
975 var_Set( p_inp->p_libvlc, "marq-marquee", val );
980 var_Set( p_inp->p_libvlc, "marq-marquee", val);
983 else if( !strcmp( psz_cmd, "marq-x" ) )
985 if( strlen( newval.psz_string ) > 0)
987 val.i_int = atoi( newval.psz_string );
988 var_Set( p_inp->p_libvlc, "marq-x", val );
991 else if( !strcmp( psz_cmd, "marq-y" ) )
993 if( strlen( newval.psz_string ) > 0)
995 val.i_int = atoi( newval.psz_string );
996 var_Set( p_inp->p_libvlc, "marq-y", val );
999 else if( !strcmp( psz_cmd, "marq-position" ) )
1001 if( strlen( newval.psz_string ) > 0)
1003 val.i_int = atoi( newval.psz_string );
1004 var_Set( p_inp->p_libvlc, "marq-position", val );
1007 else if( !strcmp( psz_cmd, "marq-color" ) )
1009 if( strlen( newval.psz_string ) > 0)
1011 val.i_int = strtol( newval.psz_string, NULL, 0 );
1012 var_Set( p_inp->p_libvlc, "marq-color", val );
1015 else if( !strcmp( psz_cmd, "marq-opacity" ) )
1017 if( strlen( newval.psz_string ) > 0)
1019 val.i_int = strtol( newval.psz_string, NULL, 0 );
1020 var_Set( p_inp->p_libvlc, "marq-opacity", val );
1023 else if( !strcmp( psz_cmd, "marq-timeout" ) )
1025 if( strlen( newval.psz_string ) > 0)
1027 val.i_int = atoi( newval.psz_string );
1028 var_Set( p_inp, "marq-timeout", val );
1031 else if( !strcmp( psz_cmd, "time-format" ) )
1033 if( strlen( newval.psz_string ) > 0 )
1035 val.psz_string = newval.psz_string;
1036 var_Set( p_inp->p_libvlc, "time-format", val );
1040 val.psz_string = "";
1041 var_Set( p_inp->p_libvlc, "time-format", val);
1044 else if( !strcmp( psz_cmd, "time-x" ) )
1046 if( strlen( newval.psz_string ) > 0)
1048 val.i_int = atoi( newval.psz_string );
1049 var_Set( p_inp->p_libvlc, "time-x", val );
1052 else if( !strcmp( psz_cmd, "time-y" ) )
1054 if( strlen( newval.psz_string ) > 0)
1056 val.i_int = atoi( newval.psz_string );
1057 var_Set( p_inp->p_libvlc, "time-y", val );
1060 else if( !strcmp( psz_cmd, "time-position" ) )
1062 if( strlen( newval.psz_string ) > 0)
1064 val.i_int = atoi( newval.psz_string );
1065 var_Set( p_inp->p_libvlc, "time-position", val );
1068 else if( !strcmp( psz_cmd, "time-color" ) )
1070 if( strlen( newval.psz_string ) > 0)
1072 val.i_int = strtol( newval.psz_string, NULL, 0 );
1073 var_Set( p_inp->p_libvlc, "time-color", val );
1076 else if( !strcmp( psz_cmd, "time-opacity" ) )
1078 if( strlen( newval.psz_string ) > 0)
1080 val.i_int = strtol( newval.psz_string, NULL, 0 );
1081 var_Set( p_inp->p_libvlc, "time-opacity", val );
1090 msg_rc( "unknown command!\n" );
1093 vlc_object_release( p_pl );
1094 vlc_object_release( p_inp );
1098 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1099 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1101 p_this->p_vlc->b_die = VLC_TRUE;
1105 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1106 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1108 intf_thread_t *p_newintf;
1110 p_newintf = intf_Create( p_this->p_vlc, newval.psz_string );
1114 p_newintf->b_block = VLC_FALSE;
1115 if( intf_RunThread( p_newintf ) )
1117 vlc_object_detach( p_newintf );
1118 intf_Destroy( p_newintf );
1125 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1126 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1128 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1131 if ( *newval.psz_string )
1134 audio_volume_t i_volume = atoi( newval.psz_string );
1135 if ( i_volume > AOUT_VOLUME_MAX )
1137 msg_rc( "Volume must be in the range %d-%d\n", AOUT_VOLUME_MIN,
1139 i_error = VLC_EBADVAR;
1141 else i_error = aout_VolumeSet( p_this, i_volume );
1146 audio_volume_t i_volume;
1147 if ( aout_VolumeGet( p_this, &i_volume ) < 0 )
1149 i_error = VLC_EGENERIC;
1153 msg_rc( "Volume is %d\n", i_volume );
1154 i_error = VLC_SUCCESS;
1161 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1162 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1164 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1165 audio_volume_t i_volume;
1166 int i_nb_steps = atoi(newval.psz_string);
1167 int i_error = VLC_SUCCESS;
1169 if ( i_nb_steps <= 0 || i_nb_steps > (AOUT_VOLUME_MAX/AOUT_VOLUME_STEP) )
1174 if ( !strcmp(psz_cmd, "volup") )
1176 if ( aout_VolumeUp( p_this, i_nb_steps, &i_volume ) < 0 )
1177 i_error = VLC_EGENERIC;
1181 if ( aout_VolumeDown( p_this, i_nb_steps, &i_volume ) < 0 )
1182 i_error = VLC_EGENERIC;
1185 if ( !i_error ) msg_rc( "Volume is %d\n", i_volume );
1189 static int AudioConfig( vlc_object_t *p_this, char const *psz_cmd,
1190 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1192 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1193 aout_instance_t * p_aout;
1194 const char * psz_variable;
1195 vlc_value_t val_name;
1198 p_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1199 if ( p_aout == NULL ) return VLC_ENOOBJ;
1201 if ( !strcmp( psz_cmd, "adev" ) )
1203 psz_variable = "audio-device";
1207 psz_variable = "audio-channels";
1210 /* Get the descriptive name of the variable */
1211 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_GETTEXT,
1213 if( !val_name.psz_string ) val_name.psz_string = strdup(psz_variable);
1215 if ( !*newval.psz_string )
1217 /* Retrieve all registered ***. */
1218 vlc_value_t val, text;
1221 if ( var_Get( (vlc_object_t *)p_aout, psz_variable, &val ) < 0 )
1223 vlc_object_release( (vlc_object_t *)p_aout );
1224 return VLC_EGENERIC;
1226 i_value = val.i_int;
1228 if ( var_Change( (vlc_object_t *)p_aout, psz_variable,
1229 VLC_VAR_GETLIST, &val, &text ) < 0 )
1231 vlc_object_release( (vlc_object_t *)p_aout );
1232 return VLC_EGENERIC;
1235 msg_rc( "+----[ %s ]\n", val_name.psz_string );
1236 for ( i = 0; i < val.p_list->i_count; i++ )
1238 if ( i_value == val.p_list->p_values[i].i_int )
1239 msg_rc( "| %i - %s *\n", val.p_list->p_values[i].i_int,
1240 text.p_list->p_values[i].psz_string );
1242 msg_rc( "| %i - %s\n", val.p_list->p_values[i].i_int,
1243 text.p_list->p_values[i].psz_string );
1245 var_Change( (vlc_object_t *)p_aout, psz_variable, VLC_VAR_FREELIST,
1247 msg_rc( "+----[ end of %s ]\n", val_name.psz_string );
1249 if( val_name.psz_string ) free( val_name.psz_string );
1250 i_error = VLC_SUCCESS;
1255 val.i_int = atoi( newval.psz_string );
1257 i_error = var_Set( (vlc_object_t *)p_aout, psz_variable, val );
1259 vlc_object_release( (vlc_object_t *)p_aout );
1265 vlc_bool_t ReadWin32( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1267 INPUT_RECORD input_record;
1270 /* On Win32, select() only works on socket descriptors */
1271 while( WaitForSingleObject( p_intf->p_sys->hConsoleIn,
1272 INTF_IDLE_SLEEP/1000 ) == WAIT_OBJECT_0 )
1274 while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1275 ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record,
1278 if( input_record.EventType != KEY_EVENT ||
1279 !input_record.Event.KeyEvent.bKeyDown ||
1280 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1281 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1282 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1283 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1285 /* nothing interesting */
1289 p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
1291 /* Echo out the command */
1292 putc( p_buffer[ *pi_size ], stdout );
1294 /* Handle special keys */
1295 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1297 putc( '\n', stdout );
1300 switch( p_buffer[ *pi_size ] )
1306 putc( ' ', stdout );
1307 putc( '\b', stdout );
1318 if( *pi_size == MAX_LINE_LENGTH ||
1319 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1321 p_buffer[ *pi_size ] = 0;
1330 vlc_bool_t ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1335 if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1336 return ReadWin32( p_intf, p_buffer, pi_size );
1337 else if( p_intf->p_sys->i_socket == -1 )
1339 msleep( INTF_IDLE_SLEEP );
1344 while( !p_intf->b_die && *pi_size < MAX_LINE_LENGTH &&
1345 (i_read = net_ReadNonBlock( p_intf, p_intf->p_sys->i_socket == -1 ?
1346 0 /*STDIN_FILENO*/ : p_intf->p_sys->i_socket, NULL,
1347 p_buffer + *pi_size, 1, INTF_IDLE_SLEEP ) ) > 0 )
1349 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1355 /* Connection closed */
1358 p_intf->p_sys->i_socket = -1;
1359 p_buffer[ *pi_size ] = 0;
1363 if( *pi_size == MAX_LINE_LENGTH ||
1364 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1366 p_buffer[ *pi_size ] = 0;
1373 /*****************************************************************************
1374 * parse_MRL: build a playlist item from a full mrl
1375 *****************************************************************************
1376 * MRL format: "simplified-mrl [:option-name[=option-value]]"
1377 * We don't check for '"' or '\'', we just assume that a ':' that follows a
1378 * space is a new option. Should be good enough for our purpose.
1379 *****************************************************************************/
1380 static playlist_item_t *parse_MRL( intf_thread_t *p_intf, char *psz_mrl )
1382 #define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
1383 #define SKIPTRAILINGSPACE( p, d ) \
1384 { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1386 playlist_item_t *p_item = NULL;
1387 char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig;
1388 char **ppsz_options = NULL;
1389 int i, i_options = 0;
1391 if( !psz_mrl ) return 0;
1393 psz_mrl = psz_orig = strdup( psz_mrl );
1396 SKIPSPACE( psz_mrl );
1399 for( ; *psz_mrl; psz_mrl++ )
1401 if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
1403 /* We have a complete item */
1406 if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
1407 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
1409 /* We have a complete item */
1414 if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
1415 SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
1417 /* Remove '"' and '\'' if necessary */
1418 if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
1419 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1420 if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
1421 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1423 if( !psz_item_mrl ) psz_item_mrl = psz_item;
1424 else if( *psz_item )
1427 ppsz_options = realloc( ppsz_options, i_options * sizeof(char *) );
1428 ppsz_options[i_options - 1] = &psz_item[1];
1431 if( *psz_mrl ) SKIPSPACE( psz_mrl );
1434 /* Now create a playlist item */
1437 p_item = playlist_ItemNew( p_intf, psz_item_mrl, psz_item_mrl );
1438 for( i = 0; i < i_options; i++ )
1440 playlist_ItemAddOption( p_item, ppsz_options[i] );
1444 if( i_options ) free( ppsz_options );