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