]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
2066940facac9c12c2d4f5b36553039bf18cb655
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "input_manager.hpp"
31 #include "dialogs_provider.hpp"
32
33 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
34                       vlc_value_t n, void *param );
35 static int ItemChanged( vlc_object_t *, const char *,
36                         vlc_value_t, vlc_value_t, void * );
37 static int PLItemChanged( vlc_object_t *, const char *,
38                         vlc_value_t, vlc_value_t, void * );
39 static int InterfaceChanged( vlc_object_t *, const char *,
40                             vlc_value_t, vlc_value_t, void * );
41 static int ItemStateChanged( vlc_object_t *, const char *,
42                         vlc_value_t, vlc_value_t, void * );
43 static int ItemRateChanged( vlc_object_t *, const char *,
44                         vlc_value_t, vlc_value_t, void * );
45 static int ItemTitleChanged( vlc_object_t *, const char *,
46                         vlc_value_t, vlc_value_t, void * );
47 static int VolumeChanged( vlc_object_t *, const char *,
48                         vlc_value_t, vlc_value_t, void * );
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     old_name     = "";
62     artUrl       = "";
63     p_input      = NULL;
64     i_rate       = 0;
65     i_input_id   = 0;
66     b_transparentTelextext = false;
67 }
68
69 InputManager::~InputManager()
70 {
71     delInput();
72 }
73
74 /* Define the Input used.
75    Add the callbacks on input
76    p_input is yield once here */
77 void InputManager::setInput( input_thread_t *_p_input )
78 {
79     delInput();
80     p_input = _p_input;
81     if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
82     {
83         vlc_object_yield( p_input );
84         emit statusChanged( PLAYING_S );
85         UpdateMeta();
86         UpdateArt();
87         UpdateSPU();
88         UpdateNavigation();
89         addCallbacks();
90         i_input_id = input_GetItem( p_input )->i_id;
91     }
92     else
93     {
94         p_input = NULL;
95         i_input_id = 0;
96         emit rateChanged( INPUT_RATE_DEFAULT );
97         emit inputUnset();
98     }
99 }
100
101 /* delete Input if it ever existed.
102    Delete the callbacls on input
103    p_input is released once here */
104 void InputManager::delInput()
105 {
106     if( p_input )
107     {
108         delCallbacks();
109         i_old_playing_status = END_S;
110         i_input_id = 0;
111         old_name   = "";
112         artUrl     = "";
113         emit positionUpdated( 0.0, 0 ,0 );
114         emit statusChanged( END_S );
115         emit nameChanged( "" );
116         emit artChanged( "" );
117         emit rateChanged( INPUT_RATE_DEFAULT );
118         vlc_object_release( p_input );
119         p_input = NULL;
120         UpdateSPU();
121     }
122 }
123
124 /* Add the callbacks on Input. Self explanatory */
125 void InputManager::addCallbacks()
126 {
127     /* We don't care about:
128        - chapter
129        - programs
130        - audio-delay
131        - spu-delay
132        - bookmark
133        - position, time, length, because they are included in intf-change
134      */
135     /* src/input/input.c:1629 */
136     var_AddCallback( p_input, "state", ItemStateChanged, this );
137     /* src/input/es-out.c:552 */
138     var_AddCallback( p_input, "spu-es", ChangeSPU, this );
139     /* src/input/input.c:1765 */
140     var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
141     /* src/input/input.c:2003 */
142     var_AddCallback( p_input, "title", ItemTitleChanged, this );
143     /* src/input/input.c:734 for timers update*/
144     var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
145 }
146
147 /* Delete the callbacks on Input. Self explanatory */
148 void InputManager::delCallbacks()
149 {
150     var_DelCallback( p_input, "spu-es", ChangeSPU, this );
151     var_DelCallback( p_input, "state", ItemStateChanged, this );
152     var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
153     var_DelCallback( p_input, "title", ItemTitleChanged, this );
154     var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
155 }
156
157 /* Convert the event from the callbacks in actions */
158 void InputManager::customEvent( QEvent *event )
159 {
160     int type = event->type();
161     IMEvent *ple = static_cast<IMEvent *>(event);
162
163     if ( type != PositionUpdate_Type &&
164          type != ItemChanged_Type &&
165          type != ItemRateChanged_Type &&
166          type != ItemTitleChanged_Type &&
167          type != ItemSpuChanged_Type &&
168          type != ItemStateChanged_Type )
169         return;
170
171     if( !hasInput() ) return;
172
173     if( ( type != PositionUpdate_Type  &&
174           type != ItemRateChanged_Type &&
175           type != ItemSpuChanged_Type &&
176           type != ItemStateChanged_Type
177         )
178         && ( i_input_id != ple->i_id ) )
179         return;
180
181     if( type != PositionUpdate_Type )
182         msg_Dbg( p_intf, "New Event: type %i", type );
183
184     /* Actions */
185     switch( type )
186     {
187     case PositionUpdate_Type:
188         UpdatePosition();
189         break;
190     case ItemChanged_Type:
191         UpdateMeta();
192         UpdateStatus();
193         UpdateArt();
194         break;
195     case ItemStateChanged_Type:
196         UpdateStatus();
197         UpdateNavigation();
198         UpdateMeta();
199         break;
200     case ItemTitleChanged_Type:
201         UpdateNavigation();
202         UpdateMeta();
203         break;
204     case ItemRateChanged_Type:
205         UpdateRate();
206         break;
207     case ItemSpuChanged_Type:
208         UpdateSPU();
209         break;
210     }
211 }
212
213 void InputManager::UpdatePosition()
214 {
215     /* Update position */
216     int i_length, i_time; /* Int is enough, since we store seconds */
217     float f_pos;
218     i_length = var_GetTime(  p_input , "length" ) / 1000000;
219     i_time = var_GetTime(  p_input , "time") / 1000000;
220     f_pos = var_GetFloat(  p_input , "position" );
221     emit positionUpdated( f_pos, i_time, i_length );
222 }
223
224 void InputManager::UpdateNavigation()
225 {
226     /* Update navigation status */
227     vlc_value_t val; val.i_int = 0;
228     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
229     if( val.i_int > 0 )
230     {
231         val.i_int = 0;
232         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
233         emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
234     }
235     else
236     {
237         emit navigationChanged( 0 );
238     }
239 }
240
241 void InputManager::UpdateStatus()
242 {
243     /* Update playing status */
244     vlc_value_t val; val.i_int = 0;
245     var_Get( p_input, "state", &val );
246     if( i_old_playing_status != val.i_int )
247     {
248         i_old_playing_status = val.i_int;
249         emit statusChanged( val.i_int );
250     }
251 }
252
253 void InputManager::UpdateRate()
254 {
255     /* Update Rate */
256     int i_new_rate = var_GetInteger( p_input, "rate");
257     if( i_new_rate != i_rate )
258     {
259         i_rate = i_new_rate;
260         /* Update rate */
261         emit rateChanged( i_rate );
262     }
263 }
264
265 void InputManager::UpdateMeta()
266 {
267     /* Update text, name and nowplaying */
268     QString text;
269
270     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
271     if( EMPTY_STR( psz_name ) )
272     {
273         free( psz_name );
274         psz_name = input_item_GetName( input_GetItem( p_input ) );
275     }
276
277     char *psz_nowplaying =
278         input_item_GetNowPlaying( input_GetItem( p_input ) );
279     if( !EMPTY_STR( psz_nowplaying ) )
280     {
281         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
282     }
283     else
284     {
285         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
286         if( !EMPTY_STR( psz_artist ) )
287         {
288             text.sprintf( "%s - %s", psz_artist, psz_name );
289         }
290         else
291         {
292             text.sprintf( "%s", psz_name );
293         }
294         free( psz_artist );
295     }
296     free( psz_name );
297     free( psz_nowplaying );
298
299     if( old_name != text )
300     {
301         emit nameChanged( text );
302         old_name=text;
303     }
304 }
305
306 bool InputManager::hasAudio()
307 {
308     if( hasInput() )
309     {
310         vlc_value_t val;
311         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
312         return val.i_int > 0;
313     }
314     return false;
315 }
316
317 bool InputManager::hasVideo()
318 {
319     if( hasInput() )
320     {
321         vlc_value_t val;
322         var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
323         return val.i_int > 0;
324     }
325     return false;
326
327 }
328
329 void InputManager::UpdateSPU()
330 {
331     if( hasInput() )
332     {
333         vlc_value_t val;
334         var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
335         telexToggle( val.i_int > 0 );
336     }
337     else
338     {
339         telexToggle( false );
340     }
341 }
342
343 void InputManager::UpdateArt()
344 {
345     /* Update Art meta */
346     QString url;
347     char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
348     url.sprintf("%s", psz_art );
349     free( psz_art );
350     if( artUrl != url )
351     {
352         artUrl = url.replace( "file://",QString("" ) );
353         /* Taglib seems to define a attachment://, It won't work yet */
354         artUrl = url.replace( "attachment://",QString("" ) );
355         emit artChanged( artUrl );
356         msg_Dbg( p_intf, "Art:  %s", qtu( artUrl ) );
357     }
358 }
359
360 /* User update of the slider */
361 void InputManager::sliderUpdate( float new_pos )
362 {
363     if( hasInput() )
364         var_SetFloat( p_input, "position", new_pos );
365 }
366
367 /* User togglePlayPause */
368 void InputManager::togglePlayPause()
369 {
370     vlc_value_t state;
371     var_Get( p_input, "state", &state );
372     state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
373     var_Set( p_input, "state", state );
374     emit statusChanged( state.i_int );
375 }
376
377 void InputManager::sectionPrev()
378 {
379     if( hasInput() )
380     {
381         int i_type = var_Type( p_input, "next-chapter" );
382         vlc_value_t val; val.b_bool = true;
383         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
384                             "prev-chapter":"prev-title", val );
385     }
386 }
387
388 void InputManager::sectionNext()
389 {
390     if( hasInput() )
391     {
392         int i_type = var_Type( p_input, "next-chapter" );
393         vlc_value_t val; val.b_bool = true;
394         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
395                             "next-chapter":"next-title", val );
396     }
397 }
398
399 void InputManager::sectionMenu()
400 {
401     if( hasInput() )
402     {
403         vlc_value_t val; val.i_int = 2;
404         var_Set( p_input, "title  0", val );
405     }
406 }
407
408 void InputManager::telexGotoPage( int page )
409 {
410     if( hasInput() )
411     {
412         vlc_object_t *p_vbi;
413         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
414                     "zvbi", FIND_ANYWHERE );
415         if( p_vbi )
416         {
417             var_SetInteger( p_vbi, "vbi-page", page );
418             vlc_object_release( p_vbi );
419         }
420     }
421     emit setNewTelexPage( page );
422 }
423
424 void InputManager::telexToggle( bool b_enabled )
425 {
426     int i_page = 100;
427
428     if( hasInput() )
429     {
430         vlc_value_t val;
431         vlc_object_t *p_vbi;
432         var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
433         b_enabled = (val.i_int > 0);
434         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
435                     "zvbi", FIND_ANYWHERE );
436         if( p_vbi )
437         {
438             i_page = var_GetInteger( p_vbi, "vbi-page" );
439             vlc_object_release( p_vbi );
440             i_page = b_enabled ? i_page : 0;
441             telexGotoPage( i_page );
442         }
443     }
444     emit teletextEnabled( b_enabled );
445 }
446
447 void InputManager::telexToggleButtons()
448 {
449     emit toggleTelexButtons();
450 }
451
452 void InputManager::telexSetTransparency()
453 {
454     if( hasInput() )
455     {
456         vlc_object_t *p_vbi;
457         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
458                     "zvbi", FIND_ANYWHERE );
459         if( p_vbi )
460         {
461             var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
462             b_transparentTelextext = !b_transparentTelextext;
463             vlc_object_release( p_vbi );
464         }
465     }
466     emit toggleTelexTransparency();
467 }
468
469 void InputManager::slower()
470 {
471     if( hasInput() )
472         var_SetVoid( p_input, "rate-slower" );
473 }
474
475 void InputManager::faster()
476 {
477     if( hasInput() )
478         var_SetVoid( p_input, "rate-faster" );
479 }
480
481 void InputManager::normalRate()
482 {
483     if( hasInput() )
484         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
485 }
486
487 void InputManager::setRate( int new_rate )
488 {
489     if( hasInput() )
490         var_SetInteger( p_input, "rate", new_rate );
491 }
492
493 /**********************************************************************
494  * MainInputManager implementation. Wrap an input manager and
495  * take care of updating the main playlist input.
496  * Used in the main playlist Dialog
497  **********************************************************************/
498 MainInputManager * MainInputManager::instance = NULL;
499
500 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
501                  : QObject(NULL), p_intf( _p_intf )
502 {
503     p_input = NULL;
504     im = new InputManager( this, p_intf );
505
506 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
507     var_AddCallback( THEPL, "item-change", ItemChanged, im );
508     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
509     var_AddCallback( THEPL, "activity", PLItemChanged, this );
510
511     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
512
513     // No necessary, I think TODO REMOVE ME at the end
514     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
515
516     /* Warn our embedded IM about input changes */
517     CONNECT( this, inputChanged( input_thread_t * ),
518              im, setInput( input_thread_t * ) );
519
520     /* emit check if playlist has allready started playing */
521     vlc_value_t val;
522     var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
523     IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
524     QApplication::postEvent( this, static_cast<QEvent*>(event) );
525
526 }
527
528 MainInputManager::~MainInputManager()
529 {
530     if( p_input )
531     {
532        var_DelCallback( p_input, "state", PLItemChanged, this );
533        vlc_object_release( p_input );
534        emit inputChanged( NULL );
535     }
536
537     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
538
539     var_DelCallback( THEPL, "activity", PLItemChanged, this );
540     var_DelCallback( THEPL, "item-change", ItemChanged, im );
541 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
542
543     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
544 }
545
546 void MainInputManager::customEvent( QEvent *event )
547 {
548     int type = event->type();
549     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
550         return;
551
552     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
553     if( type == VolumeChanged_Type )
554     {
555         emit volumeChanged();
556         return;
557     }
558
559     /* Should be PLItemChanged Event */
560     if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
561     {
562         vlc_mutex_lock( &p_intf->change_lock );
563         if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
564         {
565             var_DelCallback( p_input, "state", PLItemChanged, this );
566             vlc_object_release( p_input );
567             emit inputChanged( NULL );
568             p_input = NULL;
569             vlc_mutex_unlock( &p_intf->change_lock );
570             return;
571         }
572
573         if( !p_input )
574         {
575             QPL_LOCK;
576             p_input = THEPL->p_input;
577             if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
578             {
579                 vlc_object_yield( p_input );
580                 var_AddCallback( p_input, "state", PLItemChanged, this );
581                 emit inputChanged( p_input );
582             }
583             else
584                 p_input = NULL;
585             QPL_UNLOCK;
586         }
587         vlc_mutex_unlock( &p_intf->change_lock );
588     }
589     else
590     {
591         /* we are working as a dialogs provider */
592         playlist_t *p_playlist = pl_Yield( p_intf );
593         p_input = playlist_CurrentInput( p_playlist );
594         emit inputChanged( p_input );
595         vlc_object_release( p_input );
596         pl_Release( p_intf );
597     }
598 }
599
600 /* Playlist Control functions */
601 void MainInputManager::stop()
602 {
603    playlist_Stop( THEPL );
604 }
605
606 void MainInputManager::next()
607 {
608    playlist_Next( THEPL );
609 }
610
611 void MainInputManager::prev()
612 {
613    playlist_Prev( THEPL );
614 }
615
616 void MainInputManager::togglePlayPause()
617 {
618     if( p_input == NULL )
619     {
620         playlist_Play( THEPL );
621         return;
622     }
623     getIM()->togglePlayPause();
624 }
625
626 bool MainInputManager::teletextState()
627 {
628     im = getIM();
629     if( im->hasInput() )
630     {
631         vlc_value_t val;
632         vlc_object_t *p_vbi;
633         p_vbi = (vlc_object_t *) vlc_object_find_name( getInput(),
634                     "zvbi", FIND_ANYWHERE );
635         if( p_vbi )
636         {
637             vlc_object_release( p_vbi );
638             return true;
639         }
640         var_Change( getInput(), "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
641         return (val.i_int > 0);
642     }
643     return false;
644 }
645
646 /* Static callbacks */
647
648 /* IM */
649 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
650                            vlc_value_t oldval, vlc_value_t newval, void *param )
651 {
652     static int counter = 0;
653     InputManager *im = (InputManager*)param;
654
655     counter = ++counter % 4;
656     if(!counter)
657         return VLC_SUCCESS;
658     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
659     QApplication::postEvent( im, static_cast<QEvent*>(event) );
660     return VLC_SUCCESS;
661 }
662
663 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
664                            vlc_value_t oldval, vlc_value_t newval, void *param )
665 {
666     InputManager *im = (InputManager*)param;
667
668     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
669     QApplication::postEvent( im, static_cast<QEvent*>(event) );
670     return VLC_SUCCESS;
671 }
672
673 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
674                            vlc_value_t oldval, vlc_value_t newval, void *param )
675 {
676     InputManager *im = (InputManager*)param;
677
678     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
679     QApplication::postEvent( im, static_cast<QEvent*>(event) );
680     return VLC_SUCCESS;
681 }
682
683 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
684                            vlc_value_t oldval, vlc_value_t newval, void *param )
685 {
686     InputManager *im = (InputManager*)param;
687
688     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
689     QApplication::postEvent( im, static_cast<QEvent*>(event) );
690     return VLC_SUCCESS;
691 }
692
693 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
694                         vlc_value_t oldval, vlc_value_t newval, void *param )
695 {
696     InputManager *im = (InputManager*)param;
697
698     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
699     QApplication::postEvent( im, static_cast<QEvent*>(event) );
700     return VLC_SUCCESS;
701 }
702
703 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
704                         vlc_value_t n, void *param )
705 {
706     InputManager *im = (InputManager*)param;
707     IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
708     QApplication::postEvent( im, static_cast<QEvent*>(event) );
709     return VLC_SUCCESS;
710
711 }
712
713 /* MIM */
714 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
715                         vlc_value_t oldval, vlc_value_t newval, void *param )
716 {
717     MainInputManager *mim = (MainInputManager*)param;
718
719     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
720     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
721     return VLC_SUCCESS;
722 }
723
724 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
725                         vlc_value_t oldval, vlc_value_t newval, void *param )
726 {
727     MainInputManager *mim = (MainInputManager*)param;
728
729     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
730     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
731     return VLC_SUCCESS;
732 }
733