1 /*****************************************************************************
2 * timer.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2005 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
38 #include "wxwindows.h"
41 //void DisplayStreamDate( wxControl *, intf_thread_t *, int );
43 /* Callback prototypes */
44 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
45 vlc_value_t old_val, vlc_value_t new_val, void *param );
46 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
47 vlc_value_t old_val, vlc_value_t new_val, void *param );
49 /*****************************************************************************
51 *****************************************************************************/
52 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
55 p_main_interface = _p_main_interface;
57 i_old_playing_status = PAUSE_S;
58 i_old_rate = INPUT_RATE_DEFAULT;
60 /* Register callback for the intf-popupmenu variable */
61 playlist_t *p_playlist =
62 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
64 if( p_playlist != NULL )
66 var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
67 var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
68 vlc_object_release( p_playlist );
71 Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
76 /* Unregister callback */
77 playlist_t *p_playlist =
78 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
80 if( p_playlist != NULL )
82 var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
83 var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
84 vlc_object_release( p_playlist );
87 vlc_mutex_lock( &p_intf->change_lock );
88 if( p_intf->p_sys->p_input ) vlc_object_release( p_intf->p_sys->p_input );
89 p_intf->p_sys->p_input = NULL;
90 vlc_mutex_unlock( &p_intf->change_lock );
93 /*****************************************************************************
95 *****************************************************************************/
97 /*****************************************************************************
98 * Manage: manage main thread messages
99 *****************************************************************************
100 * In this function, called approx. 10 times a second, we check what the
101 * main program wanted to tell us.
102 *****************************************************************************/
105 #if defined( __WXMSW__ ) /* Work-around a bug with accelerators */
108 p_main_interface->Init();
113 vlc_mutex_lock( &p_intf->change_lock );
115 /* Update the input */
116 if( p_intf->p_sys->p_input == NULL )
118 playlist_t *p_playlist =
119 (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
121 if( p_playlist != NULL )
123 LockPlaylist( p_intf->p_sys, p_playlist );
124 p_intf->p_sys->p_input = p_playlist->p_input;
125 if( p_intf->p_sys->p_input )
126 vlc_object_yield( p_intf->p_sys->p_input );
127 UnlockPlaylist( p_intf->p_sys, p_playlist );
128 vlc_object_release( p_playlist );
131 /* Refresh interface */
132 if( p_intf->p_sys->p_input )
134 p_main_interface->slider->SetValue( 0 );
136 char *psz_now_playing = vlc_input_item_GetInfo(
137 p_intf->p_sys->p_input->input.p_item,
138 _("Meta-information"), _( "Now Playing" ) );
139 if( psz_now_playing && *psz_now_playing )
141 p_main_interface->statusbar->SetStatusText(
142 wxString(wxU(psz_now_playing)) + wxT( " - " ) +
143 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
147 p_main_interface->statusbar->SetStatusText(
148 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
150 free( psz_now_playing );
152 p_main_interface->TogglePlayButton( PLAYING_S );
153 #ifdef wxHAS_TASK_BAR_ICON
154 if( p_main_interface->p_systray )
156 p_main_interface->p_systray->UpdateTooltip( wxU(p_intf->p_sys->p_input->input.p_item->psz_name) + wxString(wxT(" - ")) + wxU(_("Playing")));
159 i_old_playing_status = PLAYING_S;
162 else if( p_intf->p_sys->p_input->b_dead )
164 //controls auto-hide after a timer
165 p_main_interface->m_controls_timer.Start(1000, wxTIMER_ONE_SHOT);
167 p_main_interface->TogglePlayButton( PAUSE_S );
168 i_old_playing_status = PAUSE_S;
170 p_main_interface->statusbar->SetStatusText( wxT(""), 0 );
171 p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
173 #ifdef wxHAS_TASK_BAR_ICON
174 if( p_main_interface->p_systray )
176 p_main_interface->p_systray->UpdateTooltip( wxString(wxT("VLC media player - ")) + wxU(_("Stopped")) );
179 vlc_object_release( p_intf->p_sys->p_input );
180 p_intf->p_sys->p_input = NULL;
183 if( p_intf->p_sys->p_input )
185 input_thread_t *p_input = p_intf->p_sys->p_input;
188 if( !p_input->b_die )
192 //prevent the controls from auto-hiding
193 p_main_interface->m_controls_timer.Stop();
195 /* New input or stream map change */
196 p_intf->p_sys->b_playing = 1;
198 /* Update the item name */
199 char *psz_now_playing = vlc_input_item_GetInfo(
200 p_intf->p_sys->p_input->input.p_item,
201 _("Meta-information"), _( "Now Playing" ) );
202 if( psz_now_playing && *psz_now_playing )
204 p_main_interface->statusbar->SetStatusText(
205 wxString(wxU(psz_now_playing)) + wxT( " - " ) +
206 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
210 p_main_interface->statusbar->SetStatusText(
211 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
213 free( psz_now_playing );
215 /* Manage the slider */
216 var_Get( p_input, "position", &pos );
218 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
219 if( val.i_int > 0 && !p_main_interface->disc_menu_button->IsShown() )
223 #define HELP_MENU N_("Menu")
224 #define HELP_PCH N_("Previous chapter")
225 #define HELP_NCH N_("Next chapter")
226 #define HELP_PTR N_("Previous track")
227 #define HELP_NTR N_("Next track")
229 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val,
234 p_main_interface->disc_menu_button->Show();
235 p_main_interface->disc_sizer->Show(
236 p_main_interface->disc_menu_button );
237 p_main_interface->disc_sizer->Layout();
238 p_main_interface->disc_sizer->Fit(
239 p_main_interface->disc_frame );
240 p_main_interface->disc_menu_button->SetToolTip(
241 wxU(_( HELP_MENU ) ) );
242 p_main_interface->disc_prev_button->SetToolTip(
243 wxU(_( HELP_PCH ) ) );
244 p_main_interface->disc_next_button->SetToolTip(
245 wxU(_( HELP_NCH ) ) );
249 p_main_interface->disc_menu_button->Hide();
250 p_main_interface->disc_sizer->Hide(
251 p_main_interface->disc_menu_button );
253 p_main_interface->disc_prev_button->SetToolTip(
254 wxU(_( HELP_PTR ) ) );
255 p_main_interface->disc_next_button->SetToolTip(
256 wxU(_( HELP_NTR ) ) );
259 p_main_interface->ShowDiscFrame();
263 if( pos.f_float > 0.0 )
265 /* Show the slider if it's position is significant */
266 p_main_interface->ShowSlider();
270 p_main_interface->m_slider_timer.Start(1000, wxTIMER_ONE_SHOT);
273 if( p_intf->p_sys->b_playing && p_main_interface->slider_frame->IsShown() )
275 /* Update the slider if the user isn't dragging it. */
276 if( p_intf->p_sys->b_slider_free )
278 char psz_time[ MSTRTIME_MAX_SIZE ];
279 char psz_total[ MSTRTIME_MAX_SIZE ];
283 /* Update the value */
284 if( pos.f_float >= 0.0 )
286 p_intf->p_sys->i_slider_pos =
287 (int)(SLIDER_MAX_POS * pos.f_float);
289 p_main_interface->slider->SetValue(
290 p_intf->p_sys->i_slider_pos );
292 var_Get( p_intf->p_sys->p_input, "time", &time );
293 i_seconds = time.i_time / 1000000;
294 secstotimestr ( psz_time, i_seconds );
296 var_Get( p_intf->p_sys->p_input, "length", &time );
297 i_seconds = time.i_time / 1000000;
298 secstotimestr ( psz_total, i_seconds );
300 p_main_interface->statusbar->SetStatusText(
301 wxU(psz_time) + wxString(wxT(" / ")) +
307 vlc_mutex_lock( &p_input->stream.stream_lock );
308 if( p_intf->p_sys->p_input->stream.b_seekable &&
309 !p_main_interface->slider_frame->IsShown() )
311 /* Done like this because b_seekable is set slightly after
312 * the new input object is available. */
313 p_main_interface->ShowSlider();
315 if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
317 /* Update the slider if the user isn't dragging it. */
318 if( p_intf->p_sys->b_slider_free )
321 char psz_time[ MSTRTIME_MAX_SIZE ];
322 char psz_total[ MSTRTIME_MAX_SIZE ];
326 /* Update the value */
327 var_Get( p_input, "position", &pos );
328 if( pos.f_float >= 0.0 )
330 p_intf->p_sys->i_slider_pos =
331 (int)(SLIDER_MAX_POS * pos.f_float);
333 p_main_interface->slider->SetValue(
334 p_intf->p_sys->i_slider_pos );
336 var_Get( p_intf->p_sys->p_input, "time", &time );
337 i_seconds = time.i_time / 1000000;
338 secstotimestr ( psz_time, i_seconds );
340 var_Get( p_intf->p_sys->p_input, "length", &time );
341 i_seconds = time.i_time / 1000000;
342 secstotimestr ( psz_total, i_seconds );
344 p_main_interface->statusbar->SetStatusText(
345 wxU(psz_time) + wxString(wxT(" / ")) +
350 vlc_mutex_unlock( &p_input->stream.stream_lock );
352 /* Take care of the volume, etc... */
353 p_main_interface->Update();
355 /* Manage Playing status */
356 var_Get( p_input, "state", &val );
357 if( i_old_playing_status != val.i_int )
359 if( val.i_int == PAUSE_S )
361 p_main_interface->TogglePlayButton( PAUSE_S );
365 p_main_interface->TogglePlayButton( PLAYING_S );
367 #ifdef wxHAS_TASK_BAR_ICON
368 if( p_main_interface->p_systray )
370 if( val.i_int == PAUSE_S )
372 p_main_interface->p_systray->UpdateTooltip( wxU(p_intf->p_sys->p_input->input.p_item->psz_name) + wxString(wxT(" - ")) + wxU(_("Paused")));
376 p_main_interface->p_systray->UpdateTooltip( wxU(p_intf->p_sys->p_input->input.p_item->psz_name) + wxString(wxT(" - ")) + wxU(_("Playing")));
380 i_old_playing_status = val.i_int;
383 /* Manage Speed status */
384 var_Get( p_input, "rate", &val );
385 if( i_old_rate != val.i_int )
387 p_main_interface->statusbar->SetStatusText(
388 wxString::Format(wxT("x%.2f"),
389 (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
390 i_old_rate = val.i_int;
394 else if( p_intf->p_sys->b_playing && !p_intf->b_die )
396 p_intf->p_sys->b_playing = 0;
397 p_main_interface->TogglePlayButton( PAUSE_S );
398 i_old_playing_status = PAUSE_S;
401 /* Show the interface, if requested */
402 if( p_intf->p_sys->b_intf_show )
404 p_main_interface->Raise();
405 p_intf->p_sys->b_intf_show = VLC_FALSE;
410 vlc_mutex_unlock( &p_intf->change_lock );
412 /* Prepare to die, young Skywalker */
413 p_main_interface->Close(TRUE);
417 vlc_mutex_unlock( &p_intf->change_lock );
420 /*****************************************************************************
421 * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
422 * We don't show the menu directly here because we don't want the
423 * caller to block for a too long time.
424 *****************************************************************************/
425 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
426 vlc_value_t old_val, vlc_value_t new_val, void *param )
428 intf_thread_t *p_intf = (intf_thread_t *)param;
430 if( p_intf->p_sys->pf_show_dialog )
432 p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
440 /*****************************************************************************
441 * IntfShowCB: callback triggered by the intf-show playlist variable.
442 *****************************************************************************/
443 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
444 vlc_value_t old_val, vlc_value_t new_val, void *param )
446 intf_thread_t *p_intf = (intf_thread_t *)param;
447 p_intf->p_sys->b_intf_show = VLC_TRUE;