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