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