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