]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/timer.cpp
wxwindows/playlist.cpp: also update icon on item change
[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             /* Update the item name */
161             p_main_interface->statusbar->SetStatusText(
162                 wxU(p_intf->p_sys->p_input->input.p_item->psz_name), 2 );
163
164             /* Manage the slider */
165             /* FIXME --fenrir */
166             /* Change the name of b_old_seekable into b_show_bar or something like that */
167             var_Get( p_input, "position", &pos );
168
169             if( !b_old_seekable )
170             {
171                 if( pos.f_float > 0.0 )
172                 {
173                     /* Done like this, as it's the only way to know if the slider
174                      * has to be displayed */
175                     b_old_seekable = VLC_TRUE;
176                     p_main_interface->slider_frame->Show();
177                     p_main_interface->frame_sizer->Show(
178                         p_main_interface->slider_frame );
179                     p_main_interface->frame_sizer->Layout();
180                     p_main_interface->frame_sizer->Fit( p_main_interface );
181
182                 }
183             }
184
185             if( p_intf->p_sys->b_playing && b_old_seekable )
186             {
187                 /* Update the slider if the user isn't dragging it. */
188                 if( p_intf->p_sys->b_slider_free )
189                 {
190                     char psz_time[ MSTRTIME_MAX_SIZE ];
191                     char psz_total[ MSTRTIME_MAX_SIZE ];
192                     vlc_value_t time;
193                     mtime_t i_seconds;
194
195                     /* Update the value */
196                     if( pos.f_float >= 0.0 )
197                     {
198                         p_intf->p_sys->i_slider_pos =
199                             (int)(SLIDER_MAX_POS * pos.f_float);
200
201                         p_main_interface->slider->SetValue(
202                             p_intf->p_sys->i_slider_pos );
203
204                         var_Get( p_intf->p_sys->p_input, "time", &time );
205                         i_seconds = time.i_time / 1000000;
206                         secstotimestr ( psz_time, i_seconds );
207
208                         var_Get( p_intf->p_sys->p_input, "length",  &time );
209                         i_seconds = time.i_time / 1000000;
210                         secstotimestr ( psz_total, i_seconds );
211
212                         p_main_interface->statusbar->SetStatusText(
213                             wxU(psz_time) + wxString(wxT(" / ")) +
214                             wxU(psz_total), 0 );
215                     }
216                 }
217             }
218 #if 0
219         vlc_mutex_lock( &p_input->stream.stream_lock );
220             if( p_intf->p_sys->p_input->stream.b_seekable && !b_old_seekable )
221             {
222                 /* Done like this because b_seekable is set slightly after
223                  * the new input object is available. */
224                 b_old_seekable = VLC_TRUE;
225                 p_main_interface->slider_frame->Show();
226                 p_main_interface->frame_sizer->Show(
227                     p_main_interface->slider_frame );
228                 p_main_interface->frame_sizer->Layout();
229                 p_main_interface->frame_sizer->Fit( p_main_interface );
230             }
231             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
232             {
233                 /* Update the slider if the user isn't dragging it. */
234                 if( p_intf->p_sys->b_slider_free )
235                 {
236                     vlc_value_t pos;
237                     char psz_time[ MSTRTIME_MAX_SIZE ];
238                     char psz_total[ MSTRTIME_MAX_SIZE ];
239                     vlc_value_t time;
240                     mtime_t i_seconds;
241
242                     /* Update the value */
243                     var_Get( p_input, "position", &pos );
244                     if( pos.f_float >= 0.0 )
245                     {
246                         p_intf->p_sys->i_slider_pos =
247                             (int)(SLIDER_MAX_POS * pos.f_float);
248
249                         p_main_interface->slider->SetValue(
250                             p_intf->p_sys->i_slider_pos );
251
252                         var_Get( p_intf->p_sys->p_input, "time", &time );
253                         i_seconds = time.i_time / 1000000;
254                         secstotimestr ( psz_time, i_seconds );
255
256                         var_Get( p_intf->p_sys->p_input, "length",  &time );
257                         i_seconds = time.i_time / 1000000;
258                         secstotimestr ( psz_total, i_seconds );
259
260                         p_main_interface->statusbar->SetStatusText(
261                             wxU(psz_time) + wxString(wxT(" / ")) +
262                             wxU(psz_total), 0 );
263                     }
264                 }
265             }
266         vlc_mutex_unlock( &p_input->stream.stream_lock );
267 #endif
268             /* Take care of the volume, etc... */
269             p_main_interface->Update();
270
271             /* Manage Playing status */
272             var_Get( p_input, "state", &val );
273             if( i_old_playing_status != val.i_int )
274             {
275                 if( val.i_int == PAUSE_S )
276                 {
277                     p_main_interface->TogglePlayButton( PAUSE_S );
278                 }
279                 else
280                 {
281                     p_main_interface->TogglePlayButton( PLAYING_S );
282                 }
283                 i_old_playing_status = val.i_int;
284             }
285
286             /* Manage Speed status */
287             var_Get( p_input, "rate", &val );
288             if( i_old_rate != val.i_int )
289             {
290                 p_main_interface->statusbar->SetStatusText(
291                     wxString::Format(wxT("x%.2f"),
292                     (float)INPUT_RATE_DEFAULT / val.i_int ), 1 );
293                 i_old_rate = val.i_int;
294             }
295         }
296
297     }
298     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
299     {
300         p_intf->p_sys->b_playing = 0;
301         p_main_interface->TogglePlayButton( PAUSE_S );
302         i_old_playing_status = PAUSE_S;
303     }
304
305     /* Show the interface, if requested */
306     if( p_intf->p_sys->b_intf_show )
307     {
308         p_main_interface->Raise();
309         p_intf->p_sys->b_intf_show = VLC_FALSE;
310     }
311
312     if( p_intf->b_die )
313     {
314         vlc_mutex_unlock( &p_intf->change_lock );
315
316         /* Prepare to die, young Skywalker */
317         p_main_interface->Close(TRUE);
318         return;
319     }
320
321     vlc_mutex_unlock( &p_intf->change_lock );
322 }
323
324 /*****************************************************************************
325  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
326  *  We don't show the menu directly here because we don't want the
327  *  caller to block for a too long time.
328  *****************************************************************************/
329 static 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     intf_thread_t *p_intf = (intf_thread_t *)param;
333
334     if( p_intf->p_sys->pf_show_dialog )
335     {
336         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
337                                        new_val.b_bool, 0 );
338     }
339
340     return VLC_SUCCESS;
341 }
342
343 /*****************************************************************************
344  * IntfShowCB: callback triggered by the intf-show playlist variable.
345  *****************************************************************************/
346 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
347                        vlc_value_t old_val, vlc_value_t new_val, void *param )
348 {
349     intf_thread_t *p_intf = (intf_thread_t *)param;
350     p_intf->p_sys->b_intf_show = VLC_TRUE;
351
352     return VLC_SUCCESS;
353 }