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