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