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