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