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