]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/input_manager.cpp
9419e2ef07e74d20b21ed4076b515ee8741ce60d
[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 #ifdef WIN32
62
63 #include <commctrl.h>
64
65 /*
66 ** On win32, clicking on the slider channel causes the thumb to jump up or down a page size
67 ** like a scrollbar.  This is not particularily useful for a movie track, where we'd rather
68 ** see the thumb to jump where the mouse is.
69 ** Therefore, we replace the slider (TRACKBAR control) window proc with our, which intercept
70 ** the mouse down event, and move the thumb accordingly
71 */
72 static LRESULT CALLBACK MovieSliderWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
73 {
74     switch( uMsg )
75     {
76         case WM_LBUTTONDOWN:
77         {
78             POINT click = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
79             RECT tRect = {0, 0, 0, 0};
80             SendMessage(hWnd, TBM_GETTHUMBRECT, 0, (LPARAM)&tRect);
81
82             /* check whether click is not in thumb */
83             if( ! PtInRect(&tRect, click) )
84             {
85                 LONG min = SendMessage(hWnd, TBM_GETRANGEMIN, 0, 0);
86                 LONG max = SendMessage(hWnd, TBM_GETRANGEMAX, 0, 0);
87                 LONG thumb = tRect.right-tRect.left;
88                 LONG newpos;
89
90                 SendMessage(hWnd, TBM_GETCHANNELRECT, 0, (LPARAM)&tRect);
91
92                 /* following is only valid for horizontal a trackbar */
93                 newpos = ((click.x-tRect.left-(thumb/2))*(max-min)+((tRect.right-tRect.left-thumb)/2))
94                /(tRect.right-tRect.left-thumb);
95
96                 /* set new postion */
97                 SendMessage(hWnd, TBM_SETPOS, TRUE, min+newpos);
98                 /* notify parent of change */
99                 SendMessage(GetParent(hWnd), WM_HSCROLL, TB_ENDTRACK, (LPARAM)hWnd);
100
101                 return 0;
102             }
103         }
104
105         default:
106             return CallWindowProc((WNDPROC)GetWindowLongPtr(hWnd, GWLP_USERDATA),
107                         hWnd, uMsg, wParam, lParam);
108     }
109 }
110
111 #endif
112
113 /*****************************************************************************
114  * Constructor.
115  *****************************************************************************/
116 InputManager::InputManager( intf_thread_t *_p_intf, Interface *_p_main_intf,
117                             wxWindow *p_parent )
118   : wxPanel( p_parent )
119 {
120     p_intf = _p_intf;
121     p_main_intf = _p_main_intf;
122     p_input = NULL;
123     i_old_playing_status = STATUS_STOP;
124     i_old_rate = INPUT_RATE_DEFAULT;
125     b_slider_free = true;
126     i_input_hide_delay = 0;
127
128     /* Create slider */
129     slider = new wxSlider( this, SliderScroll_Event, 0, 0, SLIDER_MAX_POS );
130
131 #ifdef WIN32
132     /* modify behaviour of WIN32 underlying control
133       in order to implement proper movie slider */
134     {
135         HWND sliderHwnd = (HWND)slider->GetHWND();
136         /* put original WNDPROC into USERDATA, this may be incompatible with future version of
137            wxwidgets. */
138         SetWindowLongPtr(sliderHwnd, GWLP_USERDATA,
139             (LONG_PTR)GetWindowLongPtr(sliderHwnd, GWLP_WNDPROC));
140         /* put our own WNDPROC */
141         SetWindowLongPtr(sliderHwnd, GWLP_WNDPROC, (LONG_PTR)MovieSliderWindowProc);
142     }
143 #endif
144
145     /* Create disc buttons */
146     disc_frame = new wxPanel( this );
147
148     disc_menu_button = new wxBitmapButton( disc_frame, DiscMenu_Event,
149                                            wxBitmap( playlist_xpm ) );
150     disc_prev_button = new wxBitmapButton( disc_frame, DiscPrev_Event,
151                                            wxBitmap( prev_xpm ) );
152     disc_next_button = new wxBitmapButton( disc_frame, DiscNext_Event,
153                                            wxBitmap( next_xpm ) );
154
155     disc_sizer = new wxBoxSizer( wxHORIZONTAL );
156     disc_sizer->Add( disc_menu_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
157     disc_sizer->Add( disc_prev_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
158     disc_sizer->Add( disc_next_button, 1, wxEXPAND | wxLEFT | wxRIGHT, 1 );
159     disc_frame->SetSizer( disc_sizer );
160     disc_sizer->Layout();
161
162     /* Add everything to the panel */
163     sizer = new wxBoxSizer( wxHORIZONTAL );
164     SetSizer( sizer );
165     sizer->Add( slider, 1, wxEXPAND | wxALL, 5 );
166     sizer->Add( disc_frame, 0, wxALL, 2 );
167
168     /* Hide by default */
169     sizer->Hide( disc_frame );
170     sizer->Hide( slider );
171
172     sizer->Layout();
173     Fit();
174 }
175
176 InputManager::~InputManager()
177 {
178     vlc_mutex_lock( &p_intf->change_lock );
179     if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
180     p_intf->p_sys->p_input = NULL;
181     vlc_mutex_unlock( &p_intf->change_lock );
182 }
183
184 /*****************************************************************************
185  * Public methods.
186  *****************************************************************************/
187 bool InputManager::IsPlaying()
188 {
189     return (p_input && !p_input->b_die);
190 }
191
192 /*****************************************************************************
193  * Private methods.
194  *****************************************************************************/
195 void InputManager::UpdateInput()
196 {
197     playlist_t *p_playlist = pl_Yield( p_intf );
198     if( p_playlist != NULL )
199     {
200         LockPlaylist( p_intf->p_sys, p_playlist );
201         p_input = p_intf->p_sys->p_input = p_playlist->p_input;
202         if( p_intf->p_sys->p_input )
203              vlc_object_yield( p_intf->p_sys->p_input );
204         UnlockPlaylist( p_intf->p_sys, p_playlist );
205         pl_Release( p_playlist );
206     }
207 }
208
209 void InputManager::UpdateNowPlaying()
210 {
211     char *psz_now_playing = input_item_GetNowPlaying( input_GetItem(p_input) );
212     if( psz_now_playing && *psz_now_playing )
213     {
214         p_main_intf->statusbar->SetStatusText(
215                     wxString(wxU(psz_now_playing)) + wxT( " - " ) +
216                     wxU(input_GetItem(p_input)->psz_name), 2 );
217     }
218     else
219     {
220         p_main_intf->statusbar->SetStatusText(
221                    wxU(input_GetItem(p_input)->psz_name), 2 );
222     }
223     free( psz_now_playing );
224 }
225
226 void InputManager::UpdateButtons( bool b_play )
227 {
228     if( !b_play )
229     {
230         if( i_old_playing_status == STATUS_STOP ) return;
231
232         i_old_playing_status = STATUS_STOP;
233         p_main_intf->TogglePlayButton( PAUSE_S );
234         p_main_intf->statusbar->SetStatusText( wxT(""), 0 );
235         p_main_intf->statusbar->SetStatusText( wxT(""), 2 );
236
237 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
238 #ifndef __APPLE__
239 #ifdef wxHAS_TASK_BAR_ICON
240         if( p_main_intf->p_systray )
241         {
242             p_main_intf->p_systray->UpdateTooltip(
243                 wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
244         }
245 #endif
246 #endif
247
248         return;
249     }
250
251     /* Manage Playing status */
252     vlc_value_t val;
253     var_Get( p_input, "state", &val );
254     val.i_int = val.i_int == PAUSE_S ? STATUS_PAUSE : STATUS_PLAYING;
255     if( i_old_playing_status != val.i_int )
256     {
257         i_old_playing_status = val.i_int;
258         p_main_intf->TogglePlayButton( val.i_int == STATUS_PAUSE ?
259                                        PAUSE_S : PLAYING_S );
260
261 /* wxCocoa pretends to support this, but at least 2.6.x doesn't */
262 #ifndef __APPLE__
263 #ifdef wxHAS_TASK_BAR_ICON
264         if( p_main_intf->p_systray )
265         {
266             p_main_intf->p_systray->UpdateTooltip(
267                 wxU(input_GetItem(p_input)->psz_name) + wxString(wxT(" - ")) +
268                 (val.i_int == PAUSE_S ? wxU(_("Paused")) : wxU(_("Playing"))));
269         }
270 #endif
271 #endif
272     }
273 }
274
275 void InputManager::UpdateDiscButtons()
276 {
277     vlc_value_t val;
278     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
279     if( val.i_int > 0 && !disc_frame->IsShown() )
280     {
281         vlc_value_t val;
282
283         #define HELP_MENU N_("Menu")
284         #define HELP_PCH N_("Previous chapter")
285         #define HELP_NCH N_("Next chapter")
286         #define HELP_PTR N_("Previous track")
287         #define HELP_NTR N_("Next track")
288
289         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
290
291         if( val.i_int > 0 )
292         {
293             disc_menu_button->Show();
294             disc_sizer->Show( disc_menu_button );
295             disc_sizer->Layout();
296             disc_sizer->Fit( disc_frame );
297             disc_menu_button->SetToolTip( wxU(_( HELP_MENU ) ) );
298             disc_prev_button->SetToolTip( wxU(_( HELP_PCH ) ) );
299             disc_next_button->SetToolTip( wxU(_( HELP_NCH ) ) );
300         }
301         else
302         {
303             disc_menu_button->Hide();
304             disc_sizer->Hide( disc_menu_button );
305             disc_prev_button->SetToolTip( wxU(_( HELP_PTR ) ) );
306             disc_next_button->SetToolTip( wxU(_( HELP_NTR ) ) );
307         }
308
309         ShowDiscFrame();
310     }
311     else if( val.i_int == 0 && disc_frame->IsShown() )
312     {
313         HideDiscFrame();
314     }
315 }
316
317 void InputManager::HideSlider()
318 {
319     ShowSlider( false );
320 }
321
322 void InputManager::HideDiscFrame()
323 {
324     ShowDiscFrame( false );
325 }
326
327 void InputManager::UpdateTime()
328 {
329     char psz_time[ MSTRTIME_MAX_SIZE ], psz_total[ MSTRTIME_MAX_SIZE ];
330     mtime_t i_seconds;
331
332     i_seconds = var_GetTime( p_intf->p_sys->p_input, "length" ) / 1000000;
333     secstotimestr( psz_total, i_seconds );
334
335     i_seconds = var_GetTime( p_intf->p_sys->p_input, "time" ) / 1000000;
336     secstotimestr( psz_time, i_seconds );
337
338     p_main_intf->statusbar->SetStatusText(
339         wxU(psz_time) + wxString(wxT(" / ")) +wxU(psz_total), 0 );
340 }
341
342 void InputManager::Update()
343 {
344     /* Update the input */
345     if( p_input == NULL )
346     {
347         UpdateInput();
348
349         if( p_input )
350         {
351             slider->SetValue( 0 );
352         }
353         else if( !i_input_hide_delay )
354         {
355             i_input_hide_delay = mdate() + 200000;
356         }
357         else if( i_input_hide_delay < mdate() )
358         {
359             if( disc_frame->IsShown() ) HideDiscFrame();
360             if( slider->IsShown() ) HideSlider();
361             i_input_hide_delay = 0;
362         }
363     }
364     else if( p_input->b_dead )
365     {
366         UpdateButtons( false );
367         vlc_object_release( p_input );
368         p_input = NULL;
369     }
370     else
371     {
372         i_input_hide_delay = 0;
373     }
374
375     if( p_input && !p_input->b_die )
376     {
377         vlc_value_t pos, len;
378
379         UpdateTime();
380         UpdateButtons( true );
381         UpdateNowPlaying();
382         UpdateDiscButtons();
383
384         /* Really manage the slider */
385         var_Get( p_input, "position", &pos );
386         var_Get( p_input, "length", &len );
387
388         if( pos.f_float > 0 && !slider->IsShown() ) ShowSlider();
389         else if(  pos.f_float <= 0 &&  slider->IsShown() ) HideSlider();
390
391         /* Update the slider if the user isn't dragging it. */
392         if( slider->IsShown() && b_slider_free )
393         {
394             i_slider_pos = (int)(SLIDER_MAX_POS * pos.f_float);
395             slider->SetValue( i_slider_pos );
396         }
397
398         /* Manage Speed status */
399         vlc_value_t val;
400         var_Get( p_input, "rate", &val );
401         if( i_old_rate != val.i_int )
402         {
403             p_main_intf->statusbar->SetStatusText(
404                 wxString::Format(wxT("x%.2f"),
405                 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
406             i_old_rate = val.i_int;
407         }
408     }
409 }
410
411 /*****************************************************************************
412  * Event Handlers.
413  *****************************************************************************/
414 void InputManager::OnDiscMenu( wxCommandEvent& WXUNUSED(event) )
415 {
416     input_thread_t *p_input =
417         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
418                                            FIND_ANYWHERE );
419     if( p_input )
420     {
421         vlc_value_t val; val.i_int = 2;
422
423         var_Set( p_input, "title  0", val);
424         vlc_object_release( p_input );
425     }
426 }
427
428 void InputManager::OnDiscPrev( wxCommandEvent& WXUNUSED(event) )
429 {
430     input_thread_t *p_input =
431         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
432                                            FIND_ANYWHERE );
433     if( p_input )
434     {
435         int i_type = var_Type( p_input, "prev-chapter" );
436         vlc_value_t val; val.b_bool = true;
437
438         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
439                  "prev-chapter" : "prev-title", val );
440
441         vlc_object_release( p_input );
442     }
443 }
444
445 void InputManager::OnDiscNext( wxCommandEvent& WXUNUSED(event) )
446 {
447     input_thread_t *p_input =
448         (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
449                                            FIND_ANYWHERE );
450     if( p_input )
451     {
452         int i_type = var_Type( p_input, "next-chapter" );
453         vlc_value_t val; val.b_bool = true;
454
455         var_Set( p_input, ( i_type & VLC_VAR_TYPE ) != 0 ?
456                  "next-chapter" : "next-title", val );
457
458         vlc_object_release( p_input );
459     }
460 }
461
462 void InputManager::OnSliderUpdate( wxScrollEvent& event )
463 {
464     vlc_mutex_lock( &p_intf->change_lock );
465
466 #ifdef WIN32
467     if( event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE
468         || event.GetEventType() == wxEVT_SCROLL_ENDSCROLL )
469     {
470 #endif
471         if( i_slider_pos != event.GetPosition() && p_intf->p_sys->p_input )
472         {
473             vlc_value_t pos;
474             pos.f_float = (float)event.GetPosition() / (float)SLIDER_MAX_POS;
475             var_Set( p_intf->p_sys->p_input, "position", pos );
476         }
477
478 #ifdef WIN32
479         b_slider_free = true;
480     }
481     else
482     {
483         b_slider_free = false;
484         if( p_intf->p_sys->p_input ) UpdateTime();
485     }
486 #endif
487
488 #undef WIN32
489     vlc_mutex_unlock( &p_intf->change_lock );
490 }
491
492 void InputManager::ShowSlider( bool show )
493 {
494     if( !!show == !!slider->IsShown() ) return;
495
496     UpdateVideoWindow( p_intf, p_main_intf->video_window );
497
498     sizer->Show( slider, show );
499     sizer->Layout();
500
501     wxCommandEvent intf_event( wxEVT_INTF, 0 );
502     p_main_intf->AddPendingEvent( intf_event );
503 }
504
505 void InputManager::ShowDiscFrame( bool show )
506 {
507     if( !!show == !!disc_frame->IsShown() ) return;
508
509     UpdateVideoWindow( p_intf, p_main_intf->video_window );
510
511     sizer->Show( disc_frame, show );
512     sizer->Layout();
513
514     wxCommandEvent intf_event( wxEVT_INTF, 0 );
515     p_main_intf->AddPendingEvent( intf_event );
516 }