1 /*****************************************************************************
2 * slider_manager.cpp : Manage an input slider
3 *****************************************************************************
4 * Copyright (C) 2000-2005 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "input_manager.hpp"
26 #include "interface.hpp"
31 /* include the toolbar graphics */
32 #include "bitmaps/prev.xpm"
33 #include "bitmaps/next.xpm"
34 #include "bitmaps/playlist.xpm"
36 /* IDs for the controls */
39 SliderScroll_Event = wxID_HIGHEST,
46 BEGIN_EVENT_TABLE(InputManager, wxPanel)
48 EVT_COMMAND_SCROLL(SliderScroll_Event, InputManager::OnSliderUpdate)
50 /* Disc Buttons events */
51 EVT_BUTTON(DiscMenu_Event, InputManager::OnDiscMenu)
52 EVT_BUTTON(DiscPrev_Event, InputManager::OnDiscPrev)
53 EVT_BUTTON(DiscNext_Event, InputManager::OnDiscNext)
58 #define STATUS_PLAYING 1
59 #define STATUS_PAUSE 2
61 /*****************************************************************************
63 *****************************************************************************/
64 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
69 p_main_intf = _p_main_intf;
71 i_old_playing_status = STATUS_STOP;
72 i_old_rate = INPUT_RATE_DEFAULT;
73 b_slider_free = VLC_TRUE;
74 i_input_hide_delay = 0;
77 slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
79 /* Create disc buttons */
80 disc_frame = new wxPanel( this );
82 disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
83 wxBitmap( playlist_xpm ) );
84 disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
85 wxBitmap( prev_xpm ) );
86 disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
87 wxBitmap( next_xpm ) );
89 disc_sizer = new wxBoxSizer( wxHORIZONTAL );
90 disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
91 disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
92 disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
93 disc_frame->SetSizer( disc_sizer );
96 /* Add everything to the panel */
97 sizer = new wxBoxSizer( wxHORIZONTAL );
99 sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
100 sizer->Add( disc_frame, 0, wxALL, 2 );
102 /* Hide by default */
103 sizer->Hide( disc_frame );
104 sizer->Hide( slider );
110 InputManager::~InputManager()
112 vlc_mutex_lock( &p_intf->change_lock );
113 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
114 p_intf->p_sys->p_input = NULL;
115 vlc_mutex_unlock( &p_intf->change_lock );
118 /*****************************************************************************
120 *****************************************************************************/
121 vlc_bool_t InputManager::IsPlaying()
123 return (p_input && !p_input->b_die);
126 /*****************************************************************************
128 *****************************************************************************/
129 void InputManager::UpdateInput()
131 playlist_t *p_playlist =
132 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
134 if( p_playlist != NULL )
136 LockPlaylist( p_intf->p_sys, p_playlist );
137 p_input = p_intf->p_sys->p_input = p_playlist->p_input;
138 if( p_intf->p_sys->p_input )
139 vlc_object_yield( p_intf->p_sys->p_input );
140 UnlockPlaylist( p_intf->p_sys, p_playlist );
141 vlc_object_release( p_playlist );
145 void InputManager::UpdateNowPlaying()
147 char *psz_now_playing = p_input->input.p_item->p_meta->psz_nowplaying ?
148 strdup( p_input->input.p_item->p_meta->psz_nowplaying ):
150 if( psz_now_playing && *psz_now_playing )
152 p_main_intf->statusbar->SetStatusText(
153 wxString(wxU(psz_now_playing)) + wxT( " - " ) +
154 wxU(p_input->input.p_item->psz_name), 2 );
158 p_main_intf->statusbar->SetStatusText(
159 wxU(p_input->input.p_item->psz_name), 2 );
161 free( psz_now_playing );
164 void InputManager::UpdateButtons( vlc_bool_t b_play )
168 if( i_old_playing_status == STATUS_STOP ) return;
170 i_old_playing_status = STATUS_STOP;
171 p_main_intf->TogglePlayButton( PAUSE_S );
172 p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
173 p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
175 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
177 #ifdef wxHAS_TASK_BAR_ICON
178 if( p_main_intf->p_systray )
180 p_main_intf->p_systray->UpdateTooltip(
181 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
189 /* Manage Playing status */
191 var_Get( p_input, "state", &val );
192 val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
193 if( i_old_playing_status != val.i_int )
195 i_old_playing_status = val.i_int;
196 p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
197 PAUSE_S : PLAYING_S );
199 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
201 #ifdef wxHAS_TASK_BAR_ICON
202 if( p_main_intf->p_systray )
204 p_main_intf->p_systray->UpdateTooltip(
205 wxU(p_input->input.p_item->psz_name) + wxString(wxT(" - ")) +
206 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
213 void InputManager::UpdateDiscButtons()
216 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
217 if( val.i_int > 0 && !disc_frame->IsShown() )
221 #define HELP_MENU N_("Menu")
222 #define HELP_PCH N_("Previous chapter")
223 #define HELP_NCH N_("Next chapter")
224 #define HELP_PTR N_("Previous track")
225 #define HELP_NTR N_("Next track")
227 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
231 disc_menu_button->Show();
232 disc_sizer->Show( disc_menu_button );
233 disc_sizer->Layout();
234 disc_sizer->Fit( disc_frame );
235 disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
236 disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
237 disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
241 disc_menu_button->Hide();
242 disc_sizer->Hide( disc_menu_button );
243 disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
244 disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
249 else if( val.i_int == 0 && disc_frame->IsShown() )
255 void InputManager::HideSlider()
260 void InputManager::HideDiscFrame()
262 ShowDiscFrame( false );
265 void InputManager::UpdateTime()
267 char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
270 i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
271 secstotimestr( psz_total, i_seconds );
273 i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
274 secstotimestr( psz_time, i_seconds );
276 p_main_intf->statusbar->SetStatusText(
277 wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
280 void InputManager::Update()
282 /* Update the input */
283 if( p_input == NULL )
289 slider->SetValue( 0 );
291 else if( !i_input_hide_delay )
293 i_input_hide_delay = mdate() + 200000;
295 else if( i_input_hide_delay < mdate() )
297 if( disc_frame->IsShown() ) HideDiscFrame();
298 if( slider->IsShown() ) HideSlider();
299 i_input_hide_delay = 0;
302 else if( p_input->b_dead )
304 UpdateButtons( VLC_FALSE );
305 vlc_object_release( p_input );
310 i_input_hide_delay = 0;
313 if( p_input && !p_input->b_die )
315 vlc_value_t pos, len;
318 UpdateButtons( VLC_TRUE );
322 /* Really manage the slider */
323 var_Get( p_input, "position", &pos );
324 var_Get( p_input, "length", &len );
326 if( pos.f_float > 0 && !slider->IsShown() ) ShowSlider();
327 else if( pos.f_float <= 0 && slider->IsShown() ) HideSlider();
329 /* Update the slider if the user isn't dragging it. */
330 if( slider->IsShown() && b_slider_free )
332 i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
333 slider->SetValue( i_slider_pos );
336 /* Manage Speed status */
338 var_Get( p_input, "rate", &val );
339 if( i_old_rate != val.i_int )
341 p_main_intf->statusbar->SetStatusText(
342 wxString::Format(wxT("x%.2f"),
343 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
344 i_old_rate = val.i_int;
349 /*****************************************************************************
351 *****************************************************************************/
352 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
354 input_thread_t *p_input =
355 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
359 vlc_value_t val; val.i_int = 2;
361 var_Set( p_input, "title 0", val);
362 vlc_object_release( p_input );
366 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
368 input_thread_t *p_input =
369 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
373 int i_type = var_Type( p_input, "prev-chapter" );
374 vlc_value_t val; val.b_bool = VLC_TRUE;
376 var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
377 "prev-chapter" : "prev-title", val );
379 vlc_object_release( p_input );
383 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
385 input_thread_t *p_input =
386 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
390 int i_type = var_Type( p_input, "next-chapter" );
391 vlc_value_t val; val.b_bool = VLC_TRUE;
393 var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
394 "next-chapter" : "next-title", val );
396 vlc_object_release( p_input );
400 void InputManager::OnSliderUpdate( wxScrollEvent& event )
402 vlc_mutex_lock( &p_intf->change_lock );
405 if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
406 || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
409 if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
412 pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
413 var_Set( p_intf->p_sys->p_input, "position", pos );
417 b_slider_free = VLC_TRUE;
421 b_slider_free = VLC_FALSE;
422 if( p_intf->p_sys->p_input ) UpdateTime();
427 vlc_mutex_unlock( &p_intf->change_lock );
430 void InputManager::ShowSlider( bool show )
432 if( !!show == !!slider->IsShown() ) return;
434 UpdateVideoWindow( p_intf, p_main_intf->video_window );
436 sizer->Show( slider, show );
439 wxCommandEvent intf_event( wxEVT_INTF, 0 );
440 p_main_intf->AddPendingEvent( intf_event );
443 void InputManager::ShowDiscFrame( bool show )
445 if( !!show == !!disc_frame->IsShown() ) return;
447 UpdateVideoWindow( p_intf, p_main_intf->video_window );
449 sizer->Show( disc_frame, show );
452 wxCommandEvent intf_event( wxEVT_INTF, 0 );
453 p_main_intf->AddPendingEvent( intf_event );