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