]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
ALSA: use IEC958 device when applicable (and small simplification)
[vlc] / modules / gui / qt4 / input_manager.cpp
1 /*****************************************************************************
2  * input_manager.cpp : Manage an input and interact with its GUI elements
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Ilkka Ollakka  <ileoo@videolan.org>
9  *          Jean-Baptiste <jb@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #define __STDC_FORMAT_MACROS 1
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "input_manager.hpp"
33 #include <vlc_keys.h>
34 #include <vlc_url.h>
35
36 #include <QApplication>
37
38 #include <assert.h>
39
40 static int ItemChanged( vlc_object_t *, const char *,
41                         vlc_value_t, vlc_value_t, void * );
42 static int LeafToParent( vlc_object_t *, const char *,
43                         vlc_value_t, vlc_value_t, void * );
44 static int PLItemChanged( vlc_object_t *, const char *,
45                         vlc_value_t, vlc_value_t, void * );
46 static int PLItemAppended( vlc_object_t *, const char *,
47                         vlc_value_t, vlc_value_t, void * );
48 static int PLItemRemoved( vlc_object_t *, const char *,
49                         vlc_value_t, vlc_value_t, void * );
50 static int VolumeChanged( vlc_object_t *, const char *,
51                         vlc_value_t, vlc_value_t, void * );
52 static int SoundMuteChanged( vlc_object_t *, const char *,
53                         vlc_value_t, vlc_value_t, void * );
54
55 static int RandomChanged( vlc_object_t *, const char *,
56                         vlc_value_t, vlc_value_t, void * );
57 static int LoopChanged( vlc_object_t *, const char *,
58                         vlc_value_t, vlc_value_t, void * );
59 static int RepeatChanged( vlc_object_t *, const char *,
60                         vlc_value_t, vlc_value_t, void * );
61
62
63 static int InputEvent( vlc_object_t *, const char *,
64                        vlc_value_t, vlc_value_t, void * );
65 static int VbiEvent( vlc_object_t *, const char *,
66                      vlc_value_t, vlc_value_t, void * );
67
68
69 /**********************************************************************
70  * InputManager implementation
71  **********************************************************************
72  * The Input Manager can be the main one around the playlist
73  * But can also be used for VLM dialog or similar
74  **********************************************************************/
75
76 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
77                            QObject( parent ), p_intf( _p_intf )
78 {
79     i_old_playing_status = END_S;
80     oldName      = "";
81     artUrl       = "";
82     p_input      = NULL;
83     p_input_vbi  = NULL;
84     f_rate       = 0.;
85     p_item       = NULL;
86     b_video      = false;
87     timeA        = 0;
88     timeB        = 0;
89     f_cache      = -1.; /* impossible initial value, different from all */
90 }
91
92 InputManager::~InputManager()
93 {
94     delInput();
95 }
96
97 /* Define the Input used.
98    Add the callbacks on input
99    p_input is held once here */
100 void InputManager::setInput( input_thread_t *_p_input )
101 {
102     delInput();
103     p_input = _p_input;
104     if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
105     {
106         msg_Dbg( p_intf, "IM: Setting an input" );
107         vlc_object_hold( p_input );
108         addCallbacks();
109         UpdateStatus();
110         UpdateName();
111         UpdateArt();
112         UpdateTeletext();
113         UpdateNavigation();
114         UpdateVout();
115
116         p_item = input_GetItem( p_input );
117         emit rateChanged( var_GetFloat( p_input, "rate" ) );
118     }
119     else
120     {
121         p_input = NULL;
122         p_item = NULL;
123         assert( !p_input_vbi );
124         emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
125     }
126 }
127
128 /* delete Input if it ever existed.
129    Delete the callbacls on input
130    p_input is released once here */
131 void InputManager::delInput()
132 {
133     if( !p_input ) return;
134     msg_Dbg( p_intf, "IM: Deleting the input" );
135
136     delCallbacks();
137     i_old_playing_status = END_S;
138     p_item               = NULL;
139     oldName              = "";
140     artUrl               = "";
141     b_video              = false;
142     timeA                = 0;
143     timeB                = 0;
144     f_rate               = 0. ;
145
146     if( p_input_vbi )
147     {
148         vlc_object_release( p_input_vbi );
149         p_input_vbi = NULL;
150     }
151
152     vlc_object_release( p_input );
153     p_input = NULL;
154
155     emit positionUpdated( -1.0, 0 ,0 );
156     emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
157     emit nameChanged( "" );
158     emit chapterChanged( 0 );
159     emit titleChanged( 0 );
160     emit playingStatusChanged( END_S );
161
162     emit teletextPossible( false );
163     emit AtoBchanged( false, false );
164     emit voutChanged( false );
165     emit voutListChanged( NULL, 0 );
166
167     /* Reset all InfoPanels but stats */
168     emit artChanged( NULL );
169     emit infoChanged( NULL );
170     emit currentMetaChanged( (input_item_t *)NULL );
171
172     emit encryptionChanged( false );
173     emit recordingStateChanged( false );
174
175     emit cachingChanged( 1 );
176 }
177
178 /* Convert the event from the callbacks in actions */
179 void InputManager::customEvent( QEvent *event )
180 {
181     int i_type = event->type();
182     IMEvent *ple = static_cast<IMEvent *>(event);
183
184     if( i_type == ItemChanged_Type )
185         UpdateMeta( ple->p_item );
186
187     if( !hasInput() )
188         return;
189
190     /* Actions */
191     switch( i_type )
192     {
193     case PositionUpdate_Type:
194         UpdatePosition();
195         break;
196     case StatisticsUpdate_Type:
197         UpdateStats();
198         break;
199     case ItemChanged_Type:
200         /* Ignore ItemChanged_Type event that does not apply to our input */
201         if( p_item == ple->p_item )
202         {
203             UpdateStatus();
204             // UpdateName();
205             UpdateArt();
206             /* Update duration of file */
207         }
208         break;
209     case ItemStateChanged_Type:
210         // TODO: Fusion with above state
211         UpdateStatus();
212         // UpdateName();
213         // UpdateNavigation(); This shouldn't be useful now
214         // UpdateTeletext(); Same
215         break;
216     case NameChanged_Type:
217         UpdateName();
218         break;
219     case MetaChanged_Type:
220         UpdateMeta();
221         UpdateName(); /* Needed for NowPlaying */
222         UpdateArt(); /* Art is part of meta in the core */
223         break;
224     case InfoChanged_Type:
225         UpdateInfo();
226         break;
227     case ItemTitleChanged_Type:
228         UpdateNavigation();
229         UpdateName(); /* Display the name of the Chapter, if exists */
230         break;
231     case ItemRateChanged_Type:
232         UpdateRate();
233         break;
234     case ItemEsChanged_Type:
235         UpdateTeletext();
236         // We don't do anything ES related. Why ?
237         break;
238     case ItemTeletextChanged_Type:
239         UpdateTeletext();
240         break;
241     case InterfaceVoutUpdate_Type:
242         UpdateVout();
243         break;
244     case SynchroChanged_Type:
245         emit synchroChanged();
246         break;
247     case CachingEvent_Type:
248         UpdateCaching();
249         break;
250     case BookmarksChanged_Type:
251         emit bookmarksChanged();
252         break;
253     case InterfaceAoutUpdate_Type:
254         UpdateAout();
255         break;
256     case RecordingEvent_Type:
257         UpdateRecord();
258         break;
259     case ProgramChanged_Type:
260         UpdateProgramEvent();
261         break;
262     case EPGEvent_Type:
263         UpdateEPG();
264         break;
265     default:
266         msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
267         assert(0);
268     }
269 }
270
271 /* Add the callbacks on Input. Self explanatory */
272 inline void InputManager::addCallbacks()
273 {
274     var_AddCallback( p_input, "intf-event", InputEvent, this );
275 }
276
277 /* Delete the callbacks on Input. Self explanatory */
278 inline void InputManager::delCallbacks()
279 {
280     var_DelCallback( p_input, "intf-event", InputEvent, this );
281 }
282
283 /* Static callbacks for IM */
284 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
285                         vlc_value_t oldval, vlc_value_t newval, void *param )
286 {
287     InputManager *im = (InputManager*)param;
288     input_item_t *p_item = static_cast<input_item_t *>(newval.p_address);
289
290     IMEvent *event = new IMEvent( ItemChanged_Type, p_item );
291     QApplication::postEvent( im, event );
292     return VLC_SUCCESS;
293 }
294
295 static int InputEvent( vlc_object_t *p_this, const char *,
296                        vlc_value_t, vlc_value_t newval, void *param )
297 {
298     InputManager *im = (InputManager*)param;
299     IMEvent *event;
300
301     switch( newval.i_int )
302     {
303     case INPUT_EVENT_STATE:
304         event = new IMEvent( ItemStateChanged_Type );
305         break;
306     case INPUT_EVENT_RATE:
307         event = new IMEvent( ItemRateChanged_Type );
308         break;
309     case INPUT_EVENT_POSITION:
310     //case INPUT_EVENT_LENGTH:
311         event = new IMEvent( PositionUpdate_Type );
312         break;
313
314     case INPUT_EVENT_TITLE:
315     case INPUT_EVENT_CHAPTER:
316         event = new IMEvent( ItemTitleChanged_Type );
317         break;
318
319     case INPUT_EVENT_ES:
320         event = new IMEvent( ItemEsChanged_Type );
321         break;
322     case INPUT_EVENT_TELETEXT:
323         event = new IMEvent( ItemTeletextChanged_Type );
324         break;
325
326     case INPUT_EVENT_STATISTICS:
327         event = new IMEvent( StatisticsUpdate_Type );
328         break;
329
330     case INPUT_EVENT_VOUT:
331         event = new IMEvent( InterfaceVoutUpdate_Type );
332         break;
333     case INPUT_EVENT_AOUT:
334         event = new IMEvent( InterfaceAoutUpdate_Type );
335         break;
336
337     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
338         event = new IMEvent( MetaChanged_Type );
339         break;
340     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
341         event = new IMEvent( InfoChanged_Type );
342         break;
343     case INPUT_EVENT_ITEM_NAME:
344         event = new IMEvent( NameChanged_Type );
345         break;
346
347     case INPUT_EVENT_AUDIO_DELAY:
348     case INPUT_EVENT_SUBTITLE_DELAY:
349         event = new IMEvent( SynchroChanged_Type );
350         break;
351
352     case INPUT_EVENT_CACHE:
353         event = new IMEvent( CachingEvent_Type );
354         break;
355
356     case INPUT_EVENT_BOOKMARK:
357         event = new IMEvent( BookmarksChanged_Type );
358         break;
359
360     case INPUT_EVENT_RECORD:
361         event = new IMEvent( RecordingEvent_Type );
362         break;
363
364     case INPUT_EVENT_PROGRAM:
365         /* This is for PID changes */
366         event = new IMEvent( ProgramChanged_Type );
367         break;
368
369     case INPUT_EVENT_ITEM_EPG:
370         /* EPG data changed */
371         event = new IMEvent( EPGEvent_Type );
372         break;
373
374     case INPUT_EVENT_SIGNAL:
375         /* This is for capture-card signals */
376         /* event = new IMEvent( SignalChanged_Type );
377         break; */
378     default:
379         event = NULL;
380         break;
381     }
382
383     if( event )
384         QApplication::postEvent( im, event );
385     return VLC_SUCCESS;
386 }
387
388 static int VbiEvent( vlc_object_t *, const char *,
389                      vlc_value_t, vlc_value_t, void *param )
390 {
391     InputManager *im = (InputManager*)param;
392     IMEvent *event = new IMEvent( ItemTeletextChanged_Type );
393
394     QApplication::postEvent( im, event );
395     return VLC_SUCCESS;
396 }
397
398 void InputManager::UpdatePosition()
399 {
400     /* Update position */
401     int i_length;
402     int64_t i_time;
403     float f_pos;
404     i_length = var_GetTime(  p_input , "length" ) / 1000000;
405     i_time = var_GetTime(  p_input , "time");
406     f_pos = var_GetFloat(  p_input , "position" );
407     emit positionUpdated( f_pos, i_time, i_length );
408 }
409
410 void InputManager::UpdateNavigation()
411 {
412     /* Update navigation status */
413     vlc_value_t val; val.i_int = 0;
414     vlc_value_t val2; val2.i_int = 0;
415
416     if( hasInput() )
417         var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
418
419     if( val.i_int > 0 )
420     {
421         emit titleChanged( true );
422         msg_Dbg( p_intf, "Title %"PRId64, val.i_int );
423         /* p_input != NULL since val.i_int != 0 */
424         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val2, NULL );
425         emit chapterChanged( (val2.i_int > 1) || ( val2.i_int > 0 && val.i_int > 1 ) );
426         msg_Dbg( p_intf, "Chapter: %"PRId64, val2.i_int );
427     }
428     else
429         emit titleChanged( false );
430 }
431
432 void InputManager::UpdateStatus()
433 {
434     /* Update playing status */
435     int state = var_GetInteger( p_input, "state" );
436     if( i_old_playing_status != state )
437     {
438         i_old_playing_status = state;
439         emit playingStatusChanged( state );
440     }
441 }
442
443 void InputManager::UpdateRate()
444 {
445     /* Update Rate */
446     float f_new_rate = var_GetFloat( p_input, "rate" );
447     if( f_new_rate != f_rate )
448     {
449         f_rate = f_new_rate;
450         /* Update rate */
451         emit rateChanged( f_rate );
452     }
453 }
454
455 void InputManager::UpdateName()
456 {
457     /* Update text, name and nowplaying */
458     QString text;
459
460     /* Try to get the Title, then the Name */
461     char *psz_name = input_item_GetTitleFbName( input_GetItem( p_input ) );
462
463     /* Try to get the nowplaying */
464     char *psz_nowplaying =
465         input_item_GetNowPlaying( input_GetItem( p_input ) );
466     if( !EMPTY_STR( psz_nowplaying ) )
467     {
468         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
469     }
470     else  /* Do it ourself */
471     {
472         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
473
474         if( !EMPTY_STR( psz_artist ) )
475             text.sprintf( "%s - %s", psz_artist, psz_name );
476         else
477             text.sprintf( "%s", psz_name );
478
479         free( psz_artist );
480     }
481     /* Free everything */
482     free( psz_name );
483     free( psz_nowplaying );
484
485     /* If we have Nothing */
486     if( text.isEmpty() )
487     {
488         psz_name = input_item_GetURI( input_GetItem( p_input ) );
489         text.sprintf( "%s", psz_name );
490         text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
491         free( psz_name );
492     }
493
494     if( oldName != text )
495     {
496         emit nameChanged( text );
497         oldName = text;
498     }
499 }
500
501 int InputManager::playingStatus()
502 {
503     return i_old_playing_status;
504 }
505
506 bool InputManager::hasAudio()
507 {
508     if( hasInput() )
509     {
510         vlc_value_t val;
511         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
512         return val.i_int > 0;
513     }
514     return false;
515 }
516
517 bool InputManager::hasVisualisation()
518 {
519     if( !p_input )
520         return false;
521
522     aout_instance_t *aout = input_GetAout( p_input );
523     if( !aout )
524         return false;
525
526     char *visual = var_InheritString( aout, "visual" );
527     vlc_object_release( aout );
528
529     if( !visual )
530         return false;
531
532     free( visual );
533     return true;
534 }
535
536 void InputManager::UpdateTeletext()
537 {
538     if( hasInput() )
539     {
540         const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
541         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
542
543         /* Teletext is possible. Show the buttons */
544         emit teletextPossible( b_enabled );
545
546         /* If Teletext is selected */
547         if( b_enabled && i_teletext_es >= 0 )
548         {
549             /* Then, find the current page */
550             int i_page = 100;
551             bool b_transparent = false;
552
553             if( p_input_vbi )
554             {
555                 var_DelCallback( p_input_vbi, "vbi-page", VbiEvent, this );
556                 vlc_object_release( p_input_vbi );
557             }
558
559             if( input_GetEsObjects( p_input, i_teletext_es, &p_input_vbi, NULL, NULL ) )
560                 p_input_vbi = NULL;
561
562             if( p_input_vbi )
563             {
564                 /* This callback is not remove explicitly, but interfaces
565                  * are guaranted to outlive input */
566                 var_AddCallback( p_input_vbi, "vbi-page", VbiEvent, this );
567
568                 i_page = var_GetInteger( p_input_vbi, "vbi-page" );
569                 b_transparent = !var_GetBool( p_input_vbi, "vbi-opaque" );
570             }
571             emit newTelexPageSet( i_page );
572             emit teletextTransparencyActivated( b_transparent );
573
574         }
575         emit teletextActivated( b_enabled && i_teletext_es >= 0 );
576     }
577     else
578     {
579         emit teletextActivated( false );
580         emit teletextPossible( false );
581     }
582 }
583
584 void InputManager::UpdateEPG()
585 {
586     if( hasInput() )
587     {
588        emit epgChanged();
589     }
590 }
591
592 void InputManager::UpdateVout()
593 {
594     if( hasInput() )
595     {
596         /* Get current vout lists from input */
597         size_t i_vout;
598         vout_thread_t **pp_vout;
599         if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
600         {
601             i_vout = 0;
602             pp_vout = NULL;
603         }
604
605         /* */
606         emit voutListChanged( pp_vout, i_vout );
607
608         /* */
609         bool b_old_video = b_video;
610         b_video = i_vout > 0;
611         if( !!b_old_video != !!b_video )
612             emit voutChanged( b_video );
613
614         /* Release the vout list */
615         for( int i = 0; i < i_vout; i++ )
616             vlc_object_release( (vlc_object_t*)pp_vout[i] );
617         free( pp_vout );
618     }
619 }
620 void InputManager::UpdateAout()
621 {
622     if( hasInput() )
623     {
624         /* TODO */
625     }
626 }
627 void InputManager::UpdateCaching()
628 {
629     if(!hasInput()) return;
630
631     float f_newCache = var_GetFloat ( p_input, "cache" );
632     if( f_newCache != f_cache )
633     {
634         f_cache = f_newCache;
635         /* Update cache */
636         emit cachingChanged( f_cache );
637     }
638 }
639
640 void InputManager::requestArtUpdate()
641 {
642     if( hasInput() )
643     {
644         playlist_AskForArtEnqueue( pl_Get(p_intf), input_GetItem( p_input ) );
645     }
646     else
647     {
648         /* No input will signal the cover art to update,
649          * let's do it ourself */
650         UpdateArt();
651     }
652 }
653
654 const QString InputManager::decodeArtURL( input_item_t *p_item )
655 {
656     assert( p_item );
657
658     char *psz_art = input_item_GetArtURL( p_item );
659     if( psz_art )
660     {
661         char *psz = make_path( psz_art );
662         free( psz_art );
663         psz_art = psz;
664     }
665
666 #if 0
667     /* Taglib seems to define a attachment://, It won't work yet */
668     url = url.replace( "attachment://", "" );
669 #endif
670
671     QString path = qfu( psz_art ? psz_art : "" );
672     free( psz_art );
673     return path;
674 }
675
676 void InputManager::UpdateArt()
677 {
678     QString url;
679
680     if( hasInput() )
681         url = decodeArtURL( input_GetItem( p_input ) );
682
683     /* the art hasn't changed, no need to update */
684     if(artUrl == url)
685         return;
686
687     /* Update Art meta */
688     artUrl = url;
689     emit artChanged( artUrl );
690 }
691
692 inline void InputManager::UpdateStats()
693 {
694     emit statisticsUpdated( input_GetItem( p_input ) );
695 }
696
697 inline void InputManager::UpdateMeta( input_item_t *p_item )
698 {
699     emit metaChanged( p_item );
700 }
701
702 inline void InputManager::UpdateMeta()
703 {
704     emit currentMetaChanged( input_GetItem( p_input ) );
705 }
706
707 inline void InputManager::UpdateInfo()
708 {
709     emit infoChanged( input_GetItem( p_input ) );
710 }
711
712 void InputManager::UpdateRecord()
713 {
714     if( hasInput() )
715     {
716         emit recordingStateChanged( var_GetBool( p_input, "record" ) );
717     }
718 }
719
720 void InputManager::UpdateProgramEvent()
721 {
722     if( hasInput() )
723     {
724         bool b_scrambled = var_GetBool( p_input, "program-scrambled" );
725         emit encryptionChanged( b_scrambled );
726     }
727 }
728
729 /* User update of the slider */
730 void InputManager::sliderUpdate( float new_pos )
731 {
732     if( hasInput() )
733         var_SetFloat( p_input, "position", new_pos );
734     emit seekRequested( new_pos );
735 }
736
737 /* User togglePlayPause */
738 void InputManager::togglePlayPause()
739 {
740     if( hasInput() )
741     {
742         int state = var_GetInteger( p_input, "state" );
743         state = ( state != PLAYING_S ) ? PLAYING_S : PAUSE_S;
744         var_SetInteger( p_input, "state", state );
745     }
746 }
747
748 void InputManager::sectionPrev()
749 {
750     if( hasInput() )
751     {
752         int i_type = var_Type( p_input, "next-chapter" );
753         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
754                              "prev-chapter":"prev-title" );
755     }
756 }
757
758 void InputManager::sectionNext()
759 {
760     if( hasInput() )
761     {
762         int i_type = var_Type( p_input, "next-chapter" );
763         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
764                              "next-chapter":"next-title" );
765     }
766 }
767
768 void InputManager::sectionMenu()
769 {
770     if( hasInput() )
771     {
772         vlc_value_t val, text;
773
774         if( var_Change( p_input, "title  0", VLC_VAR_GETLIST, &val, &text ) < 0 )
775             return;
776
777         /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
778         int root = 0;
779         for( int i = 0; i < val.p_list->i_count; i++ )
780         {
781             if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
782                 root = i;
783         }
784         var_FreeList( &val, &text );
785
786         var_SetInteger( p_input, "title  0", root );
787     }
788 }
789
790 /*
791  *  Teletext Functions
792  */
793
794 /* Set a new Teletext Page */
795 void InputManager::telexSetPage( int page )
796 {
797     if( hasInput() && p_input_vbi )
798     {
799         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
800
801         if( i_teletext_es >= 0 )
802         {
803             var_SetInteger( p_input_vbi, "vbi-page", page );
804             emit newTelexPageSet( page );
805         }
806     }
807 }
808
809 /* Set the transparency on teletext */
810 void InputManager::telexSetTransparency( bool b_transparentTelextext )
811 {
812     if( hasInput() && p_input_vbi )
813     {
814         var_SetBool( p_input_vbi, "vbi-opaque", !b_transparentTelextext );
815         emit teletextTransparencyActivated( b_transparentTelextext );
816     }
817 }
818
819 void InputManager::activateTeletext( bool b_enable )
820 {
821     vlc_value_t list;
822     vlc_value_t text;
823     if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETLIST, &list, &text ) )
824     {
825         if( list.p_list->i_count > 0 )
826         {
827             /* Prefer the page 100 if it is present */
828             int i;
829             for( i = 0; i < text.p_list->i_count; i++ )
830             {
831                 /* The description is the page number as a string */
832                 const char *psz_page = text.p_list->p_values[i].psz_string;
833                 if( psz_page && !strcmp( psz_page, "100" ) )
834                     break;
835             }
836             if( i >= list.p_list->i_count )
837                 i = 0;
838             var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
839         }
840         var_FreeList( &list, &text );
841     }
842 }
843
844 void InputManager::reverse()
845 {
846     if( hasInput() )
847     {
848         float f_rate = var_GetFloat( p_input, "rate" );
849         var_SetFloat( p_input, "rate", -f_rate );
850     }
851 }
852
853 void InputManager::slower()
854 {
855     var_TriggerCallback( THEPL, "rate-slower" );
856 }
857
858 void InputManager::faster()
859 {
860     var_TriggerCallback( THEPL, "rate-faster" );
861 }
862
863 void InputManager::littlefaster()
864 {
865     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_RATE_FASTER_FINE );
866 }
867
868 void InputManager::littleslower()
869 {
870     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_RATE_SLOWER_FINE );
871 }
872
873 void InputManager::normalRate()
874 {
875     var_SetFloat( THEPL, "rate", 1. );
876 }
877
878 void InputManager::setRate( int new_rate )
879 {
880     var_SetFloat( THEPL, "rate",
881                  (float)INPUT_RATE_DEFAULT / (float)new_rate );
882 }
883
884 void InputManager::jumpFwd()
885 {
886     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
887     if( i_interval > 0 )
888     {
889         mtime_t val = (mtime_t)(i_interval) * 1000000L;
890         var_SetTime( p_input, "time-offset", val );
891     }
892 }
893
894 void InputManager::jumpBwd()
895 {
896     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
897     if( i_interval > 0 )
898     {
899         mtime_t val = -1 *(mtime_t)(i_interval) * 1000000L;
900         var_SetTime( p_input, "time-offset", val );
901     }
902 }
903
904 void InputManager::setAtoB()
905 {
906     if( !timeA )
907     {
908         timeA = var_GetTime( THEMIM->getInput(), "time"  );
909     }
910     else if( !timeB )
911     {
912         timeB = var_GetTime( THEMIM->getInput(), "time"  );
913         var_SetTime( THEMIM->getInput(), "time" , timeA );
914         CONNECT( this, positionUpdated( float, int64_t, int ),
915                  this, AtoBLoop( float, int64_t, int ) );
916     }
917     else
918     {
919         timeA = 0;
920         timeB = 0;
921         disconnect( this, SIGNAL( positionUpdated( float, int64_t, int ) ),
922                     this, SLOT( AtoBLoop( float, int64_t, int ) ) );
923     }
924     emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
925 }
926
927 /* Function called regularly when in an AtoB loop */
928 void InputManager::AtoBLoop( float, int64_t i_time, int )
929 {
930     if( timeB )
931     {
932         if( i_time >= timeB || i_time < timeA )
933             var_SetTime( THEMIM->getInput(), "time" , timeA );
934     }
935 }
936
937 /**********************************************************************
938  * MainInputManager implementation. Wrap an input manager and
939  * take care of updating the main playlist input.
940  * Used in the main playlist Dialog
941  **********************************************************************/
942
943 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
944                  : QObject(NULL), p_intf( _p_intf )
945 {
946     p_input = NULL;
947     im = new InputManager( this, p_intf );
948
949     var_AddCallback( THEPL, "item-change", ItemChanged, im );
950     var_AddCallback( THEPL, "item-current", PLItemChanged, this );
951     var_AddCallback( THEPL, "activity", PLItemChanged, this );
952     var_AddCallback( THEPL, "leaf-to-parent", LeafToParent, this );
953     var_AddCallback( THEPL, "playlist-item-append", PLItemAppended, this );
954     var_AddCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
955     var_AddCallback( THEPL, "random", RandomChanged, this );
956     var_AddCallback( THEPL, "repeat", RepeatChanged, this );
957     var_AddCallback( THEPL, "loop", LoopChanged, this );
958
959     var_AddCallback( THEPL, "volume", VolumeChanged, this );
960     var_AddCallback( THEPL, "mute", SoundMuteChanged, this );
961
962     /* Warn our embedded IM about input changes */
963     DCONNECT( this, inputChanged( input_thread_t * ),
964               im, setInput( input_thread_t * ) );
965
966     /* emit check if playlist has already started playing */
967     input_thread_t *p_input = playlist_CurrentInput( THEPL );
968     if( p_input )
969     {
970         input_item_t *p_item = input_GetItem( p_input );
971         if( p_item )
972         {
973             IMEvent *event = new IMEvent( ItemChanged_Type, p_item );
974             customEvent( event );
975             delete event;
976         }
977         vlc_object_release( p_input );
978     }
979 }
980
981 MainInputManager::~MainInputManager()
982 {
983     if( p_input )
984     {
985        emit inputChanged( NULL );
986        var_DelCallback( p_input, "state", PLItemChanged, this );
987        vlc_object_release( p_input );
988     }
989
990     var_DelCallback( THEPL, "volume", VolumeChanged, this );
991     var_DelCallback( THEPL, "mute", SoundMuteChanged, this );
992
993     var_DelCallback( THEPL, "activity", PLItemChanged, this );
994     var_DelCallback( THEPL, "item-change", ItemChanged, im );
995     var_DelCallback( THEPL, "leaf-to-parent", LeafToParent, this );
996
997     var_DelCallback( THEPL, "item-current", PLItemChanged, this );
998     var_DelCallback( THEPL, "playlist-item-append", PLItemAppended, this );
999     var_DelCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
1000     var_DelCallback( THEPL, "random", RandomChanged, this );
1001     var_DelCallback( THEPL, "repeat", RepeatChanged, this );
1002     var_DelCallback( THEPL, "loop", LoopChanged, this );
1003
1004 }
1005
1006 vout_thread_t* MainInputManager::getVout()
1007 {
1008     return p_input ? input_GetVout( p_input ) : NULL;
1009 }
1010
1011 aout_instance_t * MainInputManager::getAout()
1012 {
1013     return p_input ? input_GetAout( p_input ) : NULL;
1014 }
1015
1016 void MainInputManager::customEvent( QEvent *event )
1017 {
1018     int type = event->type();
1019
1020     PLEvent *plEv;
1021     IMEvent *imEv;
1022
1023     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
1024     switch( type )
1025     {
1026     case VolumeChanged_Type:
1027         emit volumeChanged();
1028         return;
1029     case SoundMuteChanged_Type:
1030         emit soundMuteChanged();
1031         return;
1032     case PLItemAppended_Type:
1033         plEv = static_cast<PLEvent*>( event );
1034         emit playlistItemAppended( plEv->i_item, plEv->i_parent );
1035         return;
1036     case PLItemRemoved_Type:
1037         plEv = static_cast<PLEvent*>( event );
1038         emit playlistItemRemoved( plEv->i_item );
1039         return;
1040     case RandomChanged_Type:
1041         emit randomChanged( var_GetBool( THEPL, "random" ) );
1042         return;
1043     case LoopChanged_Type:
1044     case RepeatChanged_Type:
1045         notifyRepeatLoop();
1046         return;
1047     case LeafToParent_Type:
1048         imEv = static_cast<IMEvent*>( event );
1049         emit leafBecameParent( imEv->p_item );
1050     default:
1051         if( type != ItemChanged_Type ) return;
1052     }
1053
1054     /* Should be PLItemChanged Event */
1055     if( !p_intf->p_sys->b_isDialogProvider )
1056     {
1057         if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
1058         {
1059             emit inputChanged( p_input );
1060             var_DelCallback( p_input, "state", PLItemChanged, this );
1061             vlc_object_release( p_input );
1062             p_input = NULL;
1063             return;
1064         }
1065
1066         if( !p_input )
1067         {
1068             p_input = playlist_CurrentInput(THEPL);
1069             if( p_input )
1070             {
1071                 var_AddCallback( p_input, "state", PLItemChanged, this );
1072                 emit inputChanged( p_input );
1073             }
1074         }
1075     }
1076     else
1077     {
1078         /* remove previous stored p_input */
1079         if( p_input )
1080         {
1081             vlc_object_release( p_input );
1082             p_input = NULL;
1083         }
1084         /* we are working as a dialogs provider */
1085         p_input = playlist_CurrentInput( pl_Get(p_intf) );
1086         if( p_input )
1087         {
1088             emit inputChanged( p_input );
1089         }
1090     }
1091 }
1092
1093 /* Playlist Control functions */
1094 void MainInputManager::stop()
1095 {
1096    playlist_Stop( THEPL );
1097 }
1098
1099 void MainInputManager::next()
1100 {
1101    playlist_Next( THEPL );
1102 }
1103
1104 void MainInputManager::prev()
1105 {
1106    playlist_Prev( THEPL );
1107 }
1108
1109 void MainInputManager::togglePlayPause()
1110 {
1111     /* No input, play */
1112     if( !p_input )
1113         playlist_Play( THEPL );
1114     else
1115         getIM()->togglePlayPause();
1116 }
1117
1118 void MainInputManager::play()
1119 {
1120     /* No input, play */
1121     if( !p_input )
1122         playlist_Play( THEPL );
1123     else
1124     {
1125         if( PLAYING_S != var_GetInteger( p_input, "state" ) )
1126         {
1127             getIM()->togglePlayPause();
1128         }
1129     }
1130 }
1131
1132 void MainInputManager::pause()
1133 {
1134     if(p_input && PLAYING_S == var_GetInteger( p_input, "state" ) )
1135     {
1136         getIM()->togglePlayPause();
1137     }
1138 }
1139
1140 void MainInputManager::toggleRandom()
1141 {
1142     var_ToggleBool( THEPL, "random" );
1143 }
1144
1145 void MainInputManager::notifyRepeatLoop()
1146 {
1147     int i_value = var_GetBool( THEPL, "loop" ) * REPEAT_ALL
1148               + var_GetBool( THEPL, "repeat" ) * REPEAT_ONE;
1149
1150     emit repeatLoopChanged( i_value );
1151 }
1152
1153 void MainInputManager::loopRepeatLoopStatus()
1154 {
1155     /* Toggle Normal -> Loop -> Repeat -> Normal ... */
1156     if( var_GetBool( THEPL, "repeat" ) )
1157         var_SetBool( THEPL, "repeat", false );
1158     else if( var_GetBool( THEPL, "loop" ) )
1159     {
1160         var_SetBool( THEPL, "loop", false );
1161         var_SetBool( THEPL, "repeat", true );
1162     }
1163     else
1164         var_SetBool( THEPL, "loop", true );
1165 }
1166
1167 void MainInputManager::activatePlayQuit( bool b_exit )
1168 {
1169     var_SetBool( THEPL, "play-and-exit", b_exit );
1170 }
1171
1172 bool MainInputManager::getPlayExitState()
1173 {
1174     return var_GetBool( THEPL, "play-and-exit" );
1175 }
1176
1177 /****************************
1178  * Static callbacks for MIM *
1179  ****************************/
1180 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
1181                         vlc_value_t oldval, vlc_value_t, void *param )
1182 {
1183     MainInputManager *mim = (MainInputManager*)param;
1184
1185     IMEvent *event = new IMEvent( ItemChanged_Type );
1186     QApplication::postEvent( mim, event );
1187     return VLC_SUCCESS;
1188 }
1189
1190 static int LeafToParent( vlc_object_t *p_this, const char *psz_var,
1191                         vlc_value_t oldval, vlc_value_t newval, void *param )
1192 {
1193     MainInputManager *mim = (MainInputManager*)param;
1194
1195     IMEvent *event = new IMEvent( LeafToParent_Type,
1196                                   static_cast<input_item_t*>( newval.p_address ) );
1197     QApplication::postEvent( mim, event );
1198     return VLC_SUCCESS;
1199 }
1200
1201 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
1202                         vlc_value_t oldval, vlc_value_t newval, void *param )
1203 {
1204     MainInputManager *mim = (MainInputManager*)param;
1205
1206     IMEvent *event = new IMEvent( VolumeChanged_Type );
1207     QApplication::postEvent( mim, event );
1208     return VLC_SUCCESS;
1209 }
1210
1211 static int SoundMuteChanged( vlc_object_t *p_this, const char *psz_var,
1212                         vlc_value_t oldval, vlc_value_t newval, void *param )
1213 {
1214     MainInputManager *mim = (MainInputManager*)param;
1215
1216     IMEvent *event = new IMEvent( SoundMuteChanged_Type );
1217     QApplication::postEvent( mim, event );
1218     return VLC_SUCCESS;
1219 }
1220
1221 static int PLItemAppended
1222 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1223 {
1224     MainInputManager *mim = static_cast<MainInputManager*>(data);
1225     playlist_add_t *p_add = static_cast<playlist_add_t*>( cur.p_address );
1226
1227     PLEvent *event = new PLEvent( PLItemAppended_Type, p_add->i_item, p_add->i_node  );
1228     QApplication::postEvent( mim, event );
1229     return VLC_SUCCESS;
1230 }
1231 static int PLItemRemoved
1232 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1233 {
1234     MainInputManager *mim = static_cast<MainInputManager*>(data);
1235
1236     PLEvent *event = new PLEvent( PLItemRemoved_Type, cur.i_int, 0  );
1237     QApplication::postEvent( mim, event );
1238     return VLC_SUCCESS;
1239 }
1240
1241 static int RandomChanged
1242 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1243 {
1244     MainInputManager *mim = static_cast<MainInputManager*>(data);
1245
1246     IMEvent *event = new IMEvent( RandomChanged_Type );
1247     QApplication::postEvent( mim, event );
1248     return VLC_SUCCESS;
1249 }
1250
1251 /* Probably could be merged with next callback */
1252 static int LoopChanged
1253 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1254 {
1255     MainInputManager *mim = static_cast<MainInputManager*>(data);
1256
1257     IMEvent *event = new IMEvent( LoopChanged_Type );
1258     QApplication::postEvent( mim, event );
1259     return VLC_SUCCESS;
1260 }
1261
1262 static int RepeatChanged
1263 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1264 {
1265     MainInputManager *mim = static_cast<MainInputManager*>(data);
1266
1267     IMEvent *event = new IMEvent( RepeatChanged_Type );
1268     QApplication::postEvent( mim, event );
1269     return VLC_SUCCESS;
1270 }