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