]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/input_manager.cpp
* modules/gui/wxwidgets: massive cleanup and simplification of the main interface...
[vlc] / modules / gui / wxwidgets / input_manager.cpp
1 /*****************************************************************************
2  * slider_manager.cpp : Manage an input slider
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include "input_manager.hpp"
26 #include "interface.hpp"
27 #include "video.hpp"
28
29 #include <vlc_meta.h>
30
31 /* include the toolbar graphics */
32 #include "bitmaps/prev.xpm"
33 #include "bitmaps/next.xpm"
34 #include "bitmaps/playlist.xpm"
35
36 /* IDs for the controls */
37 enum
38 {
39     SliderScroll_Event = wxID_HIGHEST,
40
41     DiscMenu_Event,
42     DiscPrev_Event,
43     DiscNext_Event
44 };
45
46 BEGIN_EVENT_TABLE(InputManager, wxPanel)
47     /* Slider events */
48     EVT_COMMAND_SCROLL(SliderScroll_Event, InputManager::OnSliderUpdate)
49
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)
54
55 END_EVENT_TABLE()
56
57 #define STATUS_STOP 0
58 #define STATUS_PLAYING 1
59 #define STATUS_PAUSE 2
60
61 /*****************************************************************************
62  * Constructor.
63  *****************************************************************************/
64 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
65                             wxWindow *p_parent )
66   : wxPanel( p_parent )
67 {
68     p_intf = _p_intf;
69     p_main_intf = _p_main_intf;
70     p_input = NULL;
71     i_old_playing_status = STATUS_STOP;
72     i_old_rate = INPUT_RATE_DEFAULT;
73     b_slider_free = VLC_TRUE;
74
75     /* Create slider */
76     slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
77
78     /* Create disc buttons */
79     disc_frame = new wxPanel( this );
80
81     disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
82                                            wxBitmap( playlist_xpm ) );
83     disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
84                                            wxBitmap( prev_xpm ) );
85     disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
86                                            wxBitmap( next_xpm ) );
87
88     disc_sizer = new wxBoxSizer( wxHORIZONTAL );
89     disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
90     disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
91     disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
92     disc_frame->SetSizer( disc_sizer );
93     disc_sizer->Layout();
94
95     /* Add everything to the panel */
96     sizer = new wxBoxSizer( wxHORIZONTAL );
97     SetSizer( sizer );
98     sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
99     sizer->Add( disc_frame, 0, wxALL, 2 );
100
101     /* Hide by default */
102     sizer->Hide( disc_frame );
103     sizer->Hide( slider );
104
105     sizer->Layout();
106     Fit();
107 }
108
109 InputManager::~InputManager()
110 {
111     vlc_mutex_lock( &p_intf->change_lock );
112     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
113     p_intf->p_sys->p_input = NULL;
114     vlc_mutex_unlock( &p_intf->change_lock );
115 }
116
117 /*****************************************************************************
118  * Public methods.
119  *****************************************************************************/
120 vlc_bool_t InputManager::IsPlaying()
121 {
122     return (p_input && !p_input->b_die);
123 }
124
125 /*****************************************************************************
126  * Private methods.
127  *****************************************************************************/
128 void InputManager::UpdateInput()
129 {
130     playlist_t *p_playlist =
131         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
132                                        FIND_ANYWHERE );
133     if( p_playlist != NULL )
134     {
135         LockPlaylist( p_intf->p_sys, p_playlist );
136         p_input = p_intf->p_sys->p_input = p_playlist->p_input;
137         if( p_intf->p_sys->p_input )
138              vlc_object_yield( p_intf->p_sys->p_input );
139         UnlockPlaylist( p_intf->p_sys, p_playlist );
140         vlc_object_release( p_playlist );
141     }
142 }
143
144 void InputManager::UpdateNowPlaying()
145 {
146     char *psz_now_playing = vlc_input_item_GetInfo( p_input->input.p_item,
147                 _("Meta-information"), _(VLC_META_NOW_PLAYING) );
148     if( psz_now_playing && *psz_now_playing )
149     {
150         p_main_intf->statusbar->SetStatusText(
151                     wxString(wxU(psz_now_playing)) + wxT( " - " ) +
152                     wxU(p_input->input.p_item->psz_name), 2 );
153     }
154     else
155     {
156         p_main_intf->statusbar->SetStatusText(
157                    wxU(p_input->input.p_item->psz_name), 2 );
158     }
159     free( psz_now_playing );
160 }
161
162 void InputManager::UpdateButtons( vlc_bool_t b_play )
163 {
164     if( !b_play )
165     {
166         if( i_old_playing_status == STATUS_STOP ) return;
167
168         i_old_playing_status = STATUS_STOP;
169         p_main_intf->TogglePlayButton( PAUSE_S );
170         p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
171         p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
172
173 #ifdef wxHAS_TASK_BAR_ICON
174         if( p_main_intf->p_systray )
175         {
176             p_main_intf->p_systray->UpdateTooltip(
177                 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
178         }
179 #endif
180
181         return;
182     }
183
184     /* Manage Playing status */
185     vlc_value_t val;
186     var_Get( p_input, "state", &val );
187     val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
188     if( i_old_playing_status != val.i_int )
189     {
190         i_old_playing_status = val.i_int;
191         p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
192                                        PAUSE_S : PLAYING_S );
193
194 #ifdef wxHAS_TASK_BAR_ICON
195         if( p_main_intf->p_systray )
196         {
197             p_main_intf->p_systray->UpdateTooltip(
198                 wxU(p_input->input.p_item->psz_name) + wxString(wxT(" - ")) +
199                 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
200         }
201 #endif
202     }
203 }
204
205 void InputManager::UpdateDiscButtons()
206 {
207     vlc_value_t val;
208     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
209     if( val.i_int > 0 && !disc_frame->IsShown() )
210     {
211         vlc_value_t val;
212
213         #define HELP_MENU N_("Menu")
214         #define HELP_PCH N_("Previous chapter")
215         #define HELP_NCH N_("Next chapter")
216         #define HELP_PTR N_("Previous track")
217         #define HELP_NTR N_("Next track")
218
219         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
220
221         if( val.i_int > 0 )
222         {
223             disc_menu_button->Show();
224             disc_sizer->Show( disc_menu_button );
225             disc_sizer->Layout();
226             disc_sizer->Fit( disc_frame );
227             disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
228             disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
229             disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
230         }
231         else
232         {
233             disc_menu_button->Hide();
234             disc_sizer->Hide( disc_menu_button );
235             disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
236             disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
237         }
238
239         ShowDiscFrame();
240     }
241     else if( val.i_int == 0 && disc_frame->IsShown() )
242     {
243         HideDiscFrame();
244     }
245 }
246
247 void InputManager::HideSlider()
248 {
249     ShowSlider( false );
250 }
251
252 void InputManager::HideDiscFrame()
253 {
254     ShowDiscFrame( false );
255 }
256
257 void InputManager::UpdateTime()
258 {
259     char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
260     mtime_t i_seconds;
261
262     i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
263     secstotimestr( psz_total, i_seconds );
264
265     i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
266     secstotimestr( psz_time, i_seconds );
267
268     p_main_intf->statusbar->SetStatusText(
269         wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
270 }
271
272 void InputManager::Update()
273 {
274     /* Update the input */
275     if( p_input == NULL )
276     {
277         UpdateInput();
278
279         if( p_input )
280         {
281             slider->SetValue( 0 );
282         }
283         else
284         {
285             if( disc_frame->IsShown() ) HideDiscFrame();
286             if( slider->IsShown() ) HideSlider();
287         }
288     }
289     else if( p_input->b_dead )
290     {
291         UpdateButtons( VLC_FALSE );
292         vlc_object_release( p_input );
293         p_input = NULL;
294     }
295
296     if( p_input && !p_input->b_die )
297     {
298         vlc_value_t pos, len;
299
300         UpdateTime();
301         UpdateButtons( VLC_TRUE );
302         UpdateNowPlaying();
303         UpdateDiscButtons();
304
305         /* Really manage the slider */
306         var_Get( p_input, "position", &pos );
307         var_Get( p_input, "length", &len );
308
309         if( len.i_time > 0 && pos.f_float >= 0 &&
310             !slider->IsShown() ) ShowSlider();
311         else if( len.i_time < 0 && pos.f_float <= 0 &&
312                  slider->IsShown() ) HideSlider();
313
314         /* Update the slider if the user isn't dragging it. */
315         if( slider->IsShown() && b_slider_free )
316         {
317             i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
318             slider->SetValue( i_slider_pos );
319         }
320
321         /* Manage Speed status */
322         vlc_value_t val;
323         var_Get( p_input, "rate", &val );
324         if( i_old_rate != val.i_int )
325         {
326             p_main_intf->statusbar->SetStatusText(
327                 wxString::Format(wxT("x%.2f"),
328                 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
329             i_old_rate = val.i_int;
330         }
331     }
332 }
333
334 /*****************************************************************************
335  * Event Handlers.
336  *****************************************************************************/
337 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
338 {
339     input_thread_t *p_input =
340         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
341                                            FIND_ANYWHERE );
342     if( p_input )
343     {
344         vlc_value_t val; val.i_int = 2;
345
346         var_Set( p_input, "title  0", val);
347         vlc_object_release( p_input );
348     }
349 }
350
351 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
352 {
353     input_thread_t *p_input =
354         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
355                                            FIND_ANYWHERE );
356     if( p_input )
357     {
358         int i_type = var_Type( p_input, "prev-chapter" );
359         vlc_value_t val; val.b_bool = VLC_TRUE;
360
361         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
362                  "prev-chapter" : "prev-title", val );
363
364         vlc_object_release( p_input );
365     }
366 }
367
368 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
369 {
370     input_thread_t *p_input =
371         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
372                                            FIND_ANYWHERE );
373     if( p_input )
374     {
375         int i_type = var_Type( p_input, "next-chapter" );
376         vlc_value_t val; val.b_bool = VLC_TRUE;
377
378         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
379                  "next-chapter" : "next-title", val );
380
381         vlc_object_release( p_input );
382     }
383 }
384
385 void InputManager::OnSliderUpdate( wxScrollEvent& event )
386 {
387     vlc_mutex_lock( &p_intf->change_lock );
388
389 #ifdef WIN32
390     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
391         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
392     {
393 #endif
394         if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
395         {
396             vlc_value_t pos;
397             pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
398             var_Set( p_intf->p_sys->p_input, "position", pos );
399         }
400
401 #ifdef WIN32
402         b_slider_free = VLC_TRUE;
403     }
404     else
405     {
406         b_slider_free = VLC_FALSE;
407         if( p_intf->p_sys->p_input ) UpdateTime();
408     }
409 #endif
410
411 #undef WIN32
412     vlc_mutex_unlock( &p_intf->change_lock );
413 }
414
415 void InputManager::ShowSlider( bool show )
416 {
417     if( !!show == !!slider->IsShown() ) return;
418
419     if( p_intf->p_sys->b_video_autosize )
420         UpdateVideoWindow( p_intf, p_main_intf->video_window );
421
422     sizer->Show( slider, show );
423     sizer->Layout();
424
425     wxCommandEvent intf_event( wxEVT_INTF, 0 );
426     p_main_intf->AddPendingEvent( intf_event );
427 }
428
429 void InputManager::ShowDiscFrame( bool show )
430 {
431     if( !!show == !!disc_frame->IsShown() ) return;
432
433     if( p_intf->p_sys->b_video_autosize )
434         UpdateVideoWindow( p_intf, p_main_intf->video_window );
435
436     sizer->Show( disc_frame, show );
437     sizer->Layout();
438
439     wxCommandEvent intf_event( wxEVT_INTF, 0 );
440     p_main_intf->AddPendingEvent( intf_event );
441 }