]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt: Don't post the initial event of THEMIM, force it.
[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
32 #include <QApplication>
33
34 #include <assert.h>
35
36 static int ItemChanged( vlc_object_t *, const char *,
37                         vlc_value_t, vlc_value_t, void * );
38 static int PLItemChanged( vlc_object_t *, const char *,
39                         vlc_value_t, vlc_value_t, void * );
40 static int VolumeChanged( vlc_object_t *, const char *,
41                         vlc_value_t, vlc_value_t, void * );
42
43 static int InputEvent( vlc_object_t *, const char *,
44                        vlc_value_t, vlc_value_t, void * );
45
46
47 /**********************************************************************
48  * InputManager implementation
49  **********************************************************************
50  * The Input Manager can be the main one around the playlist
51  * But can also be used for VLM dialog or similar
52  **********************************************************************/
53
54 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
55                            QObject( parent ), p_intf( _p_intf )
56 {
57     i_old_playing_status = END_S;
58     oldName      = "";
59     artUrl       = "";
60     p_input      = NULL;
61     i_rate       = 0;
62     i_input_id   = 0;
63     b_video      = false;
64     timeA        = 0;
65     timeB        = 0;
66
67 }
68
69 InputManager::~InputManager()
70 {
71     delInput();
72 }
73
74 /* Define the Input used.
75    Add the callbacks on input
76    p_input is held once here */
77 void InputManager::setInput( input_thread_t *_p_input )
78 {
79     delInput();
80     p_input = _p_input;
81     if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
82     {
83         msg_Dbg( p_intf, "IM: Setting an input" );
84         vlc_object_hold( p_input );
85         emit statusChanged( PLAYING_S );
86         UpdateName();
87         UpdateArt();
88         UpdateTeletext();
89         UpdateNavigation();
90         UpdateVout();
91         addCallbacks();
92         i_input_id = input_GetItem( p_input )->i_id;
93     }
94     else
95     {
96         p_input = NULL;
97         i_input_id = 0;
98         emit rateChanged( INPUT_RATE_DEFAULT );
99     }
100 }
101
102 /* delete Input if it ever existed.
103    Delete the callbacls on input
104    p_input is released once here */
105 void InputManager::delInput()
106 {
107     if( !p_input ) return;
108     msg_Dbg( p_intf, "IM: Deleting the input" );
109
110     delCallbacks();
111     i_old_playing_status = END_S;
112     i_input_id           = 0;
113     oldName              = "";
114     artUrl               = "";
115     b_video              = false;
116     timeA                = 0;
117     timeB                = 0;
118
119     vlc_object_release( p_input );
120     p_input = NULL;
121
122     emit positionUpdated( -1.0, 0 ,0 );
123     emit rateChanged( INPUT_RATE_DEFAULT ); /* TODO: Do we want this ? */
124     emit nameChanged( "" );
125     emit chapterChanged( 0 );
126     emit titleChanged( 0 );
127     emit statusChanged( END_S );
128
129     emit teletextPossible( false );
130     emit AtoBchanged( false, false );
131     emit voutChanged( false );
132     emit voutListChanged( NULL, 0 );
133
134     /* Reset all InfoPanels but stats */
135     emit artChanged( NULL );
136     emit infoChanged( NULL );
137     emit metaChanged( NULL );
138 }
139
140 /* Convert the event from the callbacks in actions */
141 void InputManager::customEvent( QEvent *event )
142 {
143     int i_type = event->type();
144     IMEvent *ple = static_cast<IMEvent *>(event);
145
146     assert( !( i_type != PositionUpdate_Type &&
147          i_type != ItemChanged_Type &&
148          i_type != ItemRateChanged_Type &&
149          i_type != ItemTitleChanged_Type &&
150          i_type != ItemEsChanged_Type &&
151          i_type != ItemTeletextChanged_Type &&
152          i_type != ItemStateChanged_Type &&
153          i_type != StatisticsUpdate_Type &&
154          i_type != InterfaceVoutUpdate_Type &&
155          i_type != MetaChanged_Type &&
156          i_type != NameChanged_Type &&
157          i_type != InfoChanged_Type &&
158          i_type != SynchroChanged_Type &&
159          i_type != CachingEvent_Type &&
160          i_type != BookmarksChanged_Type &&
161          i_type != InterfaceAoutUpdate_Type &&
162          i_type != RecordingEvent_Type ) );
163
164     if( !hasInput() ) return;
165
166     if( i_type == CachingEvent_Type )
167         UpdateCaching();
168
169     if( ( i_type != PositionUpdate_Type  &&
170           i_type != ItemRateChanged_Type &&
171           i_type != ItemEsChanged_Type &&
172           i_type != ItemTeletextChanged_Type &&
173           i_type != ItemStateChanged_Type &&
174           i_type != StatisticsUpdate_Type &&
175           i_type != InterfaceVoutUpdate_Type &&
176           i_type != MetaChanged_Type &&
177           i_type != NameChanged_Type &&
178           i_type != InfoChanged_Type &&
179           i_type != SynchroChanged_Type &&
180           i_type != BookmarksChanged_Type &&
181           i_type != InterfaceAoutUpdate_Type &&
182           i_type != RecordingEvent_Type
183         )
184         && ( i_input_id != ple->i_id ) )
185         return;
186
187 #ifndef NDEBUG
188     if( i_type != PositionUpdate_Type &&
189         i_type != StatisticsUpdate_Type )
190         msg_Dbg( p_intf, "New Event: type %i", i_type );
191 #endif
192
193     /* Actions */
194     switch( i_type )
195     {
196     case PositionUpdate_Type:
197         UpdatePosition();
198         break;
199     case StatisticsUpdate_Type:
200         UpdateStats();
201         break;
202     case ItemChanged_Type:
203         UpdateStatus();
204         // UpdateName();
205         UpdateArt();
206         break;
207     case ItemStateChanged_Type:
208         // TODO: Fusion with above state
209         UpdateStatus();
210         // UpdateName();
211         // UpdateNavigation(); This shouldn't be useful now
212         // UpdateTeletext(); Same
213         break;
214     case NameChanged_Type:
215         UpdateName();
216         break;
217     case MetaChanged_Type:
218         UpdateMeta();
219         UpdateName(); /* Needed for NowPlaying */
220         UpdateArt(); /* Art is part of meta in the core */
221         break;
222     case InfoChanged_Type:
223         UpdateInfo();
224         break;
225     case ItemTitleChanged_Type:
226         UpdateNavigation();
227         UpdateName(); /* Display the name of the Chapter, if exists */
228         break;
229     case ItemRateChanged_Type:
230         UpdateRate();
231         break;
232     case ItemEsChanged_Type:
233         // We don't do anything. Why ?
234         break;
235     case ItemTeletextChanged_Type:
236         UpdateTeletext();
237         break;
238     case InterfaceVoutUpdate_Type:
239         UpdateVout();
240         break;
241     case SynchroChanged_Type:
242         emit synchroChanged();
243         break;
244     case CachingEvent_Type:
245         UpdateCaching();
246         break;
247     case BookmarksChanged_Type:
248         emit bookmarksChanged();
249         break;
250     case InterfaceAoutUpdate_Type:
251         UpdateAout();
252         break;
253     case RecordingEvent_Type:
254         UpdateRecord();
255         break;
256     default:
257         msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
258     }
259 }
260
261 /* Add the callbacks on Input. Self explanatory */
262 inline void InputManager::addCallbacks()
263 {
264     var_AddCallback( p_input, "intf-event", InputEvent, this );
265 }
266
267 /* Delete the callbacks on Input. Self explanatory */
268 inline void InputManager::delCallbacks()
269 {
270     var_DelCallback( p_input, "intf-event", InputEvent, this );
271 }
272
273 /* Static callbacks for IM */
274 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
275                         vlc_value_t oldval, vlc_value_t newval, void *param )
276 {
277     InputManager *im = (InputManager*)param;
278
279     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
280     QApplication::postEvent( im, static_cast<QEvent*>(event) );
281     return VLC_SUCCESS;
282 }
283
284 static int InputEvent( vlc_object_t *p_this, const char *,
285                        vlc_value_t, vlc_value_t newval, void *param )
286 {
287     InputManager *im = (InputManager*)param;
288     IMEvent *event;
289
290     switch( newval.i_int )
291     {
292     case INPUT_EVENT_STATE:
293         event = new IMEvent( ItemStateChanged_Type, 0 );
294         break;
295     case INPUT_EVENT_RATE:
296         event = new IMEvent( ItemRateChanged_Type, 0 );
297         break;
298     case INPUT_EVENT_TIMES:
299         event = new IMEvent( PositionUpdate_Type, 0 );
300         break;
301
302     case INPUT_EVENT_TITLE:
303     case INPUT_EVENT_CHAPTER:
304         event = new IMEvent( ItemTitleChanged_Type, 0 );
305         break;
306
307     case INPUT_EVENT_ES:
308         event = new IMEvent( ItemEsChanged_Type, 0 );
309         break;
310     case INPUT_EVENT_TELETEXT:
311         event = new IMEvent( ItemTeletextChanged_Type, 0 );
312         break;
313
314     case INPUT_EVENT_STATISTICS:
315         event = new IMEvent( StatisticsUpdate_Type, 0 );
316         break;
317
318     case INPUT_EVENT_VOUT:
319         event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
320         break;
321     case INPUT_EVENT_AOUT:
322         event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
323         break;
324
325     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
326         event = new IMEvent( MetaChanged_Type, 0 );
327         break;
328     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
329         event = new IMEvent( InfoChanged_Type, 0 );
330         break;
331     case INPUT_EVENT_ITEM_NAME:
332         event = new IMEvent( NameChanged_Type, 0 );
333         break;
334
335     case INPUT_EVENT_AUDIO_DELAY:
336     case INPUT_EVENT_SUBTITLE_DELAY:
337         event = new IMEvent( SynchroChanged_Type, 0 );
338         break;
339
340     case INPUT_EVENT_CACHE:
341         event = new IMEvent( CachingEvent_Type, 0 );
342         break;
343
344     case INPUT_EVENT_BOOKMARK:
345         event = new IMEvent( BookmarksChanged_Type, 0 );
346         break;
347
348     case INPUT_EVENT_RECORD:
349         event = new IMEvent( RecordingEvent_Type, 0 );
350         break;
351
352     case INPUT_EVENT_PROGRAM:
353         /* This is for PID changes */
354         /* event = new IMEvent( ProgramChanged_Type, 0 );
355         break; */
356     case INPUT_EVENT_SIGNAL:
357         /* This is for capture-card signals */
358         /* event = new IMEvent( SignalChanged_Type, 0 );
359         break; */
360     default:
361         event = NULL;
362         break;
363     }
364
365     if( event )
366         QApplication::postEvent( im, static_cast<QEvent*>(event) );
367     return VLC_SUCCESS;
368 }
369 void InputManager::UpdatePosition()
370 {
371     /* Update position */
372     int i_length, i_time; /* Int is enough, since we store seconds */
373     float f_pos;
374     i_length = var_GetTime(  p_input , "length" ) / 1000000;
375     i_time = var_GetTime(  p_input , "time") / 1000000;
376     f_pos = var_GetFloat(  p_input , "position" );
377     emit positionUpdated( f_pos, i_time, i_length );
378 }
379
380 void InputManager::UpdateNavigation()
381 {
382     /* Update navigation status */
383     vlc_value_t val; val.i_int = 0;
384
385     if( hasInput() )
386         var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
387
388     if( val.i_int > 0 )
389     {
390         emit titleChanged( true );
391         msg_Dbg( p_intf, "Title %i", val.i_int );
392         /* p_input != NULL since val.i_int != 0 */
393         val.i_int = 0;
394         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
395         emit chapterChanged( (val.i_int > 0) );
396         msg_Dbg( p_intf, "Chapter: %i", val.i_int );
397     }
398     else
399         emit titleChanged( false );
400 }
401
402 void InputManager::UpdateStatus()
403 {
404     /* Update playing status */
405     vlc_value_t val; val.i_int = 0;
406     var_Get( p_input, "state", &val );
407     if( i_old_playing_status != val.i_int )
408     {
409         i_old_playing_status = val.i_int;
410         emit statusChanged( val.i_int );
411     }
412 }
413
414 void InputManager::UpdateRate()
415 {
416     /* Update Rate */
417     int i_new_rate = var_GetInteger( p_input, "rate");
418     if( i_new_rate != i_rate )
419     {
420         i_rate = i_new_rate;
421         /* Update rate */
422         emit rateChanged( i_rate );
423     }
424 }
425
426 void InputManager::UpdateName()
427 {
428     /* Update text, name and nowplaying */
429     QString text;
430
431     /* Try to get the Title, then the Name */
432     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
433     if( EMPTY_STR( psz_name ) )
434     {
435         free( psz_name );
436         psz_name = input_item_GetName( input_GetItem( p_input ) );
437     }
438
439     /* Try to get the nowplaying */
440     char *psz_nowplaying =
441         input_item_GetNowPlaying( input_GetItem( p_input ) );
442     if( !EMPTY_STR( psz_nowplaying ) )
443     {
444         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
445     }
446     else  /* Do it ourself */
447     {
448         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
449
450         if( !EMPTY_STR( psz_artist ) )
451             text.sprintf( "%s - %s", psz_artist, psz_name );
452         else
453             text.sprintf( "%s", psz_name );
454
455         free( psz_artist );
456     }
457     /* Free everything */
458     free( psz_name );
459     free( psz_nowplaying );
460
461     /* If we have Nothing */
462     if( text.isEmpty() )
463     {
464         psz_name = input_item_GetURI( input_GetItem( p_input ) );
465         text.sprintf( "%s", psz_name );
466         text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
467         free( psz_name );
468     }
469
470     if( oldName != text )
471     {
472         emit nameChanged( text );
473         oldName = text;
474     }
475 }
476
477 bool InputManager::hasAudio()
478 {
479     if( hasInput() )
480     {
481         vlc_value_t val;
482         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
483         return val.i_int > 0;
484     }
485     return false;
486 }
487
488 void InputManager::UpdateTeletext()
489 {
490     if( hasInput() )
491         telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
492     else
493         telexActivation( false );
494 }
495
496 void InputManager::UpdateVout()
497 {
498     if( hasInput() )
499     {
500         /* Get current vout lists from input */
501         int i_vout;
502         vout_thread_t **pp_vout;
503         if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
504         {
505             i_vout = 0;
506             pp_vout = NULL;
507         }
508
509         /* */
510         emit voutListChanged( pp_vout, i_vout );
511
512         /* */
513         bool b_old_video = b_video;
514         b_video = i_vout > 0;
515         if( !!b_old_video != !!b_video )
516             emit voutChanged( b_video );
517
518         /* Release the vout list */
519         for( int i = 0; i < i_vout; i++ )
520             vlc_object_release( (vlc_object_t*)pp_vout[i] );
521         free( pp_vout );
522     }
523 }
524 void InputManager::UpdateAout()
525 {
526     if( hasInput() )
527     {
528         /* TODO */
529     }
530 }
531 void InputManager::UpdateCaching()
532 {
533     if(!hasInput()) return;
534
535     float f_newCache = var_GetFloat ( p_input, "cache" );
536     if( f_newCache != f_cache )
537     {
538         f_cache = f_newCache;
539         /* Update rate */
540         emit cachingChanged( f_cache );
541     }
542 }
543
544 void InputManager::requestArtUpdate()
545 {
546     if( hasInput() )
547     {
548         playlist_t *p_playlist = pl_Hold( p_intf );
549         playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
550         pl_Release( p_intf );
551     }
552     else
553     {
554         /* No input will signal the cover art to update,
555          * let's do it ourself */
556         UpdateArt();
557     }
558 }
559
560 void InputManager::UpdateArt()
561 {
562     QString url;
563
564     if( hasInput() )
565     {
566         char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
567         url = psz_art;
568         free( psz_art );
569     }
570     url = url.replace( "file://", QString("" ) );
571     /* Taglib seems to define a attachment://, It won't work yet */
572     url = url.replace( "attachment://", QString("" ) );
573     /* Update Art meta */
574     emit artChanged( url );
575 }
576
577 inline void InputManager::UpdateStats()
578 {
579     emit statisticsUpdated( input_GetItem( p_input ) );
580 }
581
582 inline void InputManager::UpdateMeta()
583 {
584     emit metaChanged( input_GetItem( p_input ) );
585 }
586
587 inline void InputManager::UpdateInfo()
588 {
589     emit infoChanged( input_GetItem( p_input ) );
590 }
591
592 void InputManager::UpdateRecord()
593 {
594     if( hasInput() )
595     {
596         emit recordingStateChanged( var_GetBool( p_input, "record" ) );
597     }
598 }
599
600 /* User update of the slider */
601 void InputManager::sliderUpdate( float new_pos )
602 {
603     if( hasInput() )
604         var_SetFloat( p_input, "position", new_pos );
605 }
606
607 /* User togglePlayPause */
608 void InputManager::togglePlayPause()
609 {
610     vlc_value_t state;
611     if( hasInput() )
612     {
613         var_Get( p_input, "state", &state );
614         state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
615         var_Set( p_input, "state", state );
616         emit statusChanged( state.i_int );
617     }
618 }
619
620 void InputManager::sectionPrev()
621 {
622     if( hasInput() )
623     {
624         int i_type = var_Type( p_input, "next-chapter" );
625         vlc_value_t val; val.b_bool = true;
626         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
627                             "prev-chapter":"prev-title", val );
628     }
629 }
630
631 void InputManager::sectionNext()
632 {
633     if( hasInput() )
634     {
635         int i_type = var_Type( p_input, "next-chapter" );
636         vlc_value_t val; val.b_bool = true;
637         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
638                             "next-chapter":"next-title", val );
639     }
640 }
641
642 void InputManager::sectionMenu()
643 {
644     if( hasInput() )
645     {
646         vlc_value_t val, text;
647         vlc_value_t root;
648
649         if( var_Change( p_input, "title  0", VLC_VAR_GETLIST, &val, &text ) < 0 )
650             return;
651
652         /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
653         root.i_int = 0;
654         for( int i = 0; i < val.p_list->i_count; i++ )
655         {
656             if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
657                 root.i_int = i;
658         }
659         var_Change( p_input, "title  0", VLC_VAR_FREELIST, &val, &text );
660
661         var_Set( p_input, "title  0", root );
662     }
663 }
664
665 /*
666  *  Teletext Functions
667  */
668
669 /* Set a new Teletext Page */
670 void InputManager::telexSetPage( int page )
671 {
672     if( hasInput() )
673     {
674         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
675         const int i_spu_es = var_GetInteger( p_input, "spu-es" );
676
677         if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
678         {
679             vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
680                         "zvbi", FIND_ANYWHERE );
681             if( p_vbi )
682             {
683                 var_SetInteger( p_vbi, "vbi-page", page );
684                 vlc_object_release( p_vbi );
685                 emit newTelexPageSet( page );
686             }
687         }
688     }
689 }
690
691 /* Set the transparency on teletext */
692 void InputManager::telexSetTransparency( bool b_transparentTelextext )
693 {
694     if( hasInput() )
695     {
696         vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
697                     "zvbi", FIND_ANYWHERE );
698         if( p_vbi )
699         {
700             var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
701             vlc_object_release( p_vbi );
702             emit teletextTransparencyActivated( b_transparentTelextext );
703         }
704     }
705 }
706
707 void InputManager::telexActivation( bool b_enabled )
708 {
709     if( hasInput() )
710     {
711         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
712         const int i_spu_es = var_GetInteger( p_input, "spu-es" );
713
714         /* Teletext is possible. Show the buttons */
715         b_enabled = (i_teletext_es >= 0);
716         emit teletextPossible( b_enabled );
717         if( !b_enabled ) return;
718
719         /* If Teletext is selected */
720         if( i_teletext_es == i_spu_es )
721         {
722             /* Activate the buttons */
723             teletextActivated( true );
724
725             /* Then, find the current page */
726             int i_page = 100;
727             vlc_object_t *p_vbi = (vlc_object_t *)
728                 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
729             if( p_vbi )
730             {
731                 i_page = var_GetInteger( p_vbi, "vbi-page" );
732                 vlc_object_release( p_vbi );
733                 emit newTelexPageSet( i_page );
734             }
735         }
736     }
737     else
738         emit teletextPossible( b_enabled );
739 }
740
741 void InputManager::activateTeletext( bool b_enable )
742 {
743     if( hasInput() )
744     {
745         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
746         if( i_teletext_es >= 0 )
747         {
748             var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
749         }
750     }
751 }
752
753 void InputManager::reverse()
754 {
755     if( hasInput() )
756     {
757         int i_rate = var_GetInteger( p_input, "rate" );
758         var_SetInteger( p_input, "rate", -i_rate );
759     }
760 }
761
762 void InputManager::slower()
763 {
764     if( hasInput() )
765         var_SetVoid( p_input, "rate-slower" );
766 }
767
768 void InputManager::faster()
769 {
770     if( hasInput() )
771         var_SetVoid( p_input, "rate-faster" );
772 }
773
774 void InputManager::normalRate()
775 {
776     if( hasInput() )
777         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
778 }
779
780 void InputManager::setRate( int new_rate )
781 {
782     if( hasInput() )
783         var_SetInteger( p_input, "rate", new_rate );
784 }
785
786 void InputManager::setAtoB()
787 {
788     if( !timeA )
789     {
790         timeA = var_GetTime( THEMIM->getInput(), "time"  );
791     }
792     else if( !timeB )
793     {
794         timeB = var_GetTime( THEMIM->getInput(), "time"  );
795         var_SetTime( THEMIM->getInput(), "time" , timeA );
796     }
797     else
798     {
799         timeA = 0;
800         timeB = 0;
801     }
802     emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
803 }
804
805 /* Function called regularly when in an AtoB loop */
806 void InputManager::AtoBLoop( int i_time )
807 {
808     if( timeB )
809     {
810         if( ( i_time >= (int)( timeB/1000000 ) )
811             || ( i_time < (int)( timeA/1000000 ) ) )
812             var_SetTime( THEMIM->getInput(), "time" , timeA );
813     }
814 }
815
816 /**********************************************************************
817  * MainInputManager implementation. Wrap an input manager and
818  * take care of updating the main playlist input.
819  * Used in the main playlist Dialog
820  **********************************************************************/
821 MainInputManager * MainInputManager::instance = NULL;
822
823 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
824                  : QObject(NULL), p_intf( _p_intf )
825 {
826     p_input = NULL;
827     im = new InputManager( this, p_intf );
828
829     var_AddCallback( THEPL, "item-change", ItemChanged, im );
830     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
831     var_AddCallback( THEPL, "activity", PLItemChanged, this );
832
833     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
834
835     /* Warn our embedded IM about input changes */
836     CONNECT( this, inputChanged( input_thread_t * ),
837              im, setInput( input_thread_t * ) );
838
839     /* emit check if playlist has allready started playing */
840     vlc_value_t val;
841     var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
842
843     IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
844     customEvent( static_cast<QEvent*>(event) );
845 }
846
847 MainInputManager::~MainInputManager()
848 {
849     if( p_input )
850     {
851        emit inputChanged( NULL );
852        var_DelCallback( p_input, "state", PLItemChanged, this );
853        vlc_object_release( p_input );
854     }
855
856     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
857
858     var_DelCallback( THEPL, "activity", PLItemChanged, this );
859     var_DelCallback( THEPL, "item-change", ItemChanged, im );
860
861     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
862 }
863
864 vout_thread_t* MainInputManager::getVout()
865 {
866     return p_input ? input_GetVout( p_input ) : NULL;
867 }
868
869 aout_instance_t * MainInputManager::getAout()
870 {
871     return p_input ? input_GetAout( p_input ) : NULL;
872 }
873
874 void MainInputManager::customEvent( QEvent *event )
875 {
876     int type = event->type();
877     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
878         return;
879
880     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
881     if( type == VolumeChanged_Type )
882     {
883         emit volumeChanged();
884         return;
885     }
886
887     /* Should be PLItemChanged Event */
888     if( !p_intf->p_sys->b_isDialogProvider )
889     {
890         vlc_mutex_lock( &p_intf->change_lock );
891         if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
892         {
893             emit inputChanged( NULL );
894             var_DelCallback( p_input, "state", PLItemChanged, this );
895             vlc_object_release( p_input );
896             p_input = NULL;
897             vlc_mutex_unlock( &p_intf->change_lock );
898             return;
899         }
900
901         if( !p_input )
902         {
903             p_input = playlist_CurrentInput(THEPL);
904             if( p_input )
905             {
906                 var_AddCallback( p_input, "state", PLItemChanged, this );
907                 emit inputChanged( p_input );
908             }
909         }
910         vlc_mutex_unlock( &p_intf->change_lock );
911     }
912     else
913     {
914         /* we are working as a dialogs provider */
915         playlist_t *p_playlist = pl_Hold( p_intf );
916         p_input = playlist_CurrentInput( p_playlist );
917         if( p_input )
918         {
919             emit inputChanged( p_input );
920             vlc_object_release( p_input );
921         }
922         pl_Release( p_intf );
923     }
924 }
925
926 /* Playlist Control functions */
927 void MainInputManager::stop()
928 {
929    playlist_Stop( THEPL );
930 }
931
932 void MainInputManager::next()
933 {
934    playlist_Next( THEPL );
935 }
936
937 void MainInputManager::prev()
938 {
939    playlist_Prev( THEPL );
940 }
941
942 void MainInputManager::togglePlayPause()
943 {
944     /* No input, play */
945     if( !p_input )
946         playlist_Play( THEPL );
947     else
948         getIM()->togglePlayPause();
949 }
950
951 /* Static callbacks for MIM */
952 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
953                         vlc_value_t oldval, vlc_value_t newval, void *param )
954 {
955     MainInputManager *mim = (MainInputManager*)param;
956
957     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
958     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
959     return VLC_SUCCESS;
960 }
961
962 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
963                         vlc_value_t oldval, vlc_value_t newval, void *param )
964 {
965     MainInputManager *mim = (MainInputManager*)param;
966
967     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
968     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
969     return VLC_SUCCESS;
970 }
971