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