* modules/audio_output/alsa.c: the aout-device obj var wasn't destroyed properly...
[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.19 2003/05/26 19:06:47 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 /* Callback prototype */
53 int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
54                  vlc_value_t old_val, vlc_value_t new_val, void *param );
55
56 /*****************************************************************************
57  * Constructor.
58  *****************************************************************************/
59 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
60 {
61     p_intf = _p_intf;
62     p_main_interface = _p_main_interface;
63     i_old_playing_status = PAUSE_S;
64     i_old_rate = DEFAULT_RATE;
65
66     /* Register callback for the intf-popupmenu variable */
67     playlist_t *p_playlist =
68         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69                                        FIND_ANYWHERE );
70     if( p_playlist != NULL )
71     {
72         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB,
73                          p_main_interface );
74         vlc_object_release( p_playlist );
75     }
76
77     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
78 }
79
80 Timer::~Timer()
81 {
82 }
83
84 /*****************************************************************************
85  * Private methods.
86  *****************************************************************************/
87 /*****************************************************************************
88  * wxModeManage: actualise the aspect of the interface whenever the input
89  * changes.
90  *****************************************************************************
91  * The lock has to be taken before you call the function.
92  *****************************************************************************/
93 static int wxModeManage( intf_thread_t * p_intf )
94 {
95     return 0;
96 }
97
98 /*****************************************************************************
99  * wxSetupMenus: function that generates title/chapter/audio/subpic
100  * menus with help from preceding functions
101  *****************************************************************************
102  * Function called with the lock on stream
103  *****************************************************************************/
104 static int wxSetupMenus( intf_thread_t * p_intf )
105 {
106     return 0;
107 }
108
109 /*****************************************************************************
110  * Manage: manage main thread messages
111  *****************************************************************************
112  * In this function, called approx. 10 times a second, we check what the
113  * main program wanted to tell us.
114  *****************************************************************************/
115 void Timer::Notify()
116 {
117     vlc_bool_t b_pace_control;
118
119     vlc_mutex_lock( &p_intf->change_lock );
120
121     /* If the "display popup" flag has changed */
122     if( p_main_interface->b_popup_change )
123     {
124         wxPoint mousepos = wxGetMousePosition();
125
126 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
127         wxContextMenuEvent event =
128             wxContextMenuEvent( wxEVT_NULL, 0, mousepos );
129 #else
130         wxMouseEvent event = wxMouseEvent( wxEVT_RIGHT_UP );
131         event.m_x = p_main_interface->ScreenToClient(mousepos).x;
132         event.m_y = p_main_interface->ScreenToClient(mousepos).y;
133 #endif
134         p_main_interface->AddPendingEvent(event);
135
136         p_main_interface->b_popup_change = VLC_FALSE;
137     }
138
139     /* Update the log window */
140     p_intf->p_sys->p_messages_window->UpdateLog();
141
142     /* Update the playlist */
143     p_intf->p_sys->p_playlist_window->UpdatePlaylist();
144
145     /* Update the fileinfo windows */
146     p_intf->p_sys->p_fileinfo_window->UpdateFileInfo();
147
148     /* Update the input */
149     if( p_intf->p_sys->p_input == NULL )
150     {
151         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
152                                                        VLC_OBJECT_INPUT,
153                                                        FIND_ANYWHERE );
154
155         /* Show slider */
156         if( p_intf->p_sys->p_input )
157         {
158             //if( p_intf->p_sys->p_input->stream.b_seekable )
159             {
160                 p_main_interface->slider_frame->Show();
161                 p_main_interface->frame_sizer->Show(
162                     p_main_interface->slider_frame );
163                 p_main_interface->frame_sizer->Layout();
164                 p_main_interface->frame_sizer->Fit( p_main_interface );
165             }
166
167             p_main_interface->statusbar->SetStatusText(
168                 wxU(p_intf->p_sys->p_input->psz_source), 2 );
169
170             p_main_interface->TogglePlayButton( PLAYING_S );
171             i_old_playing_status = PLAYING_S;
172         }
173
174         /* control buttons for free pace streams */
175         b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
176
177     }
178     else if( p_intf->p_sys->p_input->b_dead )
179     {
180         /* Hide slider */
181         //if( p_intf->p_sys->p_input->stream.b_seekable )
182         {
183             p_main_interface->slider_frame->Hide();
184             p_main_interface->frame_sizer->Hide(
185                 p_main_interface->slider_frame );
186             p_main_interface->frame_sizer->Layout();
187             p_main_interface->frame_sizer->Fit( p_main_interface );
188
189             p_main_interface->TogglePlayButton( PAUSE_S );
190             i_old_playing_status = PAUSE_S;
191         }
192
193         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
194
195         vlc_object_release( p_intf->p_sys->p_input );
196         p_intf->p_sys->p_input = NULL;
197     }
198
199
200
201     if( p_intf->p_sys->p_input )
202     {
203         input_thread_t *p_input = p_intf->p_sys->p_input;
204
205         vlc_mutex_lock( &p_input->stream.stream_lock );
206
207         if( !p_input->b_die )
208         {
209             /* New input or stream map change */
210             p_intf->p_sys->b_playing = 1;
211 #if 0
212             if( p_input->stream.b_changed )
213             {
214                 wxModeManage( p_intf );
215                 wxSetupMenus( p_intf );
216                 p_intf->p_sys->b_playing = 1;
217
218                 p_main_interface->TogglePlayButton( PLAYING_S );
219                 i_old_playing_status = PLAYING_S;
220             }
221 #endif
222
223             /* Manage the slider */
224             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
225             {
226                 stream_position_t position;
227
228                 /* Update the slider if the user isn't dragging it. */
229                 if( p_intf->p_sys->b_slider_free )
230                 {
231                     /* Update the value */
232                     vlc_mutex_unlock( &p_input->stream.stream_lock );
233                     input_Tell( p_input, &position );
234                     vlc_mutex_lock( &p_input->stream.stream_lock );
235                     if( position.i_size )
236                     {
237                         p_intf->p_sys->i_slider_pos =
238                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
239
240                         p_main_interface->slider->SetValue(
241                             p_intf->p_sys->i_slider_pos );
242
243                         DisplayStreamDate( p_main_interface->slider_box,p_intf,
244                                            p_intf->p_sys->i_slider_pos );
245                     }
246                 }
247             }
248
249             if( p_intf->p_sys->i_part !=
250                 p_input->stream.p_selected_area->i_part )
251             {
252                 p_intf->p_sys->b_chapter_update = 1;
253                 wxSetupMenus( p_intf );
254             }
255
256             /* Manage Playing status */
257             if( i_old_playing_status != p_input->stream.control.i_status )
258             {
259                 if( p_input->stream.control.i_status == PAUSE_S )
260                 {
261                     p_main_interface->TogglePlayButton( PAUSE_S );
262                 }
263                 else
264                 {
265                     p_main_interface->TogglePlayButton( PLAYING_S );
266                 }
267                 i_old_playing_status = p_input->stream.control.i_status;
268             }
269
270             /* Manage Speed status */
271             if( i_old_rate != p_input->stream.control.i_rate )
272             {
273                 p_main_interface->statusbar->SetStatusText(
274                     wxString::Format(wxT("x%.2f"),
275                     1000.0 / p_input->stream.control.i_rate), 1 );
276                 i_old_rate = p_input->stream.control.i_rate;
277             }
278         }
279
280         vlc_mutex_unlock( &p_input->stream.stream_lock );
281     }
282     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
283     {
284         wxModeManage( p_intf );
285         p_intf->p_sys->b_playing = 0;
286         p_main_interface->TogglePlayButton( PAUSE_S );
287         i_old_playing_status = PAUSE_S;
288     }
289
290     if( p_intf->b_die )
291     {
292         vlc_mutex_unlock( &p_intf->change_lock );
293
294         /* Prepare to die, young Skywalker */
295         p_main_interface->Close(TRUE);
296         return;
297     }
298
299     vlc_mutex_unlock( &p_intf->change_lock );
300 }
301
302 /*****************************************************************************
303  * DisplayStreamDate: display stream date
304  *****************************************************************************
305  * This function displays the current date related to the position in
306  * the stream. It is called whenever the slider changes its value.
307  * The lock has to be taken before you call the function.
308  *****************************************************************************/
309 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
310                         int i_pos )
311 {
312     if( p_intf->p_sys->p_input )
313     {
314 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
315         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
316
317         p_slider_frame->SetLabel(
318             wxU(input_OffsetToTime( p_intf->p_sys->p_input,
319                     psz_time, p_area->i_size * i_pos / SLIDER_MAX_POS )) );
320 #undef p_area
321      }
322 }
323
324 /*****************************************************************************
325  * PlaylistChanged: callback triggered by the intf-change playlist variable
326  *  We don't rebuild the playlist directly here because we don't want the
327  *  caller to block for a too long time.
328  *****************************************************************************/
329 int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
330                  vlc_value_t old_val, vlc_value_t new_val, void *param )
331 {
332     Interface *p_main_interface = (Interface *)param;
333
334     p_main_interface->b_popup_change = VLC_TRUE;
335
336     return VLC_SUCCESS;
337 }