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