]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/timer.cpp
2a7b7af543abdb9aa0c227e57fa6cc2e89261143
[vlc] / modules / gui / wxwindows / timer.cpp
1 /*****************************************************************************
2  * timer.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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     b_init = 0;
53     i_old_playing_status = PAUSE_S;
54     i_old_rate = INPUT_RATE_DEFAULT;
55
56     /* Register callback for the intf-popupmenu variable */
57     playlist_t *p_playlist =
58         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
59                                        FIND_ANYWHERE );
60     if( p_playlist != NULL )
61     {
62         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
63         vlc_object_release( p_playlist );
64     }
65
66     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
67 }
68
69 Timer::~Timer()
70 {
71     /* Unregister callback */
72     playlist_t *p_playlist =
73         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
74                                        FIND_ANYWHERE );
75     if( p_playlist != NULL )
76     {
77         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
78         vlc_object_release( p_playlist );
79     }
80 }
81
82 /*****************************************************************************
83  * Private methods.
84  *****************************************************************************/
85
86 /*****************************************************************************
87  * Manage: manage main thread messages
88  *****************************************************************************
89  * In this function, called approx. 10 times a second, we check what the
90  * main program wanted to tell us.
91  *****************************************************************************/
92 void Timer::Notify()
93 {
94 #if defined( __WXMSW__ ) /* Work-around a bug with accelerators */
95     if( !b_init )
96     {
97         p_main_interface->Init();
98         b_init = VLC_TRUE;
99     }
100 #endif
101
102     vlc_mutex_lock( &p_intf->change_lock );
103
104     /* Update the input */
105     if( p_intf->p_sys->p_input == NULL )
106     {
107         p_intf->p_sys->p_input =
108             (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
109                                                FIND_ANYWHERE );
110
111         /* Refresh interface */
112         if( p_intf->p_sys->p_input )
113         {
114             p_main_interface->slider->SetValue( 0 );
115             b_old_seekable = VLC_FALSE;
116
117             p_main_interface->statusbar->SetStatusText(
118                 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
119
120             p_main_interface->TogglePlayButton( PLAYING_S );
121             i_old_playing_status = PLAYING_S;
122         }
123     }
124     else if( p_intf->p_sys->p_input->b_dead )
125     {
126         /* Hide slider */
127         p_main_interface->slider_frame->Hide();
128         p_main_interface->frame_sizer->Hide(
129             p_main_interface->slider_frame );
130         p_main_interface->frame_sizer->Layout();
131         p_main_interface->frame_sizer->Fit( p_main_interface );
132
133         p_main_interface->TogglePlayButton( PAUSE_S );
134         i_old_playing_status = PAUSE_S;
135
136         p_main_interface->statusbar->SetStatusText( wxT(""), 0 );
137         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
138
139         vlc_object_release( p_intf->p_sys->p_input );
140         p_intf->p_sys->p_input = NULL;
141     }
142
143
144     if( p_intf->p_sys->p_input )
145     {
146         input_thread_t *p_input = p_intf->p_sys->p_input;
147         vlc_value_t val;
148
149         if( !p_input->b_die )
150         {
151             vlc_value_t pos;
152
153             /* New input or stream map change */
154             p_intf->p_sys->b_playing = 1;
155
156             /* Manage the slider */
157             /* FIXME --fenrir */
158             /* Change the name of b_old_seekable into b_show_bar or something like that */
159             var_Get( p_input, "position", &pos );
160
161             if( !b_old_seekable )
162             {
163                 if( pos.f_float > 0.0 )
164                 {
165                     /* Done like this, as it's the only way to know if the slider
166                      * has to be displayed */
167                     b_old_seekable = VLC_TRUE;
168                     p_main_interface->slider_frame->Show();
169                     p_main_interface->frame_sizer->Show(
170                         p_main_interface->slider_frame );
171                     p_main_interface->frame_sizer->Layout();
172                     p_main_interface->frame_sizer->Fit( p_main_interface );
173
174                 }
175             }
176
177             if( p_intf->p_sys->b_playing && b_old_seekable )
178             {
179                 /* Update the slider if the user isn't dragging it. */
180                 if( p_intf->p_sys->b_slider_free )
181                 {
182                     char psz_time[ MSTRTIME_MAX_SIZE ];
183                     char psz_total[ MSTRTIME_MAX_SIZE ];
184                     vlc_value_t time;
185                     mtime_t i_seconds;
186
187                     /* Update the value */
188                     if( pos.f_float >= 0.0 )
189                     {
190                         p_intf->p_sys->i_slider_pos =
191                             (int)(SLIDER_MAX_POS * pos.f_float);
192
193                         p_main_interface->slider->SetValue(
194                             p_intf->p_sys->i_slider_pos );
195
196                         var_Get( p_intf->p_sys->p_input, "time", &time );
197                         i_seconds = time.i_time / 1000000;
198                         secstotimestr ( psz_time, i_seconds );
199
200                         var_Get( p_intf->p_sys->p_input, "length",  &time );
201                         i_seconds = time.i_time / 1000000;
202                         secstotimestr ( psz_total, i_seconds );
203
204                         p_main_interface->statusbar->SetStatusText(
205                             wxU(psz_time) + wxString(wxT(" / ")) +
206                             wxU(psz_total), 0 );
207                     }
208                 }
209             }
210 #if 0
211         vlc_mutex_lock( &p_input->stream.stream_lock );
212             if( p_intf->p_sys->p_input->stream.b_seekable && !b_old_seekable )
213             {
214                 /* Done like this because b_seekable is set slightly after
215                  * the new input object is available. */
216                 b_old_seekable = VLC_TRUE;
217                 p_main_interface->slider_frame->Show();
218                 p_main_interface->frame_sizer->Show(
219                     p_main_interface->slider_frame );
220                 p_main_interface->frame_sizer->Layout();
221                 p_main_interface->frame_sizer->Fit( p_main_interface );
222             }
223             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
224             {
225                 /* Update the slider if the user isn't dragging it. */
226                 if( p_intf->p_sys->b_slider_free )
227                 {
228                     vlc_value_t pos;
229                     char psz_time[ MSTRTIME_MAX_SIZE ];
230                     char psz_total[ MSTRTIME_MAX_SIZE ];
231                     vlc_value_t time;
232                     mtime_t i_seconds;
233
234                     /* Update the value */
235                     var_Get( p_input, "position", &pos );
236                     if( pos.f_float >= 0.0 )
237                     {
238                         p_intf->p_sys->i_slider_pos =
239                             (int)(SLIDER_MAX_POS * pos.f_float);
240
241                         p_main_interface->slider->SetValue(
242                             p_intf->p_sys->i_slider_pos );
243
244                         var_Get( p_intf->p_sys->p_input, "time", &time );
245                         i_seconds = time.i_time / 1000000;
246                         secstotimestr ( psz_time, i_seconds );
247
248                         var_Get( p_intf->p_sys->p_input, "length",  &time );
249                         i_seconds = time.i_time / 1000000;
250                         secstotimestr ( psz_total, i_seconds );
251
252                         p_main_interface->statusbar->SetStatusText(
253                             wxU(psz_time) + wxString(wxT(" / ")) +
254                             wxU(psz_total), 0 );
255                     }
256                 }
257             }
258         vlc_mutex_unlock( &p_input->stream.stream_lock );
259 #endif
260             /* Take care of the volume, etc... */
261             p_main_interface->Update();
262
263             /* Manage Playing status */
264             var_Get( p_input, "state", &val );
265             if( i_old_playing_status != val.i_int )
266             {
267                 if( val.i_int == PAUSE_S )
268                 {
269                     p_main_interface->TogglePlayButton( PAUSE_S );
270                 }
271                 else
272                 {
273                     p_main_interface->TogglePlayButton( PLAYING_S );
274                 }
275                 i_old_playing_status = val.i_int;
276             }
277
278             /* Manage Speed status */
279             var_Get( p_input, "rate", &val );
280             if( i_old_rate != val.i_int )
281             {
282                 p_main_interface->statusbar->SetStatusText(
283                     wxString::Format(wxT("x%.2f"),
284                     (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
285                 i_old_rate = val.i_int;
286             }
287         }
288
289     }
290     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
291     {
292         p_intf->p_sys->b_playing = 0;
293         p_main_interface->TogglePlayButton( PAUSE_S );
294         i_old_playing_status = PAUSE_S;
295     }
296
297     if( p_intf->b_die )
298     {
299         vlc_mutex_unlock( &p_intf->change_lock );
300
301         /* Prepare to die, young Skywalker */
302         p_main_interface->Close(TRUE);
303         return;
304     }
305
306     vlc_mutex_unlock( &p_intf->change_lock );
307 }
308
309 /*****************************************************************************
310  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
311  *  We don't show the menu directly here because we don't want the
312  *  caller to block for a too long time.
313  *****************************************************************************/
314 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
315                         vlc_value_t old_val, vlc_value_t new_val, void *param )
316 {
317     intf_thread_t *p_intf = (intf_thread_t *)param;
318
319     if( p_intf->p_sys->pf_show_dialog )
320     {
321         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
322                                        new_val.b_bool, 0 );
323     }
324
325     return VLC_SUCCESS;
326 }