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