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