]> git.sesse.net Git - vlc/blob - modules/gui/wince/timer.cpp
* modules/gui/wince/*: New Windows CE interface by Cedric Marodon <cedric_marodon...
[vlc] / modules / gui / wince / timer.cpp
1 /*****************************************************************************
2  * timer.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <string>
33 #include <stdio.h>
34 using namespace std; 
35
36 #include <vlc/vlc.h>
37 #include <vlc/aout.h>
38 #include <vlc/intf.h>
39
40 #include <commctrl.h>
41
42 #include "wince.h"
43
44 /* Callback prototype */
45 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
46                         vlc_value_t old_val, vlc_value_t new_val, void *param );
47
48 /*****************************************************************************
49  * Constructor.
50  *****************************************************************************/
51 Timer::Timer( intf_thread_t *_p_intf, HWND hwnd, Interface *_p_main_interface)//, Interface *_p_main_interface )
52 {
53     p_intf = _p_intf;
54     p_main_interface = _p_main_interface;
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         vlc_object_release( p_playlist );
66     }
67
68     SetTimer( hwnd, 1, 200 /*milliseconds*/, NULL );
69 }
70
71 Timer::~Timer()
72 {
73     /* Unregister callback */
74     playlist_t *p_playlist =
75         (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
76                                        FIND_ANYWHERE );
77     if( p_playlist != NULL )
78     {
79         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
80         vlc_object_release( p_playlist );
81     }
82 }
83
84 /*****************************************************************************
85  * Private methods.
86  *****************************************************************************/
87
88 /*****************************************************************************
89  * Manage: manage main thread messages
90  *****************************************************************************
91  * In this function, called approx. 10 times a second, we check what the
92  * main program wanted to tell us.
93  *****************************************************************************/
94 void Timer::Notify( void )
95 {
96     int size;
97     vlc_value_t val;
98     char *shortname;
99     LPWSTR wUnicode;
100
101     vlc_mutex_lock( &p_intf->change_lock );
102
103     /* Update the input */
104     if( p_intf->p_sys->p_input == NULL )
105     {
106         p_intf->p_sys->p_input =
107             (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
108                                                FIND_ANYWHERE );
109
110         /* Show slider */
111         if( p_intf->p_sys->p_input )
112         {
113             ShowWindow( p_main_interface->hwndSlider, SW_SHOW);
114             ShowWindow( p_main_interface->hwndLabel, SW_SHOW);
115             ShowWindow( p_main_interface->hwndVol, SW_SHOW);
116
117             // only for local file, check if works well with net url
118             shortname = strrchr( p_intf->p_sys->p_input->input.p_item->psz_name, '\\' );
119             if (! shortname)
120                 shortname = p_intf->p_sys->p_input->input.p_item->psz_name;
121             else
122                 shortname++;
123                         
124             size = MultiByteToWideChar (CP_ACP, 0, shortname, -1, NULL, 0);
125             wUnicode = new WCHAR[size+1];
126             MultiByteToWideChar (CP_ACP, 0, shortname, -1, wUnicode + 1, size) ;
127             wUnicode[0] = L'\t';
128
129             SendMessage( p_main_interface->hwndSB, SB_SETTEXT, 
130                          (WPARAM) 0, (LPARAM)(LPCTSTR) wUnicode);
131             free( wUnicode );
132
133             p_main_interface->TogglePlayButton( PLAYING_S );
134             i_old_playing_status = PLAYING_S;
135
136             /* Take care of the volume */
137             audio_volume_t i_volume;
138             aout_VolumeGet( p_intf, &i_volume );                        
139             SendMessage( p_main_interface->hwndVol, TBM_SETPOS, 1,
140                          200 - (i_volume * 200 * 2 / AOUT_VOLUME_MAX ) );
141         }
142     }
143     else if( p_intf->p_sys->p_input->b_dead )
144     {
145         /* Hide slider */
146         ShowWindow( p_main_interface->hwndSlider, SW_HIDE);
147         ShowWindow( p_main_interface->hwndLabel, SW_HIDE);
148         ShowWindow( p_main_interface->hwndVol, SW_HIDE);
149
150         p_main_interface->TogglePlayButton( PAUSE_S );
151         i_old_playing_status = PAUSE_S;
152
153         SendMessage( p_main_interface->hwndSB, SB_SETTEXT, 
154                      (WPARAM) 0, (LPARAM)(LPCTSTR) TEXT(""));
155
156         vlc_object_release( p_intf->p_sys->p_input );
157         p_intf->p_sys->p_input = NULL;
158     }
159
160     if( p_intf->p_sys->p_input )
161     {
162         input_thread_t *p_input = p_intf->p_sys->p_input;
163
164         if( !p_input->b_die )
165         {
166             /* New input or stream map change */
167             p_intf->p_sys->b_playing = 1;
168
169             /* Manage the slider */
170             if( /*p_input->stream.b_seekable &&*/ p_intf->p_sys->b_playing )
171             {
172                 /* Update the slider if the user isn't dragging it. */
173                 if( p_intf->p_sys->b_slider_free )
174                 {
175                     vlc_value_t pos;
176                     char psz_time[ MSTRTIME_MAX_SIZE ];
177                     vlc_value_t time;
178                     mtime_t i_seconds;
179
180                     /* Update the value */
181                     var_Get( p_input, "position", &pos );
182                     if( pos.f_float >= 0.0 )
183                     {
184                         p_intf->p_sys->i_slider_pos =
185                             (int)(SLIDER_MAX_POS * pos.f_float);
186
187                         SendMessage( p_main_interface->hwndSlider,TBM_SETPOS, 
188                                      1, p_intf->p_sys->i_slider_pos );
189
190                         var_Get( p_intf->p_sys->p_input, "time", &time );
191                         i_seconds = time.i_time / 1000000;
192
193                         secstotimestr ( psz_time, i_seconds );
194
195                         size = MultiByteToWideChar (CP_ACP, 0, psz_time, -1, NULL, 0);
196                         wUnicode = new WCHAR[size];
197                         MultiByteToWideChar (CP_ACP, 0, psz_time, -1, wUnicode, size) ;
198                         SendMessage( p_main_interface->hwndLabel, WM_SETTEXT, 
199                                      (WPARAM) 1, (LPARAM)(LPCTSTR) wUnicode );
200                         free( wUnicode );
201                     }
202                 }
203             }
204
205             /* Manage Playing status */
206             var_Get( p_input, "state", &val );
207             if( i_old_playing_status != val.i_int )
208             {
209                 if( val.i_int == PAUSE_S )
210                 {
211                     p_main_interface->TogglePlayButton( PAUSE_S );
212                 }
213                 else
214                 {
215                     p_main_interface->TogglePlayButton( PLAYING_S );
216                 }
217                 i_old_playing_status = val.i_int;
218             }
219
220             /* Manage Speed status */
221             var_Get( p_input, "rate", &val );
222             if( i_old_rate != val.i_int )
223             {
224                 wUnicode = new WCHAR[10];
225                 swprintf( wUnicode + 2, TEXT("x%.2f"), 1000.0 / val.i_int );
226                 wUnicode[0] = L'\t';
227                 wUnicode[1] = L'\t';
228
229                 SendMessage( p_main_interface->hwndSB, SB_SETTEXT, 
230                              (WPARAM) 1, (LPARAM)(LPCTSTR) wUnicode);
231
232                 free(wUnicode); 
233
234                 i_old_rate = val.i_int;
235             }
236         }
237     }
238     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
239     {
240         p_intf->p_sys->b_playing = 0;
241         p_main_interface->TogglePlayButton( PAUSE_S );
242         i_old_playing_status = PAUSE_S;
243     }
244
245     if( p_intf->b_die )
246     {
247         vlc_mutex_unlock( &p_intf->change_lock );
248
249         /* Prepare to die, young Skywalker */
250 /*        p_main_interface->Close(TRUE);*/
251         return;
252     }
253
254     vlc_mutex_unlock( &p_intf->change_lock );
255 }
256
257 /*****************************************************************************
258  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
259  *  We don't show the menu directly here because we don't want the
260  *  caller to block for a too long time.
261  *****************************************************************************/
262 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
263                         vlc_value_t old_val, vlc_value_t new_val, void *param )
264 {
265     intf_thread_t *p_intf = (intf_thread_t *)param;
266
267     if( p_intf->p_sys->pf_show_dialog )
268     {
269         p_intf->p_sys->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
270                                        new_val.b_bool, 0 );
271     }
272
273     return VLC_SUCCESS;
274 }