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