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