969a55c5ab0caca872bd632b2254db14b85cdb1b
[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.9 2003/01/26 10:36:10 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 /*****************************************************************************
53  * Constructor.
54  *****************************************************************************/
55 Timer::Timer( intf_thread_t *_p_intf, Interface *_p_main_interface )
56 {
57     p_intf = _p_intf;
58     p_main_interface = _p_main_interface;
59
60     Start( 100 /*milliseconds*/, wxTIMER_CONTINUOUS );
61 }
62
63 Timer::~Timer()
64 {
65 }
66
67 /*****************************************************************************
68  * Private methods.
69  *****************************************************************************/
70 /*****************************************************************************
71  * wxModeManage: actualise the aspect of the interface whenever the input
72  * changes.
73  *****************************************************************************
74  * The lock has to be taken before you call the function.
75  *****************************************************************************/
76 static int wxModeManage( intf_thread_t * p_intf )
77 {
78     return 0;
79 }
80
81 /*****************************************************************************
82  * wxSetupMenus: function that generates title/chapter/audio/subpic
83  * menus with help from preceding functions
84  *****************************************************************************
85  * Function called with the lock on stream
86  *****************************************************************************/
87 static int wxSetupMenus( intf_thread_t * p_intf )
88 {
89     return 0;
90 }
91
92 /*****************************************************************************
93  * Manage: manage main thread messages
94  *****************************************************************************
95  * In this function, called approx. 10 times a second, we check what the
96  * main program wanted to tell us.
97  *****************************************************************************/
98 void Timer::Notify()
99 {
100     int i_stop;
101
102     vlc_mutex_lock( &p_intf->change_lock );
103
104     /* If the "display popup" flag has changed */
105     if( p_intf->b_menu_change )
106     {
107         p_main_interface->p_popup_menu =
108             new PopupMenu( p_intf, p_main_interface );
109         p_intf->b_menu_change = 0;
110     }
111
112     /* Update the log window */
113     p_intf->p_sys->p_messages_window->UpdateLog();
114
115     /* Update the playlist */
116     p_intf->p_sys->p_playlist_window->Manage();
117
118     /* Update the input */
119     if( p_intf->p_sys->p_input == NULL )
120     {
121         p_intf->p_sys->p_input = (input_thread_t *)vlc_object_find( p_intf,
122                                                        VLC_OBJECT_INPUT,
123                                                        FIND_ANYWHERE );
124         /* Show slider */
125         if(p_intf->p_sys->p_input)
126         {
127             p_main_interface->frame_sizer->Add(
128                 p_main_interface->slider_frame, 1, wxGROW, 0 );
129             p_main_interface->slider_frame->Show();
130             p_main_interface->frame_sizer->Layout();
131             p_main_interface->frame_sizer->Fit( p_main_interface );
132             p_main_interface->statusbar->SetStatusText(
133                 p_intf->p_sys->p_input->psz_source, 1 );
134         }
135     }
136     else if( p_intf->p_sys->p_input->b_dead )
137     {
138         /* Hide slider */
139         if(p_intf->p_sys->p_input)
140         {
141             p_main_interface->slider_frame->Hide();
142             p_main_interface->frame_sizer->Remove(
143                 p_main_interface->slider_frame );
144             p_main_interface->frame_sizer->Layout();
145             p_main_interface->frame_sizer->Fit( p_main_interface );
146         }
147
148         p_main_interface->statusbar->SetStatusText( "", 1 );
149
150         vlc_object_release( p_intf->p_sys->p_input );
151         p_intf->p_sys->p_input = NULL;
152     }
153
154     if( p_intf->p_sys->p_input )
155     {
156         input_thread_t *p_input = p_intf->p_sys->p_input;
157
158         vlc_mutex_lock( &p_input->stream.stream_lock );
159
160         if( !p_input->b_die )
161         {
162             /* New input or stream map change */
163             if( p_input->stream.b_changed )
164             {
165                 wxModeManage( p_intf );
166                 wxSetupMenus( p_intf );
167                 p_intf->p_sys->b_playing = 1;
168             }
169
170             /* Manage the slider */
171             if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
172             {
173
174                 stream_position_t position;
175
176                 /* Update the slider if the user isn't dragging it. */
177                 if( p_intf->p_sys->b_slider_free )
178                 {
179                     /* Update the value */
180                     vlc_mutex_unlock( &p_input->stream.stream_lock );
181                     input_Tell( p_input, &position );
182                     vlc_mutex_lock( &p_input->stream.stream_lock );
183                     p_intf->p_sys->i_slider_pos =
184                         ( SLIDER_MAX_POS * position.i_tell ) / position.i_size;
185
186                     p_main_interface->slider->SetValue(
187                         p_intf->p_sys->i_slider_pos );
188
189                     DisplayStreamDate( p_main_interface->slider_box,
190                                        p_intf,
191                                        p_intf->p_sys->i_slider_pos );
192                 }
193             }
194
195             if( p_intf->p_sys->i_part !=
196                 p_input->stream.p_selected_area->i_part )
197             {
198                 p_intf->p_sys->b_chapter_update = 1;
199                 wxSetupMenus( p_intf );
200             }
201         }
202
203         vlc_mutex_unlock( &p_input->stream.stream_lock );
204     }
205     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
206     {
207         wxModeManage( p_intf );
208         p_intf->p_sys->b_playing = 0;
209     }
210
211     if( p_intf->b_die )
212     {
213         vlc_mutex_unlock( &p_intf->change_lock );
214
215         /* Prepare to die, young Skywalker */
216         p_main_interface->Close(TRUE);
217         return;
218     }
219
220     vlc_mutex_unlock( &p_intf->change_lock );
221 }
222
223 /*****************************************************************************
224  * DisplayStreamDate: display stream date
225  *****************************************************************************
226  * This function displays the current date related to the position in
227  * the stream. It is called whenever the slider changes its value.
228  * The lock has to be taken before you call the function.
229  *****************************************************************************/
230 void DisplayStreamDate( wxControl *p_slider_frame, intf_thread_t * p_intf ,
231                         int i_pos )
232 {
233     if( p_intf->p_sys->p_input )
234     {
235 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
236         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
237
238         p_slider_frame->SetLabel(
239             input_OffsetToTime( p_intf->p_sys->p_input,
240                                 psz_time,
241                                 p_area->i_size * i_pos / SLIDER_MAX_POS ) );
242 #undef p_area
243      }
244 }