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