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