7e8e55913554d24624b32ff869f6ddd5f16106d1
[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.18 2003/05/18 19:46:35 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
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/timer.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
50 void DisplayStreamDate( wxControl *, intf_thread_t *, int );
51
52 /*****************************************************************************
53  * Constructor.
54  *****************************************************************************/
55 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
56 {
57     p_intf = _p_intf;
58     p_main_interface = _p_main_interface;
59     i_old_playing_status = PAUSE_S;
60     i_old_rate = DEFAULT_RATE;
61
62     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
63 }
64
65 Timer::~Timer()
66 {
67 }
68
69 /*****************************************************************************
70  * Private methods.
71  *****************************************************************************/
72 /*****************************************************************************
73  * wxModeManage: actualise the aspect of the interface whenever the input
74  * changes.
75  *****************************************************************************
76  * The lock has to be taken before you call the function.
77  *****************************************************************************/
78 static int wxModeManage( intf_thread_t * p_intf )
79 {
80     return 0;
81 }
82
83 /*****************************************************************************
84  * wxSetupMenus: function that generates title/chapter/audio/subpic
85  * menus with help from preceding functions
86  *****************************************************************************
87  * Function called with the lock on stream
88  *****************************************************************************/
89 static int wxSetupMenus( intf_thread_t * p_intf )
90 {
91     return 0;
92 }
93
94 /*****************************************************************************
95  * Manage: manage main thread messages
96  *****************************************************************************
97  * In this function, called approx. 10 times a second, we check what the
98  * main program wanted to tell us.
99  *****************************************************************************/
100 void Timer::Notify()
101 {
102     vlc_bool_t b_pace_control;
103
104     vlc_mutex_lock( &p_intf->change_lock );
105
106     /* If the "display popup" flag has changed */
107     if( p_intf->b_menu_change )
108     {
109         wxPoint mousepos = wxGetMousePosition();
110         PopupMenu( p_intf, p_main_interface,
111                    p_main_interface->ScreenToClient(mousepos) );
112         p_intf->b_menu_change = 0;
113     }
114
115     /* Update the log window */
116     p_intf->p_sys->p_messages_window->UpdateLog();
117
118     /* Update the playlist */
119     p_intf->p_sys->p_playlist_window->UpdatePlaylist();
120
121     /* Update the fileinfo windows */
122     p_intf->p_sys->p_fileinfo_window->UpdateFileInfo();
123     
124     /* Update the input */
125     if( p_intf->p_sys->p_input == NULL )
126     {
127         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
128                                                        VLC_OBJECT_INPUT,
129                                                        FIND_ANYWHERE );
130
131         /* Show slider */
132         if( p_intf->p_sys->p_input )
133         {
134             //if( p_intf->p_sys->p_input->stream.b_seekable )
135             {
136                 p_main_interface->slider_frame->Show();
137                 p_main_interface->frame_sizer->Show(
138                     p_main_interface->slider_frame );
139                 p_main_interface->frame_sizer->Layout();
140                 p_main_interface->frame_sizer->Fit( p_main_interface );
141             }
142
143             p_main_interface->statusbar->SetStatusText(
144                 wxU(p_intf->p_sys->p_input->psz_source), 2 );
145
146             p_main_interface->TogglePlayButton( PLAYING_S );
147             i_old_playing_status = PLAYING_S;
148         }
149
150         /* control buttons for free pace streams */
151         b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
152
153     }
154     else if( p_intf->p_sys->p_input->b_dead )
155     {
156         /* Hide slider */
157         //if( p_intf->p_sys->p_input->stream.b_seekable )
158         {
159             p_main_interface->slider_frame->Hide();
160             p_main_interface->frame_sizer->Hide(
161                 p_main_interface->slider_frame );
162             p_main_interface->frame_sizer->Layout();
163             p_main_interface->frame_sizer->Fit( p_main_interface );
164
165             p_main_interface->TogglePlayButton( PAUSE_S );
166             i_old_playing_status = PAUSE_S;
167         }
168
169         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
170
171         vlc_object_release( p_intf->p_sys->p_input );
172         p_intf->p_sys->p_input = NULL;
173     }
174
175
176
177     if( p_intf->p_sys->p_input )
178     {
179         input_thread_t *p_input = p_intf->p_sys->p_input;
180
181         vlc_mutex_lock( &p_input->stream.stream_lock );
182
183         if( !p_input->b_die )
184         {
185             /* New input or stream map change */
186             p_intf->p_sys->b_playing = 1;
187 #if 0
188             if( p_input->stream.b_changed )
189             {
190                 wxModeManage( p_intf );
191                 wxSetupMenus( p_intf );
192                 p_intf->p_sys->b_playing = 1;
193
194                 p_main_interface->TogglePlayButton( PLAYING_S );
195                 i_old_playing_status = PLAYING_S;
196             }
197 #endif
198
199             /* Manage the slider */
200             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
201             {
202                 stream_position_t position;
203
204                 /* Update the slider if the user isn't dragging it. */
205                 if( p_intf->p_sys->b_slider_free )
206                 {
207                     /* Update the value */
208                     vlc_mutex_unlock( &p_input->stream.stream_lock );
209                     input_Tell( p_input, &position );
210                     vlc_mutex_lock( &p_input->stream.stream_lock );
211                     if( position.i_size )
212                     {
213                         p_intf->p_sys->i_slider_pos =
214                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
215
216                         p_main_interface->slider->SetValue(
217                             p_intf->p_sys->i_slider_pos );
218
219                         DisplayStreamDate( p_main_interface->slider_box,p_intf,
220                                            p_intf->p_sys->i_slider_pos );
221                     }
222                 }
223             }
224
225             if( p_intf->p_sys->i_part !=
226                 p_input->stream.p_selected_area->i_part )
227             {
228                 p_intf->p_sys->b_chapter_update = 1;
229                 wxSetupMenus( p_intf );
230             }
231
232             /* Manage Playing status */
233             if( i_old_playing_status != p_input->stream.control.i_status )
234             {
235                 if( p_input->stream.control.i_status == PAUSE_S )
236                 {
237                     p_main_interface->TogglePlayButton( PAUSE_S );
238                 }
239                 else
240                 {
241                     p_main_interface->TogglePlayButton( PLAYING_S );
242                 }
243                 i_old_playing_status = p_input->stream.control.i_status;
244             }
245
246             /* Manage Speed status */
247             if( i_old_rate != p_input->stream.control.i_rate )
248             {
249                 p_main_interface->statusbar->SetStatusText(
250                     wxString::Format(wxT("x%.2f"),
251                     1000.0 / p_input->stream.control.i_rate), 1 );
252                 i_old_rate = p_input->stream.control.i_rate;
253             }
254         }
255
256         vlc_mutex_unlock( &p_input->stream.stream_lock );
257     }
258     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
259     {
260         wxModeManage( p_intf );
261         p_intf->p_sys->b_playing = 0;
262         p_main_interface->TogglePlayButton( PAUSE_S );
263         i_old_playing_status = PAUSE_S;
264     }
265
266     if( p_intf->b_die )
267     {
268         vlc_mutex_unlock( &p_intf->change_lock );
269
270         /* Prepare to die, young Skywalker */
271         p_main_interface->Close(TRUE);
272         return;
273     }
274
275     vlc_mutex_unlock( &p_intf->change_lock );
276 }
277
278 /*****************************************************************************
279  * DisplayStreamDate: display stream date
280  *****************************************************************************
281  * This function displays the current date related to the position in
282  * the stream. It is called whenever the slider changes its value.
283  * The lock has to be taken before you call the function.
284  *****************************************************************************/
285 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
286                         int i_pos )
287 {
288     if( p_intf->p_sys->p_input )
289     {
290 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
291         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
292
293         p_slider_frame->SetLabel(
294             wxU(input_OffsetToTime( p_intf->p_sys->p_input,
295                     psz_time, p_area->i_size * i_pos / SLIDER_MAX_POS )) );
296 #undef p_area
297      }
298 }