]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/skin_main.cpp
* fixed compilation under linux
[vlc] / 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.20 2003/04/28 12:00:14 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 #ifdef GTK2_SKINS
109     // Initialize GDK
110     int    i_args   = 3;
111     char  *p_args[] = { "", "", "--sync", NULL };
112     char **pp_args  = p_args;
113
114     gdk_init( &i_args, &pp_args );
115 #endif
116
117     // Initialize conditions and mutexes
118     vlc_mutex_init( p_intf, &p_intf->p_sys->init_lock );
119     vlc_cond_init( p_intf, &p_intf->p_sys->init_cond );
120
121     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
122
123     return( 0 );
124 }
125
126 //---------------------------------------------------------------------------
127 // Close: destroy interface
128 //---------------------------------------------------------------------------
129 static void Close ( vlc_object_t *p_this )
130 {
131     intf_thread_t *p_intf = (intf_thread_t *)p_this;
132
133
134     if( p_intf->p_sys->p_input )
135     {
136         vlc_object_release( p_intf->p_sys->p_input );
137     }
138
139     if( p_intf->p_sys->p_playlist )
140     {
141         vlc_object_release( p_intf->p_sys->p_playlist );
142     }
143
144     // Delete theme, it's important to do it correctly
145     delete (OSTheme *)p_intf->p_sys->p_theme;
146
147     // Unsuscribe to messages bank
148     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
149
150     // Destroy conditions and mutexes
151     vlc_cond_destroy( &p_intf->p_sys->init_cond );
152     vlc_mutex_destroy( &p_intf->p_sys->init_lock );
153
154     // Destroy structure
155     free( p_intf->p_sys );
156 }
157
158
159 //---------------------------------------------------------------------------
160 // Run: main loop
161 //---------------------------------------------------------------------------
162 static void Run( intf_thread_t *p_intf )
163 {
164
165     int a = OSAPI_GetTime();
166
167     // Load a theme
168     char *skin_last = config_GetPsz( p_intf, "skin_last" );
169     ThemeLoader *Loader = new ThemeLoader( p_intf );
170
171     if( skin_last == NULL || ! Loader->Load( skin_last ) )
172     {
173         // Too bad, it failed. Let's try with the default theme
174 #if 0
175         if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
176 #else
177 #ifdef WIN32
178         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
179                              DIRECTORY_SEPARATOR + "skins" +
180                              DIRECTORY_SEPARATOR + "default" +
181                              DIRECTORY_SEPARATOR + "theme.xml";
182 #else
183 // FIXME: find VLC directory 
184         string default_dir = (string)"./share" +
185                              DIRECTORY_SEPARATOR + "skins" +
186                              DIRECTORY_SEPARATOR + "default" +
187                              DIRECTORY_SEPARATOR + "theme.xml";
188 #endif
189         if( ! Loader->Load( default_dir ) )
190 #endif
191         {
192             // Last chance: the user can  select a new theme file
193             wxFileDialog dialog( NULL, _("Open a skin file"), "", "",
194                 "Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|"
195                     "All files|*.*", wxOPEN );
196
197             if( dialog.ShowModal() == wxID_OK )
198             {
199                 // try to load selected file
200                 if( ! Loader->Load( dialog.GetPath().c_str() ) )
201                 {
202                     // He, he, what the hell is he doing ?
203                     delete Loader;
204                     return;
205                 }
206             }
207             else
208             {
209                 delete Loader;
210                 return;
211             }
212         }
213     }
214
215     // Show the theme
216     p_intf->p_sys->p_theme->InitTheme();
217     p_intf->p_sys->p_theme->ShowTheme();
218
219     delete Loader;
220
221     msg_Err( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
222
223     // Refresh the whole interface
224     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
225
226     OSRun( p_intf );
227 }
228
229 //---------------------------------------------------------------------------
230 // Module descriptor
231 //---------------------------------------------------------------------------
232 #define DEFAULT_SKIN        N_("Last skin actually used")
233 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
234 #define SKIN_CONFIG         N_("Config of last used skin")
235 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
236 #define SKIN_TRAY           N_("Show application in system tray")
237 #define SKIN_TRAY_LONG      N_("Show application in system tray")
238 #define SKIN_TASKBAR        N_("Show application in taskbar")
239 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
240
241 vlc_module_begin();
242     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
243                 VLC_TRUE );
244     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
245                 VLC_TRUE );
246     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
247               VLC_FALSE );
248     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
249               SKIN_TASKBAR_LONG, VLC_FALSE );
250     set_description( _("Skinnable Interface") );
251     set_capability( "interface", 30 );
252     set_callbacks( Open, Close );
253     add_shortcut( "skins" );
254 vlc_module_end();
255
256
257 //---------------------------------------------------------------------------
258 // Refresh procedure
259 //---------------------------------------------------------------------------
260 int SkinManage( intf_thread_t *p_intf )
261 {
262     vlc_mutex_lock( &p_intf->change_lock );
263
264     // Update the input
265     if( p_intf->p_sys->p_input == NULL )
266     {
267         p_intf->p_sys->p_input = (input_thread_t *)
268                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
269     }
270     else if( p_intf->p_sys->p_input->b_dead )
271     {
272         vlc_object_release( p_intf->p_sys->p_input );
273         p_intf->p_sys->p_input = NULL;
274     }
275
276     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (long)false );
277
278     // Update the log window
279     p_intf->p_sys->MessagesDlg->UpdateLog();
280
281     // Update the file info window
282     p_intf->p_sys->InfoDlg->UpdateFileInfo();
283
284     //-------------------------------------------------------------------------
285     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
286     {
287         input_thread_t  * p_input = p_intf->p_sys->p_input;
288
289         vlc_mutex_lock( &p_input->stream.stream_lock );
290
291         // Refresh sound volume
292         audio_volume_t volume;
293
294         // Get sound volume from VLC
295         aout_VolumeGet( p_intf, &volume);
296
297         // Update sliders
298         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
299             (unsigned int)
300             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
301             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
302
303
304         // Refresh slider
305         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
306         if( p_input->stream.b_seekable )
307         {
308 #define p_area p_input->stream.p_selected_area
309
310             // Set value of sliders
311             long Value = SLIDER_RANGE *
312                 p_input->stream.p_selected_area->i_tell /
313                 p_input->stream.p_selected_area->i_size;
314
315             // Update sliders
316             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
317                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
318
319             // Text char * for updating text controls
320             char *text = new char[OFFSETTOTIME_MAX_SIZE];
321
322             // Create end time text
323             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
324                                 p_area->i_size - p_area->i_tell );
325             text[0] = '-';
326             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
327                 ->PostTextMessage( text );
328
329             // Create time text and update
330             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
331             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
332                 ->PostTextMessage( text );
333
334             // Create total time text
335             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
336             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
337                 ->PostTextMessage( text );
338
339             // Free memory
340             delete[] text;
341
342 #undef p_area
343         }
344         vlc_mutex_unlock( &p_input->stream.stream_lock );
345     }
346     //-------------------------------------------------------------------------
347     vlc_mutex_unlock( &p_intf->change_lock );
348
349     return( VLC_TRUE );
350 }