]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt: fill NULL-deref in jumping forward
[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 LoopChanged( vlc_object_t *, const char *,
62                         vlc_value_t, vlc_value_t, void * );
63 static int RepeatChanged( vlc_object_t *, const char *,
64                         vlc_value_t, vlc_value_t, void * );
65
66
67 static int InputEvent( vlc_object_t *, const char *,
68                        vlc_value_t, vlc_value_t, void * );
69 static int VbiEvent( vlc_object_t *, const char *,
70                      vlc_value_t, vlc_value_t, void * );
71
72
73 /**********************************************************************
74  * InputManager implementation
75  **********************************************************************
76  * The Input Manager can be the main one around the playlist
77  * But can also be used for VLM dialog or similar
78  **********************************************************************/
79
80 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
81                            QObject( parent ), p_intf( _p_intf )
82 {
83     i_old_playing_status = END_S;
84     oldName      = "";
85     artUrl       = "";
86     p_input      = NULL;
87     p_input_vbi  = NULL;
88     f_rate       = 0.;
89     p_item       = NULL;
90     b_video      = false;
91     timeA        = 0;
92     timeB        = 0;
93     f_cache      = -1.; /* impossible initial value, different from all */
94 }
95
96 InputManager::~InputManager()
97 {
98     delInput();
99 }
100
101 /* Define the Input used.
102    Add the callbacks on input
103    p_input is held once here */
104 void InputManager::setInput( input_thread_t *_p_input )
105 {
106     delInput();
107     p_input = _p_input;
108     if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
109     {
110         msg_Dbg( p_intf, "IM: Setting an input" );
111         vlc_object_hold( p_input );
112         addCallbacks();
113         UpdateStatus();
114         UpdateName();
115         UpdateArt();
116         UpdateTeletext();
117         UpdateNavigation();
118         UpdateVout();
119
120         p_item = input_GetItem( p_input );
121         emit rateChanged( var_GetFloat( p_input, "rate" ) );
122     }
123     else
124     {
125         p_input = NULL;
126         p_item = NULL;
127         assert( !p_input_vbi );
128         emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
129     }
130 }
131
132 /* delete Input if it ever existed.
133    Delete the callbacls on input
134    p_input is released once here */
135 void InputManager::delInput()
136 {
137     if( !p_input ) return;
138     msg_Dbg( p_intf, "IM: Deleting the input" );
139
140     delCallbacks();
141     i_old_playing_status = END_S;
142     p_item               = NULL;
143     oldName              = "";
144     artUrl               = "";
145     b_video              = false;
146     timeA                = 0;
147     timeB                = 0;
148     f_rate               = 0. ;
149
150     if( p_input_vbi )
151     {
152         vlc_object_release( p_input_vbi );
153         p_input_vbi = NULL;
154     }
155
156     vlc_object_release( p_input );
157     p_input = NULL;
158
159     emit positionUpdated( -1.0, 0 ,0 );
160     emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
161     emit nameChanged( "" );
162     emit chapterChanged( 0 );
163     emit titleChanged( 0 );
164     emit playingStatusChanged( END_S );
165
166     emit teletextPossible( false );
167     emit AtoBchanged( false, false );
168     emit voutChanged( false );
169     emit voutListChanged( NULL, 0 );
170
171     /* Reset all InfoPanels but stats */
172     emit artChanged( NULL );
173     emit infoChanged( NULL );
174     emit currentMetaChanged( (input_item_t *)NULL );
175
176     emit encryptionChanged( false );
177     emit recordingStateChanged( false );
178
179     emit cachingChanged( 1 );
180 }
181
182 /* Convert the event from the callbacks in actions */
183 void InputManager::customEvent( QEvent *event )
184 {
185     int i_type = event->type();
186     IMEvent *ple = static_cast<IMEvent *>(event);
187
188     if( i_type == ItemChanged_Type )
189         UpdateMeta( ple->p_item );
190
191     if( !hasInput() )
192         return;
193
194     /* Actions */
195     switch( i_type )
196     {
197     case PositionUpdate_Type:
198         UpdatePosition();
199         break;
200     case StatisticsUpdate_Type:
201         UpdateStats();
202         break;
203     case ItemChanged_Type:
204         /* Ignore ItemChanged_Type event that does not apply to our input */
205         if( p_item == ple->p_item )
206         {
207             UpdateStatus();
208             // UpdateName();
209             UpdateArt();
210             /* Update duration of file */
211         }
212         break;
213     case ItemStateChanged_Type:
214         // TODO: Fusion with above state
215         UpdateStatus();
216         // UpdateName();
217         // UpdateNavigation(); This shouldn't be useful now
218         // UpdateTeletext(); Same
219         break;
220     case NameChanged_Type:
221         UpdateName();
222         break;
223     case MetaChanged_Type:
224         UpdateMeta();
225         UpdateName(); /* Needed for NowPlaying */
226         UpdateArt(); /* Art is part of meta in the core */
227         break;
228     case InfoChanged_Type:
229         UpdateInfo();
230         break;
231     case ItemTitleChanged_Type:
232         UpdateNavigation();
233         UpdateName(); /* Display the name of the Chapter, if exists */
234         break;
235     case ItemRateChanged_Type:
236         UpdateRate();
237         break;
238     case ItemEsChanged_Type:
239         UpdateTeletext();
240         // We don't do anything ES related. Why ?
241         break;
242     case ItemTeletextChanged_Type:
243         UpdateTeletext();
244         break;
245     case InterfaceVoutUpdate_Type:
246         UpdateVout();
247         break;
248     case SynchroChanged_Type:
249         emit synchroChanged();
250         break;
251     case CachingEvent_Type:
252         UpdateCaching();
253         break;
254     case BookmarksChanged_Type:
255         emit bookmarksChanged();
256         break;
257     case InterfaceAoutUpdate_Type:
258         UpdateAout();
259         break;
260     case RecordingEvent_Type:
261         UpdateRecord();
262         break;
263     case ProgramChanged_Type:
264         UpdateProgramEvent();
265         break;
266     case EPGEvent_Type:
267         UpdateEPG();
268         break;
269     default:
270         msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
271         assert(0);
272     }
273 }
274
275 /* Add the callbacks on Input. Self explanatory */
276 inline void InputManager::addCallbacks()
277 {
278     var_AddCallback( p_input, "intf-event", InputEvent, this );
279 }
280
281 /* Delete the callbacks on Input. Self explanatory */
282 inline void InputManager::delCallbacks()
283 {
284     var_DelCallback( p_input, "intf-event", InputEvent, this );
285 }
286
287 /* Static callbacks for IM */
288 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
289                         vlc_value_t oldval, vlc_value_t newval, void *param )
290 {
291     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
292
293     InputManager *im = (InputManager*)param;
294     input_item_t *p_item = static_cast<input_item_t *>(newval.p_address);
295
296     IMEvent *event = new IMEvent( ItemChanged_Type, p_item );
297     QApplication::postEvent( im, event );
298     return VLC_SUCCESS;
299 }
300
301 static int InputEvent( vlc_object_t *p_this, const char *,
302                        vlc_value_t, vlc_value_t newval, void *param )
303 {
304     VLC_UNUSED( p_this );
305
306     InputManager *im = (InputManager*)param;
307     IMEvent *event;
308
309     switch( newval.i_int )
310     {
311     case INPUT_EVENT_STATE:
312         event = new IMEvent( ItemStateChanged_Type );
313         break;
314     case INPUT_EVENT_RATE:
315         event = new IMEvent( ItemRateChanged_Type );
316         break;
317     case INPUT_EVENT_POSITION:
318     //case INPUT_EVENT_LENGTH:
319         event = new IMEvent( PositionUpdate_Type );
320         break;
321
322     case INPUT_EVENT_TITLE:
323     case INPUT_EVENT_CHAPTER:
324         event = new IMEvent( ItemTitleChanged_Type );
325         break;
326
327     case INPUT_EVENT_ES:
328         event = new IMEvent( ItemEsChanged_Type );
329         break;
330     case INPUT_EVENT_TELETEXT:
331         event = new IMEvent( ItemTeletextChanged_Type );
332         break;
333
334     case INPUT_EVENT_STATISTICS:
335         event = new IMEvent( StatisticsUpdate_Type );
336         break;
337
338     case INPUT_EVENT_VOUT:
339         event = new IMEvent( InterfaceVoutUpdate_Type );
340         break;
341     case INPUT_EVENT_AOUT:
342         event = new IMEvent( InterfaceAoutUpdate_Type );
343         break;
344
345     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
346         event = new IMEvent( MetaChanged_Type );
347         break;
348     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
349         event = new IMEvent( InfoChanged_Type );
350         break;
351     case INPUT_EVENT_ITEM_NAME:
352         event = new IMEvent( NameChanged_Type );
353         break;
354
355     case INPUT_EVENT_AUDIO_DELAY:
356     case INPUT_EVENT_SUBTITLE_DELAY:
357         event = new IMEvent( SynchroChanged_Type );
358         break;
359
360     case INPUT_EVENT_CACHE:
361         event = new IMEvent( CachingEvent_Type );
362         break;
363
364     case INPUT_EVENT_BOOKMARK:
365         event = new IMEvent( BookmarksChanged_Type );
366         break;
367
368     case INPUT_EVENT_RECORD:
369         event = new IMEvent( RecordingEvent_Type );
370         break;
371
372     case INPUT_EVENT_PROGRAM:
373         /* This is for PID changes */
374         event = new IMEvent( ProgramChanged_Type );
375         break;
376
377     case INPUT_EVENT_ITEM_EPG:
378         /* EPG data changed */
379         event = new IMEvent( EPGEvent_Type );
380         break;
381
382     case INPUT_EVENT_SIGNAL:
383         /* This is for capture-card signals */
384         /* event = new IMEvent( SignalChanged_Type );
385         break; */
386     default:
387         event = NULL;
388         break;
389     }
390
391     if( event )
392         QApplication::postEvent( im, event );
393     return VLC_SUCCESS;
394 }
395
396 static int VbiEvent( vlc_object_t *, const char *,
397                      vlc_value_t, vlc_value_t, void *param )
398 {
399     InputManager *im = (InputManager*)param;
400     IMEvent *event = new IMEvent( ItemTeletextChanged_Type );
401
402     QApplication::postEvent( im, event );
403     return VLC_SUCCESS;
404 }
405
406 void InputManager::UpdatePosition()
407 {
408     /* Update position */
409     int i_length;
410     int64_t i_time;
411     float f_pos;
412     i_length = var_GetTime(  p_input , "length" ) / CLOCK_FREQ;
413     i_time = var_GetTime(  p_input , "time");
414     f_pos = var_GetFloat(  p_input , "position" );
415     emit positionUpdated( f_pos, i_time, i_length );
416 }
417
418 void InputManager::UpdateNavigation()
419 {
420     /* Update navigation status */
421     vlc_value_t val; val.i_int = 0;
422     vlc_value_t val2; val2.i_int = 0;
423
424     if( hasInput() )
425         var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
426
427     if( val.i_int > 0 )
428     {
429         emit titleChanged( true );
430         msg_Dbg( p_intf, "Title %"PRId64, val.i_int );
431         /* p_input != NULL since val.i_int != 0 */
432         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val2, NULL );
433         emit chapterChanged( (val2.i_int > 1) || ( val2.i_int > 0 && val.i_int > 1 ) );
434         msg_Dbg( p_intf, "Chapter: %"PRId64, val2.i_int );
435     }
436     else
437         emit titleChanged( false );
438 }
439
440 void InputManager::UpdateStatus()
441 {
442     /* Update playing status */
443     int state = var_GetInteger( p_input, "state" );
444     if( i_old_playing_status != state )
445     {
446         i_old_playing_status = state;
447         emit playingStatusChanged( state );
448     }
449 }
450
451 void InputManager::UpdateRate()
452 {
453     /* Update Rate */
454     float f_new_rate = var_GetFloat( p_input, "rate" );
455     if( f_new_rate != f_rate )
456     {
457         f_rate = f_new_rate;
458         /* Update rate */
459         emit rateChanged( f_rate );
460     }
461 }
462
463 void InputManager::UpdateName()
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( p_input, format );
471     free( format );
472     name = qfu(formated);
473     free( formated );
474
475     /* If we have Nothing */
476     if( name.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()
639 {
640     if( hasInput() )
641     {
642         playlist_AskForArtEnqueue( pl_Get(p_intf), input_GetItem( p_input ) );
643     }
644     else
645     {
646         /* No input will signal the cover art to update,
647          * let's do it ourself */
648         UpdateArt();
649     }
650 }
651
652 const QString InputManager::decodeArtURL( input_item_t *p_item )
653 {
654     assert( p_item );
655
656     char *psz_art = input_item_GetArtURL( p_item );
657     if( psz_art )
658     {
659         char *psz = make_path( psz_art );
660         free( psz_art );
661         psz_art = psz;
662     }
663
664 #if 0
665     /* Taglib seems to define a attachment://, It won't work yet */
666     url = url.replace( "attachment://", "" );
667 #endif
668
669     QString path = qfu( psz_art ? psz_art : "" );
670     free( psz_art );
671     return path;
672 }
673
674 void InputManager::UpdateArt()
675 {
676     QString url;
677
678     if( hasInput() )
679         url = decodeArtURL( input_GetItem( p_input ) );
680
681     /* the art hasn't changed, no need to update */
682     if(artUrl == url)
683         return;
684
685     /* Update Art meta */
686     artUrl = url;
687     emit artChanged( artUrl );
688 }
689
690 inline void InputManager::UpdateStats()
691 {
692     emit statisticsUpdated( input_GetItem( p_input ) );
693 }
694
695 inline void InputManager::UpdateMeta( input_item_t *p_item )
696 {
697     emit metaChanged( p_item );
698 }
699
700 inline void InputManager::UpdateMeta()
701 {
702     emit currentMetaChanged( input_GetItem( p_input ) );
703 }
704
705 inline void InputManager::UpdateInfo()
706 {
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 /* User togglePlayPause */
736 void InputManager::togglePlayPause()
737 {
738     if( hasInput() )
739     {
740         int state = var_GetInteger( p_input, "state" );
741         state = ( state != PLAYING_S ) ? PLAYING_S : PAUSE_S;
742         var_SetInteger( p_input, "state", state );
743     }
744 }
745
746 void InputManager::sectionPrev()
747 {
748     if( hasInput() )
749     {
750         int i_type = var_Type( p_input, "next-chapter" );
751         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
752                              "prev-chapter":"prev-title" );
753     }
754 }
755
756 void InputManager::sectionNext()
757 {
758     if( hasInput() )
759     {
760         int i_type = var_Type( p_input, "next-chapter" );
761         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
762                              "next-chapter":"next-title" );
763     }
764 }
765
766 void InputManager::sectionMenu()
767 {
768     if( hasInput() )
769     {
770         vlc_value_t val, text;
771
772         if( var_Change( p_input, "title  0", VLC_VAR_GETLIST, &val, &text ) < 0 )
773             return;
774
775         /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
776         int root = 0;
777         for( int i = 0; i < val.p_list->i_count; i++ )
778         {
779             if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
780                 root = i;
781         }
782         var_FreeList( &val, &text );
783
784         var_SetInteger( p_input, "title  0", root );
785     }
786 }
787
788 /*
789  *  Teletext Functions
790  */
791
792 /* Set a new Teletext Page */
793 void InputManager::telexSetPage( int page )
794 {
795     if( hasInput() && p_input_vbi )
796     {
797         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
798
799         if( i_teletext_es >= 0 )
800         {
801             var_SetInteger( p_input_vbi, "vbi-page", page );
802             emit newTelexPageSet( page );
803         }
804     }
805 }
806
807 /* Set the transparency on teletext */
808 void InputManager::telexSetTransparency( bool b_transparentTelextext )
809 {
810     if( hasInput() && p_input_vbi )
811     {
812         var_SetBool( p_input_vbi, "vbi-opaque", !b_transparentTelextext );
813         emit teletextTransparencyActivated( b_transparentTelextext );
814     }
815 }
816
817 void InputManager::activateTeletext( bool b_enable )
818 {
819     vlc_value_t list;
820     vlc_value_t text;
821     if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETLIST, &list, &text ) )
822     {
823         if( list.p_list->i_count > 0 )
824         {
825             /* Prefer the page 100 if it is present */
826             int i;
827             for( i = 0; i < text.p_list->i_count; i++ )
828             {
829                 /* The description is the page number as a string */
830                 const char *psz_page = text.p_list->p_values[i].psz_string;
831                 if( psz_page && !strcmp( psz_page, "100" ) )
832                     break;
833             }
834             if( i >= list.p_list->i_count )
835                 i = 0;
836             var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
837         }
838         var_FreeList( &list, &text );
839     }
840 }
841
842 void InputManager::reverse()
843 {
844     if( hasInput() )
845     {
846         float f_rate = var_GetFloat( p_input, "rate" );
847         var_SetFloat( p_input, "rate", -f_rate );
848     }
849 }
850
851 void InputManager::slower()
852 {
853     var_TriggerCallback( THEPL, "rate-slower" );
854 }
855
856 void InputManager::faster()
857 {
858     var_TriggerCallback( THEPL, "rate-faster" );
859 }
860
861 void InputManager::littlefaster()
862 {
863     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_RATE_FASTER_FINE );
864 }
865
866 void InputManager::littleslower()
867 {
868     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_RATE_SLOWER_FINE );
869 }
870
871 void InputManager::normalRate()
872 {
873     var_SetFloat( THEPL, "rate", 1. );
874 }
875
876 void InputManager::setRate( int new_rate )
877 {
878     var_SetFloat( THEPL, "rate",
879                  (float)INPUT_RATE_DEFAULT / (float)new_rate );
880 }
881
882 void InputManager::jumpFwd()
883 {
884     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
885     if( i_interval > 0 && hasInput() )
886     {
887         mtime_t val = CLOCK_FREQ * i_interval;
888         var_SetTime( p_input, "time-offset", val );
889     }
890 }
891
892 void InputManager::jumpBwd()
893 {
894     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
895     if( i_interval > 0 && hasInput() )
896     {
897         mtime_t val = -CLOCK_FREQ * i_interval;
898         var_SetTime( p_input, "time-offset", val );
899     }
900 }
901
902 void InputManager::setAtoB()
903 {
904     if( !timeA )
905     {
906         timeA = var_GetTime( THEMIM->getInput(), "time"  );
907     }
908     else if( !timeB )
909     {
910         timeB = var_GetTime( THEMIM->getInput(), "time"  );
911         var_SetTime( THEMIM->getInput(), "time" , timeA );
912         CONNECT( this, positionUpdated( float, int64_t, int ),
913                  this, AtoBLoop( float, int64_t, int ) );
914     }
915     else
916     {
917         timeA = 0;
918         timeB = 0;
919         disconnect( this, SIGNAL( positionUpdated( float, int64_t, int ) ),
920                     this, SLOT( AtoBLoop( float, int64_t, int ) ) );
921     }
922     emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
923 }
924
925 /* Function called regularly when in an AtoB loop */
926 void InputManager::AtoBLoop( float, int64_t i_time, int )
927 {
928     if( timeB )
929     {
930         if( i_time >= timeB || i_time < timeA )
931             var_SetTime( THEMIM->getInput(), "time" , timeA );
932     }
933 }
934
935 /**********************************************************************
936  * MainInputManager implementation. Wrap an input manager and
937  * take care of updating the main playlist input.
938  * Used in the main playlist Dialog
939  **********************************************************************/
940
941 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
942                  : QObject(NULL), p_intf( _p_intf )
943 {
944     p_input = NULL;
945     im = new InputManager( this, p_intf );
946
947     var_AddCallback( THEPL, "item-change", ItemChanged, im );
948     var_AddCallback( THEPL, "item-current", PLItemChanged, this );
949     var_AddCallback( THEPL, "activity", PLItemChanged, this );
950     var_AddCallback( THEPL, "leaf-to-parent", LeafToParent, this );
951     var_AddCallback( THEPL, "playlist-item-append", PLItemAppended, this );
952     var_AddCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
953     var_AddCallback( THEPL, "random", RandomChanged, this );
954     var_AddCallback( THEPL, "repeat", RepeatChanged, this );
955     var_AddCallback( THEPL, "loop", LoopChanged, this );
956
957     var_AddCallback( THEPL, "volume", VolumeChanged, this );
958     var_AddCallback( THEPL, "mute", SoundMuteChanged, this );
959
960     /* Warn our embedded IM about input changes */
961     DCONNECT( this, inputChanged( input_thread_t * ),
962               im, setInput( input_thread_t * ) );
963
964 }
965
966 MainInputManager::~MainInputManager()
967 {
968     if( p_input )
969     {
970        emit inputChanged( NULL );
971        var_DelCallback( p_input, "state", PLItemChanged, this );
972        vlc_object_release( p_input );
973     }
974
975     var_DelCallback( THEPL, "volume", VolumeChanged, this );
976     var_DelCallback( THEPL, "mute", SoundMuteChanged, this );
977
978     var_DelCallback( THEPL, "activity", PLItemChanged, this );
979     var_DelCallback( THEPL, "item-change", ItemChanged, im );
980     var_DelCallback( THEPL, "leaf-to-parent", LeafToParent, this );
981
982     var_DelCallback( THEPL, "item-current", PLItemChanged, this );
983     var_DelCallback( THEPL, "playlist-item-append", PLItemAppended, this );
984     var_DelCallback( THEPL, "playlist-item-deleted", PLItemRemoved, this );
985     var_DelCallback( THEPL, "random", RandomChanged, this );
986     var_DelCallback( THEPL, "repeat", RepeatChanged, this );
987     var_DelCallback( THEPL, "loop", LoopChanged, this );
988
989     /* Save some interface state in configuration, at module quit */
990     config_PutInt( p_intf, "random", var_GetBool( THEPL, "random" ) );
991     config_PutInt( p_intf, "loop", var_GetBool( THEPL, "loop" ) );
992     config_PutInt( p_intf, "repeat", var_GetBool( THEPL, "repeat" ) );
993
994     if( var_InheritBool( p_intf, "qt-autosave-volume" ) )
995         config_PutInt( p_intf, "volume", aout_VolumeGet( THEPL ) );
996 }
997
998 vout_thread_t* MainInputManager::getVout()
999 {
1000     return p_input ? input_GetVout( p_input ) : NULL;
1001 }
1002
1003 audio_output_t * MainInputManager::getAout()
1004 {
1005     return p_input ? input_GetAout( p_input ) : NULL;
1006 }
1007
1008 void MainInputManager::customEvent( QEvent *event )
1009 {
1010     int type = event->type();
1011
1012     PLEvent *plEv;
1013
1014     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
1015     switch( type )
1016     {
1017     case VolumeChanged_Type:
1018         emit volumeChanged();
1019         return;
1020     case SoundMuteChanged_Type:
1021         emit soundMuteChanged();
1022         return;
1023     case PLItemAppended_Type:
1024         plEv = static_cast<PLEvent*>( event );
1025         emit playlistItemAppended( plEv->i_item, plEv->i_parent );
1026         return;
1027     case PLItemRemoved_Type:
1028         plEv = static_cast<PLEvent*>( event );
1029         emit playlistItemRemoved( plEv->i_item );
1030         return;
1031     case RandomChanged_Type:
1032         emit randomChanged( var_GetBool( THEPL, "random" ) );
1033         return;
1034     case LoopChanged_Type:
1035     case RepeatChanged_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         getIM()->togglePlayPause();
1116 }
1117
1118 void MainInputManager::play()
1119 {
1120     /* No input, play */
1121     if( !p_input )
1122         playlist_Play( THEPL );
1123     else
1124     {
1125         if( PLAYING_S != var_GetInteger( p_input, "state" ) )
1126         {
1127             getIM()->togglePlayPause();
1128         }
1129     }
1130 }
1131
1132 void MainInputManager::pause()
1133 {
1134     if(p_input && PLAYING_S == var_GetInteger( p_input, "state" ) )
1135     {
1136         getIM()->togglePlayPause();
1137     }
1138 }
1139
1140 void MainInputManager::toggleRandom()
1141 {
1142     var_ToggleBool( THEPL, "random" );
1143 }
1144
1145 void MainInputManager::notifyRepeatLoop()
1146 {
1147     int i_value = var_GetBool( THEPL, "loop" ) * REPEAT_ALL
1148               + var_GetBool( THEPL, "repeat" ) * REPEAT_ONE;
1149
1150     emit repeatLoopChanged( i_value );
1151 }
1152
1153 void MainInputManager::loopRepeatLoopStatus()
1154 {
1155     /* Toggle Normal -> Loop -> Repeat -> Normal ... */
1156     if( var_GetBool( THEPL, "repeat" ) )
1157         var_SetBool( THEPL, "repeat", false );
1158     else if( var_GetBool( THEPL, "loop" ) )
1159     {
1160         var_SetBool( THEPL, "loop", false );
1161         var_SetBool( THEPL, "repeat", true );
1162     }
1163     else
1164         var_SetBool( THEPL, "loop", true );
1165 }
1166
1167 void MainInputManager::activatePlayQuit( bool b_exit )
1168 {
1169     var_SetBool( THEPL, "play-and-exit", b_exit );
1170 }
1171
1172 bool MainInputManager::getPlayExitState()
1173 {
1174     return var_GetBool( THEPL, "play-and-exit" );
1175 }
1176
1177 /****************************
1178  * Static callbacks for MIM *
1179  ****************************/
1180 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
1181                         vlc_value_t oldval, vlc_value_t, void *param )
1182 {
1183     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
1184
1185     MainInputManager *mim = (MainInputManager*)param;
1186
1187     IMEvent *event = new IMEvent( ItemChanged_Type );
1188     QApplication::postEvent( mim, event );
1189     return VLC_SUCCESS;
1190 }
1191
1192 static int LeafToParent( vlc_object_t *p_this, const char *psz_var,
1193                         vlc_value_t oldval, vlc_value_t newval, void *param )
1194 {
1195     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval );
1196     MainInputManager *mim = (MainInputManager*)param;
1197
1198     PLEvent *event = new PLEvent( LeafToParent_Type, newval.i_int );
1199
1200     QApplication::postEvent( mim, event );
1201     return VLC_SUCCESS;
1202 }
1203
1204 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
1205                         vlc_value_t oldval, vlc_value_t newval, void *param )
1206 {
1207     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval ); VLC_UNUSED( newval );
1208
1209     MainInputManager *mim = (MainInputManager*)param;
1210
1211     IMEvent *event = new IMEvent( VolumeChanged_Type );
1212     QApplication::postEvent( mim, event );
1213     return VLC_SUCCESS;
1214 }
1215
1216 static int SoundMuteChanged( vlc_object_t *p_this, const char *psz_var,
1217                         vlc_value_t oldval, vlc_value_t newval, void *param )
1218 {
1219     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); VLC_UNUSED( oldval ); VLC_UNUSED( newval );
1220
1221     MainInputManager *mim = (MainInputManager*)param;
1222
1223     IMEvent *event = new IMEvent( SoundMuteChanged_Type );
1224     QApplication::postEvent( mim, event );
1225     return VLC_SUCCESS;
1226 }
1227
1228 static int PLItemAppended
1229 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1230 {
1231     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old );
1232
1233     MainInputManager *mim = static_cast<MainInputManager*>(data);
1234     playlist_add_t *p_add = static_cast<playlist_add_t*>( cur.p_address );
1235
1236     PLEvent *event = new PLEvent( PLItemAppended_Type, p_add->i_item, p_add->i_node  );
1237     QApplication::postEvent( mim, event );
1238     return VLC_SUCCESS;
1239 }
1240 static int PLItemRemoved
1241 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1242 {
1243     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old );
1244
1245     MainInputManager *mim = static_cast<MainInputManager*>(data);
1246
1247     PLEvent *event = new PLEvent( PLItemRemoved_Type, cur.i_int, 0  );
1248     QApplication::postEvent( mim, event );
1249     return VLC_SUCCESS;
1250 }
1251
1252 static int RandomChanged
1253 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1254 {
1255     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old ); VLC_UNUSED( cur );
1256
1257     MainInputManager *mim = static_cast<MainInputManager*>(data);
1258
1259     IMEvent *event = new IMEvent( RandomChanged_Type );
1260     QApplication::postEvent( mim, event );
1261     return VLC_SUCCESS;
1262 }
1263
1264 /* Probably could be merged with next callback */
1265 static int LoopChanged
1266 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1267 {
1268     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old ); VLC_UNUSED( cur );
1269
1270     MainInputManager *mim = static_cast<MainInputManager*>(data);
1271
1272     IMEvent *event = new IMEvent( LoopChanged_Type );
1273     QApplication::postEvent( mim, event );
1274     return VLC_SUCCESS;
1275 }
1276
1277 static int RepeatChanged
1278 ( vlc_object_t * obj, const char *var, vlc_value_t old, vlc_value_t cur, void *data )
1279 {
1280     VLC_UNUSED( obj ); VLC_UNUSED( var ); VLC_UNUSED( old ); VLC_UNUSED( cur );
1281
1282     MainInputManager *mim = static_cast<MainInputManager*>(data);
1283
1284     IMEvent *event = new IMEvent( RepeatChanged_Type );
1285     QApplication::postEvent( mim, event );
1286     return VLC_SUCCESS;
1287 }