]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Fix toggling of transparency on teletext pages. Keep in mind that the subpicture...
[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 || p_input->b_die ) )
82     {
83         vlc_object_yield( p_input );
84         emit statusChanged( PLAYING_S );
85         UpdateMeta();
86         UpdateNavigation();
87         UpdateArt();
88         UpdateSPU();
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         UpdateNavigation();
193         UpdateStatus();
194         UpdateArt();
195         break;
196     case ItemRateChanged_Type:
197         UpdateRate();
198         break;
199     case ItemTitleChanged_Type:
200         UpdateNavigation();
201         UpdateMeta();
202         break;
203     case ItemStateChanged_Type:
204         UpdateStatus();
205     case ItemSpuChanged_Type:
206         UpdateSPU();
207         break;
208     }
209 }
210
211 void InputManager::UpdatePosition()
212 {
213     /* Update position */
214     int i_length, i_time; /* Int is enough, since we store seconds */
215     float f_pos;
216     i_length = var_GetTime(  p_input , "length" ) / 1000000;
217     i_time = var_GetTime(  p_input , "time") / 1000000;
218     f_pos = var_GetFloat(  p_input , "position" );
219     emit positionUpdated( f_pos, i_time, i_length );
220 }
221
222 void InputManager::UpdateNavigation()
223 {
224     /* Update navigation status */
225     vlc_value_t val; val.i_int = 0;
226     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
227     if( val.i_int > 0 )
228     {
229         val.i_int = 0;
230         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
231         emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
232     }
233     else
234     {
235         emit navigationChanged( 0 );
236     }
237 }
238
239 void InputManager::UpdateStatus()
240 {
241     /* Update playing status */
242     vlc_value_t val; val.i_int = 0;
243     var_Get( p_input, "state", &val );
244     if( i_old_playing_status != val.i_int )
245     {
246         i_old_playing_status = val.i_int;
247         emit statusChanged( val.i_int );
248     }
249 }
250
251 void InputManager::UpdateRate()
252 {
253     /* Update Rate */
254     int i_new_rate = var_GetInteger( p_input, "rate");
255     if( i_new_rate != i_rate )
256     {
257         i_rate = i_new_rate;
258         /* Update rate */
259         emit rateChanged( i_rate );
260     }
261 }
262
263 void InputManager::UpdateMeta()
264 {
265     /* Update text, name and nowplaying */
266     QString text;
267
268     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
269     if( EMPTY_STR( psz_name ) )
270     {
271         free( psz_name );
272         psz_name = input_item_GetName( input_GetItem( p_input ) );
273     }
274
275     char *psz_nowplaying =
276         input_item_GetNowPlaying( input_GetItem( p_input ) );
277     if( !EMPTY_STR( psz_nowplaying ) )
278     {
279         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
280     }
281     else
282     {
283         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
284         if( !EMPTY_STR( psz_artist ) )
285         {
286             text.sprintf( "%s - %s", psz_artist, psz_name );
287         }
288         else
289         {
290             text.sprintf( "%s", psz_name );
291         }
292         free( psz_artist );
293     }
294     free( psz_name );
295     free( psz_nowplaying );
296
297     if( old_name != text )
298     {
299         emit nameChanged( text );
300         old_name=text;
301     }
302 }
303
304 bool InputManager::hasAudio()
305 {
306     if( hasInput() )
307     {
308         vlc_value_t val;
309         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
310         return val.i_int > 0;
311     }
312     return false;
313 }
314
315 bool InputManager::hasVideo()
316 {
317     if( hasInput() )
318     {
319         vlc_value_t val;
320         var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
321         return val.i_int > 0;
322     }
323     return false;
324
325 }
326
327 void InputManager::UpdateSPU()
328 {
329     if( hasInput() )
330     {
331         vlc_value_t val;
332         var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
333         telexToggle( val.i_int > 0 );
334     }
335     else
336     {
337         telexToggle( false );
338     }
339 }
340
341 void InputManager::UpdateArt()
342 {
343     /* Update Art meta */
344     QString url;
345     char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
346     url.sprintf("%s", psz_art );
347     free( psz_art );
348     if( artUrl != url )
349     {
350         artUrl = url.replace( "file://",QString("" ) );
351         /* Taglib seems to define a attachment://, It won't work yet */
352         artUrl = url.replace( "attachment://",QString("" ) );
353         emit artChanged( artUrl );
354         msg_Dbg( p_intf, "Art:  %s", qtu( artUrl ) );
355     }
356 }
357
358 /* User update of the slider */
359 void InputManager::sliderUpdate( float new_pos )
360 {
361     if( hasInput() )
362         var_SetFloat( p_input, "position", new_pos );
363 }
364
365 /* User togglePlayPause */
366 void InputManager::togglePlayPause()
367 {
368     vlc_value_t state;
369     var_Get( p_input, "state", &state );
370     state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
371     var_Set( p_input, "state", state );
372     emit statusChanged( state.i_int );
373 }
374
375 void InputManager::sectionPrev()
376 {
377     if( hasInput() )
378     {
379         int i_type = var_Type( p_input, "next-chapter" );
380         vlc_value_t val; val.b_bool = true;
381         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
382                             "prev-chapter":"prev-title", val );
383     }
384 }
385
386 void InputManager::sectionNext()
387 {
388     if( hasInput() )
389     {
390         int i_type = var_Type( p_input, "next-chapter" );
391         vlc_value_t val; val.b_bool = true;
392         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
393                             "next-chapter":"next-title", val );
394     }
395 }
396
397 void InputManager::sectionMenu()
398 {
399     if( hasInput() )
400     {
401         vlc_value_t val; val.i_int = 2;
402         var_Set( p_input, "title  0", val );
403     }
404 }
405
406 void InputManager::telexGotoPage( int page )
407 {
408     if( hasInput() )
409     {
410         vlc_object_t *p_vbi;
411         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
412                     "zvbi", FIND_ANYWHERE );
413         if( p_vbi )
414         {
415             var_SetInteger( p_vbi, "vbi-page", page );
416             vlc_object_release( p_vbi );
417         }
418     }
419 }
420
421 void InputManager::telexToggle( bool b_enabled )
422 {
423     int i_page = 100;
424
425     if( hasInput() )
426     {
427         vlc_value_t val;
428         vlc_object_t *p_vbi;
429         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
430                     "zvbi", FIND_ANYWHERE );
431         if( p_vbi )
432         {
433             i_page = var_GetInteger( p_vbi, "vbi-page" );
434             vlc_object_release( p_vbi );
435         }
436         var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
437         b_enabled = (val.i_int > 0);
438     }
439     emit teletextEnabled( b_enabled );
440     i_page = b_enabled ? i_page : 0;
441     telexGotoPage( i_page );
442 }
443
444 void InputManager::telexSetTransparency()
445 {
446     if( hasInput() )
447     {
448         vlc_object_t *p_vbi;
449         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
450                     "zvbi", FIND_ANYWHERE );
451         if( p_vbi )
452         {
453             var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
454             b_transparentTelextext = !b_transparentTelextext;
455             vlc_object_release( p_vbi );
456         }
457     }
458 }
459
460 void InputManager::slower()
461 {
462     if( hasInput() )
463         var_SetVoid( p_input, "rate-slower" );
464 }
465
466 void InputManager::faster()
467 {
468     if( hasInput() )
469         var_SetVoid( p_input, "rate-faster" );
470 }
471
472 void InputManager::normalRate()
473 {
474     if( hasInput() )
475         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
476 }
477
478 void InputManager::setRate( int new_rate )
479 {
480     if( hasInput() )
481         var_SetInteger( p_input, "rate", new_rate );
482 }
483
484 /**********************************************************************
485  * MainInputManager implementation. Wrap an input manager and
486  * take care of updating the main playlist input.
487  * Used in the main playlist Dialog
488  **********************************************************************/
489 MainInputManager * MainInputManager::instance = NULL;
490
491 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
492                  : QObject(NULL), p_intf( _p_intf )
493 {
494     p_input = NULL;
495     im = new InputManager( this, p_intf );
496
497 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
498     var_AddCallback( THEPL, "item-change", ItemChanged, im );
499     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
500     var_AddCallback( THEPL, "activity", PLItemChanged, this );
501
502     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
503
504     // No necessary, I think TODO REMOVE ME at the end
505     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
506
507     /* Warn our embedded IM about input changes */
508     CONNECT( this, inputChanged( input_thread_t * ),
509              im, setInput( input_thread_t * ) );
510
511     /* emit check if playlist has allready started playing */
512     vlc_value_t val;
513     var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
514     IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
515     QApplication::postEvent( this, static_cast<QEvent*>(event) );
516
517 }
518
519 MainInputManager::~MainInputManager()
520 {
521     if( p_input )
522     {
523        var_DelCallback( p_input, "state", PLItemChanged, this );
524        vlc_object_release( p_input );
525        emit inputChanged( NULL );
526     }
527
528     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
529
530     var_DelCallback( THEPL, "activity", PLItemChanged, this );
531     var_DelCallback( THEPL, "item-change", ItemChanged, im );
532 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
533
534     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
535 }
536
537 void MainInputManager::customEvent( QEvent *event )
538 {
539     int type = event->type();
540     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
541         return;
542
543     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
544     if( type == VolumeChanged_Type )
545     {
546         emit volumeChanged();
547         return;
548     }
549
550     /* Should be PLItemChanged Event */
551     if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
552     {
553         vlc_mutex_lock( &p_intf->change_lock );
554         if( p_input && ( p_input->b_dead || p_input->b_die ) )
555         {
556             var_DelCallback( p_input, "state", PLItemChanged, this );
557             vlc_object_release( p_input );
558             emit inputChanged( NULL );
559             p_input = NULL;
560             vlc_mutex_unlock( &p_intf->change_lock );
561             return;
562         }
563
564         if( !p_input )
565         {
566             QPL_LOCK;
567             p_input = THEPL->p_input;
568             if( p_input && !( p_input->b_die || p_input->b_dead) )
569             {
570                 vlc_object_yield( p_input );
571                 var_AddCallback( p_input, "state", PLItemChanged, this );
572                 emit inputChanged( p_input );
573             }
574             else
575                 p_input = NULL;
576             QPL_UNLOCK;
577         }
578         vlc_mutex_unlock( &p_intf->change_lock );
579     }
580     else
581     {
582         /* we are working as a dialogs provider */
583         playlist_t *p_playlist = pl_Yield( p_intf );
584         p_input = p_playlist->p_input;
585         emit inputChanged( p_input );
586     }
587 }
588
589 /* Playlist Control functions */
590 void MainInputManager::stop()
591 {
592    playlist_Stop( THEPL );
593 }
594
595 void MainInputManager::next()
596 {
597    playlist_Next( THEPL );
598 }
599
600 void MainInputManager::prev()
601 {
602    playlist_Prev( THEPL );
603 }
604
605 void MainInputManager::togglePlayPause()
606 {
607     if( p_input == NULL )
608     {
609         playlist_Play( THEPL );
610         return;
611     }
612     getIM()->togglePlayPause();
613 }
614
615 bool MainInputManager::teletextState()
616 {
617     im = getIM();
618     if( im->hasInput() )
619     {
620         vlc_value_t val;
621         vlc_object_t *p_vbi;
622         p_vbi = (vlc_object_t *) vlc_object_find_name( getInput(),
623                     "zvbi", FIND_ANYWHERE );
624         if( p_vbi )
625         {
626             vlc_object_release( p_vbi );
627             return true;
628         }
629         var_Change( getInput(), "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
630         return (val.i_int > 0);
631     }
632     return false;
633 }
634
635 /* Static callbacks */
636
637 /* IM */
638 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
639                            vlc_value_t oldval, vlc_value_t newval, void *param )
640 {
641     static int counter = 0;
642     InputManager *im = (InputManager*)param;
643
644     counter = ++counter % 4;
645     if(!counter)
646         return VLC_SUCCESS;
647     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
648     QApplication::postEvent( im, static_cast<QEvent*>(event) );
649     return VLC_SUCCESS;
650 }
651
652 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
653                            vlc_value_t oldval, vlc_value_t newval, void *param )
654 {
655     InputManager *im = (InputManager*)param;
656
657     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
658     QApplication::postEvent( im, static_cast<QEvent*>(event) );
659     return VLC_SUCCESS;
660 }
661
662 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
663                            vlc_value_t oldval, vlc_value_t newval, void *param )
664 {
665     InputManager *im = (InputManager*)param;
666
667     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
668     QApplication::postEvent( im, static_cast<QEvent*>(event) );
669     return VLC_SUCCESS;
670 }
671
672 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
673                            vlc_value_t oldval, vlc_value_t newval, void *param )
674 {
675     InputManager *im = (InputManager*)param;
676
677     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
678     QApplication::postEvent( im, static_cast<QEvent*>(event) );
679     return VLC_SUCCESS;
680 }
681
682 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
683                         vlc_value_t oldval, vlc_value_t newval, void *param )
684 {
685     InputManager *im = (InputManager*)param;
686
687     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
688     QApplication::postEvent( im, static_cast<QEvent*>(event) );
689     return VLC_SUCCESS;
690 }
691
692 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
693                         vlc_value_t n, void *param )
694 {
695     InputManager *im = (InputManager*)param;
696     IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
697     QApplication::postEvent( im, static_cast<QEvent*>(event) );
698     return VLC_SUCCESS;
699
700 }
701
702 /* MIM */
703 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
704                         vlc_value_t oldval, vlc_value_t newval, void *param )
705 {
706     MainInputManager *mim = (MainInputManager*)param;
707
708     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
709     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
710     return VLC_SUCCESS;
711 }
712
713 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
714                         vlc_value_t oldval, vlc_value_t newval, void *param )
715 {
716     MainInputManager *mim = (MainInputManager*)param;
717
718     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
719     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
720     return VLC_SUCCESS;
721 }
722