* modules/gui/skins/*: fix for msvc .dsp generation, replaced the BASIC_SKINS macro...
[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.37 2003/06/11 10:42:34 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 #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
135 #elif defined WIN32
136     // Interface thread id used to post broadcast messages
137     p_intf->p_sys->dwThreadId = GetCurrentThreadId();
138
139     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
140     p_intf->p_sys->h_msimg32_dll = LoadLibrary("msimg32.dll");
141     if( !p_intf->p_sys->h_msimg32_dll ||
142         !( p_intf->p_sys->TransparentBlt =
143            (BOOL (WINAPI*)(HDC,int,int,int,int,HDC,
144                            int,int,int,int,unsigned int))
145            GetProcAddress( p_intf->p_sys->h_msimg32_dll, "TransparentBlt" ) ) )
146     {
147         p_intf->p_sys->TransparentBlt = NULL;
148         msg_Dbg( p_intf, "Couldn't find TransparentBlt(), "
149                  "falling back to BitBlt()" );
150     }
151
152     // idem for user32.dll and SetLayeredWindowAttributes()
153     p_intf->p_sys->h_user32_dll = LoadLibrary("user32.dll");
154     if( !p_intf->p_sys->h_user32_dll ||
155         !( p_intf->p_sys->SetLayeredWindowAttributes =
156            (BOOL (WINAPI *)(HWND,COLORREF,BYTE,DWORD))
157            GetProcAddress( p_intf->p_sys->h_user32_dll,
158                            "SetLayeredWindowAttributes" ) ) )
159     {
160         p_intf->p_sys->SetLayeredWindowAttributes = NULL;
161         msg_Dbg( p_intf, "Couldn't find SetLayeredWindowAttributes()" );
162     }
163
164 #endif
165
166     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
167
168     return( 0 );
169 }
170
171 //---------------------------------------------------------------------------
172 // Close: destroy interface
173 //---------------------------------------------------------------------------
174 static void Close ( vlc_object_t *p_this )
175 {
176     intf_thread_t *p_intf = (intf_thread_t *)p_this;
177
178     if( p_intf->p_sys->p_input )
179     {
180         vlc_object_release( p_intf->p_sys->p_input );
181     }
182
183     if( p_intf->p_sys->p_playlist )
184     {
185         vlc_object_release( p_intf->p_sys->p_playlist );
186     }
187
188     // Delete theme, it's important to do it correctly
189     delete (OSTheme *)p_intf->p_sys->p_theme;
190
191 #if defined X11_SKINS
192     XCloseDisplay( p_intf->p_sys->display );
193 #endif
194
195     // Unsuscribe to messages bank
196     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
197
198 #ifdef WIN32
199     // Unload msimg32.dll and user32.dll
200     if( p_intf->p_sys->h_msimg32_dll )
201         FreeLibrary( p_intf->p_sys->h_msimg32_dll );
202     if( p_intf->p_sys->h_user32_dll )
203         FreeLibrary( p_intf->p_sys->h_user32_dll );
204 #elif defined X11_SKINS
205     vlc_mutex_destroy( &p_intf->p_sys->xlock );
206 #endif
207
208     // Destroy structure
209     free( p_intf->p_sys );
210 }
211
212
213 //---------------------------------------------------------------------------
214 // Run: main loop
215 //---------------------------------------------------------------------------
216 static void Run( intf_thread_t *p_intf )
217 {
218
219     int a = OSAPI_GetTime();
220
221 #if !defined(MODULE_NAME_IS_basic_skins)
222     // Initialize the dialog boxes
223     p_intf->p_sys->p_dialogs = new Dialogs( p_intf );
224     if( !p_intf->p_sys->p_dialogs ||
225         !p_intf->p_sys->p_dialogs->OpenDlg ) return;
226 #endif
227
228     // Load a theme
229     char *skin_last = config_GetPsz( p_intf, "skin_last" );
230     ThemeLoader *Loader = new ThemeLoader( p_intf );
231
232     if( skin_last == NULL || ! Loader->Load( skin_last ) )
233     {
234         // Too bad, it failed. Let's try with the default theme
235 //        if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
236 #ifdef WIN32
237         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
238                              DIRECTORY_SEPARATOR + "skins" +
239                              DIRECTORY_SEPARATOR + "default" +
240                              DIRECTORY_SEPARATOR + "theme.xml";
241         if( ! Loader->Load( default_dir ) )
242         {
243             // Last chance: the user can  select a new theme file
244 #else
245         string user_skin = (string)p_intf->p_vlc->psz_homedir +
246                               DIRECTORY_SEPARATOR + CONFIG_DIR +
247                               DIRECTORY_SEPARATOR + "skins" +
248                               DIRECTORY_SEPARATOR + "default" +
249                               DIRECTORY_SEPARATOR + "theme.xml";
250
251         string default_skin = (string)DATA_PATH +
252                               DIRECTORY_SEPARATOR + "skins" +
253                               DIRECTORY_SEPARATOR + "default" +
254                               DIRECTORY_SEPARATOR + "theme.xml";
255         if( !Loader->Load( user_skin ) && !Loader->Load( default_skin ) )
256         {
257 #endif
258 #if !defined(MODULE_NAME_IS_basic_skins)
259             wxMutexGuiEnter();
260             wxFileDialog dialog( NULL,
261                 wxU(_("Open a skin file")), wxT(""), wxT(""),
262                 wxT("Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|"
263                     "All files|*.*"), wxOPEN );
264
265             if( dialog.ShowModal() == wxID_OK )
266             {
267                 // try to load selected file
268                 if( ! Loader->Load( (string)dialog.GetPath().mb_str() ) )
269                 {
270                     // He, he, what the hell is he doing ?
271                     delete Loader;
272                     wxMutexGuiLeave();
273                     return;
274                 }
275                 wxMutexGuiLeave();
276             }
277             else
278 #endif
279             {
280                 delete Loader;
281 #if !defined(MODULE_NAME_IS_basic_skins)
282                 wxMutexGuiLeave();
283 #endif
284                 return;
285             }
286         }
287     }
288
289     // Show the theme
290     p_intf->p_sys->p_theme->InitTheme();
291     p_intf->p_sys->p_theme->ShowTheme();
292
293     delete Loader;
294
295     msg_Dbg( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
296
297     // Refresh the whole interface
298     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
299
300     OSRun( p_intf );
301
302 #if !defined(MODULE_NAME_IS_basic_skins)
303     // clean up the dialog boxes
304     delete p_intf->p_sys->p_dialogs;
305 #endif
306 }
307
308 //---------------------------------------------------------------------------
309 // Module descriptor
310 //---------------------------------------------------------------------------
311 #define DEFAULT_SKIN        N_("Last skin actually used")
312 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
313 #define SKIN_CONFIG         N_("Config of last used skin")
314 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
315 #define SKIN_TRAY           N_("Show application in system tray")
316 #define SKIN_TRAY_LONG      N_("Show application in system tray")
317 #define SKIN_TASKBAR        N_("Show application in taskbar")
318 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
319
320 vlc_module_begin();
321     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
322                 VLC_TRUE );
323     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
324                 VLC_TRUE );
325     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
326               VLC_FALSE );
327     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
328               SKIN_TASKBAR_LONG, VLC_FALSE );
329     set_description( _("Skinnable Interface") );
330     set_capability( "interface", 30 );
331     set_callbacks( Open, Close );
332 vlc_module_end();
333
334
335 //---------------------------------------------------------------------------
336 // Refresh procedure
337 //---------------------------------------------------------------------------
338 int SkinManage( intf_thread_t *p_intf )
339 {
340     vlc_mutex_lock( &p_intf->change_lock );
341
342     // Update the input
343     if( p_intf->p_sys->p_input == NULL )
344     {
345         p_intf->p_sys->p_input = (input_thread_t *)
346                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
347     }
348     else if( p_intf->p_sys->p_input->b_dead )
349     {
350         vlc_object_release( p_intf->p_sys->p_input );
351         p_intf->p_sys->p_input = NULL;
352     }
353
354     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (long)false );
355
356 #if !defined(MODULE_NAME_IS_basic_skins) //FIXME
357     // Update the log window
358     p_intf->p_sys->p_dialogs->MessagesDlg->UpdateLog();
359
360     // Update the file info window
361     p_intf->p_sys->p_dialogs->FileInfoDlg->UpdateFileInfo();
362 #endif
363
364     //-------------------------------------------------------------------------
365     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
366     {
367         input_thread_t  * p_input = p_intf->p_sys->p_input;
368
369         vlc_mutex_lock( &p_input->stream.stream_lock );
370
371         // Refresh sound volume
372         audio_volume_t volume;
373
374         // Get sound volume from VLC
375         aout_VolumeGet( p_intf, &volume);
376
377         // Update sliders
378         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
379             (unsigned int)
380             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
381             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
382
383
384         // Refresh slider
385         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
386         if( p_input->stream.b_seekable )
387         {
388 #define p_area p_input->stream.p_selected_area
389
390             // Set value of sliders
391             long Value = SLIDER_RANGE *
392                 p_input->stream.p_selected_area->i_tell /
393                 p_input->stream.p_selected_area->i_size;
394
395             // Update sliders
396             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
397                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
398
399             // Text char * for updating text controls
400             char *text = new char[OFFSETTOTIME_MAX_SIZE];
401
402             // Create end time text
403             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
404                                 p_area->i_size - p_area->i_tell );
405             text[0] = '-';
406             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
407                 ->PostTextMessage( text );
408
409             // Create time text and update
410             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
411             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
412                 ->PostTextMessage( text );
413
414             // Create total time text
415             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
416             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
417                 ->PostTextMessage( text );
418
419             // Free memory
420             delete[] text;
421
422 #undef p_area
423         }
424         vlc_mutex_unlock( &p_input->stream.stream_lock );
425     }
426     //-------------------------------------------------------------------------
427     vlc_mutex_unlock( &p_intf->change_lock );
428
429     return( VLC_TRUE );
430 }