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