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