]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt: fix MLModel uris
[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
39 #include <QApplication>
40 #include <QFile>
41 #include <QDir>
42 #include <QSignalMapper>
43
44 #include <assert.h>
45
46 static int ItemChanged( vlc_object_t *, const char *,
47                         vlc_value_t, vlc_value_t, void * );
48 static int LeafToParent( vlc_object_t *, const char *,
49                         vlc_value_t, vlc_value_t, void * );
50 static int PLItemChanged( vlc_object_t *, const char *,
51                         vlc_value_t, vlc_value_t, void * );
52 static int PLItemAppended( vlc_object_t *, const char *,
53                         vlc_value_t, vlc_value_t, void * );
54 static int PLItemRemoved( vlc_object_t *, const char *,
55                         vlc_value_t, vlc_value_t, void * );
56
57 static int InputEvent( vlc_object_t *, const char *,
58                        vlc_value_t, vlc_value_t, void * );
59 static int VbiEvent( vlc_object_t *, const char *,
60                      vlc_value_t, vlc_value_t, void * );
61
62 /* Ensure arbitratry (not dynamically allocated) event IDs are not in use */
63 static inline void registerAndCheckEventIds( int start, int end )
64 {
65     for ( int i=start ; i<=end ; i++ )
66         Q_ASSERT( QEvent::registerEventType( i ) == i ); /* event ID collision ! */
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     registerAndCheckEventIds( IMEvent::PositionUpdate, IMEvent::FullscreenControlPlanHide );
91     registerAndCheckEventIds( PLEvent::PLItemAppended, PLEvent::PLEmpty );
92 }
93
94 InputManager::~InputManager()
95 {
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 != NULL )
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_item = NULL;
124         assert( !p_input_vbi );
125         emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
126     }
127 }
128
129 /* delete Input if it ever existed.
130    Delete the callbacls on input
131    p_input is released once here */
132 void InputManager::delInput()
133 {
134     if( !p_input ) return;
135     msg_Dbg( p_intf, "IM: Deleting the input" );
136
137     delCallbacks();
138     i_old_playing_status = END_S;
139     p_item               = NULL;
140     oldName              = "";
141     artUrl               = "";
142     b_video              = false;
143     timeA                = 0;
144     timeB                = 0;
145     f_rate               = 0. ;
146
147     if( p_input_vbi )
148     {
149         vlc_object_release( p_input_vbi );
150         p_input_vbi = NULL;
151     }
152
153     vlc_object_release( p_input );
154     p_input = NULL;
155
156     emit positionUpdated( -1.0, 0 ,0 );
157     emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
158     emit nameChanged( "" );
159     emit chapterChanged( 0 );
160     emit titleChanged( 0 );
161     emit playingStatusChanged( END_S );
162
163     emit teletextPossible( false );
164     emit AtoBchanged( false, false );
165     emit voutChanged( false );
166     emit voutListChanged( NULL, 0 );
167
168     /* Reset all InfoPanels but stats */
169     emit artChanged( NULL );
170     emit infoChanged( NULL );
171     emit currentMetaChanged( (input_item_t *)NULL );
172
173     emit encryptionChanged( false );
174     emit recordingStateChanged( false );
175
176     emit cachingChanged( 1 );
177 }
178
179 /* Convert the event from the callbacks in actions */
180 void InputManager::customEvent( QEvent *event )
181 {
182     int i_type = event->type();
183     IMEvent *ple = static_cast<IMEvent *>(event);
184
185     if( i_type == IMEvent::ItemChanged )
186         UpdateMeta( ple->item() );
187
188     if( !hasInput() )
189         return;
190
191     /* Actions */
192     switch( i_type )
193     {
194     case IMEvent::PositionUpdate:
195         UpdatePosition();
196         break;
197     case IMEvent::StatisticsUpdate:
198         UpdateStats();
199         break;
200     case IMEvent::ItemChanged:
201         /* Ignore ItemChanged_Type event that does not apply to our input */
202         if( p_item == ple->item() )
203         {
204             UpdateStatus();
205             // UpdateName();
206             UpdateArt();
207             UpdateMeta();
208             /* Update duration of file */
209         }
210         break;
211     case IMEvent::ItemStateChanged:
212         UpdateStatus();
213         break;
214     case IMEvent::NameChanged:
215         UpdateName();
216         break;
217     case IMEvent::MetaChanged:
218         UpdateMeta();
219         UpdateName(); /* Needed for NowPlaying */
220         UpdateArt(); /* Art is part of meta in the core */
221         break;
222     case IMEvent::InfoChanged:
223         UpdateInfo();
224         break;
225     case IMEvent::ItemTitleChanged:
226         UpdateNavigation();
227         UpdateName(); /* Display the name of the Chapter, if exists */
228         break;
229     case IMEvent::ItemRateChanged:
230         UpdateRate();
231         break;
232     case IMEvent::ItemEsChanged:
233         UpdateTeletext();
234         // We don't do anything ES related. Why ?
235         break;
236     case IMEvent::ItemTeletextChanged:
237         UpdateTeletext();
238         break;
239     case IMEvent::InterfaceVoutUpdate:
240         UpdateVout();
241         break;
242     case IMEvent::SynchroChanged:
243         emit synchroChanged();
244         break;
245     case IMEvent::CachingEvent:
246         UpdateCaching();
247         break;
248     case IMEvent::BookmarksChanged:
249         emit bookmarksChanged();
250         break;
251     case IMEvent::InterfaceAoutUpdate:
252         UpdateAout();
253         break;
254     case IMEvent::RecordingEvent:
255         UpdateRecord();
256         break;
257     case IMEvent::ProgramChanged:
258         UpdateProgramEvent();
259         break;
260     case IMEvent::EPGEvent:
261         UpdateEPG();
262         break;
263     default:
264         msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
265         assert(0);
266     }
267 }
268
269 /* Add the callbacks on Input. Self explanatory */
270 inline void InputManager::addCallbacks()
271 {
272     var_AddCallback( p_input, "intf-event", InputEvent, this );
273 }
274
275 /* Delete the callbacks on Input. Self explanatory */
276 inline void InputManager::delCallbacks()
277 {
278     var_DelCallback( p_input, "intf-event", InputEvent, this );
279 }
280
281 /* Static callbacks for IM */
282 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
283                         vlc_value_t oldval, vlc_value_t newval, void *param )
284 {
285     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
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( IMEvent::ItemChanged, 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     VLC_UNUSED( p_this );
299
300     InputManager *im = (InputManager*)param;
301     IMEvent *event;
302
303     switch( newval.i_int )
304     {
305     case INPUT_EVENT_STATE:
306         event = new IMEvent( IMEvent::ItemStateChanged );
307         break;
308     case INPUT_EVENT_RATE:
309         event = new IMEvent( IMEvent::ItemRateChanged );
310         break;
311     case INPUT_EVENT_POSITION:
312     //case INPUT_EVENT_LENGTH:
313         event = new IMEvent( IMEvent::PositionUpdate );
314         break;
315
316     case INPUT_EVENT_TITLE:
317     case INPUT_EVENT_CHAPTER:
318         event = new IMEvent( IMEvent::ItemTitleChanged );
319         break;
320
321     case INPUT_EVENT_ES:
322         event = new IMEvent( IMEvent::ItemEsChanged );
323         break;
324     case INPUT_EVENT_TELETEXT:
325         event = new IMEvent( IMEvent::ItemTeletextChanged );
326         break;
327
328     case INPUT_EVENT_STATISTICS:
329         event = new IMEvent( IMEvent::StatisticsUpdate );
330         break;
331
332     case INPUT_EVENT_VOUT:
333         event = new IMEvent( IMEvent::InterfaceVoutUpdate );
334         break;
335     case INPUT_EVENT_AOUT:
336         event = new IMEvent( IMEvent::InterfaceAoutUpdate );
337         break;
338
339     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
340         event = new IMEvent( IMEvent::MetaChanged );
341         break;
342     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
343         event = new IMEvent( IMEvent::InfoChanged );
344         break;
345     case INPUT_EVENT_ITEM_NAME:
346         event = new IMEvent( IMEvent::NameChanged );
347         break;
348
349     case INPUT_EVENT_AUDIO_DELAY:
350     case INPUT_EVENT_SUBTITLE_DELAY:
351         event = new IMEvent( IMEvent::SynchroChanged );
352         break;
353
354     case INPUT_EVENT_CACHE:
355         event = new IMEvent( IMEvent::CachingEvent );
356         break;
357
358     case INPUT_EVENT_BOOKMARK:
359         event = new IMEvent( IMEvent::BookmarksChanged );
360         break;
361
362     case INPUT_EVENT_RECORD:
363         event = new IMEvent( IMEvent::RecordingEvent );
364         break;
365
366     case INPUT_EVENT_PROGRAM:
367         /* This is for PID changes */
368         event = new IMEvent( IMEvent::ProgramChanged );
369         break;
370
371     case INPUT_EVENT_ITEM_EPG:
372         /* EPG data changed */
373         event = new IMEvent( IMEvent::EPGEvent );
374         break;
375
376     case INPUT_EVENT_SIGNAL:
377         /* This is for capture-card signals */
378         /* event = new IMEvent( SignalChanged_Type );
379         break; */
380     default:
381         event = NULL;
382         break;
383     }
384
385     if( event )
386         QApplication::postEvent( im, event );
387     return VLC_SUCCESS;
388 }
389
390 static int VbiEvent( vlc_object_t *, const char *,
391                      vlc_value_t, vlc_value_t, void *param )
392 {
393     InputManager *im = (InputManager*)param;
394     IMEvent *event = new IMEvent( IMEvent::ItemTeletextChanged );
395
396     QApplication::postEvent( im, event );
397     return VLC_SUCCESS;
398 }
399
400 void InputManager::UpdatePosition()
401 {
402     /* Update position */
403     int i_length;
404     int64_t i_time;
405     float f_pos;
406     i_length = var_GetTime(  p_input , "length" ) / CLOCK_FREQ;
407     i_time = var_GetTime(  p_input , "time");
408     f_pos = var_GetFloat(  p_input , "position" );
409     emit positionUpdated( f_pos, i_time, i_length );
410 }
411
412 void InputManager::UpdateNavigation()
413 {
414     /* Update navigation status */
415     vlc_value_t val; val.i_int = 0;
416     vlc_value_t val2; val2.i_int = 0;
417
418     if( hasInput() )
419         var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
420
421     if( val.i_int > 0 )
422     {
423         /* p_input != NULL since val.i_int != 0 */
424         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val2, NULL );
425
426         emit titleChanged( val.i_int > 1 );
427         emit chapterChanged( val2.i_int > 1 );
428     }
429     else
430         emit chapterChanged( false );
431
432     if( hasInput() )
433         emit inputCanSeek( var_GetBool( p_input, "can-seek" ) );
434     else
435         emit inputCanSeek( false );
436 }
437
438 void InputManager::UpdateStatus()
439 {
440     /* Update playing status */
441     int state = var_GetInteger( p_input, "state" );
442     if( i_old_playing_status != state )
443     {
444         i_old_playing_status = state;
445         emit playingStatusChanged( state );
446     }
447 }
448
449 void InputManager::UpdateRate()
450 {
451     /* Update Rate */
452     float f_new_rate = var_GetFloat( p_input, "rate" );
453     if( f_new_rate != f_rate )
454     {
455         f_rate = f_new_rate;
456         /* Update rate */
457         emit rateChanged( f_rate );
458     }
459 }
460
461 void InputManager::UpdateName()
462 {
463     assert( p_input );
464
465     /* Update text, name and nowplaying */
466     QString name;
467
468     /* Try to get the nowplaying */
469     char *format = var_InheritString( p_intf, "input-title-format" );
470     char *formated = str_format_meta( THEPL, format );
471     free( format );
472     name = qfu(formated);
473     free( formated );
474
475     /* If we have Nothing */
476     if( name.simplified().isEmpty() )
477     {
478         char *uri = input_item_GetURI( input_GetItem( p_input ) );
479         char *file = uri ? strrchr( uri, '/' ) : NULL;
480         if( file != NULL )
481         {
482             decode_URI( ++file );
483             name = qfu(file);
484         }
485         else
486             name = qfu(uri);
487         free( uri );
488     }
489
490     name = name.trimmed();
491
492     if( oldName != name )
493     {
494         emit nameChanged( name );
495         oldName = name;
496     }
497 }
498
499 int InputManager::playingStatus()
500 {
501     return i_old_playing_status;
502 }
503
504 bool InputManager::hasAudio()
505 {
506     if( hasInput() )
507     {
508         vlc_value_t val;
509         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
510         return val.i_int > 0;
511     }
512     return false;
513 }
514
515 bool InputManager::hasVisualisation()
516 {
517     if( !p_input )
518         return false;
519
520     audio_output_t *aout = input_GetAout( p_input );
521     if( !aout )
522         return false;
523
524     char *visual = var_InheritString( aout, "visual" );
525     vlc_object_release( aout );
526
527     if( !visual )
528         return false;
529
530     free( visual );
531     return true;
532 }
533
534 void InputManager::UpdateTeletext()
535 {
536     if( hasInput() )
537     {
538         const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
539         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
540
541         /* Teletext is possible. Show the buttons */
542         emit teletextPossible( b_enabled );
543
544         /* If Teletext is selected */
545         if( b_enabled && i_teletext_es >= 0 )
546         {
547             /* Then, find the current page */
548             int i_page = 100;
549             bool b_transparent = false;
550
551             if( p_input_vbi )
552             {
553                 var_DelCallback( p_input_vbi, "vbi-page", VbiEvent, this );
554                 vlc_object_release( p_input_vbi );
555             }
556
557             if( input_GetEsObjects( p_input, i_teletext_es, &p_input_vbi, NULL, NULL ) )
558                 p_input_vbi = NULL;
559
560             if( p_input_vbi )
561             {
562                 /* This callback is not remove explicitly, but interfaces
563                  * are guaranted to outlive input */
564                 var_AddCallback( p_input_vbi, "vbi-page", VbiEvent, this );
565
566                 i_page = var_GetInteger( p_input_vbi, "vbi-page" );
567                 b_transparent = !var_GetBool( p_input_vbi, "vbi-opaque" );
568             }
569             emit newTelexPageSet( i_page );
570             emit teletextTransparencyActivated( b_transparent );
571
572         }
573         emit teletextActivated( b_enabled && i_teletext_es >= 0 );
574     }
575     else
576     {
577         emit teletextActivated( false );
578         emit teletextPossible( false );
579     }
580 }
581
582 void InputManager::UpdateEPG()
583 {
584     if( hasInput() )
585     {
586        emit epgChanged();
587     }
588 }
589
590 void InputManager::UpdateVout()
591 {
592     if( hasInput() )
593     {
594         /* Get current vout lists from input */
595         size_t i_vout;
596         vout_thread_t **pp_vout;
597         if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
598         {
599             i_vout = 0;
600             pp_vout = NULL;
601         }
602
603         /* */
604         emit voutListChanged( pp_vout, i_vout );
605
606         /* */
607         bool b_old_video = b_video;
608         b_video = i_vout > 0;
609         if( !!b_old_video != !!b_video )
610             emit voutChanged( b_video );
611
612         /* Release the vout list */
613         for( size_t i = 0; i < i_vout; i++ )
614             vlc_object_release( (vlc_object_t*)pp_vout[i] );
615         free( pp_vout );
616     }
617 }
618 void InputManager::UpdateAout()
619 {
620     if( hasInput() )
621     {
622         /* TODO */
623     }
624 }
625 void InputManager::UpdateCaching()
626 {
627     if(!hasInput()) return;
628
629     float f_newCache = var_GetFloat ( p_input, "cache" );
630     if( f_newCache != f_cache )
631     {
632         f_cache = f_newCache;
633         /* Update cache */
634         emit cachingChanged( f_cache );
635     }
636 }
637
638 void InputManager::requestArtUpdate( input_item_t *p_item )
639 {
640     bool b_current_item = false;
641     if ( !p_item && hasInput() )
642     {   /* default to current item */
643         p_item = input_GetItem( p_input );
644         b_current_item = true;
645     }
646
647     if ( p_item )
648     {
649         /* check if it has already been enqueued */
650         if ( p_item->p_meta )
651         {
652             int status = vlc_meta_GetStatus( p_item->p_meta );
653             if ( status & ( ITEM_ART_NOTFOUND|ITEM_ART_FETCHED ) )
654                 return;
655         }
656         playlist_AskForArtEnqueue( pl_Get(p_intf), p_item );
657         /* No input will signal the cover art to update,
658              * let's do it ourself */
659         if ( b_current_item )
660             UpdateArt();
661         else
662             emit artChanged( p_item );
663     }
664 }
665
666 const QString InputManager::decodeArtURL( input_item_t *p_item )
667 {
668     assert( p_item );
669
670     char *psz_art = input_item_GetArtURL( p_item );
671     if( psz_art )
672     {
673         char *psz = make_path( psz_art );
674         free( psz_art );
675         psz_art = psz;
676     }
677
678 #if 0
679     /* Taglib seems to define a attachment://, It won't work yet */
680     url = url.replace( "attachment://", "" );
681 #endif
682
683     QString path = qfu( psz_art ? psz_art : "" );
684     free( psz_art );
685     return path;
686 }
687
688 void InputManager::UpdateArt()
689 {
690     QString url;
691
692     if( hasInput() )
693         url = decodeArtURL( input_GetItem( p_input ) );
694
695     /* the art hasn't changed, no need to update */
696     if(artUrl == url)
697         return;
698
699     /* Update Art meta */
700     artUrl = url;
701     emit artChanged( artUrl );
702 }
703
704 void InputManager::setArt( input_item_t *p_item, QString fileUrl )
705 {
706     if( hasInput() )
707     {
708         char *psz_cachedir = config_GetUserDir( VLC_CACHE_DIR );
709         QString old_url = THEMIM->getIM()->decodeArtURL( p_item );
710         old_url = QDir( old_url ).canonicalPath();
711
712         if( old_url.startsWith( QString::fromUtf8( psz_cachedir ) ) )
713             QFile( old_url ).remove(); /* Purge cached artwork */
714
715         free( psz_cachedir );
716
717         input_item_SetArtURL( p_item , fileUrl.toUtf8().constData() );
718         UpdateArt();
719     }
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, "activity", PLItemChanged, this );
977     var_AddCallback( THEPL, "leaf-to-parent", LeafToParent, this );
978     var_AddCallback( THEPL, "playlist-item-append", PLItemAppended, this );
979     var_AddCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
980     random.addCallback( this, SLOT(notifyRandom(bool)) );
981     repeat.addCallback( this, SLOT(notifyRepeatLoop(bool)) );
982     loop.addCallback( this, SLOT(notifyRepeatLoop(bool)) );
983
984     volume.addCallback( this, SLOT(notifyVolume(float)) );
985     mute.addCallback( this, SLOT(notifyMute(bool)) );
986
987     /* Warn our embedded IM about input changes */
988     DCONNECT( this, inputChanged( input_thread_t * ),
989               im, setInput( input_thread_t * ) );
990
991     /* initialize p_input (an input can already be running) */
992     p_input = playlist_CurrentInput( pl_Get(p_intf) );
993     if( p_input )
994         emit inputChanged( p_input );
995
996     /* Audio Menu */
997     menusAudioMapper = new QSignalMapper();
998     CONNECT( menusAudioMapper, mapped(QString), this, menusUpdateAudio( QString ) );
999 }
1000
1001 MainInputManager::~MainInputManager()
1002 {
1003     if( p_input )
1004     {
1005        emit inputChanged( NULL );
1006        vlc_object_release( p_input );
1007     }
1008
1009     var_DelCallback( THEPL, "activity", PLItemChanged, this );
1010     var_DelCallback( THEPL, "item-change", ItemChanged, im );
1011     var_DelCallback( THEPL, "leaf-to-parent", LeafToParent, this );
1012
1013     var_DelCallback( THEPL, "playlist-item-append", PLItemAppended, this );
1014     var_DelCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
1015
1016     delete menusAudioMapper;
1017 }
1018
1019 vout_thread_t* MainInputManager::getVout()
1020 {
1021     return p_input ? input_GetVout( p_input ) : NULL;
1022 }
1023
1024 audio_output_t * MainInputManager::getAout()
1025 {
1026     return playlist_GetAout( THEPL );
1027 }
1028
1029 void MainInputManager::customEvent( QEvent *event )
1030 {
1031     int type = event->type();
1032
1033     PLEvent *plEv;
1034
1035     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
1036     switch( type )
1037     {
1038     case PLEvent::PLItemAppended:
1039         plEv = static_cast<PLEvent*>( event );
1040         emit playlistItemAppended( plEv->getItemId(), plEv->getParentId() );
1041         return;
1042     case PLEvent::PLItemRemoved:
1043         plEv = static_cast<PLEvent*>( event );
1044         emit playlistItemRemoved( plEv->getItemId() );
1045         return;
1046     case PLEvent::PLEmpty:
1047         plEv = static_cast<PLEvent*>( event );
1048         emit playlistNotEmpty( plEv->getItemId() >= 0 );
1049         return;
1050     case PLEvent::LeafToParent:
1051         plEv = static_cast<PLEvent*>( event );
1052         emit leafBecameParent( plEv->getItemId() );
1053         return;
1054     default:
1055         if( type != IMEvent::ItemChanged ) return;
1056     }
1057
1058     if( p_input != NULL )
1059         vlc_object_release( p_input );
1060     p_input = playlist_CurrentInput( pl_Get(p_intf) );
1061     emit inputChanged( p_input );
1062 }
1063
1064 /* Playlist Control functions */
1065 void MainInputManager::stop()
1066 {
1067    playlist_Stop( THEPL );
1068 }
1069
1070 void MainInputManager::next()
1071 {
1072    playlist_Next( THEPL );
1073 }
1074
1075 void MainInputManager::prev()
1076 {
1077    playlist_Prev( THEPL );
1078 }
1079
1080 void MainInputManager::prevOrReset()
1081 {
1082     if( !p_input || var_GetTime(  p_input , "time") < 10000 )
1083         playlist_Prev( THEPL );
1084     else
1085         getIM()->sliderUpdate( 0.0 );
1086 }
1087
1088 void MainInputManager::togglePlayPause()
1089 {
1090     /* No input, play */
1091     if( !p_input )
1092         playlist_Play( THEPL );
1093     else
1094         playlist_Pause( THEPL );
1095 }
1096
1097 void MainInputManager::play()
1098 {
1099     /* No input, play */
1100     if( !p_input )
1101         playlist_Play( THEPL );
1102     else
1103     {
1104         if( PLAYING_S != var_GetInteger( p_input, "state" ) )
1105         {
1106             playlist_Pause( THEPL );
1107         }
1108     }
1109 }
1110
1111 void MainInputManager::pause()
1112 {
1113     if(p_input && PLAYING_S == var_GetInteger( p_input, "state" ) )
1114     {
1115         playlist_Pause( THEPL );
1116     }
1117 }
1118
1119 void MainInputManager::toggleRandom()
1120 {
1121     config_PutInt( p_intf, "random", var_ToggleBool( THEPL, "random" ) );
1122 }
1123
1124 void MainInputManager::notifyRandom(bool value)
1125 {
1126     emit randomChanged(value);
1127 }
1128
1129 void MainInputManager::notifyRepeatLoop(bool)
1130 {
1131     int i_value = var_GetBool( THEPL, "loop" ) * REPEAT_ALL
1132               + var_GetBool( THEPL, "repeat" ) * REPEAT_ONE;
1133
1134     emit repeatLoopChanged( i_value );
1135 }
1136
1137 void MainInputManager::loopRepeatLoopStatus()
1138 {
1139     /* Toggle Normal -> Loop -> Repeat -> Normal ... */
1140     bool loop = var_GetBool( THEPL, "loop" );
1141     bool repeat = var_GetBool( THEPL, "repeat" );
1142
1143     if( repeat )
1144     {
1145         loop = false;
1146         repeat = false;
1147     }
1148     else if( loop )
1149     {
1150         loop = false;
1151         repeat = true;
1152     }
1153     else
1154     {
1155         loop = true;
1156         //repeat = false;
1157     }
1158
1159     var_SetBool( THEPL, "loop", loop );
1160     var_SetBool( THEPL, "repeat", repeat );
1161     config_PutInt( p_intf, "loop", loop );
1162     config_PutInt( p_intf, "repeat", repeat );
1163 }
1164
1165 void MainInputManager::activatePlayQuit( bool b_exit )
1166 {
1167     var_SetBool( THEPL, "play-and-exit", b_exit );
1168 }
1169
1170 bool MainInputManager::getPlayExitState()
1171 {
1172     return var_GetBool( THEPL, "play-and-exit" );
1173 }
1174
1175 bool MainInputManager::hasEmptyPlaylist()
1176 {
1177     playlist_Lock( THEPL );
1178     bool b_empty = playlist_IsEmpty( THEPL );
1179     playlist_Unlock( THEPL );
1180     return b_empty;
1181 }
1182
1183 /****************************
1184  * Static callbacks for MIM *
1185  ****************************/
1186 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
1187                         vlc_value_t oldval, vlc_value_t val, void *param )
1188 {
1189     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
1190     VLC_UNUSED( val );
1191
1192     MainInputManager *mim = (MainInputManager*)param;
1193
1194     IMEvent *event = new IMEvent( IMEvent::ItemChanged );
1195     QApplication::postEvent( mim, event );
1196     return VLC_SUCCESS;
1197 }
1198
1199 static int LeafToParent( vlc_object_t *p_this, const char *psz_var,
1200                         vlc_value_t oldval, vlc_value_t newval, void *param )
1201 {
1202     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
1203     MainInputManager *mim = (MainInputManager*)param;
1204
1205     PLEvent *event = new PLEvent( PLEvent::LeafToParent, newval.i_int );
1206
1207     QApplication::postEvent( mim, event );
1208     return VLC_SUCCESS;
1209 }
1210
1211 void MainInputManager::notifyVolume( float volume )
1212 {
1213     emit volumeChanged( volume );
1214 }
1215
1216 void MainInputManager::notifyMute( bool mute )
1217 {
1218     emit soundMuteChanged(mute);
1219 }
1220
1221
1222 void MainInputManager::menusUpdateAudio( const QString& data )
1223 {
1224     audio_output_t *aout = getAout();
1225     if( aout != NULL )
1226     {
1227         aout_DeviceSet( aout, qtu(data) );
1228         vlc_object_release( aout );
1229     }
1230 }
1231
1232 static int PLItemAppended
1233 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1234 {
1235     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old );
1236     MainInputManager *mim = static_cast<MainInputManager*>(data);
1237     playlist_add_t *p_add = static_cast<playlist_add_t*>( cur.p_address );
1238
1239     PLEvent *event = new PLEvent( PLEvent::PLItemAppended, p_add->i_item, p_add->i_node  );
1240     QApplication::postEvent( mim, event );
1241     event = new PLEvent( PLEvent::PLEmpty, p_add->i_item, 0  );
1242     QApplication::postEvent( mim, event );
1243     return VLC_SUCCESS;
1244 }
1245
1246 static int PLItemRemoved
1247 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1248 {
1249     VLC_UNUSED( var ); VLC_UNUSED( old );
1250
1251     playlist_t *pl = (playlist_t *) obj;
1252     MainInputManager *mim = static_cast<MainInputManager*>(data);
1253
1254     PLEvent *event = new PLEvent( PLEvent::PLItemRemoved, cur.i_int, 0  );
1255     QApplication::postEvent( mim, event );
1256     // can't use playlist_IsEmpty(  ) as it isn't true yet
1257     if ( pl->items.i_size == 1 ) // lock is held
1258     {
1259         event = new PLEvent( PLEvent::PLEmpty, -1, 0 );
1260         QApplication::postEvent( mim, event );
1261     }
1262     return VLC_SUCCESS;
1263 }