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