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