]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/input_manager.cpp
Uniformize source files encoding
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     i_input_hide_delay = 0;
75
76     /* Create slider */
77     slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
78
79     /* Create disc buttons */
80     disc_frame = new wxPanel( this );
81
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 ) );
88
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 );
94     disc_sizer->Layout();
95
96     /* Add everything to the panel */
97     sizer = new wxBoxSizer( wxHORIZONTAL );
98     SetSizer( sizer );
99     sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
100     sizer->Add( disc_frame, 0, wxALL, 2 );
101
102     /* Hide by default */
103     sizer->Hide( disc_frame );
104     sizer->Hide( slider );
105
106     sizer->Layout();
107     Fit();
108 }
109
110 InputManager::~InputManager()
111 {
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 );
116 }
117
118 /*****************************************************************************
119  * Public methods.
120  *****************************************************************************/
121 vlc_bool_t InputManager::IsPlaying()
122 {
123     return (p_input && !p_input->b_die);
124 }
125
126 /*****************************************************************************
127  * Private methods.
128  *****************************************************************************/
129 void InputManager::UpdateInput()
130 {
131     playlist_t *p_playlist =
132         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
133                                        FIND_ANYWHERE );
134     if( p_playlist != NULL )
135     {
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 );
142     }
143 }
144
145 void InputManager::UpdateNowPlaying()
146 {
147     char *psz_now_playing = vlc_input_item_GetInfo( p_input->input.p_item,
148                 _("Meta-information"), _(VLC_META_NOW_PLAYING) );
149     if( psz_now_playing && *psz_now_playing )
150     {
151         p_main_intf->statusbar->SetStatusText(
152                     wxString(wxU(psz_now_playing)) + wxT( " - " ) +
153                     wxU(p_input->input.p_item->psz_name), 2 );
154     }
155     else
156     {
157         p_main_intf->statusbar->SetStatusText(
158                    wxU(p_input->input.p_item->psz_name), 2 );
159     }
160     free( psz_now_playing );
161 }
162
163 void InputManager::UpdateButtons( vlc_bool_t b_play )
164 {
165     if( !b_play )
166     {
167         if( i_old_playing_status == STATUS_STOP ) return;
168
169         i_old_playing_status = STATUS_STOP;
170         p_main_intf->TogglePlayButton( PAUSE_S );
171         p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
172         p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
173
174 #ifdef wxHAS_TASK_BAR_ICON
175         if( p_main_intf->p_systray )
176         {
177             p_main_intf->p_systray->UpdateTooltip(
178                 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
179         }
180 #endif
181
182         return;
183     }
184
185     /* Manage Playing status */
186     vlc_value_t val;
187     var_Get( p_input, "state", &val );
188     val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
189     if( i_old_playing_status != val.i_int )
190     {
191         i_old_playing_status = val.i_int;
192         p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
193                                        PAUSE_S : PLAYING_S );
194
195 #ifdef wxHAS_TASK_BAR_ICON
196         if( p_main_intf->p_systray )
197         {
198             p_main_intf->p_systray->UpdateTooltip(
199                 wxU(p_input->input.p_item->psz_name) + wxString(wxT(" - ")) +
200                 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
201         }
202 #endif
203     }
204 }
205
206 void InputManager::UpdateDiscButtons()
207 {
208     vlc_value_t val;
209     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
210     if( val.i_int > 0 && !disc_frame->IsShown() )
211     {
212         vlc_value_t val;
213
214         #define HELP_MENU N_("Menu")
215         #define HELP_PCH N_("Previous chapter")
216         #define HELP_NCH N_("Next chapter")
217         #define HELP_PTR N_("Previous track")
218         #define HELP_NTR N_("Next track")
219
220         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
221
222         if( val.i_int > 0 )
223         {
224             disc_menu_button->Show();
225             disc_sizer->Show( disc_menu_button );
226             disc_sizer->Layout();
227             disc_sizer->Fit( disc_frame );
228             disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
229             disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
230             disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
231         }
232         else
233         {
234             disc_menu_button->Hide();
235             disc_sizer->Hide( disc_menu_button );
236             disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
237             disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
238         }
239
240         ShowDiscFrame();
241     }
242     else if( val.i_int == 0 && disc_frame->IsShown() )
243     {
244         HideDiscFrame();
245     }
246 }
247
248 void InputManager::HideSlider()
249 {
250     ShowSlider( false );
251 }
252
253 void InputManager::HideDiscFrame()
254 {
255     ShowDiscFrame( false );
256 }
257
258 void InputManager::UpdateTime()
259 {
260     char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
261     mtime_t i_seconds;
262
263     i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
264     secstotimestr( psz_total, i_seconds );
265
266     i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
267     secstotimestr( psz_time, i_seconds );
268
269     p_main_intf->statusbar->SetStatusText(
270         wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
271 }
272
273 void InputManager::Update()
274 {
275     /* Update the input */
276     if( p_input == NULL )
277     {
278         UpdateInput();
279
280         if( p_input )
281         {
282             slider->SetValue( 0 );
283         }
284         else if( !i_input_hide_delay )
285         {
286             i_input_hide_delay = mdate() + 200000;
287         }
288         else if( i_input_hide_delay < mdate() )
289         {
290             if( disc_frame->IsShown() ) HideDiscFrame();
291             if( slider->IsShown() ) HideSlider();
292             i_input_hide_delay = 0;
293         }
294     }
295     else if( p_input->b_dead )
296     {
297         UpdateButtons( VLC_FALSE );
298         vlc_object_release( p_input );
299         p_input = NULL;
300     }
301     else
302     {
303         i_input_hide_delay = 0;
304     }
305
306     if( p_input && !p_input->b_die )
307     {
308         vlc_value_t pos, len;
309
310         UpdateTime();
311         UpdateButtons( VLC_TRUE );
312         UpdateNowPlaying();
313         UpdateDiscButtons();
314
315         /* Really manage the slider */
316         var_Get( p_input, "position", &pos );
317         var_Get( p_input, "length", &len );
318
319         if( len.i_time > 0 && pos.f_float >= 0 &&
320             !slider->IsShown() ) ShowSlider();
321         else if( len.i_time < 0 && pos.f_float <= 0 &&
322                  slider->IsShown() ) HideSlider();
323
324         /* Update the slider if the user isn't dragging it. */
325         if( slider->IsShown() && b_slider_free )
326         {
327             i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
328             slider->SetValue( i_slider_pos );
329         }
330
331         /* Manage Speed status */
332         vlc_value_t val;
333         var_Get( p_input, "rate", &val );
334         if( i_old_rate != val.i_int )
335         {
336             p_main_intf->statusbar->SetStatusText(
337                 wxString::Format(wxT("x%.2f"),
338                 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
339             i_old_rate = val.i_int;
340         }
341     }
342 }
343
344 /*****************************************************************************
345  * Event Handlers.
346  *****************************************************************************/
347 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
348 {
349     input_thread_t *p_input =
350         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
351                                            FIND_ANYWHERE );
352     if( p_input )
353     {
354         vlc_value_t val; val.i_int = 2;
355
356         var_Set( p_input, "title  0", val);
357         vlc_object_release( p_input );
358     }
359 }
360
361 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
362 {
363     input_thread_t *p_input =
364         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
365                                            FIND_ANYWHERE );
366     if( p_input )
367     {
368         int i_type = var_Type( p_input, "prev-chapter" );
369         vlc_value_t val; val.b_bool = VLC_TRUE;
370
371         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
372                  "prev-chapter" : "prev-title", val );
373
374         vlc_object_release( p_input );
375     }
376 }
377
378 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
379 {
380     input_thread_t *p_input =
381         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
382                                            FIND_ANYWHERE );
383     if( p_input )
384     {
385         int i_type = var_Type( p_input, "next-chapter" );
386         vlc_value_t val; val.b_bool = VLC_TRUE;
387
388         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
389                  "next-chapter" : "next-title", val );
390
391         vlc_object_release( p_input );
392     }
393 }
394
395 void InputManager::OnSliderUpdate( wxScrollEvent& event )
396 {
397     vlc_mutex_lock( &p_intf->change_lock );
398
399 #ifdef WIN32
400     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
401         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
402     {
403 #endif
404         if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
405         {
406             vlc_value_t pos;
407             pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
408             var_Set( p_intf->p_sys->p_input, "position", pos );
409         }
410
411 #ifdef WIN32
412         b_slider_free = VLC_TRUE;
413     }
414     else
415     {
416         b_slider_free = VLC_FALSE;
417         if( p_intf->p_sys->p_input ) UpdateTime();
418     }
419 #endif
420
421 #undef WIN32
422     vlc_mutex_unlock( &p_intf->change_lock );
423 }
424
425 void InputManager::ShowSlider( bool show )
426 {
427     if( !!show == !!slider->IsShown() ) return;
428
429     UpdateVideoWindow( p_intf, p_main_intf->video_window );
430
431     sizer->Show( slider, show );
432     sizer->Layout();
433
434     wxCommandEvent intf_event( wxEVT_INTF, 0 );
435     p_main_intf->AddPendingEvent( intf_event );
436 }
437
438 void InputManager::ShowDiscFrame( bool show )
439 {
440     if( !!show == !!disc_frame->IsShown() ) return;
441
442     UpdateVideoWindow( p_intf, p_main_intf->video_window );
443
444     sizer->Show( disc_frame, show );
445     sizer->Layout();
446
447     wxCommandEvent intf_event( wxEVT_INTF, 0 );
448     p_main_intf->AddPendingEvent( intf_event );
449 }