]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Replaced a certain amount of vlc_object_find by pl_Yield
[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         emit inputUnset();
102     }
103 }
104
105 /* delete Input if it ever existed.
106    Delete the callbacls on input
107    p_input is released once here */
108 void InputManager::delInput()
109 {
110     if( p_input )
111     {
112         delCallbacks();
113         i_old_playing_status = END_S;
114         i_input_id = 0;
115         old_name   = "";
116         artUrl     = "";
117         emit positionUpdated( 0.0, 0 ,0 );
118         emit statusChanged( END_S );
119         emit nameChanged( "" );
120         emit artChanged( "" );
121         emit rateChanged( INPUT_RATE_DEFAULT );
122         vlc_object_release( p_input );
123         p_input = NULL;
124     }
125 }
126
127 /* Add the callbacks on Input. Self explanatory */
128 void InputManager::addCallbacks()
129 {
130     /* We don't care about:
131        - spu-es
132        - chapter
133        - programs
134        - audio-delay
135        - spu-delay
136        - bookmark
137        - position, time, length, because they are included in intf-change
138      */
139     /* src/input/input.c:1629 */
140     var_AddCallback( p_input, "state", ItemStateChanged, this );
141     /* src/input/es-out.c:550 */
142     var_AddCallback( p_input, "audio-es", ChangeAudio, this );
143     /* src/input/es-out.c:551 */
144     var_AddCallback( p_input, "video-es", ChangeVideo, this );
145     /* src/input/input.c:1765 */
146     var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
147     /* src/input/input.c:2003 */
148     var_AddCallback( p_input, "title", ItemTitleChanged, this );
149     /* src/input/input.c:734 for timers update*/
150     var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
151 }
152
153 /* Delete the callbacks on Input. Self explanatory */
154 void InputManager::delCallbacks()
155 {
156     var_DelCallback( p_input, "audio-es", ChangeAudio, this );
157     var_DelCallback( p_input, "video-es", ChangeVideo, this );
158     var_DelCallback( p_input, "state", ItemStateChanged, this );
159     var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
160     var_DelCallback( p_input, "title", ItemTitleChanged, this );
161     var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
162 }
163
164 /* Convert the event from the callbacks in actions */
165 void InputManager::customEvent( QEvent *event )
166 {
167     int type = event->type();
168     IMEvent *ple = static_cast<IMEvent *>(event);
169
170     if ( type != PositionUpdate_Type &&
171          type != ItemChanged_Type &&
172          type != ItemRateChanged_Type &&
173          type != ItemTitleChanged_Type &&
174          type != ItemStateChanged_Type )
175         return;
176
177     if( !hasInput() ) return;
178
179     if( ( type != PositionUpdate_Type  &&
180           type != ItemRateChanged_Type &&
181           type != ItemStateChanged_Type
182         )
183         && ( i_input_id != ple->i_id ) )
184         return;
185
186     if( type != PositionUpdate_Type )
187         msg_Dbg( p_intf, "New Event: type %i", type );
188
189     /* Actions */
190     switch( type )
191     {
192     case PositionUpdate_Type:
193         UpdatePosition();
194         break;
195     case ItemChanged_Type:
196         UpdateMeta();
197         UpdateNavigation();
198         UpdateTracks();
199         UpdateStatus();
200         UpdateArt();
201         break;
202     case ItemRateChanged_Type:
203         UpdateRate();
204         break;
205     case ItemTitleChanged_Type:
206         UpdateNavigation();
207         UpdateMeta();
208         break;
209     case ItemStateChanged_Type:
210         UpdateTracks();
211         UpdateStatus();
212         break;
213     }
214 }
215
216 void InputManager::UpdatePosition()
217 {
218     /* Update position */
219     int i_length, i_time; /* Int is enough, since we store seconds */
220     float f_pos;
221     i_length = var_GetTime(  p_input , "length" ) / 1000000;
222     i_time = var_GetTime(  p_input , "time") / 1000000;
223     f_pos = var_GetFloat(  p_input , "position" );
224     emit positionUpdated( f_pos, i_time, i_length );
225 }
226
227 void InputManager::UpdateNavigation()
228 {
229     /* Update navigation status */
230     vlc_value_t val; val.i_int = 0;
231     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
232     if( val.i_int > 0 )
233     {
234         val.i_int = 0;
235         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
236         emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
237     }
238     else
239     {
240         emit navigationChanged( 0 );
241     }
242 }
243
244 void InputManager::UpdateStatus()
245 {
246     /* Update playing status */
247     vlc_value_t val; val.i_int = 0;
248     var_Get( p_input, "state", &val );
249     if( i_old_playing_status != val.i_int )
250     {
251         i_old_playing_status = val.i_int;
252         emit statusChanged( val.i_int );
253     }
254 }
255
256 void InputManager::UpdateRate()
257 {
258     /* Update Rate */
259     int i_new_rate = var_GetInteger( p_input, "rate");
260     if( i_new_rate != i_rate )
261     {
262         i_rate = i_new_rate;
263         /* Update rate */
264         emit rateChanged( i_rate );
265     }
266 }
267
268 void InputManager::UpdateMeta()
269 {
270     /* Update text, name and nowplaying */
271     QString text;
272
273     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
274     if( EMPTY_STR( psz_name ) )
275     {
276         free( psz_name );
277         psz_name = input_item_GetName( input_GetItem( p_input ) );
278     }
279
280     char *psz_nowplaying =
281         input_item_GetNowPlaying( input_GetItem( p_input ) );
282     if( !EMPTY_STR( psz_nowplaying ) )
283     {
284         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
285     }
286     else
287     {
288         char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
289         if( !EMPTY_STR( psz_artist ) )
290         {
291             text.sprintf( "%s - %s", psz_artist, psz_name );
292         }
293         else
294         {
295             text.sprintf( "%s", psz_name );
296         }
297         free( psz_artist );
298     }
299     free( psz_name );
300     free( psz_nowplaying );
301
302     if( old_name != text )
303     {
304         emit nameChanged( text );
305         old_name=text;
306     }
307 }
308
309 void InputManager::UpdateTracks()
310 {
311     /* Has Audio, has Video Tracks ? */
312     vlc_value_t val;
313     var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
314     b_has_audio = val.i_int > 0;
315     var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
316     b_has_video = val.i_int > 0;
317     var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
318     b_has_subs = val.i_int > 0;
319
320     /* Update ZVBI status */
321 #ifdef ZVBI_COMPILED
322     /* Update teletext status*/
323     if( b_has_subs )
324         emit teletextEnabled( false );/* FIXME */
325 #endif
326 }
327
328 void InputManager::UpdateArt()
329 {
330     /* Update Art meta */
331     QString url;
332     char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
333     url.sprintf("%s", psz_art );
334     free( psz_art );
335     if( artUrl != url )
336     {
337         artUrl = url.replace( "file://",QString("" ) );
338         /* Taglib seems to define a attachment://, It won't work yet */
339         artUrl = url.replace( "attachment://",QString("" ) );
340         emit artChanged( artUrl );
341         msg_Dbg( p_intf, "Art:  %s", qtu( artUrl ) );
342     }
343 }
344
345 /* User update of the slider */
346 void InputManager::sliderUpdate( float new_pos )
347 {
348     if( hasInput() )
349         var_SetFloat( p_input, "position", new_pos );
350 }
351
352 /* User togglePlayPause */
353 void InputManager::togglePlayPause()
354 {
355     vlc_value_t state;
356     var_Get( p_input, "state", &state );
357     state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
358     var_Set( p_input, "state", state );
359     emit statusChanged( state.i_int );
360 }
361
362 void InputManager::sectionPrev()
363 {
364     if( hasInput() )
365     {
366         int i_type = var_Type( p_input, "next-chapter" );
367         vlc_value_t val; val.b_bool = true;
368         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
369                             "prev-chapter":"prev-title", val );
370     }
371 }
372
373 void InputManager::sectionNext()
374 {
375     if( hasInput() )
376     {
377         int i_type = var_Type( p_input, "next-chapter" );
378         vlc_value_t val; val.b_bool = true;
379         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
380                             "next-chapter":"next-title", val );
381     }
382 }
383
384 void InputManager::sectionMenu()
385 {
386     if( hasInput() )
387     {
388         vlc_value_t val; val.i_int = 2;
389         var_Set( p_input, "title  0", val );
390     }
391 }
392
393 #ifdef ZVBI_COMPILED
394 void InputManager::telexGotoPage( int page )
395 {
396     if( hasInput() )
397     {
398         vlc_object_t *p_vbi;
399         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
400                     "zvbi", FIND_ANYWHERE );
401         if( p_vbi )
402         {
403             var_SetInteger( p_vbi, "vbi-page", page );
404             vlc_object_release( p_vbi );
405         }
406     }
407 }
408
409 void InputManager::telexToggle( bool b_enabled )
410 {
411     int i_page = b_enabled ? 100 : 0 ;
412
413     telexGotoPage( i_page );
414 }
415
416 void InputManager::telexSetTransparency( bool b_transp )
417 {
418     if( hasInput() )
419     {
420         vlc_object_t *p_vbi;
421         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
422                     "zvbi", FIND_ANYWHERE );
423         if( p_vbi )
424         {
425             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
426             vlc_object_release( p_vbi );
427         }
428     }
429 }
430 #endif
431
432 void InputManager::slower()
433 {
434     if( hasInput() )
435         var_SetVoid( p_input, "rate-slower" );
436 }
437
438 void InputManager::faster()
439 {
440     if( hasInput() )
441         var_SetVoid( p_input, "rate-faster" );
442 }
443
444 void InputManager::normalRate()
445 {
446     if( hasInput() )
447         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
448 }
449
450 void InputManager::setRate( int new_rate )
451 {
452     if( hasInput() )
453         var_SetInteger( p_input, "rate", new_rate );
454 }
455
456 /**********************************************************************
457  * MainInputManager implementation. Wrap an input manager and
458  * take care of updating the main playlist input.
459  * Used in the main playlist Dialog
460  **********************************************************************/
461 MainInputManager * MainInputManager::instance = NULL;
462
463 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
464                  : QObject(NULL), p_intf( _p_intf )
465 {
466     p_input = NULL;
467     im = new InputManager( this, p_intf );
468
469 //    var_AddCallback( THEPL, "item-change", PLItemChanged, this );
470     var_AddCallback( THEPL, "item-change", ItemChanged, im );
471     var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
472     var_AddCallback( THEPL, "activity", PLItemChanged, this );
473
474     var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
475
476     // No necessary, I think TODO REMOVE ME at the end
477     //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
478
479     /* Warn our embedded IM about input changes */
480     CONNECT( this, inputChanged( input_thread_t * ),
481              im, setInput( input_thread_t * ) );
482 }
483
484 MainInputManager::~MainInputManager()
485 {
486     if( p_input )
487     {
488        var_DelCallback( p_input, "state", PLItemChanged, this );
489        vlc_object_release( p_input );
490        emit inputChanged( NULL );
491     }
492
493     var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
494
495     var_DelCallback( THEPL, "activity", PLItemChanged, this );
496     var_DelCallback( THEPL, "item-change", ItemChanged, im );
497 //    var_DelCallback( THEPL, "item-change", PLItemChanged, this );
498
499     var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
500 }
501
502 void MainInputManager::customEvent( QEvent *event )
503 {
504     int type = event->type();
505     if ( type != ItemChanged_Type && type != VolumeChanged_Type )
506         return;
507
508     // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
509     if( type == VolumeChanged_Type )
510     {
511         emit volumeChanged();
512         return;
513     }
514
515     /* Should be PLItemChanged Event */
516     if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
517     {
518         vlc_mutex_lock( &p_intf->change_lock );
519         if( p_input && ( p_input->b_dead || p_input->b_die ) )
520         {
521             var_DelCallback( p_input, "state", PLItemChanged, this );
522             vlc_object_release( p_input );
523             emit inputChanged( NULL );
524             p_input = NULL;
525             vlc_mutex_unlock( &p_intf->change_lock );
526             return;
527         }
528
529         if( !p_input )
530         {
531             QPL_LOCK;
532             p_input = THEPL->p_input;
533             if( p_input && !( p_input->b_die || p_input->b_dead) )
534             {
535                 vlc_object_yield( p_input );
536                 var_AddCallback( p_input, "state", PLItemChanged, this );
537                 emit inputChanged( p_input );
538             }
539             else
540                 p_input = NULL;
541             QPL_UNLOCK;
542         }
543         vlc_mutex_unlock( &p_intf->change_lock );
544     }
545     else
546     {
547         /* we are working as a dialogs provider */
548         playlist_t *p_playlist = pl_Yield( p_intf );
549         p_input = p_playlist->p_input;
550         emit inputChanged( p_input );
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