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