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