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