]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/skin_main.cpp
* repaired basic_skins
[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.35 2003/06/09 12:33:16 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 #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 #include "dialogs.h"
53
54 #ifndef 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 #ifndef 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 0
236         if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
237 #else
238 #ifdef WIN32
239         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
240                              DIRECTORY_SEPARATOR + "skins" +
241                              DIRECTORY_SEPARATOR + "default" +
242                              DIRECTORY_SEPARATOR + "theme.xml";
243 #else
244 // FIXME: find VLC directory 
245         string default_dir = (string)"./share" +
246                              DIRECTORY_SEPARATOR + "skins" +
247                              DIRECTORY_SEPARATOR + "default" +
248                              DIRECTORY_SEPARATOR + "theme.xml";
249 #endif
250         if( ! Loader->Load( default_dir ) )
251 #endif
252         {
253             // Last chance: the user can  select a new theme file
254 #ifndef BASIC_SKINS
255             wxMutexGuiEnter();
256             wxFileDialog dialog( NULL,
257                 wxU(_("Open a skin file")), wxT(""), wxT(""),
258                 wxT("Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|"
259                     "All files|*.*"), wxOPEN );
260
261             if( dialog.ShowModal() == wxID_OK )
262             {
263                 // try to load selected file
264                 if( ! Loader->Load( (string)dialog.GetPath().mb_str() ) )
265                 {
266                     // He, he, what the hell is he doing ?
267                     delete Loader;
268                     wxMutexGuiLeave();
269                     return;
270                 }
271                 wxMutexGuiLeave();
272             }
273             else
274 #endif
275             {
276                 delete Loader;
277 #ifndef BASIC_SKINS
278                 wxMutexGuiLeave();
279 #endif
280                 return;
281             }
282         }
283     }
284
285     // Show the theme
286     p_intf->p_sys->p_theme->InitTheme();
287     p_intf->p_sys->p_theme->ShowTheme();
288
289     delete Loader;
290
291     msg_Dbg( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
292
293     // Refresh the whole interface
294     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
295
296     OSRun( p_intf );
297
298 #ifndef BASIC_SKINS
299     // clean up the dialog boxes
300     delete p_intf->p_sys->p_dialogs;
301 #endif
302 }
303
304 //---------------------------------------------------------------------------
305 // Module descriptor
306 //---------------------------------------------------------------------------
307 #define DEFAULT_SKIN        N_("Last skin actually used")
308 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
309 #define SKIN_CONFIG         N_("Config of last used skin")
310 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
311 #define SKIN_TRAY           N_("Show application in system tray")
312 #define SKIN_TRAY_LONG      N_("Show application in system tray")
313 #define SKIN_TASKBAR        N_("Show application in taskbar")
314 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
315
316 vlc_module_begin();
317     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
318                 VLC_TRUE );
319     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
320                 VLC_TRUE );
321     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
322               VLC_FALSE );
323     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
324               SKIN_TASKBAR_LONG, VLC_FALSE );
325     set_description( _("Skinnable Interface") );
326     set_capability( "interface", 30 );
327     set_callbacks( Open, Close );
328 vlc_module_end();
329
330
331 //---------------------------------------------------------------------------
332 // Refresh procedure
333 //---------------------------------------------------------------------------
334 int SkinManage( intf_thread_t *p_intf )
335 {
336     vlc_mutex_lock( &p_intf->change_lock );
337
338     // Update the input
339     if( p_intf->p_sys->p_input == NULL )
340     {
341         p_intf->p_sys->p_input = (input_thread_t *)
342                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
343     }
344     else if( p_intf->p_sys->p_input->b_dead )
345     {
346         vlc_object_release( p_intf->p_sys->p_input );
347         p_intf->p_sys->p_input = NULL;
348     }
349
350     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (long)false );
351
352 #ifndef BASIC_SKINS //FIXME
353     // Update the log window
354     p_intf->p_sys->p_dialogs->MessagesDlg->UpdateLog();
355
356     // Update the file info window
357     p_intf->p_sys->p_dialogs->FileInfoDlg->UpdateFileInfo();
358 #endif
359
360     //-------------------------------------------------------------------------
361     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
362     {
363         input_thread_t  * p_input = p_intf->p_sys->p_input;
364
365         vlc_mutex_lock( &p_input->stream.stream_lock );
366
367         // Refresh sound volume
368         audio_volume_t volume;
369
370         // Get sound volume from VLC
371         aout_VolumeGet( p_intf, &volume);
372
373         // Update sliders
374         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
375             (unsigned int)
376             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
377             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
378
379
380         // Refresh slider
381         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
382         if( p_input->stream.b_seekable )
383         {
384 #define p_area p_input->stream.p_selected_area
385
386             // Set value of sliders
387             long Value = SLIDER_RANGE *
388                 p_input->stream.p_selected_area->i_tell /
389                 p_input->stream.p_selected_area->i_size;
390
391             // Update sliders
392             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
393                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
394
395             // Text char * for updating text controls
396             char *text = new char[OFFSETTOTIME_MAX_SIZE];
397
398             // Create end time text
399             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
400                                 p_area->i_size - p_area->i_tell );
401             text[0] = '-';
402             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
403                 ->PostTextMessage( text );
404
405             // Create time text and update
406             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
407             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
408                 ->PostTextMessage( text );
409
410             // Create total time text
411             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
412             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
413                 ->PostTextMessage( text );
414
415             // Free memory
416             delete[] text;
417
418 #undef p_area
419         }
420         vlc_mutex_unlock( &p_input->stream.stream_lock );
421     }
422     //-------------------------------------------------------------------------
423     vlc_mutex_unlock( &p_intf->change_lock );
424
425     return( VLC_TRUE );
426 }