1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Ilkka Ollakka <ileoo@videolan.org>
9 * Jean-Baptiste <jb@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include "input_manager.hpp"
31 #include "dialogs_provider.hpp"
33 static int ItemChanged( vlc_object_t *, const char *,
34 vlc_value_t, vlc_value_t, void * );
35 static int PLItemChanged( vlc_object_t *, const char *,
36 vlc_value_t, vlc_value_t, void * );
37 static int VolumeChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
40 static int InputEvent( vlc_object_t *, const char *,
41 vlc_value_t, vlc_value_t, void * );
44 /**********************************************************************
45 * InputManager implementation
46 **********************************************************************
47 * The Input Manager can be the main one around the playlist
48 * But can also be used for VLM dialog or similar
49 **********************************************************************/
51 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
52 QObject( parent ), p_intf( _p_intf )
54 i_old_playing_status = END_S;
66 InputManager::~InputManager()
71 /* Define the Input used.
72 Add the callbacks on input
73 p_input is held once here */
74 void InputManager::setInput( input_thread_t *_p_input )
78 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
80 vlc_object_hold( p_input );
81 emit statusChanged( PLAYING_S );
88 i_input_id = input_GetItem( p_input )->i_id;
94 emit rateChanged( INPUT_RATE_DEFAULT );
98 /* delete Input if it ever existed.
99 Delete the callbacls on input
100 p_input is released once here */
101 void InputManager::delInput()
106 i_old_playing_status = END_S;
113 emit positionUpdated( -1.0, 0 ,0 );
114 emit statusChanged( END_S );
115 emit nameChanged( "" );
116 emit artChanged( NULL );
117 emit rateChanged( INPUT_RATE_DEFAULT );
118 emit voutChanged( false );
119 vlc_object_release( p_input );
125 /* Add the callbacks on Input. Self explanatory */
126 void InputManager::addCallbacks()
128 var_AddCallback( p_input, "intf-event", InputEvent, this );
131 /* Delete the callbacks on Input. Self explanatory */
132 void InputManager::delCallbacks()
134 var_DelCallback( p_input, "intf-event", InputEvent, this );
137 /* Convert the event from the callbacks in actions */
138 void InputManager::customEvent( QEvent *event )
140 int i_type = event->type();
141 IMEvent *ple = static_cast<IMEvent *>(event);
143 if ( i_type != PositionUpdate_Type &&
144 i_type != ItemChanged_Type &&
145 i_type != ItemRateChanged_Type &&
146 i_type != ItemTitleChanged_Type &&
147 i_type != ItemEsChanged_Type &&
148 i_type != ItemTeletextChanged_Type &&
149 i_type != ItemStateChanged_Type &&
150 i_type != StatisticsUpdate_Type &&
151 i_type != InterfaceVoutUpdate_Type &&
152 i_type != MetaChanged_Type )
155 if( !hasInput() ) return;
157 if( ( i_type != PositionUpdate_Type &&
158 i_type != ItemRateChanged_Type &&
159 i_type != ItemEsChanged_Type &&
160 i_type != ItemTeletextChanged_Type &&
161 i_type != ItemStateChanged_Type &&
162 i_type != StatisticsUpdate_Type &&
163 i_type != InterfaceVoutUpdate_Type &&
164 i_type != MetaChanged_Type
166 && ( i_input_id != ple->i_id ) )
169 if( i_type != PositionUpdate_Type &&
170 i_type != StatisticsUpdate_Type )
171 msg_Dbg( p_intf, "New Event: type %i", i_type );
176 case PositionUpdate_Type:
179 case StatisticsUpdate_Type:
182 case ItemChanged_Type:
187 case MetaChanged_Type:
191 case ItemStateChanged_Type:
197 case ItemTitleChanged_Type:
201 case ItemRateChanged_Type:
204 case ItemEsChanged_Type:
207 case ItemTeletextChanged_Type:
210 case InterfaceVoutUpdate_Type:
214 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
218 void InputManager::UpdateStats()
220 emit statisticsUpdated( input_GetItem( p_input ) );
223 void InputManager::UpdatePosition()
225 /* Update position */
226 int i_length, i_time; /* Int is enough, since we store seconds */
228 i_length = var_GetTime( p_input , "length" ) / 1000000;
229 i_time = var_GetTime( p_input , "time") / 1000000;
230 f_pos = var_GetFloat( p_input , "position" );
231 emit positionUpdated( f_pos, i_time, i_length );
234 void InputManager::UpdateNavigation()
236 /* Update navigation status */
237 vlc_value_t val; val.i_int = 0;
240 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
244 emit titleChanged( true );
245 /* p_input != NULL since val.i_int != 0 */
247 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
248 emit chapterChanged( (val.i_int > 0) );
251 emit titleChanged( false );
254 void InputManager::UpdateStatus()
256 /* Update playing status */
257 vlc_value_t val; val.i_int = 0;
258 var_Get( p_input, "state", &val );
259 if( i_old_playing_status != val.i_int )
261 i_old_playing_status = val.i_int;
262 emit statusChanged( val.i_int );
266 void InputManager::UpdateRate()
269 int i_new_rate = var_GetInteger( p_input, "rate");
270 if( i_new_rate != i_rate )
274 emit rateChanged( i_rate );
278 void InputManager::UpdateMeta()
280 /* Update text, name and nowplaying */
283 /* Try to get the Title, then the Name */
284 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
285 if( EMPTY_STR( psz_name ) )
288 psz_name = input_item_GetName( input_GetItem( p_input ) );
291 /* Try to get the nowplaying */
292 char *psz_nowplaying =
293 input_item_GetNowPlaying( input_GetItem( p_input ) );
294 if( !EMPTY_STR( psz_nowplaying ) )
296 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
298 else /* Do it ourself */
300 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
302 if( !EMPTY_STR( psz_artist ) )
303 text.sprintf( "%s - %s", psz_artist, psz_name );
305 text.sprintf( "%s", psz_name );
309 /* Free everything */
311 free( psz_nowplaying );
313 /* If we have Nothing */
316 psz_name = input_item_GetURI( input_GetItem( p_input ) );
317 text.sprintf( "%s", psz_name );
318 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
322 if( oldName != text )
324 emit nameChanged( text );
329 bool InputManager::hasAudio()
334 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
335 return val.i_int > 0;
340 void InputManager::UpdateSPU()
345 void InputManager::UpdateTeletext()
348 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
350 telexActivation( false );
353 void InputManager::UpdateVout()
357 bool b_old_video = b_video;
359 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input,
360 VLC_OBJECT_VOUT, FIND_CHILD );
361 b_video = p_vout != NULL;
363 vlc_object_release( p_vout );
364 if( !!b_old_video != !!b_video )
365 emit voutChanged( b_video );
369 void InputManager::UpdateArt()
371 /* Update Art meta */
372 emit artChanged( input_GetItem( p_input ) );
375 /* User update of the slider */
376 void InputManager::sliderUpdate( float new_pos )
379 var_SetFloat( p_input, "position", new_pos );
382 /* User togglePlayPause */
383 void InputManager::togglePlayPause()
388 var_Get( p_input, "state", &state );
389 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
390 var_Set( p_input, "state", state );
391 emit statusChanged( state.i_int );
395 void InputManager::sectionPrev()
399 int i_type = var_Type( p_input, "next-chapter" );
400 vlc_value_t val; val.b_bool = true;
401 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
402 "prev-chapter":"prev-title", val );
406 void InputManager::sectionNext()
410 int i_type = var_Type( p_input, "next-chapter" );
411 vlc_value_t val; val.b_bool = true;
412 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
413 "next-chapter":"next-title", val );
417 void InputManager::sectionMenu()
421 vlc_value_t val, text;
424 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
427 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
429 for( int i = 0; i < val.p_list->i_count; i++ )
431 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
434 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
436 var_Set( p_input, "title 0", root );
444 /* Set a new Teletext Page */
445 void InputManager::telexSetPage( int page )
449 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
450 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
452 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
454 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
455 "zvbi", FIND_ANYWHERE );
458 var_SetInteger( p_vbi, "vbi-page", page );
459 vlc_object_release( p_vbi );
460 emit newTelexPageSet( page );
466 /* Set the transparency on teletext */
467 void InputManager::telexSetTransparency( bool b_transparentTelextext )
471 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
472 "zvbi", FIND_ANYWHERE );
475 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
476 vlc_object_release( p_vbi );
477 emit teletextTransparencyActivated( b_transparentTelextext );
482 void InputManager::telexActivation( bool b_enabled )
486 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
487 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
489 /* Teletext is possible. Show the buttons */
490 b_enabled = (i_teletext_es >= 0);
491 emit teletextPossible( b_enabled );
492 if( !b_enabled ) return;
494 /* If Teletext is selected */
495 if( i_teletext_es == i_spu_es )
497 /* Activate the buttons */
498 teletextActivated( true );
500 /* Then, find the current page */
502 vlc_object_t *p_vbi = (vlc_object_t *)
503 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
506 i_page = var_GetInteger( p_vbi, "vbi-page" );
507 vlc_object_release( p_vbi );
508 emit newTelexPageSet( i_page );
513 emit teletextPossible( b_enabled );
516 void InputManager::activateTeletext( bool b_enable )
520 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
521 if( i_teletext_es >= 0 )
523 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
528 void InputManager::reverse()
532 int i_rate = var_GetInteger( p_input, "rate" );
533 var_SetInteger( p_input, "rate", -i_rate );
537 void InputManager::slower()
540 var_SetVoid( p_input, "rate-slower" );
543 void InputManager::faster()
546 var_SetVoid( p_input, "rate-faster" );
549 void InputManager::normalRate()
552 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
555 void InputManager::setRate( int new_rate )
558 var_SetInteger( p_input, "rate", new_rate );
561 void InputManager::setAtoB()
565 timeA = var_GetTime( THEMIM->getInput(), "time" );
569 timeB = var_GetTime( THEMIM->getInput(), "time" );
570 var_SetTime( THEMIM->getInput(), "time" , timeA );
577 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
580 /* Function called regularly when in an AtoB loop */
581 void InputManager::AtoBLoop( int i_time )
585 if( ( i_time >= (int)( timeB/1000000 ) )
586 || ( i_time < (int)( timeA/1000000 ) ) )
587 var_SetTime( THEMIM->getInput(), "time" , timeA );
591 /**********************************************************************
592 * MainInputManager implementation. Wrap an input manager and
593 * take care of updating the main playlist input.
594 * Used in the main playlist Dialog
595 **********************************************************************/
596 MainInputManager * MainInputManager::instance = NULL;
598 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
599 : QObject(NULL), p_intf( _p_intf )
602 im = new InputManager( this, p_intf );
604 var_AddCallback( THEPL, "item-change", ItemChanged, im );
605 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
606 var_AddCallback( THEPL, "activity", PLItemChanged, this );
608 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
610 /* Warn our embedded IM about input changes */
611 CONNECT( this, inputChanged( input_thread_t * ),
612 im, setInput( input_thread_t * ) );
614 /* emit check if playlist has allready started playing */
616 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
617 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
618 QApplication::postEvent( this, static_cast<QEvent*>(event) );
622 MainInputManager::~MainInputManager()
626 var_DelCallback( p_input, "state", PLItemChanged, this );
627 vlc_object_release( p_input );
628 emit inputChanged( NULL );
631 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
633 var_DelCallback( THEPL, "activity", PLItemChanged, this );
634 var_DelCallback( THEPL, "item-change", ItemChanged, im );
636 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
639 void MainInputManager::customEvent( QEvent *event )
641 int type = event->type();
642 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
645 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
646 if( type == VolumeChanged_Type )
648 emit volumeChanged();
652 /* Should be PLItemChanged Event */
653 if( !p_intf->p_sys->b_isDialogProvider )
655 vlc_mutex_lock( &p_intf->change_lock );
656 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
658 var_DelCallback( p_input, "state", PLItemChanged, this );
659 vlc_object_release( p_input );
660 emit inputChanged( NULL );
662 vlc_mutex_unlock( &p_intf->change_lock );
668 p_input = playlist_CurrentInput(THEPL);
671 var_AddCallback( p_input, "state", PLItemChanged, this );
672 emit inputChanged( p_input );
675 vlc_mutex_unlock( &p_intf->change_lock );
679 /* we are working as a dialogs provider */
680 playlist_t *p_playlist = pl_Hold( p_intf );
681 p_input = playlist_CurrentInput( p_playlist );
684 emit inputChanged( p_input );
685 vlc_object_release( p_input );
687 pl_Release( p_intf );
691 /* Playlist Control functions */
692 void MainInputManager::stop()
694 playlist_Stop( THEPL );
697 void MainInputManager::next()
699 playlist_Next( THEPL );
702 void MainInputManager::prev()
704 playlist_Prev( THEPL );
707 void MainInputManager::togglePlayPause()
711 playlist_Play( THEPL );
713 getIM()->togglePlayPause();
716 /* Static callbacks */
719 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
720 vlc_value_t oldval, vlc_value_t newval, void *param )
722 InputManager *im = (InputManager*)param;
724 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
725 QApplication::postEvent( im, static_cast<QEvent*>(event) );
729 static int InputEvent( vlc_object_t *p_this, const char *,
730 vlc_value_t, vlc_value_t newval, void *param )
732 InputManager *im = (InputManager*)param;
735 switch( newval.i_int )
737 case INPUT_EVENT_STATE:
738 event = new IMEvent( ItemStateChanged_Type, 0 );
740 case INPUT_EVENT_RATE:
741 event = new IMEvent( ItemRateChanged_Type, 0 );
743 case INPUT_EVENT_TIMES:
744 event = new IMEvent( PositionUpdate_Type, 0 );
747 case INPUT_EVENT_STATISTICS:
748 event = new IMEvent( StatisticsUpdate_Type, 0 );
750 case INPUT_EVENT_VOUT:
751 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
754 case INPUT_EVENT_TITLE:
755 case INPUT_EVENT_CHAPTER: /* TODO is that correct ? */
756 event = new IMEvent( ItemTitleChanged_Type, 0 );
759 case INPUT_EVENT_ITEM_META:
760 case INPUT_EVENT_ITEM_INFO:
761 case INPUT_EVENT_ITEM_NAME:
762 event = new IMEvent( MetaChanged_Type, 0 );
766 event = new IMEvent( ItemEsChanged_Type, 0 );
768 case INPUT_EVENT_TELETEXT:
769 event = new IMEvent( ItemTeletextChanged_Type, 0 );
772 case INPUT_EVENT_PROGRAM:
773 case INPUT_EVENT_RECORD:
774 case INPUT_EVENT_SIGNAL:
775 case INPUT_EVENT_AUDIO_DELAY:
776 case INPUT_EVENT_SUBTITLE_DELAY:
783 QApplication::postEvent( im, static_cast<QEvent*>(event) );
788 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
789 vlc_value_t oldval, vlc_value_t newval, void *param )
791 MainInputManager *mim = (MainInputManager*)param;
793 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
794 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
798 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
799 vlc_value_t oldval, vlc_value_t newval, void *param )
801 MainInputManager *mim = (MainInputManager*)param;
803 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
804 QApplication::postEvent( mim, static_cast<QEvent*>(event) );