]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Merge branch 'master' of git@git.videolan.org:vlc
[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         var_SetInteger( p_input, "title 0", 2 );
387 }
388
389 #ifdef ZVBI_COMPILED
390 void InputManager::telexGotoPage( int page )
391 {
392     if( hasInput() )
393     {
394         vlc_object_t *p_vbi;
395         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
396                     "zvbi", FIND_ANYWHERE );
397         if( p_vbi )
398         {
399             var_SetInteger( p_vbi, "vbi-page", page );
400             vlc_object_release( p_vbi );
401         }
402     }
403 }
404
405 void InputManager::telexToggle( bool b_enabled )
406 {
407     int i_page = b_enabled ? 100 : 0 ;
408
409     telexGotoPage( i_page );
410 }
411
412 void InputManager::telexSetTransparency( bool b_transp )
413 {
414     if( hasInput() )
415     {
416         vlc_object_t *p_vbi;
417         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
418                     "zvbi", FIND_ANYWHERE );
419         if( p_vbi )
420         {
421             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
422             vlc_object_release( p_vbi );
423         }
424     }
425 }
426 #endif
427
428 void InputManager::slower()
429 {
430     if( hasInput() )
431         var_SetVoid( p_input, "rate-slower" );
432 }
433
434 void InputManager::faster()
435 {
436     if( hasInput() )
437         var_SetVoid( p_input, "rate-faster" );
438 }
439
440 void InputManager::normalRate()
441 {
442     if( hasInput() )
443         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
444 }
445
446 void InputManager::setRate( int new_rate )
447 {
448     if( hasInput() )
449         var_SetInteger( p_input, "rate", new_rate );
450 }
451
452 /**********************************************************************
453  * MainInputManager implementation. Wrap an input manager and
454  * take care of updating the main playlist input.
455  * Used in the main playlist Dialog
456  **********************************************************************/
457 MainInputManager * MainInputManager::instance = NULL;
458
459 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
460                  : QObject(NULL), p_intf( _p_intf )
461 {
462     p_input = NULL;
463     im = new InputManager( this, p_intf );
464
465 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
466     var_AddCallback( THEPL, "item-change", ItemChanged, im );
467     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
468     var_AddCallback( THEPL, "activity", PLItemChanged, this );
469
470     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
471
472     // No necessary, I think TODO REMOVE ME at the end
473     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
474
475     /* Warn our embedded IM about input changes */
476     CONNECT( this, inputChanged( input_thread_t * ),
477              im, setInput( input_thread_t * ) );
478 }
479
480 MainInputManager::~MainInputManager()
481 {
482     if( p_input )
483     {
484        var_DelCallback( p_input, "state", PLItemChanged, this );
485        vlc_object_release( p_input );
486        emit inputChanged( NULL );
487     }
488
489     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
490
491     var_DelCallback( THEPL, "activity", PLItemChanged, this );
492     var_DelCallback( THEPL, "item-change", ItemChanged, im );
493 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
494
495     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
496 }
497
498 void MainInputManager::customEvent( QEvent *event )
499 {
500     int type = event->type();
501     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
502         return;
503
504     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
505     if( type == VolumeChanged_Type )
506     {
507         emit volumeChanged();
508         return;
509     }
510
511     /* Should be PLItemChanged Event */
512     if( VLC_OBJECT_INTF == p_intf->i_object_type )
513     {
514         vlc_mutex_lock( &p_intf->change_lock );
515         if( p_input && ( p_input->b_dead || p_input->b_die ) )
516         {
517             var_DelCallback( p_input, "state", PLItemChanged, this );
518             vlc_object_release( p_input );
519             emit inputChanged( NULL );
520             p_input = NULL;
521             vlc_mutex_unlock( &p_intf->change_lock );
522             return;
523         }
524
525         if( !p_input )
526         {
527             QPL_LOCK;
528             p_input = THEPL->p_input;
529             if( p_input && !( p_input->b_die || p_input->b_dead) )
530             {
531                 vlc_object_yield( p_input );
532                 var_AddCallback( p_input, "state", PLItemChanged, this );
533                 emit inputChanged( p_input );
534             }
535             else
536                 p_input = NULL;
537             QPL_UNLOCK;
538         }
539         vlc_mutex_unlock( &p_intf->change_lock );
540     }
541     else
542     {
543         /* we are working as a dialogs provider */
544         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
545                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
546         if( p_playlist )
547         {
548             p_input = p_playlist->p_input;
549             emit inputChanged( p_input );
550         }
551     }
552 }
553
554 /* Playlist Control functions */
555 void MainInputManager::stop()
556 {
557    playlist_Stop( THEPL );
558 }
559
560 void MainInputManager::next()
561 {
562    playlist_Next( THEPL );
563 }
564
565 void MainInputManager::prev()
566 {
567    playlist_Prev( THEPL );
568 }
569
570 void MainInputManager::togglePlayPause()
571 {
572     if( p_input == NULL )
573     {
574         playlist_Play( THEPL );
575         return;
576     }
577     getIM()->togglePlayPause();
578 }
579
580 /* Static callbacks */
581
582 /* IM */
583 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
584                            vlc_value_t oldval, vlc_value_t newval, void *param )
585 {
586     static int counter = 0;
587     InputManager *im = (InputManager*)param;
588
589     counter = ++counter % 4;
590     if(!counter)
591         return VLC_SUCCESS;
592     IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
593     QApplication::postEvent( im, static_cast<QEvent*>(event) );
594     return VLC_SUCCESS;
595 }
596
597 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
598                            vlc_value_t oldval, vlc_value_t newval, void *param )
599 {
600     InputManager *im = (InputManager*)param;
601
602     IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
603     QApplication::postEvent( im, static_cast<QEvent*>(event) );
604     return VLC_SUCCESS;
605 }
606
607 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
608                            vlc_value_t oldval, vlc_value_t newval, void *param )
609 {
610     InputManager *im = (InputManager*)param;
611
612     IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
613     QApplication::postEvent( im, static_cast<QEvent*>(event) );
614     return VLC_SUCCESS;
615 }
616
617 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
618                            vlc_value_t oldval, vlc_value_t newval, void *param )
619 {
620     InputManager *im = (InputManager*)param;
621
622     IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
623     QApplication::postEvent( im, static_cast<QEvent*>(event) );
624     return VLC_SUCCESS;
625 }
626
627 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
628                         vlc_value_t oldval, vlc_value_t newval, void *param )
629 {
630     InputManager *im = (InputManager*)param;
631
632     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
633     QApplication::postEvent( im, static_cast<QEvent*>(event) );
634     return VLC_SUCCESS;
635 }
636
637
638 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
639                         vlc_value_t n, void *param )
640 {
641     InputManager *im = (InputManager*)param;
642     im->b_has_audio = true;
643     return VLC_SUCCESS;
644 }
645
646 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
647                         vlc_value_t n, void *param )
648 {
649     InputManager *im = (InputManager*)param;
650     im->b_has_video = true;
651     return VLC_SUCCESS;
652 }
653
654 /* MIM */
655 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
656                         vlc_value_t oldval, vlc_value_t newval, void *param )
657 {
658     MainInputManager *mim = (MainInputManager*)param;
659
660     IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
661     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
662     return VLC_SUCCESS;
663 }
664
665 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
666                         vlc_value_t oldval, vlc_value_t newval, void *param )
667 {
668     MainInputManager *mim = (MainInputManager*)param;
669
670     IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
671     QApplication::postEvent( mim, static_cast<QEvent*>(event) );
672     return VLC_SUCCESS;
673 }
674