sout:display: convert the "delay" in ms to ticks using VLC_TICK macros
[vlc.git] / modules / control / oldrc.c
blob2c9c2728e3315d5242f154290fdf0c181bf03255
1 /*****************************************************************************
2 * oldrc.c : remote control stdin/stdout module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004-2009 the VideoLAN team
5 * $Id$
7 * Author: Peter Surda <shurdeek@panorama.sth.ac.at>
8 * Jean-Paul Saman <jpsaman #_at_# m2x _replaceWith#dot_ nl>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <errno.h> /* ENOMEM */
34 #include <signal.h>
35 #include <assert.h>
36 #include <math.h>
38 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_interface.h>
42 #include <vlc_input.h>
43 #include <vlc_aout.h>
44 #include <vlc_vout.h>
45 #include <vlc_playlist.h>
46 #include <vlc_actions.h>
48 #include <sys/types.h>
49 #include <unistd.h>
52 #include <vlc_network.h>
53 #include <vlc_url.h>
54 #include <vlc_charset.h>
56 #if defined(PF_UNIX) && !defined(PF_LOCAL)
57 # define PF_LOCAL PF_UNIX
58 #endif
60 #if defined(AF_LOCAL) && ! defined(_WIN32)
61 # include <sys/un.h>
62 #endif
64 #define MAX_LINE_LENGTH 1024
65 #define STATUS_CHANGE "status change: "
67 /* input_state_e from <vlc_input.h> */
68 static const char *ppsz_input_state[] = {
69 [INIT_S] = N_("Initializing"),
70 [OPENING_S] = N_("Opening"),
71 [PLAYING_S] = N_("Play"),
72 [PAUSE_S] = N_("Pause"),
73 [END_S] = N_("End"),
74 [ERROR_S] = N_("Error"),
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static int Activate ( vlc_object_t * );
81 static void Deactivate ( vlc_object_t * );
82 static void *Run ( void * );
84 static void Help ( intf_thread_t * );
85 static void RegisterCallbacks( intf_thread_t * );
87 static bool ReadCommand( intf_thread_t *, char *, int * );
89 static input_item_t *parse_MRL( const char * );
91 static int Input ( vlc_object_t *, char const *,
92 vlc_value_t, vlc_value_t, void * );
93 static int Playlist ( vlc_object_t *, char const *,
94 vlc_value_t, vlc_value_t, void * );
95 static int Quit ( vlc_object_t *, char const *,
96 vlc_value_t, vlc_value_t, void * );
97 static int Intf ( vlc_object_t *, char const *,
98 vlc_value_t, vlc_value_t, void * );
99 static int Volume ( vlc_object_t *, char const *,
100 vlc_value_t, vlc_value_t, void * );
101 static int VolumeMove ( vlc_object_t *, char const *,
102 vlc_value_t, vlc_value_t, void * );
103 static int VideoConfig ( vlc_object_t *, char const *,
104 vlc_value_t, vlc_value_t, void * );
105 static int AudioDevice ( vlc_object_t *, char const *,
106 vlc_value_t, vlc_value_t, void * );
107 static int AudioChannel ( vlc_object_t *, char const *,
108 vlc_value_t, vlc_value_t, void * );
109 static int Statistics ( vlc_object_t *, char const *,
110 vlc_value_t, vlc_value_t, void * );
112 static int updateStatistics( intf_thread_t *, input_item_t *);
114 /* Status Callbacks */
115 static int VolumeChanged( vlc_object_t *, char const *,
116 vlc_value_t, vlc_value_t, void * );
117 static int InputEvent( vlc_object_t *, char const *,
118 vlc_value_t, vlc_value_t, void * );
120 struct intf_sys_t
122 int *pi_socket_listen;
123 int i_socket;
124 char *psz_unix_path;
125 vlc_thread_t thread;
127 /* status changes */
128 vlc_mutex_t status_lock;
129 int i_last_state;
130 playlist_t *p_playlist;
131 input_thread_t *p_input;
132 bool b_input_buffering;
134 #ifdef _WIN32
135 HANDLE hConsoleIn;
136 bool b_quiet;
137 #endif
140 VLC_FORMAT(2, 3)
141 static void msg_rc( intf_thread_t *p_intf, const char *psz_fmt, ... )
143 va_list args;
144 char fmt_eol[strlen (psz_fmt) + 3];
146 snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
147 va_start( args, psz_fmt );
149 if( p_intf->p_sys->i_socket == -1 )
150 utf8_vfprintf( stdout, fmt_eol, args );
151 else
152 net_vaPrintf( p_intf, p_intf->p_sys->i_socket, fmt_eol, args );
153 va_end( args );
155 #define msg_rc( ... ) msg_rc( p_intf, __VA_ARGS__ )
157 /*****************************************************************************
158 * Module descriptor
159 *****************************************************************************/
160 #define POS_TEXT N_("Show stream position")
161 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
162 "stream from time to time." )
164 #define TTY_TEXT N_("Fake TTY")
165 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
167 #define UNIX_TEXT N_("UNIX socket command input")
168 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
169 "stdin." )
171 #define HOST_TEXT N_("TCP command input")
172 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
173 "You can set the address and port the interface will bind to." )
175 #ifdef _WIN32
176 #define QUIET_TEXT N_("Do not open a DOS command box interface")
177 #define QUIET_LONGTEXT N_( \
178 "By default the rc interface plugin will start a DOS command box. " \
179 "Enabling the quiet mode will not bring this command box but can also " \
180 "be pretty annoying when you want to stop VLC and no video window is " \
181 "open." )
182 #if !VLC_WINSTORE_APP
183 #include "intromsg.h"
184 #endif
185 #endif
187 vlc_module_begin ()
188 set_shortname( N_("RC"))
189 set_category( CAT_INTERFACE )
190 set_subcategory( SUBCAT_INTERFACE_MAIN )
191 set_description( N_("Remote control interface") )
192 add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
194 #ifdef _WIN32
195 add_bool( "rc-quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false )
196 #else
197 #if defined (HAVE_ISATTY)
198 add_bool( "rc-fake-tty", false, TTY_TEXT, TTY_LONGTEXT, true )
199 #endif
200 add_string( "rc-unix", NULL, UNIX_TEXT, UNIX_LONGTEXT, true )
201 #endif
202 add_string( "rc-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
204 set_capability( "interface", 20 )
206 set_callbacks( Activate, Deactivate )
207 #ifdef _WIN32
208 add_shortcut( "rc" )
209 #endif
210 vlc_module_end ()
212 /*****************************************************************************
213 * Activate: initialize and create stuff
214 *****************************************************************************/
215 static int Activate( vlc_object_t *p_this )
217 /* FIXME: This function is full of memory leaks and bugs in error paths. */
218 intf_thread_t *p_intf = (intf_thread_t*)p_this;
219 playlist_t *p_playlist = pl_Get( p_intf );
220 char *psz_host, *psz_unix_path = NULL;
221 int *pi_socket = NULL;
223 #ifndef _WIN32
224 #if defined(HAVE_ISATTY)
225 /* Check that stdin is a TTY */
226 if( !var_InheritBool( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
228 msg_Warn( p_intf, "fd 0 is not a TTY" );
229 return VLC_EGENERIC;
231 #endif
233 psz_unix_path = var_InheritString( p_intf, "rc-unix" );
234 if( psz_unix_path )
236 int i_socket;
238 #ifndef AF_LOCAL
239 msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
240 free( psz_unix_path );
241 return VLC_EGENERIC;
242 #else
243 struct sockaddr_un addr;
245 memset( &addr, 0, sizeof(struct sockaddr_un) );
247 msg_Dbg( p_intf, "trying UNIX socket" );
249 if( (i_socket = vlc_socket( PF_LOCAL, SOCK_STREAM, 0, false ) ) < 0 )
251 msg_Warn( p_intf, "can't open socket: %s", vlc_strerror_c(errno) );
252 free( psz_unix_path );
253 return VLC_EGENERIC;
256 addr.sun_family = AF_LOCAL;
257 strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
258 addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
260 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
261 && (errno == EADDRINUSE)
262 && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
263 && (errno == ECONNREFUSED))
265 msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
266 unlink (psz_unix_path);
268 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
270 msg_Err (p_intf, "cannot bind UNIX socket at %s: %s",
271 psz_unix_path, vlc_strerror_c(errno));
272 free (psz_unix_path);
273 net_Close (i_socket);
274 return VLC_EGENERIC;
278 if( listen( i_socket, 1 ) )
280 msg_Warn (p_intf, "can't listen on socket: %s",
281 vlc_strerror_c(errno));
282 free( psz_unix_path );
283 net_Close( i_socket );
284 return VLC_EGENERIC;
287 /* FIXME: we need a core function to merge listening sockets sets */
288 pi_socket = calloc( 2, sizeof( int ) );
289 if( pi_socket == NULL )
291 free( psz_unix_path );
292 net_Close( i_socket );
293 return VLC_ENOMEM;
295 pi_socket[0] = i_socket;
296 pi_socket[1] = -1;
297 #endif /* AF_LOCAL */
299 #endif /* !_WIN32 */
301 if( ( pi_socket == NULL ) &&
302 ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
304 vlc_url_t url;
306 vlc_UrlParse( &url, psz_host );
307 if( url.psz_host == NULL )
309 vlc_UrlClean( &url );
310 char *psz_backward_compat_host;
311 if( asprintf( &psz_backward_compat_host, "//%s", psz_host ) < 0 )
313 free( psz_host );
314 return VLC_EGENERIC;
316 free( psz_host );
317 psz_host = psz_backward_compat_host;
318 vlc_UrlParse( &url, psz_host );
321 msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
323 pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
324 if( pi_socket == NULL )
326 msg_Warn( p_intf, "can't listen to %s port %i",
327 url.psz_host, url.i_port );
328 vlc_UrlClean( &url );
329 free( psz_host );
330 return VLC_EGENERIC;
333 vlc_UrlClean( &url );
334 free( psz_host );
337 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
338 if( unlikely(p_sys == NULL) )
340 net_ListenClose( pi_socket );
341 free( psz_unix_path );
342 return VLC_ENOMEM;
345 p_intf->p_sys = p_sys;
346 p_sys->pi_socket_listen = pi_socket;
347 p_sys->i_socket = -1;
348 p_sys->psz_unix_path = psz_unix_path;
349 vlc_mutex_init( &p_sys->status_lock );
350 p_sys->i_last_state = PLAYLIST_STOPPED;
351 p_sys->b_input_buffering = false;
352 p_sys->p_playlist = p_playlist;
353 p_sys->p_input = NULL;
355 /* Non-buffered stdout */
356 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
358 #if VLC_WINSTORE_APP
359 p_sys->b_quiet = true;
360 #elif defined(_WIN32)
361 p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
362 if( !p_sys->b_quiet )
363 intf_consoleIntroMsg( p_intf );
364 #endif
366 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
367 abort();
369 msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
371 /* Listen to audio volume updates */
372 var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
373 return VLC_SUCCESS;
376 /*****************************************************************************
377 * Deactivate: uninitialize and cleanup
378 *****************************************************************************/
379 static void Deactivate( vlc_object_t *p_this )
381 intf_thread_t *p_intf = (intf_thread_t*)p_this;
382 intf_sys_t *p_sys = p_intf->p_sys;
384 vlc_cancel( p_sys->thread );
385 var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
386 vlc_join( p_sys->thread, NULL );
388 if( p_sys->p_input != NULL )
390 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
391 vlc_object_release( p_sys->p_input );
394 net_ListenClose( p_sys->pi_socket_listen );
395 if( p_sys->i_socket != -1 )
396 net_Close( p_sys->i_socket );
397 if( p_sys->psz_unix_path != NULL )
399 #if defined(AF_LOCAL) && !defined(_WIN32)
400 unlink( p_sys->psz_unix_path );
401 #endif
402 free( p_sys->psz_unix_path );
404 vlc_mutex_destroy( &p_sys->status_lock );
405 free( p_sys );
408 /*****************************************************************************
409 * RegisterCallbacks: Register callbacks to dynamic variables
410 *****************************************************************************/
411 static void RegisterCallbacks( intf_thread_t *p_intf )
413 /* Register commands that will be cleaned up upon object destruction */
414 #define ADD( name, type, target ) \
415 var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
416 var_AddCallback( p_intf, name, target, NULL );
417 ADD( "quit", VOID, Quit )
418 ADD( "intf", STRING, Intf )
420 ADD( "add", STRING, Playlist )
421 ADD( "repeat", STRING, Playlist )
422 ADD( "loop", STRING, Playlist )
423 ADD( "random", STRING, Playlist )
424 ADD( "enqueue", STRING, Playlist )
425 ADD( "playlist", VOID, Playlist )
426 ADD( "sort", VOID, Playlist )
427 ADD( "play", VOID, Playlist )
428 ADD( "stop", VOID, Playlist )
429 ADD( "clear", VOID, Playlist )
430 ADD( "prev", VOID, Playlist )
431 ADD( "next", VOID, Playlist )
432 ADD( "goto", STRING, Playlist )
433 ADD( "status", STRING, Playlist )
435 /* DVD commands */
436 ADD( "pause", VOID, Input )
437 ADD( "seek", STRING, Input )
438 ADD( "title", STRING, Input )
439 ADD( "title_n", VOID, Input )
440 ADD( "title_p", VOID, Input )
441 ADD( "chapter", STRING, Input )
442 ADD( "chapter_n", VOID, Input )
443 ADD( "chapter_p", VOID, Input )
445 ADD( "fastforward", VOID, Input )
446 ADD( "rewind", VOID, Input )
447 ADD( "faster", VOID, Input )
448 ADD( "slower", VOID, Input )
449 ADD( "normal", VOID, Input )
450 ADD( "frame", VOID, Input )
452 ADD( "atrack", STRING, Input )
453 ADD( "vtrack", STRING, Input )
454 ADD( "strack", STRING, Input )
456 /* video commands */
457 ADD( "vratio", STRING, VideoConfig )
458 ADD( "vcrop", STRING, VideoConfig )
459 ADD( "vzoom", STRING, VideoConfig )
460 ADD( "snapshot", VOID, VideoConfig )
462 /* audio commands */
463 ADD( "volume", STRING, Volume )
464 ADD( "volup", STRING, VolumeMove )
465 ADD( "voldown", STRING, VolumeMove )
466 ADD( "adev", STRING, AudioDevice )
467 ADD( "achan", STRING, AudioChannel )
469 /* misc menu commands */
470 ADD( "stats", VOID, Statistics )
472 #undef ADD
475 /*****************************************************************************
476 * Run: rc thread
477 *****************************************************************************
478 * This part of the interface is in a separate thread so that we can call
479 * exec() from within it without annoying the rest of the program.
480 *****************************************************************************/
481 static void *Run( void *data )
483 intf_thread_t *p_intf = data;
484 intf_sys_t *p_sys = p_intf->p_sys;
486 char p_buffer[ MAX_LINE_LENGTH + 1 ];
487 bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
489 int i_size = 0;
490 int i_oldpos = 0;
491 int i_newpos;
492 int canc = vlc_savecancel( );
494 p_buffer[0] = 0;
496 #if defined(_WIN32) && !VLC_WINSTORE_APP
497 /* Get the file descriptor of the console input */
498 p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
499 if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
501 msg_Err( p_intf, "couldn't find user input handle" );
502 return NULL;
504 #endif
506 /* Register commands that will be cleaned up upon object destruction */
507 RegisterCallbacks( p_intf );
509 /* status callbacks */
511 for( ;; )
513 char *psz_cmd, *psz_arg;
514 bool b_complete;
516 vlc_restorecancel( canc );
518 if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
520 p_sys->i_socket =
521 net_Accept( p_intf, p_sys->pi_socket_listen );
522 if( p_sys->i_socket == -1 ) continue;
525 b_complete = ReadCommand( p_intf, p_buffer, &i_size );
526 canc = vlc_savecancel( );
528 /* Manage the input part */
529 if( p_sys->p_input == NULL )
531 p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
532 /* New input has been registered */
533 if( p_sys->p_input )
535 char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
536 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
537 free( psz_uri );
539 var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
543 int state;
544 if( p_sys->p_input != NULL
545 && ((state = var_GetInteger( p_sys->p_input, "state")) == ERROR_S
546 || state == END_S) )
548 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
549 vlc_object_release( p_sys->p_input );
550 p_sys->p_input = NULL;
552 p_sys->i_last_state = PLAYLIST_STOPPED;
553 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
556 if( p_sys->p_input != NULL )
558 playlist_t *p_playlist = p_sys->p_playlist;
560 PL_LOCK;
561 int status = playlist_Status( p_playlist );
562 PL_UNLOCK;
564 if( p_sys->i_last_state != status )
566 if( status == PLAYLIST_STOPPED )
568 p_sys->i_last_state = PLAYLIST_STOPPED;
569 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
571 else if( status == PLAYLIST_RUNNING )
573 p_sys->i_last_state = PLAYLIST_RUNNING;
574 msg_rc( STATUS_CHANGE "( play state: 3 )" );
576 else if( status == PLAYLIST_PAUSED )
578 p_sys->i_last_state = PLAYLIST_PAUSED;
579 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
584 if( p_sys->p_input && b_showpos )
586 i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
587 if( i_oldpos != i_newpos )
589 i_oldpos = i_newpos;
590 msg_rc( "pos: %d%%", i_newpos );
594 /* Is there something to do? */
595 if( !b_complete ) continue;
597 /* Skip heading spaces */
598 psz_cmd = p_buffer;
599 while( *psz_cmd == ' ' )
601 psz_cmd++;
604 /* Split psz_cmd at the first space and make sure that
605 * psz_arg is valid */
606 psz_arg = strchr( psz_cmd, ' ' );
607 if( psz_arg )
609 *psz_arg++ = 0;
610 while( *psz_arg == ' ' )
612 psz_arg++;
615 else
617 psz_arg = (char*)"";
620 /* If the user typed a registered local command, try it */
621 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
623 int i_ret = VLC_SUCCESS;
625 if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
626 var_TriggerCallback( p_intf, psz_cmd );
627 else
628 i_ret = var_SetString( p_intf, psz_cmd, psz_arg );
629 msg_rc( "%s: returned %i (%s)",
630 psz_cmd, i_ret, vlc_error( i_ret ) );
632 /* Or maybe it's a global command */
633 else if( var_Type( p_intf->obj.libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
635 int i_ret = VLC_SUCCESS;
637 /* FIXME: it's a global command, but we should pass the
638 * local object as an argument, not p_intf->obj.libvlc. */
639 if ((var_Type( p_intf->obj.libvlc, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
640 var_TriggerCallback( p_intf, psz_cmd );
641 else
642 i_ret = var_SetString( p_intf->obj.libvlc, psz_cmd, psz_arg );
643 if( i_ret != 0 )
645 msg_rc( "%s: returned %i (%s)",
646 psz_cmd, i_ret, vlc_error( i_ret ) );
649 else if( !strcmp( psz_cmd, "logout" ) )
651 /* Close connection */
652 if( p_sys->i_socket != -1 )
654 net_Close( p_sys->i_socket );
655 p_sys->i_socket = -1;
658 else if( !strcmp( psz_cmd, "info" ) )
660 if( p_sys->p_input )
662 int i;
663 vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
664 for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
666 info_category_t *p_category = input_GetItem(p_sys->p_input)
667 ->pp_categories[i];
668 info_t *p_info;
670 msg_rc( "+----[ %s ]", p_category->psz_name );
671 msg_rc( "| " );
672 info_foreach(p_info, &p_category->infos)
673 msg_rc( "| %s: %s", p_info->psz_name,
674 p_info->psz_value );
675 msg_rc( "| " );
677 msg_rc( "+----[ end of stream info ]" );
678 vlc_mutex_unlock( &input_GetItem(p_sys->p_input)->lock );
680 else
682 msg_rc( "no input" );
685 else if( !strcmp( psz_cmd, "is_playing" ) )
687 if( p_sys->p_input == NULL )
689 msg_rc( "0" );
691 else
693 msg_rc( "1" );
696 else if( !strcmp( psz_cmd, "get_time" ) )
698 int64_t t = 0;
699 if( p_sys->p_input != NULL )
700 t = var_GetInteger( p_sys->p_input, "time" ) / CLOCK_FREQ;
701 msg_rc( "%"PRIu64, t );
703 else if( !strcmp( psz_cmd, "get_length" ) )
705 int64_t l = 0;
706 if( p_sys->p_input != NULL )
707 l = var_GetInteger( p_sys->p_input, "length" ) / CLOCK_FREQ;
708 msg_rc( "%"PRIu64, l );
710 else if( !strcmp( psz_cmd, "get_title" ) )
712 if( p_sys->p_input == NULL )
714 msg_rc("%s", "");
716 else
718 msg_rc( "%s", input_GetItem(p_sys->p_input)->psz_name );
721 else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
722 || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
724 Help( p_intf );
726 else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
728 var_SetInteger( p_intf->obj.libvlc, "key-action",
729 vlc_actions_get_id( psz_arg ) );
731 else switch( psz_cmd[0] )
733 case 'f':
734 case 'F':
736 bool fs;
738 if( !strncasecmp( psz_arg, "on", 2 ) )
739 var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
740 else if( !strncasecmp( psz_arg, "off", 3 ) )
741 var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
742 else
743 fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
745 if( p_sys->p_input != NULL )
747 vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
748 if( p_vout )
750 var_SetBool( p_vout, "fullscreen", fs );
751 vlc_object_release( p_vout );
754 break;
756 case 's':
757 case 'S':
759 break;
761 case '\0':
762 /* Ignore empty lines */
763 break;
765 default:
766 msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
767 break;
770 /* Command processed */
771 i_size = 0; p_buffer[0] = 0;
774 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
775 msg_rc( STATUS_CHANGE "( quit )" );
777 vlc_restorecancel( canc );
779 return NULL;
782 static void Help( intf_thread_t *p_intf)
784 msg_rc("%s", _("+----[ Remote control commands ]"));
785 msg_rc( "| ");
786 msg_rc("%s", _("| add XYZ . . . . . . . . . . . . add XYZ to playlist"));
787 msg_rc("%s", _("| enqueue XYZ . . . . . . . . . queue XYZ to playlist"));
788 msg_rc("%s", _("| playlist . . . . . show items currently in playlist"));
789 msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
790 msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
791 msg_rc("%s", _("| next . . . . . . . . . . . . . . next playlist item"));
792 msg_rc("%s", _("| prev . . . . . . . . . . . . previous playlist item"));
793 msg_rc("%s", _("| goto . . . . . . . . . . . . . . goto item at index"));
794 msg_rc("%s", _("| repeat [on|off] . . . . toggle playlist item repeat"));
795 msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
796 msg_rc("%s", _("| random [on|off] . . . . . . . toggle random jumping"));
797 msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
798 msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
799 msg_rc("%s", _("| title [X] . . . . . . set/get title in current item"));
800 msg_rc("%s", _("| title_n . . . . . . . . next title in current item"));
801 msg_rc("%s", _("| title_p . . . . . . previous title in current item"));
802 msg_rc("%s", _("| chapter [X] . . . . set/get chapter in current item"));
803 msg_rc("%s", _("| chapter_n . . . . . . next chapter in current item"));
804 msg_rc("%s", _("| chapter_p . . . . previous chapter in current item"));
805 msg_rc( "| ");
806 msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
807 msg_rc("%s", _("| pause . . . . . . . . . . . . . . . . toggle pause"));
808 msg_rc("%s", _("| fastforward . . . . . . . . . set to maximum rate"));
809 msg_rc("%s", _("| rewind . . . . . . . . . . . . set to minimum rate"));
810 msg_rc("%s", _("| faster . . . . . . . . . . faster playing of stream"));
811 msg_rc("%s", _("| slower . . . . . . . . . . slower playing of stream"));
812 msg_rc("%s", _("| normal . . . . . . . . . . normal playing of stream"));
813 msg_rc("%s", _("| frame. . . . . . . . . . play frame by frame"));
814 msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
815 msg_rc("%s", _("| info . . . . . information about the current stream"));
816 msg_rc("%s", _("| stats . . . . . . . . show statistical information"));
817 msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
818 msg_rc("%s", _("| is_playing . . . . 1 if a stream plays, 0 otherwise"));
819 msg_rc("%s", _("| get_title . . . . . the title of the current stream"));
820 msg_rc("%s", _("| get_length . . . . the length of the current stream"));
821 msg_rc( "| ");
822 msg_rc("%s", _("| volume [X] . . . . . . . . . . set/get audio volume"));
823 msg_rc("%s", _("| volup [X] . . . . . . . raise audio volume X steps"));
824 msg_rc("%s", _("| voldown [X] . . . . . . lower audio volume X steps"));
825 msg_rc("%s", _("| adev [device] . . . . . . . . set/get audio device"));
826 msg_rc("%s", _("| achan [X]. . . . . . . . . . set/get audio channels"));
827 msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
828 msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
829 msg_rc("%s", _("| vratio [X] . . . . . . . set/get video aspect ratio"));
830 msg_rc("%s", _("| vcrop [X] . . . . . . . . . . . set/get video crop"));
831 msg_rc("%s", _("| vzoom [X] . . . . . . . . . . . set/get video zoom"));
832 msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
833 msg_rc("%s", _("| strack [X] . . . . . . . . . set/get subtitle track"));
834 msg_rc("%s", _("| key [hotkey name] . . . . . . simulate hotkey press"));
835 msg_rc( "| ");
836 msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
837 msg_rc("%s", _("| logout . . . . . . . exit (if in socket connection)"));
838 msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . . quit vlc"));
839 msg_rc( "| ");
840 msg_rc("%s", _("+----[ end of help ]"));
843 /********************************************************************
844 * Status callback routines
845 ********************************************************************/
846 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
847 vlc_value_t oldval, vlc_value_t newval, void *p_data )
849 (void) p_this;
850 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
851 intf_thread_t *p_intf = (intf_thread_t*)p_data;
853 vlc_mutex_lock( &p_intf->p_sys->status_lock );
854 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
855 lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
856 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
857 return VLC_SUCCESS;
860 static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
862 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
864 PL_LOCK;
865 const int i_status = playlist_Status( p_playlist );
866 PL_UNLOCK;
868 /* */
869 const char *psz_cmd;
870 switch( i_status )
872 case PLAYLIST_STOPPED:
873 psz_cmd = "stop";
874 break;
875 case PLAYLIST_RUNNING:
876 psz_cmd = "play";
877 break;
878 case PLAYLIST_PAUSED:
879 psz_cmd = "pause";
880 break;
881 default:
882 psz_cmd = "";
883 break;
886 /* */
887 const int i_state = var_GetInteger( p_input, "state" );
889 vlc_mutex_lock( &p_intf->p_sys->status_lock );
890 msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
891 i_state, ppsz_input_state[i_state] );
892 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
894 static void RateChanged( intf_thread_t *p_intf,
895 input_thread_t *p_input )
897 vlc_mutex_lock( &p_intf->p_sys->status_lock );
898 msg_rc( STATUS_CHANGE "( new rate: %.3f )",
899 var_GetFloat( p_input, "rate" ) );
900 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
902 static void PositionChanged( intf_thread_t *p_intf,
903 input_thread_t *p_input )
905 vlc_mutex_lock( &p_intf->p_sys->status_lock );
906 if( p_intf->p_sys->b_input_buffering )
907 msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
908 (var_GetInteger( p_input, "time" ) / CLOCK_FREQ) );
909 p_intf->p_sys->b_input_buffering = false;
910 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
912 static void CacheChanged( intf_thread_t *p_intf )
914 vlc_mutex_lock( &p_intf->p_sys->status_lock );
915 p_intf->p_sys->b_input_buffering = true;
916 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
919 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
920 vlc_value_t oldval, vlc_value_t newval, void *p_data )
922 VLC_UNUSED(psz_cmd);
923 VLC_UNUSED(oldval);
924 input_thread_t *p_input = (input_thread_t*)p_this;
925 intf_thread_t *p_intf = p_data;
927 switch( newval.i_int )
929 case INPUT_EVENT_STATE:
930 case INPUT_EVENT_DEAD:
931 StateChanged( p_intf, p_input );
932 break;
933 case INPUT_EVENT_RATE:
934 RateChanged( p_intf, p_input );
935 break;
936 case INPUT_EVENT_POSITION:
937 PositionChanged( p_intf, p_input );
938 break;
939 case INPUT_EVENT_CACHE:
940 CacheChanged( p_intf );
941 break;
942 default:
943 break;
945 return VLC_SUCCESS;
948 /********************************************************************
949 * Command routines
950 ********************************************************************/
951 static int Input( vlc_object_t *p_this, char const *psz_cmd,
952 vlc_value_t oldval, vlc_value_t newval, void *p_data )
954 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
955 intf_thread_t *p_intf = (intf_thread_t*)p_this;
956 input_thread_t *p_input =
957 playlist_CurrentInput( p_intf->p_sys->p_playlist );
958 int i_error = VLC_EGENERIC;
960 if( !p_input )
961 return VLC_ENOOBJ;
963 int state = var_GetInteger( p_input, "state" );
964 if( ( state == PAUSE_S ) &&
965 ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
967 msg_rc( "%s", _("Press pause to continue.") );
969 else
970 /* Parse commands that only require an input */
971 if( !strcmp( psz_cmd, "pause" ) )
973 playlist_TogglePause( p_intf->p_sys->p_playlist );
974 i_error = VLC_SUCCESS;
976 else if( !strcmp( psz_cmd, "seek" ) )
978 if( strlen( newval.psz_string ) > 0 &&
979 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
981 float f = atof( newval.psz_string ) / 100.0;
982 var_SetFloat( p_input, "position", f );
984 else
986 int t = atoi( newval.psz_string );
987 var_SetInteger( p_input, "time", CLOCK_FREQ * t );
989 i_error = VLC_SUCCESS;
991 else if ( !strcmp( psz_cmd, "fastforward" ) )
993 if( var_GetBool( p_input, "can-rate" ) )
995 float f_rate = var_GetFloat( p_input, "rate" );
996 f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
997 var_SetFloat( p_input, "rate", f_rate );
999 else
1001 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
1003 i_error = VLC_SUCCESS;
1005 else if ( !strcmp( psz_cmd, "rewind" ) )
1007 if( var_GetBool( p_input, "can-rewind" ) )
1009 float f_rate = var_GetFloat( p_input, "rate" );
1010 f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
1011 var_SetFloat( p_input, "rate", f_rate );
1013 else
1015 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1017 i_error = VLC_SUCCESS;
1019 else if ( !strcmp( psz_cmd, "faster" ) )
1021 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
1022 i_error = VLC_SUCCESS;
1024 else if ( !strcmp( psz_cmd, "slower" ) )
1026 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
1027 i_error = VLC_SUCCESS;
1029 else if ( !strcmp( psz_cmd, "normal" ) )
1031 var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
1032 i_error = VLC_SUCCESS;
1034 else if ( !strcmp( psz_cmd, "frame" ) )
1036 var_TriggerCallback( p_input, "frame-next" );
1037 i_error = VLC_SUCCESS;
1039 else if( !strcmp( psz_cmd, "chapter" ) ||
1040 !strcmp( psz_cmd, "chapter_n" ) ||
1041 !strcmp( psz_cmd, "chapter_p" ) )
1043 if( !strcmp( psz_cmd, "chapter" ) )
1045 if ( *newval.psz_string )
1047 /* Set. */
1048 var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1050 else
1052 /* Get. */
1053 int i_chap = var_GetInteger( p_input, "chapter" );
1054 int i_chapter_count = var_CountChoices( p_input, "chapter" );
1055 msg_rc( "Currently playing chapter %d/%d.", i_chap,
1056 i_chapter_count );
1059 else if( !strcmp( psz_cmd, "chapter_n" ) )
1060 var_TriggerCallback( p_input, "next-chapter" );
1061 else if( !strcmp( psz_cmd, "chapter_p" ) )
1062 var_TriggerCallback( p_input, "prev-chapter" );
1063 i_error = VLC_SUCCESS;
1065 else if( !strcmp( psz_cmd, "title" ) ||
1066 !strcmp( psz_cmd, "title_n" ) ||
1067 !strcmp( psz_cmd, "title_p" ) )
1069 if( !strcmp( psz_cmd, "title" ) )
1071 if ( *newval.psz_string )
1072 /* Set. */
1073 var_SetInteger( p_input, "title", atoi( newval.psz_string ) );
1074 else
1076 /* Get. */
1077 int i_title = var_GetInteger( p_input, "title" );
1078 int i_title_count = var_CountChoices( p_input, "title" );
1079 msg_rc( "Currently playing title %d/%d.", i_title,
1080 i_title_count );
1083 else if( !strcmp( psz_cmd, "title_n" ) )
1084 var_TriggerCallback( p_input, "next-title" );
1085 else if( !strcmp( psz_cmd, "title_p" ) )
1086 var_TriggerCallback( p_input, "prev-title" );
1088 i_error = VLC_SUCCESS;
1090 else if( !strcmp( psz_cmd, "atrack" )
1091 || !strcmp( psz_cmd, "vtrack" )
1092 || !strcmp( psz_cmd, "strack" ) )
1094 const char *psz_variable;
1095 char *name;
1097 if( !strcmp( psz_cmd, "atrack" ) )
1099 psz_variable = "audio-es";
1101 else if( !strcmp( psz_cmd, "vtrack" ) )
1103 psz_variable = "video-es";
1105 else
1107 psz_variable = "spu-es";
1110 /* Get the descriptive name of the variable */
1111 var_Change( p_input, psz_variable, VLC_VAR_GETTEXT, &name );
1112 if( !name ) name = strdup(psz_variable);
1114 if( newval.psz_string && *newval.psz_string )
1116 /* set */
1117 i_error = var_SetInteger( p_input, psz_variable,
1118 atoi( newval.psz_string ) );
1120 else
1122 /* get */
1123 vlc_value_t *val;
1124 char **text;
1125 size_t count;
1127 int i_value = var_GetInteger( p_input, psz_variable );
1129 if ( var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES,
1130 &count, &val, &text ) < 0 )
1132 free( name );
1133 goto out;
1136 msg_rc( "+----[ %s ]", name );
1137 for ( size_t i = 0; i < count; i++ )
1139 msg_rc( "| %"PRId64" - %s%s", val[i].i_int, text[i],
1140 (i_value == val[i].i_int) ? " *" : "" );
1141 free(text[i]);
1143 free(text);
1144 free(val);
1145 msg_rc( "+----[ end of %s ]", name );
1147 free( name );
1149 out:
1150 vlc_object_release( p_input );
1151 return i_error;
1154 static void print_playlist( intf_thread_t *p_intf, playlist_item_t *p_item, int i_level )
1156 char psz_buffer[MSTRTIME_MAX_SIZE];
1157 for( int i = 0; i< p_item->i_children; i++ )
1159 if( p_item->pp_children[i]->p_input->i_duration != -1 )
1161 secstotimestr( psz_buffer, p_item->pp_children[i]->p_input->i_duration / CLOCK_FREQ );
1162 msg_rc( "|%*s- %s (%s)", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name, psz_buffer );
1164 else
1165 msg_rc( "|%*s- %s", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name );
1167 if( p_item->pp_children[i]->i_children >= 0 )
1168 print_playlist( p_intf, p_item->pp_children[i], i_level + 1 );
1172 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1173 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1175 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1177 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1178 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1179 input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1181 if( p_input )
1183 int state = var_GetInteger( p_input, "state" );
1184 vlc_object_release( p_input );
1186 if( state == PAUSE_S )
1188 msg_rc( "%s", _("Type 'pause' to continue.") );
1189 return VLC_EGENERIC;
1193 /* Parse commands that require a playlist */
1194 if( !strcmp( psz_cmd, "prev" ) )
1196 playlist_Prev( p_playlist );
1198 else if( !strcmp( psz_cmd, "next" ) )
1200 playlist_Next( p_playlist );
1202 else if( !strcmp( psz_cmd, "play" ) )
1204 msg_Warn( p_playlist, "play" );
1205 playlist_Play( p_playlist );
1207 else if( !strcmp( psz_cmd, "repeat" ) )
1209 bool b_update = true;
1210 bool b_value = var_GetBool( p_playlist, "repeat" );
1212 if( strlen( newval.psz_string ) > 0 )
1214 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1215 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1217 b_update = false;
1221 if ( b_update )
1223 b_value = !b_value;
1224 var_SetBool( p_playlist, "repeat", b_value );
1226 msg_rc( "Setting repeat to %s", b_value ? "true" : "false" );
1228 else if( !strcmp( psz_cmd, "loop" ) )
1230 bool b_update = true;
1231 bool b_value = var_GetBool( p_playlist, "loop" );
1233 if( strlen( newval.psz_string ) > 0 )
1235 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1236 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1238 b_update = false;
1242 if ( b_update )
1244 b_value = !b_value;
1245 var_SetBool( p_playlist, "loop", b_value );
1247 msg_rc( "Setting loop to %s", b_value ? "true" : "false" );
1249 else if( !strcmp( psz_cmd, "random" ) )
1251 bool b_update = true;
1252 bool b_value = var_GetBool( p_playlist, "random" );
1254 if( strlen( newval.psz_string ) > 0 )
1256 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1257 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1259 b_update = false;
1263 if ( b_update )
1265 b_value = !b_value;
1266 var_SetBool( p_playlist, "random", b_value );
1268 msg_rc( "Setting random to %s", b_value ? "true" : "false" );
1270 else if (!strcmp( psz_cmd, "goto" ) )
1272 PL_LOCK;
1273 unsigned i_pos = atoi( newval.psz_string );
1274 unsigned i_size = p_playlist->items.i_size;
1276 if( i_pos <= 0 )
1277 msg_rc( "%s", _("Error: `goto' needs an argument greater than zero.") );
1278 else if( i_pos <= i_size )
1280 playlist_item_t *p_item, *p_parent;
1281 p_item = p_parent = p_playlist->items.p_elems[i_pos-1];
1282 while( p_parent->p_parent )
1283 p_parent = p_parent->p_parent;
1284 playlist_ViewPlay( p_playlist, p_parent, p_item );
1286 else
1287 msg_rc( vlc_ngettext("Playlist has only %u element",
1288 "Playlist has only %u elements", i_size),
1289 i_size );
1290 PL_UNLOCK;
1292 else if( !strcmp( psz_cmd, "stop" ) )
1294 playlist_Stop( p_playlist );
1296 else if( !strcmp( psz_cmd, "clear" ) )
1298 playlist_Stop( p_playlist );
1299 playlist_Clear( p_playlist, pl_Unlocked );
1301 else if( !strcmp( psz_cmd, "add" ) &&
1302 newval.psz_string && *newval.psz_string )
1304 input_item_t *p_item = parse_MRL( newval.psz_string );
1306 if( p_item )
1308 msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1309 int i_ret = playlist_AddInput( p_playlist, p_item, true, true );
1310 input_item_Release( p_item );
1311 if( i_ret != VLC_SUCCESS )
1313 return VLC_EGENERIC;
1317 else if( !strcmp( psz_cmd, "enqueue" ) &&
1318 newval.psz_string && *newval.psz_string )
1320 input_item_t *p_item = parse_MRL( newval.psz_string );
1322 if( p_item )
1324 msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1325 int ret = playlist_AddInput( p_playlist, p_item, false, true );
1326 input_item_Release( p_item );
1327 if( ret != VLC_SUCCESS )
1329 return VLC_EGENERIC;
1333 else if( !strcmp( psz_cmd, "playlist" ) )
1335 msg_rc( "+----[ Playlist ]" );
1336 print_playlist( p_intf, &p_playlist->root, 0 );
1337 msg_rc( "+----[ End of playlist ]" );
1340 else if( !strcmp( psz_cmd, "sort" ))
1342 PL_LOCK;
1343 playlist_RecursiveNodeSort( p_playlist, &p_playlist->root,
1344 SORT_ARTIST, ORDER_NORMAL );
1345 PL_UNLOCK;
1347 else if( !strcmp( psz_cmd, "status" ) )
1349 p_input = playlist_CurrentInput( p_playlist );
1350 if( p_input )
1352 /* Replay the current state of the system. */
1353 char *psz_uri =
1354 input_item_GetURI( input_GetItem( p_input ) );
1355 vlc_object_release( p_input );
1356 if( likely(psz_uri != NULL) )
1358 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
1359 free( psz_uri );
1363 float volume = playlist_VolumeGet( p_playlist );
1364 if( volume >= 0.f )
1365 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1366 lroundf(volume * AOUT_VOLUME_DEFAULT) );
1368 int status;
1369 PL_LOCK;
1370 status = playlist_Status(p_playlist);
1371 PL_UNLOCK;
1372 switch( status )
1374 case PLAYLIST_STOPPED:
1375 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
1376 break;
1377 case PLAYLIST_RUNNING:
1378 msg_rc( STATUS_CHANGE "( play state: 3 )" );
1379 break;
1380 case PLAYLIST_PAUSED:
1381 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
1382 break;
1383 default:
1384 msg_rc( STATUS_CHANGE "( unknown state: -1 )" );
1385 break;
1390 * sanity check
1392 else
1394 msg_rc( "unknown command!" );
1397 return VLC_SUCCESS;
1400 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1401 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1403 VLC_UNUSED(p_data); VLC_UNUSED(psz_cmd);
1404 VLC_UNUSED(oldval); VLC_UNUSED(newval);
1406 libvlc_Quit( p_this->obj.libvlc );
1407 return VLC_SUCCESS;
1410 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1411 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1413 intf_thread_t *intf = (intf_thread_t *)p_this;
1415 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1416 return intf_Create(pl_Get(intf), newval.psz_string );
1419 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1420 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1422 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1423 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1424 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1425 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
1426 int i_error = VLC_EGENERIC;
1428 if( !p_input )
1429 return VLC_ENOOBJ;
1431 if( p_input )
1433 int state = var_GetInteger( p_input, "state" );
1434 vlc_object_release( p_input );
1435 if( state == PAUSE_S )
1437 msg_rc( "%s", _("Type 'pause' to continue.") );
1438 return VLC_EGENERIC;
1442 if ( *newval.psz_string )
1444 /* Set. */
1445 int i_volume = atoi( newval.psz_string );
1446 if( !playlist_VolumeSet( p_playlist,
1447 i_volume / (float)AOUT_VOLUME_DEFAULT ) )
1448 i_error = VLC_SUCCESS;
1449 playlist_MuteSet( p_playlist, i_volume == 0 );
1450 msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1452 else
1454 /* Get. */
1455 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1456 lroundf( playlist_VolumeGet( p_playlist ) * AOUT_VOLUME_DEFAULT ) );
1457 i_error = VLC_SUCCESS;
1460 return i_error;
1463 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1464 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1466 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1467 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1468 float volume;
1469 input_thread_t *p_input =
1470 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1471 int i_nb_steps = atoi(newval.psz_string);
1472 int i_error = VLC_SUCCESS;
1474 if( !p_input )
1475 return VLC_ENOOBJ;
1477 int state = var_GetInteger( p_input, "state" );
1478 vlc_object_release( p_input );
1479 if( state == PAUSE_S )
1481 msg_rc( "%s", _("Type 'pause' to continue.") );
1482 return VLC_EGENERIC;
1485 if( !strcmp(psz_cmd, "voldown") )
1486 i_nb_steps *= -1;
1487 if( playlist_VolumeUp( p_intf->p_sys->p_playlist, i_nb_steps, &volume ) < 0 )
1488 i_error = VLC_EGENERIC;
1490 if ( !i_error )
1491 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1492 lroundf( volume * AOUT_VOLUME_DEFAULT ) );
1493 return i_error;
1497 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1498 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1500 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1501 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1502 input_thread_t *p_input =
1503 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1504 vout_thread_t * p_vout;
1505 const char * psz_variable = NULL;
1506 int i_error = VLC_SUCCESS;
1508 if( !p_input )
1509 return VLC_ENOOBJ;
1511 p_vout = input_GetVout( p_input );
1512 vlc_object_release( p_input );
1513 if( !p_vout )
1514 return VLC_ENOOBJ;
1516 if( !strcmp( psz_cmd, "vcrop" ) )
1518 psz_variable = "crop";
1520 else if( !strcmp( psz_cmd, "vratio" ) )
1522 psz_variable = "aspect-ratio";
1524 else if( !strcmp( psz_cmd, "vzoom" ) )
1526 psz_variable = "zoom";
1528 else if( !strcmp( psz_cmd, "snapshot" ) )
1530 psz_variable = "video-snapshot";
1532 else
1533 /* This case can't happen */
1534 vlc_assert_unreachable();
1536 if( newval.psz_string && *newval.psz_string )
1538 /* set */
1539 if( !strcmp( psz_variable, "zoom" ) )
1541 float f_float = atof( newval.psz_string );
1542 i_error = var_SetFloat( p_vout, psz_variable, f_float );
1544 else
1546 i_error = var_SetString( p_vout, psz_variable, newval.psz_string );
1549 else if( !strcmp( psz_cmd, "snapshot" ) )
1551 var_TriggerCallback( p_vout, psz_variable );
1553 else
1555 /* get */
1556 char *name;
1557 vlc_value_t *val;
1558 char **text;
1559 float f_value = 0.;
1560 char *psz_value = NULL;
1561 size_t count;
1563 if( !strcmp( psz_variable, "zoom" ) )
1564 f_value = var_GetFloat( p_vout, "zoom" );
1565 else
1567 psz_value = var_GetString( p_vout, psz_variable );
1568 if( psz_value == NULL )
1570 vlc_object_release( p_vout );
1571 return VLC_EGENERIC;
1575 if ( var_Change( p_vout, psz_variable, VLC_VAR_GETCHOICES,
1576 &count, &val, &text ) < 0 )
1578 vlc_object_release( p_vout );
1579 free( psz_value );
1580 return VLC_EGENERIC;
1583 /* Get the descriptive name of the variable */
1584 var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT, &name );
1585 if( !name ) name = strdup(psz_variable);
1587 msg_rc( "+----[ %s ]", name );
1588 if( !strcmp( psz_variable, "zoom" ) )
1590 for ( size_t i = 0; i < count; i++ )
1592 msg_rc( "| %f - %s%s", val[i].f_float, text[i],
1593 f_value == val[i].f_float ? " *" : "" );
1594 free(text[i]);
1597 else
1599 for ( size_t i = 0; i < count; i++ )
1601 msg_rc( "| %s - %s%s", val[i].psz_string, text[i],
1602 strcmp(psz_value, val[i].psz_string)
1603 ? "" : " *" );
1604 free(text[i]);
1605 free(val[i].psz_string);
1607 free( psz_value );
1609 free(text);
1610 free(val);
1611 msg_rc( "+----[ end of %s ]", name );
1613 free( name );
1615 vlc_object_release( p_vout );
1616 return i_error;
1619 static int AudioDevice( vlc_object_t *obj, char const *cmd,
1620 vlc_value_t old, vlc_value_t cur, void *dummy )
1622 intf_thread_t *p_intf = (intf_thread_t *)obj;
1623 audio_output_t *p_aout = playlist_GetAout( pl_Get(p_intf) );
1624 if( p_aout == NULL )
1625 return VLC_ENOOBJ;
1627 if( !*cur.psz_string )
1629 char **ids, **names;
1630 int n = aout_DevicesList( p_aout, &ids, &names );
1631 if( n < 0 )
1632 goto out;
1634 char *dev = aout_DeviceGet( p_aout );
1635 const char *devstr = (dev != NULL) ? dev : "";
1637 msg_rc( "+----[ %s ]", cmd );
1638 for ( int i = 0; i < n; i++ )
1640 const char *fmt = "| %s - %s";
1642 if( !strcmp(devstr, ids[i]) )
1643 fmt = "| %s - %s *";
1644 msg_rc( fmt, ids[i], names[i] );
1645 free( names[i] );
1646 free( ids[i] );
1648 msg_rc( "+----[ end of %s ]", cmd );
1650 free( dev );
1651 free( names );
1652 free( ids );
1654 else
1655 aout_DeviceSet( p_aout, cur.psz_string );
1656 out:
1657 vlc_object_release( p_aout );
1658 (void) old; (void) dummy;
1659 return VLC_SUCCESS;
1662 static int AudioChannel( vlc_object_t *obj, char const *cmd,
1663 vlc_value_t old, vlc_value_t cur, void *dummy )
1665 intf_thread_t *p_intf = (intf_thread_t*)obj;
1666 vlc_object_t *p_aout = (vlc_object_t *)playlist_GetAout( pl_Get(p_intf) );
1667 if ( p_aout == NULL )
1668 return VLC_ENOOBJ;
1670 int ret = VLC_SUCCESS;
1672 if ( !*cur.psz_string )
1674 /* Retrieve all registered ***. */
1675 vlc_value_t *val;
1676 char **text;
1677 size_t count;
1679 if ( var_Change( p_aout, "stereo-mode", VLC_VAR_GETCHOICES,
1680 &count, &val, &text ) < 0 )
1682 ret = VLC_ENOVAR;
1683 goto out;
1686 int i_value = var_GetInteger( p_aout, "stereo-mode" );
1688 msg_rc( "+----[ %s ]", cmd );
1689 for ( size_t i = 0; i < count; i++ )
1691 msg_rc( "| %"PRId64" - %s%s", val[i].i_int, text[i],
1692 i_value == val[i].i_int ? " *" : "" );
1693 free(text[i]);
1695 free(text);
1696 free(val);
1697 msg_rc( "+----[ end of %s ]", cmd );
1699 else
1700 ret = var_SetInteger( p_aout, "stereo-mode", atoi( cur.psz_string ) );
1701 out:
1702 vlc_object_release( p_aout );
1703 (void) old; (void) dummy;
1704 return ret;
1707 static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
1708 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1710 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1711 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1712 input_thread_t *p_input =
1713 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1715 if( !p_input )
1716 return VLC_ENOOBJ;
1718 updateStatistics( p_intf, input_GetItem(p_input) );
1719 vlc_object_release( p_input );
1720 return VLC_SUCCESS;
1723 static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
1725 if( !p_item ) return VLC_EGENERIC;
1727 vlc_mutex_lock( &p_item->lock );
1728 msg_rc( "+----[ begin of statistical info ]" );
1730 /* Input */
1731 msg_rc("%s", _("+-[Incoming]"));
1732 msg_rc(_("| input bytes read : %8.0f KiB"),
1733 (float)(p_item->p_stats->i_read_bytes)/1024 );
1734 msg_rc(_("| input bitrate : %6.0f kb/s"),
1735 (float)(p_item->p_stats->f_input_bitrate)*8000 );
1736 msg_rc(_("| demux bytes read : %8.0f KiB"),
1737 (float)(p_item->p_stats->i_demux_read_bytes)/1024 );
1738 msg_rc(_("| demux bitrate : %6.0f kb/s"),
1739 (float)(p_item->p_stats->f_demux_bitrate)*8000 );
1740 msg_rc(_("| demux corrupted : %5"PRIi64),
1741 p_item->p_stats->i_demux_corrupted );
1742 msg_rc(_("| discontinuities : %5"PRIi64),
1743 p_item->p_stats->i_demux_discontinuity );
1744 msg_rc("|");
1745 /* Video */
1746 msg_rc("%s", _("+-[Video Decoding]"));
1747 msg_rc(_("| video decoded : %5"PRIi64),
1748 p_item->p_stats->i_decoded_video );
1749 msg_rc(_("| frames displayed : %5"PRIi64),
1750 p_item->p_stats->i_displayed_pictures );
1751 msg_rc(_("| frames lost : %5"PRIi64),
1752 p_item->p_stats->i_lost_pictures );
1753 msg_rc("|");
1754 /* Audio*/
1755 msg_rc("%s", _("+-[Audio Decoding]"));
1756 msg_rc(_("| audio decoded : %5"PRIi64),
1757 p_item->p_stats->i_decoded_audio );
1758 msg_rc(_("| buffers played : %5"PRIi64),
1759 p_item->p_stats->i_played_abuffers );
1760 msg_rc(_("| buffers lost : %5"PRIi64),
1761 p_item->p_stats->i_lost_abuffers );
1762 msg_rc("|");
1763 msg_rc( "+----[ end of statistical info ]" );
1764 vlc_mutex_unlock( &p_item->lock );
1766 return VLC_SUCCESS;
1769 #if defined(_WIN32) && !VLC_WINSTORE_APP
1770 static bool ReadWin32( intf_thread_t *p_intf, unsigned char *p_buffer, int *pi_size )
1772 INPUT_RECORD input_record;
1773 DWORD i_dw;
1775 /* On Win32, select() only works on socket descriptors */
1776 while( WaitForSingleObjectEx( p_intf->p_sys->hConsoleIn,
1777 INTF_IDLE_SLEEP/1000, TRUE ) == WAIT_OBJECT_0 )
1779 // Prefer to fail early when there's not enough space to store a 4 bytes
1780 // UTF8 character. The function will be immediatly called again and we won't
1781 // lose an input
1782 while( *pi_size < MAX_LINE_LENGTH - 4 &&
1783 ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record, 1, &i_dw ) )
1785 if( input_record.EventType != KEY_EVENT ||
1786 !input_record.Event.KeyEvent.bKeyDown ||
1787 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1788 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1789 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1790 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1792 /* nothing interesting */
1793 continue;
1795 if( input_record.Event.KeyEvent.uChar.AsciiChar == '\n' ||
1796 input_record.Event.KeyEvent.uChar.AsciiChar == '\r' )
1798 putc( '\n', stdout );
1799 break;
1801 switch( input_record.Event.KeyEvent.uChar.AsciiChar )
1803 case '\b':
1804 if ( *pi_size == 0 )
1805 break;
1806 if ( *pi_size > 1 && (p_buffer[*pi_size - 1] & 0xC0) == 0x80 )
1808 // pi_size currently points to the character to be written, so
1809 // we need to roll back from 2 bytes to start erasing the previous
1810 // character
1811 (*pi_size) -= 2;
1812 unsigned int nbBytes = 1;
1813 while( *pi_size > 0 && (p_buffer[*pi_size] & 0xC0) == 0x80 )
1815 (*pi_size)--;
1816 nbBytes++;
1818 assert( clz( (unsigned char)~(p_buffer[*pi_size]) ) == nbBytes + 1 );
1819 // The first utf8 byte will be overriden by a \0
1821 else
1822 (*pi_size)--;
1823 p_buffer[*pi_size] = 0;
1825 fputs( "\b \b", stdout );
1826 break;
1827 default:
1829 WCHAR psz_winput[] = { input_record.Event.KeyEvent.uChar.UnicodeChar, L'\0' };
1830 char* psz_input = FromWide( psz_winput );
1831 int input_size = strlen(psz_input);
1832 if ( *pi_size + input_size > MAX_LINE_LENGTH )
1834 p_buffer[ *pi_size ] = 0;
1835 return false;
1837 strcpy( (char*)&p_buffer[*pi_size], psz_input );
1838 utf8_fprintf( stdout, "%s", psz_input );
1839 free(psz_input);
1840 *pi_size += input_size;
1845 p_buffer[ *pi_size ] = 0;
1846 return true;
1849 vlc_testcancel ();
1851 return false;
1853 #endif
1855 bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1857 #if defined(_WIN32) && !VLC_WINSTORE_APP
1858 if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1859 return ReadWin32( p_intf, (unsigned char*)p_buffer, pi_size );
1860 else if( p_intf->p_sys->i_socket == -1 )
1862 vlc_tick_sleep( INTF_IDLE_SLEEP );
1863 return false;
1865 #endif
1867 while( *pi_size < MAX_LINE_LENGTH )
1869 if( p_intf->p_sys->i_socket == -1 )
1871 if( read( 0/*STDIN_FILENO*/, p_buffer + *pi_size, 1 ) <= 0 )
1872 { /* Standard input closed: exit */
1873 libvlc_Quit( p_intf->obj.libvlc );
1874 p_buffer[*pi_size] = 0;
1875 return true;
1878 else
1879 { /* Connection closed */
1880 if( net_Read( p_intf, p_intf->p_sys->i_socket, p_buffer + *pi_size,
1881 1 ) <= 0 )
1883 net_Close( p_intf->p_sys->i_socket );
1884 p_intf->p_sys->i_socket = -1;
1885 p_buffer[*pi_size] = 0;
1886 return true;
1890 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1891 break;
1893 (*pi_size)++;
1896 if( *pi_size == MAX_LINE_LENGTH ||
1897 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1899 p_buffer[ *pi_size ] = 0;
1900 return true;
1903 return false;
1906 /*****************************************************************************
1907 * parse_MRL: build a input item from a full mrl
1908 *****************************************************************************
1909 * MRL format: "simplified-mrl [:option-name[=option-value]]"
1910 * We don't check for '"' or '\'', we just assume that a ':' that follows a
1911 * space is a new option. Should be good enough for our purpose.
1912 *****************************************************************************/
1913 static input_item_t *parse_MRL( const char *mrl )
1915 #define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
1916 #define SKIPTRAILINGSPACE( p, d ) \
1917 { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1919 input_item_t *p_item = NULL;
1920 char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig, *psz_mrl;
1921 char **ppsz_options = NULL;
1922 int i_options = 0;
1924 if( !mrl ) return 0;
1926 psz_mrl = psz_orig = strdup( mrl );
1927 if( !psz_mrl )
1928 return NULL;
1929 while( *psz_mrl )
1931 SKIPSPACE( psz_mrl );
1932 psz_item = psz_mrl;
1934 for( ; *psz_mrl; psz_mrl++ )
1936 if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
1938 /* We have a complete item */
1939 break;
1941 if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
1942 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
1944 /* We have a complete item */
1945 break;
1949 if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
1950 SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
1952 /* Remove '"' and '\'' if necessary */
1953 if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
1954 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1955 if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
1956 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1958 if( !psz_item_mrl )
1960 if( strstr( psz_item, "://" ) != NULL )
1961 psz_item_mrl = strdup( psz_item );
1962 else
1963 psz_item_mrl = vlc_path2uri( psz_item, NULL );
1964 if( psz_item_mrl == NULL )
1966 free( psz_orig );
1967 return NULL;
1970 else if( *psz_item )
1972 i_options++;
1973 ppsz_options = xrealloc( ppsz_options, i_options * sizeof(char *) );
1974 ppsz_options[i_options - 1] = &psz_item[1];
1977 if( *psz_mrl ) SKIPSPACE( psz_mrl );
1980 /* Now create a playlist item */
1981 if( psz_item_mrl )
1983 p_item = input_item_New( psz_item_mrl, NULL );
1984 for( int i = 0; i < i_options; i++ )
1986 input_item_AddOption( p_item, ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
1988 free( psz_item_mrl );
1991 if( i_options ) free( ppsz_options );
1992 free( psz_orig );
1994 return p_item;