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