]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
4b81c073dee98f8f69049c87ec33d6de6f8e945d
[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     old_name     = "";
65     artUrl       = "";
66     p_input      = NULL;
67     i_rate       = 0;
68     i_input_id   = 0;
69 }
70
71 InputManager::~InputManager()
72 {
73     delInput();
74 }
75
76 /* Define the Input used.
77    Add the callbacks on input
78    p_input is yield once here */
79 void InputManager::setInput( input_thread_t *_p_input )
80 {
81     delInput();
82     p_input = _p_input;
83     b_had_audio = b_had_video = b_has_audio = b_has_video = false;
84     if( p_input && !( p_input->b_dead || p_input->b_die ) )
85     {
86         vlc_object_yield( p_input );
87         emit statusChanged( PLAYING_S );
88         UpdateMeta();
89         UpdateTracks();
90         UpdateNavigation();
91         UpdateArt();
92         addCallbacks();
93         i_input_id = input_GetItem( p_input )->i_id;
94     }
95     else
96     {
97         p_input = NULL;
98         i_input_id = 0;
99         emit rateChanged( INPUT_RATE_DEFAULT );
100     }
101 }
102
103 /* delete Input if it ever existed.
104    Delete the callbacls on input
105    p_input is released once here */
106 void InputManager::delInput()
107 {
108     if( p_input )
109     {
110         delCallbacks();
111         i_old_playing_status = END_S;
112         i_input_id = 0;
113         old_name   = "";
114         artUrl     = "";
115         emit positionUpdated( 0.0, 0 ,0 );
116         emit statusChanged( END_S );
117         emit nameChanged( "" );
118         emit artChanged( "" );
119         emit rateChanged( INPUT_RATE_DEFAULT );
120         vlc_object_release( p_input );
121         p_input = NULL;
122     }
123 }
124
125 /* Add the callbacks on Input. Self explanatory */
126 void InputManager::addCallbacks()
127 {
128     /* We don't care about:
129        - spu-es
130        - chapter
131        - programs
132        - audio-delay
133        - spu-delay
134        - bookmark
135        - position, time, length, because they are included in intf-change
136      */
137     /* src/input/input.c:1629 */
138     var_AddCallback( p_input, "state", ItemStateChanged, this );
139     /* src/input/es-out.c:550 */
140     var_AddCallback( p_input, "audio-es", ChangeAudio, this );
141     /* src/input/es-out.c:551 */
142     var_AddCallback( p_input, "video-es", ChangeVideo, this );
143     /* src/input/input.c:1765 */
144     var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
145     /* src/input/input.c:2003 */
146     var_AddCallback( p_input, "title", ItemTitleChanged, this );
147     /* src/input/input.c:734 for timers update*/
148     var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
149 }
150
151 /* Delete the callbacks on Input. Self explanatory */
152 void InputManager::delCallbacks()
153 {
154     var_DelCallback( p_input, "audio-es", ChangeAudio, this );
155     var_DelCallback( p_input, "video-es", ChangeVideo, this );
156     var_DelCallback( p_input, "state", ItemStateChanged, this );
157     var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
158     var_DelCallback( p_input, "title", ItemTitleChanged, this );
159     var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
160 }
161
162 /* Convert the event from the callbacks in actions */
163 void InputManager::customEvent( QEvent *event )
164 {
165     int type = event->type();
166     IMEvent *ple = static_cast<IMEvent *>(event);
167
168     if ( type != PositionUpdate_Type &&
169          type != ItemChanged_Type &&
170          type != ItemRateChanged_Type &&
171          type != ItemTitleChanged_Type &&
172          type != ItemStateChanged_Type )
173         return;
174
175     if( !hasInput() ) return;
176
177     if( ( type != PositionUpdate_Type && type != ItemRateChanged_Type ) &&
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         UpdateTracks();
194         UpdateStatus();
195         UpdateArt();
196         break;
197     case ItemRateChanged_Type:
198         UpdateRate();
199         break;
200     case ItemTitleChanged_Type:
201         UpdateNavigation();
202         UpdateMeta();
203         break;
204     case ItemStateChanged_Type:
205         UpdateTracks();
206         UpdateStatus();
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 void InputManager::UpdateTracks()
305 {
306     /* Has Audio, has Video Tracks ? */
307     vlc_value_t val;
308     var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
309     b_has_audio = val.i_int > 0;
310     var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
311     b_has_video = val.i_int > 0;
312
313     msg_Dbg( p_input, "I have audio-video: %i %i", b_has_audio, b_has_video );
314
315     /* Update ZVBI status */
316 #ifdef ZVBI_COMPILED
317     /* Update teletext status*/
318     emit teletextEnabled( true );/* FIXME */
319 #endif
320 }
321
322 void InputManager::UpdateArt()
323 {
324     /* Update Art meta */
325     QString url;
326     char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
327     url.sprintf("%s", psz_art );
328     free( psz_art );
329     if( artUrl != url )
330     {
331         artUrl = url.replace( "file://",QString("" ) );
332         /* Taglib seems to define a attachment://, It won't work yet */
333         artUrl = url.replace( "attachment://",QString("" ) );
334         emit artChanged( artUrl );
335         msg_Dbg( p_intf, "Art:  %s", qtu( artUrl ) );
336     }
337 }
338
339 /* User update of the slider */
340 void InputManager::sliderUpdate( float new_pos )
341 {
342     if( hasInput() )
343         var_SetFloat( p_input, "position", new_pos );
344 }
345
346 /* User togglePlayPause */
347 void InputManager::togglePlayPause()
348 {
349     vlc_value_t state;
350     var_Get( p_input, "state", &state );
351     state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
352     var_Set( p_input, "state", state );
353     emit statusChanged( state.i_int );
354 }
355
356 void InputManager::sectionPrev()
357 {
358     if( hasInput() )
359     {
360         int i_type = var_Type( p_input, "next-chapter" );
361         vlc_value_t val; val.b_bool = VLC_TRUE;
362         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
363                             "prev-chapter":"prev-title", val );
364     }
365 }
366
367 void InputManager::sectionNext()
368 {
369     if( hasInput() )
370     {
371         int i_type = var_Type( p_input, "next-chapter" );
372         vlc_value_t val; val.b_bool = VLC_TRUE;
373         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
374                             "next-chapter":"next-title", val );
375     }
376 }
377
378 void InputManager::sectionMenu()
379 {
380     if( hasInput() )
381         var_SetInteger( p_input, "title 0", 2 );
382 }
383
384 #ifdef ZVBI_COMPILED
385 void InputManager::telexGotoPage( int page )
386 {
387     if( hasInput() )
388     {
389         vlc_object_t *p_vbi;
390         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
391                     "zvbi", FIND_ANYWHERE );
392         if( p_vbi )
393         {
394             var_SetInteger( p_vbi, "vbi-page", page );
395             vlc_object_release( p_vbi );
396         }
397     }
398 }
399
400 void InputManager::telexToggle( bool b_enabled )
401 {
402     int i_page = b_enabled ? 100 : 0 ;
403
404     telexGotoPage( i_page );
405 }
406
407 void InputManager::telexSetTransparency( bool b_transp )
408 {
409     if( hasInput() )
410     {
411         vlc_object_t *p_vbi;
412         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
413                     "zvbi", FIND_ANYWHERE );
414         if( p_vbi )
415         {
416             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
417             vlc_object_release( p_vbi );
418         }
419     }
420 }
421 #endif
422
423 void InputManager::slower()
424 {
425     if( hasInput() )
426         var_SetVoid( p_input, "rate-slower" );
427 }
428
429 void InputManager::faster()
430 {
431     if( hasInput() )
432         var_SetVoid( p_input, "rate-faster" );
433 }
434
435 void InputManager::normalRate()
436 {
437     if( hasInput() )
438         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
439 }
440
441 void InputManager::setRate( int new_rate )
442 {
443     if( hasInput() )
444         var_SetInteger( p_input, "rate", new_rate );
445 }
446
447 /**********************************************************************
448  * MainInputManager implementation. Wrap an input manager and
449  * take care of updating the main playlist input.
450  * Used in the main playlist Dialog
451  **********************************************************************/
452 MainInputManager * MainInputManager::instance = NULL;
453
454 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
455                  : QObject(NULL), p_intf( _p_intf )
456 {
457     p_input = NULL;
458     im = new InputManager( this, p_intf );
459
460 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
461     var_AddCallback( THEPL, "item-change", ItemChanged, im );
462     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
463     var_AddCallback( THEPL, "activity", PLItemChanged, this );
464
465     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
466
467     // No necessary, I think TODO REMOVE ME at the end
468     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
469
470     /* Warn our embedded IM about input changes */
471     CONNECT( this, inputChanged( input_thread_t * ),
472              im, setInput( input_thread_t * ) );
473 }
474
475 MainInputManager::~MainInputManager()
476 {
477     if( p_input )
478     {
479        var_DelCallback( p_input, "state", PLItemChanged, this );
480        vlc_object_release( p_input );
481        emit inputChanged( NULL );
482     }
483
484     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
485
486     var_DelCallback( THEPL, "activity", PLItemChanged, this );
487     var_DelCallback( THEPL, "item-change", ItemChanged, im );
488 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
489
490     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
491 }
492
493 void MainInputManager::customEvent( QEvent *event )
494 {
495     int type = event->type();
496     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
497         return;
498
499     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
500     if( type == VolumeChanged_Type )
501     {
502         emit volumeChanged();
503         return;
504     }
505
506     /* Should be PLItemChanged Event */
507     if( VLC_OBJECT_INTF == p_intf->i_object_type )
508     {
509         vlc_mutex_lock( &p_intf->change_lock );
510         if( p_input && ( p_input->b_dead || p_input->b_die ) )
511         {
512             var_DelCallback( p_input, "state", PLItemChanged, this );
513             vlc_object_release( p_input );
514             emit inputChanged( NULL );
515             p_input = NULL;
516             vlc_mutex_unlock( &p_intf->change_lock );
517             return;
518         }
519
520         if( !p_input )
521         {
522             QPL_LOCK;
523             p_input = THEPL->p_input;
524             if( p_input && !( p_input->b_die || p_input->b_dead) )
525             {
526                 vlc_object_yield( p_input );
527                 var_AddCallback( p_input, "state", PLItemChanged, this );
528                 emit inputChanged( p_input );
529             }
530             else
531                 p_input = NULL;
532             QPL_UNLOCK;
533         }
534         vlc_mutex_unlock( &p_intf->change_lock );
535     }
536     else
537     {
538         /* we are working as a dialogs provider */
539         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
540                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
541         if( p_playlist )
542         {
543             p_input = p_playlist->p_input;
544             emit inputChanged( p_input );
545         }
546     }
547 }
548
549 /* Playlist Control functions */
550 void MainInputManager::stop()
551 {
552    playlist_Stop( THEPL );
553 }
554
555 void MainInputManager::next()
556 {
557    playlist_Next( THEPL );
558 }
559
560 void MainInputManager::prev()
561 {
562    playlist_Prev( THEPL );
563 }
564
565 void MainInputManager::togglePlayPause()
566 {
567     if( p_input == NULL )
568     {
569         playlist_Play( THEPL );
570         return;
571     }
572     getIM()->togglePlayPause();
573 }
574
575 /* Static callbacks */
576
577 /* IM */
578 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
579                            vlc_value_t oldval, vlc_value_t newval, void *param )
580 {
581     static int counter = 0;
582     InputManager *im = (InputManager*)param;
583
584     counter = counter++ % 4;
585     if(!counter)
586         return VLC_SUCCESS;
587     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
588     QApplication::postEvent( im, static_cast<QEvent*>(event) );
589     return VLC_SUCCESS;
590 }
591
592 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
593                             vlc_value_t oldval, vlc_value_t newval, void *param )
594 {
595     InputManager *im = (InputManager*)param;
596
597     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
598     QApplication::postEvent( im, static_cast<QEvent*>(event) );
599     return VLC_SUCCESS;
600 }
601
602 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
603                             vlc_value_t oldval, vlc_value_t newval, void *param )
604 {
605     InputManager *im = (InputManager*)param;
606     
607     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
608     QApplication::postEvent( im, static_cast<QEvent*>(event) );
609     return VLC_SUCCESS;
610 }
611
612 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
613                             vlc_value_t oldval, vlc_value_t newval, void *param )
614 {
615     InputManager *im = (InputManager*)param;
616
617     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
618     QApplication::postEvent( im, static_cast<QEvent*>(event) );
619     return VLC_SUCCESS;
620 }
621
622 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
623                         vlc_value_t oldval, vlc_value_t newval, void *param )
624 {
625     InputManager *im = (InputManager*)param;
626
627     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
628     QApplication::postEvent( im, static_cast<QEvent*>(event) );
629     return VLC_SUCCESS;
630 }
631
632
633 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
634                         vlc_value_t n, void *param )
635 {
636     InputManager *im = (InputManager*)param;
637     im->b_has_audio = true;
638     return VLC_SUCCESS;
639 }
640
641 static int ChangeVideo( 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_video = true;
646     return VLC_SUCCESS;
647 }
648
649 /* MIM */
650 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
651                         vlc_value_t oldval, vlc_value_t newval, void *param )
652 {
653     MainInputManager *mim = (MainInputManager*)param;
654
655     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
656     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
657     return VLC_SUCCESS;
658 }
659
660 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
661                         vlc_value_t oldval, vlc_value_t newval, void *param )
662 {
663     MainInputManager *mim = (MainInputManager*)param;
664
665     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
666     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
667     return VLC_SUCCESS;
668 }
669