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