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