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