* fixed a timing issue under windows ?
[vlc.git] / modules / gui / skins / src / skin_main.cpp
1 /*****************************************************************************
2  * skin-main.cpp: skins plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: skin_main.cpp,v 1.15 2003/04/21 02:50:49 asmax Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
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,
23  * USA.
24  *****************************************************************************/
25
26 //--- GENERAL ---------------------------------------------------------------
27 #include <wx/wx.h>
28
29 //--- VLC -------------------------------------------------------------------
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc/aout.h>
33
34 //--- SKIN ------------------------------------------------------------------
35 #include "../os_api.h"
36 #include "event.h"
37 #include "banks.h"
38 #include "window.h"
39 #include "theme.h"
40 #include "../os_theme.h"
41 #include "themeloader.h"
42 #include "vlcproc.h"
43 #include "skin_common.h"
44 #include "wxdialogs.h"
45
46
47 //---------------------------------------------------------------------------
48 // Interface thread
49 // It is a global variable because we have C code for the parser, and we
50 // need to access C++ objects from there
51 //---------------------------------------------------------------------------
52 intf_thread_t *g_pIntf;
53
54 //---------------------------------------------------------------------------
55 // Exported interface functions.
56 //---------------------------------------------------------------------------
57 #ifdef WIN32
58 extern "C" __declspec( dllexport )
59     int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
60 #endif
61
62 //---------------------------------------------------------------------------
63 // Local prototypes.
64 //---------------------------------------------------------------------------
65 static int  Open   ( vlc_object_t * );
66 static void Close  ( vlc_object_t * );
67 static void Run    ( intf_thread_t * );
68
69 int  SkinManage( intf_thread_t *p_intf );
70 void OSRun( intf_thread_t *p_intf );
71
72 //---------------------------------------------------------------------------
73 // Open: initialize interface
74 //---------------------------------------------------------------------------
75 static int Open ( vlc_object_t *p_this )
76 {
77     intf_thread_t *p_intf = (intf_thread_t *)p_this;
78     g_pIntf = p_intf;
79
80     // Allocate instance and initialize some members
81     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
82     if( p_intf->p_sys == NULL )
83     {
84         msg_Err( p_intf, "out of memory" );
85         return( 1 );
86     };
87
88     p_intf->pf_run = Run;
89
90
91     // Suscribe to messages bank
92     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
93
94     // Set no new theme when opening file
95     p_intf->p_sys->p_new_theme_file = NULL;
96
97     // Initialize Win32 thread
98     p_intf->p_sys->i_index        = -1;
99     p_intf->p_sys->i_size         = 0;
100
101
102     p_intf->p_sys->i_close_status = VLC_NOTHING;
103
104     p_intf->p_sys->p_input = NULL;
105     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
106         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
107
108     // Initialize conditions
109     vlc_mutex_init( p_intf, &p_intf->p_sys->init_lock);
110     vlc_cond_init( p_intf, &p_intf->p_sys->init_cond);
111     
112     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
113
114     return( 0 );
115 }
116
117 //---------------------------------------------------------------------------
118 // Close: destroy interface
119 //---------------------------------------------------------------------------
120 static void Close ( vlc_object_t *p_this )
121 {
122     intf_thread_t *p_intf = (intf_thread_t *)p_this;
123
124
125     if( p_intf->p_sys->p_input )
126     {
127         vlc_object_release( p_intf->p_sys->p_input );
128     }
129
130     if( p_intf->p_sys->p_playlist )
131     {
132         vlc_object_release( p_intf->p_sys->p_playlist );
133     }
134
135     // Delete theme, it's important to do it correctly
136     delete (OSTheme *)p_intf->p_sys->p_theme;
137
138     // Unsuscribe to messages bank
139     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
140
141     // Destroy conditions
142     vlc_cond_destroy( &p_intf->p_sys->init_cond);
143     vlc_mutex_destroy( &p_intf->p_sys->init_lock);
144     
145     // Destroy structure
146     free( p_intf->p_sys );
147 }
148
149
150 //---------------------------------------------------------------------------
151 // Run: main loop
152 //---------------------------------------------------------------------------
153 static void Run( intf_thread_t *p_intf )
154 {
155
156 #if !defined WIN32
157 /* FIXME: should be elsewhere ? */
158     // Initialize GDK
159     int    i_args   = 1;
160     char  *p_args[] = { "", NULL };
161     char **pp_args  = p_args;
162
163     gdk_init( &i_args, &pp_args );
164 #endif
165
166     int a = OSAPI_GetTime();
167
168     // Load a theme
169     char *skin_last = config_GetPsz( p_intf, "skin_last" );
170     ThemeLoader *Loader = new ThemeLoader( p_intf );
171
172     if( skin_last == NULL || ! Loader->Load( skin_last ) )
173     {
174         // Too bad, it failed. Let's try with the default theme
175 #if 0
176         if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
177 #else
178 #ifdef WIN32
179         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
180                              DIRECTORY_SEPARATOR + "skins" +
181                              DIRECTORY_SEPARATOR + "default" +
182                              DIRECTORY_SEPARATOR + "theme.xml";
183 #else
184 // FIXME: find VLC directory 
185         string default_dir = (string)"./share" +
186                              DIRECTORY_SEPARATOR + "skins" +
187                              DIRECTORY_SEPARATOR + "default" +
188                              DIRECTORY_SEPARATOR + "theme.xml";
189 #endif
190         if( ! Loader->Load( default_dir ) )
191 #endif
192         {
193             // Last chance: the user can  select a new theme file
194             wxFileDialog dialog( NULL, _("Open a skin file"), "", "",
195                 "Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|"
196                     "All files|*.*", wxOPEN );
197
198             if( dialog.ShowModal() == wxID_OK )
199             {
200                 // try to load selected file
201                 if( ! Loader->Load( dialog.GetPath().c_str() ) )
202                 {
203                     // He, he, what the hell is he doing ?
204                     delete Loader;
205                     return;
206                 }
207             }
208             else
209             {
210                 delete Loader;
211                 return;
212             }
213         }
214     }
215
216     // Show the theme
217     p_intf->p_sys->p_theme->InitTheme();
218     p_intf->p_sys->p_theme->ShowTheme();
219
220     delete Loader;
221
222     msg_Err( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
223
224     // Refresh the whole interface
225     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
226
227     OSRun( p_intf );
228 }
229
230 //---------------------------------------------------------------------------
231 // Module descriptor
232 //---------------------------------------------------------------------------
233 #define DEFAULT_SKIN        N_("Last skin actually used")
234 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
235 #define SKIN_CONFIG         N_("Config of last used skin")
236 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
237 #define SKIN_TRAY           N_("Show application in system tray")
238 #define SKIN_TRAY_LONG      N_("Show application in system tray")
239 #define SKIN_TASKBAR        N_("Show application in taskbar")
240 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
241
242 vlc_module_begin();
243     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
244                 VLC_TRUE );
245     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
246                 VLC_TRUE );
247     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
248               VLC_FALSE );
249     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
250               SKIN_TASKBAR_LONG, VLC_FALSE );
251     set_description( _("Skinnable Interface") );
252     set_capability( "interface", 30 );
253     set_callbacks( Open, Close );
254     add_shortcut( "skins" );
255 vlc_module_end();
256
257
258 //---------------------------------------------------------------------------
259 // Refresh procedure
260 //---------------------------------------------------------------------------
261 int SkinManage( intf_thread_t *p_intf )
262 {
263     vlc_mutex_lock( &p_intf->change_lock );
264
265     // Update the input
266     if( p_intf->p_sys->p_input == NULL )
267     {
268         p_intf->p_sys->p_input = (input_thread_t *)
269                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
270     }
271     else if( p_intf->p_sys->p_input->b_dead )
272     {
273         vlc_object_release( p_intf->p_sys->p_input );
274         p_intf->p_sys->p_input = NULL;
275     }
276
277     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (long)false );
278
279     // Update the log window
280     p_intf->p_sys->MessagesDlg->UpdateLog();
281
282     // Update the file info window
283     p_intf->p_sys->InfoDlg->UpdateFileInfo();
284
285     //-------------------------------------------------------------------------
286     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
287     {
288         input_thread_t  * p_input = p_intf->p_sys->p_input;
289
290         vlc_mutex_lock( &p_input->stream.stream_lock );
291
292         // Refresh sound volume
293         audio_volume_t volume;
294
295         // Get sound volume from VLC
296         aout_VolumeGet( p_intf, &volume);
297
298         // Update sliders
299         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
300             (unsigned int)
301             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
302             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
303
304
305         // Refresh slider
306        // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
307         if( p_input->stream.b_seekable )
308         {
309 #define p_area p_input->stream.p_selected_area
310
311             // Set value of sliders
312             long Value = SLIDER_RANGE *
313                 p_input->stream.p_selected_area->i_tell /
314                 p_input->stream.p_selected_area->i_size;
315
316             // Update sliders
317             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
318                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
319
320             // Text char * for updating text controls
321             char *text = new char[OFFSETTOTIME_MAX_SIZE];
322
323             // Create end time text
324             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
325                                 p_area->i_size - p_area->i_tell );
326             text[0] = '-';
327             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
328                 ->PostTextMessage( text );
329
330             // Create time text and update
331             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
332             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
333                 ->PostTextMessage( text );
334
335             // Create total time text
336             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
337             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
338                 ->PostTextMessage( text );
339
340             // Free memory
341             delete[] text;
342
343 #undef p_area
344         }
345         vlc_mutex_unlock( &p_input->stream.stream_lock );
346     }
347     //-------------------------------------------------------------------------
348     vlc_mutex_unlock( &p_intf->change_lock );
349
350     return( VLC_TRUE );
351 }