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