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