demux: ty: fix all warnings
[vlc.git] / modules / control / oldrc.c
blob88a362ec74c9f9d7ab359f6993c823534a366bed
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_legacy.h>
46 #include <vlc_actions.h>
48 #include <sys/types.h>
49 #include <unistd.h>
51 #include <vlc_fs.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], *msg;
145 int len;
147 snprintf (fmt_eol, sizeof (fmt_eol), "%s\r\n", psz_fmt);
148 va_start( args, psz_fmt );
149 len = vasprintf( &msg, fmt_eol, args );
150 va_end( args );
152 if( len < 0 )
153 return;
155 if( p_intf->p_sys->i_socket == -1 )
156 #ifdef _WIN32
157 utf8_fprintf( stdout, "%s", msg );
158 #else
159 vlc_write( 1, msg, len );
160 #endif
161 else
162 net_Write( p_intf, p_intf->p_sys->i_socket, msg, len );
164 free( msg );
166 #define msg_rc( ... ) msg_rc( p_intf, __VA_ARGS__ )
168 /*****************************************************************************
169 * Module descriptor
170 *****************************************************************************/
171 #define POS_TEXT N_("Show stream position")
172 #define POS_LONGTEXT N_("Show the current position in seconds within the " \
173 "stream from time to time." )
175 #define TTY_TEXT N_("Fake TTY")
176 #define TTY_LONGTEXT N_("Force the rc module to use stdin as if it was a TTY.")
178 #define UNIX_TEXT N_("UNIX socket command input")
179 #define UNIX_LONGTEXT N_("Accept commands over a Unix socket rather than " \
180 "stdin." )
182 #define HOST_TEXT N_("TCP command input")
183 #define HOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
184 "You can set the address and port the interface will bind to." )
186 #ifdef _WIN32
187 #define QUIET_TEXT N_("Do not open a DOS command box interface")
188 #define QUIET_LONGTEXT N_( \
189 "By default the rc interface plugin will start a DOS command box. " \
190 "Enabling the quiet mode will not bring this command box but can also " \
191 "be pretty annoying when you want to stop VLC and no video window is " \
192 "open." )
193 #if !VLC_WINSTORE_APP
194 #include "intromsg.h"
195 #endif
196 #endif
198 vlc_module_begin ()
199 set_shortname( N_("RC"))
200 set_category( CAT_INTERFACE )
201 set_subcategory( SUBCAT_INTERFACE_MAIN )
202 set_description( N_("Remote control interface") )
203 add_bool( "rc-show-pos", false, POS_TEXT, POS_LONGTEXT, true )
205 #ifdef _WIN32
206 add_bool( "rc-quiet", false, QUIET_TEXT, QUIET_LONGTEXT, false )
207 #else
208 #if defined (HAVE_ISATTY)
209 add_bool( "rc-fake-tty", false, TTY_TEXT, TTY_LONGTEXT, true )
210 #endif
211 add_string( "rc-unix", NULL, UNIX_TEXT, UNIX_LONGTEXT, true )
212 #endif
213 add_string( "rc-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
215 set_capability( "interface", 20 )
217 set_callbacks( Activate, Deactivate )
218 #ifdef _WIN32
219 add_shortcut( "rc" )
220 #endif
221 vlc_module_end ()
223 /*****************************************************************************
224 * Activate: initialize and create stuff
225 *****************************************************************************/
226 static int Activate( vlc_object_t *p_this )
228 /* FIXME: This function is full of memory leaks and bugs in error paths. */
229 intf_thread_t *p_intf = (intf_thread_t*)p_this;
230 playlist_t *p_playlist = pl_Get( p_intf );
231 char *psz_host, *psz_unix_path = NULL;
232 int *pi_socket = NULL;
234 #ifndef _WIN32
235 #if defined(HAVE_ISATTY)
236 /* Check that stdin is a TTY */
237 if( !var_InheritBool( p_intf, "rc-fake-tty" ) && !isatty( 0 ) )
239 msg_Warn( p_intf, "fd 0 is not a TTY" );
240 return VLC_EGENERIC;
242 #endif
244 psz_unix_path = var_InheritString( p_intf, "rc-unix" );
245 if( psz_unix_path )
247 int i_socket;
249 #ifndef AF_LOCAL
250 msg_Warn( p_intf, "your OS doesn't support filesystem sockets" );
251 free( psz_unix_path );
252 return VLC_EGENERIC;
253 #else
254 struct sockaddr_un addr;
256 memset( &addr, 0, sizeof(struct sockaddr_un) );
258 msg_Dbg( p_intf, "trying UNIX socket" );
260 if( (i_socket = vlc_socket( PF_LOCAL, SOCK_STREAM, 0, false ) ) < 0 )
262 msg_Warn( p_intf, "can't open socket: %s", vlc_strerror_c(errno) );
263 free( psz_unix_path );
264 return VLC_EGENERIC;
267 addr.sun_family = AF_LOCAL;
268 strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) );
269 addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0';
271 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr))
272 && (errno == EADDRINUSE)
273 && connect (i_socket, (struct sockaddr *)&addr, sizeof (addr))
274 && (errno == ECONNREFUSED))
276 msg_Info (p_intf, "Removing dead UNIX socket: %s", psz_unix_path);
277 unlink (psz_unix_path);
279 if (bind (i_socket, (struct sockaddr *)&addr, sizeof (addr)))
281 msg_Err (p_intf, "cannot bind UNIX socket at %s: %s",
282 psz_unix_path, vlc_strerror_c(errno));
283 free (psz_unix_path);
284 net_Close (i_socket);
285 return VLC_EGENERIC;
289 if( listen( i_socket, 1 ) )
291 msg_Warn (p_intf, "can't listen on socket: %s",
292 vlc_strerror_c(errno));
293 free( psz_unix_path );
294 net_Close( i_socket );
295 return VLC_EGENERIC;
298 /* FIXME: we need a core function to merge listening sockets sets */
299 pi_socket = calloc( 2, sizeof( int ) );
300 if( pi_socket == NULL )
302 free( psz_unix_path );
303 net_Close( i_socket );
304 return VLC_ENOMEM;
306 pi_socket[0] = i_socket;
307 pi_socket[1] = -1;
308 #endif /* AF_LOCAL */
310 #endif /* !_WIN32 */
312 if( ( pi_socket == NULL ) &&
313 ( psz_host = var_InheritString( p_intf, "rc-host" ) ) != NULL )
315 vlc_url_t url;
317 vlc_UrlParse( &url, psz_host );
318 if( url.psz_host == NULL )
320 vlc_UrlClean( &url );
321 char *psz_backward_compat_host;
322 if( asprintf( &psz_backward_compat_host, "//%s", psz_host ) < 0 )
324 free( psz_host );
325 return VLC_EGENERIC;
327 free( psz_host );
328 psz_host = psz_backward_compat_host;
329 vlc_UrlParse( &url, psz_host );
332 msg_Dbg( p_intf, "base: %s, port: %d", url.psz_host, url.i_port );
334 pi_socket = net_ListenTCP(p_this, url.psz_host, url.i_port);
335 if( pi_socket == NULL )
337 msg_Warn( p_intf, "can't listen to %s port %i",
338 url.psz_host, url.i_port );
339 vlc_UrlClean( &url );
340 free( psz_host );
341 return VLC_EGENERIC;
344 vlc_UrlClean( &url );
345 free( psz_host );
348 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
349 if( unlikely(p_sys == NULL) )
351 net_ListenClose( pi_socket );
352 free( psz_unix_path );
353 return VLC_ENOMEM;
356 p_intf->p_sys = p_sys;
357 p_sys->pi_socket_listen = pi_socket;
358 p_sys->i_socket = -1;
359 p_sys->psz_unix_path = psz_unix_path;
360 vlc_mutex_init( &p_sys->status_lock );
361 p_sys->i_last_state = PLAYLIST_STOPPED;
362 p_sys->b_input_buffering = false;
363 p_sys->p_playlist = p_playlist;
364 p_sys->p_input = NULL;
366 /* Non-buffered stdout */
367 setvbuf( stdout, (char *)NULL, _IOLBF, 0 );
369 #if VLC_WINSTORE_APP
370 p_sys->b_quiet = true;
371 #elif defined(_WIN32)
372 p_sys->b_quiet = var_InheritBool( p_intf, "rc-quiet" );
373 if( !p_sys->b_quiet )
374 intf_consoleIntroMsg( p_intf );
375 #endif
377 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
378 goto error;
380 msg_rc( "%s", _("Remote control interface initialized. Type `help' for help.") );
382 /* Listen to audio volume updates */
383 var_AddCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
384 return VLC_SUCCESS;
386 error:
387 net_ListenClose( pi_socket );
388 free( psz_unix_path );
389 vlc_mutex_destroy( &p_sys->status_lock );
390 free( p_sys );
391 return VLC_EGENERIC;
394 /*****************************************************************************
395 * Deactivate: uninitialize and cleanup
396 *****************************************************************************/
397 static void Deactivate( vlc_object_t *p_this )
399 intf_thread_t *p_intf = (intf_thread_t*)p_this;
400 intf_sys_t *p_sys = p_intf->p_sys;
402 vlc_cancel( p_sys->thread );
403 var_DelCallback( p_sys->p_playlist, "volume", VolumeChanged, p_intf );
404 vlc_join( p_sys->thread, NULL );
406 if( p_sys->p_input != NULL )
408 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
409 vlc_object_release( p_sys->p_input );
412 net_ListenClose( p_sys->pi_socket_listen );
413 if( p_sys->i_socket != -1 )
414 net_Close( p_sys->i_socket );
415 if( p_sys->psz_unix_path != NULL )
417 #if defined(AF_LOCAL) && !defined(_WIN32)
418 unlink( p_sys->psz_unix_path );
419 #endif
420 free( p_sys->psz_unix_path );
422 vlc_mutex_destroy( &p_sys->status_lock );
423 free( p_sys );
426 /*****************************************************************************
427 * RegisterCallbacks: Register callbacks to dynamic variables
428 *****************************************************************************/
429 static void RegisterCallbacks( intf_thread_t *p_intf )
431 /* Register commands that will be cleaned up upon object destruction */
432 #define ADD( name, type, target ) \
433 var_Create( p_intf, name, VLC_VAR_ ## type | VLC_VAR_ISCOMMAND ); \
434 var_AddCallback( p_intf, name, target, NULL );
435 ADD( "quit", VOID, Quit )
436 ADD( "intf", STRING, Intf )
438 ADD( "add", STRING, Playlist )
439 ADD( "repeat", STRING, Playlist )
440 ADD( "loop", STRING, Playlist )
441 ADD( "random", STRING, Playlist )
442 ADD( "enqueue", STRING, Playlist )
443 ADD( "playlist", VOID, Playlist )
444 ADD( "sort", VOID, Playlist )
445 ADD( "play", VOID, Playlist )
446 ADD( "stop", VOID, Playlist )
447 ADD( "clear", VOID, Playlist )
448 ADD( "prev", VOID, Playlist )
449 ADD( "next", VOID, Playlist )
450 ADD( "goto", STRING, Playlist )
451 ADD( "status", STRING, Playlist )
453 /* DVD commands */
454 ADD( "pause", VOID, Input )
455 ADD( "seek", STRING, Input )
456 ADD( "title", STRING, Input )
457 ADD( "title_n", VOID, Input )
458 ADD( "title_p", VOID, Input )
459 ADD( "chapter", STRING, Input )
460 ADD( "chapter_n", VOID, Input )
461 ADD( "chapter_p", VOID, Input )
463 ADD( "fastforward", VOID, Input )
464 ADD( "rewind", VOID, Input )
465 ADD( "faster", VOID, Input )
466 ADD( "slower", VOID, Input )
467 ADD( "normal", VOID, Input )
468 ADD( "frame", VOID, Input )
470 ADD( "atrack", STRING, Input )
471 ADD( "vtrack", STRING, Input )
472 ADD( "strack", STRING, Input )
474 /* video commands */
475 ADD( "vratio", STRING, VideoConfig )
476 ADD( "vcrop", STRING, VideoConfig )
477 ADD( "vzoom", STRING, VideoConfig )
478 ADD( "snapshot", VOID, VideoConfig )
480 /* audio commands */
481 ADD( "volume", STRING, Volume )
482 ADD( "volup", STRING, VolumeMove )
483 ADD( "voldown", STRING, VolumeMove )
484 ADD( "adev", STRING, AudioDevice )
485 ADD( "achan", STRING, AudioChannel )
487 /* misc menu commands */
488 ADD( "stats", VOID, Statistics )
490 #undef ADD
493 /*****************************************************************************
494 * Run: rc thread
495 *****************************************************************************
496 * This part of the interface is in a separate thread so that we can call
497 * exec() from within it without annoying the rest of the program.
498 *****************************************************************************/
499 static void *Run( void *data )
501 intf_thread_t *p_intf = data;
502 intf_sys_t *p_sys = p_intf->p_sys;
504 char p_buffer[ MAX_LINE_LENGTH + 1 ];
505 bool b_showpos = var_InheritBool( p_intf, "rc-show-pos" );
507 int i_size = 0;
508 int i_oldpos = 0;
509 int i_newpos;
510 int canc = vlc_savecancel( );
512 p_buffer[0] = 0;
514 #if defined(_WIN32) && !VLC_WINSTORE_APP
515 /* Get the file descriptor of the console input */
516 p_intf->p_sys->hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
517 if( p_intf->p_sys->hConsoleIn == INVALID_HANDLE_VALUE )
519 msg_Err( p_intf, "couldn't find user input handle" );
520 return NULL;
522 #endif
524 /* Register commands that will be cleaned up upon object destruction */
525 RegisterCallbacks( p_intf );
527 /* status callbacks */
529 for( ;; )
531 char *psz_cmd, *psz_arg;
532 bool b_complete;
534 vlc_restorecancel( canc );
536 if( p_sys->pi_socket_listen != NULL && p_sys->i_socket == -1 )
538 p_sys->i_socket =
539 net_Accept( p_intf, p_sys->pi_socket_listen );
540 if( p_sys->i_socket == -1 ) continue;
543 b_complete = ReadCommand( p_intf, p_buffer, &i_size );
544 canc = vlc_savecancel( );
546 /* Manage the input part */
547 if( p_sys->p_input == NULL )
549 p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
550 /* New input has been registered */
551 if( p_sys->p_input )
553 char *psz_uri = input_item_GetURI( input_GetItem( p_sys->p_input ) );
554 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
555 free( psz_uri );
557 var_AddCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
561 int state;
562 if( p_sys->p_input != NULL
563 && ((state = var_GetInteger( p_sys->p_input, "state")) == ERROR_S
564 || state == END_S) )
566 var_DelCallback( p_sys->p_input, "intf-event", InputEvent, p_intf );
567 vlc_object_release( p_sys->p_input );
568 p_sys->p_input = NULL;
570 p_sys->i_last_state = PLAYLIST_STOPPED;
571 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
574 if( p_sys->p_input != NULL )
576 playlist_t *p_playlist = p_sys->p_playlist;
578 PL_LOCK;
579 int status = playlist_Status( p_playlist );
580 PL_UNLOCK;
582 if( p_sys->i_last_state != status )
584 if( status == PLAYLIST_STOPPED )
586 p_sys->i_last_state = PLAYLIST_STOPPED;
587 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
589 else if( status == PLAYLIST_RUNNING )
591 p_sys->i_last_state = PLAYLIST_RUNNING;
592 msg_rc( STATUS_CHANGE "( play state: 3 )" );
594 else if( status == PLAYLIST_PAUSED )
596 p_sys->i_last_state = PLAYLIST_PAUSED;
597 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
602 if( p_sys->p_input && b_showpos )
604 i_newpos = 100 * var_GetFloat( p_sys->p_input, "position" );
605 if( i_oldpos != i_newpos )
607 i_oldpos = i_newpos;
608 msg_rc( "pos: %d%%", i_newpos );
612 /* Is there something to do? */
613 if( !b_complete ) continue;
615 /* Skip heading spaces */
616 psz_cmd = p_buffer;
617 while( *psz_cmd == ' ' )
619 psz_cmd++;
622 /* Split psz_cmd at the first space and make sure that
623 * psz_arg is valid */
624 psz_arg = strchr( psz_cmd, ' ' );
625 if( psz_arg )
627 *psz_arg++ = 0;
628 while( *psz_arg == ' ' )
630 psz_arg++;
633 else
635 psz_arg = (char*)"";
638 /* If the user typed a registered local command, try it */
639 if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
641 int i_ret = VLC_SUCCESS;
643 if ((var_Type( p_intf, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
644 var_TriggerCallback( p_intf, psz_cmd );
645 else
646 i_ret = var_SetString( p_intf, psz_cmd, psz_arg );
647 msg_rc( "%s: returned %i (%s)",
648 psz_cmd, i_ret, vlc_error( i_ret ) );
650 /* Or maybe it's a global command */
651 else if( var_Type( p_intf->obj.libvlc, psz_cmd ) & VLC_VAR_ISCOMMAND )
653 int i_ret = VLC_SUCCESS;
655 /* FIXME: it's a global command, but we should pass the
656 * local object as an argument, not p_intf->obj.libvlc. */
657 if ((var_Type( p_intf->obj.libvlc, psz_cmd) & VLC_VAR_CLASS) == VLC_VAR_VOID)
658 var_TriggerCallback( p_intf, psz_cmd );
659 else
660 i_ret = var_SetString( p_intf->obj.libvlc, psz_cmd, psz_arg );
661 if( i_ret != 0 )
663 msg_rc( "%s: returned %i (%s)",
664 psz_cmd, i_ret, vlc_error( i_ret ) );
667 else if( !strcmp( psz_cmd, "logout" ) )
669 /* Close connection */
670 if( p_sys->i_socket != -1 )
672 net_Close( p_sys->i_socket );
673 p_sys->i_socket = -1;
676 else if( !strcmp( psz_cmd, "info" ) )
678 if( p_sys->p_input )
680 int i;
681 vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
682 for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
684 info_category_t *p_category = input_GetItem(p_sys->p_input)
685 ->pp_categories[i];
686 info_t *p_info;
688 msg_rc( "+----[ %s ]", p_category->psz_name );
689 msg_rc( "| " );
690 info_foreach(p_info, &p_category->infos)
691 msg_rc( "| %s: %s", p_info->psz_name,
692 p_info->psz_value );
693 msg_rc( "| " );
695 msg_rc( "+----[ end of stream info ]" );
696 vlc_mutex_unlock( &input_GetItem(p_sys->p_input)->lock );
698 else
700 msg_rc( "no input" );
703 else if( !strcmp( psz_cmd, "is_playing" ) )
705 if( p_sys->p_input == NULL )
707 msg_rc( "0" );
709 else
711 msg_rc( "1" );
714 else if( !strcmp( psz_cmd, "get_time" ) )
716 int64_t t = 0;
717 if( p_sys->p_input != NULL )
718 t = SEC_FROM_VLC_TICK(var_GetInteger( p_sys->p_input, "time" ));
719 msg_rc( "%"PRIu64, t );
721 else if( !strcmp( psz_cmd, "get_length" ) )
723 int64_t l = 0;
724 if( p_sys->p_input != NULL )
725 l = SEC_FROM_VLC_TICK(var_GetInteger( p_sys->p_input, "length" ));
726 msg_rc( "%"PRIu64, l );
728 else if( !strcmp( psz_cmd, "get_title" ) )
730 if( p_sys->p_input == NULL )
732 msg_rc("%s", "");
734 else
736 msg_rc( "%s", input_GetItem(p_sys->p_input)->psz_name );
739 else if( !strcmp( psz_cmd, "longhelp" ) || !strncmp( psz_cmd, "h", 1 )
740 || !strncmp( psz_cmd, "H", 1 ) || !strncmp( psz_cmd, "?", 1 ) )
742 Help( p_intf );
744 else if( !strcmp( psz_cmd, "key" ) || !strcmp( psz_cmd, "hotkey" ) )
746 var_SetInteger( p_intf->obj.libvlc, "key-action",
747 vlc_actions_get_id( psz_arg ) );
749 else switch( psz_cmd[0] )
751 case 'f':
752 case 'F':
754 bool fs;
756 if( !strncasecmp( psz_arg, "on", 2 ) )
757 var_SetBool( p_sys->p_playlist, "fullscreen", fs = true );
758 else if( !strncasecmp( psz_arg, "off", 3 ) )
759 var_SetBool( p_sys->p_playlist, "fullscreen", fs = false );
760 else
761 fs = var_ToggleBool( p_sys->p_playlist, "fullscreen" );
763 if( p_sys->p_input != NULL )
765 vout_thread_t *p_vout = input_GetVout( p_sys->p_input );
766 if( p_vout )
768 var_SetBool( p_vout, "fullscreen", fs );
769 vlc_object_release( p_vout );
772 break;
774 case 's':
775 case 'S':
777 break;
779 case '\0':
780 /* Ignore empty lines */
781 break;
783 default:
784 msg_rc(_("Unknown command `%s'. Type `help' for help."), psz_cmd);
785 break;
788 /* Command processed */
789 i_size = 0; p_buffer[0] = 0;
792 msg_rc( STATUS_CHANGE "( stop state: 0 )" );
793 msg_rc( STATUS_CHANGE "( quit )" );
795 vlc_restorecancel( canc );
797 return NULL;
800 static void Help( intf_thread_t *p_intf)
802 msg_rc("%s", _("+----[ Remote control commands ]"));
803 msg_rc( "| ");
804 msg_rc("%s", _("| add XYZ . . . . . . . . . . . . add XYZ to playlist"));
805 msg_rc("%s", _("| enqueue XYZ . . . . . . . . . queue XYZ to playlist"));
806 msg_rc("%s", _("| playlist . . . . . show items currently in playlist"));
807 msg_rc("%s", _("| play . . . . . . . . . . . . . . . . . . play stream"));
808 msg_rc("%s", _("| stop . . . . . . . . . . . . . . . . . . stop stream"));
809 msg_rc("%s", _("| next . . . . . . . . . . . . . . next playlist item"));
810 msg_rc("%s", _("| prev . . . . . . . . . . . . previous playlist item"));
811 msg_rc("%s", _("| goto . . . . . . . . . . . . . . goto item at index"));
812 msg_rc("%s", _("| repeat [on|off] . . . . toggle playlist item repeat"));
813 msg_rc("%s", _("| loop [on|off] . . . . . . . . . toggle playlist loop"));
814 msg_rc("%s", _("| random [on|off] . . . . . . . toggle random jumping"));
815 msg_rc("%s", _("| clear . . . . . . . . . . . . . . clear the playlist"));
816 msg_rc("%s", _("| status . . . . . . . . . . . current playlist status"));
817 msg_rc("%s", _("| title [X] . . . . . . set/get title in current item"));
818 msg_rc("%s", _("| title_n . . . . . . . . next title in current item"));
819 msg_rc("%s", _("| title_p . . . . . . previous title in current item"));
820 msg_rc("%s", _("| chapter [X] . . . . set/get chapter in current item"));
821 msg_rc("%s", _("| chapter_n . . . . . . next chapter in current item"));
822 msg_rc("%s", _("| chapter_p . . . . previous chapter in current item"));
823 msg_rc( "| ");
824 msg_rc("%s", _("| seek X . . . seek in seconds, for instance `seek 12'"));
825 msg_rc("%s", _("| pause . . . . . . . . . . . . . . . . toggle pause"));
826 msg_rc("%s", _("| fastforward . . . . . . . . . set to maximum rate"));
827 msg_rc("%s", _("| rewind . . . . . . . . . . . . set to minimum rate"));
828 msg_rc("%s", _("| faster . . . . . . . . . . faster playing of stream"));
829 msg_rc("%s", _("| slower . . . . . . . . . . slower playing of stream"));
830 msg_rc("%s", _("| normal . . . . . . . . . . normal playing of stream"));
831 msg_rc("%s", _("| frame. . . . . . . . . . play frame by frame"));
832 msg_rc("%s", _("| f [on|off] . . . . . . . . . . . . toggle fullscreen"));
833 msg_rc("%s", _("| info . . . . . information about the current stream"));
834 msg_rc("%s", _("| stats . . . . . . . . show statistical information"));
835 msg_rc("%s", _("| get_time . . seconds elapsed since stream's beginning"));
836 msg_rc("%s", _("| is_playing . . . . 1 if a stream plays, 0 otherwise"));
837 msg_rc("%s", _("| get_title . . . . . the title of the current stream"));
838 msg_rc("%s", _("| get_length . . . . the length of the current stream"));
839 msg_rc( "| ");
840 msg_rc("%s", _("| volume [X] . . . . . . . . . . set/get audio volume"));
841 msg_rc("%s", _("| volup [X] . . . . . . . raise audio volume X steps"));
842 msg_rc("%s", _("| voldown [X] . . . . . . lower audio volume X steps"));
843 msg_rc("%s", _("| adev [device] . . . . . . . . set/get audio device"));
844 msg_rc("%s", _("| achan [X]. . . . . . . . . . set/get audio channels"));
845 msg_rc("%s", _("| atrack [X] . . . . . . . . . . . set/get audio track"));
846 msg_rc("%s", _("| vtrack [X] . . . . . . . . . . . set/get video track"));
847 msg_rc("%s", _("| vratio [X] . . . . . . . set/get video aspect ratio"));
848 msg_rc("%s", _("| vcrop [X] . . . . . . . . . . . set/get video crop"));
849 msg_rc("%s", _("| vzoom [X] . . . . . . . . . . . set/get video zoom"));
850 msg_rc("%s", _("| snapshot . . . . . . . . . . . . take video snapshot"));
851 msg_rc("%s", _("| strack [X] . . . . . . . . . set/get subtitle track"));
852 msg_rc("%s", _("| key [hotkey name] . . . . . . simulate hotkey press"));
853 msg_rc( "| ");
854 msg_rc("%s", _("| help . . . . . . . . . . . . . . . this help message"));
855 msg_rc("%s", _("| logout . . . . . . . exit (if in socket connection)"));
856 msg_rc("%s", _("| quit . . . . . . . . . . . . . . . . . . . quit vlc"));
857 msg_rc( "| ");
858 msg_rc("%s", _("+----[ end of help ]"));
861 /********************************************************************
862 * Status callback routines
863 ********************************************************************/
864 static int VolumeChanged( vlc_object_t *p_this, char const *psz_cmd,
865 vlc_value_t oldval, vlc_value_t newval, void *p_data )
867 (void) p_this;
868 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval);
869 intf_thread_t *p_intf = (intf_thread_t*)p_data;
871 vlc_mutex_lock( &p_intf->p_sys->status_lock );
872 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
873 lroundf(newval.f_float * AOUT_VOLUME_DEFAULT) );
874 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
875 return VLC_SUCCESS;
878 static void StateChanged( intf_thread_t *p_intf, input_thread_t *p_input )
880 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
882 PL_LOCK;
883 const int i_status = playlist_Status( p_playlist );
884 PL_UNLOCK;
886 /* */
887 const char *psz_cmd;
888 switch( i_status )
890 case PLAYLIST_STOPPED:
891 psz_cmd = "stop";
892 break;
893 case PLAYLIST_RUNNING:
894 psz_cmd = "play";
895 break;
896 case PLAYLIST_PAUSED:
897 psz_cmd = "pause";
898 break;
899 default:
900 psz_cmd = "";
901 break;
904 /* */
905 const int i_state = var_GetInteger( p_input, "state" );
907 vlc_mutex_lock( &p_intf->p_sys->status_lock );
908 msg_rc( STATUS_CHANGE "( %s state: %d ): %s", psz_cmd,
909 i_state, ppsz_input_state[i_state] );
910 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
912 static void RateChanged( intf_thread_t *p_intf,
913 input_thread_t *p_input )
915 vlc_mutex_lock( &p_intf->p_sys->status_lock );
916 msg_rc( STATUS_CHANGE "( new rate: %.3f )",
917 var_GetFloat( p_input, "rate" ) );
918 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
920 static void PositionChanged( intf_thread_t *p_intf,
921 input_thread_t *p_input )
923 vlc_mutex_lock( &p_intf->p_sys->status_lock );
924 if( p_intf->p_sys->b_input_buffering )
925 msg_rc( STATUS_CHANGE "( time: %"PRId64"s )",
926 SEC_FROM_VLC_TICK(var_GetInteger( p_input, "time" )) );
927 p_intf->p_sys->b_input_buffering = false;
928 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
930 static void CacheChanged( intf_thread_t *p_intf )
932 vlc_mutex_lock( &p_intf->p_sys->status_lock );
933 p_intf->p_sys->b_input_buffering = true;
934 vlc_mutex_unlock( &p_intf->p_sys->status_lock );
937 static int InputEvent( vlc_object_t *p_this, char const *psz_cmd,
938 vlc_value_t oldval, vlc_value_t newval, void *p_data )
940 VLC_UNUSED(psz_cmd);
941 VLC_UNUSED(oldval);
942 input_thread_t *p_input = (input_thread_t*)p_this;
943 intf_thread_t *p_intf = p_data;
945 switch( newval.i_int )
947 case INPUT_EVENT_STATE:
948 case INPUT_EVENT_DEAD:
949 StateChanged( p_intf, p_input );
950 break;
951 case INPUT_EVENT_RATE:
952 RateChanged( p_intf, p_input );
953 break;
954 case INPUT_EVENT_POSITION:
955 PositionChanged( p_intf, p_input );
956 break;
957 case INPUT_EVENT_CACHE:
958 CacheChanged( p_intf );
959 break;
960 default:
961 break;
963 return VLC_SUCCESS;
966 /********************************************************************
967 * Command routines
968 ********************************************************************/
969 static int Input( vlc_object_t *p_this, char const *psz_cmd,
970 vlc_value_t oldval, vlc_value_t newval, void *p_data )
972 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
973 intf_thread_t *p_intf = (intf_thread_t*)p_this;
974 input_thread_t *p_input =
975 playlist_CurrentInput( p_intf->p_sys->p_playlist );
976 int i_error = VLC_EGENERIC;
978 if( !p_input )
979 return VLC_ENOOBJ;
981 int state = var_GetInteger( p_input, "state" );
982 if( ( state == PAUSE_S ) &&
983 ( strcmp( psz_cmd, "pause" ) != 0 ) && (strcmp( psz_cmd,"frame") != 0 ) )
985 msg_rc( "%s", _("Press pause to continue.") );
987 else
988 /* Parse commands that only require an input */
989 if( !strcmp( psz_cmd, "pause" ) )
991 playlist_TogglePause( p_intf->p_sys->p_playlist );
992 i_error = VLC_SUCCESS;
994 else if( !strcmp( psz_cmd, "seek" ) )
996 if( strlen( newval.psz_string ) > 0 &&
997 newval.psz_string[strlen( newval.psz_string ) - 1] == '%' )
999 float f = atof( newval.psz_string ) / 100.0;
1000 var_SetFloat( p_input, "position", f );
1002 else
1004 int t = atoi( newval.psz_string );
1005 var_SetInteger( p_input, "time", vlc_tick_from_sec( t ) );
1007 i_error = VLC_SUCCESS;
1009 else if ( !strcmp( psz_cmd, "fastforward" ) )
1011 if( var_GetBool( p_input, "can-rate" ) )
1013 float f_rate = var_GetFloat( p_input, "rate" );
1014 f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
1015 var_SetFloat( p_input, "rate", f_rate );
1017 else
1019 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
1021 i_error = VLC_SUCCESS;
1023 else if ( !strcmp( psz_cmd, "rewind" ) )
1025 if( var_GetBool( p_input, "can-rewind" ) )
1027 float f_rate = var_GetFloat( p_input, "rate" );
1028 f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
1029 var_SetFloat( p_input, "rate", f_rate );
1031 else
1033 var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
1035 i_error = VLC_SUCCESS;
1037 else if ( !strcmp( psz_cmd, "faster" ) )
1039 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-faster" );
1040 i_error = VLC_SUCCESS;
1042 else if ( !strcmp( psz_cmd, "slower" ) )
1044 var_TriggerCallback( p_intf->p_sys->p_playlist, "rate-slower" );
1045 i_error = VLC_SUCCESS;
1047 else if ( !strcmp( psz_cmd, "normal" ) )
1049 var_SetFloat( p_intf->p_sys->p_playlist, "rate", 1. );
1050 i_error = VLC_SUCCESS;
1052 else if ( !strcmp( psz_cmd, "frame" ) )
1054 var_TriggerCallback( p_input, "frame-next" );
1055 i_error = VLC_SUCCESS;
1057 else if( !strcmp( psz_cmd, "chapter" ) ||
1058 !strcmp( psz_cmd, "chapter_n" ) ||
1059 !strcmp( psz_cmd, "chapter_p" ) )
1061 if( !strcmp( psz_cmd, "chapter" ) )
1063 if ( *newval.psz_string )
1065 /* Set. */
1066 var_SetInteger( p_input, "chapter", atoi( newval.psz_string ) );
1068 else
1070 /* Get. */
1071 int i_chap = var_GetInteger( p_input, "chapter" );
1072 int i_chapter_count = var_CountChoices( p_input, "chapter" );
1073 msg_rc( "Currently playing chapter %d/%d.", i_chap,
1074 i_chapter_count );
1077 else if( !strcmp( psz_cmd, "chapter_n" ) )
1078 var_TriggerCallback( p_input, "next-chapter" );
1079 else if( !strcmp( psz_cmd, "chapter_p" ) )
1080 var_TriggerCallback( p_input, "prev-chapter" );
1081 i_error = VLC_SUCCESS;
1083 else if( !strcmp( psz_cmd, "title" ) ||
1084 !strcmp( psz_cmd, "title_n" ) ||
1085 !strcmp( psz_cmd, "title_p" ) )
1087 if( !strcmp( psz_cmd, "title" ) )
1089 if ( *newval.psz_string )
1090 /* Set. */
1091 var_SetInteger( p_input, "title", atoi( newval.psz_string ) );
1092 else
1094 /* Get. */
1095 int i_title = var_GetInteger( p_input, "title" );
1096 int i_title_count = var_CountChoices( p_input, "title" );
1097 msg_rc( "Currently playing title %d/%d.", i_title,
1098 i_title_count );
1101 else if( !strcmp( psz_cmd, "title_n" ) )
1102 var_TriggerCallback( p_input, "next-title" );
1103 else if( !strcmp( psz_cmd, "title_p" ) )
1104 var_TriggerCallback( p_input, "prev-title" );
1106 i_error = VLC_SUCCESS;
1108 else if( !strcmp( psz_cmd, "atrack" )
1109 || !strcmp( psz_cmd, "vtrack" )
1110 || !strcmp( psz_cmd, "strack" ) )
1112 const char *psz_variable;
1113 char *name;
1115 if( !strcmp( psz_cmd, "atrack" ) )
1117 psz_variable = "audio-es";
1119 else if( !strcmp( psz_cmd, "vtrack" ) )
1121 psz_variable = "video-es";
1123 else
1125 psz_variable = "spu-es";
1128 /* Get the descriptive name of the variable */
1129 var_Change( p_input, psz_variable, VLC_VAR_GETTEXT, &name );
1130 if( !name ) name = strdup(psz_variable);
1132 if( newval.psz_string && *newval.psz_string )
1134 /* set */
1135 i_error = var_SetInteger( p_input, psz_variable,
1136 atoi( newval.psz_string ) );
1138 else
1140 /* get */
1141 vlc_value_t *val;
1142 char **text;
1143 size_t count;
1145 int i_value = var_GetInteger( p_input, psz_variable );
1147 if ( var_Change( p_input, psz_variable, VLC_VAR_GETCHOICES,
1148 &count, &val, &text ) < 0 )
1150 free( name );
1151 goto out;
1154 msg_rc( "+----[ %s ]", name );
1155 for ( size_t i = 0; i < count; i++ )
1157 msg_rc( "| %"PRId64" - %s%s", val[i].i_int, text[i],
1158 (i_value == val[i].i_int) ? " *" : "" );
1159 free(text[i]);
1161 free(text);
1162 free(val);
1163 msg_rc( "+----[ end of %s ]", name );
1165 free( name );
1167 out:
1168 vlc_object_release( p_input );
1169 return i_error;
1172 static void print_playlist( intf_thread_t *p_intf, playlist_item_t *p_item, int i_level )
1174 char psz_buffer[MSTRTIME_MAX_SIZE];
1175 for( int i = 0; i< p_item->i_children; i++ )
1177 if( p_item->pp_children[i]->p_input->i_duration != INPUT_DURATION_INDEFINITE )
1179 secstotimestr( psz_buffer, SEC_FROM_VLC_TICK(p_item->pp_children[i]->p_input->i_duration) );
1180 msg_rc( "|%*s- %s (%s)", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name, psz_buffer );
1182 else
1183 msg_rc( "|%*s- %s", 2 * i_level, "", p_item->pp_children[i]->p_input->psz_name );
1185 if( p_item->pp_children[i]->i_children >= 0 )
1186 print_playlist( p_intf, p_item->pp_children[i], i_level + 1 );
1190 static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
1191 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1193 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1195 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1196 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1197 input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1199 if( p_input )
1201 int state = var_GetInteger( p_input, "state" );
1202 vlc_object_release( p_input );
1204 if( state == PAUSE_S )
1206 msg_rc( "%s", _("Type 'pause' to continue.") );
1207 return VLC_EGENERIC;
1211 /* Parse commands that require a playlist */
1212 if( !strcmp( psz_cmd, "prev" ) )
1214 playlist_Prev( p_playlist );
1216 else if( !strcmp( psz_cmd, "next" ) )
1218 playlist_Next( p_playlist );
1220 else if( !strcmp( psz_cmd, "play" ) )
1222 msg_Warn( p_playlist, "play" );
1223 playlist_Play( p_playlist );
1225 else if( !strcmp( psz_cmd, "repeat" ) )
1227 bool b_update = true;
1228 bool b_value = var_GetBool( p_playlist, "repeat" );
1230 if( strlen( newval.psz_string ) > 0 )
1232 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1233 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1235 b_update = false;
1239 if ( b_update )
1241 b_value = !b_value;
1242 var_SetBool( p_playlist, "repeat", b_value );
1244 msg_rc( "Setting repeat to %s", b_value ? "true" : "false" );
1246 else if( !strcmp( psz_cmd, "loop" ) )
1248 bool b_update = true;
1249 bool b_value = var_GetBool( p_playlist, "loop" );
1251 if( strlen( newval.psz_string ) > 0 )
1253 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1254 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1256 b_update = false;
1260 if ( b_update )
1262 b_value = !b_value;
1263 var_SetBool( p_playlist, "loop", b_value );
1265 msg_rc( "Setting loop to %s", b_value ? "true" : "false" );
1267 else if( !strcmp( psz_cmd, "random" ) )
1269 bool b_update = true;
1270 bool b_value = var_GetBool( p_playlist, "random" );
1272 if( strlen( newval.psz_string ) > 0 )
1274 if ( ( !strncmp( newval.psz_string, "on", 2 ) && b_value ) ||
1275 ( !strncmp( newval.psz_string, "off", 3 ) && !b_value ) )
1277 b_update = false;
1281 if ( b_update )
1283 b_value = !b_value;
1284 var_SetBool( p_playlist, "random", b_value );
1286 msg_rc( "Setting random to %s", b_value ? "true" : "false" );
1288 else if (!strcmp( psz_cmd, "goto" ) )
1290 PL_LOCK;
1291 unsigned i_pos = atoi( newval.psz_string );
1292 unsigned i_size = p_playlist->items.i_size;
1294 if( i_pos <= 0 )
1295 msg_rc( "%s", _("Error: `goto' needs an argument greater than zero.") );
1296 else if( i_pos <= i_size )
1298 playlist_item_t *p_item, *p_parent;
1299 p_item = p_parent = p_playlist->items.p_elems[i_pos-1];
1300 while( p_parent->p_parent )
1301 p_parent = p_parent->p_parent;
1302 playlist_ViewPlay( p_playlist, p_parent, p_item );
1304 else
1305 msg_rc( vlc_ngettext("Playlist has only %u element",
1306 "Playlist has only %u elements", i_size),
1307 i_size );
1308 PL_UNLOCK;
1310 else if( !strcmp( psz_cmd, "stop" ) )
1312 playlist_Stop( p_playlist );
1314 else if( !strcmp( psz_cmd, "clear" ) )
1316 playlist_Stop( p_playlist );
1317 playlist_Clear( p_playlist, pl_Unlocked );
1319 else if( !strcmp( psz_cmd, "add" ) &&
1320 newval.psz_string && *newval.psz_string )
1322 input_item_t *p_item = parse_MRL( newval.psz_string );
1324 if( p_item )
1326 msg_rc( "Trying to add %s to playlist.", newval.psz_string );
1327 int i_ret = playlist_AddInput( p_playlist, p_item, true );
1328 input_item_Release( p_item );
1329 if( i_ret != VLC_SUCCESS )
1331 return VLC_EGENERIC;
1335 else if( !strcmp( psz_cmd, "enqueue" ) &&
1336 newval.psz_string && *newval.psz_string )
1338 input_item_t *p_item = parse_MRL( newval.psz_string );
1340 if( p_item )
1342 msg_rc( "trying to enqueue %s to playlist", newval.psz_string );
1343 int ret = playlist_AddInput( p_playlist, p_item, false );
1344 input_item_Release( p_item );
1345 if( ret != VLC_SUCCESS )
1347 return VLC_EGENERIC;
1351 else if( !strcmp( psz_cmd, "playlist" ) )
1353 msg_rc( "+----[ Playlist ]" );
1354 print_playlist( p_intf, &p_playlist->root, 0 );
1355 msg_rc( "+----[ End of playlist ]" );
1358 else if( !strcmp( psz_cmd, "sort" ))
1360 PL_LOCK;
1361 playlist_RecursiveNodeSort( p_playlist, &p_playlist->root,
1362 SORT_ARTIST, ORDER_NORMAL );
1363 PL_UNLOCK;
1365 else if( !strcmp( psz_cmd, "status" ) )
1367 p_input = playlist_CurrentInput( p_playlist );
1368 if( p_input )
1370 /* Replay the current state of the system. */
1371 char *psz_uri =
1372 input_item_GetURI( input_GetItem( p_input ) );
1373 vlc_object_release( p_input );
1374 if( likely(psz_uri != NULL) )
1376 msg_rc( STATUS_CHANGE "( new input: %s )", psz_uri );
1377 free( psz_uri );
1381 float volume = playlist_VolumeGet( p_playlist );
1382 if( volume >= 0.f )
1383 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1384 lroundf(volume * AOUT_VOLUME_DEFAULT) );
1386 int status;
1387 PL_LOCK;
1388 status = playlist_Status(p_playlist);
1389 PL_UNLOCK;
1390 switch( status )
1392 case PLAYLIST_STOPPED:
1393 msg_rc( STATUS_CHANGE "( stop state: 5 )" );
1394 break;
1395 case PLAYLIST_RUNNING:
1396 msg_rc( STATUS_CHANGE "( play state: 3 )" );
1397 break;
1398 case PLAYLIST_PAUSED:
1399 msg_rc( STATUS_CHANGE "( pause state: 4 )" );
1400 break;
1401 default:
1402 msg_rc( STATUS_CHANGE "( unknown state: -1 )" );
1403 break;
1408 * sanity check
1410 else
1412 msg_rc( "unknown command!" );
1415 return VLC_SUCCESS;
1418 static int Quit( vlc_object_t *p_this, char const *psz_cmd,
1419 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1421 VLC_UNUSED(p_data); VLC_UNUSED(psz_cmd);
1422 VLC_UNUSED(oldval); VLC_UNUSED(newval);
1424 libvlc_Quit( p_this->obj.libvlc );
1425 return VLC_SUCCESS;
1428 static int Intf( vlc_object_t *p_this, char const *psz_cmd,
1429 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1431 intf_thread_t *intf = (intf_thread_t *)p_this;
1433 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1434 return intf_Create(pl_Get(intf), newval.psz_string );
1437 static int Volume( vlc_object_t *p_this, char const *psz_cmd,
1438 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1440 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1441 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1442 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
1443 int i_error = VLC_EGENERIC;
1445 if ( *newval.psz_string )
1447 /* Set. */
1448 int i_volume = atoi( newval.psz_string );
1449 if( !playlist_VolumeSet( p_playlist,
1450 i_volume / (float)AOUT_VOLUME_DEFAULT ) )
1451 i_error = VLC_SUCCESS;
1452 playlist_MuteSet( p_playlist, i_volume == 0 );
1453 msg_rc( STATUS_CHANGE "( audio volume: %d )", i_volume );
1455 else
1457 /* Get. */
1458 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1459 lroundf( playlist_VolumeGet( p_playlist ) * AOUT_VOLUME_DEFAULT ) );
1460 i_error = VLC_SUCCESS;
1463 return i_error;
1466 static int VolumeMove( vlc_object_t *p_this, char const *psz_cmd,
1467 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1469 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1470 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1471 float volume;
1472 int i_nb_steps = atoi(newval.psz_string);
1473 int i_error = VLC_SUCCESS;
1475 if( !strcmp(psz_cmd, "voldown") )
1476 i_nb_steps *= -1;
1477 if( playlist_VolumeUp( p_intf->p_sys->p_playlist, i_nb_steps, &volume ) < 0 )
1478 i_error = VLC_EGENERIC;
1480 if ( !i_error )
1481 msg_rc( STATUS_CHANGE "( audio volume: %ld )",
1482 lroundf( volume * AOUT_VOLUME_DEFAULT ) );
1483 return i_error;
1487 static int VideoConfig( vlc_object_t *p_this, char const *psz_cmd,
1488 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1490 VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1491 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1492 input_thread_t *p_input =
1493 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1494 vout_thread_t * p_vout;
1495 const char * psz_variable = NULL;
1496 int i_error = VLC_SUCCESS;
1498 if( !p_input )
1499 return VLC_ENOOBJ;
1501 p_vout = input_GetVout( p_input );
1502 vlc_object_release( p_input );
1503 if( !p_vout )
1504 return VLC_ENOOBJ;
1506 if( !strcmp( psz_cmd, "vcrop" ) )
1508 psz_variable = "crop";
1510 else if( !strcmp( psz_cmd, "vratio" ) )
1512 psz_variable = "aspect-ratio";
1514 else if( !strcmp( psz_cmd, "vzoom" ) )
1516 psz_variable = "zoom";
1518 else if( !strcmp( psz_cmd, "snapshot" ) )
1520 psz_variable = "video-snapshot";
1522 else
1523 /* This case can't happen */
1524 vlc_assert_unreachable();
1526 if( newval.psz_string && *newval.psz_string )
1528 /* set */
1529 if( !strcmp( psz_variable, "zoom" ) )
1531 float f_float = atof( newval.psz_string );
1532 i_error = var_SetFloat( p_vout, psz_variable, f_float );
1534 else
1536 i_error = var_SetString( p_vout, psz_variable, newval.psz_string );
1539 else if( !strcmp( psz_cmd, "snapshot" ) )
1541 var_TriggerCallback( p_vout, psz_variable );
1543 else
1545 /* get */
1546 char *name;
1547 vlc_value_t *val;
1548 char **text;
1549 float f_value = 0.;
1550 char *psz_value = NULL;
1551 size_t count;
1553 if( !strcmp( psz_variable, "zoom" ) )
1554 f_value = var_GetFloat( p_vout, "zoom" );
1555 else
1557 psz_value = var_GetString( p_vout, psz_variable );
1558 if( psz_value == NULL )
1560 vlc_object_release( p_vout );
1561 return VLC_EGENERIC;
1565 if ( var_Change( p_vout, psz_variable, VLC_VAR_GETCHOICES,
1566 &count, &val, &text ) < 0 )
1568 vlc_object_release( p_vout );
1569 free( psz_value );
1570 return VLC_EGENERIC;
1573 /* Get the descriptive name of the variable */
1574 var_Change( p_vout, psz_variable, VLC_VAR_GETTEXT, &name );
1575 if( !name ) name = strdup(psz_variable);
1577 msg_rc( "+----[ %s ]", name );
1578 if( !strcmp( psz_variable, "zoom" ) )
1580 for ( size_t i = 0; i < count; i++ )
1582 msg_rc( "| %f - %s%s", val[i].f_float, text[i],
1583 f_value == val[i].f_float ? " *" : "" );
1584 free(text[i]);
1587 else
1589 for ( size_t i = 0; i < count; i++ )
1591 msg_rc( "| %s - %s%s", val[i].psz_string, text[i],
1592 strcmp(psz_value, val[i].psz_string)
1593 ? "" : " *" );
1594 free(text[i]);
1595 free(val[i].psz_string);
1597 free( psz_value );
1599 free(text);
1600 free(val);
1601 msg_rc( "+----[ end of %s ]", name );
1603 free( name );
1605 vlc_object_release( p_vout );
1606 return i_error;
1609 static int AudioDevice( vlc_object_t *obj, char const *cmd,
1610 vlc_value_t old, vlc_value_t cur, void *dummy )
1612 intf_thread_t *p_intf = (intf_thread_t *)obj;
1613 audio_output_t *p_aout = playlist_GetAout( pl_Get(p_intf) );
1614 if( p_aout == NULL )
1615 return VLC_ENOOBJ;
1617 if( !*cur.psz_string )
1619 char **ids, **names;
1620 int n = aout_DevicesList( p_aout, &ids, &names );
1621 if( n < 0 )
1622 goto out;
1624 char *dev = aout_DeviceGet( p_aout );
1625 const char *devstr = (dev != NULL) ? dev : "";
1627 msg_rc( "+----[ %s ]", cmd );
1628 for ( int i = 0; i < n; i++ )
1630 const char *fmt = "| %s - %s";
1632 if( !strcmp(devstr, ids[i]) )
1633 fmt = "| %s - %s *";
1634 msg_rc( fmt, ids[i], names[i] );
1635 free( names[i] );
1636 free( ids[i] );
1638 msg_rc( "+----[ end of %s ]", cmd );
1640 free( dev );
1641 free( names );
1642 free( ids );
1644 else
1645 aout_DeviceSet( p_aout, cur.psz_string );
1646 out:
1647 vlc_object_release( p_aout );
1648 (void) old; (void) dummy;
1649 return VLC_SUCCESS;
1652 static int AudioChannel( vlc_object_t *obj, char const *cmd,
1653 vlc_value_t old, vlc_value_t cur, void *dummy )
1655 intf_thread_t *p_intf = (intf_thread_t*)obj;
1656 vlc_object_t *p_aout = (vlc_object_t *)playlist_GetAout( pl_Get(p_intf) );
1657 if ( p_aout == NULL )
1658 return VLC_ENOOBJ;
1660 int ret = VLC_SUCCESS;
1662 if ( !*cur.psz_string )
1664 /* Retrieve all registered ***. */
1665 vlc_value_t *val;
1666 char **text;
1667 size_t count;
1669 if ( var_Change( p_aout, "stereo-mode", VLC_VAR_GETCHOICES,
1670 &count, &val, &text ) < 0 )
1672 ret = VLC_ENOVAR;
1673 goto out;
1676 int i_value = var_GetInteger( p_aout, "stereo-mode" );
1678 msg_rc( "+----[ %s ]", cmd );
1679 for ( size_t i = 0; i < count; i++ )
1681 msg_rc( "| %"PRId64" - %s%s", val[i].i_int, text[i],
1682 i_value == val[i].i_int ? " *" : "" );
1683 free(text[i]);
1685 free(text);
1686 free(val);
1687 msg_rc( "+----[ end of %s ]", cmd );
1689 else
1690 ret = var_SetInteger( p_aout, "stereo-mode", atoi( cur.psz_string ) );
1691 out:
1692 vlc_object_release( p_aout );
1693 (void) old; (void) dummy;
1694 return ret;
1697 static int Statistics ( vlc_object_t *p_this, char const *psz_cmd,
1698 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1700 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
1701 intf_thread_t *p_intf = (intf_thread_t*)p_this;
1702 input_thread_t *p_input =
1703 playlist_CurrentInput( p_intf->p_sys->p_playlist );
1705 if( !p_input )
1706 return VLC_ENOOBJ;
1708 updateStatistics( p_intf, input_GetItem(p_input) );
1709 vlc_object_release( p_input );
1710 return VLC_SUCCESS;
1713 static int updateStatistics( intf_thread_t *p_intf, input_item_t *p_item )
1715 if( !p_item ) return VLC_EGENERIC;
1717 vlc_mutex_lock( &p_item->lock );
1718 msg_rc( "+----[ begin of statistical info ]" );
1720 /* Input */
1721 msg_rc("%s", _("+-[Incoming]"));
1722 msg_rc(_("| input bytes read : %8.0f KiB"),
1723 (float)(p_item->p_stats->i_read_bytes)/1024 );
1724 msg_rc(_("| input bitrate : %6.0f kb/s"),
1725 (float)(p_item->p_stats->f_input_bitrate)*8000 );
1726 msg_rc(_("| demux bytes read : %8.0f KiB"),
1727 (float)(p_item->p_stats->i_demux_read_bytes)/1024 );
1728 msg_rc(_("| demux bitrate : %6.0f kb/s"),
1729 (float)(p_item->p_stats->f_demux_bitrate)*8000 );
1730 msg_rc(_("| demux corrupted : %5"PRIi64),
1731 p_item->p_stats->i_demux_corrupted );
1732 msg_rc(_("| discontinuities : %5"PRIi64),
1733 p_item->p_stats->i_demux_discontinuity );
1734 msg_rc("|");
1735 /* Video */
1736 msg_rc("%s", _("+-[Video Decoding]"));
1737 msg_rc(_("| video decoded : %5"PRIi64),
1738 p_item->p_stats->i_decoded_video );
1739 msg_rc(_("| frames displayed : %5"PRIi64),
1740 p_item->p_stats->i_displayed_pictures );
1741 msg_rc(_("| frames lost : %5"PRIi64),
1742 p_item->p_stats->i_lost_pictures );
1743 msg_rc("|");
1744 /* Audio*/
1745 msg_rc("%s", _("+-[Audio Decoding]"));
1746 msg_rc(_("| audio decoded : %5"PRIi64),
1747 p_item->p_stats->i_decoded_audio );
1748 msg_rc(_("| buffers played : %5"PRIi64),
1749 p_item->p_stats->i_played_abuffers );
1750 msg_rc(_("| buffers lost : %5"PRIi64),
1751 p_item->p_stats->i_lost_abuffers );
1752 msg_rc("|");
1753 msg_rc( "+----[ end of statistical info ]" );
1754 vlc_mutex_unlock( &p_item->lock );
1756 return VLC_SUCCESS;
1759 #if defined(_WIN32) && !VLC_WINSTORE_APP
1760 static bool ReadWin32( intf_thread_t *p_intf, unsigned char *p_buffer, int *pi_size )
1762 INPUT_RECORD input_record;
1763 DWORD i_dw;
1765 /* On Win32, select() only works on socket descriptors */
1766 while( WaitForSingleObjectEx( p_intf->p_sys->hConsoleIn,
1767 MS_FROM_VLC_TICK(INTF_IDLE_SLEEP), TRUE ) == WAIT_OBJECT_0 )
1769 // Prefer to fail early when there's not enough space to store a 4 bytes
1770 // UTF8 character. The function will be immediatly called again and we won't
1771 // lose an input
1772 while( *pi_size < MAX_LINE_LENGTH - 4 &&
1773 ReadConsoleInput( p_intf->p_sys->hConsoleIn, &input_record, 1, &i_dw ) )
1775 if( input_record.EventType != KEY_EVENT ||
1776 !input_record.Event.KeyEvent.bKeyDown ||
1777 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
1778 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
1779 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
1780 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
1782 /* nothing interesting */
1783 continue;
1785 if( input_record.Event.KeyEvent.uChar.AsciiChar == '\n' ||
1786 input_record.Event.KeyEvent.uChar.AsciiChar == '\r' )
1788 putc( '\n', stdout );
1789 break;
1791 switch( input_record.Event.KeyEvent.uChar.AsciiChar )
1793 case '\b':
1794 if ( *pi_size == 0 )
1795 break;
1796 if ( *pi_size > 1 && (p_buffer[*pi_size - 1] & 0xC0) == 0x80 )
1798 // pi_size currently points to the character to be written, so
1799 // we need to roll back from 2 bytes to start erasing the previous
1800 // character
1801 (*pi_size) -= 2;
1802 unsigned int nbBytes = 1;
1803 while( *pi_size > 0 && (p_buffer[*pi_size] & 0xC0) == 0x80 )
1805 (*pi_size)--;
1806 nbBytes++;
1808 assert( clz( (unsigned char)~(p_buffer[*pi_size]) ) == nbBytes + 1 );
1809 // The first utf8 byte will be overriden by a \0
1811 else
1812 (*pi_size)--;
1813 p_buffer[*pi_size] = 0;
1815 fputs( "\b \b", stdout );
1816 break;
1817 default:
1819 WCHAR psz_winput[] = { input_record.Event.KeyEvent.uChar.UnicodeChar, L'\0' };
1820 char* psz_input = FromWide( psz_winput );
1821 int input_size = strlen(psz_input);
1822 if ( *pi_size + input_size > MAX_LINE_LENGTH )
1824 p_buffer[ *pi_size ] = 0;
1825 return false;
1827 strcpy( (char*)&p_buffer[*pi_size], psz_input );
1828 utf8_fprintf( stdout, "%s", psz_input );
1829 free(psz_input);
1830 *pi_size += input_size;
1835 p_buffer[ *pi_size ] = 0;
1836 return true;
1839 vlc_testcancel ();
1841 return false;
1843 #endif
1845 bool ReadCommand( intf_thread_t *p_intf, char *p_buffer, int *pi_size )
1847 #if defined(_WIN32) && !VLC_WINSTORE_APP
1848 if( p_intf->p_sys->i_socket == -1 && !p_intf->p_sys->b_quiet )
1849 return ReadWin32( p_intf, (unsigned char*)p_buffer, pi_size );
1850 else if( p_intf->p_sys->i_socket == -1 )
1852 vlc_tick_sleep( INTF_IDLE_SLEEP );
1853 return false;
1855 #endif
1857 while( *pi_size < MAX_LINE_LENGTH )
1859 if( p_intf->p_sys->i_socket == -1 )
1861 if( read( 0/*STDIN_FILENO*/, p_buffer + *pi_size, 1 ) <= 0 )
1862 { /* Standard input closed: exit */
1863 libvlc_Quit( p_intf->obj.libvlc );
1864 p_buffer[*pi_size] = 0;
1865 return true;
1868 else
1869 { /* Connection closed */
1870 if( net_Read( p_intf, p_intf->p_sys->i_socket, p_buffer + *pi_size,
1871 1 ) <= 0 )
1873 net_Close( p_intf->p_sys->i_socket );
1874 p_intf->p_sys->i_socket = -1;
1875 p_buffer[*pi_size] = 0;
1876 return true;
1880 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1881 break;
1883 (*pi_size)++;
1886 if( *pi_size == MAX_LINE_LENGTH ||
1887 p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
1889 p_buffer[ *pi_size ] = 0;
1890 return true;
1893 return false;
1896 /*****************************************************************************
1897 * parse_MRL: build a input item from a full mrl
1898 *****************************************************************************
1899 * MRL format: "simplified-mrl [:option-name[=option-value]]"
1900 * We don't check for '"' or '\'', we just assume that a ':' that follows a
1901 * space is a new option. Should be good enough for our purpose.
1902 *****************************************************************************/
1903 static input_item_t *parse_MRL( const char *mrl )
1905 #define SKIPSPACE( p ) { while( *p == ' ' || *p == '\t' ) p++; }
1906 #define SKIPTRAILINGSPACE( p, d ) \
1907 { char *e=d; while( e > p && (*(e-1)==' ' || *(e-1)=='\t') ){e--;*e=0;} }
1909 input_item_t *p_item = NULL;
1910 char *psz_item = NULL, *psz_item_mrl = NULL, *psz_orig, *psz_mrl;
1911 char **ppsz_options = NULL;
1912 int i_options = 0;
1914 if( !mrl ) return 0;
1916 psz_mrl = psz_orig = strdup( mrl );
1917 if( !psz_mrl )
1918 return NULL;
1919 while( *psz_mrl )
1921 SKIPSPACE( psz_mrl );
1922 psz_item = psz_mrl;
1924 for( ; *psz_mrl; psz_mrl++ )
1926 if( (*psz_mrl == ' ' || *psz_mrl == '\t') && psz_mrl[1] == ':' )
1928 /* We have a complete item */
1929 break;
1931 if( (*psz_mrl == ' ' || *psz_mrl == '\t') &&
1932 (psz_mrl[1] == '"' || psz_mrl[1] == '\'') && psz_mrl[2] == ':')
1934 /* We have a complete item */
1935 break;
1939 if( *psz_mrl ) { *psz_mrl = 0; psz_mrl++; }
1940 SKIPTRAILINGSPACE( psz_item, psz_item + strlen( psz_item ) );
1942 /* Remove '"' and '\'' if necessary */
1943 if( *psz_item == '"' && psz_item[strlen(psz_item)-1] == '"' )
1944 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1945 if( *psz_item == '\'' && psz_item[strlen(psz_item)-1] == '\'' )
1946 { psz_item++; psz_item[strlen(psz_item)-1] = 0; }
1948 if( !psz_item_mrl )
1950 if( strstr( psz_item, "://" ) != NULL )
1951 psz_item_mrl = strdup( psz_item );
1952 else
1953 psz_item_mrl = vlc_path2uri( psz_item, NULL );
1954 if( psz_item_mrl == NULL )
1956 free( psz_orig );
1957 return NULL;
1960 else if( *psz_item )
1962 i_options++;
1963 ppsz_options = xrealloc( ppsz_options, i_options * sizeof(char *) );
1964 ppsz_options[i_options - 1] = &psz_item[1];
1967 if( *psz_mrl ) SKIPSPACE( psz_mrl );
1970 /* Now create a playlist item */
1971 if( psz_item_mrl )
1973 p_item = input_item_New( psz_item_mrl, NULL );
1974 for( int i = 0; i < i_options; i++ )
1976 input_item_AddOption( p_item, ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
1978 free( psz_item_mrl );
1981 if( i_options ) free( ppsz_options );
1982 free( psz_orig );
1984 return p_item;