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