]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/timer.cpp
* modules/gui/wxwindows/*: some cleanup + disable seekbar if stream is not seekable.
[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     i_old_playing_status = PAUSE_S;
53     i_old_rate = DEFAULT_RATE;
54
55     /* Register callback for the intf-popupmenu variable */
56     playlist_t *p_playlist =
57         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
58                                        FIND_ANYWHERE );
59     if( p_playlist != NULL )
60     {
61         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
62         vlc_object_release( p_playlist );
63     }
64
65     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
66 }
67
68 Timer::~Timer()
69 {
70     /* Unregister callback */
71     playlist_t *p_playlist =
72         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
73                                        FIND_ANYWHERE );
74     if( p_playlist != NULL )
75     {
76         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
77         vlc_object_release( p_playlist );
78     }
79 }
80
81 /*****************************************************************************
82  * Private methods.
83  *****************************************************************************/
84
85 /*****************************************************************************
86  * Manage: manage main thread messages
87  *****************************************************************************
88  * In this function, called approx. 10 times a second, we check what the
89  * main program wanted to tell us.
90  *****************************************************************************/
91 void Timer::Notify()
92 {
93     vlc_bool_t b_pace_control;
94
95     vlc_mutex_lock( &p_intf->change_lock );
96
97     /* Update the input */
98     if( p_intf->p_sys->p_input == NULL )
99     {
100         p_intf->p_sys->p_input =
101             (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
102                                                FIND_ANYWHERE );
103
104         /* Show slider */
105         if( p_intf->p_sys->p_input )
106         {
107             p_main_interface->slider->SetValue( 0 );
108             p_main_interface->slider_frame->Show();
109             p_main_interface->frame_sizer->Show(
110                 p_main_interface->slider_frame );
111             p_main_interface->frame_sizer->Layout();
112             p_main_interface->frame_sizer->Fit( p_main_interface );
113
114             if( p_intf->p_sys->p_input->stream.b_seekable )
115                 p_main_interface->slider->Enable();
116             else
117                 p_main_interface->slider->Disable();
118
119             p_main_interface->statusbar->SetStatusText(
120                 wxU(p_intf->p_sys->p_input->psz_source), 2 );
121
122             p_main_interface->TogglePlayButton( PLAYING_S );
123             i_old_playing_status = PLAYING_S;
124
125             /* control buttons for free pace streams */
126             b_pace_control = p_intf->p_sys->p_input->stream.b_pace_control;
127         }
128     }
129     else if( p_intf->p_sys->p_input->b_dead )
130     {
131         /* Hide slider */
132         p_main_interface->slider_frame->Hide();
133         p_main_interface->frame_sizer->Hide(
134             p_main_interface->slider_frame );
135         p_main_interface->frame_sizer->Layout();
136         p_main_interface->frame_sizer->Fit( p_main_interface );
137
138         p_main_interface->TogglePlayButton( PAUSE_S );
139         i_old_playing_status = PAUSE_S;
140
141         p_main_interface->statusbar->SetStatusText( wxT(""), 0 );
142         p_main_interface->statusbar->SetStatusText( wxT(""), 2 );
143
144         vlc_object_release( p_intf->p_sys->p_input );
145         p_intf->p_sys->p_input = NULL;
146     }
147
148
149
150     if( p_intf->p_sys->p_input )
151     {
152         input_thread_t *p_input = p_intf->p_sys->p_input;
153
154         vlc_mutex_lock( &p_input->stream.stream_lock );
155
156         if( !p_input->b_die )
157         {
158             /* New input or stream map change */
159             p_intf->p_sys->b_playing = 1;
160
161             /* Manage the slider */
162             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
163             {
164                 /* Update the slider if the user isn't dragging it. */
165                 if( p_intf->p_sys->b_slider_free )
166                 {
167                     vlc_value_t pos;
168                     char psz_time[ MSTRTIME_MAX_SIZE ];
169                     char psz_total[ MSTRTIME_MAX_SIZE ];
170                     vlc_value_t time;
171                     mtime_t i_seconds;
172
173                     /* Update the value */
174                     var_Get( p_input, "position", &pos );
175                     if( pos.f_float >= 0.0 )
176                     {
177                         p_intf->p_sys->i_slider_pos =
178                             (int)(SLIDER_MAX_POS * pos.f_float);
179
180                         p_main_interface->slider->SetValue(
181                             p_intf->p_sys->i_slider_pos );
182
183                         var_Get( p_intf->p_sys->p_input, "time", &time );
184                         i_seconds = time.i_time / 1000000;
185                         secstotimestr ( psz_time, i_seconds );
186
187                         var_Get( p_intf->p_sys->p_input, "length",  &time );
188                         i_seconds = time.i_time / 1000000;
189                         secstotimestr ( psz_total, i_seconds );
190
191                         p_main_interface->statusbar->SetStatusText(
192                             wxU(psz_time) + wxString(wxT(" / ")) +
193                             wxU(psz_total), 0 );
194                     }
195                 }
196             }
197
198             /* Take care of the volume, etc... */
199             p_main_interface->Update();
200
201             /* Manage Playing status */
202             if( i_old_playing_status != p_input->stream.control.i_status )
203             {
204                 if( p_input->stream.control.i_status == PAUSE_S )
205                 {
206                     p_main_interface->TogglePlayButton( PAUSE_S );
207                 }
208                 else
209                 {
210                     p_main_interface->TogglePlayButton( PLAYING_S );
211                 }
212                 i_old_playing_status = p_input->stream.control.i_status;
213             }
214
215             /* Manage Speed status */
216             if( i_old_rate != p_input->stream.control.i_rate )
217             {
218                 p_main_interface->statusbar->SetStatusText(
219                     wxString::Format(wxT("x%.2f"),
220                     1000.0 / p_input->stream.control.i_rate), 1 );
221                 i_old_rate = p_input->stream.control.i_rate;
222             }
223         }
224
225         vlc_mutex_unlock( &p_input->stream.stream_lock );
226     }
227     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
228     {
229         p_intf->p_sys->b_playing = 0;
230         p_main_interface->TogglePlayButton( PAUSE_S );
231         i_old_playing_status = PAUSE_S;
232     }
233
234     if( p_intf->b_die )
235     {
236         vlc_mutex_unlock( &p_intf->change_lock );
237
238         /* Prepare to die, young Skywalker */
239         p_main_interface->Close(TRUE);
240         return;
241     }
242
243     vlc_mutex_unlock( &p_intf->change_lock );
244 }
245
246 /*****************************************************************************
247  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
248  *  We don't show the menu directly here because we don't want the
249  *  caller to block for a too long time.
250  *****************************************************************************/
251 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
252                         vlc_value_t old_val, vlc_value_t new_val, void *param )
253 {
254     intf_thread_t *p_intf = (intf_thread_t *)param;
255
256     if( p_intf->p_sys->pf_show_dialog )
257     {
258         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
259                                        new_val.b_bool, 0 );
260     }
261
262     return VLC_SUCCESS;
263 }