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