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