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