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: 5aee413ff4a4f1f4f206ef66a97e47d1007b5056 $
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 "recents.hpp"
32 
33 #include <vlc_actions.h>           /* ACTION_ID */
34 #include <vlc_url.h>            /* vlc_uri_decode */
35 #include <vlc_strings.h>        /* vlc_strfinput */
36 #include <vlc_aout.h>           /* audio_output_t */
37 
38 #include <QApplication>
39 #include <QFile>
40 #include <QDir>
41 #include <QSignalMapper>
42 #include <QMessageBox>
43 
44 #include <assert.h>
45 
46 static int InputEvent( vlc_object_t *, const char *,
47                        vlc_value_t, vlc_value_t, void * );
48 static int VbiEvent( vlc_object_t *, const char *,
49                      vlc_value_t, vlc_value_t, void * );
50 
51 /* Ensure arbitratry (not dynamically allocated) event IDs are not in use */
registerAndCheckEventIds(int start,int end)52 static inline void registerAndCheckEventIds( int start, int end )
53 {
54     for ( int i=start ; i<=end ; i++ )
55         Q_ASSERT( QEvent::registerEventType( i ) == i ); /* event ID collision ! */
56 }
57 
58 /**********************************************************************
59  * InputManager implementation
60  **********************************************************************
61  * The Input Manager can be the main one around the playlist
62  * But can also be used for VLM dialog or similar
63  **********************************************************************/
64 
InputManager(MainInputManager * mim,intf_thread_t * _p_intf)65 InputManager::InputManager( MainInputManager *mim, intf_thread_t *_p_intf) :
66                            QObject( mim ), p_intf( _p_intf )
67 {
68     p_mim        = mim;
69     i_old_playing_status = END_S;
70     oldName      = "";
71     artUrl       = "";
72     p_input      = NULL;
73     p_input_vbi  = NULL;
74     f_rate       = 0.;
75     p_item       = NULL;
76     b_video      = false;
77     timeA        = 0;
78     timeB        = 0;
79     f_cache      = -1.; /* impossible initial value, different from all */
80     registerAndCheckEventIds( IMEvent::PositionUpdate, IMEvent::FullscreenControlPlanHide );
81     registerAndCheckEventIds( PLEvent::PLItemAppended, PLEvent::PLEmpty );
82 }
83 
~InputManager()84 InputManager::~InputManager()
85 {
86     delInput();
87 }
88 
inputChangedHandler()89 void InputManager::inputChangedHandler()
90 {
91     setInput( p_mim->getInput() );
92 }
93 
94 /* Define the Input used.
95    Add the callbacks on input
96    p_input is held once here */
setInput(input_thread_t * _p_input)97 void InputManager::setInput( input_thread_t *_p_input )
98 {
99     delInput();
100     p_input = _p_input;
101     if( p_input != NULL )
102     {
103         msg_Dbg( p_intf, "IM: Setting an input" );
104         vlc_object_hold( p_input );
105         addCallbacks();
106 
107         UpdateStatus();
108         UpdateName();
109         UpdateArt();
110         UpdateTeletext();
111         UpdateNavigation();
112         UpdateVout();
113 
114         p_item = input_GetItem( p_input );
115         emit rateChanged( var_GetFloat( p_input, "rate" ) );
116 
117         /* Get Saved Time */
118         if( p_item->i_type == ITEM_TYPE_FILE )
119         {
120             char *uri = input_item_GetURI( p_item );
121 
122             int i_time = RecentsMRL::getInstance( p_intf )->time( qfu(uri) );
123             if( i_time > 0 && qfu( uri ) != lastURI &&
124                     !var_GetFloat( p_input, "run-time" ) &&
125                     !var_GetFloat( p_input, "start-time" ) &&
126                     !var_GetFloat( p_input, "stop-time" ) )
127             {
128                 emit resumePlayback( (int64_t)i_time * 1000 );
129             }
130             playlist_Lock( THEPL );
131             // Add root items only
132             playlist_item_t* p_node = playlist_CurrentPlayingItem( THEPL );
133             if ( p_node != NULL && p_node->p_parent != NULL && p_node->p_parent->i_id == THEPL->p_playing->i_id )
134             {
135                 // Save the latest URI to avoid asking to restore the
136                 // position on the same input file.
137                 lastURI = qfu( uri );
138                 RecentsMRL::getInstance( p_intf )->addRecent( lastURI );
139             }
140             playlist_Unlock( THEPL );
141             free( uri );
142         }
143     }
144     else
145     {
146         p_item = NULL;
147         lastURI.clear();
148         assert( !p_input_vbi );
149         emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
150     }
151 }
152 
153 /* delete Input if it ever existed.
154    Delete the callbacls on input
155    p_input is released once here */
delInput()156 void InputManager::delInput()
157 {
158     if( !p_input ) return;
159     msg_Dbg( p_intf, "IM: Deleting the input" );
160 
161     /* Save time / position */
162     char *uri = input_item_GetURI( p_item );
163     if( uri != NULL ) {
164         float f_pos = var_GetFloat( p_input , "position" );
165         int64_t i_time = -1;
166 
167         if( f_pos >= 0.05f && f_pos <= 0.95f
168          && var_GetInteger( p_input, "length" ) >= 60 * CLOCK_FREQ )
169             i_time = var_GetInteger( p_input, "time");
170 
171         RecentsMRL::getInstance( p_intf )->setTime( qfu(uri), i_time );
172         free(uri);
173     }
174 
175     delCallbacks();
176     i_old_playing_status = END_S;
177     p_item               = NULL;
178     oldName              = "";
179     artUrl               = "";
180     b_video              = false;
181     timeA                = 0;
182     timeB                = 0;
183     f_rate               = 0. ;
184 
185     if( p_input_vbi )
186     {
187         vlc_object_release( p_input_vbi );
188         p_input_vbi = NULL;
189     }
190 
191     vlc_object_release( p_input );
192     p_input = NULL;
193 
194     emit positionUpdated( -1.0, 0 ,0 );
195     emit rateChanged( var_InheritFloat( p_intf, "rate" ) );
196     emit nameChanged( "" );
197     emit chapterChanged( 0 );
198     emit titleChanged( 0 );
199     emit playingStatusChanged( END_S );
200 
201     emit teletextPossible( false );
202     emit AtoBchanged( false, false );
203     emit voutChanged( false );
204     emit voutListChanged( NULL, 0 );
205 
206     /* Reset all InfoPanels but stats */
207     emit artChanged( NULL );
208     emit artChanged( "" );
209     emit infoChanged( NULL );
210     emit currentMetaChanged( (input_item_t *)NULL );
211 
212     emit encryptionChanged( false );
213     emit recordingStateChanged( false );
214 
215     emit cachingChanged( 0.0 );
216 }
217 
218 /* Convert the event from the callbacks in actions */
customEvent(QEvent * event)219 void InputManager::customEvent( QEvent *event )
220 {
221     int i_type = event->type();
222     IMEvent *ple = static_cast<IMEvent *>(event);
223 
224     if( i_type == IMEvent::ItemChanged )
225         UpdateMeta( ple->item() );
226 
227     if( !hasInput() )
228         return;
229 
230     /* Actions */
231     switch( i_type )
232     {
233     case IMEvent::PositionUpdate:
234         UpdatePosition();
235         break;
236     case IMEvent::StatisticsUpdate:
237         UpdateStats();
238         break;
239     case IMEvent::ItemChanged:
240         /* Ignore ItemChanged_Type event that does not apply to our input */
241         if( p_item == ple->item() )
242         {
243             UpdateStatus();
244             UpdateName();
245             UpdateArt();
246             UpdateMeta();
247             /* Update duration of file */
248         }
249         break;
250     case IMEvent::ItemStateChanged:
251         UpdateStatus();
252         break;
253     case IMEvent::MetaChanged:
254         UpdateMeta();
255         UpdateName(); /* Needed for NowPlaying */
256         UpdateArt(); /* Art is part of meta in the core */
257         break;
258     case IMEvent::InfoChanged:
259         UpdateInfo();
260         break;
261     case IMEvent::ItemTitleChanged:
262         UpdateNavigation();
263         UpdateName(); /* Display the name of the Chapter, if exists */
264         break;
265     case IMEvent::ItemRateChanged:
266         UpdateRate();
267         break;
268     case IMEvent::ItemEsChanged:
269         UpdateTeletext();
270         // We don't do anything ES related. Why ?
271         break;
272     case IMEvent::ItemTeletextChanged:
273         UpdateTeletext();
274         break;
275     case IMEvent::InterfaceVoutUpdate:
276         UpdateVout();
277         break;
278     case IMEvent::SynchroChanged:
279         emit synchroChanged();
280         break;
281     case IMEvent::CachingEvent:
282         UpdateCaching();
283         break;
284     case IMEvent::BookmarksChanged:
285         emit bookmarksChanged();
286         break;
287     case IMEvent::InterfaceAoutUpdate:
288         UpdateAout();
289         break;
290     case IMEvent::RecordingEvent:
291         UpdateRecord();
292         break;
293     case IMEvent::ProgramChanged:
294         UpdateProgramEvent();
295         break;
296     case IMEvent::EPGEvent:
297         UpdateEPG();
298         break;
299     default:
300         msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
301         vlc_assert_unreachable();
302     }
303 }
304 
305 /* Add the callbacks on Input. Self explanatory */
addCallbacks()306 inline void InputManager::addCallbacks()
307 {
308     var_AddCallback( p_input, "intf-event", InputEvent, this );
309 }
310 
311 /* Delete the callbacks on Input. Self explanatory */
delCallbacks()312 inline void InputManager::delCallbacks()
313 {
314     var_DelCallback( p_input, "intf-event", InputEvent, this );
315 }
316 
317 /* Static callbacks for IM */
ItemChanged(vlc_object_t *,const char *,vlc_value_t,vlc_value_t val,void * param)318 int MainInputManager::ItemChanged( vlc_object_t *, const char *,
319                                    vlc_value_t, vlc_value_t val, void *param )
320 {
321     InputManager *im = (InputManager*)param;
322     input_item_t *p_item = static_cast<input_item_t *>(val.p_address);
323 
324     IMEvent *event = new IMEvent( IMEvent::ItemChanged, p_item );
325     QApplication::postEvent( im, event );
326     return VLC_SUCCESS;
327 }
328 
InputEvent(vlc_object_t *,const char *,vlc_value_t,vlc_value_t newval,void * param)329 static int InputEvent( vlc_object_t *, const char *,
330                        vlc_value_t, vlc_value_t newval, void *param )
331 {
332     InputManager *im = (InputManager*)param;
333     IMEvent *event;
334 
335     switch( newval.i_int )
336     {
337     case INPUT_EVENT_STATE:
338         event = new IMEvent( IMEvent::ItemStateChanged );
339         break;
340     case INPUT_EVENT_RATE:
341         event = new IMEvent( IMEvent::ItemRateChanged );
342         break;
343     case INPUT_EVENT_POSITION:
344     //case INPUT_EVENT_LENGTH:
345         event = new IMEvent( IMEvent::PositionUpdate );
346         break;
347 
348     case INPUT_EVENT_TITLE:
349     case INPUT_EVENT_CHAPTER:
350         event = new IMEvent( IMEvent::ItemTitleChanged );
351         break;
352 
353     case INPUT_EVENT_ES:
354         event = new IMEvent( IMEvent::ItemEsChanged );
355         break;
356     case INPUT_EVENT_TELETEXT:
357         event = new IMEvent( IMEvent::ItemTeletextChanged );
358         break;
359 
360     case INPUT_EVENT_STATISTICS:
361         event = new IMEvent( IMEvent::StatisticsUpdate );
362         break;
363 
364     case INPUT_EVENT_VOUT:
365         event = new IMEvent( IMEvent::InterfaceVoutUpdate );
366         break;
367     case INPUT_EVENT_AOUT:
368         event = new IMEvent( IMEvent::InterfaceAoutUpdate );
369         break;
370 
371     case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
372         event = new IMEvent( IMEvent::MetaChanged );
373         break;
374     case INPUT_EVENT_ITEM_INFO: /* Codec Info */
375         event = new IMEvent( IMEvent::InfoChanged );
376         break;
377 
378     case INPUT_EVENT_AUDIO_DELAY:
379     case INPUT_EVENT_SUBTITLE_DELAY:
380         event = new IMEvent( IMEvent::SynchroChanged );
381         break;
382 
383     case INPUT_EVENT_CACHE:
384         event = new IMEvent( IMEvent::CachingEvent );
385         break;
386 
387     case INPUT_EVENT_BOOKMARK:
388         event = new IMEvent( IMEvent::BookmarksChanged );
389         break;
390 
391     case INPUT_EVENT_RECORD:
392         event = new IMEvent( IMEvent::RecordingEvent );
393         break;
394 
395     case INPUT_EVENT_PROGRAM:
396         /* This is for PID changes */
397         event = new IMEvent( IMEvent::ProgramChanged );
398         break;
399 
400     case INPUT_EVENT_ITEM_EPG:
401         /* EPG data changed */
402         event = new IMEvent( IMEvent::EPGEvent );
403         break;
404 
405     case INPUT_EVENT_SIGNAL:
406         /* This is for capture-card signals */
407         /* event = new IMEvent( SignalChanged_Type );
408         break; */
409     default:
410         event = NULL;
411         break;
412     }
413 
414     if( event )
415         QApplication::postEvent( im, event );
416     return VLC_SUCCESS;
417 }
418 
VbiEvent(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * param)419 static int VbiEvent( vlc_object_t *, const char *,
420                      vlc_value_t, vlc_value_t, void *param )
421 {
422     InputManager *im = (InputManager*)param;
423     IMEvent *event = new IMEvent( IMEvent::ItemTeletextChanged );
424 
425     QApplication::postEvent( im, event );
426     return VLC_SUCCESS;
427 }
428 
UpdatePosition()429 void InputManager::UpdatePosition()
430 {
431     /* Update position */
432     int64_t i_length = var_GetInteger(  p_input , "length" );
433     int64_t i_time = var_GetInteger(  p_input , "time");
434     float f_pos = var_GetFloat(  p_input , "position" );
435     emit positionUpdated( f_pos, i_time, i_length / CLOCK_FREQ );
436 }
437 
UpdateNavigation()438 void InputManager::UpdateNavigation()
439 {
440     /* Update navigation status */
441     vlc_value_t val; val.i_int = 0;
442     vlc_value_t val2; val2.i_int = 0;
443 
444     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
445 
446     if( val.i_int > 0 )
447     {
448         bool b_menu = false;
449         if( val.i_int > 1 )
450         {
451             input_title_t **pp_title = NULL;
452             int i_title = 0;
453             if( input_Control( p_input, INPUT_GET_FULL_TITLE_INFO, &pp_title, &i_title ) == VLC_SUCCESS )
454             {
455                 for( int i = 0; i < i_title; i++ )
456                 {
457                     if( pp_title[i]->i_flags & INPUT_TITLE_MENU )
458                         b_menu = true;
459                     vlc_input_title_Delete(pp_title[i]);
460                 }
461                 free( pp_title );
462             }
463         }
464 
465         /* p_input != NULL since val.i_int != 0 */
466         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val2, NULL );
467 
468         emit titleChanged( b_menu );
469         emit chapterChanged( val2.i_int > 1 );
470     }
471     else
472         emit chapterChanged( false );
473 
474     if( hasInput() )
475         emit inputCanSeek( var_GetBool( p_input, "can-seek" ) );
476     else
477         emit inputCanSeek( false );
478 }
479 
UpdateStatus()480 void InputManager::UpdateStatus()
481 {
482     /* Update playing status */
483     int state = var_GetInteger( p_input, "state" );
484     if( i_old_playing_status != state )
485     {
486         i_old_playing_status = state;
487         emit playingStatusChanged( state );
488     }
489 }
490 
UpdateRate()491 void InputManager::UpdateRate()
492 {
493     /* Update Rate */
494     float f_new_rate = var_GetFloat( p_input, "rate" );
495     if( f_new_rate != f_rate )
496     {
497         f_rate = f_new_rate;
498         /* Update rate */
499         emit rateChanged( f_rate );
500     }
501 }
502 
UpdateName()503 void InputManager::UpdateName()
504 {
505     /* Update text, name and nowplaying */
506     QString name;
507 
508     /* Try to get the nowplaying */
509     char *format = var_InheritString( p_intf, "input-title-format" );
510     char *formatted = NULL;
511     if (format != NULL)
512     {
513         formatted = vlc_strfinput( p_input, format );
514         free( format );
515         if( formatted != NULL )
516         {
517             name = qfu(formatted);
518             free( formatted );
519         }
520     }
521 
522     /* If we have Nothing */
523     if( name.simplified().isEmpty() )
524     {
525         char *uri = input_item_GetURI( input_GetItem( p_input ) );
526         char *file = uri ? strrchr( uri, '/' ) : NULL;
527         if( file != NULL )
528         {
529             vlc_uri_decode( ++file );
530             name = qfu(file);
531         }
532         else
533             name = qfu(uri);
534         free( uri );
535     }
536 
537     name = name.trimmed();
538 
539     if( oldName != name )
540     {
541         emit nameChanged( name );
542         oldName = name;
543     }
544 }
545 
playingStatus() const546 int InputManager::playingStatus() const
547 {
548     return i_old_playing_status;
549 }
550 
hasAudio()551 bool InputManager::hasAudio()
552 {
553     if( hasInput() )
554     {
555         vlc_value_t val;
556         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
557         return val.i_int > 0;
558     }
559     return false;
560 }
561 
hasVisualisation()562 bool InputManager::hasVisualisation()
563 {
564     if( !p_input )
565         return false;
566 
567     audio_output_t *aout = input_GetAout( p_input );
568     if( !aout )
569         return false;
570 
571     char *visual = var_InheritString( aout, "visual" );
572     vlc_object_release( aout );
573 
574     if( !visual )
575         return false;
576 
577     free( visual );
578     return true;
579 }
580 
UpdateTeletext()581 void InputManager::UpdateTeletext()
582 {
583     const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
584     const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
585 
586     /* Teletext is possible. Show the buttons */
587     emit teletextPossible( b_enabled );
588 
589     /* If Teletext is selected */
590     if( b_enabled && i_teletext_es >= 0 )
591     {
592         /* Then, find the current page */
593         int i_page = 100;
594         bool b_transparent = false;
595 
596         if( p_input_vbi )
597         {
598             var_DelCallback( p_input_vbi, "vbi-page", VbiEvent, this );
599             vlc_object_release( p_input_vbi );
600         }
601 
602         if( input_GetEsObjects( p_input, i_teletext_es, &p_input_vbi, NULL, NULL ) )
603             p_input_vbi = NULL;
604 
605         if( p_input_vbi )
606         {
607             /* This callback is not remove explicitly, but interfaces
608              * are guaranted to outlive input */
609             var_AddCallback( p_input_vbi, "vbi-page", VbiEvent, this );
610 
611             i_page = var_GetInteger( p_input_vbi, "vbi-page" );
612             b_transparent = !var_GetBool( p_input_vbi, "vbi-opaque" );
613         }
614         emit newTelexPageSet( i_page );
615         emit teletextTransparencyActivated( b_transparent );
616 
617     }
618     emit teletextActivated( b_enabled && i_teletext_es >= 0 );
619 }
620 
UpdateEPG()621 void InputManager::UpdateEPG()
622 {
623     emit epgChanged();
624 }
625 
UpdateVout()626 void InputManager::UpdateVout()
627 {
628     size_t i_vout;
629     vout_thread_t **pp_vout;
630 
631     if( !p_input )
632         return;
633 
634     /* Get current vout lists from input */
635     if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
636     {
637         i_vout = 0;
638         pp_vout = NULL;
639     }
640 
641     /* */
642     emit voutListChanged( pp_vout, i_vout );
643 
644     /* */
645     bool b_old_video = b_video;
646     b_video = i_vout > 0;
647     if( !!b_old_video != !!b_video )
648         emit voutChanged( b_video );
649 
650     /* Release the vout list */
651     for( size_t i = 0; i < i_vout; i++ )
652         vlc_object_release( (vlc_object_t*)pp_vout[i] );
653     free( pp_vout );
654 }
655 
UpdateAout()656 void InputManager::UpdateAout()
657 {
658     /* TODO */
659 }
660 
UpdateCaching()661 void InputManager::UpdateCaching()
662 {
663     float f_newCache = var_GetFloat ( p_input, "cache" );
664     if( f_newCache != f_cache )
665     {
666         f_cache = f_newCache;
667         /* Update cache */
668         emit cachingChanged( f_cache );
669     }
670 }
671 
requestArtUpdate(input_item_t * p_item,bool b_forced)672 void InputManager::requestArtUpdate( input_item_t *p_item, bool b_forced )
673 {
674     bool b_current_item = false;
675     if ( !p_item && hasInput() )
676     {   /* default to current item */
677         p_item = input_GetItem( p_input );
678         b_current_item = true;
679     }
680 
681     if ( p_item )
682     {
683         /* check if it has already been enqueued */
684         if ( p_item->p_meta && !b_forced )
685         {
686             int status = vlc_meta_GetStatus( p_item->p_meta );
687             if ( status & ( ITEM_ART_NOTFOUND|ITEM_ART_FETCHED ) )
688                 return;
689         }
690         libvlc_ArtRequest( p_intf->obj.libvlc, p_item,
691                            (b_forced) ? META_REQUEST_OPTION_SCOPE_ANY
692                                       : META_REQUEST_OPTION_NONE );
693         /* No input will signal the cover art to update,
694              * let's do it ourself */
695         if ( b_current_item )
696             UpdateArt();
697         else
698             emit artChanged( p_item );
699     }
700 }
701 
decodeArtURL(input_item_t * p_item)702 const QString InputManager::decodeArtURL( input_item_t *p_item )
703 {
704     assert( p_item );
705 
706     char *psz_art = input_item_GetArtURL( p_item );
707     if( psz_art )
708     {
709         char *psz = vlc_uri2path( psz_art );
710         free( psz_art );
711         psz_art = psz;
712     }
713 
714 #if 0
715     /* Taglib seems to define a attachment://, It won't work yet */
716     url = url.replace( "attachment://", "" );
717 #endif
718 
719     QString path = qfu( psz_art ? psz_art : "" );
720     free( psz_art );
721     return path;
722 }
723 
UpdateArt()724 void InputManager::UpdateArt()
725 {
726     QString url = decodeArtURL( input_GetItem( p_input ) );
727 
728     /* the art hasn't changed, no need to update */
729     if(artUrl == url)
730         return;
731 
732     /* Update Art meta */
733     artUrl = url;
734     emit artChanged( artUrl );
735 }
736 
setArt(input_item_t * p_item,QString fileUrl)737 void InputManager::setArt( input_item_t *p_item, QString fileUrl )
738 {
739     if( hasInput() )
740     {
741         char *psz_cachedir = config_GetUserDir( VLC_CACHE_DIR );
742         QString old_url = p_mim->getIM()->decodeArtURL( p_item );
743         old_url = QDir( old_url ).canonicalPath();
744 
745         if( old_url.startsWith( QString::fromUtf8( psz_cachedir ) ) )
746             QFile( old_url ).remove(); /* Purge cached artwork */
747 
748         free( psz_cachedir );
749 
750         input_item_SetArtURL( p_item , fileUrl.toUtf8().constData() );
751         UpdateArt();
752     }
753 }
754 
UpdateStats()755 inline void InputManager::UpdateStats()
756 {
757     emit statisticsUpdated( input_GetItem( p_input ) );
758 }
759 
UpdateMeta(input_item_t * p_item_)760 inline void InputManager::UpdateMeta( input_item_t *p_item_ )
761 {
762     emit metaChanged( p_item_ );
763     emit artChanged( p_item_ );
764 }
765 
UpdateMeta()766 inline void InputManager::UpdateMeta()
767 {
768     emit currentMetaChanged( input_GetItem( p_input ) );
769 }
770 
UpdateInfo()771 inline void InputManager::UpdateInfo()
772 {
773     assert( p_input );
774     emit infoChanged( input_GetItem( p_input ) );
775 }
776 
UpdateRecord()777 void InputManager::UpdateRecord()
778 {
779     emit recordingStateChanged( var_GetBool( p_input, "record" ) );
780 }
781 
UpdateProgramEvent()782 void InputManager::UpdateProgramEvent()
783 {
784     bool b_scrambled = var_GetBool( p_input, "program-scrambled" );
785     emit encryptionChanged( b_scrambled );
786 }
787 
788 /* User update of the slider */
sliderUpdate(float new_pos)789 void InputManager::sliderUpdate( float new_pos )
790 {
791     if( hasInput() )
792         var_SetFloat( p_input, "position", new_pos );
793     emit seekRequested( new_pos );
794 }
795 
sectionPrev()796 void InputManager::sectionPrev()
797 {
798     if( hasInput() )
799     {
800         int i_type = var_Type( p_input, "next-chapter" );
801         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
802                              "prev-chapter":"prev-title" );
803     }
804 }
805 
sectionNext()806 void InputManager::sectionNext()
807 {
808     if( hasInput() )
809     {
810         int i_type = var_Type( p_input, "next-chapter" );
811         var_TriggerCallback( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
812                              "next-chapter":"next-title" );
813     }
814 }
815 
sectionMenu()816 void InputManager::sectionMenu()
817 {
818     if( hasInput() )
819     {
820         var_TriggerCallback( p_input, "menu-title" );
821     }
822 }
823 
824 /*
825  *  Teletext Functions
826  */
827 
changeProgram(int program)828 void InputManager::changeProgram( int program )
829 {
830     if( hasInput() )
831     {
832         var_SetInteger( p_input, "program", program );
833     }
834 }
835 
836 /* Set a new Teletext Page */
telexSetPage(int page)837 void InputManager::telexSetPage( int page )
838 {
839     if( hasInput() && p_input_vbi )
840     {
841         const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
842 
843         if( i_teletext_es >= 0 )
844         {
845             var_SetInteger( p_input_vbi, "vbi-page", page );
846             emit newTelexPageSet( page );
847         }
848     }
849 }
850 
851 /* Set the transparency on teletext */
telexSetTransparency(bool b_transparentTelextext)852 void InputManager::telexSetTransparency( bool b_transparentTelextext )
853 {
854     if( hasInput() && p_input_vbi )
855     {
856         var_SetBool( p_input_vbi, "vbi-opaque", !b_transparentTelextext );
857         emit teletextTransparencyActivated( b_transparentTelextext );
858     }
859 }
860 
activateTeletext(bool b_enable)861 void InputManager::activateTeletext( bool b_enable )
862 {
863     vlc_value_t list;
864     vlc_value_t text;
865     if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETCHOICES, &list, &text ) )
866     {
867         if( list.p_list->i_count > 0 )
868         {
869             /* Prefer the page 100 if it is present */
870             int i;
871             for( i = 0; i < text.p_list->i_count; i++ )
872             {
873                 /* The description is the page number as a string */
874                 const char *psz_page = text.p_list->p_values[i].psz_string;
875                 if( psz_page && !strcmp( psz_page, "100" ) )
876                     break;
877             }
878             if( i >= list.p_list->i_count )
879                 i = 0;
880             var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
881         }
882         var_FreeList( &list, &text );
883     }
884 }
885 
reverse()886 void InputManager::reverse()
887 {
888     if( hasInput() )
889     {
890         float f_rate_ = var_GetFloat( p_input, "rate" );
891         var_SetFloat( p_input, "rate", -f_rate_ );
892     }
893 }
894 
slower()895 void InputManager::slower()
896 {
897     var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_SLOWER );
898 }
899 
faster()900 void InputManager::faster()
901 {
902     var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_FASTER );
903 }
904 
littlefaster()905 void InputManager::littlefaster()
906 {
907     var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_RATE_FASTER_FINE );
908 }
909 
littleslower()910 void InputManager::littleslower()
911 {
912     var_SetInteger( p_intf->obj.libvlc, "key-action", ACTIONID_RATE_SLOWER_FINE );
913 }
914 
normalRate()915 void InputManager::normalRate()
916 {
917     var_SetFloat( THEPL, "rate", 1. );
918 }
919 
setRate(int new_rate)920 void InputManager::setRate( int new_rate )
921 {
922     var_SetFloat( THEPL, "rate",
923                  (float)INPUT_RATE_DEFAULT / (float)new_rate );
924 }
925 
jumpFwd()926 void InputManager::jumpFwd()
927 {
928     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
929     if( i_interval > 0 && hasInput() )
930     {
931         mtime_t val = CLOCK_FREQ * i_interval;
932         var_SetInteger( p_input, "time-offset", val );
933     }
934 }
935 
jumpBwd()936 void InputManager::jumpBwd()
937 {
938     int i_interval = var_InheritInteger( p_input, "short-jump-size" );
939     if( i_interval > 0 && hasInput() )
940     {
941         mtime_t val = -CLOCK_FREQ * i_interval;
942         var_SetInteger( p_input, "time-offset", val );
943     }
944 }
945 
setAtoB()946 void InputManager::setAtoB()
947 {
948     if( !timeA )
949     {
950         timeA = var_GetInteger( p_mim->getInput(), "time"  );
951     }
952     else if( !timeB )
953     {
954         timeB = var_GetInteger( p_mim->getInput(), "time"  );
955         var_SetInteger( p_mim->getInput(), "time" , timeA );
956         CONNECT( this, positionUpdated( float, int64_t, int ),
957                  this, AtoBLoop( float, int64_t, int ) );
958     }
959     else
960     {
961         timeA = 0;
962         timeB = 0;
963         disconnect( this, SIGNAL( positionUpdated( float, int64_t, int ) ),
964                     this, SLOT( AtoBLoop( float, int64_t, int ) ) );
965     }
966     emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
967 }
968 
969 /* Function called regularly when in an AtoB loop */
AtoBLoop(float,int64_t i_time,int)970 void InputManager::AtoBLoop( float, int64_t i_time, int )
971 {
972     if( timeB && i_time >= timeB )
973         var_SetInteger( p_mim->getInput(), "time" , timeA );
974 }
975 
976 /**********************************************************************
977  * MainInputManager implementation. Wrap an input manager and
978  * take care of updating the main playlist input.
979  * Used in the main playlist Dialog
980  **********************************************************************/
981 
MainInputManager(intf_thread_t * _p_intf)982 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
983     : QObject(NULL), p_input( NULL), p_intf( _p_intf ),
984       random( VLC_OBJECT(THEPL), "random" ),
985       repeat( VLC_OBJECT(THEPL), "repeat" ), loop( VLC_OBJECT(THEPL), "loop" ),
986       volume( VLC_OBJECT(THEPL), "volume" ), mute( VLC_OBJECT(THEPL), "mute" )
987 {
988     im = new InputManager( this, p_intf );
989 
990     /* Audio Menu */
991     menusAudioMapper = new QSignalMapper();
992     CONNECT( menusAudioMapper, mapped(QString), this, menusUpdateAudio( QString ) );
993 
994     /* Core Callbacks */
995     var_AddCallback( THEPL, "item-change", MainInputManager::ItemChanged, im );
996     var_AddCallback( THEPL, "input-current", MainInputManager::PLItemChanged, this );
997     var_AddCallback( THEPL, "leaf-to-parent", MainInputManager::LeafToParent, this );
998     var_AddCallback( THEPL, "playlist-item-append", MainInputManager::PLItemAppended, this );
999     var_AddCallback( THEPL, "playlist-item-deleted", MainInputManager::PLItemRemoved, this );
1000 
1001     /* Core Callbacks to widget */
1002     random.addCallback( this, SLOT(notifyRandom(bool)) );
1003     repeat.addCallback( this, SLOT(notifyRepeatLoop(bool)) );
1004     loop.addCallback(   this, SLOT(notifyRepeatLoop(bool)) );
1005     volume.addCallback( this, SLOT(notifyVolume(float)) );
1006     mute.addCallback(   this, SLOT(notifyMute(bool)) );
1007 
1008     /* Warn our embedded IM about input changes */
1009     DCONNECT( this, inputChanged( bool ),
1010               im, inputChangedHandler() );
1011 }
1012 
~MainInputManager()1013 MainInputManager::~MainInputManager()
1014 {
1015     if( p_input )
1016     {
1017        vlc_object_release( p_input );
1018        p_input = NULL;
1019        emit inputChanged( false );
1020     }
1021 
1022     var_DelCallback( THEPL, "input-current", MainInputManager::PLItemChanged, this );
1023     var_DelCallback( THEPL, "item-change", MainInputManager::ItemChanged, im );
1024     var_DelCallback( THEPL, "leaf-to-parent", MainInputManager::LeafToParent, this );
1025 
1026     var_DelCallback( THEPL, "playlist-item-append", MainInputManager::PLItemAppended, this );
1027     var_DelCallback( THEPL, "playlist-item-deleted", MainInputManager::PLItemRemoved, this );
1028 
1029     delete menusAudioMapper;
1030 }
1031 
getVout()1032 vout_thread_t* MainInputManager::getVout()
1033 {
1034     return p_input ? input_GetVout( p_input ) : NULL;
1035 }
1036 
getVouts() const1037 QVector<vout_thread_t*> MainInputManager::getVouts() const
1038 {
1039     vout_thread_t **pp_vout;
1040     size_t i_vout;
1041 
1042     if( p_input == NULL
1043      || input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) != VLC_SUCCESS
1044      || i_vout == 0 )
1045         return QVector<vout_thread_t*>();
1046 
1047     QVector<vout_thread_t*> vector = QVector<vout_thread_t*>();
1048     vector.reserve( i_vout );
1049     for( size_t i = 0; i < i_vout; i++ )
1050     {
1051         assert( pp_vout[i] );
1052         vector.append( pp_vout[i] );
1053     }
1054     free( pp_vout );
1055 
1056     return vector;
1057 }
1058 
getAout()1059 audio_output_t * MainInputManager::getAout()
1060 {
1061     return playlist_GetAout( THEPL );
1062 }
1063 
customEvent(QEvent * event)1064 void MainInputManager::customEvent( QEvent *event )
1065 {
1066     int type = event->type();
1067 
1068     PLEvent *plEv;
1069 
1070     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
1071     switch( type )
1072     {
1073     case PLEvent::PLItemAppended:
1074         plEv = static_cast<PLEvent*>( event );
1075         emit playlistItemAppended( plEv->getItemId(), plEv->getParentId() );
1076         return;
1077     case PLEvent::PLItemRemoved:
1078         plEv = static_cast<PLEvent*>( event );
1079         emit playlistItemRemoved( plEv->getItemId() );
1080         return;
1081     case PLEvent::PLEmpty:
1082         plEv = static_cast<PLEvent*>( event );
1083         emit playlistNotEmpty( plEv->getItemId() >= 0 );
1084         return;
1085     case PLEvent::LeafToParent:
1086         plEv = static_cast<PLEvent*>( event );
1087         emit leafBecameParent( plEv->getItemId() );
1088         return;
1089     default:
1090         if( type != IMEvent::ItemChanged ) return;
1091     }
1092     probeCurrentInput();
1093 }
1094 
probeCurrentInput()1095 void MainInputManager::probeCurrentInput()
1096 {
1097     if( p_input != NULL )
1098         vlc_object_release( p_input );
1099     p_input = playlist_CurrentInput( THEPL );
1100     emit inputChanged( p_input != NULL );
1101 }
1102 
1103 /* Playlist Control functions */
stop()1104 void MainInputManager::stop()
1105 {
1106    playlist_Stop( THEPL );
1107 }
1108 
next()1109 void MainInputManager::next()
1110 {
1111    playlist_Next( THEPL );
1112 }
1113 
prev()1114 void MainInputManager::prev()
1115 {
1116    playlist_Prev( THEPL );
1117 }
1118 
prevOrReset()1119 void MainInputManager::prevOrReset()
1120 {
1121     if( !p_input || var_GetInteger( p_input, "time") < INT64_C(10000) )
1122         playlist_Prev( THEPL );
1123     else
1124         getIM()->sliderUpdate( 0.0 );
1125 }
1126 
togglePlayPause()1127 void MainInputManager::togglePlayPause()
1128 {
1129     playlist_TogglePause( THEPL );
1130 }
1131 
play()1132 void MainInputManager::play()
1133 {
1134     playlist_Play( THEPL );
1135 }
1136 
pause()1137 void MainInputManager::pause()
1138 {
1139     playlist_Pause( THEPL );
1140 }
1141 
toggleRandom()1142 void MainInputManager::toggleRandom()
1143 {
1144     config_PutInt( p_intf, "random", var_ToggleBool( THEPL, "random" ) );
1145 }
1146 
notifyRandom(bool value)1147 void MainInputManager::notifyRandom(bool value)
1148 {
1149     emit randomChanged(value);
1150 }
1151 
notifyRepeatLoop(bool)1152 void MainInputManager::notifyRepeatLoop(bool)
1153 {
1154     int i_state = NORMAL;
1155 
1156     if( var_GetBool( THEPL, "loop" ) )   i_state = REPEAT_ALL;
1157     if( var_GetBool( THEPL, "repeat" ) ) i_state = REPEAT_ONE;
1158 
1159     emit repeatLoopChanged( i_state );
1160 }
1161 
loopRepeatLoopStatus()1162 void MainInputManager::loopRepeatLoopStatus()
1163 {
1164     /* Toggle Normal -> Loop -> Repeat -> Normal ... */
1165     bool loop = var_GetBool( THEPL, "loop" );
1166     bool repeat = var_GetBool( THEPL, "repeat" );
1167 
1168     if( repeat )
1169     {
1170         loop = false;
1171         repeat = false;
1172     }
1173     else if( loop )
1174     {
1175         loop = false;
1176         repeat = true;
1177     }
1178     else
1179     {
1180         loop = true;
1181         //repeat = false;
1182     }
1183 
1184     var_SetBool( THEPL, "loop", loop );
1185     var_SetBool( THEPL, "repeat", repeat );
1186     config_PutInt( p_intf, "loop", loop );
1187     config_PutInt( p_intf, "repeat", repeat );
1188 }
1189 
activatePlayQuit(bool b_exit)1190 void MainInputManager::activatePlayQuit( bool b_exit )
1191 {
1192     var_SetBool( THEPL, "play-and-exit", b_exit );
1193     config_PutInt( p_intf, "play-and-exit", b_exit );
1194 }
1195 
getPlayExitState()1196 bool MainInputManager::getPlayExitState()
1197 {
1198     return var_InheritBool( THEPL, "play-and-exit" );
1199 }
1200 
hasEmptyPlaylist()1201 bool MainInputManager::hasEmptyPlaylist()
1202 {
1203     playlist_Lock( THEPL );
1204     bool b_empty = playlist_IsEmpty( THEPL );
1205     playlist_Unlock( THEPL );
1206     return b_empty;
1207 }
1208 
1209 /****************************
1210  * Static callbacks for MIM *
1211  ****************************/
PLItemChanged(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * param)1212 int MainInputManager::PLItemChanged( vlc_object_t *, const char *,
1213                                      vlc_value_t, vlc_value_t, void *param )
1214 {
1215     MainInputManager *mim = (MainInputManager*)param;
1216 
1217     IMEvent *event = new IMEvent( IMEvent::ItemChanged );
1218     QApplication::postEvent( mim, event );
1219     return VLC_SUCCESS;
1220 }
1221 
LeafToParent(vlc_object_t *,const char *,vlc_value_t,vlc_value_t val,void * param)1222 int MainInputManager::LeafToParent( vlc_object_t *, const char *,
1223                                     vlc_value_t, vlc_value_t val, void *param )
1224 {
1225     MainInputManager *mim = (MainInputManager*)param;
1226 
1227     PLEvent *event = new PLEvent( PLEvent::LeafToParent, val.i_int );
1228 
1229     QApplication::postEvent( mim, event );
1230     return VLC_SUCCESS;
1231 }
1232 
notifyVolume(float volume)1233 void MainInputManager::notifyVolume( float volume )
1234 {
1235     emit volumeChanged( volume );
1236 }
1237 
notifyMute(bool mute)1238 void MainInputManager::notifyMute( bool mute )
1239 {
1240     emit soundMuteChanged(mute);
1241 }
1242 
1243 
menusUpdateAudio(const QString & data)1244 void MainInputManager::menusUpdateAudio( const QString& data )
1245 {
1246     audio_output_t *aout = getAout();
1247     if( aout != NULL )
1248     {
1249         aout_DeviceSet( aout, qtu(data) );
1250         vlc_object_release( aout );
1251     }
1252 }
1253 
PLItemAppended(vlc_object_t *,const char *,vlc_value_t,vlc_value_t cur,void * data)1254 int MainInputManager::PLItemAppended( vlc_object_t *, const char *,
1255                                       vlc_value_t, vlc_value_t cur,
1256                                       void *data )
1257 {
1258     MainInputManager *mim = static_cast<MainInputManager*>(data);
1259     playlist_item_t *item = static_cast<playlist_item_t *>( cur.p_address );
1260 
1261     PLEvent *event = new PLEvent( PLEvent::PLItemAppended, item->i_id,
1262         (item->p_parent != NULL) ? item->p_parent->i_id : -1  );
1263     QApplication::postEvent( mim, event );
1264 
1265     event = new PLEvent( PLEvent::PLEmpty, item->i_id, 0  );
1266     QApplication::postEvent( mim, event );
1267     return VLC_SUCCESS;
1268 }
1269 
PLItemRemoved(vlc_object_t * obj,const char *,vlc_value_t,vlc_value_t cur,void * data)1270 int MainInputManager::PLItemRemoved( vlc_object_t *obj, const char *,
1271                                      vlc_value_t, vlc_value_t cur, void *data )
1272 {
1273     playlist_t *pl = (playlist_t *) obj;
1274     MainInputManager *mim = static_cast<MainInputManager*>(data);
1275     playlist_item_t *item = static_cast<playlist_item_t *>( cur.p_address );
1276 
1277     PLEvent *event = new PLEvent( PLEvent::PLItemRemoved, item->i_id, 0  );
1278     QApplication::postEvent( mim, event );
1279     // can't use playlist_IsEmpty(  ) as it isn't true yet
1280     if ( pl->items.i_size == 1 ) // lock is held
1281     {
1282         event = new PLEvent( PLEvent::PLEmpty, -1, 0 );
1283         QApplication::postEvent( mim, event );
1284     }
1285     return VLC_SUCCESS;
1286 }
1287 
changeFullscreen(bool new_val)1288 void MainInputManager::changeFullscreen( bool new_val )
1289 {
1290     if ( var_GetBool( THEPL, "fullscreen" ) != new_val)
1291         var_SetBool( THEPL, "fullscreen", new_val );
1292 }
1293