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