17def5be0ad6e8a519e93c513f5e2332724f1ef7
[vlc.git] / modules / gui / wxwindows / timer.cpp
1 /*****************************************************************************
2  * timer.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: timer.cpp,v 1.31 2003/08/28 15:59:04 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34 #include <vlc/intf.h>
35
36 #include "wxwindows.h"
37 #include <wx/timer.h>
38
39 void DisplayStreamDate( wxControl *, intf_thread_t *, int );
40
41 /* Callback prototype */
42 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
43                        vlc_value_t old_val, vlc_value_t new_val, void *param );
44
45 /*****************************************************************************
46  * Constructor.
47  *****************************************************************************/
48 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
49 {
50     p_intf = _p_intf;
51     p_main_interface = _p_main_interface;
52     i_old_playing_status = PAUSE_S;
53     i_old_rate = DEFAULT_RATE;
54
55     /* Register callback for the intf-popupmenu variable */
56     playlist_t *p_playlist =
57         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
58                                        FIND_ANYWHERE );
59     if( p_playlist != NULL )
60     {
61         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
62         vlc_object_release( p_playlist );
63     }
64
65     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
66 }
67
68 Timer::~Timer()
69 {
70 }
71
72 /*****************************************************************************
73  * Private methods.
74  *****************************************************************************/
75
76 /*****************************************************************************
77  * Manage: manage main thread messages
78  *****************************************************************************
79  * In this function, called approx. 10 times a second, we check what the
80  * main program wanted to tell us.
81  *****************************************************************************/
82 void Timer::Notify()
83 {
84     vlc_bool_t b_pace_control;
85
86     vlc_mutex_lock( &p_intf->change_lock );
87
88     /* Update the input */
89     if( p_intf->p_sys->p_input == NULL )
90     {
91         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
92                                                        VLC_OBJECT_INPUT,
93                                                        FIND_ANYWHERE );
94
95         /* Show slider */
96         if( p_intf->p_sys->p_input )
97         {
98             //if( p_intf->p_sys->p_input->stream.b_seekable )
99             {
100                 p_main_interface->slider_frame->Show();
101                 p_main_interface->frame_sizer->Show(
102                     p_main_interface->slider_frame );
103                 p_main_interface->frame_sizer->Layout();
104                 p_main_interface->frame_sizer->Fit( p_main_interface );
105             }
106
107             p_main_interface->statusbar->SetStatusText(
108                 wxU(p_intf->p_sys->p_input->psz_source), 2 );
109
110             p_main_interface->TogglePlayButton( PLAYING_S );
111             i_old_playing_status = PLAYING_S;
112
113             /* Take care of the volume */
114             audio_volume_t i_volume;
115             aout_VolumeGet( p_intf, &i_volume );
116             p_main_interface->volctrl->SetValue( i_volume * 200 /
117                                                  AOUT_VOLUME_MAX );
118             p_main_interface->volctrl->SetToolTip(
119                 wxString::Format((wxString)wxU(_("Volume")) + wxT(" %d"),
120                 i_volume * 200 / AOUT_VOLUME_MAX ) );
121
122             /* control buttons for free pace streams */
123             b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
124         }
125     }
126     else if( p_intf->p_sys->p_input->b_dead )
127     {
128         /* Hide slider */
129         //if( p_intf->p_sys->p_input->stream.b_seekable )
130         {
131             p_main_interface->slider_frame->Hide();
132             p_main_interface->frame_sizer->Hide(
133                 p_main_interface->slider_frame );
134             p_main_interface->frame_sizer->Layout();
135             p_main_interface->frame_sizer->Fit( p_main_interface );
136
137             p_main_interface->TogglePlayButton( PAUSE_S );
138             i_old_playing_status = PAUSE_S;
139         }
140
141         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
142
143         vlc_object_release( p_intf->p_sys->p_input );
144         p_intf->p_sys->p_input = NULL;
145     }
146
147
148
149     if( p_intf->p_sys->p_input )
150     {
151         input_thread_t *p_input = p_intf->p_sys->p_input;
152
153         vlc_mutex_lock( &p_input->stream.stream_lock );
154
155         if( !p_input->b_die )
156         {
157             /* New input or stream map change */
158             p_intf->p_sys->b_playing = 1;
159 #if 0
160             if( p_input->stream.b_changed )
161             {
162                 wxModeManage( p_intf );
163                 wxSetupMenus( p_intf );
164                 p_intf->p_sys->b_playing = 1;
165
166                 p_main_interface->TogglePlayButton( PLAYING_S );
167                 i_old_playing_status = PLAYING_S;
168             }
169 #endif
170
171             /* Manage the slider */
172             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
173             {
174                 stream_position_t position;
175
176                 /* Update the slider if the user isn't dragging it. */
177                 if( p_intf->p_sys->b_slider_free )
178                 {
179                     /* Update the value */
180                     vlc_mutex_unlock( &p_input->stream.stream_lock );
181                     input_Tell( p_input, &position );
182                     vlc_mutex_lock( &p_input->stream.stream_lock );
183                     if( position.i_size )
184                     {
185                         p_intf->p_sys->i_slider_pos =
186                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
187
188                         p_main_interface->slider->SetValue(
189                             p_intf->p_sys->i_slider_pos );
190
191                         DisplayStreamDate( p_main_interface->slider_box,p_intf,
192                                            p_intf->p_sys->i_slider_pos );
193                     }
194                 }
195             }
196
197             /* Manage Playing status */
198             if( i_old_playing_status != p_input->stream.control.i_status )
199             {
200                 if( p_input->stream.control.i_status == PAUSE_S )
201                 {
202                     p_main_interface->TogglePlayButton( PAUSE_S );
203                 }
204                 else
205                 {
206                     p_main_interface->TogglePlayButton( PLAYING_S );
207                 }
208                 i_old_playing_status = p_input->stream.control.i_status;
209             }
210
211             /* Manage Speed status */
212             if( i_old_rate != p_input->stream.control.i_rate )
213             {
214                 p_main_interface->statusbar->SetStatusText(
215                     wxString::Format(wxT("x%.2f"),
216                     1000.0 / p_input->stream.control.i_rate), 1 );
217                 i_old_rate = p_input->stream.control.i_rate;
218             }
219         }
220
221         vlc_mutex_unlock( &p_input->stream.stream_lock );
222     }
223     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
224     {
225         p_intf->p_sys->b_playing = 0;
226         p_main_interface->TogglePlayButton( PAUSE_S );
227         i_old_playing_status = PAUSE_S;
228     }
229
230     if( p_intf->b_die )
231     {
232         vlc_mutex_unlock( &p_intf->change_lock );
233
234         /* Prepare to die, young Skywalker */
235         p_main_interface->Close(TRUE);
236         return;
237     }
238
239     vlc_mutex_unlock( &p_intf->change_lock );
240 }
241
242 /*****************************************************************************
243  * DisplayStreamDate: display stream date
244  *****************************************************************************
245  * This function displays the current date related to the position in
246  * the stream. It is called whenever the slider changes its value.
247  * The lock has to be taken before you call the function.
248  *****************************************************************************/
249 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
250                         int i_pos )
251 {
252     if( p_intf->p_sys->p_input )
253     {
254 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
255         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
256
257         p_slider_frame->SetLabel(
258             wxU(input_OffsetToTime( p_intf->p_sys->p_input,
259                     psz_time, p_area->i_size * i_pos / SLIDER_MAX_POS )) );
260 #undef p_area
261      }
262 }
263
264 /*****************************************************************************
265  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
266  *  We don't show the menu directly here because we don't want the
267  *  caller to block for a too long time.
268  *****************************************************************************/
269 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
270                         vlc_value_t old_val, vlc_value_t new_val, void *param )
271 {
272     intf_thread_t *p_intf = (intf_thread_t *)param;
273
274     if( p_intf->p_sys->pf_show_dialog )
275         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
276                                        new_val.b_bool, 0 );
277
278     return VLC_SUCCESS;
279 }