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