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