]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Fix teletex buttons
[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     emit setNewTelexPage( page );
420 }
421
422 void InputManager::telexToggle( bool b_enabled )
423 {
424     int i_page = 100;
425
426     if( hasInput() )
427     {
428         vlc_value_t val;
429         vlc_object_t *p_vbi;
430         var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
431         b_enabled = (val.i_int > 0);
432         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
433                     "zvbi", FIND_ANYWHERE );
434         if( p_vbi )
435         {
436             i_page = var_GetInteger( p_vbi, "vbi-page" );
437             vlc_object_release( p_vbi );
438             i_page = b_enabled ? i_page : 0;
439             telexGotoPage( i_page );
440         }
441     }
442     emit teletextEnabled( b_enabled );
443 }
444
445 void InputManager::telexToggleButtons()
446 {
447     emit toggleTelexButtons();
448 }
449
450 void InputManager::telexSetTransparency()
451 {
452     if( hasInput() )
453     {
454         vlc_object_t *p_vbi;
455         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
456                     "zvbi", FIND_ANYWHERE );
457         if( p_vbi )
458         {
459             var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
460             b_transparentTelextext = !b_transparentTelextext;
461             vlc_object_release( p_vbi );
462         }
463     }
464     emit toggleTelexTransparency();
465 }
466
467 void InputManager::slower()
468 {
469     if( hasInput() )
470         var_SetVoid( p_input, "rate-slower" );
471 }
472
473 void InputManager::faster()
474 {
475     if( hasInput() )
476         var_SetVoid( p_input, "rate-faster" );
477 }
478
479 void InputManager::normalRate()
480 {
481     if( hasInput() )
482         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
483 }
484
485 void InputManager::setRate( int new_rate )
486 {
487     if( hasInput() )
488         var_SetInteger( p_input, "rate", new_rate );
489 }
490
491 /**********************************************************************
492  * MainInputManager implementation. Wrap an input manager and
493  * take care of updating the main playlist input.
494  * Used in the main playlist Dialog
495  **********************************************************************/
496 MainInputManager * MainInputManager::instance = NULL;
497
498 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
499                  : QObject(NULL), p_intf( _p_intf )
500 {
501     p_input = NULL;
502     im = new InputManager( this, p_intf );
503
504 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
505     var_AddCallback( THEPL, "item-change", ItemChanged, im );
506     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
507     var_AddCallback( THEPL, "activity", PLItemChanged, this );
508
509     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
510
511     // No necessary, I think TODO REMOVE ME at the end
512     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
513
514     /* Warn our embedded IM about input changes */
515     CONNECT( this, inputChanged( input_thread_t * ),
516              im, setInput( input_thread_t * ) );
517
518     /* emit check if playlist has allready started playing */
519     vlc_value_t val;
520     var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
521     IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
522     QApplication::postEvent( this, static_cast<QEvent*>(event) );
523
524 }
525
526 MainInputManager::~MainInputManager()
527 {
528     if( p_input )
529     {
530        var_DelCallback( p_input, "state", PLItemChanged, this );
531        vlc_object_release( p_input );
532        emit inputChanged( NULL );
533     }
534
535     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
536
537     var_DelCallback( THEPL, "activity", PLItemChanged, this );
538     var_DelCallback( THEPL, "item-change", ItemChanged, im );
539 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
540
541     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
542 }
543
544 void MainInputManager::customEvent( QEvent *event )
545 {
546     int type = event->type();
547     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
548         return;
549
550     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
551     if( type == VolumeChanged_Type )
552     {
553         emit volumeChanged();
554         return;
555     }
556
557     /* Should be PLItemChanged Event */
558     if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
559     {
560         vlc_mutex_lock( &p_intf->change_lock );
561         if( p_input && ( p_input->b_dead || p_input->b_die ) )
562         {
563             var_DelCallback( p_input, "state", PLItemChanged, this );
564             vlc_object_release( p_input );
565             emit inputChanged( NULL );
566             p_input = NULL;
567             vlc_mutex_unlock( &p_intf->change_lock );
568             return;
569         }
570
571         if( !p_input )
572         {
573             QPL_LOCK;
574             p_input = THEPL->p_input;
575             if( p_input && !( p_input->b_die || p_input->b_dead) )
576             {
577                 vlc_object_yield( p_input );
578                 var_AddCallback( p_input, "state", PLItemChanged, this );
579                 emit inputChanged( p_input );
580             }
581             else
582                 p_input = NULL;
583             QPL_UNLOCK;
584         }
585         vlc_mutex_unlock( &p_intf->change_lock );
586     }
587     else
588     {
589         /* we are working as a dialogs provider */
590         playlist_t *p_playlist = pl_Yield( p_intf );
591         p_input = p_playlist->p_input;
592         emit inputChanged( p_input );
593     }
594 }
595
596 /* Playlist Control functions */
597 void MainInputManager::stop()
598 {
599    playlist_Stop( THEPL );
600 }
601
602 void MainInputManager::next()
603 {
604    playlist_Next( THEPL );
605 }
606
607 void MainInputManager::prev()
608 {
609    playlist_Prev( THEPL );
610 }
611
612 void MainInputManager::togglePlayPause()
613 {
614     if( p_input == NULL )
615     {
616         playlist_Play( THEPL );
617         return;
618     }
619     getIM()->togglePlayPause();
620 }
621
622 bool MainInputManager::teletextState()
623 {
624     im = getIM();
625     if( im->hasInput() )
626     {
627         vlc_value_t val;
628         vlc_object_t *p_vbi;
629         p_vbi = (vlc_object_t *) vlc_object_find_name( getInput(),
630                     "zvbi", FIND_ANYWHERE );
631         if( p_vbi )
632         {
633             vlc_object_release( p_vbi );
634             return true;
635         }
636         var_Change( getInput(), "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
637         return (val.i_int > 0);
638     }
639     return false;
640 }
641
642 /* Static callbacks */
643
644 /* IM */
645 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
646                            vlc_value_t oldval, vlc_value_t newval, void *param )
647 {
648     static int counter = 0;
649     InputManager *im = (InputManager*)param;
650
651     counter = ++counter % 4;
652     if(!counter)
653         return VLC_SUCCESS;
654     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
655     QApplication::postEvent( im, static_cast<QEvent*>(event) );
656     return VLC_SUCCESS;
657 }
658
659 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
660                            vlc_value_t oldval, vlc_value_t newval, void *param )
661 {
662     InputManager *im = (InputManager*)param;
663
664     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
665     QApplication::postEvent( im, static_cast<QEvent*>(event) );
666     return VLC_SUCCESS;
667 }
668
669 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
670                            vlc_value_t oldval, vlc_value_t newval, void *param )
671 {
672     InputManager *im = (InputManager*)param;
673
674     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
675     QApplication::postEvent( im, static_cast<QEvent*>(event) );
676     return VLC_SUCCESS;
677 }
678
679 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
680                            vlc_value_t oldval, vlc_value_t newval, void *param )
681 {
682     InputManager *im = (InputManager*)param;
683
684     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
685     QApplication::postEvent( im, static_cast<QEvent*>(event) );
686     return VLC_SUCCESS;
687 }
688
689 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
690                         vlc_value_t oldval, vlc_value_t newval, void *param )
691 {
692     InputManager *im = (InputManager*)param;
693
694     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
695     QApplication::postEvent( im, static_cast<QEvent*>(event) );
696     return VLC_SUCCESS;
697 }
698
699 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
700                         vlc_value_t n, void *param )
701 {
702     InputManager *im = (InputManager*)param;
703     IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
704     QApplication::postEvent( im, static_cast<QEvent*>(event) );
705     return VLC_SUCCESS;
706
707 }
708
709 /* MIM */
710 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
711                         vlc_value_t oldval, vlc_value_t newval, void *param )
712 {
713     MainInputManager *mim = (MainInputManager*)param;
714
715     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
716     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
717     return VLC_SUCCESS;
718 }
719
720 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
721                         vlc_value_t oldval, vlc_value_t newval, void *param )
722 {
723     MainInputManager *mim = (MainInputManager*)param;
724
725     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
726     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
727     return VLC_SUCCESS;
728 }
729